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/Module.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/LeakDetector.h"
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
30 /// it. This involves recursively eliminating any dead users of the
32 static bool removeDeadUsersOfConstant(const Constant *C) {
33 if (isa<GlobalValue>(C)) return false; // Cannot remove this
35 while (!C->use_empty()) {
36 const Constant *User = dyn_cast<Constant>(C->use_back());
37 if (!User) return false; // Non-constant usage;
38 if (!removeDeadUsersOfConstant(User))
39 return false; // Constant wasn't dead
42 const_cast<Constant*>(C)->destroyConstant();
46 /// removeDeadConstantUsers - If there are any dead constant users dangling
47 /// off of this global value, remove them. This method is useful for clients
48 /// that want to check to see if a global is unused, but don't want to deal
49 /// with potentially dead constants hanging off of the globals.
50 void GlobalValue::removeDeadConstantUsers() const {
51 Value::use_const_iterator I = use_begin(), E = use_end();
52 Value::use_const_iterator LastNonDeadUser = E;
54 if (const Constant *User = dyn_cast<Constant>(*I)) {
55 if (!removeDeadUsersOfConstant(User)) {
56 // If the constant wasn't dead, remember that this was the last live use
57 // and move on to the next constant.
61 // If the constant was dead, then the iterator is invalidated.
62 if (LastNonDeadUser == E) {
78 /// Override destroyConstant to make sure it doesn't get called on
79 /// GlobalValue's because they shouldn't be treated like other constants.
80 void GlobalValue::destroyConstant() {
81 llvm_unreachable("You can't GV->destroyConstant()!");
84 /// copyAttributesFrom - copy all additional attributes (those not needed to
85 /// create a GlobalValue) from the GlobalValue Src to this one.
86 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
87 setAlignment(Src->getAlignment());
88 setSection(Src->getSection());
89 setVisibility(Src->getVisibility());
93 //===----------------------------------------------------------------------===//
94 // GlobalVariable Implementation
95 //===----------------------------------------------------------------------===//
97 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
98 Constant *InitVal, const Twine &Name,
99 bool ThreadLocal, unsigned AddressSpace)
100 : GlobalValue(PointerType::get(Ty, AddressSpace),
101 Value::GlobalVariableVal,
102 OperandTraits<GlobalVariable>::op_begin(this),
103 InitVal != 0, Link, Name),
104 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
106 assert(InitVal->getType() == Ty &&
107 "Initializer should be the same type as the GlobalVariable!");
111 LeakDetector::addGarbageObject(this);
114 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
115 LinkageTypes Link, Constant *InitVal,
117 GlobalVariable *Before, bool ThreadLocal,
118 unsigned AddressSpace)
119 : GlobalValue(PointerType::get(Ty, AddressSpace),
120 Value::GlobalVariableVal,
121 OperandTraits<GlobalVariable>::op_begin(this),
122 InitVal != 0, Link, Name),
123 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
125 assert(InitVal->getType() == Ty &&
126 "Initializer should be the same type as the GlobalVariable!");
130 LeakDetector::addGarbageObject(this);
133 Before->getParent()->getGlobalList().insert(Before, this);
135 M.getGlobalList().push_back(this);
138 void GlobalVariable::setParent(Module *parent) {
140 LeakDetector::addGarbageObject(this);
143 LeakDetector::removeGarbageObject(this);
146 void GlobalVariable::removeFromParent() {
147 getParent()->getGlobalList().remove(this);
150 void GlobalVariable::eraseFromParent() {
151 getParent()->getGlobalList().erase(this);
154 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
156 // If you call this, then you better know this GVar has a constant
157 // initializer worth replacing. Enforce that here.
158 assert(getNumOperands() == 1 &&
159 "Attempt to replace uses of Constants on a GVar with no initializer");
161 // And, since you know it has an initializer, the From value better be
162 // the initializer :)
163 assert(getOperand(0) == From &&
164 "Attempt to replace wrong constant initializer in GVar");
166 // And, you better have a constant for the replacement value
167 assert(isa<Constant>(To) &&
168 "Attempt to replace GVar initializer with non-constant");
170 // Okay, preconditions out of the way, replace the constant initializer.
171 this->setOperand(0, cast<Constant>(To));
174 /// copyAttributesFrom - copy all additional attributes (those not needed to
175 /// create a GlobalVariable) from the GlobalVariable Src to this one.
176 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
177 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
178 GlobalValue::copyAttributesFrom(Src);
179 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
180 setThreadLocal(SrcVar->isThreadLocal());
184 //===----------------------------------------------------------------------===//
185 // GlobalAlias Implementation
186 //===----------------------------------------------------------------------===//
188 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
189 const Twine &Name, Constant* aliasee,
190 Module *ParentModule)
191 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
192 LeakDetector::addGarbageObject(this);
195 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
199 ParentModule->getAliasList().push_back(this);
202 void GlobalAlias::setParent(Module *parent) {
204 LeakDetector::addGarbageObject(this);
207 LeakDetector::removeGarbageObject(this);
210 void GlobalAlias::removeFromParent() {
211 getParent()->getAliasList().remove(this);
214 void GlobalAlias::eraseFromParent() {
215 getParent()->getAliasList().erase(this);
218 bool GlobalAlias::isDeclaration() const {
219 const GlobalValue* AV = getAliasedGlobal();
221 return AV->isDeclaration();
226 void GlobalAlias::setAliasee(Constant *Aliasee)
229 assert(Aliasee->getType() == getType() &&
230 "Alias and aliasee types should match!");
232 setOperand(0, Aliasee);
235 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
236 const Constant *C = getAliasee();
238 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
241 const ConstantExpr *CE = 0;
242 if ((CE = dyn_cast<ConstantExpr>(C)) &&
243 (CE->getOpcode() == Instruction::BitCast ||
244 CE->getOpcode() == Instruction::GetElementPtr))
245 return dyn_cast<GlobalValue>(CE->getOperand(0));
247 llvm_unreachable("Unsupported aliasee");
253 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
254 SmallPtrSet<const GlobalValue*, 3> Visited;
256 // Check if we need to stop early.
257 if (stopOnWeak && mayBeOverridden())
260 const GlobalValue *GV = getAliasedGlobal();
263 // Iterate over aliasing chain, stopping on weak alias if necessary.
264 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
265 if (stopOnWeak && GA->mayBeOverridden())
268 GV = GA->getAliasedGlobal();
270 if (!Visited.insert(GV))