blob: c64b719095dae422c8b6e587c0e89e5fb680dfc4 [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
Reid Spencer3d169b12004-07-18 00:06:26 +000015#include "llvm/GlobalVariable.h"
Anton Korobeynikova97b6942007-04-25 14:27:10 +000016#include "llvm/GlobalAlias.h"
Chris Lattnereef2fe72006-01-24 04:13:11 +000017#include "llvm/DerivedTypes.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000018#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/LeakDetector.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// GlobalValue Class
24//===----------------------------------------------------------------------===//
25
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000026/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
27/// it. This involves recursively eliminating any dead users of the
28/// constantexpr.
29static bool removeDeadUsersOfConstant(Constant *C) {
Chris Lattner0145eb72004-07-18 08:12:57 +000030 if (isa<GlobalValue>(C)) return false; // Cannot remove this
31
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000032 while (!C->use_empty()) {
33 Constant *User = dyn_cast<Constant>(C->use_back());
34 if (!User) return false; // Non-constant usage;
35 if (!removeDeadUsersOfConstant(User))
36 return false; // Constant wasn't dead
37 }
Chris Lattner0145eb72004-07-18 08:12:57 +000038
39 C->destroyConstant();
Reid Spencer3d169b12004-07-18 00:06:26 +000040 return true;
41}
Reid Spencer3d169b12004-07-18 00:06:26 +000042
43/// removeDeadConstantUsers - If there are any dead constant users dangling
44/// off of this global value, remove them. This method is useful for clients
45/// that want to check to see if a global is unused, but don't want to deal
46/// with potentially dead constants hanging off of the globals.
Chris Lattner2730e6a2004-07-19 00:55:35 +000047void GlobalValue::removeDeadConstantUsers() {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000048 Value::use_iterator I = use_begin(), E = use_end();
49 Value::use_iterator LastNonDeadUser = E;
50 while (I != E) {
51 if (Constant *User = dyn_cast<Constant>(*I)) {
52 if (!removeDeadUsersOfConstant(User)) {
53 // If the constant wasn't dead, remember that this was the last live use
54 // and move on to the next constant.
55 LastNonDeadUser = I;
56 ++I;
57 } else {
58 // If the constant was dead, then the iterator is invalidated.
59 if (LastNonDeadUser == E) {
60 I = use_begin();
61 if (I == E) break;
62 } else {
63 I = LastNonDeadUser;
64 ++I;
65 }
66 }
Reid Spencer3d169b12004-07-18 00:06:26 +000067 } else {
Chris Lattnerdd89d9c2007-02-26 05:02:39 +000068 LastNonDeadUser = I;
69 ++I;
Reid Spencer3d169b12004-07-18 00:06:26 +000070 }
71 }
Reid Spencer3d169b12004-07-18 00:06:26 +000072}
73
Misha Brukmanb1c93172005-04-21 23:48:37 +000074/// Override destroyConstant to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000075/// GlobalValue's because they shouldn't be treated like other constants.
76void GlobalValue::destroyConstant() {
77 assert(0 && "You can't GV->destroyConstant()!");
78 abort();
79}
Anton Korobeynikova97b6942007-04-25 14:27:10 +000080
Reid Spencer3d169b12004-07-18 00:06:26 +000081//===----------------------------------------------------------------------===//
82// GlobalVariable Implementation
83//===----------------------------------------------------------------------===//
84
85GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +000086 Constant *InitVal, const std::string &Name,
87 Module *ParentModule, bool ThreadLocal)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000088 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
89 &Initializer, InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +000090 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000091 if (InitVal) {
92 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000093 "Initializer should be the same type as the GlobalVariable!");
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000094 Initializer.init(InitVal, this);
95 } else {
96 Initializer.init(0, this);
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +000097 }
Reid Spencer3d169b12004-07-18 00:06:26 +000098
99 LeakDetector::addGarbageObject(this);
100
101 if (ParentModule)
102 ParentModule->getGlobalList().push_back(this);
103}
104
Chris Lattner87732cf2006-09-30 21:31:26 +0000105GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000106 Constant *InitVal, const std::string &Name,
107 GlobalVariable *Before, bool ThreadLocal)
Chris Lattner87732cf2006-09-30 21:31:26 +0000108 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
109 &Initializer, InitVal != 0, Link, Name),
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000110 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
Chris Lattner87732cf2006-09-30 21:31:26 +0000111 if (InitVal) {
112 assert(InitVal->getType() == Ty &&
113 "Initializer should be the same type as the GlobalVariable!");
114 Initializer.init(InitVal, this);
115 } else {
116 Initializer.init(0, this);
117 }
118
119 LeakDetector::addGarbageObject(this);
120
121 if (Before)
122 Before->getParent()->getGlobalList().insert(Before, this);
123}
124
Reid Spencer3d169b12004-07-18 00:06:26 +0000125void GlobalVariable::setParent(Module *parent) {
126 if (getParent())
127 LeakDetector::addGarbageObject(this);
128 Parent = parent;
129 if (getParent())
130 LeakDetector::removeGarbageObject(this);
131}
132
Chris Lattner02a71e72004-10-11 22:21:39 +0000133void GlobalVariable::removeFromParent() {
134 getParent()->getGlobalList().remove(this);
135}
136
137void GlobalVariable::eraseFromParent() {
138 getParent()->getGlobalList().erase(this);
139}
140
Reid Spencer3d169b12004-07-18 00:06:26 +0000141void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +0000142 Use *U) {
Reid Spencer3d169b12004-07-18 00:06:26 +0000143 // If you call this, then you better know this GVar has a constant
144 // initializer worth replacing. Enforce that here.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000145 assert(getNumOperands() == 1 &&
Reid Spencer3d169b12004-07-18 00:06:26 +0000146 "Attempt to replace uses of Constants on a GVar with no initializer");
147
148 // And, since you know it has an initializer, the From value better be
149 // the initializer :)
150 assert(getOperand(0) == From &&
151 "Attempt to replace wrong constant initializer in GVar");
152
153 // And, you better have a constant for the replacement value
154 assert(isa<Constant>(To) &&
155 "Attempt to replace GVar initializer with non-constant");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156
Reid Spencer3d169b12004-07-18 00:06:26 +0000157 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner37b570a2004-08-04 02:27:17 +0000158 this->setOperand(0, cast<Constant>(To));
Reid Spencer3d169b12004-07-18 00:06:26 +0000159}
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000160
161//===----------------------------------------------------------------------===//
162// GlobalAlias Implementation
163//===----------------------------------------------------------------------===//
164
165GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
166 const std::string &Name, const GlobalValue* aliasee,
167 Module *ParentModule)
168 : GlobalValue(Ty, Value::GlobalAliasVal, 0, 0,
169 Link, Name), Aliasee(aliasee) {
170 LeakDetector::addGarbageObject(this);
171
172 if (ParentModule)
173 ParentModule->getAliasList().push_back(this);
174}
175
176void GlobalAlias::setParent(Module *parent) {
177 if (getParent())
178 LeakDetector::addGarbageObject(this);
179 Parent = parent;
180 if (getParent())
181 LeakDetector::removeGarbageObject(this);
182}
183
184void GlobalAlias::removeFromParent() {
185 getParent()->getAliasList().remove(this);
186}
187
188void GlobalAlias::eraseFromParent() {
189 getParent()->getAliasList().erase(this);
190}
191
192bool GlobalAlias::isDeclaration() const {
193 return (Aliasee && Aliasee->isDeclaration());
194}
195
196void GlobalAlias::setAliasee(const GlobalValue *GV)
197{
198 // FIXME: Some checks?
199 Aliasee = GV;
200}
201