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