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