blob: 8c5d551c15ea333e63c424421df9a73f99d5fceb [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"
Owen Anderson4b660a82009-06-17 17:36:57 +000026#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000027#include "llvm/System/Threading.h"
Chris Lattner90234f32009-03-31 22:11:05 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000029#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
33// Value Class
34//===----------------------------------------------------------------------===//
35
Chris Lattner25450e32001-12-13 00:41:27 +000036static inline const Type *checkType(const Type *Ty) {
37 assert(Ty && "Value defined with a null type: Error!");
38 return Ty;
39}
40
Chris Lattner32ab6432007-02-12 05:18:08 +000041Value::Value(const Type *ty, unsigned scid)
Chris Lattner90234f32009-03-31 22:11:05 +000042 : SubclassID(scid), HasValueHandle(0), SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000043 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000044 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000045 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
46 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000047 "invalid CallInst type!");
48 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000049 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000050 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000051 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000052}
53
Gordon Henriksen14a55692007-12-10 02:14:30 +000054Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000055 // Notify all ValueHandles (if present) that this value is going away.
56 if (HasValueHandle)
57 ValueHandleBase::ValueIsDeleted(this);
58
Chris Lattner2f7c9632001-06-06 20:29:01 +000059#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000060 // Check to make sure that there are no uses of this value that are still
61 // around when the value is destroyed. If there are, then we have a dangling
62 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000063 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000064 // a <badref>
65 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000066 if (!use_empty()) {
Chris Lattner695bc772008-12-13 18:37:58 +000067 cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000068 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Chris Lattner695bc772008-12-13 18:37:58 +000069 cerr << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000070 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000071 }
72#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000073 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000074
Chris Lattneraf867a32007-03-20 00:18:10 +000075 // If this value is named, destroy the name. This should not be in a symtab
76 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000077 if (Name)
78 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000079
Chris Lattner184b2982002-09-08 18:59:35 +000080 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000081 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000082}
83
Chris Lattner4947e672005-02-01 01:24:21 +000084/// hasNUses - Return true if this Value has exactly N users.
85///
86bool Value::hasNUses(unsigned N) const {
87 use_const_iterator UI = use_begin(), E = use_end();
88
89 for (; N; --N, ++UI)
90 if (UI == E) return false; // Too few.
91 return UI == E;
92}
93
Chris Lattnerd36552f2005-02-23 16:51:11 +000094/// hasNUsesOrMore - Return true if this value has N users or more. This is
95/// logically equivalent to getNumUses() >= N.
96///
97bool Value::hasNUsesOrMore(unsigned N) const {
98 use_const_iterator UI = use_begin(), E = use_end();
99
100 for (; N; --N, ++UI)
101 if (UI == E) return false; // Too few.
102
103 return true;
104}
105
Evan Cheng89553cc2008-06-12 21:15:59 +0000106/// isUsedInBasicBlock - Return true if this value is used in the specified
107/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000108bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000109 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
110 const Instruction *User = dyn_cast<Instruction>(*I);
111 if (User && User->getParent() == BB)
112 return true;
113 }
114 return false;
115}
116
Chris Lattnerd36552f2005-02-23 16:51:11 +0000117
Chris Lattner4947e672005-02-01 01:24:21 +0000118/// getNumUses - This method computes the number of uses of this Value. This
119/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
120/// values.
121unsigned Value::getNumUses() const {
122 return (unsigned)std::distance(use_begin(), use_end());
123}
124
Chris Lattnerb6250822007-02-11 00:37:27 +0000125static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000126 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000127 if (Instruction *I = dyn_cast<Instruction>(V)) {
128 if (BasicBlock *P = I->getParent())
129 if (Function *PP = P->getParent())
130 ST = &PP->getValueSymbolTable();
131 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
132 if (Function *P = BB->getParent())
133 ST = &P->getValueSymbolTable();
134 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
135 if (Module *P = GV->getParent())
136 ST = &P->getValueSymbolTable();
137 } else if (Argument *A = dyn_cast<Argument>(V)) {
138 if (Function *P = A->getParent())
139 ST = &P->getValueSymbolTable();
140 } else {
141 assert(isa<Constant>(V) && "Unknown value type!");
142 return true; // no name is setable for this.
143 }
144 return false;
145}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000146
Chris Lattner5109a882007-08-10 15:34:35 +0000147/// getNameStart - Return a pointer to a null terminated string for this name.
148/// Note that names can have null characters within the string as well as at
149/// their end. This always returns a non-null pointer.
150const char *Value::getNameStart() const {
151 if (Name == 0) return "";
152 return Name->getKeyData();
153}
154
155/// getNameLen - Return the length of the string, correctly handling nul
156/// characters embedded into them.
157unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000158 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000159}
160
Chris Lattnera1d850e2008-04-30 03:55:40 +0000161/// isName - Return true if this value has the name specified by the provided
162/// nul terminated string.
163bool Value::isName(const char *N) const {
164 unsigned InLen = strlen(N);
Chris Lattner78769f02008-04-30 15:27:09 +0000165 return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
Chris Lattnera1d850e2008-04-30 03:55:40 +0000166}
167
Chris Lattner5109a882007-08-10 15:34:35 +0000168
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000169std::string Value::getNameStr() const {
Chris Lattner32ab6432007-02-12 05:18:08 +0000170 if (Name == 0) return "";
171 return std::string(Name->getKeyData(),
172 Name->getKeyData()+Name->getKeyLength());
173}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000174
Chris Lattner32ab6432007-02-12 05:18:08 +0000175void Value::setName(const std::string &name) {
Chris Lattner1a5de582007-02-12 18:52:59 +0000176 setName(&name[0], name.size());
177}
178
Chris Lattnercb9a6262007-02-13 07:53:34 +0000179void Value::setName(const char *Name) {
180 setName(Name, Name ? strlen(Name) : 0);
181}
182
Chris Lattner1a5de582007-02-12 18:52:59 +0000183void Value::setName(const char *NameStr, unsigned NameLen) {
184 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000185 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000186
Chris Lattnerffb37782005-03-06 02:14:28 +0000187 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000188 ValueSymbolTable *ST;
189 if (getSymTab(this, ST))
190 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000191
Chris Lattner32ab6432007-02-12 05:18:08 +0000192 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000193 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000194 // Free the name for this value.
195 Name->Destroy();
196 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000197 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000198 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000199
200 if (Name) {
201 // Name isn't changing?
202 if (NameLen == Name->getKeyLength() &&
203 !memcmp(Name->getKeyData(), NameStr, NameLen))
204 return;
205 Name->Destroy();
206 }
207
208 // NOTE: Could optimize for the case the name is shrinking to not deallocate
209 // then reallocated.
210
211 // Create the new name.
212 Name = ValueName::Create(NameStr, NameStr+NameLen);
213 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000214 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000215 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000216
217 // NOTE: Could optimize for the case the name is shrinking to not deallocate
218 // then reallocated.
219 if (hasName()) {
220 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000221 if (NameLen == Name->getKeyLength() &&
222 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000223 return;
224
225 // Remove old name.
226 ST->removeValueName(Name);
227 Name->Destroy();
228 Name = 0;
229
Chris Lattner1a5de582007-02-12 18:52:59 +0000230 if (NameLen == 0)
231 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000232 }
233
234 // Name is changing to something new.
Chris Lattner1a5de582007-02-12 18:52:59 +0000235 Name = ST->createValueName(NameStr, NameLen, this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000236}
237
Chris Lattner1a5de582007-02-12 18:52:59 +0000238
Chris Lattnerb6250822007-02-11 00:37:27 +0000239/// takeName - transfer the name from V to this value, setting V's name to
240/// empty. It is an error to call V->takeName(V).
241void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000242 ValueSymbolTable *ST = 0;
243 // If this value has a name, drop it.
244 if (hasName()) {
245 // Get the symtab this is in.
246 if (getSymTab(this, ST)) {
247 // We can't set a name on this value, but we need to clear V's name if
248 // it has one.
249 if (V->hasName()) V->setName(0, 0);
250 return; // Cannot set a name on this value (e.g. constant).
251 }
252
253 // Remove old name.
254 if (ST)
255 ST->removeValueName(Name);
256 Name->Destroy();
257 Name = 0;
258 }
259
260 // Now we know that this has no name.
261
262 // If V has no name either, we're done.
263 if (!V->hasName()) return;
264
265 // Get this's symtab if we didn't before.
266 if (!ST) {
267 if (getSymTab(this, ST)) {
268 // Clear V's name.
269 V->setName(0, 0);
270 return; // Cannot set a name on this value (e.g. constant).
271 }
272 }
273
274 // Get V's ST, this should always succed, because V has a name.
275 ValueSymbolTable *VST;
276 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000277 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000278
279 // If these values are both in the same symtab, we can do this very fast.
280 // This works even if both values have no symtab yet.
281 if (ST == VST) {
282 // Take the name!
283 Name = V->Name;
284 V->Name = 0;
285 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000286 return;
287 }
288
Chris Lattnercf835ff2007-02-15 20:01:43 +0000289 // Otherwise, things are slightly more complex. Remove V's name from VST and
290 // then reinsert it into ST.
291
292 if (VST)
293 VST->removeValueName(V->Name);
294 Name = V->Name;
295 V->Name = 0;
296 Name->setValue(this);
297
298 if (ST)
299 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000300}
301
302
Chris Lattner9f158122003-08-29 05:09:37 +0000303// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
304// except that it doesn't have all of the asserts. The asserts fail because we
305// are half-way done resolving types, which causes some types to exist as two
306// different Type*'s at the same time. This is a sledgehammer to work around
307// this problem.
308//
309void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000310 // Notify all ValueHandles (if present) that this value is going away.
311 if (HasValueHandle)
312 ValueHandleBase::ValueIsRAUWd(this, New);
313
Chris Lattner4947e672005-02-01 01:24:21 +0000314 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000315 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000316 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000317 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000318 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000319 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000320 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000321 continue;
322 }
Chris Lattner9f158122003-08-29 05:09:37 +0000323 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000324
325 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000326 }
327}
328
Chris Lattner079edeb2003-10-16 16:53:07 +0000329void Value::replaceAllUsesWith(Value *New) {
330 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
331 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
332 assert(New->getType() == getType() &&
333 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000334
Chris Lattner079edeb2003-10-16 16:53:07 +0000335 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000336}
337
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000338Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000339 if (!isa<PointerType>(getType()))
340 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000341 Value *V = this;
342 do {
343 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
344 if (CE->getOpcode() == Instruction::GetElementPtr) {
345 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
346 if (!CE->getOperand(i)->isNullValue())
347 return V;
348 V = CE->getOperand(0);
349 } else if (CE->getOpcode() == Instruction::BitCast) {
350 V = CE->getOperand(0);
351 } else {
352 return V;
353 }
354 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
355 if (!GEP->hasAllZeroIndices())
356 return V;
357 V = GEP->getOperand(0);
358 } else if (BitCastInst *CI = dyn_cast<BitCastInst>(V)) {
359 V = CI->getOperand(0);
360 } else {
361 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000362 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000363 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
364 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000365}
366
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000367Value *Value::getUnderlyingObject() {
368 if (!isa<PointerType>(getType()))
369 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000370 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000371 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000372 do {
373 if (Instruction *I = dyn_cast<Instruction>(V)) {
374 if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I))
375 return V;
376 V = I->getOperand(0);
377 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
378 if (CE->getOpcode() != Instruction::BitCast &&
379 CE->getOpcode() != Instruction::GetElementPtr)
380 return V;
381 V = CE->getOperand(0);
382 } else {
383 return V;
384 }
385 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000386 } while (--MaxLookup);
387 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000388}
389
Chris Lattner027d7262008-12-02 18:33:11 +0000390/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000391/// return the value in the PHI node corresponding to PredBB. If not, return
392/// ourself. This is useful if you want to know the value something has in a
393/// predecessor block.
394Value *Value::DoPHITranslation(const BasicBlock *CurBB,
395 const BasicBlock *PredBB) {
396 PHINode *PN = dyn_cast<PHINode>(this);
397 if (PN && PN->getParent() == CurBB)
398 return PN->getIncomingValueForBlock(PredBB);
399 return this;
400}
401
Chris Lattner90234f32009-03-31 22:11:05 +0000402//===----------------------------------------------------------------------===//
403// ValueHandleBase Class
404//===----------------------------------------------------------------------===//
405
406/// ValueHandles - This map keeps track of all of the value handles that are
407/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
408/// not a value has an entry in this map.
409typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
410static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000411static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000412
Dan Gohman3f025952009-05-04 17:25:21 +0000413/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
414/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000415void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
416 assert(List && "Handle list is null?");
417
418 // Splice ourselves into the list.
419 Next = *List;
420 *List = this;
421 setPrevPtr(List);
422 if (Next) {
423 Next->setPrevPtr(&Next);
424 assert(VP == Next->VP && "Added to wrong list?");
425 }
426}
427
428/// AddToUseList - Add this ValueHandle to the use list for VP.
429void ValueHandleBase::AddToUseList() {
430 assert(VP && "Null pointer doesn't have a use list!");
431 if (VP->HasValueHandle) {
432 // If this value already has a ValueHandle, then it must be in the
433 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000434 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000435 ValueHandleBase *&Entry = (*ValueHandles)[VP];
436 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000437 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000438 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000439 }
440
441 // Ok, it doesn't have any handles yet, so we must insert it into the
442 // DenseMap. However, doing this insertion could cause the DenseMap to
443 // reallocate itself, which would invalidate all of the PrevP pointers that
444 // point into the old table. Handle this by checking for reallocation and
445 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000446 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000447 ValueHandlesTy &Handles = *ValueHandles;
448 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
449
450 ValueHandleBase *&Entry = Handles[VP];
451 assert(Entry == 0 && "Value really did already have handles?");
452 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000453 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000454
455 // If reallocation didn't happen or if this was the first insertion, don't
456 // walk the table.
457 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000458 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000459 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000460 }
Chris Lattner90234f32009-03-31 22:11:05 +0000461
462 // Okay, reallocation did happen. Fix the Prev Pointers.
463 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
464 I != E; ++I) {
465 assert(I->second && I->first == I->second->VP && "List invariant broken!");
466 I->second->setPrevPtr(&I->second);
467 }
468}
469
470/// RemoveFromUseList - Remove this ValueHandle from its current use list.
471void ValueHandleBase::RemoveFromUseList() {
472 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
473
474 // Unlink this from its use list.
475 ValueHandleBase **PrevPtr = getPrevPtr();
476 assert(*PrevPtr == this && "List invariant broken");
477
478 *PrevPtr = Next;
479 if (Next) {
480 assert(Next->getPrevPtr() == &Next && "List invariant broken");
481 Next->setPrevPtr(PrevPtr);
482 return;
483 }
484
485 // If the Next pointer was null, then it is possible that this was the last
486 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
487 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000488 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000489 ValueHandlesTy &Handles = *ValueHandles;
490 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
491 Handles.erase(VP);
492 VP->HasValueHandle = false;
493 }
494}
495
496
497void ValueHandleBase::ValueIsDeleted(Value *V) {
498 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
499
500 // Get the linked list base, which is guaranteed to exist since the
501 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000502 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000503 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000504 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000505 assert(Entry && "Value bit set but no entries exist");
506
507 while (Entry) {
508 // Advance pointer to avoid invalidation.
509 ValueHandleBase *ThisNode = Entry;
510 Entry = Entry->Next;
511
512 switch (ThisNode->getKind()) {
513 case Assert:
514#ifndef NDEBUG // Only in -g mode...
515 cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr()
516 << "\n";
517#endif
Torok Edwin6dd27302009-07-08 18:01:40 +0000518 llvm_report_error("An asserting value handle still pointed to this"
519 "value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000520 case Weak:
521 // Weak just goes to null, which will unlink it from the list.
522 ThisNode->operator=(0);
523 break;
524 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000525 // Forward to the subclass's implementation.
526 static_cast<CallbackVH*>(ThisNode)->deleted();
527 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000528 }
529 }
530
531 // All callbacks and weak references should be dropped by now.
532 assert(!V->HasValueHandle && "All references to V were not removed?");
533}
534
535
536void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
537 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
538 assert(Old != New && "Changing value into itself!");
539
540 // Get the linked list base, which is guaranteed to exist since the
541 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000542 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000543 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000544 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000545 assert(Entry && "Value bit set but no entries exist");
546
547 while (Entry) {
548 // Advance pointer to avoid invalidation.
549 ValueHandleBase *ThisNode = Entry;
550 Entry = Entry->Next;
551
552 switch (ThisNode->getKind()) {
553 case Assert:
554 // Asserting handle does not follow RAUW implicitly.
555 break;
556 case Weak:
557 // Weak goes to the new value, which will unlink it from Old's list.
558 ThisNode->operator=(New);
559 break;
560 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000561 // Forward to the subclass's implementation.
562 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
563 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000564 }
565 }
566}
567
Dan Gohman745ad442009-05-02 21:10:48 +0000568/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
569/// more than once.
570CallbackVH::~CallbackVH() {}
571
Chris Lattner9c1b5022008-12-02 07:16:45 +0000572
Chris Lattner2f7c9632001-06-06 20:29:01 +0000573//===----------------------------------------------------------------------===//
574// User Class
575//===----------------------------------------------------------------------===//
576
Chris Lattner2f7c9632001-06-06 20:29:01 +0000577// replaceUsesOfWith - Replaces all references to the "From" definition with
578// references to the "To" definition.
579//
580void User::replaceUsesOfWith(Value *From, Value *To) {
581 if (From == To) return; // Duh what?
582
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000583 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000584 "Cannot call User::replaceUsesofWith on a constant!");
585
Chris Lattnera073acb2001-07-07 08:36:50 +0000586 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
587 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588 // The side effects of this setOperand call include linking to
589 // "To", adding "this" to the uses list of To, and
590 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000591 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000592 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593}
Nate Begeman60f93202008-05-15 01:23:11 +0000594