blob: 8fde4b8e9d77546875e1596b2884bd00ed019552 [file] [log] [blame]
Chris Lattnereef2fe72006-01-24 04:13:11 +00001//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Reid Spencer3d169b12004-07-18 00:06:26 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Reid Spencer3d169b12004-07-18 00:06:26 +00008//===----------------------------------------------------------------------===//
9//
Chandler Carruthef860a22013-01-02 09:10:48 +000010// This file implements the GlobalValue & GlobalVariable classes for the IR
Reid Spencer3d169b12004-07-18 00:06:26 +000011// library.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/GlobalValue.h"
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +000016#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#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"
Rafael Espindola64c1e182014-06-03 02:41:57 +000022#include "llvm/IR/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// GlobalValue Class
28//===----------------------------------------------------------------------===//
29
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000030bool GlobalValue::isMaterializable() const {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +000031 if (const Function *F = dyn_cast<Function>(this))
32 return F->isMaterializable();
33 return false;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000034}
35bool GlobalValue::isDematerializable() const {
Nick Lewycky780d2fe2010-02-15 21:27:20 +000036 return getParent() && getParent()->isDematerializable(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000037}
Rafael Espindola5a52e6d2014-10-24 22:50:48 +000038std::error_code GlobalValue::materialize() {
39 return getParent()->materialize(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000040}
Eric Christopher97cb5652015-05-15 18:20:14 +000041void GlobalValue::dematerialize() {
42 getParent()->dematerialize(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000043}
44
Pete Cooper86dd4cf2015-06-23 21:55:11 +000045/// Override destroyConstantImpl to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000046/// GlobalValue's because they shouldn't be treated like other constants.
Pete Cooper86dd4cf2015-06-23 21:55:11 +000047void GlobalValue::destroyConstantImpl() {
48 llvm_unreachable("You can't GV->destroyConstantImpl()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000049}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000050
Pete Cooper5815b1f2015-06-24 18:55:24 +000051Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To, Use *U) {
52 llvm_unreachable("Unsupported class for handleOperandChange()!");
Pete Coopera272f7f2015-06-24 00:05:07 +000053}
54
Duncan Sandsdd7daee2008-05-26 19:58:59 +000055/// copyAttributesFrom - copy all additional attributes (those not needed to
56/// create a GlobalValue) from the GlobalValue Src to this one.
57void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
Duncan Sandsdd7daee2008-05-26 19:58:59 +000058 setVisibility(Src->getVisibility());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000059 setUnnamedAddr(Src->hasUnnamedAddr());
Rafael Espindola75ec01f2014-02-13 05:11:35 +000060 setDLLStorageClass(Src->getDLLStorageClass());
Duncan Sandsdd7daee2008-05-26 19:58:59 +000061}
62
Rafael Espindola64c1e182014-06-03 02:41:57 +000063unsigned 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.
David Majnemerdad0a642014-06-27 18:19:56 +000066 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola64c1e182014-06-03 02:41:57 +000067 return GO->getAlignment();
68
69 // FIXME: we should also be able to handle:
70 // Alias = Global + Offset
71 // Alias = Absolute
72 return 0;
73 }
Rafael Espindola99e05cf2014-05-13 18:45:48 +000074 return cast<GlobalObject>(this)->getAlignment();
Rafael Espindola52dc5d82014-05-06 16:48:58 +000075}
76
Rafael Espindola99e05cf2014-05-13 18:45:48 +000077void GlobalObject::setAlignment(unsigned Align) {
Dan Gohman390914c2010-07-28 20:56:48 +000078 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
79 assert(Align <= MaximumAlignment &&
80 "Alignment is greater than MaximumAlignment!");
Rafael Espindolad4bcefc2014-10-24 18:13:04 +000081 unsigned AlignmentData = Log2_32(Align) + 1;
82 unsigned OldData = getGlobalValueSubClassData();
83 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
Dan Gohman390914c2010-07-28 20:56:48 +000084 assert(getAlignment() == Align && "Alignment representation error!");
85}
Chris Lattner923053a2011-07-14 18:10:41 +000086
Rafael Espindolad4bcefc2014-10-24 18:13:04 +000087unsigned GlobalObject::getGlobalObjectSubClassData() const {
88 unsigned ValueData = getGlobalValueSubClassData();
89 return ValueData >> AlignmentBits;
90}
91
92void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
93 unsigned OldData = getGlobalValueSubClassData();
94 setGlobalValueSubClassData((OldData & AlignmentMask) |
95 (Val << AlignmentBits));
96 assert(getGlobalObjectSubClassData() == Val && "representation error");
97}
98
Rafael Espindola99e05cf2014-05-13 18:45:48 +000099void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
100 const auto *GV = cast<GlobalObject>(Src);
101 GlobalValue::copyAttributesFrom(GV);
102 setAlignment(GV->getAlignment());
103 setSection(GV->getSection());
104}
105
Rafael Espindola64c1e182014-06-03 02:41:57 +0000106const char *GlobalValue::getSection() const {
107 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
108 // In general we cannot compute this at the IR level, but we try.
David Majnemerdad0a642014-06-27 18:19:56 +0000109 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola64c1e182014-06-03 02:41:57 +0000110 return GO->getSection();
111 return "";
112 }
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000113 return cast<GlobalObject>(this)->getSection();
Rafael Espindola8d8f1002014-05-06 22:44:30 +0000114}
115
David Majnemerdad0a642014-06-27 18:19:56 +0000116Comdat *GlobalValue::getComdat() {
117 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
118 // In general we cannot compute this at the IR level, but we try.
119 if (const GlobalObject *GO = GA->getBaseObject())
120 return const_cast<GlobalObject *>(GO)->getComdat();
121 return nullptr;
122 }
123 return cast<GlobalObject>(this)->getComdat();
124}
125
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000126void GlobalObject::setSection(StringRef S) { Section = S; }
Rafael Espindola8fbbfbb2014-05-06 14:59:14 +0000127
Chris Lattner923053a2011-07-14 18:10:41 +0000128bool GlobalValue::isDeclaration() const {
Chris Lattner85617212011-07-14 18:12:44 +0000129 // Globals are definitions if they have an initializer.
Chris Lattner923053a2011-07-14 18:10:41 +0000130 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
131 return GV->getNumOperands() == 0;
132
Chris Lattner85617212011-07-14 18:12:44 +0000133 // Functions are definitions if they have a body.
Chris Lattner923053a2011-07-14 18:10:41 +0000134 if (const Function *F = dyn_cast<Function>(this))
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000135 return F->empty() && !F->isMaterializable();
Chris Lattner85617212011-07-14 18:12:44 +0000136
Chris Lattner81210d22011-07-14 20:22:18 +0000137 // Aliases are always definitions.
138 assert(isa<GlobalAlias>(this));
Chris Lattner923053a2011-07-14 18:10:41 +0000139 return false;
140}
Rafael Espindolaa03c5992014-05-09 14:31:07 +0000141
Reid Spencer3d169b12004-07-18 00:06:26 +0000142//===----------------------------------------------------------------------===//
143// GlobalVariable Implementation
144//===----------------------------------------------------------------------===//
145
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000146GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000147 Constant *InitVal, const Twine &Name,
148 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000149 bool isExternallyInitialized)
David Blaikied583b192015-08-21 21:35:28 +0000150 : GlobalObject(Ty, Value::GlobalVariableVal,
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000151 OperandTraits<GlobalVariable>::op_begin(this),
David Blaikied583b192015-08-21 21:35:28 +0000152 InitVal != nullptr, Link, Name, AddressSpace),
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000153 isConstantGlobal(constant),
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000154 isExternallyInitializedConstant(isExternallyInitialized) {
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000155 setThreadLocalMode(TLMode);
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000156 if (InitVal) {
157 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000158 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000159 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000160 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000161}
162
Chris Lattner229907c2011-07-18 04:54:35 +0000163GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersonb17f3292009-07-08 19:03:57 +0000164 LinkageTypes Link, Constant *InitVal,
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000165 const Twine &Name, GlobalVariable *Before,
166 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000167 bool isExternallyInitialized)
David Blaikied583b192015-08-21 21:35:28 +0000168 : GlobalObject(Ty, Value::GlobalVariableVal,
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000169 OperandTraits<GlobalVariable>::op_begin(this),
David Blaikied583b192015-08-21 21:35:28 +0000170 InitVal != nullptr, Link, Name, AddressSpace),
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000171 isConstantGlobal(constant),
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000172 isExternallyInitializedConstant(isExternallyInitialized) {
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000173 setThreadLocalMode(TLMode);
Chris Lattner87732cf2006-09-30 21:31:26 +0000174 if (InitVal) {
175 assert(InitVal->getType() == Ty &&
176 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000177 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000178 }
Rafael Espindolaa03c5992014-05-09 14:31:07 +0000179
Chris Lattner87732cf2006-09-30 21:31:26 +0000180 if (Before)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000181 Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000182 else
183 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000184}
185
Reid Spencer3d169b12004-07-18 00:06:26 +0000186void GlobalVariable::setParent(Module *parent) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000187 Parent = parent;
Reid Spencer3d169b12004-07-18 00:06:26 +0000188}
189
Chris Lattner02a71e72004-10-11 22:21:39 +0000190void GlobalVariable::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000191 getParent()->getGlobalList().remove(getIterator());
Chris Lattner02a71e72004-10-11 22:21:39 +0000192}
193
194void GlobalVariable::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000195 getParent()->getGlobalList().erase(getIterator());
Chris Lattner02a71e72004-10-11 22:21:39 +0000196}
197
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000198void GlobalVariable::setInitializer(Constant *InitVal) {
Craig Topperc6207612014-04-09 06:08:46 +0000199 if (!InitVal) {
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000200 if (hasInitializer()) {
Pete Cooperb4eede22015-06-12 17:48:10 +0000201 // Note, the num operands is used to compute the offset of the operand, so
202 // the order here matters. Clearing the operand then clearing the num
203 // operands ensures we have the correct offset to the operand.
Craig Topperc6207612014-04-09 06:08:46 +0000204 Op<0>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000205 setGlobalVariableNumOperands(0);
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000206 }
207 } else {
208 assert(InitVal->getType() == getType()->getElementType() &&
209 "Initializer type must match GlobalVariable type");
Pete Cooperb4eede22015-06-12 17:48:10 +0000210 // Note, the num operands is used to compute the offset of the operand, so
211 // the order here matters. We need to set num operands to 1 first so that
212 // we get the correct offset to the first operand when we set it.
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000213 if (!hasInitializer())
Pete Cooperb4eede22015-06-12 17:48:10 +0000214 setGlobalVariableNumOperands(1);
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000215 Op<0>().set(InitVal);
216 }
217}
218
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000219/// copyAttributesFrom - copy all additional attributes (those not needed to
220/// create a GlobalVariable) from the GlobalVariable Src to this one.
221void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
222 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000223 GlobalObject::copyAttributesFrom(Src);
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000224 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
Hans Wennborg4a4be112014-02-10 17:13:56 +0000225 setThreadLocalMode(SrcVar->getThreadLocalMode());
Rafael Espindola75b809c2014-11-10 18:41:59 +0000226 setExternallyInitialized(SrcVar->isExternallyInitialized());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000227}
228
229
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000230//===----------------------------------------------------------------------===//
231// GlobalAlias Implementation
232//===----------------------------------------------------------------------===//
233
David Blaikie16a2f3e2015-09-14 18:01:59 +0000234GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
235 const Twine &Name, Constant *Aliasee,
236 Module *ParentModule)
David Blaikie798f3072015-09-14 21:47:27 +0000237 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name,
238 AddressSpace) {
Rafael Espindola4fe00942014-05-16 13:34:04 +0000239 Op<0>() = Aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000240
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000241 if (ParentModule)
242 ParentModule->getAliasList().push_back(this);
243}
244
David Blaikie16a2f3e2015-09-14 18:01:59 +0000245GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
246 LinkageTypes Link, const Twine &Name,
247 Constant *Aliasee, Module *ParentModule) {
248 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000249}
Rafael Espindola83705652014-05-17 19:57:46 +0000250
David Blaikie16a2f3e2015-09-14 18:01:59 +0000251GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
252 LinkageTypes Linkage, const Twine &Name,
253 Module *Parent) {
254 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000255}
Rafael Espindola83705652014-05-17 19:57:46 +0000256
David Blaikie16a2f3e2015-09-14 18:01:59 +0000257GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
258 LinkageTypes Linkage, const Twine &Name,
259 GlobalValue *Aliasee) {
260 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000261}
Rafael Espindola83705652014-05-17 19:57:46 +0000262
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000263GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
Rafael Espindola64c1e182014-06-03 02:41:57 +0000264 GlobalValue *Aliasee) {
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000265 PointerType *PTy = Aliasee->getType();
David Blaikie16a2f3e2015-09-14 18:01:59 +0000266 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
267 Aliasee);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000268}
269
Rafael Espindola64c1e182014-06-03 02:41:57 +0000270GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000271 return create(Aliasee->getLinkage(), Name, Aliasee);
272}
Rafael Espindola83705652014-05-17 19:57:46 +0000273
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000274void GlobalAlias::setParent(Module *parent) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000275 Parent = parent;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000276}
277
278void GlobalAlias::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000279 getParent()->getAliasList().remove(getIterator());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000280}
281
282void GlobalAlias::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000283 getParent()->getAliasList().erase(getIterator());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000284}
285
Rafael Espindola64c1e182014-06-03 02:41:57 +0000286void GlobalAlias::setAliasee(Constant *Aliasee) {
Patrik Hagglund31541712014-06-04 11:21:11 +0000287 assert((!Aliasee || Aliasee->getType() == getType()) &&
Rafael Espindola64c1e182014-06-03 02:41:57 +0000288 "Alias and aliasee types should match!");
289 setOperand(0, Aliasee);
290}