blob: 0120333481c3071a7d04ab9145783852bd8a2a94 [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)
Chris Lattner90234f32009-03-31 22:11:05 +000043 : SubclassID(scid), HasValueHandle(0), SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000044 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000045 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000046 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
47 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000048 "invalid CallInst type!");
49 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000050 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000051 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000052 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000053}
54
Gordon Henriksen14a55692007-12-10 02:14:30 +000055Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000056 // Notify all ValueHandles (if present) that this value is going away.
57 if (HasValueHandle)
58 ValueHandleBase::ValueIsDeleted(this);
59
Chris Lattner2f7c9632001-06-06 20:29:01 +000060#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000061 // Check to make sure that there are no uses of this value that are still
62 // around when the value is destroyed. If there are, then we have a dangling
63 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000064 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000065 // a <badref>
66 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000067 if (!use_empty()) {
Chris Lattner695bc772008-12-13 18:37:58 +000068 cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000069 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner695bc772008-12-13 18:37:58 +000070 cerr << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000071 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000072 }
73#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000074 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000075
Chris Lattneraf867a32007-03-20 00:18:10 +000076 // If this value is named, destroy the name. This should not be in a symtab
77 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000078 if (Name)
79 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000080
Chris Lattner184b2982002-09-08 18:59:35 +000081 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000082 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000083}
84
Chris Lattner4947e672005-02-01 01:24:21 +000085/// hasNUses - Return true if this Value has exactly N users.
86///
87bool Value::hasNUses(unsigned N) const {
88 use_const_iterator UI = use_begin(), E = use_end();
89
90 for (; N; --N, ++UI)
91 if (UI == E) return false; // Too few.
92 return UI == E;
93}
94
Chris Lattnerd36552f2005-02-23 16:51:11 +000095/// hasNUsesOrMore - Return true if this value has N users or more. This is
96/// logically equivalent to getNumUses() >= N.
97///
98bool Value::hasNUsesOrMore(unsigned N) const {
99 use_const_iterator UI = use_begin(), E = use_end();
100
101 for (; N; --N, ++UI)
102 if (UI == E) return false; // Too few.
103
104 return true;
105}
106
Evan Cheng89553cc2008-06-12 21:15:59 +0000107/// isUsedInBasicBlock - Return true if this value is used in the specified
108/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000109bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000110 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
111 const Instruction *User = dyn_cast<Instruction>(*I);
112 if (User && User->getParent() == BB)
113 return true;
114 }
115 return false;
116}
117
Chris Lattnerd36552f2005-02-23 16:51:11 +0000118
Chris Lattner4947e672005-02-01 01:24:21 +0000119/// getNumUses - This method computes the number of uses of this Value. This
120/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
121/// values.
122unsigned Value::getNumUses() const {
123 return (unsigned)std::distance(use_begin(), use_end());
124}
125
Chris Lattnerb6250822007-02-11 00:37:27 +0000126static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000127 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000128 if (Instruction *I = dyn_cast<Instruction>(V)) {
129 if (BasicBlock *P = I->getParent())
130 if (Function *PP = P->getParent())
131 ST = &PP->getValueSymbolTable();
132 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
133 if (Function *P = BB->getParent())
134 ST = &P->getValueSymbolTable();
135 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
136 if (Module *P = GV->getParent())
137 ST = &P->getValueSymbolTable();
138 } else if (Argument *A = dyn_cast<Argument>(V)) {
139 if (Function *P = A->getParent())
140 ST = &P->getValueSymbolTable();
141 } else {
142 assert(isa<Constant>(V) && "Unknown value type!");
143 return true; // no name is setable for this.
144 }
145 return false;
146}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000147
Chris Lattner5109a882007-08-10 15:34:35 +0000148/// getNameStart - Return a pointer to a null terminated string for this name.
149/// Note that names can have null characters within the string as well as at
150/// their end. This always returns a non-null pointer.
151const char *Value::getNameStart() const {
152 if (Name == 0) return "";
153 return Name->getKeyData();
154}
155
156/// getNameLen - Return the length of the string, correctly handling nul
157/// characters embedded into them.
158unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000159 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000160}
161
Chris Lattnera1d850e2008-04-30 03:55:40 +0000162/// isName - Return true if this value has the name specified by the provided
163/// nul terminated string.
164bool Value::isName(const char *N) const {
165 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000166 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000167}
168
Chris Lattner5109a882007-08-10 15:34:35 +0000169
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000170std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000171 if (Name == 0) return "";
172 return std::string(Name->getKeyData(),
173 Name->getKeyData()+Name->getKeyLength());
174}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000175
Chris Lattner32ab6432007-02-12 05:18:08 +0000176void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000177 setName(&name[0], name.size());
178}
179
Chris Lattnercb9a6262007-02-13 07:53:34 +0000180void Value::setName(const char *Name) {
181 setName(Name, Name ? strlen(Name) : 0);
182}
183
Chris Lattner1a5de582007-02-12 18:52:59 +0000184void Value::setName(const char *NameStr, unsigned NameLen) {
185 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000186 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000187
Chris Lattnerffb37782005-03-06 02:14:28 +0000188 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000189 ValueSymbolTable *ST;
190 if (getSymTab(this, ST))
191 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000192
Chris Lattner32ab6432007-02-12 05:18:08 +0000193 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000194 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000195 // Free the name for this value.
196 Name->Destroy();
197 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000198 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000199 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000200
201 if (Name) {
202 // Name isn't changing?
203 if (NameLen == Name->getKeyLength() &&
204 !memcmp(Name->getKeyData(), NameStr, NameLen))
205 return;
206 Name->Destroy();
207 }
208
209 // NOTE: Could optimize for the case the name is shrinking to not deallocate
210 // then reallocated.
211
212 // Create the new name.
213 Name = ValueName::Create(NameStr, NameStr+NameLen);
214 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000215 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000216 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000217
218 // NOTE: Could optimize for the case the name is shrinking to not deallocate
219 // then reallocated.
220 if (hasName()) {
221 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000222 if (NameLen == Name->getKeyLength() &&
223 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000224 return;
225
226 // Remove old name.
227 ST->removeValueName(Name);
228 Name->Destroy();
229 Name = 0;
230
Chris Lattner1a5de582007-02-12 18:52:59 +0000231 if (NameLen == 0)
232 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000233 }
234
235 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000236 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000237}
238
Chris Lattner1a5de582007-02-12 18:52:59 +0000239
Chris Lattnerb6250822007-02-11 00:37:27 +0000240/// takeName - transfer the name from V to this value, setting V's name to
241/// empty. It is an error to call V->takeName(V).
242void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000243 ValueSymbolTable *ST = 0;
244 // If this value has a name, drop it.
245 if (hasName()) {
246 // Get the symtab this is in.
247 if (getSymTab(this, ST)) {
248 // We can't set a name on this value, but we need to clear V's name if
249 // it has one.
250 if (V->hasName()) V->setName(0, 0);
251 return; // Cannot set a name on this value (e.g. constant).
252 }
253
254 // Remove old name.
255 if (ST)
256 ST->removeValueName(Name);
257 Name->Destroy();
258 Name = 0;
259 }
260
261 // Now we know that this has no name.
262
263 // If V has no name either, we're done.
264 if (!V->hasName()) return;
265
266 // Get this's symtab if we didn't before.
267 if (!ST) {
268 if (getSymTab(this, ST)) {
269 // Clear V's name.
270 V->setName(0, 0);
271 return; // Cannot set a name on this value (e.g. constant).
272 }
273 }
274
275 // Get V's ST, this should always succed, because V has a name.
276 ValueSymbolTable *VST;
277 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000278 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000279
280 // If these values are both in the same symtab, we can do this very fast.
281 // This works even if both values have no symtab yet.
282 if (ST == VST) {
283 // Take the name!
284 Name = V->Name;
285 V->Name = 0;
286 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000287 return;
288 }
289
Chris Lattnercf835ff2007-02-15 20:01:43 +0000290 // Otherwise, things are slightly more complex. Remove V's name from VST and
291 // then reinsert it into ST.
292
293 if (VST)
294 VST->removeValueName(V->Name);
295 Name = V->Name;
296 V->Name = 0;
297 Name->setValue(this);
298
299 if (ST)
300 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000301}
302
303
Chris Lattner9f158122003-08-29 05:09:37 +0000304// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
305// except that it doesn't have all of the asserts. The asserts fail because we
306// are half-way done resolving types, which causes some types to exist as two
307// different Type*'s at the same time. This is a sledgehammer to work around
308// this problem.
309//
310void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000311 // Notify all ValueHandles (if present) that this value is going away.
312 if (HasValueHandle)
313 ValueHandleBase::ValueIsRAUWd(this, New);
314
Chris Lattner4947e672005-02-01 01:24:21 +0000315 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000316 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000317 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000318 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000319 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000320 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000321 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000322 continue;
323 }
Chris Lattner9f158122003-08-29 05:09:37 +0000324 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000325
326 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000327 }
328}
329
Chris Lattner079edeb2003-10-16 16:53:07 +0000330void Value::replaceAllUsesWith(Value *New) {
331 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
332 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
333 assert(New->getType() == getType() &&
334 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000335
Chris Lattner079edeb2003-10-16 16:53:07 +0000336 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000337}
338
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000339Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000340 if (!isa<PointerType>(getType()))
341 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000342 Value *V = this;
343 do {
344 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
345 if (CE->getOpcode() == Instruction::GetElementPtr) {
346 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
347 if (!CE->getOperand(i)->isNullValue())
348 return V;
349 V = CE->getOperand(0);
350 } else if (CE->getOpcode() == Instruction::BitCast) {
351 V = CE->getOperand(0);
352 } else {
353 return V;
354 }
355 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
356 if (!GEP->hasAllZeroIndices())
357 return V;
358 V = GEP->getOperand(0);
359 } else if (BitCastInst *CI = dyn_cast<BitCastInst>(V)) {
360 V = CI->getOperand(0);
361 } else {
362 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000363 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000364 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
365 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000366}
367
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000368Value *Value::getUnderlyingObject() {
369 if (!isa<PointerType>(getType()))
370 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000371 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000372 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000373 do {
374 if (Instruction *I = dyn_cast<Instruction>(V)) {
375 if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I))
376 return V;
377 V = I->getOperand(0);
378 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
379 if (CE->getOpcode() != Instruction::BitCast &&
380 CE->getOpcode() != Instruction::GetElementPtr)
381 return V;
382 V = CE->getOperand(0);
383 } else {
384 return V;
385 }
386 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000387 } while (--MaxLookup);
388 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000389}
390
Chris Lattner027d7262008-12-02 18:33:11 +0000391/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000392/// return the value in the PHI node corresponding to PredBB. If not, return
393/// ourself. This is useful if you want to know the value something has in a
394/// predecessor block.
395Value *Value::DoPHITranslation(const BasicBlock *CurBB,
396 const BasicBlock *PredBB) {
397 PHINode *PN = dyn_cast<PHINode>(this);
398 if (PN && PN->getParent() == CurBB)
399 return PN->getIncomingValueForBlock(PredBB);
400 return this;
401}
402
Chris Lattner90234f32009-03-31 22:11:05 +0000403//===----------------------------------------------------------------------===//
404// ValueHandleBase Class
405//===----------------------------------------------------------------------===//
406
407/// ValueHandles - This map keeps track of all of the value handles that are
408/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
409/// not a value has an entry in this map.
410typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
411static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000412static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000413
Dan Gohman3f025952009-05-04 17:25:21 +0000414/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
415/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000416void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
417 assert(List && "Handle list is null?");
418
419 // Splice ourselves into the list.
420 Next = *List;
421 *List = this;
422 setPrevPtr(List);
423 if (Next) {
424 Next->setPrevPtr(&Next);
425 assert(VP == Next->VP && "Added to wrong list?");
426 }
427}
428
429/// AddToUseList - Add this ValueHandle to the use list for VP.
430void ValueHandleBase::AddToUseList() {
431 assert(VP && "Null pointer doesn't have a use list!");
432 if (VP->HasValueHandle) {
433 // If this value already has a ValueHandle, then it must be in the
434 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000435 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000436 ValueHandleBase *&Entry = (*ValueHandles)[VP];
437 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000438 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000439 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000440 }
441
442 // Ok, it doesn't have any handles yet, so we must insert it into the
443 // DenseMap. However, doing this insertion could cause the DenseMap to
444 // reallocate itself, which would invalidate all of the PrevP pointers that
445 // point into the old table. Handle this by checking for reallocation and
446 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000447 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000448 ValueHandlesTy &Handles = *ValueHandles;
449 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
450
451 ValueHandleBase *&Entry = Handles[VP];
452 assert(Entry == 0 && "Value really did already have handles?");
453 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000454 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000455
456 // If reallocation didn't happen or if this was the first insertion, don't
457 // walk the table.
458 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000459 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000460 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000461 }
Chris Lattner90234f32009-03-31 22:11:05 +0000462
463 // Okay, reallocation did happen. Fix the Prev Pointers.
464 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
465 I != E; ++I) {
466 assert(I->second && I->first == I->second->VP && "List invariant broken!");
467 I->second->setPrevPtr(&I->second);
468 }
469}
470
471/// RemoveFromUseList - Remove this ValueHandle from its current use list.
472void ValueHandleBase::RemoveFromUseList() {
473 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
474
475 // Unlink this from its use list.
476 ValueHandleBase **PrevPtr = getPrevPtr();
477 assert(*PrevPtr == this && "List invariant broken");
478
479 *PrevPtr = Next;
480 if (Next) {
481 assert(Next->getPrevPtr() == &Next && "List invariant broken");
482 Next->setPrevPtr(PrevPtr);
483 return;
484 }
485
486 // If the Next pointer was null, then it is possible that this was the last
487 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
488 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000489 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000490 ValueHandlesTy &Handles = *ValueHandles;
491 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
492 Handles.erase(VP);
493 VP->HasValueHandle = false;
494 }
495}
496
497
498void ValueHandleBase::ValueIsDeleted(Value *V) {
499 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
500
501 // Get the linked list base, which is guaranteed to exist since the
502 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000503 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000504 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000505 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000506 assert(Entry && "Value bit set but no entries exist");
507
508 while (Entry) {
509 // Advance pointer to avoid invalidation.
510 ValueHandleBase *ThisNode = Entry;
511 Entry = Entry->Next;
512
513 switch (ThisNode->getKind()) {
514 case Assert:
515#ifndef NDEBUG // Only in -g mode...
516 cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr()
517 << "\n";
518#endif
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000519 LLVM_UNREACHABLE("An asserting value handle still pointed to this"
520 "value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000521 case Weak:
522 // Weak just goes to null, which will unlink it from the list.
523 ThisNode->operator=(0);
524 break;
525 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000526 // Forward to the subclass's implementation.
527 static_cast<CallbackVH*>(ThisNode)->deleted();
528 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000529 }
530 }
531
532 // All callbacks and weak references should be dropped by now.
533 assert(!V->HasValueHandle && "All references to V were not removed?");
534}
535
536
537void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
538 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
539 assert(Old != New && "Changing value into itself!");
540
541 // Get the linked list base, which is guaranteed to exist since the
542 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000543 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000544 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000545 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000546 assert(Entry && "Value bit set but no entries exist");
547
548 while (Entry) {
549 // Advance pointer to avoid invalidation.
550 ValueHandleBase *ThisNode = Entry;
551 Entry = Entry->Next;
552
553 switch (ThisNode->getKind()) {
554 case Assert:
555 // Asserting handle does not follow RAUW implicitly.
556 break;
557 case Weak:
558 // Weak goes to the new value, which will unlink it from Old's list.
559 ThisNode->operator=(New);
560 break;
561 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000562 // Forward to the subclass's implementation.
563 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
564 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000565 }
566 }
567}
568
Dan Gohman745ad442009-05-02 21:10:48 +0000569/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
570/// more than once.
571CallbackVH::~CallbackVH() {}
572
Chris Lattner9c1b5022008-12-02 07:16:45 +0000573
Chris Lattner2f7c9632001-06-06 20:29:01 +0000574//===----------------------------------------------------------------------===//
575// User Class
576//===----------------------------------------------------------------------===//
577
Chris Lattner2f7c9632001-06-06 20:29:01 +0000578// replaceUsesOfWith - Replaces all references to the "From" definition with
579// references to the "To" definition.
580//
581void User::replaceUsesOfWith(Value *From, Value *To) {
582 if (From == To) return; // Duh what?
583
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000584 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000585 "Cannot call User::replaceUsesofWith on a constant!");
586
Chris Lattnera073acb2001-07-07 08:36:50 +0000587 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
588 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589 // The side effects of this setOperand call include linking to
590 // "To", adding "this" to the uses list of To, and
591 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000592 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594}
Nate Begeman60f93202008-05-15 01:23:11 +0000595