Check that GlobalAliases don't have section or alignment.
[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   setDLLStorageClass(Src->getDLLStorageClass());
57 }
58
59 void GlobalValue::setAlignment(unsigned Align) {
60   assert((!isa<GlobalAlias>(this) || !Align) &&
61          "GlobalAlias should not have an alignment!");
62   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
63   assert(Align <= MaximumAlignment &&
64          "Alignment is greater than MaximumAlignment!");
65   Alignment = Log2_32(Align) + 1;
66   assert(getAlignment() == Align && "Alignment representation error!");
67 }
68
69 bool GlobalValue::isDeclaration() const {
70   // Globals are definitions if they have an initializer.
71   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
72     return GV->getNumOperands() == 0;
73
74   // Functions are definitions if they have a body.
75   if (const Function *F = dyn_cast<Function>(this))
76     return F->empty();
77
78   // Aliases are always definitions.
79   assert(isa<GlobalAlias>(this));
80   return false;
81 }
82   
83 //===----------------------------------------------------------------------===//
84 // GlobalVariable Implementation
85 //===----------------------------------------------------------------------===//
86
87 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
88                                Constant *InitVal,
89                                const Twine &Name, ThreadLocalMode TLMode,
90                                unsigned AddressSpace,
91                                bool isExternallyInitialized)
92   : GlobalValue(PointerType::get(Ty, AddressSpace),
93                 Value::GlobalVariableVal,
94                 OperandTraits<GlobalVariable>::op_begin(this),
95                 InitVal != 0, Link, Name),
96     isConstantGlobal(constant), threadLocalMode(TLMode),
97     isExternallyInitializedConstant(isExternallyInitialized) {
98   if (InitVal) {
99     assert(InitVal->getType() == Ty &&
100            "Initializer should be the same type as the GlobalVariable!");
101     Op<0>() = InitVal;
102   }
103
104   LeakDetector::addGarbageObject(this);
105 }
106
107 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
108                                LinkageTypes Link, Constant *InitVal,
109                                const Twine &Name,
110                                GlobalVariable *Before, ThreadLocalMode TLMode,
111                                unsigned AddressSpace,
112                                bool isExternallyInitialized)
113   : GlobalValue(PointerType::get(Ty, AddressSpace),
114                 Value::GlobalVariableVal,
115                 OperandTraits<GlobalVariable>::op_begin(this),
116                 InitVal != 0, Link, Name),
117     isConstantGlobal(constant), threadLocalMode(TLMode),
118     isExternallyInitializedConstant(isExternallyInitialized) {
119   if (InitVal) {
120     assert(InitVal->getType() == Ty &&
121            "Initializer should be the same type as the GlobalVariable!");
122     Op<0>() = InitVal;
123   }
124   
125   LeakDetector::addGarbageObject(this);
126   
127   if (Before)
128     Before->getParent()->getGlobalList().insert(Before, this);
129   else
130     M.getGlobalList().push_back(this);
131 }
132
133 void GlobalVariable::setParent(Module *parent) {
134   if (getParent())
135     LeakDetector::addGarbageObject(this);
136   Parent = parent;
137   if (getParent())
138     LeakDetector::removeGarbageObject(this);
139 }
140
141 void GlobalVariable::removeFromParent() {
142   getParent()->getGlobalList().remove(this);
143 }
144
145 void GlobalVariable::eraseFromParent() {
146   getParent()->getGlobalList().erase(this);
147 }
148
149 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
150                                                  Use *U) {
151   // If you call this, then you better know this GVar has a constant
152   // initializer worth replacing. Enforce that here.
153   assert(getNumOperands() == 1 &&
154          "Attempt to replace uses of Constants on a GVar with no initializer");
155
156   // And, since you know it has an initializer, the From value better be
157   // the initializer :)
158   assert(getOperand(0) == From &&
159          "Attempt to replace wrong constant initializer in GVar");
160
161   // And, you better have a constant for the replacement value
162   assert(isa<Constant>(To) &&
163          "Attempt to replace GVar initializer with non-constant");
164
165   // Okay, preconditions out of the way, replace the constant initializer.
166   this->setOperand(0, cast<Constant>(To));
167 }
168
169 void GlobalVariable::setInitializer(Constant *InitVal) {
170   if (InitVal == 0) {
171     if (hasInitializer()) {
172       Op<0>().set(0);
173       NumOperands = 0;
174     }
175   } else {
176     assert(InitVal->getType() == getType()->getElementType() &&
177            "Initializer type must match GlobalVariable type");
178     if (!hasInitializer())
179       NumOperands = 1;
180     Op<0>().set(InitVal);
181   }
182 }
183
184 /// copyAttributesFrom - copy all additional attributes (those not needed to
185 /// create a GlobalVariable) from the GlobalVariable Src to this one.
186 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
187   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
188   GlobalValue::copyAttributesFrom(Src);
189   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
190   setThreadLocalMode(SrcVar->getThreadLocalMode());
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::AddrSpaceCast ||
245           CE->getOpcode() == Instruction::GetElementPtr) &&
246          "Unsupported aliasee");
247   
248   return cast<GlobalValue>(CE->getOperand(0));
249 }
250
251 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
252   SmallPtrSet<GlobalValue*, 3> Visited;
253
254   // Check if we need to stop early.
255   if (stopOnWeak && mayBeOverridden())
256     return this;
257
258   GlobalValue *GV = getAliasedGlobal();
259   Visited.insert(GV);
260
261   // Iterate over aliasing chain, stopping on weak alias if necessary.
262   while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
263     if (stopOnWeak && GA->mayBeOverridden())
264       break;
265
266     GV = GA->getAliasedGlobal();
267
268     if (!Visited.insert(GV))
269       return 0;
270   }
271
272   return GV;
273 }