blob: dae22e48aea1294ed49da7c65de9f02c93ab20ac [file] [log] [blame]
Chris Lattnercc041ba2006-01-24 04:13:11 +00001//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Reid Spencere253cf62004-07-18 00:06:26 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
Reid Spencere253cf62004-07-18 00:06:26 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the GlobalValue & GlobalVariable classes for the VMCore
11// library.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +000015#include "llvm/Constants.h"
Reid Spencere253cf62004-07-18 00:06:26 +000016#include "llvm/GlobalVariable.h"
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000017#include "llvm/GlobalAlias.h"
Chris Lattnercc041ba2006-01-24 04:13:11 +000018#include "llvm/DerivedTypes.h"
Reid Spencere253cf62004-07-18 00:06:26 +000019#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Reid Spencere253cf62004-07-18 00:06:26 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// GlobalValue Class
25//===----------------------------------------------------------------------===//
26
Chris Lattner00f23ec2007-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 Lattnerda6cdfa2004-07-18 08:12:57 +000031 if (isa<GlobalValue>(C)) return false; // Cannot remove this
32
Chris Lattner00f23ec2007-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 Lattnerda6cdfa2004-07-18 08:12:57 +000039
40 C->destroyConstant();
Reid Spencere253cf62004-07-18 00:06:26 +000041 return true;
42}
Reid Spencere253cf62004-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 Lattnerf8083b72004-07-19 00:55:35 +000048void GlobalValue::removeDeadConstantUsers() {
Chris Lattner00f23ec2007-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 Spencere253cf62004-07-18 00:06:26 +000068 } else {
Chris Lattner00f23ec2007-02-26 05:02:39 +000069 LastNonDeadUser = I;
70 ++I;
Reid Spencere253cf62004-07-18 00:06:26 +000071 }
72 }
Reid Spencere253cf62004-07-18 00:06:26 +000073}
74
Misha Brukmanfd939082005-04-21 23:48:37 +000075/// Override destroyConstant to make sure it doesn't get called on
Reid Spencere253cf62004-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 Korobeynikov8b0a8c82007-04-25 14:27:10 +000081
Reid Spencere253cf62004-07-18 00:06:26 +000082//===----------------------------------------------------------------------===//
83// GlobalVariable Implementation
84//===----------------------------------------------------------------------===//
85
86GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +000087 Constant *InitVal, const std::string &Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +000088 Module *ParentModule, bool ThreadLocal,
89 unsigned AddressSpace)
90 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
Chris Lattner96d83f62005-01-29 00:35:33 +000091 &Initializer, InitVal != 0, Link, Name),
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +000092 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner96d83f62005-01-29 00:35:33 +000093 if (InitVal) {
94 assert(InitVal->getType() == Ty &&
Alkis Evlogimenos82439762004-08-05 11:28:34 +000095 "Initializer should be the same type as the GlobalVariable!");
Chris Lattner96d83f62005-01-29 00:35:33 +000096 Initializer.init(InitVal, this);
97 } else {
98 Initializer.init(0, this);
Alkis Evlogimenos82439762004-08-05 11:28:34 +000099 }
Reid Spencere253cf62004-07-18 00:06:26 +0000100
101 LeakDetector::addGarbageObject(this);
102
103 if (ParentModule)
104 ParentModule->getGlobalList().push_back(this);
105}
106
Chris Lattneradc95462006-09-30 21:31:26 +0000107GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000108 Constant *InitVal, const std::string &Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000109 GlobalVariable *Before, bool ThreadLocal,
110 unsigned AddressSpace)
111 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
Chris Lattneradc95462006-09-30 21:31:26 +0000112 &Initializer, InitVal != 0, Link, Name),
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000113 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattneradc95462006-09-30 21:31:26 +0000114 if (InitVal) {
115 assert(InitVal->getType() == Ty &&
116 "Initializer should be the same type as the GlobalVariable!");
117 Initializer.init(InitVal, this);
118 } else {
119 Initializer.init(0, this);
120 }
121
122 LeakDetector::addGarbageObject(this);
123
124 if (Before)
125 Before->getParent()->getGlobalList().insert(Before, this);
126}
127
Reid Spencere253cf62004-07-18 00:06:26 +0000128void GlobalVariable::setParent(Module *parent) {
129 if (getParent())
130 LeakDetector::addGarbageObject(this);
131 Parent = parent;
132 if (getParent())
133 LeakDetector::removeGarbageObject(this);
134}
135
Chris Lattner4b833802004-10-11 22:21:39 +0000136void GlobalVariable::removeFromParent() {
137 getParent()->getGlobalList().remove(this);
138}
139
140void GlobalVariable::eraseFromParent() {
141 getParent()->getGlobalList().erase(this);
142}
143
Reid Spencere253cf62004-07-18 00:06:26 +0000144void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +0000145 Use *U) {
Reid Spencere253cf62004-07-18 00:06:26 +0000146 // If you call this, then you better know this GVar has a constant
147 // initializer worth replacing. Enforce that here.
Misha Brukmanfd939082005-04-21 23:48:37 +0000148 assert(getNumOperands() == 1 &&
Reid Spencere253cf62004-07-18 00:06:26 +0000149 "Attempt to replace uses of Constants on a GVar with no initializer");
150
151 // And, since you know it has an initializer, the From value better be
152 // the initializer :)
153 assert(getOperand(0) == From &&
154 "Attempt to replace wrong constant initializer in GVar");
155
156 // And, you better have a constant for the replacement value
157 assert(isa<Constant>(To) &&
158 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanfd939082005-04-21 23:48:37 +0000159
Reid Spencere253cf62004-07-18 00:06:26 +0000160 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner07d7c9d2004-08-04 02:27:17 +0000161 this->setOperand(0, cast<Constant>(To));
Reid Spencere253cf62004-07-18 00:06:26 +0000162}
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000163
164//===----------------------------------------------------------------------===//
165// GlobalAlias Implementation
166//===----------------------------------------------------------------------===//
167
168GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000169 const std::string &Name, Constant* aliasee,
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000170 Module *ParentModule)
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000171 : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000172 LeakDetector::addGarbageObject(this);
173
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000174 if (aliasee)
175 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
176 Aliasee.init(aliasee, this);
177
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000178 if (ParentModule)
179 ParentModule->getAliasList().push_back(this);
180}
181
182void GlobalAlias::setParent(Module *parent) {
183 if (getParent())
184 LeakDetector::addGarbageObject(this);
185 Parent = parent;
186 if (getParent())
187 LeakDetector::removeGarbageObject(this);
188}
189
190void GlobalAlias::removeFromParent() {
191 getParent()->getAliasList().remove(this);
192}
193
194void GlobalAlias::eraseFromParent() {
195 getParent()->getAliasList().erase(this);
196}
197
198bool GlobalAlias::isDeclaration() const {
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000199 const GlobalValue* AV = getAliasedGlobal();
200 if (AV)
201 return AV->isDeclaration();
202 else
203 return false;
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000204}
205
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000206void GlobalAlias::setAliasee(Constant *Aliasee)
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000207{
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000208 if (Aliasee)
209 assert(Aliasee->getType() == getType() &&
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000210 "Alias and aliasee types should match!");
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000211
212 setOperand(0, Aliasee);
213}
214
Chris Lattner2bf6e6a2007-05-05 23:49:02 +0000215const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000216 const Constant *C = getAliasee();
217 if (C) {
218 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
219 return GV;
220 else {
221 const ConstantExpr *CE = 0;
Anton Korobeynikovbb1f97c2007-04-30 10:28:40 +0000222 if ((CE = dyn_cast<ConstantExpr>(C)) &&
Chris Lattner2bf6e6a2007-05-05 23:49:02 +0000223 (CE->getOpcode() == Instruction::BitCast ||
224 CE->getOpcode() == Instruction::GetElementPtr))
225 return dyn_cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000226 else
227 assert(0 && "Unsupported aliasee");
228 }
Jeff Cohenb4dbd9e2007-05-03 22:09:21 +0000229 }
230 return 0;
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000231}
232