blob: f149c446b43b440eb2c9082f7f5e52389e1d8100 [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
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000029/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
30/// it. This involves recursively eliminating any dead users of the
31/// constantexpr.
Chris Lattner6f884e02009-03-09 05:50:45 +000032static bool removeDeadUsersOfConstant(const Constant *C) {
Chris Lattner0145eb72004-07-18 08:12:57 +000033 if (isa<GlobalValue>(C)) return false; // Cannot remove this
34
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000035 while (!C->use_empty()) {
Chris Lattner6f884e02009-03-09 05:50:45 +000036 const Constant *User = dyn_cast<Constant>(C->use_back());
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000037 if (!User) return false; // Non-constant usage;
38 if (!removeDeadUsersOfConstant(User))
39 return false; // Constant wasn't dead
40 }
Chris Lattner0145eb72004-07-18 08:12:57 +000041
Chris Lattner6f884e02009-03-09 05:50:45 +000042 const_cast<Constant*>(C)->destroyConstant();
Reid Spencer3d169b12004-07-18 00:06:26 +000043 return true;
44}
Reid Spencer3d169b12004-07-18 00:06:26 +000045
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000046bool GlobalValue::isMaterializable() const {
47 return getParent()->isMaterializable(this);
48}
49bool GlobalValue::isDematerializable() const {
50 return getParent()->isDematerializable(this);
51}
52bool GlobalValue::Materialize(std::string *ErrInfo) {
53 return getParent()->Materialize(this, ErrInfo);
54}
55void GlobalValue::Dematerialize() {
56 getParent()->Dematerialize(this);
57}
58
Reid Spencer3d169b12004-07-18 00:06:26 +000059/// removeDeadConstantUsers - If there are any dead constant users dangling
60/// off of this global value, remove them. This method is useful for clients
61/// that want to check to see if a global is unused, but don't want to deal
62/// with potentially dead constants hanging off of the globals.
Chris Lattner6f884e02009-03-09 05:50:45 +000063void GlobalValue::removeDeadConstantUsers() const {
64 Value::use_const_iterator I = use_begin(), E = use_end();
65 Value::use_const_iterator LastNonDeadUser = E;
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000066 while (I != E) {
Chris Lattner6f884e02009-03-09 05:50:45 +000067 if (const Constant *User = dyn_cast<Constant>(*I)) {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000068 if (!removeDeadUsersOfConstant(User)) {
69 // If the constant wasn't dead, remember that this was the last live use
70 // and move on to the next constant.
71 LastNonDeadUser = I;
72 ++I;
73 } else {
74 // If the constant was dead, then the iterator is invalidated.
75 if (LastNonDeadUser == E) {
76 I = use_begin();
77 if (I == E) break;
78 } else {
79 I = LastNonDeadUser;
80 ++I;
81 }
82 }
Reid Spencer3d169b12004-07-18 00:06:26 +000083 } else {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000084 LastNonDeadUser = I;
85 ++I;
Reid Spencer3d169b12004-07-18 00:06:26 +000086 }
87 }
Reid Spencer3d169b12004-07-18 00:06:26 +000088}
89
Chris Lattner27471742009-11-01 04:08:01 +000090
Misha Brukmanb1c93172005-04-21 23:48:37 +000091/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000092/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000093void GlobalValue::destroyConstant() {
Torok Edwinfbcc6632009-07-14 16:55:14 +000094 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000095}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000096
97/// copyAttributesFrom - copy all additional attributes (those not needed to
98/// create a GlobalValue) from the GlobalValue Src to this one.
99void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
100 setAlignment(Src->getAlignment());
101 setSection(Src->getSection());
102 setVisibility(Src->getVisibility());
103}
104
105
Reid Spencer3d169b12004-07-18 00:06:26 +0000106//===----------------------------------------------------------------------===//
107// GlobalVariable Implementation
108//===----------------------------------------------------------------------===//
109
Chris Lattner46b5c642009-11-06 04:27:31 +0000110GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000111 Constant *InitVal, const Twine &Name,
Owen Andersonb17f3292009-07-08 19:03:57 +0000112 bool ThreadLocal, unsigned AddressSpace)
Owen Anderson4056ca92009-07-29 22:17:13 +0000113 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson5948fdf2009-07-08 01:26:06 +0000114 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000115 OperandTraits<GlobalVariable>::op_begin(this),
116 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000117 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000118 if (InitVal) {
119 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000120 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000121 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000122 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000123
124 LeakDetector::addGarbageObject(this);
Reid Spencer3d169b12004-07-18 00:06:26 +0000125}
126
Owen Andersonb17f3292009-07-08 19:03:57 +0000127GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
128 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000129 const Twine &Name,
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000130 GlobalVariable *Before, bool ThreadLocal,
131 unsigned AddressSpace)
Owen Anderson4056ca92009-07-29 22:17:13 +0000132 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson785c56c2009-07-08 23:50:31 +0000133 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000134 OperandTraits<GlobalVariable>::op_begin(this),
135 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000136 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000137 if (InitVal) {
138 assert(InitVal->getType() == Ty &&
139 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000140 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000141 }
142
143 LeakDetector::addGarbageObject(this);
144
145 if (Before)
146 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000147 else
148 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000149}
150
Reid Spencer3d169b12004-07-18 00:06:26 +0000151void GlobalVariable::setParent(Module *parent) {
152 if (getParent())
153 LeakDetector::addGarbageObject(this);
154 Parent = parent;
155 if (getParent())
156 LeakDetector::removeGarbageObject(this);
157}
158
Chris Lattner02a71e72004-10-11 22:21:39 +0000159void GlobalVariable::removeFromParent() {
160 getParent()->getGlobalList().remove(this);
161}
162
163void GlobalVariable::eraseFromParent() {
164 getParent()->getGlobalList().erase(this);
165}
166
Reid Spencer3d169b12004-07-18 00:06:26 +0000167void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000168 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000169 // If you call this, then you better know this GVar has a constant
170 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000172 "Attempt to replace uses of Constants on a GVar with no initializer");
173
174 // And, since you know it has an initializer, the From value better be
175 // the initializer :)
176 assert(getOperand(0) == From &&
177 "Attempt to replace wrong constant initializer in GVar");
178
179 // And, you better have a constant for the replacement value
180 assert(isa<Constant>(To) &&
181 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000182
Reid Spencer3d169b12004-07-18 00:06:26 +0000183 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000184 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000185}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000186
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000187void GlobalVariable::setInitializer(Constant *InitVal) {
188 if (InitVal == 0) {
189 if (hasInitializer()) {
190 Op<0>().set(0);
191 NumOperands = 0;
192 }
193 } else {
194 assert(InitVal->getType() == getType()->getElementType() &&
195 "Initializer type must match GlobalVariable type");
196 if (!hasInitializer())
197 NumOperands = 1;
198 Op<0>().set(InitVal);
199 }
200}
201
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000202/// copyAttributesFrom - copy all additional attributes (those not needed to
203/// create a GlobalVariable) from the GlobalVariable Src to this one.
204void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
205 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
206 GlobalValue::copyAttributesFrom(Src);
207 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
208 setThreadLocal(SrcVar->isThreadLocal());
209}
210
211
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000212//===----------------------------------------------------------------------===//
213// GlobalAlias Implementation
214//===----------------------------------------------------------------------===//
215
216GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000217 const Twine &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000218 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000220 LeakDetector::addGarbageObject(this);
221
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000222 if (aliasee)
223 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000224 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000225
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000226 if (ParentModule)
227 ParentModule->getAliasList().push_back(this);
228}
229
230void GlobalAlias::setParent(Module *parent) {
231 if (getParent())
232 LeakDetector::addGarbageObject(this);
233 Parent = parent;
234 if (getParent())
235 LeakDetector::removeGarbageObject(this);
236}
237
238void GlobalAlias::removeFromParent() {
239 getParent()->getAliasList().remove(this);
240}
241
242void GlobalAlias::eraseFromParent() {
243 getParent()->getAliasList().erase(this);
244}
245
246bool GlobalAlias::isDeclaration() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000247 const GlobalValue* AV = getAliasedGlobal();
248 if (AV)
249 return AV->isDeclaration();
250 else
251 return false;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000252}
253
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000254void GlobalAlias::setAliasee(Constant *Aliasee)
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000255{
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000256 if (Aliasee)
257 assert(Aliasee->getType() == getType() &&
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000258 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000259
260 setOperand(0, Aliasee);
261}
262
Chris Lattnerc2d05302007-05-05 23:49:02 +0000263const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000264 const Constant *C = getAliasee();
265 if (C) {
266 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
267 return GV;
268 else {
269 const ConstantExpr *CE = 0;
Anton Korobeynikova30bc8f2007-04-30 10:28:40 +0000270 if ((CE = dyn_cast<ConstantExpr>(C)) &&
Chris Lattnerc2d05302007-05-05 23:49:02 +0000271 (CE->getOpcode() == Instruction::BitCast ||
272 CE->getOpcode() == Instruction::GetElementPtr))
273 return dyn_cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000274 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000275 llvm_unreachable("Unsupported aliasee");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000276 }
Jeff Cohenee7bf762007-05-03 22:09:21 +0000277 }
278 return 0;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000279}
280
Anton Korobeynikov1a114042008-09-09 20:05:04 +0000281const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov2fb38972008-03-22 07:48:40 +0000282 SmallPtrSet<const GlobalValue*, 3> Visited;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000283
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000284 // Check if we need to stop early.
Duncan Sands715b2892009-01-08 20:55:49 +0000285 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000286 return this;
287
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000288 const GlobalValue *GV = getAliasedGlobal();
289 Visited.insert(GV);
290
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000291 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000292 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands715b2892009-01-08 20:55:49 +0000293 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000294 break;
295
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000296 GV = GA->getAliasedGlobal();
297
298 if (!Visited.insert(GV))
299 return NULL;
300 }
301
302 return GV;
303}