Globals.cpp revision 86062afbc49b549a653d38fb03702051f3917df7
1//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the GlobalValue & GlobalVariable classes for the VMCore 11// library. 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/Constants.h" 16#include "llvm/GlobalVariable.h" 17#include "llvm/GlobalAlias.h" 18#include "llvm/DerivedTypes.h" 19#include "llvm/Module.h" 20#include "llvm/ADT/SmallPtrSet.h" 21#include "llvm/Support/LeakDetector.h" 22using namespace llvm; 23 24//===----------------------------------------------------------------------===// 25// GlobalValue Class 26//===----------------------------------------------------------------------===// 27 28/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove 29/// it. This involves recursively eliminating any dead users of the 30/// constantexpr. 31static bool removeDeadUsersOfConstant(Constant *C) { 32 if (isa<GlobalValue>(C)) return false; // Cannot remove this 33 34 while (!C->use_empty()) { 35 Constant *User = dyn_cast<Constant>(C->use_back()); 36 if (!User) return false; // Non-constant usage; 37 if (!removeDeadUsersOfConstant(User)) 38 return false; // Constant wasn't dead 39 } 40 41 C->destroyConstant(); 42 return true; 43} 44 45/// removeDeadConstantUsers - If there are any dead constant users dangling 46/// off of this global value, remove them. This method is useful for clients 47/// that want to check to see if a global is unused, but don't want to deal 48/// with potentially dead constants hanging off of the globals. 49void GlobalValue::removeDeadConstantUsers() { 50 Value::use_iterator I = use_begin(), E = use_end(); 51 Value::use_iterator LastNonDeadUser = E; 52 while (I != E) { 53 if (Constant *User = dyn_cast<Constant>(*I)) { 54 if (!removeDeadUsersOfConstant(User)) { 55 // If the constant wasn't dead, remember that this was the last live use 56 // and move on to the next constant. 57 LastNonDeadUser = I; 58 ++I; 59 } else { 60 // If the constant was dead, then the iterator is invalidated. 61 if (LastNonDeadUser == E) { 62 I = use_begin(); 63 if (I == E) break; 64 } else { 65 I = LastNonDeadUser; 66 ++I; 67 } 68 } 69 } else { 70 LastNonDeadUser = I; 71 ++I; 72 } 73 } 74} 75 76/// Override destroyConstant to make sure it doesn't get called on 77/// GlobalValue's because they shouldn't be treated like other constants. 78void GlobalValue::destroyConstant() { 79 assert(0 && "You can't GV->destroyConstant()!"); 80 abort(); 81} 82 83/// copyAttributesFrom - copy all additional attributes (those not needed to 84/// create a GlobalValue) from the GlobalValue Src to this one. 85void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { 86 setAlignment(Src->getAlignment()); 87 setSection(Src->getSection()); 88 setVisibility(Src->getVisibility()); 89} 90 91 92//===----------------------------------------------------------------------===// 93// GlobalVariable Implementation 94//===----------------------------------------------------------------------===// 95 96GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, 97 Constant *InitVal, const std::string &Name, 98 Module *ParentModule, bool ThreadLocal, 99 unsigned AddressSpace) 100 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, 101 OperandTraits<GlobalVariable>::op_begin(this), 102 InitVal != 0, Link, Name), 103 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { 104 if (InitVal) { 105 assert(InitVal->getType() == Ty && 106 "Initializer should be the same type as the GlobalVariable!"); 107 Op<0>() = InitVal; 108 } 109 110 LeakDetector::addGarbageObject(this); 111 112 if (ParentModule) 113 ParentModule->getGlobalList().push_back(this); 114} 115 116GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, 117 Constant *InitVal, const std::string &Name, 118 GlobalVariable *Before, bool ThreadLocal, 119 unsigned AddressSpace) 120 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, 121 OperandTraits<GlobalVariable>::op_begin(this), 122 InitVal != 0, Link, Name), 123 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { 124 if (InitVal) { 125 assert(InitVal->getType() == Ty && 126 "Initializer should be the same type as the GlobalVariable!"); 127 Op<0>() = InitVal; 128 } 129 130 LeakDetector::addGarbageObject(this); 131 132 if (Before) 133 Before->getParent()->getGlobalList().insert(Before, this); 134} 135 136void GlobalVariable::setParent(Module *parent) { 137 if (getParent()) 138 LeakDetector::addGarbageObject(this); 139 Parent = parent; 140 if (getParent()) 141 LeakDetector::removeGarbageObject(this); 142} 143 144void GlobalVariable::removeFromParent() { 145 getParent()->getGlobalList().remove(this); 146} 147 148void GlobalVariable::eraseFromParent() { 149 getParent()->getGlobalList().erase(this); 150} 151 152void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, 153 Use *U) { 154 // If you call this, then you better know this GVar has a constant 155 // initializer worth replacing. Enforce that here. 156 assert(getNumOperands() == 1 && 157 "Attempt to replace uses of Constants on a GVar with no initializer"); 158 159 // And, since you know it has an initializer, the From value better be 160 // the initializer :) 161 assert(getOperand(0) == From && 162 "Attempt to replace wrong constant initializer in GVar"); 163 164 // And, you better have a constant for the replacement value 165 assert(isa<Constant>(To) && 166 "Attempt to replace GVar initializer with non-constant"); 167 168 // Okay, preconditions out of the way, replace the constant initializer. 169 this->setOperand(0, cast<Constant>(To)); 170} 171 172/// copyAttributesFrom - copy all additional attributes (those not needed to 173/// create a GlobalVariable) from the GlobalVariable Src to this one. 174void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { 175 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!"); 176 GlobalValue::copyAttributesFrom(Src); 177 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src); 178 setThreadLocal(SrcVar->isThreadLocal()); 179} 180 181 182//===----------------------------------------------------------------------===// 183// GlobalAlias Implementation 184//===----------------------------------------------------------------------===// 185 186GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link, 187 const std::string &Name, Constant* aliasee, 188 Module *ParentModule) 189 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { 190 LeakDetector::addGarbageObject(this); 191 192 if (aliasee) 193 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); 194 Op<0>() = aliasee; 195 196 if (ParentModule) 197 ParentModule->getAliasList().push_back(this); 198} 199 200void GlobalAlias::setParent(Module *parent) { 201 if (getParent()) 202 LeakDetector::addGarbageObject(this); 203 Parent = parent; 204 if (getParent()) 205 LeakDetector::removeGarbageObject(this); 206} 207 208void GlobalAlias::removeFromParent() { 209 getParent()->getAliasList().remove(this); 210} 211 212void GlobalAlias::eraseFromParent() { 213 getParent()->getAliasList().erase(this); 214} 215 216bool GlobalAlias::isDeclaration() const { 217 const GlobalValue* AV = getAliasedGlobal(); 218 if (AV) 219 return AV->isDeclaration(); 220 else 221 return false; 222} 223 224void GlobalAlias::setAliasee(Constant *Aliasee) 225{ 226 if (Aliasee) 227 assert(Aliasee->getType() == getType() && 228 "Alias and aliasee types should match!"); 229 230 setOperand(0, Aliasee); 231} 232 233const GlobalValue *GlobalAlias::getAliasedGlobal() const { 234 const Constant *C = getAliasee(); 235 if (C) { 236 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 237 return GV; 238 else { 239 const ConstantExpr *CE = 0; 240 if ((CE = dyn_cast<ConstantExpr>(C)) && 241 (CE->getOpcode() == Instruction::BitCast || 242 CE->getOpcode() == Instruction::GetElementPtr)) 243 return dyn_cast<GlobalValue>(CE->getOperand(0)); 244 else 245 assert(0 && "Unsupported aliasee"); 246 } 247 } 248 return 0; 249} 250 251const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const { 252 SmallPtrSet<const GlobalValue*, 3> Visited; 253 254 // Check if we need to stop early. 255 if (stopOnWeak && mayBeOverridden()) 256 return this; 257 258 const GlobalValue *GV = getAliasedGlobal(); 259 Visited.insert(GV); 260 261 // Iterate over aliasing chain, stopping on weak alias if necessary. 262 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) { 263 if (stopOnWeak && GA->mayBeOverridden()) 264 break; 265 266 GV = GA->getAliasedGlobal(); 267 268 if (!Visited.insert(GV)) 269 return NULL; 270 } 271 272 return GV; 273} 274