blob: ff056ba74ac8441cf464aaf2c40cbb0e8406ca9e [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"
Anton Korobeynikov82c02b22008-05-06 22:52:30 +000015#include "llvm/Constants.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000016#include "llvm/DerivedTypes.h"
17#include "llvm/InstrTypes.h"
Devang Patel1f00b532008-02-21 01:54:02 +000018#include "llvm/Instructions.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000019#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000021#include "llvm/Support/Debug.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000023#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner2f7c9632001-06-06 20:29:01 +000026//===----------------------------------------------------------------------===//
27// Value Class
28//===----------------------------------------------------------------------===//
29
Chris Lattner25450e32001-12-13 00:41:27 +000030static inline const Type *checkType(const Type *Ty) {
31 assert(Ty && "Value defined with a null type: Error!");
32 return Ty;
33}
34
Chris Lattner32ab6432007-02-12 05:18:08 +000035Value::Value(const Type *ty, unsigned scid)
Chris Lattnera29c92f2005-02-05 01:37:58 +000036 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
Chris Lattner32ab6432007-02-12 05:18:08 +000037 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000038 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Devang Patel1f00b532008-02-21 01:54:02 +000039 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
40 isa<OpaqueType>(ty) || Ty->getTypeID() == Type::StructTyID) &&
41 "invalid CallInst type!");
42 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Misha Brukmanb1c93172005-04-21 23:48:37 +000043 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000044 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000045 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000046}
47
Gordon Henriksen14a55692007-12-10 02:14:30 +000048Value::~Value() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000049#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000050 // Check to make sure that there are no uses of this value that are still
51 // around when the value is destroyed. If there are, then we have a dangling
52 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000053 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000054 // a <badref>
55 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000056 if (!use_empty()) {
Nick Lewycky7f6e95a2008-03-01 17:20:55 +000057 DOUT << "While deleting: " << *Ty << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000058 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Bill Wendling6a462f12006-11-17 08:03:48 +000059 DOUT << "Use still stuck around after Def is destroyed:"
60 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000061 }
62#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000063 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000064
Chris Lattneraf867a32007-03-20 00:18:10 +000065 // If this value is named, destroy the name. This should not be in a symtab
66 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000067 if (Name)
68 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000069
Chris Lattner184b2982002-09-08 18:59:35 +000070 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000071 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000072}
73
Chris Lattner4947e672005-02-01 01:24:21 +000074/// hasNUses - Return true if this Value has exactly N users.
75///
76bool Value::hasNUses(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 return UI == E;
82}
83
Chris Lattnerd36552f2005-02-23 16:51:11 +000084/// hasNUsesOrMore - Return true if this value has N users or more. This is
85/// logically equivalent to getNumUses() >= N.
86///
87bool Value::hasNUsesOrMore(unsigned N) const {
88 use_const_iterator UI = use_begin(), E = use_end();
89
90 for (; N; --N, ++UI)
91 if (UI == E) return false; // Too few.
92
93 return true;
94}
95
96
Chris Lattner4947e672005-02-01 01:24:21 +000097/// getNumUses - This method computes the number of uses of this Value. This
98/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
99/// values.
100unsigned Value::getNumUses() const {
101 return (unsigned)std::distance(use_begin(), use_end());
102}
103
Chris Lattnerb6250822007-02-11 00:37:27 +0000104static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000105 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000106 if (Instruction *I = dyn_cast<Instruction>(V)) {
107 if (BasicBlock *P = I->getParent())
108 if (Function *PP = P->getParent())
109 ST = &PP->getValueSymbolTable();
110 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
111 if (Function *P = BB->getParent())
112 ST = &P->getValueSymbolTable();
113 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
114 if (Module *P = GV->getParent())
115 ST = &P->getValueSymbolTable();
116 } else if (Argument *A = dyn_cast<Argument>(V)) {
117 if (Function *P = A->getParent())
118 ST = &P->getValueSymbolTable();
119 } else {
120 assert(isa<Constant>(V) && "Unknown value type!");
121 return true; // no name is setable for this.
122 }
123 return false;
124}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000125
Chris Lattner5109a882007-08-10 15:34:35 +0000126/// getNameStart - Return a pointer to a null terminated string for this name.
127/// Note that names can have null characters within the string as well as at
128/// their end. This always returns a non-null pointer.
129const char *Value::getNameStart() const {
130 if (Name == 0) return "";
131 return Name->getKeyData();
132}
133
134/// getNameLen - Return the length of the string, correctly handling nul
135/// characters embedded into them.
136unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000137 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000138}
139
Chris Lattnera1d850e2008-04-30 03:55:40 +0000140/// isName - Return true if this value has the name specified by the provided
141/// nul terminated string.
142bool Value::isName(const char *N) const {
143 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000144 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000145}
146
Chris Lattner5109a882007-08-10 15:34:35 +0000147
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000148std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000149 if (Name == 0) return "";
150 return std::string(Name->getKeyData(),
151 Name->getKeyData()+Name->getKeyLength());
152}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000153
Chris Lattner32ab6432007-02-12 05:18:08 +0000154void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000155 setName(&name[0], name.size());
156}
157
Chris Lattnercb9a6262007-02-13 07:53:34 +0000158void Value::setName(const char *Name) {
159 setName(Name, Name ? strlen(Name) : 0);
160}
161
Chris Lattner1a5de582007-02-12 18:52:59 +0000162void Value::setName(const char *NameStr, unsigned NameLen) {
163 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000164 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000165
Chris Lattnerffb37782005-03-06 02:14:28 +0000166 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000167 ValueSymbolTable *ST;
168 if (getSymTab(this, ST))
169 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000170
Chris Lattner32ab6432007-02-12 05:18:08 +0000171 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000172 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000173 // Free the name for this value.
174 Name->Destroy();
175 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000176 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000177 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000178
179 if (Name) {
180 // Name isn't changing?
181 if (NameLen == Name->getKeyLength() &&
182 !memcmp(Name->getKeyData(), NameStr, NameLen))
183 return;
184 Name->Destroy();
185 }
186
187 // NOTE: Could optimize for the case the name is shrinking to not deallocate
188 // then reallocated.
189
190 // Create the new name.
191 Name = ValueName::Create(NameStr, NameStr+NameLen);
192 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000193 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000194 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000195
196 // NOTE: Could optimize for the case the name is shrinking to not deallocate
197 // then reallocated.
198 if (hasName()) {
199 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000200 if (NameLen == Name->getKeyLength() &&
201 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000202 return;
203
204 // Remove old name.
205 ST->removeValueName(Name);
206 Name->Destroy();
207 Name = 0;
208
Chris Lattner1a5de582007-02-12 18:52:59 +0000209 if (NameLen == 0)
210 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000211 }
212
213 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000214 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000215}
216
Chris Lattner1a5de582007-02-12 18:52:59 +0000217
Chris Lattnerb6250822007-02-11 00:37:27 +0000218/// takeName - transfer the name from V to this value, setting V's name to
219/// empty. It is an error to call V->takeName(V).
220void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000221 ValueSymbolTable *ST = 0;
222 // If this value has a name, drop it.
223 if (hasName()) {
224 // Get the symtab this is in.
225 if (getSymTab(this, ST)) {
226 // We can't set a name on this value, but we need to clear V's name if
227 // it has one.
228 if (V->hasName()) V->setName(0, 0);
229 return; // Cannot set a name on this value (e.g. constant).
230 }
231
232 // Remove old name.
233 if (ST)
234 ST->removeValueName(Name);
235 Name->Destroy();
236 Name = 0;
237 }
238
239 // Now we know that this has no name.
240
241 // If V has no name either, we're done.
242 if (!V->hasName()) return;
243
244 // Get this's symtab if we didn't before.
245 if (!ST) {
246 if (getSymTab(this, ST)) {
247 // Clear V's name.
248 V->setName(0, 0);
249 return; // Cannot set a name on this value (e.g. constant).
250 }
251 }
252
253 // Get V's ST, this should always succed, because V has a name.
254 ValueSymbolTable *VST;
255 bool Failure = getSymTab(V, VST);
256 assert(!Failure && "V has a name, so it should have a ST!");
257
258 // If these values are both in the same symtab, we can do this very fast.
259 // This works even if both values have no symtab yet.
260 if (ST == VST) {
261 // Take the name!
262 Name = V->Name;
263 V->Name = 0;
264 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000265 return;
266 }
267
Chris Lattnercf835ff2007-02-15 20:01:43 +0000268 // Otherwise, things are slightly more complex. Remove V's name from VST and
269 // then reinsert it into ST.
270
271 if (VST)
272 VST->removeValueName(V->Name);
273 Name = V->Name;
274 V->Name = 0;
275 Name->setValue(this);
276
277 if (ST)
278 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000279}
280
281
Chris Lattner9f158122003-08-29 05:09:37 +0000282// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
283// except that it doesn't have all of the asserts. The asserts fail because we
284// are half-way done resolving types, which causes some types to exist as two
285// different Type*'s at the same time. This is a sledgehammer to work around
286// this problem.
287//
288void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner4947e672005-02-01 01:24:21 +0000289 while (!use_empty()) {
290 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000291 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000292 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000293 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000294 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000295 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000296 continue;
297 }
Chris Lattner9f158122003-08-29 05:09:37 +0000298 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000299
300 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000301 }
302}
303
Chris Lattner079edeb2003-10-16 16:53:07 +0000304void Value::replaceAllUsesWith(Value *New) {
305 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
306 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
307 assert(New->getType() == getType() &&
308 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000309
Chris Lattner079edeb2003-10-16 16:53:07 +0000310 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311}
312
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000313Value *Value::stripPointerCasts() {
314 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
315 if (CE->getOpcode() == Instruction::BitCast) {
316 if (isa<PointerType>(CE->getOperand(0)->getType()))
317 return CE->getOperand(0)->stripPointerCasts();
318 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
319 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
320 if (!CE->getOperand(i)->isNullValue())
321 return this;
322 return CE->getOperand(0)->stripPointerCasts();
323 }
324 return this;
325 }
326
327 if (BitCastInst *CI = dyn_cast<BitCastInst>(this)) {
328 if (isa<PointerType>(CI->getOperand(0)->getType()))
329 return CI->getOperand(0)->stripPointerCasts();
330 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(this)) {
331 if (GEP->hasAllZeroIndices())
332 return GEP->getOperand(0)->stripPointerCasts();
333 }
334 return this;
335}
336
Chris Lattner2f7c9632001-06-06 20:29:01 +0000337//===----------------------------------------------------------------------===//
338// User Class
339//===----------------------------------------------------------------------===//
340
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341// replaceUsesOfWith - Replaces all references to the "From" definition with
342// references to the "To" definition.
343//
344void User::replaceUsesOfWith(Value *From, Value *To) {
345 if (From == To) return; // Duh what?
346
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000347 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000348 "Cannot call User::replaceUsesofWith on a constant!");
349
Chris Lattnera073acb2001-07-07 08:36:50 +0000350 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
351 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352 // The side effects of this setOperand call include linking to
353 // "To", adding "this" to the uses list of To, and
354 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000355 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000356 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357}