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