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