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