blob: 021d6bf801297f0fd470eb2b3e88e33fc1bc88f9 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// GlobalValue Class
25//===----------------------------------------------------------------------===//
26
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000027/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
28/// it. This involves recursively eliminating any dead users of the
29/// constantexpr.
30static bool removeDeadUsersOfConstant(Constant *C) {
Chris Lattner0145eb72004-07-18 08:12:57 +000031 if (isa<GlobalValue>(C)) return false; // Cannot remove this
32
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000033 while (!C->use_empty()) {
34 Constant *User = dyn_cast<Constant>(C->use_back());
35 if (!User) return false; // Non-constant usage;
36 if (!removeDeadUsersOfConstant(User))
37 return false; // Constant wasn't dead
38 }
Chris Lattner0145eb72004-07-18 08:12:57 +000039
40 C->destroyConstant();
Reid Spencer3d169b12004-07-18 00:06:26 +000041 return true;
42}
Reid Spencer3d169b12004-07-18 00:06:26 +000043
44/// removeDeadConstantUsers - If there are any dead constant users dangling
45/// off of this global value, remove them. This method is useful for clients
46/// that want to check to see if a global is unused, but don't want to deal
47/// with potentially dead constants hanging off of the globals.
Chris Lattner2730e6a2004-07-19 00:55:35 +000048void GlobalValue::removeDeadConstantUsers() {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000049 Value::use_iterator I = use_begin(), E = use_end();
50 Value::use_iterator LastNonDeadUser = E;
51 while (I != E) {
52 if (Constant *User = dyn_cast<Constant>(*I)) {
53 if (!removeDeadUsersOfConstant(User)) {
54 // If the constant wasn't dead, remember that this was the last live use
55 // and move on to the next constant.
56 LastNonDeadUser = I;
57 ++I;
58 } else {
59 // If the constant was dead, then the iterator is invalidated.
60 if (LastNonDeadUser == E) {
61 I = use_begin();
62 if (I == E) break;
63 } else {
64 I = LastNonDeadUser;
65 ++I;
66 }
67 }
Reid Spencer3d169b12004-07-18 00:06:26 +000068 } else {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000069 LastNonDeadUser = I;
70 ++I;
Reid Spencer3d169b12004-07-18 00:06:26 +000071 }
72 }
Reid Spencer3d169b12004-07-18 00:06:26 +000073}
74
Misha Brukmanb1c93172005-04-21 23:48:37 +000075/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000076/// GlobalValue's because they shouldn't be treated like other constants.
77void GlobalValue::destroyConstant() {
78 assert(0 && "You can't GV->destroyConstant()!");
79 abort();
80}
Anton Korobeynikova97b6942007-04-25 14:27:10 +000081
Reid Spencer3d169b12004-07-18 00:06:26 +000082//===----------------------------------------------------------------------===//
83// GlobalVariable Implementation
84//===----------------------------------------------------------------------===//
85
86GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +000087 Constant *InitVal, const std::string &Name,
88 Module *ParentModule, bool ThreadLocal)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000089 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
90 &Initializer, InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +000091 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000092 if (InitVal) {
93 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000094 "Initializer should be the same type as the GlobalVariable!");
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000095 Initializer.init(InitVal, this);
96 } else {
97 Initializer.init(0, this);
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000098 }
Reid Spencer3d169b12004-07-18 00:06:26 +000099
100 LeakDetector::addGarbageObject(this);
101
102 if (ParentModule)
103 ParentModule->getGlobalList().push_back(this);
104}
105
Chris Lattner87732cf2006-09-30 21:31:26 +0000106GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000107 Constant *InitVal, const std::string &Name,
108 GlobalVariable *Before, bool ThreadLocal)
Chris Lattner87732cf2006-09-30 21:31:26 +0000109 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
110 &Initializer, InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000111 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000112 if (InitVal) {
113 assert(InitVal->getType() == Ty &&
114 "Initializer should be the same type as the GlobalVariable!");
115 Initializer.init(InitVal, this);
116 } else {
117 Initializer.init(0, this);
118 }
119
120 LeakDetector::addGarbageObject(this);
121
122 if (Before)
123 Before->getParent()->getGlobalList().insert(Before, this);
124}
125
Reid Spencer3d169b12004-07-18 00:06:26 +0000126void GlobalVariable::setParent(Module *parent) {
127 if (getParent())
128 LeakDetector::addGarbageObject(this);
129 Parent = parent;
130 if (getParent())
131 LeakDetector::removeGarbageObject(this);
132}
133
Chris Lattner02a71e72004-10-11 22:21:39 +0000134void GlobalVariable::removeFromParent() {
135 getParent()->getGlobalList().remove(this);
136}
137
138void GlobalVariable::eraseFromParent() {
139 getParent()->getGlobalList().erase(this);
140}
141
Reid Spencer3d169b12004-07-18 00:06:26 +0000142void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000143 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000144 // If you call this, then you better know this GVar has a constant
145 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000146 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000147 "Attempt to replace uses of Constants on a GVar with no initializer");
148
149 // And, since you know it has an initializer, the From value better be
150 // the initializer :)
151 assert(getOperand(0) == From &&
152 "Attempt to replace wrong constant initializer in GVar");
153
154 // And, you better have a constant for the replacement value
155 assert(isa<Constant>(To) &&
156 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000157
Reid Spencer3d169b12004-07-18 00:06:26 +0000158 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000159 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000160}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000161
162//===----------------------------------------------------------------------===//
163// GlobalAlias Implementation
164//===----------------------------------------------------------------------===//
165
166GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000167 const std::string &Name, Constant* aliasee,
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000168 Module *ParentModule)
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000169 : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000170 LeakDetector::addGarbageObject(this);
171
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000172 if (aliasee)
173 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
174 Aliasee.init(aliasee, this);
175
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000176 if (ParentModule)
177 ParentModule->getAliasList().push_back(this);
178}
179
180void GlobalAlias::setParent(Module *parent) {
181 if (getParent())
182 LeakDetector::addGarbageObject(this);
183 Parent = parent;
184 if (getParent())
185 LeakDetector::removeGarbageObject(this);
186}
187
188void GlobalAlias::removeFromParent() {
189 getParent()->getAliasList().remove(this);
190}
191
192void GlobalAlias::eraseFromParent() {
193 getParent()->getAliasList().erase(this);
194}
195
196bool GlobalAlias::isDeclaration() const {
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000197 const GlobalValue* AV = getAliasedGlobal();
198 if (AV)
199 return AV->isDeclaration();
200 else
201 return false;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000202}
203
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000204void GlobalAlias::setAliasee(Constant *Aliasee)
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000205{
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000206 if (Aliasee)
207 assert(Aliasee->getType() == getType() &&
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000208 "Alias and aliasee types should match!");
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000209
210 setOperand(0, Aliasee);
211}
212
213const GlobalValue *GlobalAlias::getAliasedGlobal() const {
214 const Constant *C = getAliasee();
215 if (C) {
216 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
217 return GV;
218 else {
219 const ConstantExpr *CE = 0;
Anton Korobeynikova30bc8f2007-04-30 10:28:40 +0000220 if ((CE = dyn_cast<ConstantExpr>(C)) &&
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000221 (CE->getOpcode() == Instruction::BitCast))
222 return cast<GlobalValue>(CE->getOperand(0));
223 else
224 assert(0 && "Unsupported aliasee");
225 }
Jeff Cohenee7bf762007-05-03 22:09:21 +0000226 }
227 return 0;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000228}
229