blob: a61a1a02472765c08af97da068790177a5542176 [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//
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//
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"
Devang Patel1f00b532008-02-21 01:54:02 +000017#include "llvm/Instructions.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000018#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000019#include "llvm/ValueSymbolTable.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000020#include "llvm/Support/Debug.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner2f7c9632001-06-06 20:29:01 +000025//===----------------------------------------------------------------------===//
26// Value Class
27//===----------------------------------------------------------------------===//
28
Chris Lattner25450e32001-12-13 00:41:27 +000029static inline const Type *checkType(const Type *Ty) {
30 assert(Ty && "Value defined with a null type: Error!");
31 return Ty;
32}
33
Chris Lattner32ab6432007-02-12 05:18:08 +000034Value::Value(const Type *ty, unsigned scid)
Chris Lattnera29c92f2005-02-05 01:37:58 +000035 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
Chris Lattner32ab6432007-02-12 05:18:08 +000036 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000037 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Devang Patel1f00b532008-02-21 01:54:02 +000038 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
39 isa<OpaqueType>(ty) || Ty->getTypeID() == Type::StructTyID) &&
40 "invalid CallInst type!");
41 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Misha Brukmanb1c93172005-04-21 23:48:37 +000042 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000043 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000044 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000045}
46
Gordon Henriksen14a55692007-12-10 02:14:30 +000047Value::~Value() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000048#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000049 // Check to make sure that there are no uses of this value that are still
50 // around when the value is destroyed. If there are, then we have a dangling
51 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000052 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000053 // a <badref>
54 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000055 if (!use_empty()) {
Nick Lewycky7f6e95a2008-03-01 17:20:55 +000056 DOUT << "While deleting: " << *Ty << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000057 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Bill Wendling6a462f12006-11-17 08:03:48 +000058 DOUT << "Use still stuck around after Def is destroyed:"
59 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000060 }
61#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000062 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000063
Chris Lattneraf867a32007-03-20 00:18:10 +000064 // If this value is named, destroy the name. This should not be in a symtab
65 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000066 if (Name)
67 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000068
Chris Lattner184b2982002-09-08 18:59:35 +000069 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000070 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000071}
72
Chris Lattner4947e672005-02-01 01:24:21 +000073/// hasNUses - Return true if this Value has exactly N users.
74///
75bool Value::hasNUses(unsigned N) const {
76 use_const_iterator UI = use_begin(), E = use_end();
77
78 for (; N; --N, ++UI)
79 if (UI == E) return false; // Too few.
80 return UI == E;
81}
82
Chris Lattnerd36552f2005-02-23 16:51:11 +000083/// hasNUsesOrMore - Return true if this value has N users or more. This is
84/// logically equivalent to getNumUses() >= N.
85///
86bool Value::hasNUsesOrMore(unsigned N) const {
87 use_const_iterator UI = use_begin(), E = use_end();
88
89 for (; N; --N, ++UI)
90 if (UI == E) return false; // Too few.
91
92 return true;
93}
94
95
Chris Lattner4947e672005-02-01 01:24:21 +000096/// getNumUses - This method computes the number of uses of this Value. This
97/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
98/// values.
99unsigned Value::getNumUses() const {
100 return (unsigned)std::distance(use_begin(), use_end());
101}
102
Chris Lattnerb6250822007-02-11 00:37:27 +0000103static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000104 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000105 if (Instruction *I = dyn_cast<Instruction>(V)) {
106 if (BasicBlock *P = I->getParent())
107 if (Function *PP = P->getParent())
108 ST = &PP->getValueSymbolTable();
109 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
110 if (Function *P = BB->getParent())
111 ST = &P->getValueSymbolTable();
112 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
113 if (Module *P = GV->getParent())
114 ST = &P->getValueSymbolTable();
115 } else if (Argument *A = dyn_cast<Argument>(V)) {
116 if (Function *P = A->getParent())
117 ST = &P->getValueSymbolTable();
118 } else {
119 assert(isa<Constant>(V) && "Unknown value type!");
120 return true; // no name is setable for this.
121 }
122 return false;
123}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000124
Chris Lattner5109a882007-08-10 15:34:35 +0000125/// getNameStart - Return a pointer to a null terminated string for this name.
126/// Note that names can have null characters within the string as well as at
127/// their end. This always returns a non-null pointer.
128const char *Value::getNameStart() const {
129 if (Name == 0) return "";
130 return Name->getKeyData();
131}
132
133/// getNameLen - Return the length of the string, correctly handling nul
134/// characters embedded into them.
135unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000136 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000137}
138
139
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000140std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000141 if (Name == 0) return "";
142 return std::string(Name->getKeyData(),
143 Name->getKeyData()+Name->getKeyLength());
144}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000145
Chris Lattner32ab6432007-02-12 05:18:08 +0000146void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000147 setName(&name[0], name.size());
148}
149
Chris Lattnercb9a6262007-02-13 07:53:34 +0000150void Value::setName(const char *Name) {
151 setName(Name, Name ? strlen(Name) : 0);
152}
153
Chris Lattner1a5de582007-02-12 18:52:59 +0000154void Value::setName(const char *NameStr, unsigned NameLen) {
155 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000156 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000157
Chris Lattnerffb37782005-03-06 02:14:28 +0000158 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000159 ValueSymbolTable *ST;
160 if (getSymTab(this, ST))
161 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000162
Chris Lattner32ab6432007-02-12 05:18:08 +0000163 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000164 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000165 // Free the name for this value.
166 Name->Destroy();
167 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000168 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000169 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000170
171 if (Name) {
172 // Name isn't changing?
173 if (NameLen == Name->getKeyLength() &&
174 !memcmp(Name->getKeyData(), NameStr, NameLen))
175 return;
176 Name->Destroy();
177 }
178
179 // NOTE: Could optimize for the case the name is shrinking to not deallocate
180 // then reallocated.
181
182 // Create the new name.
183 Name = ValueName::Create(NameStr, NameStr+NameLen);
184 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000185 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000186 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000187
188 // NOTE: Could optimize for the case the name is shrinking to not deallocate
189 // then reallocated.
190 if (hasName()) {
191 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000192 if (NameLen == Name->getKeyLength() &&
193 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000194 return;
195
196 // Remove old name.
197 ST->removeValueName(Name);
198 Name->Destroy();
199 Name = 0;
200
Chris Lattner1a5de582007-02-12 18:52:59 +0000201 if (NameLen == 0)
202 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000203 }
204
205 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000206 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000207}
208
Chris Lattner1a5de582007-02-12 18:52:59 +0000209
Chris Lattnerb6250822007-02-11 00:37:27 +0000210/// takeName - transfer the name from V to this value, setting V's name to
211/// empty. It is an error to call V->takeName(V).
212void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000213 ValueSymbolTable *ST = 0;
214 // If this value has a name, drop it.
215 if (hasName()) {
216 // Get the symtab this is in.
217 if (getSymTab(this, ST)) {
218 // We can't set a name on this value, but we need to clear V's name if
219 // it has one.
220 if (V->hasName()) V->setName(0, 0);
221 return; // Cannot set a name on this value (e.g. constant).
222 }
223
224 // Remove old name.
225 if (ST)
226 ST->removeValueName(Name);
227 Name->Destroy();
228 Name = 0;
229 }
230
231 // Now we know that this has no name.
232
233 // If V has no name either, we're done.
234 if (!V->hasName()) return;
235
236 // Get this's symtab if we didn't before.
237 if (!ST) {
238 if (getSymTab(this, ST)) {
239 // Clear V's name.
240 V->setName(0, 0);
241 return; // Cannot set a name on this value (e.g. constant).
242 }
243 }
244
245 // Get V's ST, this should always succed, because V has a name.
246 ValueSymbolTable *VST;
247 bool Failure = getSymTab(V, VST);
248 assert(!Failure && "V has a name, so it should have a ST!");
249
250 // If these values are both in the same symtab, we can do this very fast.
251 // This works even if both values have no symtab yet.
252 if (ST == VST) {
253 // Take the name!
254 Name = V->Name;
255 V->Name = 0;
256 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000257 return;
258 }
259
Chris Lattnercf835ff2007-02-15 20:01:43 +0000260 // Otherwise, things are slightly more complex. Remove V's name from VST and
261 // then reinsert it into ST.
262
263 if (VST)
264 VST->removeValueName(V->Name);
265 Name = V->Name;
266 V->Name = 0;
267 Name->setValue(this);
268
269 if (ST)
270 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000271}
272
273
Chris Lattner9f158122003-08-29 05:09:37 +0000274// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
275// except that it doesn't have all of the asserts. The asserts fail because we
276// are half-way done resolving types, which causes some types to exist as two
277// different Type*'s at the same time. This is a sledgehammer to work around
278// this problem.
279//
280void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000281 while (!use_empty()) {
282 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000283 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000284 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000285 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000286 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000287 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000288 continue;
289 }
Chris Lattner9f158122003-08-29 05:09:37 +0000290 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000291
292 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000293 }
294}
295
Chris Lattner079edeb2003-10-16 16:53:07 +0000296void Value::replaceAllUsesWith(Value *New) {
297 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
298 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
299 assert(New->getType() == getType() &&
300 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000301
Chris Lattner079edeb2003-10-16 16:53:07 +0000302 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000303}
304
Chris Lattner2f7c9632001-06-06 20:29:01 +0000305//===----------------------------------------------------------------------===//
306// User Class
307//===----------------------------------------------------------------------===//
308
Chris Lattner2f7c9632001-06-06 20:29:01 +0000309// replaceUsesOfWith - Replaces all references to the "From" definition with
310// references to the "To" definition.
311//
312void User::replaceUsesOfWith(Value *From, Value *To) {
313 if (From == To) return; // Duh what?
314
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000315 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000316 "Cannot call User::replaceUsesofWith on a constant!");
317
Chris Lattnera073acb2001-07-07 08:36:50 +0000318 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
319 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320 // The side effects of this setOperand call include linking to
321 // "To", adding "this" to the uses list of To, and
322 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000323 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000324 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000326