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