Use address-taken to disambiguate global variable and indirect memops.
[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,
86                                const Twine &Name, ThreadLocalMode TLMode,
87                                unsigned AddressSpace,
88                                bool isExternallyInitialized)
89   : GlobalValue(PointerType::get(Ty, AddressSpace),
90                 Value::GlobalVariableVal,
91                 OperandTraits<GlobalVariable>::op_begin(this),
92                 InitVal != 0, Link, Name),
93     isConstantGlobal(constant), threadLocalMode(TLMode),
94     isExternallyInitializedConstant(isExternallyInitialized) {
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   setAddressMaybeTaken(true);
103 }
104
105 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
106                                LinkageTypes Link, Constant *InitVal,
107                                const Twine &Name,
108                                GlobalVariable *Before, ThreadLocalMode TLMode,
109                                unsigned AddressSpace,
110                                bool isExternallyInitialized)
111   : GlobalValue(PointerType::get(Ty, AddressSpace),
112                 Value::GlobalVariableVal,
113                 OperandTraits<GlobalVariable>::op_begin(this),
114                 InitVal != 0, Link, Name),
115     isConstantGlobal(constant), threadLocalMode(TLMode),
116     isExternallyInitializedConstant(isExternallyInitialized) {
117   if (InitVal) {
118     assert(InitVal->getType() == Ty &&
119            "Initializer should be the same type as the GlobalVariable!");
120     Op<0>() = InitVal;
121   }
122   
123   LeakDetector::addGarbageObject(this);
124   
125   if (Before)
126     Before->getParent()->getGlobalList().insert(Before, this);
127   else
128     M.getGlobalList().push_back(this);
129   setAddressMaybeTaken(true);
130 }
131
132 void GlobalVariable::setParent(Module *parent) {
133   if (getParent())
134     LeakDetector::addGarbageObject(this);
135   Parent = parent;
136   if (getParent())
137     LeakDetector::removeGarbageObject(this);
138 }
139
140 void GlobalVariable::removeFromParent() {
141   getParent()->getGlobalList().remove(this);
142 }
143
144 void GlobalVariable::eraseFromParent() {
145   getParent()->getGlobalList().erase(this);
146 }
147
148 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
149                                                  Use *U) {
150   // If you call this, then you better know this GVar has a constant
151   // initializer worth replacing. Enforce that here.
152   assert(getNumOperands() == 1 &&
153          "Attempt to replace uses of Constants on a GVar with no initializer");
154
155   // And, since you know it has an initializer, the From value better be
156   // the initializer :)
157   assert(getOperand(0) == From &&
158          "Attempt to replace wrong constant initializer in GVar");
159
160   // And, you better have a constant for the replacement value
161   assert(isa<Constant>(To) &&
162          "Attempt to replace GVar initializer with non-constant");
163
164   // Okay, preconditions out of the way, replace the constant initializer.
165   this->setOperand(0, cast<Constant>(To));
166 }
167
168 void GlobalVariable::setInitializer(Constant *InitVal) {
169   if (InitVal == 0) {
170     if (hasInitializer()) {
171       Op<0>().set(0);
172       NumOperands = 0;
173     }
174   } else {
175     assert(InitVal->getType() == getType()->getElementType() &&
176            "Initializer type must match GlobalVariable type");
177     if (!hasInitializer())
178       NumOperands = 1;
179     Op<0>().set(InitVal);
180   }
181 }
182
183 /// copyAttributesFrom - copy all additional attributes (those not needed to
184 /// create a GlobalVariable) from the GlobalVariable Src to this one.
185 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
186   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
187   GlobalValue::copyAttributesFrom(Src);
188   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
189   setThreadLocal(SrcVar->isThreadLocal());
190   setAddressMaybeTaken(SrcVar->AddressMaybeTaken());
191 }
192
193
194 //===----------------------------------------------------------------------===//
195 // GlobalAlias Implementation
196 //===----------------------------------------------------------------------===//
197
198 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
199                          const Twine &Name, Constant* aliasee,
200                          Module *ParentModule)
201   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
202   LeakDetector::addGarbageObject(this);
203
204   if (aliasee)
205     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
206   Op<0>() = aliasee;
207
208   if (ParentModule)
209     ParentModule->getAliasList().push_back(this);
210 }
211
212 void GlobalAlias::setParent(Module *parent) {
213   if (getParent())
214     LeakDetector::addGarbageObject(this);
215   Parent = parent;
216   if (getParent())
217     LeakDetector::removeGarbageObject(this);
218 }
219
220 void GlobalAlias::removeFromParent() {
221   getParent()->getAliasList().remove(this);
222 }
223
224 void GlobalAlias::eraseFromParent() {
225   getParent()->getAliasList().erase(this);
226 }
227
228 void GlobalAlias::setAliasee(Constant *Aliasee) {
229   assert((!Aliasee || Aliasee->getType() == getType()) &&
230          "Alias and aliasee types should match!");
231   
232   setOperand(0, Aliasee);
233 }
234
235 GlobalValue *GlobalAlias::getAliasedGlobal() {
236   Constant *C = getAliasee();
237   if (C == 0) return 0;
238   
239   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
240     return GV;
241
242   ConstantExpr *CE = cast<ConstantExpr>(C);
243   assert((CE->getOpcode() == Instruction::BitCast || 
244           CE->getOpcode() == Instruction::GetElementPtr) &&
245          "Unsupported aliasee");
246   
247   return cast<GlobalValue>(CE->getOperand(0));
248 }
249
250 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
251   SmallPtrSet<GlobalValue*, 3> Visited;
252
253   // Check if we need to stop early.
254   if (stopOnWeak && mayBeOverridden())
255     return this;
256
257   GlobalValue *GV = getAliasedGlobal();
258   Visited.insert(GV);
259
260   // Iterate over aliasing chain, stopping on weak alias if necessary.
261   while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
262     if (stopOnWeak && GA->mayBeOverridden())
263       break;
264
265     GV = GA->getAliasedGlobal();
266
267     if (!Visited.insert(GV))
268       return 0;
269   }
270
271   return GV;
272 }