Devirtualize Constant::destroyConstant.
[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 destroyConstantImpl to make sure it doesn't get called on
46 /// GlobalValue's because they shouldn't be treated like other constants.
47 void GlobalValue::destroyConstantImpl() {
48   llvm_unreachable("You can't GV->destroyConstantImpl()!");
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       // Note, the num operands is used to compute the offset of the operand, so
218       // the order here matters.  Clearing the operand then clearing the num
219       // operands ensures we have the correct offset to the operand.
220       Op<0>().set(nullptr);
221       setGlobalVariableNumOperands(0);
222     }
223   } else {
224     assert(InitVal->getType() == getType()->getElementType() &&
225            "Initializer type must match GlobalVariable type");
226     // Note, the num operands is used to compute the offset of the operand, so
227     // the order here matters.  We need to set num operands to 1 first so that
228     // we get the correct offset to the first operand when we set it.
229     if (!hasInitializer())
230       setGlobalVariableNumOperands(1);
231     Op<0>().set(InitVal);
232   }
233 }
234
235 /// copyAttributesFrom - copy all additional attributes (those not needed to
236 /// create a GlobalVariable) from the GlobalVariable Src to this one.
237 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
238   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
239   GlobalObject::copyAttributesFrom(Src);
240   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
241   setThreadLocalMode(SrcVar->getThreadLocalMode());
242   setExternallyInitialized(SrcVar->isExternallyInitialized());
243 }
244
245
246 //===----------------------------------------------------------------------===//
247 // GlobalAlias Implementation
248 //===----------------------------------------------------------------------===//
249
250 GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
251                          Constant *Aliasee, Module *ParentModule)
252     : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
253   Op<0>() = Aliasee;
254
255   if (ParentModule)
256     ParentModule->getAliasList().push_back(this);
257 }
258
259 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
260                                  const Twine &Name, Constant *Aliasee,
261                                  Module *ParentModule) {
262   return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
263 }
264
265 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
266                                  const Twine &Name, Module *Parent) {
267   return create(Ty, Linkage, Name, nullptr, Parent);
268 }
269
270 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
271                                  const Twine &Name, GlobalValue *Aliasee) {
272   return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
273 }
274
275 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
276                                  GlobalValue *Aliasee) {
277   PointerType *PTy = Aliasee->getType();
278   return create(PTy, Link, Name, Aliasee);
279 }
280
281 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
282   return create(Aliasee->getLinkage(), Name, Aliasee);
283 }
284
285 void GlobalAlias::setParent(Module *parent) {
286   Parent = parent;
287 }
288
289 void GlobalAlias::removeFromParent() {
290   getParent()->getAliasList().remove(this);
291 }
292
293 void GlobalAlias::eraseFromParent() {
294   getParent()->getAliasList().erase(this);
295 }
296
297 void GlobalAlias::setAliasee(Constant *Aliasee) {
298   assert((!Aliasee || Aliasee->getType() == getType()) &&
299          "Alias and aliasee types should match!");
300   setOperand(0, Aliasee);
301 }