blob: 4c76b2b5532c528832803784aef09769887a1e17 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +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//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Misha Brukmanb1c93172005-04-21 23:48:37 +000010// This file implements the Value and User classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2c6abea2002-10-09 23:12:59 +000014#include "llvm/Constant.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000015#include "llvm/DerivedTypes.h"
16#include "llvm/InstrTypes.h"
17#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000018#include "llvm/ValueSymbolTable.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner2f7c9632001-06-06 20:29:01 +000024//===----------------------------------------------------------------------===//
25// Value Class
26//===----------------------------------------------------------------------===//
27
Chris Lattner25450e32001-12-13 00:41:27 +000028static inline const Type *checkType(const Type *Ty) {
29 assert(Ty && "Value defined with a null type: Error!");
30 return Ty;
31}
32
Chris Lattner32ab6432007-02-12 05:18:08 +000033Value::Value(const Type *ty, unsigned scid)
Chris Lattnera29c92f2005-02-05 01:37:58 +000034 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
Chris Lattner32ab6432007-02-12 05:18:08 +000035 UseList(0), Name(0) {
Chris Lattnerbea72472004-07-06 17:44:17 +000036 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Misha Brukmanb1c93172005-04-21 23:48:37 +000037 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000038 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000039 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000040}
41
42Value::~Value() {
43#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000044 // Check to make sure that there are no uses of this value that are still
45 // around when the value is destroyed. If there are, then we have a dangling
46 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000047 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000048 // a <badref>
49 //
Chris Lattner4947e672005-02-01 01:24:21 +000050 if (use_begin() != use_end()) {
Bill Wendling6a462f12006-11-17 08:03:48 +000051 DOUT << "While deleting: " << *Ty << " %" << Name << "\n";
Chris Lattner4947e672005-02-01 01:24:21 +000052 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Bill Wendling6a462f12006-11-17 08:03:48 +000053 DOUT << "Use still stuck around after Def is destroyed:"
54 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 }
56#endif
Chris Lattner4947e672005-02-01 01:24:21 +000057 assert(use_begin() == use_end() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000058
59 // There should be no uses of this object anymore, remove it.
60 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000061}
62
Chris Lattner4947e672005-02-01 01:24:21 +000063/// hasNUses - Return true if this Value has exactly N users.
64///
65bool Value::hasNUses(unsigned N) const {
66 use_const_iterator UI = use_begin(), E = use_end();
67
68 for (; N; --N, ++UI)
69 if (UI == E) return false; // Too few.
70 return UI == E;
71}
72
Chris Lattnerd36552f2005-02-23 16:51:11 +000073/// hasNUsesOrMore - Return true if this value has N users or more. This is
74/// logically equivalent to getNumUses() >= N.
75///
76bool Value::hasNUsesOrMore(unsigned N) const {
77 use_const_iterator UI = use_begin(), E = use_end();
78
79 for (; N; --N, ++UI)
80 if (UI == E) return false; // Too few.
81
82 return true;
83}
84
85
Chris Lattner4947e672005-02-01 01:24:21 +000086/// getNumUses - This method computes the number of uses of this Value. This
87/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
88/// values.
89unsigned Value::getNumUses() const {
90 return (unsigned)std::distance(use_begin(), use_end());
91}
92
Chris Lattnerb6250822007-02-11 00:37:27 +000093static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +000094 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +000095 if (Instruction *I = dyn_cast<Instruction>(V)) {
96 if (BasicBlock *P = I->getParent())
97 if (Function *PP = P->getParent())
98 ST = &PP->getValueSymbolTable();
99 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
100 if (Function *P = BB->getParent())
101 ST = &P->getValueSymbolTable();
102 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
103 if (Module *P = GV->getParent())
104 ST = &P->getValueSymbolTable();
105 } else if (Argument *A = dyn_cast<Argument>(V)) {
106 if (Function *P = A->getParent())
107 ST = &P->getValueSymbolTable();
108 } else {
109 assert(isa<Constant>(V) && "Unknown value type!");
110 return true; // no name is setable for this.
111 }
112 return false;
113}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000114
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000115std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000116 if (Name == 0) return "";
117 return std::string(Name->getKeyData(),
118 Name->getKeyData()+Name->getKeyLength());
119}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000120
Chris Lattner32ab6432007-02-12 05:18:08 +0000121void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000122 setName(&name[0], name.size());
123}
124
Chris Lattnercb9a6262007-02-13 07:53:34 +0000125void Value::setName(const char *Name) {
126 setName(Name, Name ? strlen(Name) : 0);
127}
128
Chris Lattner1a5de582007-02-12 18:52:59 +0000129void Value::setName(const char *NameStr, unsigned NameLen) {
130 if (NameLen == 0 && !hasName()) return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000131 if (getType() != Type::VoidTy && "Cannot assign a name to void values!");
132
Chris Lattnerffb37782005-03-06 02:14:28 +0000133 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000134 ValueSymbolTable *ST;
135 if (getSymTab(this, ST))
136 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000137
Chris Lattner32ab6432007-02-12 05:18:08 +0000138 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000139 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000140 // Free the name for this value.
141 Name->Destroy();
142 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000143 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000144 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000145
146 if (Name) {
147 // Name isn't changing?
148 if (NameLen == Name->getKeyLength() &&
149 !memcmp(Name->getKeyData(), NameStr, NameLen))
150 return;
151 Name->Destroy();
152 }
153
154 // NOTE: Could optimize for the case the name is shrinking to not deallocate
155 // then reallocated.
156
157 // Create the new name.
158 Name = ValueName::Create(NameStr, NameStr+NameLen);
159 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000160 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000161 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000162
163 // NOTE: Could optimize for the case the name is shrinking to not deallocate
164 // then reallocated.
165 if (hasName()) {
166 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000167 if (NameLen == Name->getKeyLength() &&
168 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000169 return;
170
171 // Remove old name.
172 ST->removeValueName(Name);
173 Name->Destroy();
174 Name = 0;
175
Chris Lattner1a5de582007-02-12 18:52:59 +0000176 if (NameLen == 0)
177 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000178 }
179
180 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000181 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000182}
183
Chris Lattner1a5de582007-02-12 18:52:59 +0000184
Chris Lattnerb6250822007-02-11 00:37:27 +0000185/// takeName - transfer the name from V to this value, setting V's name to
186/// empty. It is an error to call V->takeName(V).
187void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000188 ValueSymbolTable *ST = 0;
189 // If this value has a name, drop it.
190 if (hasName()) {
191 // Get the symtab this is in.
192 if (getSymTab(this, ST)) {
193 // We can't set a name on this value, but we need to clear V's name if
194 // it has one.
195 if (V->hasName()) V->setName(0, 0);
196 return; // Cannot set a name on this value (e.g. constant).
197 }
198
199 // Remove old name.
200 if (ST)
201 ST->removeValueName(Name);
202 Name->Destroy();
203 Name = 0;
204 }
205
206 // Now we know that this has no name.
207
208 // If V has no name either, we're done.
209 if (!V->hasName()) return;
210
211 // Get this's symtab if we didn't before.
212 if (!ST) {
213 if (getSymTab(this, ST)) {
214 // Clear V's name.
215 V->setName(0, 0);
216 return; // Cannot set a name on this value (e.g. constant).
217 }
218 }
219
220 // Get V's ST, this should always succed, because V has a name.
221 ValueSymbolTable *VST;
222 bool Failure = getSymTab(V, VST);
223 assert(!Failure && "V has a name, so it should have a ST!");
224
225 // If these values are both in the same symtab, we can do this very fast.
226 // This works even if both values have no symtab yet.
227 if (ST == VST) {
228 // Take the name!
229 Name = V->Name;
230 V->Name = 0;
231 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000232 return;
233 }
234
Chris Lattnercf835ff2007-02-15 20:01:43 +0000235 // Otherwise, things are slightly more complex. Remove V's name from VST and
236 // then reinsert it into ST.
237
238 if (VST)
239 VST->removeValueName(V->Name);
240 Name = V->Name;
241 V->Name = 0;
242 Name->setValue(this);
243
244 if (ST)
245 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000246}
247
248
Chris Lattner9f158122003-08-29 05:09:37 +0000249// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
250// except that it doesn't have all of the asserts. The asserts fail because we
251// are half-way done resolving types, which causes some types to exist as two
252// different Type*'s at the same time. This is a sledgehammer to work around
253// this problem.
254//
255void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000256 while (!use_empty()) {
257 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000258 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
259 // constant!
Chris Lattner079edeb2003-10-16 16:53:07 +0000260 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Reid Spencerbbddbf32004-07-18 00:01:50 +0000261 if (!isa<GlobalValue>(C))
Chris Lattner7a1450d2005-10-04 18:13:04 +0000262 C->replaceUsesOfWithOnConstant(this, New, &U);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000263 else
Reid Spencerbbddbf32004-07-18 00:01:50 +0000264 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000265 } else {
Chris Lattner079edeb2003-10-16 16:53:07 +0000266 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000267 }
268 }
269}
270
Chris Lattner079edeb2003-10-16 16:53:07 +0000271void Value::replaceAllUsesWith(Value *New) {
272 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
273 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
274 assert(New->getType() == getType() &&
275 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000276
Chris Lattner079edeb2003-10-16 16:53:07 +0000277 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278}
279
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280//===----------------------------------------------------------------------===//
281// User Class
282//===----------------------------------------------------------------------===//
283
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284// replaceUsesOfWith - Replaces all references to the "From" definition with
285// references to the "To" definition.
286//
287void User::replaceUsesOfWith(Value *From, Value *To) {
288 if (From == To) return; // Duh what?
289
Reid Spencerbbddbf32004-07-18 00:01:50 +0000290 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000291 "Cannot call User::replaceUsesofWith on a constant!");
292
Chris Lattnera073acb2001-07-07 08:36:50 +0000293 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
294 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295 // The side effects of this setOperand call include linking to
296 // "To", adding "this" to the uses list of To, and
297 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000298 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000301