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