[llvm] ce9b89f - [NFC] Refactor GlobalVariable Ctor

Guillaume Chatelet via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 00:30:17 PDT 2023


Author: Guillaume Chatelet
Date: 2023-05-15T07:30:07Z
New Revision: ce9b89f8be59919565ec33ed087c16e8c034ff42

URL: https://github.com/llvm/llvm-project/commit/ce9b89f8be59919565ec33ed087c16e8c034ff42
DIFF: https://github.com/llvm/llvm-project/commit/ce9b89f8be59919565ec33ed087c16e8c034ff42.diff

LOG: [NFC] Refactor GlobalVariable Ctor

Reuse logic from other ctor and remove code duplication.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D150453

Added: 
    

Modified: 
    llvm/lib/IR/Globals.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index a37143fa6916..7bd4503a689e 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -44,9 +44,7 @@ bool GlobalValue::isMaterializable() const {
     return F->isMaterializable();
   return false;
 }
-Error GlobalValue::materialize() {
-  return getParent()->materialize(this);
-}
+Error GlobalValue::materialize() { return getParent()->materialize(this); }
 
 /// Override destroyConstantImpl to make sure it doesn't get called on
 /// GlobalValue's because they shouldn't be treated like other constants.
@@ -438,23 +436,11 @@ GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
                                ThreadLocalMode TLMode,
                                std::optional<unsigned> AddressSpace,
                                bool isExternallyInitialized)
-    : GlobalObject(Ty, Value::GlobalVariableVal,
-                   OperandTraits<GlobalVariable>::op_begin(this),
-                   InitVal != nullptr, Link, Name,
-                   AddressSpace
-                       ? *AddressSpace
-                       : M.getDataLayout().getDefaultGlobalsAddressSpace()),
-      isConstantGlobal(constant),
-      isExternallyInitializedConstant(isExternallyInitialized) {
-  assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
-         "invalid type for global variable");
-  setThreadLocalMode(TLMode);
-  if (InitVal) {
-    assert(InitVal->getType() == Ty &&
-           "Initializer should be the same type as the GlobalVariable!");
-    Op<0>() = InitVal;
-  }
-
+    : GlobalVariable(Ty, constant, Link, InitVal, Name, TLMode,
+                     AddressSpace
+                         ? *AddressSpace
+                         : M.getDataLayout().getDefaultGlobalsAddressSpace(),
+                     isExternallyInitialized) {
   if (Before)
     Before->getParent()->insertGlobalVariable(Before->getIterator(), this);
   else
@@ -545,13 +531,9 @@ GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
   return create(Aliasee->getLinkage(), Name, Aliasee);
 }
 
-void GlobalAlias::removeFromParent() {
-  getParent()->removeAlias(this);
-}
+void GlobalAlias::removeFromParent() { getParent()->removeAlias(this); }
 
-void GlobalAlias::eraseFromParent() {
-  getParent()->eraseAlias(this);
-}
+void GlobalAlias::eraseFromParent() { getParent()->eraseAlias(this); }
 
 void GlobalAlias::setAliasee(Constant *Aliasee) {
   assert((!Aliasee || Aliasee->getType() == getType()) &&


        


More information about the llvm-commits mailing list