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