Slightly increase default set size. It's cheap and won't hurt.
[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/LeakDetector.h"
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 //                            GlobalValue Class
26 //===----------------------------------------------------------------------===//
27
28 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
29 /// it.  This involves recursively eliminating any dead users of the
30 /// constantexpr.
31 static bool removeDeadUsersOfConstant(Constant *C) {
32   if (isa<GlobalValue>(C)) return false; // Cannot remove this
33
34   while (!C->use_empty()) {
35     Constant *User = dyn_cast<Constant>(C->use_back());
36     if (!User) return false; // Non-constant usage;
37     if (!removeDeadUsersOfConstant(User))
38       return false; // Constant wasn't dead
39   }
40
41   C->destroyConstant();
42   return true;
43 }
44
45 /// removeDeadConstantUsers - If there are any dead constant users dangling
46 /// off of this global value, remove them.  This method is useful for clients
47 /// that want to check to see if a global is unused, but don't want to deal
48 /// with potentially dead constants hanging off of the globals.
49 void GlobalValue::removeDeadConstantUsers() {
50   Value::use_iterator I = use_begin(), E = use_end();
51   Value::use_iterator LastNonDeadUser = E;
52   while (I != E) {
53     if (Constant *User = dyn_cast<Constant>(*I)) {
54       if (!removeDeadUsersOfConstant(User)) {
55         // If the constant wasn't dead, remember that this was the last live use
56         // and move on to the next constant.
57         LastNonDeadUser = I;
58         ++I;
59       } else {
60         // If the constant was dead, then the iterator is invalidated.
61         if (LastNonDeadUser == E) {
62           I = use_begin();
63           if (I == E) break;
64         } else {
65           I = LastNonDeadUser;
66           ++I;
67         }
68       }
69     } else {
70       LastNonDeadUser = I;
71       ++I;
72     }
73   }
74 }
75
76 /// Override destroyConstant to make sure it doesn't get called on
77 /// GlobalValue's because they shouldn't be treated like other constants.
78 void GlobalValue::destroyConstant() {
79   assert(0 && "You can't GV->destroyConstant()!");
80   abort();
81 }
82   
83 //===----------------------------------------------------------------------===//
84 // GlobalVariable Implementation
85 //===----------------------------------------------------------------------===//
86
87 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
88                                Constant *InitVal, const std::string &Name,
89                                Module *ParentModule, bool ThreadLocal, 
90                                unsigned AddressSpace)
91   : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
92                 &Initializer, InitVal != 0, Link, Name),
93     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
94   if (InitVal) {
95     assert(InitVal->getType() == Ty &&
96            "Initializer should be the same type as the GlobalVariable!");
97     Initializer.init(InitVal, this);
98   } else {
99     Initializer.init(0, this);
100   }
101
102   LeakDetector::addGarbageObject(this);
103
104   if (ParentModule)
105     ParentModule->getGlobalList().push_back(this);
106 }
107
108 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
109                                Constant *InitVal, const std::string &Name,
110                                GlobalVariable *Before, bool ThreadLocal,
111                                unsigned AddressSpace)
112   : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
113                 &Initializer, InitVal != 0, Link, Name), 
114     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
115   if (InitVal) {
116     assert(InitVal->getType() == Ty &&
117            "Initializer should be the same type as the GlobalVariable!");
118     Initializer.init(InitVal, this);
119   } else {
120     Initializer.init(0, this);
121   }
122   
123   LeakDetector::addGarbageObject(this);
124   
125   if (Before)
126     Before->getParent()->getGlobalList().insert(Before, this);
127 }
128
129 void GlobalVariable::setParent(Module *parent) {
130   if (getParent())
131     LeakDetector::addGarbageObject(this);
132   Parent = parent;
133   if (getParent())
134     LeakDetector::removeGarbageObject(this);
135 }
136
137 void GlobalVariable::removeFromParent() {
138   getParent()->getGlobalList().remove(this);
139 }
140
141 void GlobalVariable::eraseFromParent() {
142   getParent()->getGlobalList().erase(this);
143 }
144
145 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
146                                                  Use *U) {
147   // If you call this, then you better know this GVar has a constant
148   // initializer worth replacing. Enforce that here.
149   assert(getNumOperands() == 1 &&
150          "Attempt to replace uses of Constants on a GVar with no initializer");
151
152   // And, since you know it has an initializer, the From value better be
153   // the initializer :)
154   assert(getOperand(0) == From &&
155          "Attempt to replace wrong constant initializer in GVar");
156
157   // And, you better have a constant for the replacement value
158   assert(isa<Constant>(To) &&
159          "Attempt to replace GVar initializer with non-constant");
160
161   // Okay, preconditions out of the way, replace the constant initializer.
162   this->setOperand(0, cast<Constant>(To));
163 }
164
165 //===----------------------------------------------------------------------===//
166 // GlobalAlias Implementation
167 //===----------------------------------------------------------------------===//
168
169 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
170                          const std::string &Name, Constant* aliasee,
171                          Module *ParentModule)
172   : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) {
173   LeakDetector::addGarbageObject(this);
174
175   if (aliasee)
176     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
177   Aliasee.init(aliasee, this);
178
179   if (ParentModule)
180     ParentModule->getAliasList().push_back(this);
181 }
182
183 void GlobalAlias::setParent(Module *parent) {
184   if (getParent())
185     LeakDetector::addGarbageObject(this);
186   Parent = parent;
187   if (getParent())
188     LeakDetector::removeGarbageObject(this);
189 }
190
191 void GlobalAlias::removeFromParent() {
192   getParent()->getAliasList().remove(this);
193 }
194
195 void GlobalAlias::eraseFromParent() {
196   getParent()->getAliasList().erase(this);
197 }
198
199 bool GlobalAlias::isDeclaration() const {
200   const GlobalValue* AV = getAliasedGlobal();
201   if (AV)
202     return AV->isDeclaration();
203   else
204     return false;
205 }
206
207 void GlobalAlias::setAliasee(Constant *Aliasee) 
208 {
209   if (Aliasee)
210     assert(Aliasee->getType() == getType() &&
211            "Alias and aliasee types should match!");
212   
213   setOperand(0, Aliasee);
214 }
215
216 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
217   const Constant *C = getAliasee();
218   if (C) {
219     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
220       return GV;
221     else {
222       const ConstantExpr *CE = 0;
223       if ((CE = dyn_cast<ConstantExpr>(C)) &&
224           (CE->getOpcode() == Instruction::BitCast || 
225            CE->getOpcode() == Instruction::GetElementPtr))
226         return dyn_cast<GlobalValue>(CE->getOperand(0));
227       else
228         assert(0 && "Unsupported aliasee");
229     }
230   }
231   return 0;
232 }
233
234 const GlobalValue *GlobalAlias::resolveAliasedGlobal() const {
235   SmallPtrSet<const GlobalValue*, 3> Visited;
236
237   const GlobalValue *GV = getAliasedGlobal();
238   Visited.insert(GV);
239
240   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
241     GV = GA->getAliasedGlobal();
242
243     if (!Visited.insert(GV))
244       return NULL;
245   }
246
247   return GV;
248 }