blob: 5abe1f9ac40d6cfb447ba5409b73e7c080b2f664 [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25// GlobalValue Class
26//===----------------------------------------------------------------------===//
27
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000028/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
29/// it. This involves recursively eliminating any dead users of the
30/// constantexpr.
Chris Lattner6f884e02009-03-09 05:50:45 +000031static bool removeDeadUsersOfConstant(const Constant *C) {
Chris Lattner0145eb72004-07-18 08:12:57 +000032 if (isa<GlobalValue>(C)) return false; // Cannot remove this
33
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000034 while (!C->use_empty()) {
Chris Lattner6f884e02009-03-09 05:50:45 +000035 const Constant *User = dyn_cast<Constant>(C->use_back());
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000036 if (!User) return false; // Non-constant usage;
37 if (!removeDeadUsersOfConstant(User))
38 return false; // Constant wasn't dead
39 }
Chris Lattner0145eb72004-07-18 08:12:57 +000040
Chris Lattner6f884e02009-03-09 05:50:45 +000041 const_cast<Constant*>(C)->destroyConstant();
Reid Spencer3d169b12004-07-18 00:06:26 +000042 return true;
43}
Reid Spencer3d169b12004-07-18 00:06:26 +000044
45/// removeDeadConstantUsers - If there are any dead constant users dangling
46/// off of this global value, remove them. This method is useful for clients
47/// that want to check to see if a global is unused, but don't want to deal
48/// with potentially dead constants hanging off of the globals.
Chris Lattner6f884e02009-03-09 05:50:45 +000049void GlobalValue::removeDeadConstantUsers() const {
50 Value::use_const_iterator I = use_begin(), E = use_end();
51 Value::use_const_iterator LastNonDeadUser = E;
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000052 while (I != E) {
Chris Lattner6f884e02009-03-09 05:50:45 +000053 if (const Constant *User = dyn_cast<Constant>(*I)) {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000054 if (!removeDeadUsersOfConstant(User)) {
55 // If the constant wasn't dead, remember that this was the last live use
56 // and move on to the next constant.
57 LastNonDeadUser = I;
58 ++I;
59 } else {
60 // If the constant was dead, then the iterator is invalidated.
61 if (LastNonDeadUser == E) {
62 I = use_begin();
63 if (I == E) break;
64 } else {
65 I = LastNonDeadUser;
66 ++I;
67 }
68 }
Reid Spencer3d169b12004-07-18 00:06:26 +000069 } else {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000070 LastNonDeadUser = I;
71 ++I;
Reid Spencer3d169b12004-07-18 00:06:26 +000072 }
73 }
Reid Spencer3d169b12004-07-18 00:06:26 +000074}
75
Misha Brukmanb1c93172005-04-21 23:48:37 +000076/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000077/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000078void GlobalValue::destroyConstant() {
Reid Spencer3d169b12004-07-18 00:06:26 +000079 assert(0 && "You can't GV->destroyConstant()!");
80 abort();
81}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000082
83/// copyAttributesFrom - copy all additional attributes (those not needed to
84/// create a GlobalValue) from the GlobalValue Src to this one.
85void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
86 setAlignment(Src->getAlignment());
87 setSection(Src->getSection());
88 setVisibility(Src->getVisibility());
89}
90
91
Reid Spencer3d169b12004-07-18 00:06:26 +000092//===----------------------------------------------------------------------===//
93// GlobalVariable Implementation
94//===----------------------------------------------------------------------===//
95
96GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +000097 Constant *InitVal, const std::string &Name,
Christopher Lamb54dd24c2007-12-11 08:59:05 +000098 Module *ParentModule, bool ThreadLocal,
99 unsigned AddressSpace)
100 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000101 OperandTraits<GlobalVariable>::op_begin(this),
102 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000103 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000104 if (InitVal) {
105 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000106 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000107 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000108 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000109
110 LeakDetector::addGarbageObject(this);
111
112 if (ParentModule)
113 ParentModule->getGlobalList().push_back(this);
114}
115
Chris Lattner87732cf2006-09-30 21:31:26 +0000116GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000117 Constant *InitVal, const std::string &Name,
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000118 GlobalVariable *Before, bool ThreadLocal,
119 unsigned AddressSpace)
120 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000121 OperandTraits<GlobalVariable>::op_begin(this),
122 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000123 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000124 if (InitVal) {
125 assert(InitVal->getType() == Ty &&
126 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000127 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000128 }
129
130 LeakDetector::addGarbageObject(this);
131
132 if (Before)
133 Before->getParent()->getGlobalList().insert(Before, this);
134}
135
Reid Spencer3d169b12004-07-18 00:06:26 +0000136void GlobalVariable::setParent(Module *parent) {
137 if (getParent())
138 LeakDetector::addGarbageObject(this);
139 Parent = parent;
140 if (getParent())
141 LeakDetector::removeGarbageObject(this);
142}
143
Chris Lattner02a71e72004-10-11 22:21:39 +0000144void GlobalVariable::removeFromParent() {
145 getParent()->getGlobalList().remove(this);
146}
147
148void GlobalVariable::eraseFromParent() {
149 getParent()->getGlobalList().erase(this);
150}
151
Reid Spencer3d169b12004-07-18 00:06:26 +0000152void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000153 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000154 // If you call this, then you better know this GVar has a constant
155 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000157 "Attempt to replace uses of Constants on a GVar with no initializer");
158
159 // And, since you know it has an initializer, the From value better be
160 // the initializer :)
161 assert(getOperand(0) == From &&
162 "Attempt to replace wrong constant initializer in GVar");
163
164 // And, you better have a constant for the replacement value
165 assert(isa<Constant>(To) &&
166 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000167
Reid Spencer3d169b12004-07-18 00:06:26 +0000168 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000169 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000170}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000171
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000172/// copyAttributesFrom - copy all additional attributes (those not needed to
173/// create a GlobalVariable) from the GlobalVariable Src to this one.
174void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
175 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
176 GlobalValue::copyAttributesFrom(Src);
177 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
178 setThreadLocal(SrcVar->isThreadLocal());
179}
180
181
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000182//===----------------------------------------------------------------------===//
183// GlobalAlias Implementation
184//===----------------------------------------------------------------------===//
185
186GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000187 const std::string &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000188 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000189 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000190 LeakDetector::addGarbageObject(this);
191
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000192 if (aliasee)
193 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000194 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000195
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000196 if (ParentModule)
197 ParentModule->getAliasList().push_back(this);
198}
199
200void GlobalAlias::setParent(Module *parent) {
201 if (getParent())
202 LeakDetector::addGarbageObject(this);
203 Parent = parent;
204 if (getParent())
205 LeakDetector::removeGarbageObject(this);
206}
207
208void GlobalAlias::removeFromParent() {
209 getParent()->getAliasList().remove(this);
210}
211
212void GlobalAlias::eraseFromParent() {
213 getParent()->getAliasList().erase(this);
214}
215
216bool GlobalAlias::isDeclaration() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000217 const GlobalValue* AV = getAliasedGlobal();
218 if (AV)
219 return AV->isDeclaration();
220 else
221 return false;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000222}
223
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000224void GlobalAlias::setAliasee(Constant *Aliasee)
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000225{
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000226 if (Aliasee)
227 assert(Aliasee->getType() == getType() &&
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000228 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000229
230 setOperand(0, Aliasee);
231}
232
Chris Lattnerc2d05302007-05-05 23:49:02 +0000233const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000234 const Constant *C = getAliasee();
235 if (C) {
236 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
237 return GV;
238 else {
239 const ConstantExpr *CE = 0;
Anton Korobeynikova30bc8f2007-04-30 10:28:40 +0000240 if ((CE = dyn_cast<ConstantExpr>(C)) &&
Chris Lattnerc2d05302007-05-05 23:49:02 +0000241 (CE->getOpcode() == Instruction::BitCast ||
242 CE->getOpcode() == Instruction::GetElementPtr))
243 return dyn_cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000244 else
245 assert(0 && "Unsupported aliasee");
246 }
Jeff Cohenee7bf762007-05-03 22:09:21 +0000247 }
248 return 0;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000249}
250
Anton Korobeynikov1a114042008-09-09 20:05:04 +0000251const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov2fb38972008-03-22 07:48:40 +0000252 SmallPtrSet<const GlobalValue*, 3> Visited;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000253
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000254 // Check if we need to stop early.
Duncan Sands715b2892009-01-08 20:55:49 +0000255 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000256 return this;
257
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000258 const GlobalValue *GV = getAliasedGlobal();
259 Visited.insert(GV);
260
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000261 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000262 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands715b2892009-01-08 20:55:49 +0000263 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000264 break;
265
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000266 GV = GA->getAliasedGlobal();
267
268 if (!Visited.insert(GV))
269 return NULL;
270 }
271
272 return GV;
273}