1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GlobalValue & GlobalVariable classes for the VMCore
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/GlobalAlias.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/LeakDetector.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
31 /// it. This involves recursively eliminating any dead users of the
33 static bool removeDeadUsersOfConstant(const Constant *C) {
34 if (isa<GlobalValue>(C)) return false; // Cannot remove this
36 while (!C->use_empty()) {
37 const Constant *User = dyn_cast<Constant>(C->use_back());
38 if (!User) return false; // Non-constant usage;
39 if (!removeDeadUsersOfConstant(User))
40 return false; // Constant wasn't dead
43 const_cast<Constant*>(C)->destroyConstant();
47 /// removeDeadConstantUsers - If there are any dead constant users dangling
48 /// off of this global value, remove them. This method is useful for clients
49 /// that want to check to see if a global is unused, but don't want to deal
50 /// with potentially dead constants hanging off of the globals.
51 void GlobalValue::removeDeadConstantUsers() const {
52 Value::use_const_iterator I = use_begin(), E = use_end();
53 Value::use_const_iterator LastNonDeadUser = E;
55 if (const Constant *User = dyn_cast<Constant>(*I)) {
56 if (!removeDeadUsersOfConstant(User)) {
57 // If the constant wasn't dead, remember that this was the last live use
58 // and move on to the next constant.
62 // If the constant was dead, then the iterator is invalidated.
63 if (LastNonDeadUser == E) {
79 /// Override destroyConstant to make sure it doesn't get called on
80 /// GlobalValue's because they shouldn't be treated like other constants.
81 void GlobalValue::destroyConstant() {
82 llvm_unreachable("You can't GV->destroyConstant()!");
85 /// copyAttributesFrom - copy all additional attributes (those not needed to
86 /// create a GlobalValue) from the GlobalValue Src to this one.
87 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
88 setAlignment(Src->getAlignment());
89 setSection(Src->getSection());
90 setVisibility(Src->getVisibility());
94 //===----------------------------------------------------------------------===//
95 // GlobalVariable Implementation
96 //===----------------------------------------------------------------------===//
98 GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty,
99 bool constant, LinkageTypes Link,
100 Constant *InitVal, const Twine &Name,
101 bool ThreadLocal, unsigned AddressSpace)
102 : GlobalValue(PointerType::get(Ty, AddressSpace),
103 Value::GlobalVariableVal,
104 OperandTraits<GlobalVariable>::op_begin(this),
105 InitVal != 0, Link, Name),
106 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
108 assert(InitVal->getType() == Ty &&
109 "Initializer should be the same type as the GlobalVariable!");
113 LeakDetector::addGarbageObject(this);
116 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
117 LinkageTypes Link, Constant *InitVal,
119 GlobalVariable *Before, bool ThreadLocal,
120 unsigned AddressSpace)
121 : GlobalValue(PointerType::get(Ty, AddressSpace),
122 Value::GlobalVariableVal,
123 OperandTraits<GlobalVariable>::op_begin(this),
124 InitVal != 0, Link, Name),
125 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
127 assert(InitVal->getType() == Ty &&
128 "Initializer should be the same type as the GlobalVariable!");
132 LeakDetector::addGarbageObject(this);
135 Before->getParent()->getGlobalList().insert(Before, this);
137 M.getGlobalList().push_back(this);
140 void GlobalVariable::setParent(Module *parent) {
142 LeakDetector::addGarbageObject(this);
145 LeakDetector::removeGarbageObject(this);
148 void GlobalVariable::removeFromParent() {
149 getParent()->getGlobalList().remove(this);
152 void GlobalVariable::eraseFromParent() {
153 getParent()->getGlobalList().erase(this);
156 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
158 // If you call this, then you better know this GVar has a constant
159 // initializer worth replacing. Enforce that here.
160 assert(getNumOperands() == 1 &&
161 "Attempt to replace uses of Constants on a GVar with no initializer");
163 // And, since you know it has an initializer, the From value better be
164 // the initializer :)
165 assert(getOperand(0) == From &&
166 "Attempt to replace wrong constant initializer in GVar");
168 // And, you better have a constant for the replacement value
169 assert(isa<Constant>(To) &&
170 "Attempt to replace GVar initializer with non-constant");
172 // Okay, preconditions out of the way, replace the constant initializer.
173 this->setOperand(0, cast<Constant>(To));
176 /// copyAttributesFrom - copy all additional attributes (those not needed to
177 /// create a GlobalVariable) from the GlobalVariable Src to this one.
178 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
179 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
180 GlobalValue::copyAttributesFrom(Src);
181 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
182 setThreadLocal(SrcVar->isThreadLocal());
186 //===----------------------------------------------------------------------===//
187 // GlobalAlias Implementation
188 //===----------------------------------------------------------------------===//
190 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
191 const Twine &Name, Constant* aliasee,
192 Module *ParentModule)
193 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
194 LeakDetector::addGarbageObject(this);
197 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
201 ParentModule->getAliasList().push_back(this);
204 void GlobalAlias::setParent(Module *parent) {
206 LeakDetector::addGarbageObject(this);
209 LeakDetector::removeGarbageObject(this);
212 void GlobalAlias::removeFromParent() {
213 getParent()->getAliasList().remove(this);
216 void GlobalAlias::eraseFromParent() {
217 getParent()->getAliasList().erase(this);
220 bool GlobalAlias::isDeclaration() const {
221 const GlobalValue* AV = getAliasedGlobal();
223 return AV->isDeclaration();
228 void GlobalAlias::setAliasee(Constant *Aliasee)
231 assert(Aliasee->getType() == getType() &&
232 "Alias and aliasee types should match!");
234 setOperand(0, Aliasee);
237 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
238 const Constant *C = getAliasee();
240 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
243 const ConstantExpr *CE = 0;
244 if ((CE = dyn_cast<ConstantExpr>(C)) &&
245 (CE->getOpcode() == Instruction::BitCast ||
246 CE->getOpcode() == Instruction::GetElementPtr))
247 return dyn_cast<GlobalValue>(CE->getOperand(0));
249 llvm_unreachable("Unsupported aliasee");
255 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
256 SmallPtrSet<const GlobalValue*, 3> Visited;
258 // Check if we need to stop early.
259 if (stopOnWeak && mayBeOverridden())
262 const GlobalValue *GV = getAliasedGlobal();
265 // Iterate over aliasing chain, stopping on weak alias if necessary.
266 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
267 if (stopOnWeak && GA->mayBeOverridden())
270 GV = GA->getAliasedGlobal();
272 if (!Visited.insert(GV))