blob: b35ad50798409a8a17da4a9a8f87fd18cbe5a725 [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"
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"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/LeakDetector.h"
Chris Lattner90234f32009-03-31 22:11:05 +000024#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/ValueHandle.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000026#include "llvm/Support/raw_ostream.h"
Owen Anderson4b660a82009-06-17 17:36:57 +000027#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000028#include "llvm/System/Threading.h"
Chris Lattner90234f32009-03-31 22:11:05 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000030#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34// Value Class
35//===----------------------------------------------------------------------===//
36
Chris Lattner25450e32001-12-13 00:41:27 +000037static inline const Type *checkType(const Type *Ty) {
38 assert(Ty && "Value defined with a null type: Error!");
39 return Ty;
40}
41
Chris Lattner32ab6432007-02-12 05:18:08 +000042Value::Value(const Type *ty, unsigned scid)
Dan Gohman9691e712009-07-17 17:16:59 +000043 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
44 SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000045 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000046 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000047 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
48 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000049 "invalid CallInst type!");
50 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000051 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000052 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000053 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000054}
55
Gordon Henriksen14a55692007-12-10 02:14:30 +000056Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000057 // Notify all ValueHandles (if present) that this value is going away.
58 if (HasValueHandle)
59 ValueHandleBase::ValueIsDeleted(this);
60
Chris Lattner2f7c9632001-06-06 20:29:01 +000061#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000062 // Check to make sure that there are no uses of this value that are still
63 // around when the value is destroyed. If there are, then we have a dangling
64 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000065 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000066 // a <badref>
67 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000068 if (!use_empty()) {
Chris Lattner695bc772008-12-13 18:37:58 +000069 cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000070 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner695bc772008-12-13 18:37:58 +000071 cerr << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000072 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000073 }
74#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000075 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000076
Chris Lattneraf867a32007-03-20 00:18:10 +000077 // If this value is named, destroy the name. This should not be in a symtab
78 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000079 if (Name)
80 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000081
Chris Lattner184b2982002-09-08 18:59:35 +000082 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000083 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000084}
85
Chris Lattner4947e672005-02-01 01:24:21 +000086/// hasNUses - Return true if this Value has exactly N users.
87///
88bool Value::hasNUses(unsigned N) const {
89 use_const_iterator UI = use_begin(), E = use_end();
90
91 for (; N; --N, ++UI)
92 if (UI == E) return false; // Too few.
93 return UI == E;
94}
95
Chris Lattnerd36552f2005-02-23 16:51:11 +000096/// hasNUsesOrMore - Return true if this value has N users or more. This is
97/// logically equivalent to getNumUses() >= N.
98///
99bool Value::hasNUsesOrMore(unsigned N) const {
100 use_const_iterator UI = use_begin(), E = use_end();
101
102 for (; N; --N, ++UI)
103 if (UI == E) return false; // Too few.
104
105 return true;
106}
107
Evan Cheng89553cc2008-06-12 21:15:59 +0000108/// isUsedInBasicBlock - Return true if this value is used in the specified
109/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000110bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000111 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
112 const Instruction *User = dyn_cast<Instruction>(*I);
113 if (User && User->getParent() == BB)
114 return true;
115 }
116 return false;
117}
118
Chris Lattnerd36552f2005-02-23 16:51:11 +0000119
Chris Lattner4947e672005-02-01 01:24:21 +0000120/// getNumUses - This method computes the number of uses of this Value. This
121/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
122/// values.
123unsigned Value::getNumUses() const {
124 return (unsigned)std::distance(use_begin(), use_end());
125}
126
Chris Lattnerb6250822007-02-11 00:37:27 +0000127static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000128 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000129 if (Instruction *I = dyn_cast<Instruction>(V)) {
130 if (BasicBlock *P = I->getParent())
131 if (Function *PP = P->getParent())
132 ST = &PP->getValueSymbolTable();
133 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
134 if (Function *P = BB->getParent())
135 ST = &P->getValueSymbolTable();
136 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
137 if (Module *P = GV->getParent())
138 ST = &P->getValueSymbolTable();
139 } else if (Argument *A = dyn_cast<Argument>(V)) {
140 if (Function *P = A->getParent())
141 ST = &P->getValueSymbolTable();
142 } else {
143 assert(isa<Constant>(V) && "Unknown value type!");
144 return true; // no name is setable for this.
145 }
146 return false;
147}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000148
Chris Lattner5109a882007-08-10 15:34:35 +0000149/// getNameStart - Return a pointer to a null terminated string for this name.
150/// Note that names can have null characters within the string as well as at
151/// their end. This always returns a non-null pointer.
152const char *Value::getNameStart() const {
153 if (Name == 0) return "";
154 return Name->getKeyData();
155}
156
157/// getNameLen - Return the length of the string, correctly handling nul
158/// characters embedded into them.
159unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000160 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000161}
162
Chris Lattnera1d850e2008-04-30 03:55:40 +0000163/// isName - Return true if this value has the name specified by the provided
164/// nul terminated string.
165bool Value::isName(const char *N) const {
166 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000167 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000168}
169
Chris Lattner5109a882007-08-10 15:34:35 +0000170
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000171std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000172 if (Name == 0) return "";
173 return std::string(Name->getKeyData(),
174 Name->getKeyData()+Name->getKeyLength());
175}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000176
Chris Lattner32ab6432007-02-12 05:18:08 +0000177void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000178 setName(&name[0], name.size());
179}
180
Chris Lattnercb9a6262007-02-13 07:53:34 +0000181void Value::setName(const char *Name) {
182 setName(Name, Name ? strlen(Name) : 0);
183}
184
Chris Lattner1a5de582007-02-12 18:52:59 +0000185void Value::setName(const char *NameStr, unsigned NameLen) {
186 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000187 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000188
Chris Lattnerffb37782005-03-06 02:14:28 +0000189 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000190 ValueSymbolTable *ST;
191 if (getSymTab(this, ST))
192 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000193
Chris Lattner32ab6432007-02-12 05:18:08 +0000194 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000195 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000196 // Free the name for this value.
197 Name->Destroy();
198 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000199 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000200 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000201
202 if (Name) {
203 // Name isn't changing?
204 if (NameLen == Name->getKeyLength() &&
205 !memcmp(Name->getKeyData(), NameStr, NameLen))
206 return;
207 Name->Destroy();
208 }
209
210 // NOTE: Could optimize for the case the name is shrinking to not deallocate
211 // then reallocated.
212
213 // Create the new name.
214 Name = ValueName::Create(NameStr, NameStr+NameLen);
215 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000216 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000217 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000218
219 // NOTE: Could optimize for the case the name is shrinking to not deallocate
220 // then reallocated.
221 if (hasName()) {
222 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000223 if (NameLen == Name->getKeyLength() &&
224 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000225 return;
226
227 // Remove old name.
228 ST->removeValueName(Name);
229 Name->Destroy();
230 Name = 0;
231
Chris Lattner1a5de582007-02-12 18:52:59 +0000232 if (NameLen == 0)
233 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000234 }
235
236 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000237 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000238}
239
Chris Lattner1a5de582007-02-12 18:52:59 +0000240
Chris Lattnerb6250822007-02-11 00:37:27 +0000241/// takeName - transfer the name from V to this value, setting V's name to
242/// empty. It is an error to call V->takeName(V).
243void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000244 ValueSymbolTable *ST = 0;
245 // If this value has a name, drop it.
246 if (hasName()) {
247 // Get the symtab this is in.
248 if (getSymTab(this, ST)) {
249 // We can't set a name on this value, but we need to clear V's name if
250 // it has one.
251 if (V->hasName()) V->setName(0, 0);
252 return; // Cannot set a name on this value (e.g. constant).
253 }
254
255 // Remove old name.
256 if (ST)
257 ST->removeValueName(Name);
258 Name->Destroy();
259 Name = 0;
260 }
261
262 // Now we know that this has no name.
263
264 // If V has no name either, we're done.
265 if (!V->hasName()) return;
266
267 // Get this's symtab if we didn't before.
268 if (!ST) {
269 if (getSymTab(this, ST)) {
270 // Clear V's name.
271 V->setName(0, 0);
272 return; // Cannot set a name on this value (e.g. constant).
273 }
274 }
275
276 // Get V's ST, this should always succed, because V has a name.
277 ValueSymbolTable *VST;
278 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000279 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000280
281 // If these values are both in the same symtab, we can do this very fast.
282 // This works even if both values have no symtab yet.
283 if (ST == VST) {
284 // Take the name!
285 Name = V->Name;
286 V->Name = 0;
287 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000288 return;
289 }
290
Chris Lattnercf835ff2007-02-15 20:01:43 +0000291 // Otherwise, things are slightly more complex. Remove V's name from VST and
292 // then reinsert it into ST.
293
294 if (VST)
295 VST->removeValueName(V->Name);
296 Name = V->Name;
297 V->Name = 0;
298 Name->setValue(this);
299
300 if (ST)
301 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000302}
303
304
Chris Lattner9f158122003-08-29 05:09:37 +0000305// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
306// except that it doesn't have all of the asserts. The asserts fail because we
307// are half-way done resolving types, which causes some types to exist as two
308// different Type*'s at the same time. This is a sledgehammer to work around
309// this problem.
310//
311void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000312 // Notify all ValueHandles (if present) that this value is going away.
313 if (HasValueHandle)
314 ValueHandleBase::ValueIsRAUWd(this, New);
315
Chris Lattner4947e672005-02-01 01:24:21 +0000316 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000317 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000318 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000319 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000320 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000321 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000322 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000323 continue;
324 }
Chris Lattner9f158122003-08-29 05:09:37 +0000325 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000326
327 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000328 }
329}
330
Chris Lattner079edeb2003-10-16 16:53:07 +0000331void Value::replaceAllUsesWith(Value *New) {
332 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
333 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
334 assert(New->getType() == getType() &&
335 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000336
Chris Lattner079edeb2003-10-16 16:53:07 +0000337 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338}
339
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000340Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000341 if (!isa<PointerType>(getType()))
342 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000343 Value *V = this;
344 do {
345 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
346 if (CE->getOpcode() == Instruction::GetElementPtr) {
347 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
348 if (!CE->getOperand(i)->isNullValue())
349 return V;
350 V = CE->getOperand(0);
351 } else if (CE->getOpcode() == Instruction::BitCast) {
352 V = CE->getOperand(0);
353 } else {
354 return V;
355 }
356 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
357 if (!GEP->hasAllZeroIndices())
358 return V;
359 V = GEP->getOperand(0);
360 } else if (BitCastInst *CI = dyn_cast<BitCastInst>(V)) {
361 V = CI->getOperand(0);
362 } else {
363 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000364 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000365 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
366 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000367}
368
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000369Value *Value::getUnderlyingObject() {
370 if (!isa<PointerType>(getType()))
371 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000372 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000373 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000374 do {
375 if (Instruction *I = dyn_cast<Instruction>(V)) {
376 if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I))
377 return V;
378 V = I->getOperand(0);
379 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
380 if (CE->getOpcode() != Instruction::BitCast &&
381 CE->getOpcode() != Instruction::GetElementPtr)
382 return V;
383 V = CE->getOperand(0);
384 } else {
385 return V;
386 }
387 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000388 } while (--MaxLookup);
389 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000390}
391
Chris Lattner027d7262008-12-02 18:33:11 +0000392/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000393/// return the value in the PHI node corresponding to PredBB. If not, return
394/// ourself. This is useful if you want to know the value something has in a
395/// predecessor block.
396Value *Value::DoPHITranslation(const BasicBlock *CurBB,
397 const BasicBlock *PredBB) {
398 PHINode *PN = dyn_cast<PHINode>(this);
399 if (PN && PN->getParent() == CurBB)
400 return PN->getIncomingValueForBlock(PredBB);
401 return this;
402}
403
Chris Lattner90234f32009-03-31 22:11:05 +0000404//===----------------------------------------------------------------------===//
405// ValueHandleBase Class
406//===----------------------------------------------------------------------===//
407
408/// ValueHandles - This map keeps track of all of the value handles that are
409/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
410/// not a value has an entry in this map.
411typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
412static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000413static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000414
Dan Gohman3f025952009-05-04 17:25:21 +0000415/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
416/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000417void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
418 assert(List && "Handle list is null?");
419
420 // Splice ourselves into the list.
421 Next = *List;
422 *List = this;
423 setPrevPtr(List);
424 if (Next) {
425 Next->setPrevPtr(&Next);
426 assert(VP == Next->VP && "Added to wrong list?");
427 }
428}
429
430/// AddToUseList - Add this ValueHandle to the use list for VP.
431void ValueHandleBase::AddToUseList() {
432 assert(VP && "Null pointer doesn't have a use list!");
433 if (VP->HasValueHandle) {
434 // If this value already has a ValueHandle, then it must be in the
435 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000436 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000437 ValueHandleBase *&Entry = (*ValueHandles)[VP];
438 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000439 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000440 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000441 }
442
443 // Ok, it doesn't have any handles yet, so we must insert it into the
444 // DenseMap. However, doing this insertion could cause the DenseMap to
445 // reallocate itself, which would invalidate all of the PrevP pointers that
446 // point into the old table. Handle this by checking for reallocation and
447 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000448 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000449 ValueHandlesTy &Handles = *ValueHandles;
450 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
451
452 ValueHandleBase *&Entry = Handles[VP];
453 assert(Entry == 0 && "Value really did already have handles?");
454 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000455 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000456
457 // If reallocation didn't happen or if this was the first insertion, don't
458 // walk the table.
459 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000460 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000461 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000462 }
Chris Lattner90234f32009-03-31 22:11:05 +0000463
464 // Okay, reallocation did happen. Fix the Prev Pointers.
465 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
466 I != E; ++I) {
467 assert(I->second && I->first == I->second->VP && "List invariant broken!");
468 I->second->setPrevPtr(&I->second);
469 }
470}
471
472/// RemoveFromUseList - Remove this ValueHandle from its current use list.
473void ValueHandleBase::RemoveFromUseList() {
474 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
475
476 // Unlink this from its use list.
477 ValueHandleBase **PrevPtr = getPrevPtr();
478 assert(*PrevPtr == this && "List invariant broken");
479
480 *PrevPtr = Next;
481 if (Next) {
482 assert(Next->getPrevPtr() == &Next && "List invariant broken");
483 Next->setPrevPtr(PrevPtr);
484 return;
485 }
486
487 // If the Next pointer was null, then it is possible that this was the last
488 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
489 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000490 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000491 ValueHandlesTy &Handles = *ValueHandles;
492 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
493 Handles.erase(VP);
494 VP->HasValueHandle = false;
495 }
496}
497
498
499void ValueHandleBase::ValueIsDeleted(Value *V) {
500 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
501
502 // Get the linked list base, which is guaranteed to exist since the
503 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000504 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000505 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000506 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000507 assert(Entry && "Value bit set but no entries exist");
508
509 while (Entry) {
510 // Advance pointer to avoid invalidation.
511 ValueHandleBase *ThisNode = Entry;
512 Entry = Entry->Next;
513
514 switch (ThisNode->getKind()) {
515 case Assert:
516#ifndef NDEBUG // Only in -g mode...
517 cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr()
518 << "\n";
519#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000520 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000521 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000522 case Weak:
523 // Weak just goes to null, which will unlink it from the list.
524 ThisNode->operator=(0);
525 break;
526 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000527 // Forward to the subclass's implementation.
528 static_cast<CallbackVH*>(ThisNode)->deleted();
529 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000530 }
531 }
532
533 // All callbacks and weak references should be dropped by now.
534 assert(!V->HasValueHandle && "All references to V were not removed?");
535}
536
537
538void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
539 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
540 assert(Old != New && "Changing value into itself!");
541
542 // Get the linked list base, which is guaranteed to exist since the
543 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000544 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000545 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000546 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000547 assert(Entry && "Value bit set but no entries exist");
548
549 while (Entry) {
550 // Advance pointer to avoid invalidation.
551 ValueHandleBase *ThisNode = Entry;
552 Entry = Entry->Next;
553
554 switch (ThisNode->getKind()) {
555 case Assert:
556 // Asserting handle does not follow RAUW implicitly.
557 break;
558 case Weak:
559 // Weak goes to the new value, which will unlink it from Old's list.
560 ThisNode->operator=(New);
561 break;
562 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000563 // Forward to the subclass's implementation.
564 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
565 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000566 }
567 }
568}
569
Dan Gohman745ad442009-05-02 21:10:48 +0000570/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
571/// more than once.
572CallbackVH::~CallbackVH() {}
573
Chris Lattner9c1b5022008-12-02 07:16:45 +0000574
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575//===----------------------------------------------------------------------===//
576// User Class
577//===----------------------------------------------------------------------===//
578
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579// replaceUsesOfWith - Replaces all references to the "From" definition with
580// references to the "To" definition.
581//
582void User::replaceUsesOfWith(Value *From, Value *To) {
583 if (From == To) return; // Duh what?
584
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000585 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000586 "Cannot call User::replaceUsesofWith on a constant!");
587
Chris Lattnera073acb2001-07-07 08:36:50 +0000588 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
589 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590 // The side effects of this setOperand call include linking to
591 // "To", adding "this" to the uses list of To, and
592 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000593 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595}