remove a dead bool.
[llvm/avr.git] / lib / VMCore / Globals.cpp
blobd18a20162dd9e223899f1a1d4789fe9b2863acd1
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.
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"
24 using namespace llvm;
26 //===----------------------------------------------------------------------===//
27 // GlobalValue Class
28 //===----------------------------------------------------------------------===//
30 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
31 /// it. This involves recursively eliminating any dead users of the
32 /// constantexpr.
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();
44 return true;
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;
54 while (I != 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.
59 LastNonDeadUser = I;
60 ++I;
61 } else {
62 // If the constant was dead, then the iterator is invalidated.
63 if (LastNonDeadUser == E) {
64 I = use_begin();
65 if (I == E) break;
66 } else {
67 I = LastNonDeadUser;
68 ++I;
71 } else {
72 LastNonDeadUser = I;
73 ++I;
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(LLVMContext &Context, const Type *Ty,
98 bool constant, LinkageTypes Link,
99 Constant *InitVal, const Twine &Name,
100 bool ThreadLocal, unsigned AddressSpace)
101 : GlobalValue(PointerType::get(Ty, AddressSpace),
102 Value::GlobalVariableVal,
103 OperandTraits<GlobalVariable>::op_begin(this),
104 InitVal != 0, Link, Name),
105 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
106 if (InitVal) {
107 assert(InitVal->getType() == Ty &&
108 "Initializer should be the same type as the GlobalVariable!");
109 Op<0>() = InitVal;
112 LeakDetector::addGarbageObject(this);
115 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
116 LinkageTypes Link, Constant *InitVal,
117 const Twine &Name,
118 GlobalVariable *Before, bool ThreadLocal,
119 unsigned AddressSpace)
120 : GlobalValue(PointerType::get(Ty, AddressSpace),
121 Value::GlobalVariableVal,
122 OperandTraits<GlobalVariable>::op_begin(this),
123 InitVal != 0, Link, Name),
124 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
125 if (InitVal) {
126 assert(InitVal->getType() == Ty &&
127 "Initializer should be the same type as the GlobalVariable!");
128 Op<0>() = InitVal;
131 LeakDetector::addGarbageObject(this);
133 if (Before)
134 Before->getParent()->getGlobalList().insert(Before, this);
135 else
136 M.getGlobalList().push_back(this);
139 void GlobalVariable::setParent(Module *parent) {
140 if (getParent())
141 LeakDetector::addGarbageObject(this);
142 Parent = parent;
143 if (getParent())
144 LeakDetector::removeGarbageObject(this);
147 void GlobalVariable::removeFromParent() {
148 getParent()->getGlobalList().remove(this);
151 void GlobalVariable::eraseFromParent() {
152 getParent()->getGlobalList().erase(this);
155 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
156 Use *U) {
157 // If you call this, then you better know this GVar has a constant
158 // initializer worth replacing. Enforce that here.
159 assert(getNumOperands() == 1 &&
160 "Attempt to replace uses of Constants on a GVar with no initializer");
162 // And, since you know it has an initializer, the From value better be
163 // the initializer :)
164 assert(getOperand(0) == From &&
165 "Attempt to replace wrong constant initializer in GVar");
167 // And, you better have a constant for the replacement value
168 assert(isa<Constant>(To) &&
169 "Attempt to replace GVar initializer with non-constant");
171 // Okay, preconditions out of the way, replace the constant initializer.
172 this->setOperand(0, cast<Constant>(To));
175 /// copyAttributesFrom - copy all additional attributes (those not needed to
176 /// create a GlobalVariable) from the GlobalVariable Src to this one.
177 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
178 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
179 GlobalValue::copyAttributesFrom(Src);
180 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
181 setThreadLocal(SrcVar->isThreadLocal());
185 //===----------------------------------------------------------------------===//
186 // GlobalAlias Implementation
187 //===----------------------------------------------------------------------===//
189 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
190 const Twine &Name, Constant* aliasee,
191 Module *ParentModule)
192 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
193 LeakDetector::addGarbageObject(this);
195 if (aliasee)
196 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
197 Op<0>() = aliasee;
199 if (ParentModule)
200 ParentModule->getAliasList().push_back(this);
203 void GlobalAlias::setParent(Module *parent) {
204 if (getParent())
205 LeakDetector::addGarbageObject(this);
206 Parent = parent;
207 if (getParent())
208 LeakDetector::removeGarbageObject(this);
211 void GlobalAlias::removeFromParent() {
212 getParent()->getAliasList().remove(this);
215 void GlobalAlias::eraseFromParent() {
216 getParent()->getAliasList().erase(this);
219 bool GlobalAlias::isDeclaration() const {
220 const GlobalValue* AV = getAliasedGlobal();
221 if (AV)
222 return AV->isDeclaration();
223 else
224 return false;
227 void GlobalAlias::setAliasee(Constant *Aliasee)
229 if (Aliasee)
230 assert(Aliasee->getType() == getType() &&
231 "Alias and aliasee types should match!");
233 setOperand(0, Aliasee);
236 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
237 const Constant *C = getAliasee();
238 if (C) {
239 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
240 return GV;
241 else {
242 const ConstantExpr *CE = 0;
243 if ((CE = dyn_cast<ConstantExpr>(C)) &&
244 (CE->getOpcode() == Instruction::BitCast ||
245 CE->getOpcode() == Instruction::GetElementPtr))
246 return dyn_cast<GlobalValue>(CE->getOperand(0));
247 else
248 llvm_unreachable("Unsupported aliasee");
251 return 0;
254 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
255 SmallPtrSet<const GlobalValue*, 3> Visited;
257 // Check if we need to stop early.
258 if (stopOnWeak && mayBeOverridden())
259 return this;
261 const GlobalValue *GV = getAliasedGlobal();
262 Visited.insert(GV);
264 // Iterate over aliasing chain, stopping on weak alias if necessary.
265 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
266 if (stopOnWeak && GA->mayBeOverridden())
267 break;
269 GV = GA->getAliasedGlobal();
271 if (!Visited.insert(GV))
272 return NULL;
275 return GV;