60000ad1b50e3e2fa127b73a8542278bb82bdbfd
[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 bool GlobalValue::isMaterializable() const {
30   return getParent() && getParent()->isMaterializable(this);
31 }
32 bool GlobalValue::isDematerializable() const {
33   return getParent() && getParent()->isDematerializable(this);
34 }
35 bool GlobalValue::Materialize(std::string *ErrInfo) {
36   return getParent()->Materialize(this, ErrInfo);
37 }
38 void GlobalValue::Dematerialize() {
39   getParent()->Dematerialize(this);
40 }
41
42 /// Override destroyConstant to make sure it doesn't get called on
43 /// GlobalValue's because they shouldn't be treated like other constants.
44 void GlobalValue::destroyConstant() {
45   llvm_unreachable("You can't GV->destroyConstant()!");
46 }
47
48 /// copyAttributesFrom - copy all additional attributes (those not needed to
49 /// create a GlobalValue) from the GlobalValue Src to this one.
50 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
51   setAlignment(Src->getAlignment());
52   setSection(Src->getSection());
53   setVisibility(Src->getVisibility());
54 }
55
56 void GlobalValue::setAlignment(unsigned Align) {
57   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
58   assert(Align <= MaximumAlignment &&
59          "Alignment is greater than MaximumAlignment!");
60   Alignment = Log2_32(Align) + 1;
61   assert(getAlignment() == Align && "Alignment representation error!");
62 }
63   
64 //===----------------------------------------------------------------------===//
65 // GlobalVariable Implementation
66 //===----------------------------------------------------------------------===//
67
68 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
69                                Constant *InitVal, const Twine &Name,
70                                bool ThreadLocal, unsigned AddressSpace)
71   : GlobalValue(PointerType::get(Ty, AddressSpace), 
72                 Value::GlobalVariableVal,
73                 OperandTraits<GlobalVariable>::op_begin(this),
74                 InitVal != 0, Link, Name),
75     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
76   if (InitVal) {
77     assert(InitVal->getType() == Ty &&
78            "Initializer should be the same type as the GlobalVariable!");
79     Op<0>() = InitVal;
80   }
81
82   LeakDetector::addGarbageObject(this);
83 }
84
85 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
86                                LinkageTypes Link, Constant *InitVal,
87                                const Twine &Name,
88                                GlobalVariable *Before, bool ThreadLocal,
89                                unsigned AddressSpace)
90   : GlobalValue(PointerType::get(Ty, AddressSpace), 
91                 Value::GlobalVariableVal,
92                 OperandTraits<GlobalVariable>::op_begin(this),
93                 InitVal != 0, Link, Name),
94     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
95   if (InitVal) {
96     assert(InitVal->getType() == Ty &&
97            "Initializer should be the same type as the GlobalVariable!");
98     Op<0>() = InitVal;
99   }
100   
101   LeakDetector::addGarbageObject(this);
102   
103   if (Before)
104     Before->getParent()->getGlobalList().insert(Before, this);
105   else
106     M.getGlobalList().push_back(this);
107 }
108
109 void GlobalVariable::setParent(Module *parent) {
110   if (getParent())
111     LeakDetector::addGarbageObject(this);
112   Parent = parent;
113   if (getParent())
114     LeakDetector::removeGarbageObject(this);
115 }
116
117 void GlobalVariable::removeFromParent() {
118   getParent()->getGlobalList().remove(this);
119 }
120
121 void GlobalVariable::eraseFromParent() {
122   getParent()->getGlobalList().erase(this);
123 }
124
125 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
126                                                  Use *U) {
127   // If you call this, then you better know this GVar has a constant
128   // initializer worth replacing. Enforce that here.
129   assert(getNumOperands() == 1 &&
130          "Attempt to replace uses of Constants on a GVar with no initializer");
131
132   // And, since you know it has an initializer, the From value better be
133   // the initializer :)
134   assert(getOperand(0) == From &&
135          "Attempt to replace wrong constant initializer in GVar");
136
137   // And, you better have a constant for the replacement value
138   assert(isa<Constant>(To) &&
139          "Attempt to replace GVar initializer with non-constant");
140
141   // Okay, preconditions out of the way, replace the constant initializer.
142   this->setOperand(0, cast<Constant>(To));
143 }
144
145 void GlobalVariable::setInitializer(Constant *InitVal) {
146   if (InitVal == 0) {
147     if (hasInitializer()) {
148       Op<0>().set(0);
149       NumOperands = 0;
150     }
151   } else {
152     assert(InitVal->getType() == getType()->getElementType() &&
153            "Initializer type must match GlobalVariable type");
154     if (!hasInitializer())
155       NumOperands = 1;
156     Op<0>().set(InitVal);
157   }
158 }
159
160 /// copyAttributesFrom - copy all additional attributes (those not needed to
161 /// create a GlobalVariable) from the GlobalVariable Src to this one.
162 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
163   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
164   GlobalValue::copyAttributesFrom(Src);
165   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
166   setThreadLocal(SrcVar->isThreadLocal());
167 }
168
169
170 //===----------------------------------------------------------------------===//
171 // GlobalAlias Implementation
172 //===----------------------------------------------------------------------===//
173
174 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
175                          const Twine &Name, Constant* aliasee,
176                          Module *ParentModule)
177   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
178   LeakDetector::addGarbageObject(this);
179
180   if (aliasee)
181     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
182   Op<0>() = aliasee;
183
184   if (ParentModule)
185     ParentModule->getAliasList().push_back(this);
186 }
187
188 void GlobalAlias::setParent(Module *parent) {
189   if (getParent())
190     LeakDetector::addGarbageObject(this);
191   Parent = parent;
192   if (getParent())
193     LeakDetector::removeGarbageObject(this);
194 }
195
196 void GlobalAlias::removeFromParent() {
197   getParent()->getAliasList().remove(this);
198 }
199
200 void GlobalAlias::eraseFromParent() {
201   getParent()->getAliasList().erase(this);
202 }
203
204 bool GlobalAlias::isDeclaration() const {
205   const GlobalValue* AV = getAliasedGlobal();
206   if (AV)
207     return AV->isDeclaration();
208   else
209     return false;
210 }
211
212 void GlobalAlias::setAliasee(Constant *Aliasee) 
213 {
214   if (Aliasee)
215     assert(Aliasee->getType() == getType() &&
216            "Alias and aliasee types should match!");
217   
218   setOperand(0, Aliasee);
219 }
220
221 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
222   const Constant *C = getAliasee();
223   if (C) {
224     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
225       return GV;
226     else {
227       const ConstantExpr *CE = 0;
228       if ((CE = dyn_cast<ConstantExpr>(C)) &&
229           (CE->getOpcode() == Instruction::BitCast || 
230            CE->getOpcode() == Instruction::GetElementPtr))
231         return dyn_cast<GlobalValue>(CE->getOperand(0));
232       else
233         llvm_unreachable("Unsupported aliasee");
234     }
235   }
236   return 0;
237 }
238
239 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
240   SmallPtrSet<const GlobalValue*, 3> Visited;
241
242   // Check if we need to stop early.
243   if (stopOnWeak && mayBeOverridden())
244     return this;
245
246   const GlobalValue *GV = getAliasedGlobal();
247   Visited.insert(GV);
248
249   // Iterate over aliasing chain, stopping on weak alias if necessary.
250   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
251     if (stopOnWeak && GA->mayBeOverridden())
252       break;
253
254     GV = GA->getAliasedGlobal();
255
256     if (!Visited.insert(GV))
257       return NULL;
258   }
259
260   return GV;
261 }