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) {
78 /// removeDeadBlockAddress - If there is a blockaddress node for this basic
79 /// block, try to remove it and any dead constant users of it.
80 void BasicBlock::removeDeadBlockAddress() {
81 if (!hasAddressTaken()) return;
82 removeDeadUsersOfConstant(BlockAddress::get(this));
86 /// Override destroyConstant to make sure it doesn't get called on
87 /// GlobalValue's because they shouldn't be treated like other constants.
88 void GlobalValue::destroyConstant() {
89 llvm_unreachable("You can't GV->destroyConstant()!");
92 /// copyAttributesFrom - copy all additional attributes (those not needed to
93 /// create a GlobalValue) from the GlobalValue Src to this one.
94 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
95 setAlignment(Src->getAlignment());
96 setSection(Src->getSection());
97 setVisibility(Src->getVisibility());
101 //===----------------------------------------------------------------------===//
102 // GlobalVariable Implementation
103 //===----------------------------------------------------------------------===//
105 GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty,
106 bool constant, LinkageTypes Link,
107 Constant *InitVal, const Twine &Name,
108 bool ThreadLocal, unsigned AddressSpace)
109 : GlobalValue(PointerType::get(Ty, AddressSpace),
110 Value::GlobalVariableVal,
111 OperandTraits<GlobalVariable>::op_begin(this),
112 InitVal != 0, Link, Name),
113 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
115 assert(InitVal->getType() == Ty &&
116 "Initializer should be the same type as the GlobalVariable!");
120 LeakDetector::addGarbageObject(this);
123 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
124 LinkageTypes Link, Constant *InitVal,
126 GlobalVariable *Before, bool ThreadLocal,
127 unsigned AddressSpace)
128 : GlobalValue(PointerType::get(Ty, AddressSpace),
129 Value::GlobalVariableVal,
130 OperandTraits<GlobalVariable>::op_begin(this),
131 InitVal != 0, Link, Name),
132 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
134 assert(InitVal->getType() == Ty &&
135 "Initializer should be the same type as the GlobalVariable!");
139 LeakDetector::addGarbageObject(this);
142 Before->getParent()->getGlobalList().insert(Before, this);
144 M.getGlobalList().push_back(this);
147 void GlobalVariable::setParent(Module *parent) {
149 LeakDetector::addGarbageObject(this);
152 LeakDetector::removeGarbageObject(this);
155 void GlobalVariable::removeFromParent() {
156 getParent()->getGlobalList().remove(this);
159 void GlobalVariable::eraseFromParent() {
160 getParent()->getGlobalList().erase(this);
163 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
165 // If you call this, then you better know this GVar has a constant
166 // initializer worth replacing. Enforce that here.
167 assert(getNumOperands() == 1 &&
168 "Attempt to replace uses of Constants on a GVar with no initializer");
170 // And, since you know it has an initializer, the From value better be
171 // the initializer :)
172 assert(getOperand(0) == From &&
173 "Attempt to replace wrong constant initializer in GVar");
175 // And, you better have a constant for the replacement value
176 assert(isa<Constant>(To) &&
177 "Attempt to replace GVar initializer with non-constant");
179 // Okay, preconditions out of the way, replace the constant initializer.
180 this->setOperand(0, cast<Constant>(To));
183 /// copyAttributesFrom - copy all additional attributes (those not needed to
184 /// create a GlobalVariable) from the GlobalVariable Src to this one.
185 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
186 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
187 GlobalValue::copyAttributesFrom(Src);
188 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
189 setThreadLocal(SrcVar->isThreadLocal());
193 //===----------------------------------------------------------------------===//
194 // GlobalAlias Implementation
195 //===----------------------------------------------------------------------===//
197 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
198 const Twine &Name, Constant* aliasee,
199 Module *ParentModule)
200 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
201 LeakDetector::addGarbageObject(this);
204 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
208 ParentModule->getAliasList().push_back(this);
211 void GlobalAlias::setParent(Module *parent) {
213 LeakDetector::addGarbageObject(this);
216 LeakDetector::removeGarbageObject(this);
219 void GlobalAlias::removeFromParent() {
220 getParent()->getAliasList().remove(this);
223 void GlobalAlias::eraseFromParent() {
224 getParent()->getAliasList().erase(this);
227 bool GlobalAlias::isDeclaration() const {
228 const GlobalValue* AV = getAliasedGlobal();
230 return AV->isDeclaration();
235 void GlobalAlias::setAliasee(Constant *Aliasee)
238 assert(Aliasee->getType() == getType() &&
239 "Alias and aliasee types should match!");
241 setOperand(0, Aliasee);
244 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
245 const Constant *C = getAliasee();
247 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
250 const ConstantExpr *CE = 0;
251 if ((CE = dyn_cast<ConstantExpr>(C)) &&
252 (CE->getOpcode() == Instruction::BitCast ||
253 CE->getOpcode() == Instruction::GetElementPtr))
254 return dyn_cast<GlobalValue>(CE->getOperand(0));
256 llvm_unreachable("Unsupported aliasee");
262 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
263 SmallPtrSet<const GlobalValue*, 3> Visited;
265 // Check if we need to stop early.
266 if (stopOnWeak && mayBeOverridden())
269 const GlobalValue *GV = getAliasedGlobal();
272 // Iterate over aliasing chain, stopping on weak alias if necessary.
273 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
274 if (stopOnWeak && GA->mayBeOverridden())
277 GV = GA->getAliasedGlobal();
279 if (!Visited.insert(GV))