blob: 07fbfc622fff78b721541035b3f8d09cfff4258d [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//
Chris Lattner90234f32009-03-31 22:11:05 +000010// This file implements the Value, ValueHandle, 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"
Dan Gohman1d548d82009-07-17 22:25:10 +000019#include "llvm/Operator.h"
Chris Lattnercdb9bfc2005-03-05 19:51:50 +000020#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000021#include "llvm/ValueSymbolTable.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000022#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/LeakDetector.h"
Chris Lattner90234f32009-03-31 22:11:05 +000025#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/ValueHandle.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000027#include "llvm/Support/raw_ostream.h"
Owen Anderson4b660a82009-06-17 17:36:57 +000028#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000029#include "llvm/System/Threading.h"
Chris Lattner90234f32009-03-31 22:11:05 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000031#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner2f7c9632001-06-06 20:29:01 +000034//===----------------------------------------------------------------------===//
35// Value Class
36//===----------------------------------------------------------------------===//
37
Chris Lattner25450e32001-12-13 00:41:27 +000038static inline const Type *checkType(const Type *Ty) {
39 assert(Ty && "Value defined with a null type: Error!");
40 return Ty;
41}
42
Chris Lattner32ab6432007-02-12 05:18:08 +000043Value::Value(const Type *ty, unsigned scid)
Dan Gohman9691e712009-07-17 17:16:59 +000044 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
45 SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000046 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000047 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000048 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
49 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000050 "invalid CallInst type!");
51 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000052 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000053 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000054 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000055}
56
Gordon Henriksen14a55692007-12-10 02:14:30 +000057Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000058 // Notify all ValueHandles (if present) that this value is going away.
59 if (HasValueHandle)
60 ValueHandleBase::ValueIsDeleted(this);
61
Chris Lattner2f7c9632001-06-06 20:29:01 +000062#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000063 // Check to make sure that there are no uses of this value that are still
64 // around when the value is destroyed. If there are, then we have a dangling
65 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000066 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000067 // a <badref>
68 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000069 if (!use_empty()) {
Chris Lattner695bc772008-12-13 18:37:58 +000070 cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000071 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner695bc772008-12-13 18:37:58 +000072 cerr << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000073 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000074 }
75#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000076 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000077
Chris Lattneraf867a32007-03-20 00:18:10 +000078 // If this value is named, destroy the name. This should not be in a symtab
79 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000080 if (Name)
81 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000082
Chris Lattner184b2982002-09-08 18:59:35 +000083 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000084 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000085}
86
Chris Lattner4947e672005-02-01 01:24:21 +000087/// hasNUses - Return true if this Value has exactly N users.
88///
89bool Value::hasNUses(unsigned N) const {
90 use_const_iterator UI = use_begin(), E = use_end();
91
92 for (; N; --N, ++UI)
93 if (UI == E) return false; // Too few.
94 return UI == E;
95}
96
Chris Lattnerd36552f2005-02-23 16:51:11 +000097/// hasNUsesOrMore - Return true if this value has N users or more. This is
98/// logically equivalent to getNumUses() >= N.
99///
100bool Value::hasNUsesOrMore(unsigned N) const {
101 use_const_iterator UI = use_begin(), E = use_end();
102
103 for (; N; --N, ++UI)
104 if (UI == E) return false; // Too few.
105
106 return true;
107}
108
Evan Cheng89553cc2008-06-12 21:15:59 +0000109/// isUsedInBasicBlock - Return true if this value is used in the specified
110/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000111bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000112 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
113 const Instruction *User = dyn_cast<Instruction>(*I);
114 if (User && User->getParent() == BB)
115 return true;
116 }
117 return false;
118}
119
Chris Lattnerd36552f2005-02-23 16:51:11 +0000120
Chris Lattner4947e672005-02-01 01:24:21 +0000121/// getNumUses - This method computes the number of uses of this Value. This
122/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
123/// values.
124unsigned Value::getNumUses() const {
125 return (unsigned)std::distance(use_begin(), use_end());
126}
127
Chris Lattnerb6250822007-02-11 00:37:27 +0000128static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000129 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000130 if (Instruction *I = dyn_cast<Instruction>(V)) {
131 if (BasicBlock *P = I->getParent())
132 if (Function *PP = P->getParent())
133 ST = &PP->getValueSymbolTable();
134 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
135 if (Function *P = BB->getParent())
136 ST = &P->getValueSymbolTable();
137 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
138 if (Module *P = GV->getParent())
139 ST = &P->getValueSymbolTable();
140 } else if (Argument *A = dyn_cast<Argument>(V)) {
141 if (Function *P = A->getParent())
142 ST = &P->getValueSymbolTable();
143 } else {
144 assert(isa<Constant>(V) && "Unknown value type!");
145 return true; // no name is setable for this.
146 }
147 return false;
148}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000149
Chris Lattner5109a882007-08-10 15:34:35 +0000150/// getNameStart - Return a pointer to a null terminated string for this name.
151/// Note that names can have null characters within the string as well as at
152/// their end. This always returns a non-null pointer.
153const char *Value::getNameStart() const {
154 if (Name == 0) return "";
155 return Name->getKeyData();
156}
157
158/// getNameLen - Return the length of the string, correctly handling nul
159/// characters embedded into them.
160unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000161 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000162}
163
Chris Lattnera1d850e2008-04-30 03:55:40 +0000164/// isName - Return true if this value has the name specified by the provided
165/// nul terminated string.
166bool Value::isName(const char *N) const {
167 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000168 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000169}
170
Chris Lattner5109a882007-08-10 15:34:35 +0000171
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000172std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000173 if (Name == 0) return "";
174 return std::string(Name->getKeyData(),
175 Name->getKeyData()+Name->getKeyLength());
176}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000177
Chris Lattner32ab6432007-02-12 05:18:08 +0000178void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000179 setName(&name[0], name.size());
180}
181
Chris Lattnercb9a6262007-02-13 07:53:34 +0000182void Value::setName(const char *Name) {
183 setName(Name, Name ? strlen(Name) : 0);
184}
185
Chris Lattner1a5de582007-02-12 18:52:59 +0000186void Value::setName(const char *NameStr, unsigned NameLen) {
187 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000188 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000189
Chris Lattnerffb37782005-03-06 02:14:28 +0000190 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000191 ValueSymbolTable *ST;
192 if (getSymTab(this, ST))
193 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000194
Chris Lattner32ab6432007-02-12 05:18:08 +0000195 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000196 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000197 // Free the name for this value.
198 Name->Destroy();
199 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000200 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000201 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000202
203 if (Name) {
204 // Name isn't changing?
205 if (NameLen == Name->getKeyLength() &&
206 !memcmp(Name->getKeyData(), NameStr, NameLen))
207 return;
208 Name->Destroy();
209 }
210
211 // NOTE: Could optimize for the case the name is shrinking to not deallocate
212 // then reallocated.
213
214 // Create the new name.
215 Name = ValueName::Create(NameStr, NameStr+NameLen);
216 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000217 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000218 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000219
220 // NOTE: Could optimize for the case the name is shrinking to not deallocate
221 // then reallocated.
222 if (hasName()) {
223 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000224 if (NameLen == Name->getKeyLength() &&
225 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000226 return;
227
228 // Remove old name.
229 ST->removeValueName(Name);
230 Name->Destroy();
231 Name = 0;
232
Chris Lattner1a5de582007-02-12 18:52:59 +0000233 if (NameLen == 0)
234 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000235 }
236
237 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000238 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000239}
240
Chris Lattner1a5de582007-02-12 18:52:59 +0000241
Chris Lattnerb6250822007-02-11 00:37:27 +0000242/// takeName - transfer the name from V to this value, setting V's name to
243/// empty. It is an error to call V->takeName(V).
244void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000245 ValueSymbolTable *ST = 0;
246 // If this value has a name, drop it.
247 if (hasName()) {
248 // Get the symtab this is in.
249 if (getSymTab(this, ST)) {
250 // We can't set a name on this value, but we need to clear V's name if
251 // it has one.
252 if (V->hasName()) V->setName(0, 0);
253 return; // Cannot set a name on this value (e.g. constant).
254 }
255
256 // Remove old name.
257 if (ST)
258 ST->removeValueName(Name);
259 Name->Destroy();
260 Name = 0;
261 }
262
263 // Now we know that this has no name.
264
265 // If V has no name either, we're done.
266 if (!V->hasName()) return;
267
268 // Get this's symtab if we didn't before.
269 if (!ST) {
270 if (getSymTab(this, ST)) {
271 // Clear V's name.
272 V->setName(0, 0);
273 return; // Cannot set a name on this value (e.g. constant).
274 }
275 }
276
277 // Get V's ST, this should always succed, because V has a name.
278 ValueSymbolTable *VST;
279 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000280 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000281
282 // If these values are both in the same symtab, we can do this very fast.
283 // This works even if both values have no symtab yet.
284 if (ST == VST) {
285 // Take the name!
286 Name = V->Name;
287 V->Name = 0;
288 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000289 return;
290 }
291
Chris Lattnercf835ff2007-02-15 20:01:43 +0000292 // Otherwise, things are slightly more complex. Remove V's name from VST and
293 // then reinsert it into ST.
294
295 if (VST)
296 VST->removeValueName(V->Name);
297 Name = V->Name;
298 V->Name = 0;
299 Name->setValue(this);
300
301 if (ST)
302 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000303}
304
305
Chris Lattner9f158122003-08-29 05:09:37 +0000306// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
307// except that it doesn't have all of the asserts. The asserts fail because we
308// are half-way done resolving types, which causes some types to exist as two
309// different Type*'s at the same time. This is a sledgehammer to work around
310// this problem.
311//
312void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000313 // Notify all ValueHandles (if present) that this value is going away.
314 if (HasValueHandle)
315 ValueHandleBase::ValueIsRAUWd(this, New);
316
Chris Lattner4947e672005-02-01 01:24:21 +0000317 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000318 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000319 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000320 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000321 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000322 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000323 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000324 continue;
325 }
Chris Lattner9f158122003-08-29 05:09:37 +0000326 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000327
328 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000329 }
330}
331
Chris Lattner079edeb2003-10-16 16:53:07 +0000332void Value::replaceAllUsesWith(Value *New) {
333 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
334 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
335 assert(New->getType() == getType() &&
336 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000337
Chris Lattner079edeb2003-10-16 16:53:07 +0000338 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000339}
340
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000341Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000342 if (!isa<PointerType>(getType()))
343 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000344 Value *V = this;
345 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000346 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000347 if (!GEP->hasAllZeroIndices())
348 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000349 V = GEP->getPointerOperand();
350 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
351 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000352 } else {
353 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000354 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000355 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
356 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000357}
358
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000359Value *Value::getUnderlyingObject() {
360 if (!isa<PointerType>(getType()))
361 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000362 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000363 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000364 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000365 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000366 V = GEP->getPointerOperand();
367 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
368 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000369 } else {
370 return V;
371 }
372 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000373 } while (--MaxLookup);
374 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000375}
376
Chris Lattner027d7262008-12-02 18:33:11 +0000377/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000378/// return the value in the PHI node corresponding to PredBB. If not, return
379/// ourself. This is useful if you want to know the value something has in a
380/// predecessor block.
381Value *Value::DoPHITranslation(const BasicBlock *CurBB,
382 const BasicBlock *PredBB) {
383 PHINode *PN = dyn_cast<PHINode>(this);
384 if (PN && PN->getParent() == CurBB)
385 return PN->getIncomingValueForBlock(PredBB);
386 return this;
387}
388
Owen Anderson47db9412009-07-22 00:24:57 +0000389LLVMContext &Value::getContext() const { return VTy->getContext(); }
390
Chris Lattner90234f32009-03-31 22:11:05 +0000391//===----------------------------------------------------------------------===//
392// ValueHandleBase Class
393//===----------------------------------------------------------------------===//
394
395/// ValueHandles - This map keeps track of all of the value handles that are
396/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
397/// not a value has an entry in this map.
398typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
399static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000400static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000401
Dan Gohman3f025952009-05-04 17:25:21 +0000402/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
403/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000404void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
405 assert(List && "Handle list is null?");
406
407 // Splice ourselves into the list.
408 Next = *List;
409 *List = this;
410 setPrevPtr(List);
411 if (Next) {
412 Next->setPrevPtr(&Next);
413 assert(VP == Next->VP && "Added to wrong list?");
414 }
415}
416
417/// AddToUseList - Add this ValueHandle to the use list for VP.
418void ValueHandleBase::AddToUseList() {
419 assert(VP && "Null pointer doesn't have a use list!");
420 if (VP->HasValueHandle) {
421 // If this value already has a ValueHandle, then it must be in the
422 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000423 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000424 ValueHandleBase *&Entry = (*ValueHandles)[VP];
425 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000426 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000427 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000428 }
429
430 // Ok, it doesn't have any handles yet, so we must insert it into the
431 // DenseMap. However, doing this insertion could cause the DenseMap to
432 // reallocate itself, which would invalidate all of the PrevP pointers that
433 // point into the old table. Handle this by checking for reallocation and
434 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000435 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000436 ValueHandlesTy &Handles = *ValueHandles;
437 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
438
439 ValueHandleBase *&Entry = Handles[VP];
440 assert(Entry == 0 && "Value really did already have handles?");
441 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000442 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000443
444 // If reallocation didn't happen or if this was the first insertion, don't
445 // walk the table.
446 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000447 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000448 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000449 }
Chris Lattner90234f32009-03-31 22:11:05 +0000450
451 // Okay, reallocation did happen. Fix the Prev Pointers.
452 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
453 I != E; ++I) {
454 assert(I->second && I->first == I->second->VP && "List invariant broken!");
455 I->second->setPrevPtr(&I->second);
456 }
457}
458
459/// RemoveFromUseList - Remove this ValueHandle from its current use list.
460void ValueHandleBase::RemoveFromUseList() {
461 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
462
463 // Unlink this from its use list.
464 ValueHandleBase **PrevPtr = getPrevPtr();
465 assert(*PrevPtr == this && "List invariant broken");
466
467 *PrevPtr = Next;
468 if (Next) {
469 assert(Next->getPrevPtr() == &Next && "List invariant broken");
470 Next->setPrevPtr(PrevPtr);
471 return;
472 }
473
474 // If the Next pointer was null, then it is possible that this was the last
475 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
476 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000477 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000478 ValueHandlesTy &Handles = *ValueHandles;
479 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
480 Handles.erase(VP);
481 VP->HasValueHandle = false;
482 }
483}
484
485
486void ValueHandleBase::ValueIsDeleted(Value *V) {
487 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
488
489 // Get the linked list base, which is guaranteed to exist since the
490 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000491 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000492 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000493 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000494 assert(Entry && "Value bit set but no entries exist");
495
496 while (Entry) {
497 // Advance pointer to avoid invalidation.
498 ValueHandleBase *ThisNode = Entry;
499 Entry = Entry->Next;
500
501 switch (ThisNode->getKind()) {
502 case Assert:
503#ifndef NDEBUG // Only in -g mode...
504 cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr()
505 << "\n";
506#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000507 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000508 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000509 case Weak:
510 // Weak just goes to null, which will unlink it from the list.
511 ThisNode->operator=(0);
512 break;
513 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000514 // Forward to the subclass's implementation.
515 static_cast<CallbackVH*>(ThisNode)->deleted();
516 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000517 }
518 }
519
520 // All callbacks and weak references should be dropped by now.
521 assert(!V->HasValueHandle && "All references to V were not removed?");
522}
523
524
525void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
526 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
527 assert(Old != New && "Changing value into itself!");
528
529 // Get the linked list base, which is guaranteed to exist since the
530 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000531 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000532 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000533 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000534 assert(Entry && "Value bit set but no entries exist");
535
536 while (Entry) {
537 // Advance pointer to avoid invalidation.
538 ValueHandleBase *ThisNode = Entry;
539 Entry = Entry->Next;
540
541 switch (ThisNode->getKind()) {
542 case Assert:
543 // Asserting handle does not follow RAUW implicitly.
544 break;
545 case Weak:
546 // Weak goes to the new value, which will unlink it from Old's list.
547 ThisNode->operator=(New);
548 break;
549 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000550 // Forward to the subclass's implementation.
551 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
552 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000553 }
554 }
555}
556
Dan Gohman745ad442009-05-02 21:10:48 +0000557/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
558/// more than once.
559CallbackVH::~CallbackVH() {}
560
Chris Lattner9c1b5022008-12-02 07:16:45 +0000561
Chris Lattner2f7c9632001-06-06 20:29:01 +0000562//===----------------------------------------------------------------------===//
563// User Class
564//===----------------------------------------------------------------------===//
565
Chris Lattner2f7c9632001-06-06 20:29:01 +0000566// replaceUsesOfWith - Replaces all references to the "From" definition with
567// references to the "To" definition.
568//
569void User::replaceUsesOfWith(Value *From, Value *To) {
570 if (From == To) return; // Duh what?
571
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000572 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000573 "Cannot call User::replaceUsesofWith on a constant!");
574
Chris Lattnera073acb2001-07-07 08:36:50 +0000575 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
576 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000577 // The side effects of this setOperand call include linking to
578 // "To", adding "this" to the uses list of To, and
579 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000580 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000581 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000582}