LLVM API Documentation

Globals.cpp
Go to the documentation of this file.
00001 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the GlobalValue & GlobalVariable classes for the IR
00011 // library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/IR/GlobalValue.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/DerivedTypes.h"
00019 #include "llvm/IR/GlobalAlias.h"
00020 #include "llvm/IR/GlobalVariable.h"
00021 #include "llvm/IR/LeakDetector.h"
00022 #include "llvm/IR/Module.h"
00023 #include "llvm/IR/Operator.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 using namespace llvm;
00026 
00027 //===----------------------------------------------------------------------===//
00028 //                            GlobalValue Class
00029 //===----------------------------------------------------------------------===//
00030 
00031 bool GlobalValue::isMaterializable() const {
00032   if (const Function *F = dyn_cast<Function>(this))
00033     return F->isMaterializable();
00034   return false;
00035 }
00036 bool GlobalValue::isDematerializable() const {
00037   return getParent() && getParent()->isDematerializable(this);
00038 }
00039 std::error_code GlobalValue::materialize() {
00040   return getParent()->materialize(this);
00041 }
00042 void GlobalValue::Dematerialize() {
00043   getParent()->Dematerialize(this);
00044 }
00045 
00046 const DataLayout *GlobalValue::getDataLayout() const {
00047   return getParent()->getDataLayout();
00048 }
00049 
00050 /// Override destroyConstant to make sure it doesn't get called on
00051 /// GlobalValue's because they shouldn't be treated like other constants.
00052 void GlobalValue::destroyConstant() {
00053   llvm_unreachable("You can't GV->destroyConstant()!");
00054 }
00055 
00056 /// copyAttributesFrom - copy all additional attributes (those not needed to
00057 /// create a GlobalValue) from the GlobalValue Src to this one.
00058 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
00059   setVisibility(Src->getVisibility());
00060   setUnnamedAddr(Src->hasUnnamedAddr());
00061   setDLLStorageClass(Src->getDLLStorageClass());
00062 }
00063 
00064 unsigned GlobalValue::getAlignment() const {
00065   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00066     // In general we cannot compute this at the IR level, but we try.
00067     if (const GlobalObject *GO = GA->getBaseObject())
00068       return GO->getAlignment();
00069 
00070     // FIXME: we should also be able to handle:
00071     // Alias = Global + Offset
00072     // Alias = Absolute
00073     return 0;
00074   }
00075   return cast<GlobalObject>(this)->getAlignment();
00076 }
00077 
00078 void GlobalObject::setAlignment(unsigned Align) {
00079   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
00080   assert(Align <= MaximumAlignment &&
00081          "Alignment is greater than MaximumAlignment!");
00082   unsigned AlignmentData = Log2_32(Align) + 1;
00083   unsigned OldData = getGlobalValueSubClassData();
00084   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
00085   assert(getAlignment() == Align && "Alignment representation error!");
00086 }
00087 
00088 unsigned GlobalObject::getGlobalObjectSubClassData() const {
00089   unsigned ValueData = getGlobalValueSubClassData();
00090   return ValueData >> AlignmentBits;
00091 }
00092 
00093 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
00094   unsigned OldData = getGlobalValueSubClassData();
00095   setGlobalValueSubClassData((OldData & AlignmentMask) |
00096                              (Val << AlignmentBits));
00097   assert(getGlobalObjectSubClassData() == Val && "representation error");
00098 }
00099 
00100 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
00101   const auto *GV = cast<GlobalObject>(Src);
00102   GlobalValue::copyAttributesFrom(GV);
00103   setAlignment(GV->getAlignment());
00104   setSection(GV->getSection());
00105 }
00106 
00107 const char *GlobalValue::getSection() const {
00108   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00109     // In general we cannot compute this at the IR level, but we try.
00110     if (const GlobalObject *GO = GA->getBaseObject())
00111       return GO->getSection();
00112     return "";
00113   }
00114   return cast<GlobalObject>(this)->getSection();
00115 }
00116 
00117 Comdat *GlobalValue::getComdat() {
00118   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00119     // In general we cannot compute this at the IR level, but we try.
00120     if (const GlobalObject *GO = GA->getBaseObject())
00121       return const_cast<GlobalObject *>(GO)->getComdat();
00122     return nullptr;
00123   }
00124   return cast<GlobalObject>(this)->getComdat();
00125 }
00126 
00127 void GlobalObject::setSection(StringRef S) { Section = S; }
00128 
00129 bool GlobalValue::isDeclaration() const {
00130   // Globals are definitions if they have an initializer.
00131   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
00132     return GV->getNumOperands() == 0;
00133 
00134   // Functions are definitions if they have a body.
00135   if (const Function *F = dyn_cast<Function>(this))
00136     return F->empty() && !F->isMaterializable();
00137 
00138   // Aliases are always definitions.
00139   assert(isa<GlobalAlias>(this));
00140   return false;
00141 }
00142 
00143 //===----------------------------------------------------------------------===//
00144 // GlobalVariable Implementation
00145 //===----------------------------------------------------------------------===//
00146 
00147 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
00148                                Constant *InitVal, const Twine &Name,
00149                                ThreadLocalMode TLMode, unsigned AddressSpace,
00150                                bool isExternallyInitialized)
00151     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
00152                    OperandTraits<GlobalVariable>::op_begin(this),
00153                    InitVal != nullptr, Link, Name),
00154       isConstantGlobal(constant),
00155       isExternallyInitializedConstant(isExternallyInitialized) {
00156   setThreadLocalMode(TLMode);
00157   if (InitVal) {
00158     assert(InitVal->getType() == Ty &&
00159            "Initializer should be the same type as the GlobalVariable!");
00160     Op<0>() = InitVal;
00161   }
00162 
00163   LeakDetector::addGarbageObject(this);
00164 }
00165 
00166 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
00167                                LinkageTypes Link, Constant *InitVal,
00168                                const Twine &Name, GlobalVariable *Before,
00169                                ThreadLocalMode TLMode, unsigned AddressSpace,
00170                                bool isExternallyInitialized)
00171     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
00172                    OperandTraits<GlobalVariable>::op_begin(this),
00173                    InitVal != nullptr, Link, Name),
00174       isConstantGlobal(constant),
00175       isExternallyInitializedConstant(isExternallyInitialized) {
00176   setThreadLocalMode(TLMode);
00177   if (InitVal) {
00178     assert(InitVal->getType() == Ty &&
00179            "Initializer should be the same type as the GlobalVariable!");
00180     Op<0>() = InitVal;
00181   }
00182 
00183   LeakDetector::addGarbageObject(this);
00184 
00185   if (Before)
00186     Before->getParent()->getGlobalList().insert(Before, this);
00187   else
00188     M.getGlobalList().push_back(this);
00189 }
00190 
00191 void GlobalVariable::setParent(Module *parent) {
00192   if (getParent())
00193     LeakDetector::addGarbageObject(this);
00194   Parent = parent;
00195   if (getParent())
00196     LeakDetector::removeGarbageObject(this);
00197 }
00198 
00199 void GlobalVariable::removeFromParent() {
00200   getParent()->getGlobalList().remove(this);
00201 }
00202 
00203 void GlobalVariable::eraseFromParent() {
00204   getParent()->getGlobalList().erase(this);
00205 }
00206 
00207 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
00208                                                  Use *U) {
00209   // If you call this, then you better know this GVar has a constant
00210   // initializer worth replacing. Enforce that here.
00211   assert(getNumOperands() == 1 &&
00212          "Attempt to replace uses of Constants on a GVar with no initializer");
00213 
00214   // And, since you know it has an initializer, the From value better be
00215   // the initializer :)
00216   assert(getOperand(0) == From &&
00217          "Attempt to replace wrong constant initializer in GVar");
00218 
00219   // And, you better have a constant for the replacement value
00220   assert(isa<Constant>(To) &&
00221          "Attempt to replace GVar initializer with non-constant");
00222 
00223   // Okay, preconditions out of the way, replace the constant initializer.
00224   this->setOperand(0, cast<Constant>(To));
00225 }
00226 
00227 void GlobalVariable::setInitializer(Constant *InitVal) {
00228   if (!InitVal) {
00229     if (hasInitializer()) {
00230       Op<0>().set(nullptr);
00231       NumOperands = 0;
00232     }
00233   } else {
00234     assert(InitVal->getType() == getType()->getElementType() &&
00235            "Initializer type must match GlobalVariable type");
00236     if (!hasInitializer())
00237       NumOperands = 1;
00238     Op<0>().set(InitVal);
00239   }
00240 }
00241 
00242 /// copyAttributesFrom - copy all additional attributes (those not needed to
00243 /// create a GlobalVariable) from the GlobalVariable Src to this one.
00244 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
00245   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
00246   GlobalObject::copyAttributesFrom(Src);
00247   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
00248   setThreadLocalMode(SrcVar->getThreadLocalMode());
00249 }
00250 
00251 
00252 //===----------------------------------------------------------------------===//
00253 // GlobalAlias Implementation
00254 //===----------------------------------------------------------------------===//
00255 
00256 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
00257                          const Twine &Name, Constant *Aliasee,
00258                          Module *ParentModule)
00259     : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
00260                   &Op<0>(), 1, Link, Name) {
00261   LeakDetector::addGarbageObject(this);
00262   Op<0>() = Aliasee;
00263 
00264   if (ParentModule)
00265     ParentModule->getAliasList().push_back(this);
00266 }
00267 
00268 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
00269                                  LinkageTypes Link, const Twine &Name,
00270                                  Constant *Aliasee, Module *ParentModule) {
00271   return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
00272 }
00273 
00274 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
00275                                  LinkageTypes Linkage, const Twine &Name,
00276                                  Module *Parent) {
00277   return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
00278 }
00279 
00280 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
00281                                  LinkageTypes Linkage, const Twine &Name,
00282                                  GlobalValue *Aliasee) {
00283   return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
00284 }
00285 
00286 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
00287                                  GlobalValue *Aliasee) {
00288   PointerType *PTy = Aliasee->getType();
00289   return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
00290                 Aliasee);
00291 }
00292 
00293 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
00294   return create(Aliasee->getLinkage(), Name, Aliasee);
00295 }
00296 
00297 void GlobalAlias::setParent(Module *parent) {
00298   if (getParent())
00299     LeakDetector::addGarbageObject(this);
00300   Parent = parent;
00301   if (getParent())
00302     LeakDetector::removeGarbageObject(this);
00303 }
00304 
00305 void GlobalAlias::removeFromParent() {
00306   getParent()->getAliasList().remove(this);
00307 }
00308 
00309 void GlobalAlias::eraseFromParent() {
00310   getParent()->getAliasList().erase(this);
00311 }
00312 
00313 void GlobalAlias::setAliasee(Constant *Aliasee) {
00314   assert((!Aliasee || Aliasee->getType() == getType()) &&
00315          "Alias and aliasee types should match!");
00316   setOperand(0, Aliasee);
00317 }