blob: a70ea0f37b904a22e861d7c5f0a74ecc2736e515 [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"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/LeakDetector.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 {
Nick Lewycky780d2fe2010-02-15 21:27:20 +000031 return getParent() && getParent()->isMaterializable(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000032}
33bool GlobalValue::isDematerializable() const {
Nick Lewycky780d2fe2010-02-15 21:27:20 +000034 return getParent() && getParent()->isDematerializable(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000035}
36bool GlobalValue::Materialize(std::string *ErrInfo) {
37 return getParent()->Materialize(this, ErrInfo);
38}
39void GlobalValue::Dematerialize() {
40 getParent()->Dematerialize(this);
41}
42
Misha Brukmanb1c93172005-04-21 23:48:37 +000043/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000044/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000045void GlobalValue::destroyConstant() {
Torok Edwinfbcc6632009-07-14 16:55:14 +000046 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000047}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000048
49/// copyAttributesFrom - copy all additional attributes (those not needed to
50/// create a GlobalValue) from the GlobalValue Src to this one.
51void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52 setAlignment(Src->getAlignment());
53 setSection(Src->getSection());
54 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
Dan Gohman390914c2010-07-28 20:56:48 +000059void GlobalValue::setAlignment(unsigned Align) {
60 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
61 assert(Align <= MaximumAlignment &&
62 "Alignment is greater than MaximumAlignment!");
63 Alignment = Log2_32(Align) + 1;
64 assert(getAlignment() == Align && "Alignment representation error!");
65}
Chris Lattner923053a2011-07-14 18:10:41 +000066
67bool GlobalValue::isDeclaration() const {
Chris Lattner85617212011-07-14 18:12:44 +000068 // Globals are definitions if they have an initializer.
Chris Lattner923053a2011-07-14 18:10:41 +000069 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
70 return GV->getNumOperands() == 0;
71
Chris Lattner85617212011-07-14 18:12:44 +000072 // Functions are definitions if they have a body.
Chris Lattner923053a2011-07-14 18:10:41 +000073 if (const Function *F = dyn_cast<Function>(this))
74 return F->empty();
Chris Lattner85617212011-07-14 18:12:44 +000075
Chris Lattner81210d22011-07-14 20:22:18 +000076 // Aliases are always definitions.
77 assert(isa<GlobalAlias>(this));
Chris Lattner923053a2011-07-14 18:10:41 +000078 return false;
79}
Dan Gohman390914c2010-07-28 20:56:48 +000080
Reid Spencer3d169b12004-07-18 00:06:26 +000081//===----------------------------------------------------------------------===//
82// GlobalVariable Implementation
83//===----------------------------------------------------------------------===//
84
Hans Wennborgcbe34b42012-06-23 11:37:03 +000085GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +000086 Constant *InitVal,
87 const Twine &Name, ThreadLocalMode TLMode,
88 unsigned AddressSpace,
89 bool isExternallyInitialized)
Hans Wennborgcbe34b42012-06-23 11:37:03 +000090 : GlobalValue(PointerType::get(Ty, AddressSpace),
91 Value::GlobalVariableVal,
92 OperandTraits<GlobalVariable>::op_begin(this),
93 InitVal != 0, Link, Name),
Michael Gottesman4c4ffd72013-02-03 21:54:38 +000094 isConstantGlobal(constant), threadLocalMode(TLMode),
95 isExternallyInitializedConstant(isExternallyInitialized) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000096 if (InitVal) {
97 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000098 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +000099 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000100 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000101
102 LeakDetector::addGarbageObject(this);
Reid Spencer3d169b12004-07-18 00:06:26 +0000103}
104
Chris Lattner229907c2011-07-18 04:54:35 +0000105GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersonb17f3292009-07-08 19:03:57 +0000106 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000107 const Twine &Name,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000108 GlobalVariable *Before, ThreadLocalMode TLMode,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000109 unsigned AddressSpace,
110 bool isExternallyInitialized)
Hans Wennborgac9fb362012-06-23 12:14:23 +0000111 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson785c56c2009-07-08 23:50:31 +0000112 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000113 OperandTraits<GlobalVariable>::op_begin(this),
114 InitVal != 0, Link, Name),
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000115 isConstantGlobal(constant), threadLocalMode(TLMode),
116 isExternallyInitializedConstant(isExternallyInitialized) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000117 if (InitVal) {
118 assert(InitVal->getType() == Ty &&
119 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000120 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000121 }
122
123 LeakDetector::addGarbageObject(this);
124
125 if (Before)
126 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000127 else
128 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000129}
130
Reid Spencer3d169b12004-07-18 00:06:26 +0000131void GlobalVariable::setParent(Module *parent) {
132 if (getParent())
133 LeakDetector::addGarbageObject(this);
134 Parent = parent;
135 if (getParent())
136 LeakDetector::removeGarbageObject(this);
137}
138
Chris Lattner02a71e72004-10-11 22:21:39 +0000139void GlobalVariable::removeFromParent() {
140 getParent()->getGlobalList().remove(this);
141}
142
143void GlobalVariable::eraseFromParent() {
144 getParent()->getGlobalList().erase(this);
145}
146
Reid Spencer3d169b12004-07-18 00:06:26 +0000147void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000148 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000149 // If you call this, then you better know this GVar has a constant
150 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000151 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000152 "Attempt to replace uses of Constants on a GVar with no initializer");
153
154 // And, since you know it has an initializer, the From value better be
155 // the initializer :)
156 assert(getOperand(0) == From &&
157 "Attempt to replace wrong constant initializer in GVar");
158
159 // And, you better have a constant for the replacement value
160 assert(isa<Constant>(To) &&
161 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000162
Reid Spencer3d169b12004-07-18 00:06:26 +0000163 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000164 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000165}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000166
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000167void GlobalVariable::setInitializer(Constant *InitVal) {
168 if (InitVal == 0) {
169 if (hasInitializer()) {
170 Op<0>().set(0);
171 NumOperands = 0;
172 }
173 } else {
174 assert(InitVal->getType() == getType()->getElementType() &&
175 "Initializer type must match GlobalVariable type");
176 if (!hasInitializer())
177 NumOperands = 1;
178 Op<0>().set(InitVal);
179 }
180}
181
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000182/// copyAttributesFrom - copy all additional attributes (those not needed to
183/// create a GlobalVariable) from the GlobalVariable Src to this one.
184void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
185 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
186 GlobalValue::copyAttributesFrom(Src);
187 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
Hans Wennborg4a4be112014-02-10 17:13:56 +0000188 setThreadLocalMode(SrcVar->getThreadLocalMode());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000189}
190
191
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000192//===----------------------------------------------------------------------===//
193// GlobalAlias Implementation
194//===----------------------------------------------------------------------===//
195
Chris Lattner229907c2011-07-18 04:54:35 +0000196GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000197 const Twine &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000198 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000199 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000200 LeakDetector::addGarbageObject(this);
201
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000202 if (aliasee)
203 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000204 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000205
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000206 if (ParentModule)
207 ParentModule->getAliasList().push_back(this);
208}
209
210void GlobalAlias::setParent(Module *parent) {
211 if (getParent())
212 LeakDetector::addGarbageObject(this);
213 Parent = parent;
214 if (getParent())
215 LeakDetector::removeGarbageObject(this);
216}
217
218void GlobalAlias::removeFromParent() {
219 getParent()->getAliasList().remove(this);
220}
221
222void GlobalAlias::eraseFromParent() {
223 getParent()->getAliasList().erase(this);
224}
225
Chris Lattner79617812011-07-14 18:01:49 +0000226void GlobalAlias::setAliasee(Constant *Aliasee) {
227 assert((!Aliasee || Aliasee->getType() == getType()) &&
228 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000229
230 setOperand(0, Aliasee);
231}
232
Peter Collingbournef708c872013-08-19 23:13:33 +0000233GlobalValue *GlobalAlias::getAliasedGlobal() {
234 Constant *C = getAliasee();
Chris Lattner79617812011-07-14 18:01:49 +0000235 if (C == 0) return 0;
236
Peter Collingbournef708c872013-08-19 23:13:33 +0000237 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattner79617812011-07-14 18:01:49 +0000238 return GV;
239
Peter Collingbournef708c872013-08-19 23:13:33 +0000240 ConstantExpr *CE = cast<ConstantExpr>(C);
Matt Arsenault00436ea2014-01-02 20:55:01 +0000241 assert((CE->getOpcode() == Instruction::BitCast ||
242 CE->getOpcode() == Instruction::AddrSpaceCast ||
Chris Lattner79617812011-07-14 18:01:49 +0000243 CE->getOpcode() == Instruction::GetElementPtr) &&
244 "Unsupported aliasee");
245
Jay Foad7f4cd9e2011-08-01 12:28:01 +0000246 return cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000247}
248
Peter Collingbournef708c872013-08-19 23:13:33 +0000249GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
250 SmallPtrSet<GlobalValue*, 3> Visited;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000251
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000252 // Check if we need to stop early.
Duncan Sands715b2892009-01-08 20:55:49 +0000253 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000254 return this;
255
Peter Collingbournef708c872013-08-19 23:13:33 +0000256 GlobalValue *GV = getAliasedGlobal();
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000257 Visited.insert(GV);
258
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000259 // Iterate over aliasing chain, stopping on weak alias if necessary.
Peter Collingbournef708c872013-08-19 23:13:33 +0000260 while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands715b2892009-01-08 20:55:49 +0000261 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000262 break;
263
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000264 GV = GA->getAliasedGlobal();
265
266 if (!Visited.insert(GV))
Chris Lattner79617812011-07-14 18:01:49 +0000267 return 0;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000268 }
269
270 return GV;
271}