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/LLVMContext.h"
00020 #include "llvm/Module.h"
00021 #include "llvm/ADT/SmallPtrSet.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 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
00031 /// it.  This involves recursively eliminating any dead users of the
00032 /// constantexpr.
00033 static bool removeDeadUsersOfConstant(const Constant *C) {
00034   if (isa<GlobalValue>(C)) return false; // Cannot remove this
00035 
00036   while (!C->use_empty()) {
00037     const Constant *User = dyn_cast<Constant>(C->use_back());
00038     if (!User) return false; // Non-constant usage;
00039     if (!removeDeadUsersOfConstant(User))
00040       return false; // Constant wasn't dead
00041   }
00042 
00043   const_cast<Constant*>(C)->destroyConstant();
00044   return true;
00045 }
00046 
00047 /// removeDeadConstantUsers - If there are any dead constant users dangling
00048 /// off of this global value, remove them.  This method is useful for clients
00049 /// that want to check to see if a global is unused, but don't want to deal
00050 /// with potentially dead constants hanging off of the globals.
00051 void GlobalValue::removeDeadConstantUsers() const {
00052   Value::use_const_iterator I = use_begin(), E = use_end();
00053   Value::use_const_iterator LastNonDeadUser = E;
00054   while (I != E) {
00055     if (const Constant *User = dyn_cast<Constant>(*I)) {
00056       if (!removeDeadUsersOfConstant(User)) {
00057         // If the constant wasn't dead, remember that this was the last live use
00058         // and move on to the next constant.
00059         LastNonDeadUser = I;
00060         ++I;
00061       } else {
00062         // If the constant was dead, then the iterator is invalidated.
00063         if (LastNonDeadUser == E) {
00064           I = use_begin();
00065           if (I == E) break;
00066         } else {
00067           I = LastNonDeadUser;
00068           ++I;
00069         }
00070       }
00071     } else {
00072       LastNonDeadUser = I;
00073       ++I;
00074     }
00075   }
00076 }
00077 
00078 /// Override destroyConstant to make sure it doesn't get called on
00079 /// GlobalValue's because they shouldn't be treated like other constants.
00080 void GlobalValue::destroyConstant() {
00081   llvm_unreachable("You can't GV->destroyConstant()!");
00082 }
00083 
00084 /// copyAttributesFrom - copy all additional attributes (those not needed to
00085 /// create a GlobalValue) from the GlobalValue Src to this one.
00086 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
00087   setAlignment(Src->getAlignment());
00088   setSection(Src->getSection());
00089   setVisibility(Src->getVisibility());
00090 }
00091 
00092 
00093 //===----------------------------------------------------------------------===//
00094 // GlobalVariable Implementation
00095 //===----------------------------------------------------------------------===//
00096 
00097 GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty,
00098                                bool constant, LinkageTypes Link,
00099                                Constant *InitVal, const Twine &Name,
00100                                bool ThreadLocal, unsigned AddressSpace)
00101   : GlobalValue(PointerType::get(Ty, AddressSpace), 
00102                 Value::GlobalVariableVal,
00103                 OperandTraits<GlobalVariable>::op_begin(this),
00104                 InitVal != 0, Link, Name),
00105     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
00106   if (InitVal) {
00107     assert(InitVal->getType() == Ty &&
00108            "Initializer should be the same type as the GlobalVariable!");
00109     Op<0>() = InitVal;
00110   }
00111 
00112   LeakDetector::addGarbageObject(this);
00113 }
00114 
00115 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
00116                                LinkageTypes Link, Constant *InitVal,
00117                                const Twine &Name,
00118                                GlobalVariable *Before, bool ThreadLocal,
00119                                unsigned AddressSpace)
00120   : GlobalValue(PointerType::get(Ty, AddressSpace), 
00121                 Value::GlobalVariableVal,
00122                 OperandTraits<GlobalVariable>::op_begin(this),
00123                 InitVal != 0, Link, Name),
00124     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
00125   if (InitVal) {
00126     assert(InitVal->getType() == Ty &&
00127            "Initializer should be the same type as the GlobalVariable!");
00128     Op<0>() = InitVal;
00129   }
00130   
00131   LeakDetector::addGarbageObject(this);
00132   
00133   if (Before)
00134     Before->getParent()->getGlobalList().insert(Before, this);
00135   else
00136     M.getGlobalList().push_back(this);
00137 }
00138 
00139 void GlobalVariable::setParent(Module *parent) {
00140   if (getParent())
00141     LeakDetector::addGarbageObject(this);
00142   Parent = parent;
00143   if (getParent())
00144     LeakDetector::removeGarbageObject(this);
00145 }
00146 
00147 void GlobalVariable::removeFromParent() {
00148   getParent()->getGlobalList().remove(this);
00149 }
00150 
00151 void GlobalVariable::eraseFromParent() {
00152   getParent()->getGlobalList().erase(this);
00153 }
00154 
00155 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
00156                                                  Use *U) {
00157   // If you call this, then you better know this GVar has a constant
00158   // initializer worth replacing. Enforce that here.
00159   assert(getNumOperands() == 1 &&
00160          "Attempt to replace uses of Constants on a GVar with no initializer");
00161 
00162   // And, since you know it has an initializer, the From value better be
00163   // the initializer :)
00164   assert(getOperand(0) == From &&
00165          "Attempt to replace wrong constant initializer in GVar");
00166 
00167   // And, you better have a constant for the replacement value
00168   assert(isa<Constant>(To) &&
00169          "Attempt to replace GVar initializer with non-constant");
00170 
00171   // Okay, preconditions out of the way, replace the constant initializer.
00172   this->setOperand(0, cast<Constant>(To));
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(const 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 bool GlobalAlias::isDeclaration() const {
00220   const GlobalValue* AV = getAliasedGlobal();
00221   if (AV)
00222     return AV->isDeclaration();
00223   else
00224     return false;
00225 }
00226 
00227 void GlobalAlias::setAliasee(Constant *Aliasee) 
00228 {
00229   if (Aliasee)
00230     assert(Aliasee->getType() == getType() &&
00231            "Alias and aliasee types should match!");
00232   
00233   setOperand(0, Aliasee);
00234 }
00235 
00236 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
00237   const Constant *C = getAliasee();
00238   if (C) {
00239     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
00240       return GV;
00241     else {
00242       const ConstantExpr *CE = 0;
00243       if ((CE = dyn_cast<ConstantExpr>(C)) &&
00244           (CE->getOpcode() == Instruction::BitCast || 
00245            CE->getOpcode() == Instruction::GetElementPtr))
00246         return dyn_cast<GlobalValue>(CE->getOperand(0));
00247       else
00248         llvm_unreachable("Unsupported aliasee");
00249     }
00250   }
00251   return 0;
00252 }
00253 
00254 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
00255   SmallPtrSet<const GlobalValue*, 3> Visited;
00256 
00257   // Check if we need to stop early.
00258   if (stopOnWeak && mayBeOverridden())
00259     return this;
00260 
00261   const GlobalValue *GV = getAliasedGlobal();
00262   Visited.insert(GV);
00263 
00264   // Iterate over aliasing chain, stopping on weak alias if necessary.
00265   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
00266     if (stopOnWeak && GA->mayBeOverridden())
00267       break;
00268 
00269     GV = GA->getAliasedGlobal();
00270 
00271     if (!Visited.insert(GV))
00272       return NULL;
00273   }
00274 
00275   return GV;
00276 }