5a6adb3f304555324e9c6510b29ef75d19970258
[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/Module.h"
22 #include "llvm/IR/Operator.h"
23 #include "llvm/Support/ErrorHandling.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 //                            GlobalValue Class
28 //===----------------------------------------------------------------------===//
29
30 bool GlobalValue::isMaterializable() const {
31   if (const Function *F = dyn_cast<Function>(this))
32     return F->isMaterializable();
33   return false;
34 }
35 bool GlobalValue::isDematerializable() const {
36   return getParent() && getParent()->isDematerializable(this);
37 }
38 std::error_code GlobalValue::materialize() {
39   return getParent()->materialize(this);
40 }
41 void GlobalValue::Dematerialize() {
42   getParent()->Dematerialize(this);
43 }
44
45 /// Override destroyConstant to make sure it doesn't get called on
46 /// GlobalValue's because they shouldn't be treated like other constants.
47 void GlobalValue::destroyConstant() {
48   llvm_unreachable("You can't GV->destroyConstant()!");
49 }
50
51 /// copyAttributesFrom - copy all additional attributes (those not needed to
52 /// create a GlobalValue) from the GlobalValue Src to this one.
53 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
54   setVisibility(Src->getVisibility());
55   setUnnamedAddr(Src->hasUnnamedAddr());
56   setDLLStorageClass(Src->getDLLStorageClass());
57 }
58
59 unsigned GlobalValue::getAlignment() const {
60   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
61     // In general we cannot compute this at the IR level, but we try.
62     if (const GlobalObject *GO = GA->getBaseObject())
63       return GO->getAlignment();
64
65     // FIXME: we should also be able to handle:
66     // Alias = Global + Offset
67     // Alias = Absolute
68     return 0;
69   }
70   return cast<GlobalObject>(this)->getAlignment();
71 }
72
73 void GlobalObject::setAlignment(unsigned Align) {
74   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
75   assert(Align <= MaximumAlignment &&
76          "Alignment is greater than MaximumAlignment!");
77   unsigned AlignmentData = Log2_32(Align) + 1;
78   unsigned OldData = getGlobalValueSubClassData();
79   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
80   assert(getAlignment() == Align && "Alignment representation error!");
81 }
82
83 unsigned GlobalObject::getGlobalObjectSubClassData() const {
84   unsigned ValueData = getGlobalValueSubClassData();
85   return ValueData >> AlignmentBits;
86 }
87
88 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
89   unsigned OldData = getGlobalValueSubClassData();
90   setGlobalValueSubClassData((OldData & AlignmentMask) |
91                              (Val << AlignmentBits));
92   assert(getGlobalObjectSubClassData() == Val && "representation error");
93 }
94
95 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
96   const auto *GV = cast<GlobalObject>(Src);
97   GlobalValue::copyAttributesFrom(GV);
98   setAlignment(GV->getAlignment());
99   setSection(GV->getSection());
100 }
101
102 const char *GlobalValue::getSection() const {
103   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
104     // In general we cannot compute this at the IR level, but we try.
105     if (const GlobalObject *GO = GA->getBaseObject())
106       return GO->getSection();
107     return "";
108   }
109   return cast<GlobalObject>(this)->getSection();
110 }
111
112 Comdat *GlobalValue::getComdat() {
113   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
114     // In general we cannot compute this at the IR level, but we try.
115     if (const GlobalObject *GO = GA->getBaseObject())
116       return const_cast<GlobalObject *>(GO)->getComdat();
117     return nullptr;
118   }
119   return cast<GlobalObject>(this)->getComdat();
120 }
121
122 void GlobalObject::setSection(StringRef S) { Section = S; }
123
124 bool GlobalValue::isDeclaration() const {
125   // Globals are definitions if they have an initializer.
126   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
127     return GV->getNumOperands() == 0;
128
129   // Functions are definitions if they have a body.
130   if (const Function *F = dyn_cast<Function>(this))
131     return F->empty() && !F->isMaterializable();
132
133   // Aliases are always definitions.
134   assert(isa<GlobalAlias>(this));
135   return false;
136 }
137
138 //===----------------------------------------------------------------------===//
139 // GlobalVariable Implementation
140 //===----------------------------------------------------------------------===//
141
142 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
143                                Constant *InitVal, const Twine &Name,
144                                ThreadLocalMode TLMode, unsigned AddressSpace,
145                                bool isExternallyInitialized)
146     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
147                    OperandTraits<GlobalVariable>::op_begin(this),
148                    InitVal != nullptr, Link, Name),
149       isConstantGlobal(constant),
150       isExternallyInitializedConstant(isExternallyInitialized) {
151   setThreadLocalMode(TLMode);
152   if (InitVal) {
153     assert(InitVal->getType() == Ty &&
154            "Initializer should be the same type as the GlobalVariable!");
155     Op<0>() = InitVal;
156   }
157 }
158
159 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
160                                LinkageTypes Link, Constant *InitVal,
161                                const Twine &Name, GlobalVariable *Before,
162                                ThreadLocalMode TLMode, unsigned AddressSpace,
163                                bool isExternallyInitialized)
164     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
165                    OperandTraits<GlobalVariable>::op_begin(this),
166                    InitVal != nullptr, Link, Name),
167       isConstantGlobal(constant),
168       isExternallyInitializedConstant(isExternallyInitialized) {
169   setThreadLocalMode(TLMode);
170   if (InitVal) {
171     assert(InitVal->getType() == Ty &&
172            "Initializer should be the same type as the GlobalVariable!");
173     Op<0>() = InitVal;
174   }
175
176   if (Before)
177     Before->getParent()->getGlobalList().insert(Before, this);
178   else
179     M.getGlobalList().push_back(this);
180 }
181
182 void GlobalVariable::setParent(Module *parent) {
183   Parent = parent;
184 }
185
186 void GlobalVariable::removeFromParent() {
187   getParent()->getGlobalList().remove(this);
188 }
189
190 void GlobalVariable::eraseFromParent() {
191   getParent()->getGlobalList().erase(this);
192 }
193
194 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
195                                                  Use *U) {
196   // If you call this, then you better know this GVar has a constant
197   // initializer worth replacing. Enforce that here.
198   assert(getNumOperands() == 1 &&
199          "Attempt to replace uses of Constants on a GVar with no initializer");
200
201   // And, since you know it has an initializer, the From value better be
202   // the initializer :)
203   assert(getOperand(0) == From &&
204          "Attempt to replace wrong constant initializer in GVar");
205
206   // And, you better have a constant for the replacement value
207   assert(isa<Constant>(To) &&
208          "Attempt to replace GVar initializer with non-constant");
209
210   // Okay, preconditions out of the way, replace the constant initializer.
211   this->setOperand(0, cast<Constant>(To));
212 }
213
214 void GlobalVariable::setInitializer(Constant *InitVal) {
215   if (!InitVal) {
216     if (hasInitializer()) {
217       Op<0>().set(nullptr);
218       NumOperands = 0;
219     }
220   } else {
221     assert(InitVal->getType() == getType()->getElementType() &&
222            "Initializer type must match GlobalVariable type");
223     if (!hasInitializer())
224       NumOperands = 1;
225     Op<0>().set(InitVal);
226   }
227 }
228
229 /// copyAttributesFrom - copy all additional attributes (those not needed to
230 /// create a GlobalVariable) from the GlobalVariable Src to this one.
231 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
232   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
233   GlobalObject::copyAttributesFrom(Src);
234   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
235   setThreadLocalMode(SrcVar->getThreadLocalMode());
236   setExternallyInitialized(SrcVar->isExternallyInitialized());
237 }
238
239
240 //===----------------------------------------------------------------------===//
241 // GlobalAlias Implementation
242 //===----------------------------------------------------------------------===//
243
244 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
245                          const Twine &Name, Constant *Aliasee,
246                          Module *ParentModule)
247     : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
248                   &Op<0>(), 1, Link, Name) {
249   Op<0>() = Aliasee;
250
251   if (ParentModule)
252     ParentModule->getAliasList().push_back(this);
253 }
254
255 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
256                                  LinkageTypes Link, const Twine &Name,
257                                  Constant *Aliasee, Module *ParentModule) {
258   return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
259 }
260
261 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
262                                  LinkageTypes Linkage, const Twine &Name,
263                                  Module *Parent) {
264   return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
265 }
266
267 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
268                                  LinkageTypes Linkage, const Twine &Name,
269                                  GlobalValue *Aliasee) {
270   return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
271 }
272
273 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
274                                  GlobalValue *Aliasee) {
275   PointerType *PTy = Aliasee->getType();
276   return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
277                 Aliasee);
278 }
279
280 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
281   return create(Aliasee->getLinkage(), Name, Aliasee);
282 }
283
284 void GlobalAlias::setParent(Module *parent) {
285   Parent = parent;
286 }
287
288 void GlobalAlias::removeFromParent() {
289   getParent()->getAliasList().remove(this);
290 }
291
292 void GlobalAlias::eraseFromParent() {
293   getParent()->getAliasList().erase(this);
294 }
295
296 void GlobalAlias::setAliasee(Constant *Aliasee) {
297   assert((!Aliasee || Aliasee->getType() == getType()) &&
298          "Alias and aliasee types should match!");
299   setOperand(0, Aliasee);
300 }