blob: adc7a82e6df10cc118bc48760d5fe5bae65830ba [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"
Owen Anderson5948fdf2009-07-08 01:26:06 +000019#include "llvm/LLVMContext.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000020#include "llvm/Module.h"
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +000021#include "llvm/ADT/SmallPtrSet.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"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000024#include "llvm/Support/Streams.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28// GlobalValue Class
29//===----------------------------------------------------------------------===//
30
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000031/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
32/// it. This involves recursively eliminating any dead users of the
33/// constantexpr.
Chris Lattner6f884e02009-03-09 05:50:45 +000034static bool removeDeadUsersOfConstant(const Constant *C) {
Chris Lattner0145eb72004-07-18 08:12:57 +000035 if (isa<GlobalValue>(C)) return false; // Cannot remove this
36
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000037 while (!C->use_empty()) {
Chris Lattner6f884e02009-03-09 05:50:45 +000038 const Constant *User = dyn_cast<Constant>(C->use_back());
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000039 if (!User) return false; // Non-constant usage;
40 if (!removeDeadUsersOfConstant(User))
41 return false; // Constant wasn't dead
42 }
Chris Lattner0145eb72004-07-18 08:12:57 +000043
Chris Lattner6f884e02009-03-09 05:50:45 +000044 const_cast<Constant*>(C)->destroyConstant();
Reid Spencer3d169b12004-07-18 00:06:26 +000045 return true;
46}
Reid Spencer3d169b12004-07-18 00:06:26 +000047
48/// removeDeadConstantUsers - If there are any dead constant users dangling
49/// off of this global value, remove them. This method is useful for clients
50/// that want to check to see if a global is unused, but don't want to deal
51/// with potentially dead constants hanging off of the globals.
Chris Lattner6f884e02009-03-09 05:50:45 +000052void GlobalValue::removeDeadConstantUsers() const {
53 Value::use_const_iterator I = use_begin(), E = use_end();
54 Value::use_const_iterator LastNonDeadUser = E;
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000055 while (I != E) {
Chris Lattner6f884e02009-03-09 05:50:45 +000056 if (const Constant *User = dyn_cast<Constant>(*I)) {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000057 if (!removeDeadUsersOfConstant(User)) {
58 // If the constant wasn't dead, remember that this was the last live use
59 // and move on to the next constant.
60 LastNonDeadUser = I;
61 ++I;
62 } else {
63 // If the constant was dead, then the iterator is invalidated.
64 if (LastNonDeadUser == E) {
65 I = use_begin();
66 if (I == E) break;
67 } else {
68 I = LastNonDeadUser;
69 ++I;
70 }
71 }
Reid Spencer3d169b12004-07-18 00:06:26 +000072 } else {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000073 LastNonDeadUser = I;
74 ++I;
Reid Spencer3d169b12004-07-18 00:06:26 +000075 }
76 }
Reid Spencer3d169b12004-07-18 00:06:26 +000077}
78
Misha Brukmanb1c93172005-04-21 23:48:37 +000079/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000080/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000081void GlobalValue::destroyConstant() {
Torok Edwin6dd27302009-07-08 18:01:40 +000082 LLVM_UNREACHABLE("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000083}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000084
85/// copyAttributesFrom - copy all additional attributes (those not needed to
86/// create a GlobalValue) from the GlobalValue Src to this one.
87void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
88 setAlignment(Src->getAlignment());
89 setSection(Src->getSection());
90 setVisibility(Src->getVisibility());
91}
92
93
Reid Spencer3d169b12004-07-18 00:06:26 +000094//===----------------------------------------------------------------------===//
95// GlobalVariable Implementation
96//===----------------------------------------------------------------------===//
97
Owen Anderson5948fdf2009-07-08 01:26:06 +000098GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty,
99 bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000100 Constant *InitVal, const std::string &Name,
Owen Andersonb17f3292009-07-08 19:03:57 +0000101 bool ThreadLocal, unsigned AddressSpace)
Owen Anderson5948fdf2009-07-08 01:26:06 +0000102 : GlobalValue(Context.getPointerType(Ty, AddressSpace),
103 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000104 OperandTraits<GlobalVariable>::op_begin(this),
105 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000106 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000107 if (InitVal) {
108 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000109 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000110 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000111 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000112
113 LeakDetector::addGarbageObject(this);
Reid Spencer3d169b12004-07-18 00:06:26 +0000114}
115
Owen Andersonb17f3292009-07-08 19:03:57 +0000116GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
117 LinkageTypes Link, Constant *InitVal,
118 const std::string &Name,
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000119 GlobalVariable *Before, bool ThreadLocal,
120 unsigned AddressSpace)
Owen Anderson785c56c2009-07-08 23:50:31 +0000121 : GlobalValue(M.getContext().getPointerType(Ty, AddressSpace),
122 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000123 OperandTraits<GlobalVariable>::op_begin(this),
124 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000125 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000126 if (InitVal) {
127 assert(InitVal->getType() == Ty &&
128 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000129 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000130 }
131
132 LeakDetector::addGarbageObject(this);
133
134 if (Before)
135 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000136 else
137 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000138}
139
Reid Spencer3d169b12004-07-18 00:06:26 +0000140void GlobalVariable::setParent(Module *parent) {
141 if (getParent())
142 LeakDetector::addGarbageObject(this);
143 Parent = parent;
144 if (getParent())
145 LeakDetector::removeGarbageObject(this);
146}
147
Chris Lattner02a71e72004-10-11 22:21:39 +0000148void GlobalVariable::removeFromParent() {
149 getParent()->getGlobalList().remove(this);
150}
151
152void GlobalVariable::eraseFromParent() {
153 getParent()->getGlobalList().erase(this);
154}
155
Reid Spencer3d169b12004-07-18 00:06:26 +0000156void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000157 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000158 // If you call this, then you better know this GVar has a constant
159 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000160 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000161 "Attempt to replace uses of Constants on a GVar with no initializer");
162
163 // And, since you know it has an initializer, the From value better be
164 // the initializer :)
165 assert(getOperand(0) == From &&
166 "Attempt to replace wrong constant initializer in GVar");
167
168 // And, you better have a constant for the replacement value
169 assert(isa<Constant>(To) &&
170 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171
Reid Spencer3d169b12004-07-18 00:06:26 +0000172 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000173 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000174}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000175
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000176/// copyAttributesFrom - copy all additional attributes (those not needed to
177/// create a GlobalVariable) from the GlobalVariable Src to this one.
178void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
179 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
180 GlobalValue::copyAttributesFrom(Src);
181 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
182 setThreadLocal(SrcVar->isThreadLocal());
183}
184
185
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000186//===----------------------------------------------------------------------===//
187// GlobalAlias Implementation
188//===----------------------------------------------------------------------===//
189
190GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000191 const std::string &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000192 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000193 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000194 LeakDetector::addGarbageObject(this);
195
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000196 if (aliasee)
197 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000198 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000199
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000200 if (ParentModule)
201 ParentModule->getAliasList().push_back(this);
202}
203
204void GlobalAlias::setParent(Module *parent) {
205 if (getParent())
206 LeakDetector::addGarbageObject(this);
207 Parent = parent;
208 if (getParent())
209 LeakDetector::removeGarbageObject(this);
210}
211
212void GlobalAlias::removeFromParent() {
213 getParent()->getAliasList().remove(this);
214}
215
216void GlobalAlias::eraseFromParent() {
217 getParent()->getAliasList().erase(this);
218}
219
220bool GlobalAlias::isDeclaration() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000221 const GlobalValue* AV = getAliasedGlobal();
222 if (AV)
223 return AV->isDeclaration();
224 else
225 return false;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000226}
227
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000228void GlobalAlias::setAliasee(Constant *Aliasee)
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000229{
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000230 if (Aliasee)
231 assert(Aliasee->getType() == getType() &&
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000232 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000233
234 setOperand(0, Aliasee);
235}
236
Chris Lattnerc2d05302007-05-05 23:49:02 +0000237const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000238 const Constant *C = getAliasee();
239 if (C) {
240 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
241 return GV;
242 else {
243 const ConstantExpr *CE = 0;
Anton Korobeynikova30bc8f2007-04-30 10:28:40 +0000244 if ((CE = dyn_cast<ConstantExpr>(C)) &&
Chris Lattnerc2d05302007-05-05 23:49:02 +0000245 (CE->getOpcode() == Instruction::BitCast ||
246 CE->getOpcode() == Instruction::GetElementPtr))
247 return dyn_cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000248 else
Torok Edwin6dd27302009-07-08 18:01:40 +0000249 LLVM_UNREACHABLE("Unsupported aliasee");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000250 }
Jeff Cohenee7bf762007-05-03 22:09:21 +0000251 }
252 return 0;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000253}
254
Anton Korobeynikov1a114042008-09-09 20:05:04 +0000255const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov2fb38972008-03-22 07:48:40 +0000256 SmallPtrSet<const 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
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000262 const GlobalValue *GV = getAliasedGlobal();
263 Visited.insert(GV);
264
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000265 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000266 while (const 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))
273 return NULL;
274 }
275
276 return GV;
277}