blob: 11152d5d6c21c9a941a62a192e0d36414f5abe00 [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"
Chandler Carruth4b6845c2014-03-04 12:46:06 +000021#include "llvm/IR/LeakDetector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.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 {
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
Rafael Espindola339430f2014-02-25 23:25:17 +000043const DataLayout *GlobalValue::getDataLayout() const {
44 return getParent()->getDataLayout();
45}
46
Misha Brukmanb1c93172005-04-21 23:48:37 +000047/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000048/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000049void GlobalValue::destroyConstant() {
Torok Edwinfbcc6632009-07-14 16:55:14 +000050 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000051}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000052
53/// copyAttributesFrom - copy all additional attributes (those not needed to
54/// create a GlobalValue) from the GlobalValue Src to this one.
55void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
56 setAlignment(Src->getAlignment());
57 setSection(Src->getSection());
58 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
Dan Gohman390914c2010-07-28 20:56:48 +000063void GlobalValue::setAlignment(unsigned Align) {
Rafael Espindola79c3ab72014-02-13 18:26:41 +000064 assert((!isa<GlobalAlias>(this) || !Align) &&
65 "GlobalAlias should not have an alignment!");
Dan Gohman390914c2010-07-28 20:56:48 +000066 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
67 assert(Align <= MaximumAlignment &&
68 "Alignment is greater than MaximumAlignment!");
69 Alignment = Log2_32(Align) + 1;
70 assert(getAlignment() == Align && "Alignment representation error!");
71}
Chris Lattner923053a2011-07-14 18:10:41 +000072
73bool GlobalValue::isDeclaration() const {
Chris Lattner85617212011-07-14 18:12:44 +000074 // Globals are definitions if they have an initializer.
Chris Lattner923053a2011-07-14 18:10:41 +000075 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
76 return GV->getNumOperands() == 0;
77
Chris Lattner85617212011-07-14 18:12:44 +000078 // Functions are definitions if they have a body.
Chris Lattner923053a2011-07-14 18:10:41 +000079 if (const Function *F = dyn_cast<Function>(this))
80 return F->empty();
Chris Lattner85617212011-07-14 18:12:44 +000081
Chris Lattner81210d22011-07-14 20:22:18 +000082 // Aliases are always definitions.
83 assert(isa<GlobalAlias>(this));
Chris Lattner923053a2011-07-14 18:10:41 +000084 return false;
85}
Dan Gohman390914c2010-07-28 20:56:48 +000086
Reid Spencer3d169b12004-07-18 00:06:26 +000087//===----------------------------------------------------------------------===//
88// GlobalVariable Implementation
89//===----------------------------------------------------------------------===//
90
Hans Wennborgcbe34b42012-06-23 11:37:03 +000091GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +000092 Constant *InitVal,
93 const Twine &Name, ThreadLocalMode TLMode,
94 unsigned AddressSpace,
95 bool isExternallyInitialized)
Hans Wennborgcbe34b42012-06-23 11:37:03 +000096 : GlobalValue(PointerType::get(Ty, AddressSpace),
97 Value::GlobalVariableVal,
98 OperandTraits<GlobalVariable>::op_begin(this),
99 InitVal != 0, Link, Name),
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000100 isConstantGlobal(constant), threadLocalMode(TLMode),
101 isExternallyInitializedConstant(isExternallyInitialized) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000102 if (InitVal) {
103 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000104 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000105 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000106 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000107
108 LeakDetector::addGarbageObject(this);
Reid Spencer3d169b12004-07-18 00:06:26 +0000109}
110
Chris Lattner229907c2011-07-18 04:54:35 +0000111GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersonb17f3292009-07-08 19:03:57 +0000112 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000113 const Twine &Name,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000114 GlobalVariable *Before, ThreadLocalMode TLMode,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000115 unsigned AddressSpace,
116 bool isExternallyInitialized)
Hans Wennborgac9fb362012-06-23 12:14:23 +0000117 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson785c56c2009-07-08 23:50:31 +0000118 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000119 OperandTraits<GlobalVariable>::op_begin(this),
120 InitVal != 0, Link, Name),
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000121 isConstantGlobal(constant), threadLocalMode(TLMode),
122 isExternallyInitializedConstant(isExternallyInitialized) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000123 if (InitVal) {
124 assert(InitVal->getType() == Ty &&
125 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000126 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000127 }
128
129 LeakDetector::addGarbageObject(this);
130
131 if (Before)
132 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000133 else
134 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000135}
136
Reid Spencer3d169b12004-07-18 00:06:26 +0000137void GlobalVariable::setParent(Module *parent) {
138 if (getParent())
139 LeakDetector::addGarbageObject(this);
140 Parent = parent;
141 if (getParent())
142 LeakDetector::removeGarbageObject(this);
143}
144
Chris Lattner02a71e72004-10-11 22:21:39 +0000145void GlobalVariable::removeFromParent() {
146 getParent()->getGlobalList().remove(this);
147}
148
149void GlobalVariable::eraseFromParent() {
150 getParent()->getGlobalList().erase(this);
151}
152
Reid Spencer3d169b12004-07-18 00:06:26 +0000153void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000154 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000155 // If you call this, then you better know this GVar has a constant
156 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000157 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000158 "Attempt to replace uses of Constants on a GVar with no initializer");
159
160 // And, since you know it has an initializer, the From value better be
161 // the initializer :)
162 assert(getOperand(0) == From &&
163 "Attempt to replace wrong constant initializer in GVar");
164
165 // And, you better have a constant for the replacement value
166 assert(isa<Constant>(To) &&
167 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000168
Reid Spencer3d169b12004-07-18 00:06:26 +0000169 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000170 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000171}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000172
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000173void GlobalVariable::setInitializer(Constant *InitVal) {
174 if (InitVal == 0) {
175 if (hasInitializer()) {
176 Op<0>().set(0);
177 NumOperands = 0;
178 }
179 } else {
180 assert(InitVal->getType() == getType()->getElementType() &&
181 "Initializer type must match GlobalVariable type");
182 if (!hasInitializer())
183 NumOperands = 1;
184 Op<0>().set(InitVal);
185 }
186}
187
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000188/// copyAttributesFrom - copy all additional attributes (those not needed to
189/// create a GlobalVariable) from the GlobalVariable Src to this one.
190void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
191 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
192 GlobalValue::copyAttributesFrom(Src);
193 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
Hans Wennborg4a4be112014-02-10 17:13:56 +0000194 setThreadLocalMode(SrcVar->getThreadLocalMode());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000195}
196
197
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000198//===----------------------------------------------------------------------===//
199// GlobalAlias Implementation
200//===----------------------------------------------------------------------===//
201
Chris Lattner229907c2011-07-18 04:54:35 +0000202GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000203 const Twine &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000204 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000205 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000206 LeakDetector::addGarbageObject(this);
207
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000208 if (aliasee)
209 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000210 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000211
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000212 if (ParentModule)
213 ParentModule->getAliasList().push_back(this);
214}
215
216void GlobalAlias::setParent(Module *parent) {
217 if (getParent())
218 LeakDetector::addGarbageObject(this);
219 Parent = parent;
220 if (getParent())
221 LeakDetector::removeGarbageObject(this);
222}
223
224void GlobalAlias::removeFromParent() {
225 getParent()->getAliasList().remove(this);
226}
227
228void GlobalAlias::eraseFromParent() {
229 getParent()->getAliasList().erase(this);
230}
231
Chris Lattner79617812011-07-14 18:01:49 +0000232void GlobalAlias::setAliasee(Constant *Aliasee) {
233 assert((!Aliasee || Aliasee->getType() == getType()) &&
234 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000235
236 setOperand(0, Aliasee);
237}
238
Peter Collingbournef708c872013-08-19 23:13:33 +0000239GlobalValue *GlobalAlias::getAliasedGlobal() {
240 Constant *C = getAliasee();
Chris Lattner79617812011-07-14 18:01:49 +0000241 if (C == 0) return 0;
242
Peter Collingbournef708c872013-08-19 23:13:33 +0000243 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattner79617812011-07-14 18:01:49 +0000244 return GV;
245
Peter Collingbournef708c872013-08-19 23:13:33 +0000246 ConstantExpr *CE = cast<ConstantExpr>(C);
Matt Arsenault00436ea2014-01-02 20:55:01 +0000247 assert((CE->getOpcode() == Instruction::BitCast ||
248 CE->getOpcode() == Instruction::AddrSpaceCast ||
Chris Lattner79617812011-07-14 18:01:49 +0000249 CE->getOpcode() == Instruction::GetElementPtr) &&
250 "Unsupported aliasee");
251
Jay Foad7f4cd9e2011-08-01 12:28:01 +0000252 return cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000253}
254
Peter Collingbournef708c872013-08-19 23:13:33 +0000255GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
256 SmallPtrSet<GlobalValue*, 3> Visited;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000257
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000258 // Check if we need to stop early.
Duncan Sands715b2892009-01-08 20:55:49 +0000259 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000260 return this;
261
Peter Collingbournef708c872013-08-19 23:13:33 +0000262 GlobalValue *GV = getAliasedGlobal();
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000263 Visited.insert(GV);
264
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000265 // Iterate over aliasing chain, stopping on weak alias if necessary.
Peter Collingbournef708c872013-08-19 23:13:33 +0000266 while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands715b2892009-01-08 20:55:49 +0000267 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000268 break;
269
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000270 GV = GA->getAliasedGlobal();
271
272 if (!Visited.insert(GV))
Chris Lattner79617812011-07-14 18:01:49 +0000273 return 0;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000274 }
275
276 return GV;
277}