Home | History | Annotate | Download | only in IR
      1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the GlobalValue & GlobalVariable classes for the IR
     11 // library.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm/IR/GlobalValue.h"
     16 #include "llvm/ADT/SmallPtrSet.h"
     17 #include "llvm/IR/Constants.h"
     18 #include "llvm/IR/DerivedTypes.h"
     19 #include "llvm/IR/GlobalAlias.h"
     20 #include "llvm/IR/GlobalVariable.h"
     21 #include "llvm/IR/LeakDetector.h"
     22 #include "llvm/IR/Module.h"
     23 #include "llvm/IR/Operator.h"
     24 #include "llvm/Support/ErrorHandling.h"
     25 using namespace llvm;
     26 
     27 //===----------------------------------------------------------------------===//
     28 //                            GlobalValue Class
     29 //===----------------------------------------------------------------------===//
     30 
     31 bool GlobalValue::isMaterializable() const {
     32   return getParent() && getParent()->isMaterializable(this);
     33 }
     34 bool GlobalValue::isDematerializable() const {
     35   return getParent() && getParent()->isDematerializable(this);
     36 }
     37 bool GlobalValue::Materialize(std::string *ErrInfo) {
     38   return getParent()->Materialize(this, ErrInfo);
     39 }
     40 void GlobalValue::Dematerialize() {
     41   getParent()->Dematerialize(this);
     42 }
     43 
     44 const DataLayout *GlobalValue::getDataLayout() const {
     45   return getParent()->getDataLayout();
     46 }
     47 
     48 /// Override destroyConstant to make sure it doesn't get called on
     49 /// GlobalValue's because they shouldn't be treated like other constants.
     50 void GlobalValue::destroyConstant() {
     51   llvm_unreachable("You can't GV->destroyConstant()!");
     52 }
     53 
     54 /// copyAttributesFrom - copy all additional attributes (those not needed to
     55 /// create a GlobalValue) from the GlobalValue Src to this one.
     56 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
     57   setVisibility(Src->getVisibility());
     58   setUnnamedAddr(Src->hasUnnamedAddr());
     59   setDLLStorageClass(Src->getDLLStorageClass());
     60 }
     61 
     62 unsigned GlobalValue::getAlignment() const {
     63   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
     64     // In general we cannot compute this at the IR level, but we try.
     65     if (const GlobalObject *GO = GA->getBaseObject())
     66       return GO->getAlignment();
     67 
     68     // FIXME: we should also be able to handle:
     69     // Alias = Global + Offset
     70     // Alias = Absolute
     71     return 0;
     72   }
     73   return cast<GlobalObject>(this)->getAlignment();
     74 }
     75 
     76 void GlobalObject::setAlignment(unsigned Align) {
     77   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
     78   assert(Align <= MaximumAlignment &&
     79          "Alignment is greater than MaximumAlignment!");
     80   setGlobalValueSubClassData(Log2_32(Align) + 1);
     81   assert(getAlignment() == Align && "Alignment representation error!");
     82 }
     83 
     84 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
     85   const auto *GV = cast<GlobalObject>(Src);
     86   GlobalValue::copyAttributesFrom(GV);
     87   setAlignment(GV->getAlignment());
     88   setSection(GV->getSection());
     89 }
     90 
     91 const char *GlobalValue::getSection() const {
     92   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
     93     // In general we cannot compute this at the IR level, but we try.
     94     if (const GlobalObject *GO = GA->getBaseObject())
     95       return GO->getSection();
     96     return "";
     97   }
     98   return cast<GlobalObject>(this)->getSection();
     99 }
    100 
    101 Comdat *GlobalValue::getComdat() {
    102   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
    103     // In general we cannot compute this at the IR level, but we try.
    104     if (const GlobalObject *GO = GA->getBaseObject())
    105       return const_cast<GlobalObject *>(GO)->getComdat();
    106     return nullptr;
    107   }
    108   return cast<GlobalObject>(this)->getComdat();
    109 }
    110 
    111 void GlobalObject::setSection(StringRef S) { Section = S; }
    112 
    113 bool GlobalValue::isDeclaration() const {
    114   // Globals are definitions if they have an initializer.
    115   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
    116     return GV->getNumOperands() == 0;
    117 
    118   // Functions are definitions if they have a body.
    119   if (const Function *F = dyn_cast<Function>(this))
    120     return F->empty();
    121 
    122   // Aliases are always definitions.
    123   assert(isa<GlobalAlias>(this));
    124   return false;
    125 }
    126 
    127 //===----------------------------------------------------------------------===//
    128 // GlobalVariable Implementation
    129 //===----------------------------------------------------------------------===//
    130 
    131 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
    132                                Constant *InitVal, const Twine &Name,
    133                                ThreadLocalMode TLMode, unsigned AddressSpace,
    134                                bool isExternallyInitialized)
    135     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
    136                    OperandTraits<GlobalVariable>::op_begin(this),
    137                    InitVal != nullptr, Link, Name),
    138       isConstantGlobal(constant),
    139       isExternallyInitializedConstant(isExternallyInitialized) {
    140   setThreadLocalMode(TLMode);
    141   if (InitVal) {
    142     assert(InitVal->getType() == Ty &&
    143            "Initializer should be the same type as the GlobalVariable!");
    144     Op<0>() = InitVal;
    145   }
    146 
    147   LeakDetector::addGarbageObject(this);
    148 }
    149 
    150 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
    151                                LinkageTypes Link, Constant *InitVal,
    152                                const Twine &Name, GlobalVariable *Before,
    153                                ThreadLocalMode TLMode, unsigned AddressSpace,
    154                                bool isExternallyInitialized)
    155     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
    156                    OperandTraits<GlobalVariable>::op_begin(this),
    157                    InitVal != nullptr, Link, Name),
    158       isConstantGlobal(constant),
    159       isExternallyInitializedConstant(isExternallyInitialized) {
    160   setThreadLocalMode(TLMode);
    161   if (InitVal) {
    162     assert(InitVal->getType() == Ty &&
    163            "Initializer should be the same type as the GlobalVariable!");
    164     Op<0>() = InitVal;
    165   }
    166 
    167   LeakDetector::addGarbageObject(this);
    168 
    169   if (Before)
    170     Before->getParent()->getGlobalList().insert(Before, this);
    171   else
    172     M.getGlobalList().push_back(this);
    173 }
    174 
    175 void GlobalVariable::setParent(Module *parent) {
    176   if (getParent())
    177     LeakDetector::addGarbageObject(this);
    178   Parent = parent;
    179   if (getParent())
    180     LeakDetector::removeGarbageObject(this);
    181 }
    182 
    183 void GlobalVariable::removeFromParent() {
    184   getParent()->getGlobalList().remove(this);
    185 }
    186 
    187 void GlobalVariable::eraseFromParent() {
    188   getParent()->getGlobalList().erase(this);
    189 }
    190 
    191 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
    192                                                  Use *U) {
    193   // If you call this, then you better know this GVar has a constant
    194   // initializer worth replacing. Enforce that here.
    195   assert(getNumOperands() == 1 &&
    196          "Attempt to replace uses of Constants on a GVar with no initializer");
    197 
    198   // And, since you know it has an initializer, the From value better be
    199   // the initializer :)
    200   assert(getOperand(0) == From &&
    201          "Attempt to replace wrong constant initializer in GVar");
    202 
    203   // And, you better have a constant for the replacement value
    204   assert(isa<Constant>(To) &&
    205          "Attempt to replace GVar initializer with non-constant");
    206 
    207   // Okay, preconditions out of the way, replace the constant initializer.
    208   this->setOperand(0, cast<Constant>(To));
    209 }
    210 
    211 void GlobalVariable::setInitializer(Constant *InitVal) {
    212   if (!InitVal) {
    213     if (hasInitializer()) {
    214       Op<0>().set(nullptr);
    215       NumOperands = 0;
    216     }
    217   } else {
    218     assert(InitVal->getType() == getType()->getElementType() &&
    219            "Initializer type must match GlobalVariable type");
    220     if (!hasInitializer())
    221       NumOperands = 1;
    222     Op<0>().set(InitVal);
    223   }
    224 }
    225 
    226 /// copyAttributesFrom - copy all additional attributes (those not needed to
    227 /// create a GlobalVariable) from the GlobalVariable Src to this one.
    228 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
    229   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
    230   GlobalObject::copyAttributesFrom(Src);
    231   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
    232   setThreadLocalMode(SrcVar->getThreadLocalMode());
    233 }
    234 
    235 
    236 //===----------------------------------------------------------------------===//
    237 // GlobalAlias Implementation
    238 //===----------------------------------------------------------------------===//
    239 
    240 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
    241                          const Twine &Name, Constant *Aliasee,
    242                          Module *ParentModule)
    243     : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
    244                   &Op<0>(), 1, Link, Name) {
    245   LeakDetector::addGarbageObject(this);
    246   Op<0>() = Aliasee;
    247 
    248   if (ParentModule)
    249     ParentModule->getAliasList().push_back(this);
    250 }
    251 
    252 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
    253                                  LinkageTypes Link, const Twine &Name,
    254                                  Constant *Aliasee, Module *ParentModule) {
    255   return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
    256 }
    257 
    258 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
    259                                  LinkageTypes Linkage, const Twine &Name,
    260                                  Module *Parent) {
    261   return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
    262 }
    263 
    264 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
    265                                  LinkageTypes Linkage, const Twine &Name,
    266                                  GlobalValue *Aliasee) {
    267   return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
    268 }
    269 
    270 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
    271                                  GlobalValue *Aliasee) {
    272   PointerType *PTy = Aliasee->getType();
    273   return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
    274                 Aliasee);
    275 }
    276 
    277 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
    278   return create(Aliasee->getLinkage(), Name, Aliasee);
    279 }
    280 
    281 void GlobalAlias::setParent(Module *parent) {
    282   if (getParent())
    283     LeakDetector::addGarbageObject(this);
    284   Parent = parent;
    285   if (getParent())
    286     LeakDetector::removeGarbageObject(this);
    287 }
    288 
    289 void GlobalAlias::removeFromParent() {
    290   getParent()->getAliasList().remove(this);
    291 }
    292 
    293 void GlobalAlias::eraseFromParent() {
    294   getParent()->getAliasList().erase(this);
    295 }
    296 
    297 void GlobalAlias::setAliasee(Constant *Aliasee) {
    298   assert((!Aliasee || Aliasee->getType() == getType()) &&
    299          "Alias and aliasee types should match!");
    300   setOperand(0, Aliasee);
    301 }
    302