Extend the IL for selecting TLS models (PR9788)
[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 bool GlobalValue::isDeclaration() const {
66   // Globals are definitions if they have an initializer.
67   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
68     return GV->getNumOperands() == 0;
69
70   // Functions are definitions if they have a body.
71   if (const Function *F = dyn_cast<Function>(this))
72     return F->empty();
73
74   // Aliases are always definitions.
75   assert(isa<GlobalAlias>(this));
76   return false;
77 }
78   
79 //===----------------------------------------------------------------------===//
80 // GlobalVariable Implementation
81 //===----------------------------------------------------------------------===//
82
83 // TODO: Remove once clang is updated.
84 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
85                                Constant *InitVal, const Twine &Name,
86                                bool ThreadLocal, 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),
92     threadLocalMode(ThreadLocal ? GeneralDynamicTLSModel : NotThreadLocal) {
93   if (InitVal) {
94     assert(InitVal->getType() == Ty &&
95            "Initializer should be the same type as the GlobalVariable!");
96     Op<0>() = InitVal;
97   }
98
99   LeakDetector::addGarbageObject(this);
100 }
101
102 // TODO: Remove once clang is updated.
103 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
104                                LinkageTypes Link, Constant *InitVal,
105                                const Twine &Name,
106                                GlobalVariable *Before, bool ThreadLocal,
107                                unsigned AddressSpace)
108   : GlobalValue(PointerType::get(Ty, AddressSpace),
109                 Value::GlobalVariableVal,
110                 OperandTraits<GlobalVariable>::op_begin(this),
111                 InitVal != 0, Link, Name),
112     isConstantGlobal(constant),
113     threadLocalMode(ThreadLocal ? GeneralDynamicTLSModel : NotThreadLocal) {
114   if (InitVal) {
115     assert(InitVal->getType() == Ty &&
116            "Initializer should be the same type as the GlobalVariable!");
117     Op<0>() = InitVal;
118   }
119
120   LeakDetector::addGarbageObject(this);
121
122   if (Before)
123     Before->getParent()->getGlobalList().insert(Before, this);
124   else
125     M.getGlobalList().push_back(this);
126 }
127
128 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
129                                Constant *InitVal, const Twine &Name,
130                                ThreadLocalMode TLMode, unsigned AddressSpace)
131   : GlobalValue(PointerType::get(Ty, AddressSpace),
132                 Value::GlobalVariableVal,
133                 OperandTraits<GlobalVariable>::op_begin(this),
134                 InitVal != 0, Link, Name),
135     isConstantGlobal(constant), threadLocalMode(TLMode) {
136   if (InitVal) {
137     assert(InitVal->getType() == Ty &&
138            "Initializer should be the same type as the GlobalVariable!");
139     Op<0>() = InitVal;
140   }
141
142   LeakDetector::addGarbageObject(this);
143 }
144
145 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
146                                LinkageTypes Link, Constant *InitVal,
147                                const Twine &Name,
148                                GlobalVariable *Before, ThreadLocalMode TLMode,
149                                unsigned AddressSpace)
150   : GlobalValue(PointerType::get(Ty, AddressSpace), 
151                 Value::GlobalVariableVal,
152                 OperandTraits<GlobalVariable>::op_begin(this),
153                 InitVal != 0, Link, Name),
154     isConstantGlobal(constant), threadLocalMode(TLMode) {
155   if (InitVal) {
156     assert(InitVal->getType() == Ty &&
157            "Initializer should be the same type as the GlobalVariable!");
158     Op<0>() = InitVal;
159   }
160   
161   LeakDetector::addGarbageObject(this);
162   
163   if (Before)
164     Before->getParent()->getGlobalList().insert(Before, this);
165   else
166     M.getGlobalList().push_back(this);
167 }
168
169 void GlobalVariable::setParent(Module *parent) {
170   if (getParent())
171     LeakDetector::addGarbageObject(this);
172   Parent = parent;
173   if (getParent())
174     LeakDetector::removeGarbageObject(this);
175 }
176
177 void GlobalVariable::removeFromParent() {
178   getParent()->getGlobalList().remove(this);
179 }
180
181 void GlobalVariable::eraseFromParent() {
182   getParent()->getGlobalList().erase(this);
183 }
184
185 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
186                                                  Use *U) {
187   // If you call this, then you better know this GVar has a constant
188   // initializer worth replacing. Enforce that here.
189   assert(getNumOperands() == 1 &&
190          "Attempt to replace uses of Constants on a GVar with no initializer");
191
192   // And, since you know it has an initializer, the From value better be
193   // the initializer :)
194   assert(getOperand(0) == From &&
195          "Attempt to replace wrong constant initializer in GVar");
196
197   // And, you better have a constant for the replacement value
198   assert(isa<Constant>(To) &&
199          "Attempt to replace GVar initializer with non-constant");
200
201   // Okay, preconditions out of the way, replace the constant initializer.
202   this->setOperand(0, cast<Constant>(To));
203 }
204
205 void GlobalVariable::setInitializer(Constant *InitVal) {
206   if (InitVal == 0) {
207     if (hasInitializer()) {
208       Op<0>().set(0);
209       NumOperands = 0;
210     }
211   } else {
212     assert(InitVal->getType() == getType()->getElementType() &&
213            "Initializer type must match GlobalVariable type");
214     if (!hasInitializer())
215       NumOperands = 1;
216     Op<0>().set(InitVal);
217   }
218 }
219
220 /// copyAttributesFrom - copy all additional attributes (those not needed to
221 /// create a GlobalVariable) from the GlobalVariable Src to this one.
222 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
223   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
224   GlobalValue::copyAttributesFrom(Src);
225   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
226   setThreadLocal(SrcVar->isThreadLocal());
227 }
228
229
230 //===----------------------------------------------------------------------===//
231 // GlobalAlias Implementation
232 //===----------------------------------------------------------------------===//
233
234 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
235                          const Twine &Name, Constant* aliasee,
236                          Module *ParentModule)
237   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
238   LeakDetector::addGarbageObject(this);
239
240   if (aliasee)
241     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
242   Op<0>() = aliasee;
243
244   if (ParentModule)
245     ParentModule->getAliasList().push_back(this);
246 }
247
248 void GlobalAlias::setParent(Module *parent) {
249   if (getParent())
250     LeakDetector::addGarbageObject(this);
251   Parent = parent;
252   if (getParent())
253     LeakDetector::removeGarbageObject(this);
254 }
255
256 void GlobalAlias::removeFromParent() {
257   getParent()->getAliasList().remove(this);
258 }
259
260 void GlobalAlias::eraseFromParent() {
261   getParent()->getAliasList().erase(this);
262 }
263
264 void GlobalAlias::setAliasee(Constant *Aliasee) {
265   assert((!Aliasee || Aliasee->getType() == getType()) &&
266          "Alias and aliasee types should match!");
267   
268   setOperand(0, Aliasee);
269 }
270
271 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
272   const Constant *C = getAliasee();
273   if (C == 0) return 0;
274   
275   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
276     return GV;
277
278   const ConstantExpr *CE = cast<ConstantExpr>(C);
279   assert((CE->getOpcode() == Instruction::BitCast || 
280           CE->getOpcode() == Instruction::GetElementPtr) &&
281          "Unsupported aliasee");
282   
283   return cast<GlobalValue>(CE->getOperand(0));
284 }
285
286 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
287   SmallPtrSet<const GlobalValue*, 3> Visited;
288
289   // Check if we need to stop early.
290   if (stopOnWeak && mayBeOverridden())
291     return this;
292
293   const GlobalValue *GV = getAliasedGlobal();
294   Visited.insert(GV);
295
296   // Iterate over aliasing chain, stopping on weak alias if necessary.
297   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
298     if (stopOnWeak && GA->mayBeOverridden())
299       break;
300
301     GV = GA->getAliasedGlobal();
302
303     if (!Visited.insert(GV))
304       return 0;
305   }
306
307   return GV;
308 }