c538c7baa1feba5928ecc86a0176a5253ea3a216
[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 Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) {
52   llvm_unreachable("Unsupported class for handleOperandChange()!");
53 }
54
55 /// copyAttributesFrom - copy all additional attributes (those not needed to
56 /// create a GlobalValue) from the GlobalValue Src to this one.
57 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
58   setVisibility(Src->getVisibility());
59   setUnnamedAddr(Src->hasUnnamedAddr());
60   setDLLStorageClass(Src->getDLLStorageClass());
61 }
62
63 unsigned GlobalValue::getAlignment() const {
64   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
65     // In general we cannot compute this at the IR level, but we try.
66     if (const GlobalObject *GO = GA->getBaseObject())
67       return GO->getAlignment();
68
69     // FIXME: we should also be able to handle:
70     // Alias = Global + Offset
71     // Alias = Absolute
72     return 0;
73   }
74   return cast<GlobalObject>(this)->getAlignment();
75 }
76
77 void GlobalObject::setAlignment(unsigned Align) {
78   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
79   assert(Align <= MaximumAlignment &&
80          "Alignment is greater than MaximumAlignment!");
81   unsigned AlignmentData = Log2_32(Align) + 1;
82   unsigned OldData = getGlobalValueSubClassData();
83   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
84   assert(getAlignment() == Align && "Alignment representation error!");
85 }
86
87 unsigned GlobalObject::getGlobalObjectSubClassData() const {
88   unsigned ValueData = getGlobalValueSubClassData();
89   return ValueData >> AlignmentBits;
90 }
91
92 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
93   unsigned OldData = getGlobalValueSubClassData();
94   setGlobalValueSubClassData((OldData & AlignmentMask) |
95                              (Val << AlignmentBits));
96   assert(getGlobalObjectSubClassData() == Val && "representation error");
97 }
98
99 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
100   GlobalValue::copyAttributesFrom(Src);
101   if (const auto *GV = dyn_cast<GlobalObject>(Src)) {
102     setAlignment(GV->getAlignment());
103     setSection(GV->getSection());
104   }
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(Ty, Value::GlobalVariableVal,
152                    OperandTraits<GlobalVariable>::op_begin(this),
153                    InitVal != nullptr, Link, Name, AddressSpace),
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
164 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
165                                LinkageTypes Link, Constant *InitVal,
166                                const Twine &Name, GlobalVariable *Before,
167                                ThreadLocalMode TLMode, unsigned AddressSpace,
168                                bool isExternallyInitialized)
169     : GlobalObject(Ty, Value::GlobalVariableVal,
170                    OperandTraits<GlobalVariable>::op_begin(this),
171                    InitVal != nullptr, Link, Name, AddressSpace),
172       isConstantGlobal(constant),
173       isExternallyInitializedConstant(isExternallyInitialized) {
174   setThreadLocalMode(TLMode);
175   if (InitVal) {
176     assert(InitVal->getType() == Ty &&
177            "Initializer should be the same type as the GlobalVariable!");
178     Op<0>() = InitVal;
179   }
180
181   if (Before)
182     Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
183   else
184     M.getGlobalList().push_back(this);
185 }
186
187 void GlobalVariable::setParent(Module *parent) {
188   Parent = parent;
189 }
190
191 void GlobalVariable::removeFromParent() {
192   getParent()->getGlobalList().remove(getIterator());
193 }
194
195 void GlobalVariable::eraseFromParent() {
196   getParent()->getGlobalList().erase(getIterator());
197 }
198
199 void GlobalVariable::setInitializer(Constant *InitVal) {
200   if (!InitVal) {
201     if (hasInitializer()) {
202       // Note, the num operands is used to compute the offset of the operand, so
203       // the order here matters.  Clearing the operand then clearing the num
204       // operands ensures we have the correct offset to the operand.
205       Op<0>().set(nullptr);
206       setGlobalVariableNumOperands(0);
207     }
208   } else {
209     assert(InitVal->getType() == getType()->getElementType() &&
210            "Initializer type must match GlobalVariable type");
211     // Note, the num operands is used to compute the offset of the operand, so
212     // the order here matters.  We need to set num operands to 1 first so that
213     // we get the correct offset to the first operand when we set it.
214     if (!hasInitializer())
215       setGlobalVariableNumOperands(1);
216     Op<0>().set(InitVal);
217   }
218 }
219
220 /// Copy all additional attributes (those not needed to create a GlobalVariable)
221 /// from the GlobalVariable Src to this one.
222 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
223   GlobalObject::copyAttributesFrom(Src);
224   if (const GlobalVariable *SrcVar = dyn_cast<GlobalVariable>(Src)) {
225     setThreadLocalMode(SrcVar->getThreadLocalMode());
226     setExternallyInitialized(SrcVar->isExternallyInitialized());
227   }
228 }
229
230
231 //===----------------------------------------------------------------------===//
232 // GlobalAlias Implementation
233 //===----------------------------------------------------------------------===//
234
235 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
236                          const Twine &Name, Constant *Aliasee,
237                          Module *ParentModule)
238     : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name,
239                   AddressSpace) {
240   Op<0>() = Aliasee;
241
242   if (ParentModule)
243     ParentModule->getAliasList().push_back(this);
244 }
245
246 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
247                                  LinkageTypes Link, const Twine &Name,
248                                  Constant *Aliasee, Module *ParentModule) {
249   return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
250 }
251
252 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
253                                  LinkageTypes Linkage, const Twine &Name,
254                                  Module *Parent) {
255   return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
256 }
257
258 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
259                                  LinkageTypes Linkage, const Twine &Name,
260                                  GlobalValue *Aliasee) {
261   return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
262 }
263
264 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
265                                  GlobalValue *Aliasee) {
266   PointerType *PTy = Aliasee->getType();
267   return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
268                 Aliasee);
269 }
270
271 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
272   return create(Aliasee->getLinkage(), Name, Aliasee);
273 }
274
275 void GlobalAlias::setParent(Module *parent) {
276   Parent = parent;
277 }
278
279 void GlobalAlias::removeFromParent() {
280   getParent()->getAliasList().remove(getIterator());
281 }
282
283 void GlobalAlias::eraseFromParent() {
284   getParent()->getAliasList().erase(getIterator());
285 }
286
287 void GlobalAlias::setAliasee(Constant *Aliasee) {
288   assert((!Aliasee || Aliasee->getType() == getType()) &&
289          "Alias and aliasee types should match!");
290   setOperand(0, Aliasee);
291 }