blob: e55e558e0f99bc70e71ff084e50eeb2af7b1b905 [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
Chris Lattnera1d850e2008-04-30 03:55:40 +0000139/// isName - Return true if this value has the name specified by the provided
140/// nul terminated string.
141bool Value::isName(const char *N) const {
142 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000143 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000144}
145
Chris Lattner5109a882007-08-10 15:34:35 +0000146
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000147std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000148 if (Name == 0) return "";
149 return std::string(Name->getKeyData(),
150 Name->getKeyData()+Name->getKeyLength());
151}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000152
Chris Lattner32ab6432007-02-12 05:18:08 +0000153void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000154 setName(&name[0], name.size());
155}
156
Chris Lattnercb9a6262007-02-13 07:53:34 +0000157void Value::setName(const char *Name) {
158 setName(Name, Name ? strlen(Name) : 0);
159}
160
Chris Lattner1a5de582007-02-12 18:52:59 +0000161void Value::setName(const char *NameStr, unsigned NameLen) {
162 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000163 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000164
Chris Lattnerffb37782005-03-06 02:14:28 +0000165 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000166 ValueSymbolTable *ST;
167 if (getSymTab(this, ST))
168 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000169
Chris Lattner32ab6432007-02-12 05:18:08 +0000170 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000171 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000172 // Free the name for this value.
173 Name->Destroy();
174 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000175 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000176 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000177
178 if (Name) {
179 // Name isn't changing?
180 if (NameLen == Name->getKeyLength() &&
181 !memcmp(Name->getKeyData(), NameStr, NameLen))
182 return;
183 Name->Destroy();
184 }
185
186 // NOTE: Could optimize for the case the name is shrinking to not deallocate
187 // then reallocated.
188
189 // Create the new name.
190 Name = ValueName::Create(NameStr, NameStr+NameLen);
191 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000192 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000193 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000194
195 // NOTE: Could optimize for the case the name is shrinking to not deallocate
196 // then reallocated.
197 if (hasName()) {
198 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000199 if (NameLen == Name->getKeyLength() &&
200 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000201 return;
202
203 // Remove old name.
204 ST->removeValueName(Name);
205 Name->Destroy();
206 Name = 0;
207
Chris Lattner1a5de582007-02-12 18:52:59 +0000208 if (NameLen == 0)
209 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000210 }
211
212 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000213 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000214}
215
Chris Lattner1a5de582007-02-12 18:52:59 +0000216
Chris Lattnerb6250822007-02-11 00:37:27 +0000217/// takeName - transfer the name from V to this value, setting V's name to
218/// empty. It is an error to call V->takeName(V).
219void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000220 ValueSymbolTable *ST = 0;
221 // If this value has a name, drop it.
222 if (hasName()) {
223 // Get the symtab this is in.
224 if (getSymTab(this, ST)) {
225 // We can't set a name on this value, but we need to clear V's name if
226 // it has one.
227 if (V->hasName()) V->setName(0, 0);
228 return; // Cannot set a name on this value (e.g. constant).
229 }
230
231 // Remove old name.
232 if (ST)
233 ST->removeValueName(Name);
234 Name->Destroy();
235 Name = 0;
236 }
237
238 // Now we know that this has no name.
239
240 // If V has no name either, we're done.
241 if (!V->hasName()) return;
242
243 // Get this's symtab if we didn't before.
244 if (!ST) {
245 if (getSymTab(this, ST)) {
246 // Clear V's name.
247 V->setName(0, 0);
248 return; // Cannot set a name on this value (e.g. constant).
249 }
250 }
251
252 // Get V's ST, this should always succed, because V has a name.
253 ValueSymbolTable *VST;
254 bool Failure = getSymTab(V, VST);
255 assert(!Failure && "V has a name, so it should have a ST!");
256
257 // If these values are both in the same symtab, we can do this very fast.
258 // This works even if both values have no symtab yet.
259 if (ST == VST) {
260 // Take the name!
261 Name = V->Name;
262 V->Name = 0;
263 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000264 return;
265 }
266
Chris Lattnercf835ff2007-02-15 20:01:43 +0000267 // Otherwise, things are slightly more complex. Remove V's name from VST and
268 // then reinsert it into ST.
269
270 if (VST)
271 VST->removeValueName(V->Name);
272 Name = V->Name;
273 V->Name = 0;
274 Name->setValue(this);
275
276 if (ST)
277 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000278}
279
280
Chris Lattner9f158122003-08-29 05:09:37 +0000281// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
282// except that it doesn't have all of the asserts. The asserts fail because we
283// are half-way done resolving types, which causes some types to exist as two
284// different Type*'s at the same time. This is a sledgehammer to work around
285// this problem.
286//
287void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000288 while (!use_empty()) {
289 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000290 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000291 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000292 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000293 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000294 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000295 continue;
296 }
Chris Lattner9f158122003-08-29 05:09:37 +0000297 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000298
299 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000300 }
301}
302
Chris Lattner079edeb2003-10-16 16:53:07 +0000303void Value::replaceAllUsesWith(Value *New) {
304 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
305 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
306 assert(New->getType() == getType() &&
307 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000308
Chris Lattner079edeb2003-10-16 16:53:07 +0000309 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310}
311
Chris Lattner2f7c9632001-06-06 20:29:01 +0000312//===----------------------------------------------------------------------===//
313// User Class
314//===----------------------------------------------------------------------===//
315
Chris Lattner2f7c9632001-06-06 20:29:01 +0000316// replaceUsesOfWith - Replaces all references to the "From" definition with
317// references to the "To" definition.
318//
319void User::replaceUsesOfWith(Value *From, Value *To) {
320 if (From == To) return; // Duh what?
321
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000322 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000323 "Cannot call User::replaceUsesofWith on a constant!");
324
Chris Lattnera073acb2001-07-07 08:36:50 +0000325 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
326 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000327 // The side effects of this setOperand call include linking to
328 // "To", adding "this" to the uses list of To, and
329 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000330 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332}
Reid Spencerbbddbf32004-07-18 00:01:50 +0000333