blob: 79a458c26f770ce1287b16748dd339ead88fb049 [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
Misha Brukmanb1c93172005-04-21 23:48:37 +000045/// Override destroyConstant 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.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000047void GlobalValue::destroyConstant() {
Torok Edwinfbcc6632009-07-14 16:55:14 +000048 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000049}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000050
51/// copyAttributesFrom - copy all additional attributes (those not needed to
52/// create a GlobalValue) from the GlobalValue Src to this one.
53void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
Duncan Sandsdd7daee2008-05-26 19:58:59 +000054 setVisibility(Src->getVisibility());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000055 setUnnamedAddr(Src->hasUnnamedAddr());
Rafael Espindola75ec01f2014-02-13 05:11:35 +000056 setDLLStorageClass(Src->getDLLStorageClass());
Duncan Sandsdd7daee2008-05-26 19:58:59 +000057}
58
Rafael Espindola64c1e182014-06-03 02:41:57 +000059unsigned 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.
David Majnemerdad0a642014-06-27 18:19:56 +000062 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola64c1e182014-06-03 02:41:57 +000063 return GO->getAlignment();
64
65 // FIXME: we should also be able to handle:
66 // Alias = Global + Offset
67 // Alias = Absolute
68 return 0;
69 }
Rafael Espindola99e05cf2014-05-13 18:45:48 +000070 return cast<GlobalObject>(this)->getAlignment();
Rafael Espindola52dc5d82014-05-06 16:48:58 +000071}
72
Rafael Espindola99e05cf2014-05-13 18:45:48 +000073void GlobalObject::setAlignment(unsigned Align) {
Dan Gohman390914c2010-07-28 20:56:48 +000074 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
75 assert(Align <= MaximumAlignment &&
76 "Alignment is greater than MaximumAlignment!");
Rafael Espindolad4bcefc2014-10-24 18:13:04 +000077 unsigned AlignmentData = Log2_32(Align) + 1;
78 unsigned OldData = getGlobalValueSubClassData();
79 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
Dan Gohman390914c2010-07-28 20:56:48 +000080 assert(getAlignment() == Align && "Alignment representation error!");
81}
Chris Lattner923053a2011-07-14 18:10:41 +000082
Rafael Espindolad4bcefc2014-10-24 18:13:04 +000083unsigned GlobalObject::getGlobalObjectSubClassData() const {
84 unsigned ValueData = getGlobalValueSubClassData();
85 return ValueData >> AlignmentBits;
86}
87
88void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
89 unsigned OldData = getGlobalValueSubClassData();
90 setGlobalValueSubClassData((OldData & AlignmentMask) |
91 (Val << AlignmentBits));
92 assert(getGlobalObjectSubClassData() == Val && "representation error");
93}
94
Rafael Espindola99e05cf2014-05-13 18:45:48 +000095void 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
Rafael Espindola64c1e182014-06-03 02:41:57 +0000102const 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.
David Majnemerdad0a642014-06-27 18:19:56 +0000105 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola64c1e182014-06-03 02:41:57 +0000106 return GO->getSection();
107 return "";
108 }
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000109 return cast<GlobalObject>(this)->getSection();
Rafael Espindola8d8f1002014-05-06 22:44:30 +0000110}
111
David Majnemerdad0a642014-06-27 18:19:56 +0000112Comdat *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
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000122void GlobalObject::setSection(StringRef S) { Section = S; }
Rafael Espindola8fbbfbb2014-05-06 14:59:14 +0000123
Chris Lattner923053a2011-07-14 18:10:41 +0000124bool GlobalValue::isDeclaration() const {
Chris Lattner85617212011-07-14 18:12:44 +0000125 // Globals are definitions if they have an initializer.
Chris Lattner923053a2011-07-14 18:10:41 +0000126 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
127 return GV->getNumOperands() == 0;
128
Chris Lattner85617212011-07-14 18:12:44 +0000129 // Functions are definitions if they have a body.
Chris Lattner923053a2011-07-14 18:10:41 +0000130 if (const Function *F = dyn_cast<Function>(this))
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000131 return F->empty() && !F->isMaterializable();
Chris Lattner85617212011-07-14 18:12:44 +0000132
Chris Lattner81210d22011-07-14 20:22:18 +0000133 // Aliases are always definitions.
134 assert(isa<GlobalAlias>(this));
Chris Lattner923053a2011-07-14 18:10:41 +0000135 return false;
136}
Rafael Espindolaa03c5992014-05-09 14:31:07 +0000137
Reid Spencer3d169b12004-07-18 00:06:26 +0000138//===----------------------------------------------------------------------===//
139// GlobalVariable Implementation
140//===----------------------------------------------------------------------===//
141
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000142GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000143 Constant *InitVal, const Twine &Name,
144 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000145 bool isExternallyInitialized)
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000146 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
147 OperandTraits<GlobalVariable>::op_begin(this),
148 InitVal != nullptr, Link, Name),
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000149 isConstantGlobal(constant),
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000150 isExternallyInitializedConstant(isExternallyInitialized) {
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000151 setThreadLocalMode(TLMode);
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000152 if (InitVal) {
153 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000154 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000155 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000156 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000157}
158
Chris Lattner229907c2011-07-18 04:54:35 +0000159GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersonb17f3292009-07-08 19:03:57 +0000160 LinkageTypes Link, Constant *InitVal,
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000161 const Twine &Name, GlobalVariable *Before,
162 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000163 bool isExternallyInitialized)
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000164 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
165 OperandTraits<GlobalVariable>::op_begin(this),
166 InitVal != nullptr, Link, Name),
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000167 isConstantGlobal(constant),
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000168 isExternallyInitializedConstant(isExternallyInitialized) {
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000169 setThreadLocalMode(TLMode);
Chris Lattner87732cf2006-09-30 21:31:26 +0000170 if (InitVal) {
171 assert(InitVal->getType() == Ty &&
172 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000173 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000174 }
Rafael Espindolaa03c5992014-05-09 14:31:07 +0000175
Chris Lattner87732cf2006-09-30 21:31:26 +0000176 if (Before)
177 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000178 else
179 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000180}
181
Reid Spencer3d169b12004-07-18 00:06:26 +0000182void GlobalVariable::setParent(Module *parent) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000183 Parent = parent;
Reid Spencer3d169b12004-07-18 00:06:26 +0000184}
185
Chris Lattner02a71e72004-10-11 22:21:39 +0000186void GlobalVariable::removeFromParent() {
187 getParent()->getGlobalList().remove(this);
188}
189
190void GlobalVariable::eraseFromParent() {
191 getParent()->getGlobalList().erase(this);
192}
193
Reid Spencer3d169b12004-07-18 00:06:26 +0000194void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000195 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000196 // If you call this, then you better know this GVar has a constant
197 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000198 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000199 "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");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000209
Reid Spencer3d169b12004-07-18 00:06:26 +0000210 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000211 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000212}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000213
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000214void GlobalVariable::setInitializer(Constant *InitVal) {
Craig Topperc6207612014-04-09 06:08:46 +0000215 if (!InitVal) {
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000216 if (hasInitializer()) {
Pete Cooperb4eede22015-06-12 17:48:10 +0000217 // 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.
Craig Topperc6207612014-04-09 06:08:46 +0000220 Op<0>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000221 setGlobalVariableNumOperands(0);
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000222 }
223 } else {
224 assert(InitVal->getType() == getType()->getElementType() &&
225 "Initializer type must match GlobalVariable type");
Pete Cooperb4eede22015-06-12 17:48:10 +0000226 // 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.
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000229 if (!hasInitializer())
Pete Cooperb4eede22015-06-12 17:48:10 +0000230 setGlobalVariableNumOperands(1);
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000231 Op<0>().set(InitVal);
232 }
233}
234
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000235/// copyAttributesFrom - copy all additional attributes (those not needed to
236/// create a GlobalVariable) from the GlobalVariable Src to this one.
237void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
238 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000239 GlobalObject::copyAttributesFrom(Src);
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000240 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
Hans Wennborg4a4be112014-02-10 17:13:56 +0000241 setThreadLocalMode(SrcVar->getThreadLocalMode());
Rafael Espindola75b809c2014-11-10 18:41:59 +0000242 setExternallyInitialized(SrcVar->isExternallyInitialized());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000243}
244
245
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000246//===----------------------------------------------------------------------===//
247// GlobalAlias Implementation
248//===----------------------------------------------------------------------===//
249
David Blaikief64246b2015-04-29 21:22:39 +0000250GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
251 Constant *Aliasee, Module *ParentModule)
252 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +0000253 Op<0>() = Aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000254
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000255 if (ParentModule)
256 ParentModule->getAliasList().push_back(this);
257}
258
David Blaikief64246b2015-04-29 21:22:39 +0000259GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
260 const Twine &Name, Constant *Aliasee,
261 Module *ParentModule) {
262 return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000263}
Rafael Espindola83705652014-05-17 19:57:46 +0000264
David Blaikief64246b2015-04-29 21:22:39 +0000265GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
266 const Twine &Name, Module *Parent) {
267 return create(Ty, Linkage, Name, nullptr, Parent);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000268}
Rafael Espindola83705652014-05-17 19:57:46 +0000269
David Blaikief64246b2015-04-29 21:22:39 +0000270GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
271 const Twine &Name, GlobalValue *Aliasee) {
272 return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000273}
Rafael Espindola83705652014-05-17 19:57:46 +0000274
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000275GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
Rafael Espindola64c1e182014-06-03 02:41:57 +0000276 GlobalValue *Aliasee) {
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000277 PointerType *PTy = Aliasee->getType();
David Blaikief64246b2015-04-29 21:22:39 +0000278 return create(PTy, Link, Name, Aliasee);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000279}
280
Rafael Espindola64c1e182014-06-03 02:41:57 +0000281GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000282 return create(Aliasee->getLinkage(), Name, Aliasee);
283}
Rafael Espindola83705652014-05-17 19:57:46 +0000284
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000285void GlobalAlias::setParent(Module *parent) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000286 Parent = parent;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000287}
288
289void GlobalAlias::removeFromParent() {
290 getParent()->getAliasList().remove(this);
291}
292
293void GlobalAlias::eraseFromParent() {
294 getParent()->getAliasList().erase(this);
295}
296
Rafael Espindola64c1e182014-06-03 02:41:57 +0000297void GlobalAlias::setAliasee(Constant *Aliasee) {
Patrik Hagglund31541712014-06-04 11:21:11 +0000298 assert((!Aliasee || Aliasee->getType() == getType()) &&
Rafael Espindola64c1e182014-06-03 02:41:57 +0000299 "Alias and aliasee types should match!");
300 setOperand(0, Aliasee);
301}