blob: b3c0692c0e5c6c8304439aa4e8ba274b8f5f61a8 [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"
Devang Patel7428d8a2009-07-22 17:43:22 +000021#include "llvm/MDNode.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000022#include "llvm/ValueSymbolTable.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000023#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000024#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/LeakDetector.h"
Chris Lattner90234f32009-03-31 22:11:05 +000026#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/ValueHandle.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000028#include "llvm/Support/raw_ostream.h"
Owen Anderson4b660a82009-06-17 17:36:57 +000029#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000030#include "llvm/System/Threading.h"
Chris Lattner90234f32009-03-31 22:11:05 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000032#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner2f7c9632001-06-06 20:29:01 +000035//===----------------------------------------------------------------------===//
36// Value Class
37//===----------------------------------------------------------------------===//
38
Chris Lattner25450e32001-12-13 00:41:27 +000039static inline const Type *checkType(const Type *Ty) {
40 assert(Ty && "Value defined with a null type: Error!");
41 return Ty;
42}
43
Chris Lattner32ab6432007-02-12 05:18:08 +000044Value::Value(const Type *ty, unsigned scid)
Dan Gohman9691e712009-07-17 17:16:59 +000045 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
46 SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000047 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000048 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000049 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
50 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000051 "invalid CallInst type!");
52 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000053 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000054 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000055 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000056}
57
Gordon Henriksen14a55692007-12-10 02:14:30 +000058Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000059 // Notify all ValueHandles (if present) that this value is going away.
60 if (HasValueHandle)
61 ValueHandleBase::ValueIsDeleted(this);
62
Chris Lattner2f7c9632001-06-06 20:29:01 +000063#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000064 // Check to make sure that there are no uses of this value that are still
65 // around when the value is destroyed. If there are, then we have a dangling
66 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000067 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000068 // a <badref>
69 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000070 if (!use_empty()) {
Chris Lattner695bc772008-12-13 18:37:58 +000071 cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000072 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner695bc772008-12-13 18:37:58 +000073 cerr << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000074 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000075 }
76#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000077 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000078
Chris Lattneraf867a32007-03-20 00:18:10 +000079 // If this value is named, destroy the name. This should not be in a symtab
80 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000081 if (Name)
82 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000083
Chris Lattner184b2982002-09-08 18:59:35 +000084 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000085 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000086}
87
Chris Lattner4947e672005-02-01 01:24:21 +000088/// hasNUses - Return true if this Value has exactly N users.
89///
90bool Value::hasNUses(unsigned N) const {
91 use_const_iterator UI = use_begin(), E = use_end();
92
93 for (; N; --N, ++UI)
94 if (UI == E) return false; // Too few.
95 return UI == E;
96}
97
Chris Lattnerd36552f2005-02-23 16:51:11 +000098/// hasNUsesOrMore - Return true if this value has N users or more. This is
99/// logically equivalent to getNumUses() >= N.
100///
101bool Value::hasNUsesOrMore(unsigned N) const {
102 use_const_iterator UI = use_begin(), E = use_end();
103
104 for (; N; --N, ++UI)
105 if (UI == E) return false; // Too few.
106
107 return true;
108}
109
Evan Cheng89553cc2008-06-12 21:15:59 +0000110/// isUsedInBasicBlock - Return true if this value is used in the specified
111/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000112bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000113 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
114 const Instruction *User = dyn_cast<Instruction>(*I);
115 if (User && User->getParent() == BB)
116 return true;
117 }
118 return false;
119}
120
Chris Lattnerd36552f2005-02-23 16:51:11 +0000121
Chris Lattner4947e672005-02-01 01:24:21 +0000122/// getNumUses - This method computes the number of uses of this Value. This
123/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
124/// values.
125unsigned Value::getNumUses() const {
126 return (unsigned)std::distance(use_begin(), use_end());
127}
128
Chris Lattnerb6250822007-02-11 00:37:27 +0000129static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000130 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000131 if (Instruction *I = dyn_cast<Instruction>(V)) {
132 if (BasicBlock *P = I->getParent())
133 if (Function *PP = P->getParent())
134 ST = &PP->getValueSymbolTable();
135 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
136 if (Function *P = BB->getParent())
137 ST = &P->getValueSymbolTable();
138 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
139 if (Module *P = GV->getParent())
140 ST = &P->getValueSymbolTable();
141 } else if (Argument *A = dyn_cast<Argument>(V)) {
142 if (Function *P = A->getParent())
143 ST = &P->getValueSymbolTable();
Devang Patel7428d8a2009-07-22 17:43:22 +0000144 } else if (isa<MDString>(V))
145 return true;
146 else {
Chris Lattnerb6250822007-02-11 00:37:27 +0000147 assert(isa<Constant>(V) && "Unknown value type!");
148 return true; // no name is setable for this.
149 }
150 return false;
151}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000152
Chris Lattner5109a882007-08-10 15:34:35 +0000153/// getNameStart - Return a pointer to a null terminated string for this name.
154/// Note that names can have null characters within the string as well as at
155/// their end. This always returns a non-null pointer.
156const char *Value::getNameStart() const {
157 if (Name == 0) return "";
158 return Name->getKeyData();
159}
160
161/// getNameLen - Return the length of the string, correctly handling nul
162/// characters embedded into them.
163unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000164 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000165}
166
Chris Lattnera1d850e2008-04-30 03:55:40 +0000167/// isName - Return true if this value has the name specified by the provided
168/// nul terminated string.
169bool Value::isName(const char *N) const {
170 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000171 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000172}
173
Chris Lattner5109a882007-08-10 15:34:35 +0000174
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000175std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000176 if (Name == 0) return "";
177 return std::string(Name->getKeyData(),
178 Name->getKeyData()+Name->getKeyLength());
179}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000180
Chris Lattner32ab6432007-02-12 05:18:08 +0000181void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000182 setName(&name[0], name.size());
183}
184
Chris Lattnercb9a6262007-02-13 07:53:34 +0000185void Value::setName(const char *Name) {
186 setName(Name, Name ? strlen(Name) : 0);
187}
188
Chris Lattner1a5de582007-02-12 18:52:59 +0000189void Value::setName(const char *NameStr, unsigned NameLen) {
190 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000191 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000192
Chris Lattnerffb37782005-03-06 02:14:28 +0000193 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000194 ValueSymbolTable *ST;
195 if (getSymTab(this, ST))
196 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000197
Chris Lattner32ab6432007-02-12 05:18:08 +0000198 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000199 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000200 // Free the name for this value.
201 Name->Destroy();
202 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000203 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000204 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000205
206 if (Name) {
207 // Name isn't changing?
208 if (NameLen == Name->getKeyLength() &&
209 !memcmp(Name->getKeyData(), NameStr, NameLen))
210 return;
211 Name->Destroy();
212 }
213
214 // NOTE: Could optimize for the case the name is shrinking to not deallocate
215 // then reallocated.
216
217 // Create the new name.
218 Name = ValueName::Create(NameStr, NameStr+NameLen);
219 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000220 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000221 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000222
223 // NOTE: Could optimize for the case the name is shrinking to not deallocate
224 // then reallocated.
225 if (hasName()) {
226 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000227 if (NameLen == Name->getKeyLength() &&
228 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000229 return;
230
231 // Remove old name.
232 ST->removeValueName(Name);
233 Name->Destroy();
234 Name = 0;
235
Chris Lattner1a5de582007-02-12 18:52:59 +0000236 if (NameLen == 0)
237 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000238 }
239
240 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000241 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000242}
243
Chris Lattner1a5de582007-02-12 18:52:59 +0000244
Chris Lattnerb6250822007-02-11 00:37:27 +0000245/// takeName - transfer the name from V to this value, setting V's name to
246/// empty. It is an error to call V->takeName(V).
247void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000248 ValueSymbolTable *ST = 0;
249 // If this value has a name, drop it.
250 if (hasName()) {
251 // Get the symtab this is in.
252 if (getSymTab(this, ST)) {
253 // We can't set a name on this value, but we need to clear V's name if
254 // it has one.
255 if (V->hasName()) V->setName(0, 0);
256 return; // Cannot set a name on this value (e.g. constant).
257 }
258
259 // Remove old name.
260 if (ST)
261 ST->removeValueName(Name);
262 Name->Destroy();
263 Name = 0;
264 }
265
266 // Now we know that this has no name.
267
268 // If V has no name either, we're done.
269 if (!V->hasName()) return;
270
271 // Get this's symtab if we didn't before.
272 if (!ST) {
273 if (getSymTab(this, ST)) {
274 // Clear V's name.
275 V->setName(0, 0);
276 return; // Cannot set a name on this value (e.g. constant).
277 }
278 }
279
280 // Get V's ST, this should always succed, because V has a name.
281 ValueSymbolTable *VST;
282 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000283 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000284
285 // If these values are both in the same symtab, we can do this very fast.
286 // This works even if both values have no symtab yet.
287 if (ST == VST) {
288 // Take the name!
289 Name = V->Name;
290 V->Name = 0;
291 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000292 return;
293 }
294
Chris Lattnercf835ff2007-02-15 20:01:43 +0000295 // Otherwise, things are slightly more complex. Remove V's name from VST and
296 // then reinsert it into ST.
297
298 if (VST)
299 VST->removeValueName(V->Name);
300 Name = V->Name;
301 V->Name = 0;
302 Name->setValue(this);
303
304 if (ST)
305 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000306}
307
308
Chris Lattner9f158122003-08-29 05:09:37 +0000309// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
310// except that it doesn't have all of the asserts. The asserts fail because we
311// are half-way done resolving types, which causes some types to exist as two
312// different Type*'s at the same time. This is a sledgehammer to work around
313// this problem.
314//
315void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000316 // Notify all ValueHandles (if present) that this value is going away.
317 if (HasValueHandle)
318 ValueHandleBase::ValueIsRAUWd(this, New);
319
Chris Lattner4947e672005-02-01 01:24:21 +0000320 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000321 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000322 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000323 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000324 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000325 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000326 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000327 continue;
328 }
Chris Lattner9f158122003-08-29 05:09:37 +0000329 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000330
331 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000332 }
333}
334
Chris Lattner079edeb2003-10-16 16:53:07 +0000335void Value::replaceAllUsesWith(Value *New) {
336 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
337 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
338 assert(New->getType() == getType() &&
339 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000340
Chris Lattner079edeb2003-10-16 16:53:07 +0000341 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000342}
343
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000344Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000345 if (!isa<PointerType>(getType()))
346 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000347 Value *V = this;
348 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000349 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000350 if (!GEP->hasAllZeroIndices())
351 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000352 V = GEP->getPointerOperand();
353 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
354 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000355 } else {
356 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000357 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000358 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
359 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000360}
361
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000362Value *Value::getUnderlyingObject() {
363 if (!isa<PointerType>(getType()))
364 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000365 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000366 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000367 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000368 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000369 V = GEP->getPointerOperand();
370 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
371 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000372 } else {
373 return V;
374 }
375 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000376 } while (--MaxLookup);
377 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000378}
379
Chris Lattner027d7262008-12-02 18:33:11 +0000380/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000381/// return the value in the PHI node corresponding to PredBB. If not, return
382/// ourself. This is useful if you want to know the value something has in a
383/// predecessor block.
384Value *Value::DoPHITranslation(const BasicBlock *CurBB,
385 const BasicBlock *PredBB) {
386 PHINode *PN = dyn_cast<PHINode>(this);
387 if (PN && PN->getParent() == CurBB)
388 return PN->getIncomingValueForBlock(PredBB);
389 return this;
390}
391
Owen Anderson47db9412009-07-22 00:24:57 +0000392LLVMContext &Value::getContext() const { return VTy->getContext(); }
393
Chris Lattner90234f32009-03-31 22:11:05 +0000394//===----------------------------------------------------------------------===//
395// ValueHandleBase Class
396//===----------------------------------------------------------------------===//
397
398/// ValueHandles - This map keeps track of all of the value handles that are
399/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
400/// not a value has an entry in this map.
401typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
402static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000403static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000404
Dan Gohman3f025952009-05-04 17:25:21 +0000405/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
406/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000407void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
408 assert(List && "Handle list is null?");
409
410 // Splice ourselves into the list.
411 Next = *List;
412 *List = this;
413 setPrevPtr(List);
414 if (Next) {
415 Next->setPrevPtr(&Next);
416 assert(VP == Next->VP && "Added to wrong list?");
417 }
418}
419
420/// AddToUseList - Add this ValueHandle to the use list for VP.
421void ValueHandleBase::AddToUseList() {
422 assert(VP && "Null pointer doesn't have a use list!");
423 if (VP->HasValueHandle) {
424 // If this value already has a ValueHandle, then it must be in the
425 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000426 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000427 ValueHandleBase *&Entry = (*ValueHandles)[VP];
428 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000429 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000430 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000431 }
432
433 // Ok, it doesn't have any handles yet, so we must insert it into the
434 // DenseMap. However, doing this insertion could cause the DenseMap to
435 // reallocate itself, which would invalidate all of the PrevP pointers that
436 // point into the old table. Handle this by checking for reallocation and
437 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000438 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000439 ValueHandlesTy &Handles = *ValueHandles;
440 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
441
442 ValueHandleBase *&Entry = Handles[VP];
443 assert(Entry == 0 && "Value really did already have handles?");
444 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000445 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000446
447 // If reallocation didn't happen or if this was the first insertion, don't
448 // walk the table.
449 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000450 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000451 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000452 }
Chris Lattner90234f32009-03-31 22:11:05 +0000453
454 // Okay, reallocation did happen. Fix the Prev Pointers.
455 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
456 I != E; ++I) {
457 assert(I->second && I->first == I->second->VP && "List invariant broken!");
458 I->second->setPrevPtr(&I->second);
459 }
460}
461
462/// RemoveFromUseList - Remove this ValueHandle from its current use list.
463void ValueHandleBase::RemoveFromUseList() {
464 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
465
466 // Unlink this from its use list.
467 ValueHandleBase **PrevPtr = getPrevPtr();
468 assert(*PrevPtr == this && "List invariant broken");
469
470 *PrevPtr = Next;
471 if (Next) {
472 assert(Next->getPrevPtr() == &Next && "List invariant broken");
473 Next->setPrevPtr(PrevPtr);
474 return;
475 }
476
477 // If the Next pointer was null, then it is possible that this was the last
478 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
479 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000480 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000481 ValueHandlesTy &Handles = *ValueHandles;
482 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
483 Handles.erase(VP);
484 VP->HasValueHandle = false;
485 }
486}
487
488
489void ValueHandleBase::ValueIsDeleted(Value *V) {
490 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
491
492 // Get the linked list base, which is guaranteed to exist since the
493 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000494 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000495 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000496 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000497 assert(Entry && "Value bit set but no entries exist");
498
499 while (Entry) {
500 // Advance pointer to avoid invalidation.
501 ValueHandleBase *ThisNode = Entry;
502 Entry = Entry->Next;
503
504 switch (ThisNode->getKind()) {
505 case Assert:
506#ifndef NDEBUG // Only in -g mode...
507 cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr()
508 << "\n";
509#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000510 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000511 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000512 case Weak:
513 // Weak just goes to null, which will unlink it from the list.
514 ThisNode->operator=(0);
515 break;
516 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000517 // Forward to the subclass's implementation.
518 static_cast<CallbackVH*>(ThisNode)->deleted();
519 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000520 }
521 }
522
523 // All callbacks and weak references should be dropped by now.
524 assert(!V->HasValueHandle && "All references to V were not removed?");
525}
526
527
528void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
529 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
530 assert(Old != New && "Changing value into itself!");
531
532 // Get the linked list base, which is guaranteed to exist since the
533 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000534 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000535 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000536 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000537 assert(Entry && "Value bit set but no entries exist");
538
539 while (Entry) {
540 // Advance pointer to avoid invalidation.
541 ValueHandleBase *ThisNode = Entry;
542 Entry = Entry->Next;
543
544 switch (ThisNode->getKind()) {
545 case Assert:
546 // Asserting handle does not follow RAUW implicitly.
547 break;
548 case Weak:
549 // Weak goes to the new value, which will unlink it from Old's list.
550 ThisNode->operator=(New);
551 break;
552 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000553 // Forward to the subclass's implementation.
554 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
555 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000556 }
557 }
558}
559
Dan Gohman745ad442009-05-02 21:10:48 +0000560/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
561/// more than once.
562CallbackVH::~CallbackVH() {}
563
Chris Lattner9c1b5022008-12-02 07:16:45 +0000564
Chris Lattner2f7c9632001-06-06 20:29:01 +0000565//===----------------------------------------------------------------------===//
566// User Class
567//===----------------------------------------------------------------------===//
568
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569// replaceUsesOfWith - Replaces all references to the "From" definition with
570// references to the "To" definition.
571//
572void User::replaceUsesOfWith(Value *From, Value *To) {
573 if (From == To) return; // Duh what?
574
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000575 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000576 "Cannot call User::replaceUsesofWith on a constant!");
577
Chris Lattnera073acb2001-07-07 08:36:50 +0000578 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
579 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000580 // The side effects of this setOperand call include linking to
581 // "To", adding "this" to the uses list of To, and
582 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000583 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585}