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