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