What the loop unroller cares about, rather than just not unrolling loops with calls, is
[oota-llvm.git] / lib / VMCore / Globals.cpp
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/ErrorHandling.h"
22 #include "llvm/Support/LeakDetector.h"
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 //                            GlobalValue Class
27 //===----------------------------------------------------------------------===//
28
29 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
30 /// it.  This involves recursively eliminating any dead users of the
31 /// constantexpr.
32 static bool removeDeadUsersOfConstant(const Constant *C) {
33   if (isa<GlobalValue>(C)) return false; // Cannot remove this
34
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
40   }
41
42   const_cast<Constant*>(C)->destroyConstant();
43   return true;
44 }
45
46 bool GlobalValue::isMaterializable() const {
47   return getParent() && getParent()->isMaterializable(this);
48 }
49 bool GlobalValue::isDematerializable() const {
50   return getParent() && getParent()->isDematerializable(this);
51 }
52 bool GlobalValue::Materialize(std::string *ErrInfo) {
53   return getParent()->Materialize(this, ErrInfo);
54 }
55 void GlobalValue::Dematerialize() {
56   getParent()->Dematerialize(this);
57 }
58
59 /// removeDeadConstantUsers - If there are any dead constant users dangling
60 /// off of this global value, remove them.  This method is useful for clients
61 /// that want to check to see if a global is unused, but don't want to deal
62 /// with potentially dead constants hanging off of the globals.
63 void GlobalValue::removeDeadConstantUsers() const {
64   Value::const_use_iterator I = use_begin(), E = use_end();
65   Value::const_use_iterator LastNonDeadUser = E;
66   while (I != E) {
67     if (const Constant *User = dyn_cast<Constant>(*I)) {
68       if (!removeDeadUsersOfConstant(User)) {
69         // If the constant wasn't dead, remember that this was the last live use
70         // and move on to the next constant.
71         LastNonDeadUser = I;
72         ++I;
73       } else {
74         // If the constant was dead, then the iterator is invalidated.
75         if (LastNonDeadUser == E) {
76           I = use_begin();
77           if (I == E) break;
78         } else {
79           I = LastNonDeadUser;
80           ++I;
81         }
82       }
83     } else {
84       LastNonDeadUser = I;
85       ++I;
86     }
87   }
88 }
89
90
91 /// Override destroyConstant to make sure it doesn't get called on
92 /// GlobalValue's because they shouldn't be treated like other constants.
93 void GlobalValue::destroyConstant() {
94   llvm_unreachable("You can't GV->destroyConstant()!");
95 }
96
97 /// copyAttributesFrom - copy all additional attributes (those not needed to
98 /// create a GlobalValue) from the GlobalValue Src to this one.
99 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
100   setAlignment(Src->getAlignment());
101   setSection(Src->getSection());
102   setVisibility(Src->getVisibility());
103 }
104
105 void GlobalValue::setAlignment(unsigned Align) {
106   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
107   assert(Align <= MaximumAlignment &&
108          "Alignment is greater than MaximumAlignment!");
109   Alignment = Log2_32(Align) + 1;
110   assert(getAlignment() == Align && "Alignment representation error!");
111 }
112   
113 //===----------------------------------------------------------------------===//
114 // GlobalVariable Implementation
115 //===----------------------------------------------------------------------===//
116
117 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
118                                Constant *InitVal, const Twine &Name,
119                                bool ThreadLocal, 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;
129   }
130
131   LeakDetector::addGarbageObject(this);
132 }
133
134 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
135                                LinkageTypes Link, Constant *InitVal,
136                                const Twine &Name,
137                                GlobalVariable *Before, bool ThreadLocal,
138                                unsigned AddressSpace)
139   : GlobalValue(PointerType::get(Ty, AddressSpace), 
140                 Value::GlobalVariableVal,
141                 OperandTraits<GlobalVariable>::op_begin(this),
142                 InitVal != 0, Link, Name),
143     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
144   if (InitVal) {
145     assert(InitVal->getType() == Ty &&
146            "Initializer should be the same type as the GlobalVariable!");
147     Op<0>() = InitVal;
148   }
149   
150   LeakDetector::addGarbageObject(this);
151   
152   if (Before)
153     Before->getParent()->getGlobalList().insert(Before, this);
154   else
155     M.getGlobalList().push_back(this);
156 }
157
158 void GlobalVariable::setParent(Module *parent) {
159   if (getParent())
160     LeakDetector::addGarbageObject(this);
161   Parent = parent;
162   if (getParent())
163     LeakDetector::removeGarbageObject(this);
164 }
165
166 void GlobalVariable::removeFromParent() {
167   getParent()->getGlobalList().remove(this);
168 }
169
170 void GlobalVariable::eraseFromParent() {
171   getParent()->getGlobalList().erase(this);
172 }
173
174 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
175                                                  Use *U) {
176   // If you call this, then you better know this GVar has a constant
177   // initializer worth replacing. Enforce that here.
178   assert(getNumOperands() == 1 &&
179          "Attempt to replace uses of Constants on a GVar with no initializer");
180
181   // And, since you know it has an initializer, the From value better be
182   // the initializer :)
183   assert(getOperand(0) == From &&
184          "Attempt to replace wrong constant initializer in GVar");
185
186   // And, you better have a constant for the replacement value
187   assert(isa<Constant>(To) &&
188          "Attempt to replace GVar initializer with non-constant");
189
190   // Okay, preconditions out of the way, replace the constant initializer.
191   this->setOperand(0, cast<Constant>(To));
192 }
193
194 void GlobalVariable::setInitializer(Constant *InitVal) {
195   if (InitVal == 0) {
196     if (hasInitializer()) {
197       Op<0>().set(0);
198       NumOperands = 0;
199     }
200   } else {
201     assert(InitVal->getType() == getType()->getElementType() &&
202            "Initializer type must match GlobalVariable type");
203     if (!hasInitializer())
204       NumOperands = 1;
205     Op<0>().set(InitVal);
206   }
207 }
208
209 /// copyAttributesFrom - copy all additional attributes (those not needed to
210 /// create a GlobalVariable) from the GlobalVariable Src to this one.
211 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
212   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
213   GlobalValue::copyAttributesFrom(Src);
214   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
215   setThreadLocal(SrcVar->isThreadLocal());
216 }
217
218
219 //===----------------------------------------------------------------------===//
220 // GlobalAlias Implementation
221 //===----------------------------------------------------------------------===//
222
223 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
224                          const Twine &Name, Constant* aliasee,
225                          Module *ParentModule)
226   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
227   LeakDetector::addGarbageObject(this);
228
229   if (aliasee)
230     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
231   Op<0>() = aliasee;
232
233   if (ParentModule)
234     ParentModule->getAliasList().push_back(this);
235 }
236
237 void GlobalAlias::setParent(Module *parent) {
238   if (getParent())
239     LeakDetector::addGarbageObject(this);
240   Parent = parent;
241   if (getParent())
242     LeakDetector::removeGarbageObject(this);
243 }
244
245 void GlobalAlias::removeFromParent() {
246   getParent()->getAliasList().remove(this);
247 }
248
249 void GlobalAlias::eraseFromParent() {
250   getParent()->getAliasList().erase(this);
251 }
252
253 bool GlobalAlias::isDeclaration() const {
254   const GlobalValue* AV = getAliasedGlobal();
255   if (AV)
256     return AV->isDeclaration();
257   else
258     return false;
259 }
260
261 void GlobalAlias::setAliasee(Constant *Aliasee) 
262 {
263   if (Aliasee)
264     assert(Aliasee->getType() == getType() &&
265            "Alias and aliasee types should match!");
266   
267   setOperand(0, Aliasee);
268 }
269
270 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
271   const Constant *C = getAliasee();
272   if (C) {
273     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
274       return GV;
275     else {
276       const ConstantExpr *CE = 0;
277       if ((CE = dyn_cast<ConstantExpr>(C)) &&
278           (CE->getOpcode() == Instruction::BitCast || 
279            CE->getOpcode() == Instruction::GetElementPtr))
280         return dyn_cast<GlobalValue>(CE->getOperand(0));
281       else
282         llvm_unreachable("Unsupported aliasee");
283     }
284   }
285   return 0;
286 }
287
288 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
289   SmallPtrSet<const GlobalValue*, 3> Visited;
290
291   // Check if we need to stop early.
292   if (stopOnWeak && mayBeOverridden())
293     return this;
294
295   const GlobalValue *GV = getAliasedGlobal();
296   Visited.insert(GV);
297
298   // Iterate over aliasing chain, stopping on weak alias if necessary.
299   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
300     if (stopOnWeak && GA->mayBeOverridden())
301       break;
302
303     GV = GA->getAliasedGlobal();
304
305     if (!Visited.insert(GV))
306       return NULL;
307   }
308
309   return GV;
310 }