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