blob: 54ea5b7502f0b3bb48928ab84104465f72536c6f [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Misha Brukmanfd939082005-04-21 23:48:37 +000010// This file implements the Value and User classes.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner28eca8b2002-10-09 23:12:59 +000014#include "llvm/Constant.h"
Chris Lattner0d1e4072005-03-05 19:51:50 +000015#include "llvm/DerivedTypes.h"
16#include "llvm/InstrTypes.h"
17#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000018#include "llvm/ValueSymbolTable.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner00950542001-06-06 20:29:01 +000024//===----------------------------------------------------------------------===//
25// Value Class
26//===----------------------------------------------------------------------===//
27
Chris Lattner82d18aa2001-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 Lattnerdec628e2007-02-12 05:18:08 +000033Value::Value(const Type *ty, unsigned scid)
Chris Lattner905547b2005-02-05 01:37:58 +000034 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
Chris Lattnerdec628e2007-02-12 05:18:08 +000035 UseList(0), Name(0) {
Chris Lattnera9e77812004-07-06 17:44:17 +000036 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Misha Brukmanfd939082005-04-21 23:48:37 +000037 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
Chris Lattner5c45e6d2004-07-07 18:07:46 +000038 isa<OpaqueType>(ty)) &&
Chris Lattnera9e77812004-07-06 17:44:17 +000039 "Cannot create non-first-class values except for constants!");
Chris Lattner00950542001-06-06 20:29:01 +000040}
41
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000042Value::~Value() {
Chris Lattner00950542001-06-06 20:29:01 +000043#ifndef NDEBUG // Only in -g mode...
Chris Lattneree976f32001-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 Brukmanfd939082005-04-21 23:48:37 +000047 // still being referenced. The value in question should be printed as
Chris Lattneree976f32001-06-11 15:04:40 +000048 // a <badref>
49 //
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000050 if (!use_empty()) {
51 DOUT << "While deleting: " << *Ty << " %" << Name << "\n";
52 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Bill Wendling2e3def12006-11-17 08:03:48 +000053 DOUT << "Use still stuck around after Def is destroyed:"
54 << **I << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000055 }
56#endif
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000057 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattnerd1e693f2002-09-08 18:59:35 +000058
Chris Lattner91857da2007-03-20 00:18:10 +000059 // If this value is named, destroy the name. This should not be in a symtab
60 // at this point.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000061 if (Name)
62 Name->Destroy();
Chris Lattner91857da2007-03-20 00:18:10 +000063
Chris Lattnerd1e693f2002-09-08 18:59:35 +000064 // There should be no uses of this object anymore, remove it.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000065 LeakDetector::removeGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +000066}
67
Chris Lattner29d1ca62005-02-01 01:24:21 +000068/// hasNUses - Return true if this Value has exactly N users.
69///
70bool Value::hasNUses(unsigned N) const {
71 use_const_iterator UI = use_begin(), E = use_end();
72
73 for (; N; --N, ++UI)
74 if (UI == E) return false; // Too few.
75 return UI == E;
76}
77
Chris Lattner8daf0562005-02-23 16:51:11 +000078/// hasNUsesOrMore - Return true if this value has N users or more. This is
79/// logically equivalent to getNumUses() >= N.
80///
81bool Value::hasNUsesOrMore(unsigned N) const {
82 use_const_iterator UI = use_begin(), E = use_end();
83
84 for (; N; --N, ++UI)
85 if (UI == E) return false; // Too few.
86
87 return true;
88}
89
90
Chris Lattner29d1ca62005-02-01 01:24:21 +000091/// getNumUses - This method computes the number of uses of this Value. This
92/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
93/// values.
94unsigned Value::getNumUses() const {
95 return (unsigned)std::distance(use_begin(), use_end());
96}
97
Chris Lattner72168112007-02-11 00:37:27 +000098static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattnerea7acb82007-02-11 19:12:18 +000099 ST = 0;
Chris Lattner72168112007-02-11 00:37:27 +0000100 if (Instruction *I = dyn_cast<Instruction>(V)) {
101 if (BasicBlock *P = I->getParent())
102 if (Function *PP = P->getParent())
103 ST = &PP->getValueSymbolTable();
104 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
105 if (Function *P = BB->getParent())
106 ST = &P->getValueSymbolTable();
107 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
108 if (Module *P = GV->getParent())
109 ST = &P->getValueSymbolTable();
110 } else if (Argument *A = dyn_cast<Argument>(V)) {
111 if (Function *P = A->getParent())
112 ST = &P->getValueSymbolTable();
113 } else {
114 assert(isa<Constant>(V) && "Unknown value type!");
115 return true; // no name is setable for this.
116 }
117 return false;
118}
Chris Lattner28eca8b2002-10-09 23:12:59 +0000119
Chris Lattner71996e72007-08-10 15:34:35 +0000120/// getNameStart - Return a pointer to a null terminated string for this name.
121/// Note that names can have null characters within the string as well as at
122/// their end. This always returns a non-null pointer.
123const char *Value::getNameStart() const {
124 if (Name == 0) return "";
125 return Name->getKeyData();
126}
127
128/// getNameLen - Return the length of the string, correctly handling nul
129/// characters embedded into them.
130unsigned Value::getNameLen() const {
Chris Lattnerd7c4ca12007-09-28 20:09:40 +0000131 return Name ? Name->getKeyLength() : 0;
Chris Lattner71996e72007-08-10 15:34:35 +0000132}
133
134
Chris Lattner924b1ca2007-02-15 18:53:54 +0000135std::string Value::getNameStr() const {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000136 if (Name == 0) return "";
137 return std::string(Name->getKeyData(),
138 Name->getKeyData()+Name->getKeyLength());
139}
Chris Lattner0d1e4072005-03-05 19:51:50 +0000140
Chris Lattnerdec628e2007-02-12 05:18:08 +0000141void Value::setName(const std::string &name) {
Chris Lattner042ad362007-02-12 18:52:59 +0000142 setName(&name[0], name.size());
143}
144
Chris Lattnerec79b3d2007-02-13 07:53:34 +0000145void Value::setName(const char *Name) {
146 setName(Name, Name ? strlen(Name) : 0);
147}
148
Chris Lattner042ad362007-02-12 18:52:59 +0000149void Value::setName(const char *NameStr, unsigned NameLen) {
150 if (NameLen == 0 && !hasName()) return;
Jeff Cohenca5183d2007-03-05 00:00:42 +0000151 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000152
Chris Lattner04cb8002005-03-06 02:14:28 +0000153 // Get the symbol table to update for this object.
Chris Lattner72168112007-02-11 00:37:27 +0000154 ValueSymbolTable *ST;
155 if (getSymTab(this, ST))
156 return; // Cannot set a name on this value (e.g. constant).
Chris Lattner0d1e4072005-03-05 19:51:50 +0000157
Chris Lattnerdec628e2007-02-12 05:18:08 +0000158 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner042ad362007-02-12 18:52:59 +0000159 if (NameLen == 0) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000160 // Free the name for this value.
161 Name->Destroy();
162 Name = 0;
Chris Lattner042ad362007-02-12 18:52:59 +0000163 return;
Chris Lattner04cb8002005-03-06 02:14:28 +0000164 }
Chris Lattner042ad362007-02-12 18:52:59 +0000165
166 if (Name) {
167 // Name isn't changing?
168 if (NameLen == Name->getKeyLength() &&
169 !memcmp(Name->getKeyData(), NameStr, NameLen))
170 return;
171 Name->Destroy();
172 }
173
174 // NOTE: Could optimize for the case the name is shrinking to not deallocate
175 // then reallocated.
176
177 // Create the new name.
178 Name = ValueName::Create(NameStr, NameStr+NameLen);
179 Name->setValue(this);
Chris Lattnerdec628e2007-02-12 05:18:08 +0000180 return;
Chris Lattner04cb8002005-03-06 02:14:28 +0000181 }
Chris Lattnerdec628e2007-02-12 05:18:08 +0000182
183 // NOTE: Could optimize for the case the name is shrinking to not deallocate
184 // then reallocated.
185 if (hasName()) {
186 // Name isn't changing?
Chris Lattner042ad362007-02-12 18:52:59 +0000187 if (NameLen == Name->getKeyLength() &&
188 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattnerdec628e2007-02-12 05:18:08 +0000189 return;
190
191 // Remove old name.
192 ST->removeValueName(Name);
193 Name->Destroy();
194 Name = 0;
195
Chris Lattner042ad362007-02-12 18:52:59 +0000196 if (NameLen == 0)
197 return;
Chris Lattnerdec628e2007-02-12 05:18:08 +0000198 }
199
200 // Name is changing to something new.
Chris Lattner042ad362007-02-12 18:52:59 +0000201 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattner0d1e4072005-03-05 19:51:50 +0000202}
203
Chris Lattner042ad362007-02-12 18:52:59 +0000204
Chris Lattner72168112007-02-11 00:37:27 +0000205/// takeName - transfer the name from V to this value, setting V's name to
206/// empty. It is an error to call V->takeName(V).
207void Value::takeName(Value *V) {
Chris Lattner0878c312007-02-15 20:01:43 +0000208 ValueSymbolTable *ST = 0;
209 // If this value has a name, drop it.
210 if (hasName()) {
211 // Get the symtab this is in.
212 if (getSymTab(this, ST)) {
213 // We can't set a name on this value, but we need to clear V's name if
214 // it has one.
215 if (V->hasName()) V->setName(0, 0);
216 return; // Cannot set a name on this value (e.g. constant).
217 }
218
219 // Remove old name.
220 if (ST)
221 ST->removeValueName(Name);
222 Name->Destroy();
223 Name = 0;
224 }
225
226 // Now we know that this has no name.
227
228 // If V has no name either, we're done.
229 if (!V->hasName()) return;
230
231 // Get this's symtab if we didn't before.
232 if (!ST) {
233 if (getSymTab(this, ST)) {
234 // Clear V's name.
235 V->setName(0, 0);
236 return; // Cannot set a name on this value (e.g. constant).
237 }
238 }
239
240 // Get V's ST, this should always succed, because V has a name.
241 ValueSymbolTable *VST;
242 bool Failure = getSymTab(V, VST);
243 assert(!Failure && "V has a name, so it should have a ST!");
244
245 // If these values are both in the same symtab, we can do this very fast.
246 // This works even if both values have no symtab yet.
247 if (ST == VST) {
248 // Take the name!
249 Name = V->Name;
250 V->Name = 0;
251 Name->setValue(this);
Chris Lattnerf41916e2007-02-11 01:04:09 +0000252 return;
253 }
254
Chris Lattner0878c312007-02-15 20:01:43 +0000255 // Otherwise, things are slightly more complex. Remove V's name from VST and
256 // then reinsert it into ST.
257
258 if (VST)
259 VST->removeValueName(V->Name);
260 Name = V->Name;
261 V->Name = 0;
262 Name->setValue(this);
263
264 if (ST)
265 ST->reinsertValue(this);
Chris Lattner72168112007-02-11 00:37:27 +0000266}
267
268
Chris Lattner2bc065b2003-08-29 05:09:37 +0000269// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
270// except that it doesn't have all of the asserts. The asserts fail because we
271// are half-way done resolving types, which causes some types to exist as two
272// different Type*'s at the same time. This is a sledgehammer to work around
273// this problem.
274//
275void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner29d1ca62005-02-01 01:24:21 +0000276 while (!use_empty()) {
277 Use &U = *UseList;
Chris Lattner2bc065b2003-08-29 05:09:37 +0000278 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner2d691332007-08-21 00:21:07 +0000279 // constant because they are uniqued.
Chris Lattnerc3cc71a2003-10-16 16:53:07 +0000280 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner2d691332007-08-21 00:21:07 +0000281 if (!isa<GlobalValue>(C)) {
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +0000282 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner2d691332007-08-21 00:21:07 +0000283 continue;
284 }
Chris Lattner2bc065b2003-08-29 05:09:37 +0000285 }
Chris Lattner2d691332007-08-21 00:21:07 +0000286
287 U.set(New);
Chris Lattner2bc065b2003-08-29 05:09:37 +0000288 }
289}
290
Chris Lattnerc3cc71a2003-10-16 16:53:07 +0000291void Value::replaceAllUsesWith(Value *New) {
292 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
293 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
294 assert(New->getType() == getType() &&
295 "replaceAllUses of value with new value of different type!");
Chris Lattner2bc065b2003-08-29 05:09:37 +0000296
Chris Lattnerc3cc71a2003-10-16 16:53:07 +0000297 uncheckedReplaceAllUsesWith(New);
Chris Lattner00950542001-06-06 20:29:01 +0000298}
299
Chris Lattner00950542001-06-06 20:29:01 +0000300//===----------------------------------------------------------------------===//
301// User Class
302//===----------------------------------------------------------------------===//
303
Chris Lattner00950542001-06-06 20:29:01 +0000304// replaceUsesOfWith - Replaces all references to the "From" definition with
305// references to the "To" definition.
306//
307void User::replaceUsesOfWith(Value *From, Value *To) {
308 if (From == To) return; // Duh what?
309
Reid Spencer4ec2e4c2004-07-18 00:01:50 +0000310 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
Chris Lattner28eca8b2002-10-09 23:12:59 +0000311 "Cannot call User::replaceUsesofWith on a constant!");
312
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000313 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
314 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner00950542001-06-06 20:29:01 +0000315 // The side effects of this setOperand call include linking to
316 // "To", adding "this" to the uses list of To, and
317 // most importantly, removing "this" from the use list of "From".
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000318 setOperand(i, To); // Fix it now...
Chris Lattner00950542001-06-06 20:29:01 +0000319 }
Chris Lattner00950542001-06-06 20:29:01 +0000320}
Reid Spencer4ec2e4c2004-07-18 00:01:50 +0000321