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