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