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