blob: 94bf3dea9ab3ca05b275286d61dfc476709f6f05 [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
46/// removeDeadConstantUsers - If there are any dead constant users dangling
47/// off of this global value, remove them. This method is useful for clients
48/// that want to check to see if a global is unused, but don't want to deal
49/// with potentially dead constants hanging off of the globals.
Chris Lattner6f884e02009-03-09 05:50:45 +000050void GlobalValue::removeDeadConstantUsers() const {
51 Value::use_const_iterator I = use_begin(), E = use_end();
52 Value::use_const_iterator LastNonDeadUser = E;
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000053 while (I != E) {
Chris Lattner6f884e02009-03-09 05:50:45 +000054 if (const Constant *User = dyn_cast<Constant>(*I)) {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000055 if (!removeDeadUsersOfConstant(User)) {
56 // If the constant wasn't dead, remember that this was the last live use
57 // and move on to the next constant.
58 LastNonDeadUser = I;
59 ++I;
60 } else {
61 // If the constant was dead, then the iterator is invalidated.
62 if (LastNonDeadUser == E) {
63 I = use_begin();
64 if (I == E) break;
65 } else {
66 I = LastNonDeadUser;
67 ++I;
68 }
69 }
Reid Spencer3d169b12004-07-18 00:06:26 +000070 } else {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000071 LastNonDeadUser = I;
72 ++I;
Reid Spencer3d169b12004-07-18 00:06:26 +000073 }
74 }
Reid Spencer3d169b12004-07-18 00:06:26 +000075}
76
Chris Lattner27471742009-11-01 04:08:01 +000077
Misha Brukmanb1c93172005-04-21 23:48:37 +000078/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000079/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson0d2de8c2009-06-20 00:24:58 +000080void GlobalValue::destroyConstant() {
Torok Edwinfbcc6632009-07-14 16:55:14 +000081 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000082}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000083
84/// copyAttributesFrom - copy all additional attributes (those not needed to
85/// create a GlobalValue) from the GlobalValue Src to this one.
86void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
87 setAlignment(Src->getAlignment());
88 setSection(Src->getSection());
89 setVisibility(Src->getVisibility());
90}
91
92
Reid Spencer3d169b12004-07-18 00:06:26 +000093//===----------------------------------------------------------------------===//
94// GlobalVariable Implementation
95//===----------------------------------------------------------------------===//
96
Chris Lattner46b5c642009-11-06 04:27:31 +000097GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Daniel Dunbar4975db62009-07-25 04:41:11 +000098 Constant *InitVal, const Twine &Name,
Owen Andersonb17f3292009-07-08 19:03:57 +000099 bool ThreadLocal, unsigned AddressSpace)
Owen Anderson4056ca92009-07-29 22:17:13 +0000100 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson5948fdf2009-07-08 01:26:06 +0000101 Value::GlobalVariableVal,
Gabor Greiff6caff662008-05-10 08:32:32 +0000102 OperandTraits<GlobalVariable>::op_begin(this),
103 InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000104 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000105 if (InitVal) {
106 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000107 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000108 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000109 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000110
111 LeakDetector::addGarbageObject(this);
Reid Spencer3d169b12004-07-18 00:06:26 +0000112}
113
Owen Andersonb17f3292009-07-08 19:03:57 +0000114GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
115 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000116 const Twine &Name,
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000117 GlobalVariable *Before, bool ThreadLocal,
118 unsigned AddressSpace)
Owen Anderson4056ca92009-07-29 22:17:13 +0000119 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Anderson785c56c2009-07-08 23:50:31 +0000120 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);
Owen Andersonb17f3292009-07-08 19:03:57 +0000134 else
135 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000136}
137
Reid Spencer3d169b12004-07-18 00:06:26 +0000138void GlobalVariable::setParent(Module *parent) {
139 if (getParent())
140 LeakDetector::addGarbageObject(this);
141 Parent = parent;
142 if (getParent())
143 LeakDetector::removeGarbageObject(this);
144}
145
Chris Lattner02a71e72004-10-11 22:21:39 +0000146void GlobalVariable::removeFromParent() {
147 getParent()->getGlobalList().remove(this);
148}
149
150void GlobalVariable::eraseFromParent() {
151 getParent()->getGlobalList().erase(this);
152}
153
Reid Spencer3d169b12004-07-18 00:06:26 +0000154void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000155 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000156 // If you call this, then you better know this GVar has a constant
157 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000159 "Attempt to replace uses of Constants on a GVar with no initializer");
160
161 // And, since you know it has an initializer, the From value better be
162 // the initializer :)
163 assert(getOperand(0) == From &&
164 "Attempt to replace wrong constant initializer in GVar");
165
166 // And, you better have a constant for the replacement value
167 assert(isa<Constant>(To) &&
168 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000169
Reid Spencer3d169b12004-07-18 00:06:26 +0000170 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000171 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000172}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000173
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000174void GlobalVariable::setInitializer(Constant *InitVal) {
175 if (InitVal == 0) {
176 if (hasInitializer()) {
177 Op<0>().set(0);
178 NumOperands = 0;
179 }
180 } else {
181 assert(InitVal->getType() == getType()->getElementType() &&
182 "Initializer type must match GlobalVariable type");
183 if (!hasInitializer())
184 NumOperands = 1;
185 Op<0>().set(InitVal);
186 }
187}
188
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000189/// copyAttributesFrom - copy all additional attributes (those not needed to
190/// create a GlobalVariable) from the GlobalVariable Src to this one.
191void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
192 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
193 GlobalValue::copyAttributesFrom(Src);
194 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
195 setThreadLocal(SrcVar->isThreadLocal());
196}
197
198
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000199//===----------------------------------------------------------------------===//
200// GlobalAlias Implementation
201//===----------------------------------------------------------------------===//
202
203GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000204 const Twine &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000205 Module *ParentModule)
Gabor Greiff6caff662008-05-10 08:32:32 +0000206 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000207 LeakDetector::addGarbageObject(this);
208
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000209 if (aliasee)
210 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000211 Op<0>() = aliasee;
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000212
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000213 if (ParentModule)
214 ParentModule->getAliasList().push_back(this);
215}
216
217void GlobalAlias::setParent(Module *parent) {
218 if (getParent())
219 LeakDetector::addGarbageObject(this);
220 Parent = parent;
221 if (getParent())
222 LeakDetector::removeGarbageObject(this);
223}
224
225void GlobalAlias::removeFromParent() {
226 getParent()->getAliasList().remove(this);
227}
228
229void GlobalAlias::eraseFromParent() {
230 getParent()->getAliasList().erase(this);
231}
232
233bool GlobalAlias::isDeclaration() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000234 const GlobalValue* AV = getAliasedGlobal();
235 if (AV)
236 return AV->isDeclaration();
237 else
238 return false;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000239}
240
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000241void GlobalAlias::setAliasee(Constant *Aliasee)
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000242{
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000243 if (Aliasee)
244 assert(Aliasee->getType() == getType() &&
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000245 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000246
247 setOperand(0, Aliasee);
248}
249
Chris Lattnerc2d05302007-05-05 23:49:02 +0000250const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000251 const Constant *C = getAliasee();
252 if (C) {
253 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
254 return GV;
255 else {
256 const ConstantExpr *CE = 0;
Anton Korobeynikova30bc8f2007-04-30 10:28:40 +0000257 if ((CE = dyn_cast<ConstantExpr>(C)) &&
Chris Lattnerc2d05302007-05-05 23:49:02 +0000258 (CE->getOpcode() == Instruction::BitCast ||
259 CE->getOpcode() == Instruction::GetElementPtr))
260 return dyn_cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000261 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000262 llvm_unreachable("Unsupported aliasee");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000263 }
Jeff Cohenee7bf762007-05-03 22:09:21 +0000264 }
265 return 0;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000266}
267
Anton Korobeynikov1a114042008-09-09 20:05:04 +0000268const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov2fb38972008-03-22 07:48:40 +0000269 SmallPtrSet<const GlobalValue*, 3> Visited;
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000270
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000271 // Check if we need to stop early.
Duncan Sands715b2892009-01-08 20:55:49 +0000272 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000273 return this;
274
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000275 const GlobalValue *GV = getAliasedGlobal();
276 Visited.insert(GV);
277
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000278 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000279 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands715b2892009-01-08 20:55:49 +0000280 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikovac2c6552008-09-09 18:23:48 +0000281 break;
282
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +0000283 GV = GA->getAliasedGlobal();
284
285 if (!Visited.insert(GV))
286 return NULL;
287 }
288
289 return GV;
290}