blob: b8acc45d1815614b5d9a17fa466fca5389f7b159 [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//
10// This file implements the GlobalValue & GlobalVariable classes for the VMCore
11// library.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +000015#include "llvm/Constants.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000016#include "llvm/GlobalVariable.h"
Anton Korobeynikova97b6942007-04-25 14:27:10 +000017#include "llvm/GlobalAlias.h"
Chris Lattnereef2fe72006-01-24 04:13:11 +000018#include "llvm/DerivedTypes.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000019#include "llvm/Module.h"
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +000020#include "llvm/ADT/SmallPtrSet.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000021#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/LeakDetector.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// GlobalValue Class
27//===----------------------------------------------------------------------===//
28
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000029bool GlobalValue::isMaterializable() const {
Nick Lewycky780d2fe2010-02-15 21:27:20 +000030 return getParent() && getParent()->isMaterializable(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000031}
32bool GlobalValue::isDematerializable() const {
Nick Lewycky780d2fe2010-02-15 21:27:20 +000033 return getParent() && getParent()->isDematerializable(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000034}
35bool GlobalValue::Materialize(std::string *ErrInfo) {
36 return getParent()->Materialize(this, ErrInfo);
37}
38void GlobalValue::Dematerialize() {
39 getParent()->Dematerialize(this);
40}
41
Misha Brukmanb1c93172005-04-21 23:48:37 +000042/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000043/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000044void GlobalValue::destroyConstant() {
Torok Edwinfbcc6632009-07-14 16:55:14 +000045 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000046}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000047
48/// copyAttributesFrom - copy all additional attributes (those not needed to
49/// create a GlobalValue) from the GlobalValue Src to this one.
50void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
51 setAlignment(Src->getAlignment());
52 setSection(Src->getSection());
53 setVisibility(Src->getVisibility());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000054 setUnnamedAddr(Src->hasUnnamedAddr());
Duncan Sandsdd7daee2008-05-26 19:58:59 +000055}
56
Dan Gohman390914c2010-07-28 20:56:48 +000057void GlobalValue::setAlignment(unsigned Align) {
58 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
59 assert(Align <= MaximumAlignment &&
60 "Alignment is greater than MaximumAlignment!");
61 Alignment = Log2_32(Align) + 1;
62 assert(getAlignment() == Align && "Alignment representation error!");
63}
Chris Lattner923053a2011-07-14 18:10:41 +000064
65bool GlobalValue::isDeclaration() const {
Chris Lattner85617212011-07-14 18:12:44 +000066 // Globals are definitions if they have an initializer.
Chris Lattner923053a2011-07-14 18:10:41 +000067 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
68 return GV->getNumOperands() == 0;
69
Chris Lattner85617212011-07-14 18:12:44 +000070 // Functions are definitions if they have a body.
Chris Lattner923053a2011-07-14 18:10:41 +000071 if (const Function *F = dyn_cast<Function>(this))
72 return F->empty();
Chris Lattner85617212011-07-14 18:12:44 +000073
Chris Lattner81210d22011-07-14 20:22:18 +000074 // Aliases are always definitions.
75 assert(isa<GlobalAlias>(this));
Chris Lattner923053a2011-07-14 18:10:41 +000076 return false;
77}
Dan Gohman390914c2010-07-28 20:56:48 +000078
Reid Spencer3d169b12004-07-18 00:06:26 +000079//===----------------------------------------------------------------------===//
80// GlobalVariable Implementation
81//===----------------------------------------------------------------------===//
82
Chris Lattner229907c2011-07-18 04:54:35 +000083GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Daniel Dunbar4975db62009-07-25 04:41:11 +000084 Constant *InitVal, const Twine &Name,
Owen Andersonb17f3292009-07-08 19:03:57 +000085 bool ThreadLocal, unsigned AddressSpace)
Owen Anderson4056ca92009-07-29 22:17:13 +000086 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson5948fdf2009-07-08 01:26:06 +000087 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 OperandTraits<GlobalVariable>::op_begin(this),
89 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +000090 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000091 if (InitVal) {
92 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000093 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +000094 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000095 }
Reid Spencer3d169b12004-07-18 00:06:26 +000096
97 LeakDetector::addGarbageObject(this);
Reid Spencer3d169b12004-07-18 00:06:26 +000098}
99
Chris Lattner229907c2011-07-18 04:54:35 +0000100GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersonb17f3292009-07-08 19:03:57 +0000101 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000102 const Twine &Name,
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000103 GlobalVariable *Before, bool ThreadLocal,
104 unsigned AddressSpace)
Owen Anderson4056ca92009-07-29 22:17:13 +0000105 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson785c56c2009-07-08 23:50:31 +0000106 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000107 OperandTraits<GlobalVariable>::op_begin(this),
108 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000109 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000110 if (InitVal) {
111 assert(InitVal->getType() == Ty &&
112 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000113 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000114 }
115
116 LeakDetector::addGarbageObject(this);
117
118 if (Before)
119 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000120 else
121 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000122}
123
Reid Spencer3d169b12004-07-18 00:06:26 +0000124void GlobalVariable::setParent(Module *parent) {
125 if (getParent())
126 LeakDetector::addGarbageObject(this);
127 Parent = parent;
128 if (getParent())
129 LeakDetector::removeGarbageObject(this);
130}
131
Chris Lattner02a71e72004-10-11 22:21:39 +0000132void GlobalVariable::removeFromParent() {
133 getParent()->getGlobalList().remove(this);
134}
135
136void GlobalVariable::eraseFromParent() {
137 getParent()->getGlobalList().erase(this);
138}
139
Reid Spencer3d169b12004-07-18 00:06:26 +0000140void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000141 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000142 // If you call this, then you better know this GVar has a constant
143 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000144 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000145 "Attempt to replace uses of Constants on a GVar with no initializer");
146
147 // And, since you know it has an initializer, the From value better be
148 // the initializer :)
149 assert(getOperand(0) == From &&
150 "Attempt to replace wrong constant initializer in GVar");
151
152 // And, you better have a constant for the replacement value
153 assert(isa<Constant>(To) &&
154 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000155
Reid Spencer3d169b12004-07-18 00:06:26 +0000156 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000157 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000158}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000159
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000160void GlobalVariable::setInitializer(Constant *InitVal) {
161 if (InitVal == 0) {
162 if (hasInitializer()) {
163 Op<0>().set(0);
164 NumOperands = 0;
165 }
166 } else {
167 assert(InitVal->getType() == getType()->getElementType() &&
168 "Initializer type must match GlobalVariable type");
169 if (!hasInitializer())
170 NumOperands = 1;
171 Op<0>().set(InitVal);
172 }
173}
174
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000175/// copyAttributesFrom - copy all additional attributes (those not needed to
176/// create a GlobalVariable) from the GlobalVariable Src to this one.
177void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
178 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
179 GlobalValue::copyAttributesFrom(Src);
180 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
181 setThreadLocal(SrcVar->isThreadLocal());
182}
183
184
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000185//===----------------------------------------------------------------------===//
186// GlobalAlias Implementation
187//===----------------------------------------------------------------------===//
188
Chris Lattner229907c2011-07-18 04:54:35 +0000189GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000190 const Twine &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000191 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000192 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000193 LeakDetector::addGarbageObject(this);
194
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000195 if (aliasee)
196 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000197 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000198
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000199 if (ParentModule)
200 ParentModule->getAliasList().push_back(this);
201}
202
203void GlobalAlias::setParent(Module *parent) {
204 if (getParent())
205 LeakDetector::addGarbageObject(this);
206 Parent = parent;
207 if (getParent())
208 LeakDetector::removeGarbageObject(this);
209}
210
211void GlobalAlias::removeFromParent() {
212 getParent()->getAliasList().remove(this);
213}
214
215void GlobalAlias::eraseFromParent() {
216 getParent()->getAliasList().erase(this);
217}
218
Chris Lattner79617812011-07-14 18:01:49 +0000219void GlobalAlias::setAliasee(Constant *Aliasee) {
220 assert((!Aliasee || Aliasee->getType() == getType()) &&
221 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000222
223 setOperand(0, Aliasee);
224}
225
Chris Lattnerc2d05302007-05-05 23:49:02 +0000226const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000227 const Constant *C = getAliasee();
Chris Lattner79617812011-07-14 18:01:49 +0000228 if (C == 0) return 0;
229
230 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
231 return GV;
232
233 const ConstantExpr *CE = cast<ConstantExpr>(C);
234 assert((CE->getOpcode() == Instruction::BitCast ||
235 CE->getOpcode() == Instruction::GetElementPtr) &&
236 "Unsupported aliasee");
237
238 return dyn_cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000239}
240
Anton Korobeynikov1a114042008-09-09 20:05:04 +0000241const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov2fb38972008-03-22 07:48:40 +0000242 SmallPtrSet<const GlobalValue*, 3> Visited;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000243
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000244 // Check if we need to stop early.
Duncan Sands715b2892009-01-08 20:55:49 +0000245 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000246 return this;
247
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000248 const GlobalValue *GV = getAliasedGlobal();
249 Visited.insert(GV);
250
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000251 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000252 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands715b2892009-01-08 20:55:49 +0000253 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000254 break;
255
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000256 GV = GA->getAliasedGlobal();
257
258 if (!Visited.insert(GV))
Chris Lattner79617812011-07-14 18:01:49 +0000259 return 0;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000260 }
261
262 return GV;
263}