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