blob: 6157dd5a75326b391d6b3e90d35ae5e1d205e5aa [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"
Daniel Dunbar4975db62009-07-25 04:41:11 +000023#include "llvm/ADT/SmallString.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000024#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/LeakDetector.h"
Chris Lattner90234f32009-03-31 22:11:05 +000027#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/ValueHandle.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000029#include "llvm/Support/raw_ostream.h"
Owen Anderson4b660a82009-06-17 17:36:57 +000030#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000031#include "llvm/System/Threading.h"
Chris Lattner90234f32009-03-31 22:11:05 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000033#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner2f7c9632001-06-06 20:29:01 +000036//===----------------------------------------------------------------------===//
37// Value Class
38//===----------------------------------------------------------------------===//
39
Chris Lattner25450e32001-12-13 00:41:27 +000040static inline const Type *checkType(const Type *Ty) {
41 assert(Ty && "Value defined with a null type: Error!");
42 return Ty;
43}
44
Chris Lattner32ab6432007-02-12 05:18:08 +000045Value::Value(const Type *ty, unsigned scid)
Dan Gohman9691e712009-07-17 17:16:59 +000046 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
47 SubclassData(0), VTy(checkType(ty)),
Gabor Greif715b9d22008-09-19 15:13:20 +000048 UseList(0), Name(0) {
Devang Patelad582fc2008-02-21 02:14:01 +000049 if (isa<CallInst>(this) || isa<InvokeInst>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000050 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
51 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000052 "invalid CallInst type!");
53 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Evan Chenga05c07e2008-07-24 00:08:56 +000054 assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000055 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000056 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000057}
58
Gordon Henriksen14a55692007-12-10 02:14:30 +000059Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000060 // Notify all ValueHandles (if present) that this value is going away.
61 if (HasValueHandle)
62 ValueHandleBase::ValueIsDeleted(this);
63
Chris Lattner2f7c9632001-06-06 20:29:01 +000064#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000065 // Check to make sure that there are no uses of this value that are still
66 // around when the value is destroyed. If there are, then we have a dangling
67 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000068 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000069 // a <badref>
70 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000071 if (!use_empty()) {
Daniel Dunbarb99eac82009-07-24 10:05:20 +000072 errs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000073 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Daniel Dunbarb99eac82009-07-24 10:05:20 +000074 errs() << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000075 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000076 }
77#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000078 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner184b2982002-09-08 18:59:35 +000079
Chris Lattneraf867a32007-03-20 00:18:10 +000080 // If this value is named, destroy the name. This should not be in a symtab
81 // at this point.
Gordon Henriksen14a55692007-12-10 02:14:30 +000082 if (Name)
83 Name->Destroy();
Chris Lattneraf867a32007-03-20 00:18:10 +000084
Chris Lattner184b2982002-09-08 18:59:35 +000085 // There should be no uses of this object anymore, remove it.
Gordon Henriksen14a55692007-12-10 02:14:30 +000086 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000087}
88
Chris Lattner4947e672005-02-01 01:24:21 +000089/// hasNUses - Return true if this Value has exactly N users.
90///
91bool Value::hasNUses(unsigned N) const {
92 use_const_iterator UI = use_begin(), E = use_end();
93
94 for (; N; --N, ++UI)
95 if (UI == E) return false; // Too few.
96 return UI == E;
97}
98
Chris Lattnerd36552f2005-02-23 16:51:11 +000099/// hasNUsesOrMore - Return true if this value has N users or more. This is
100/// logically equivalent to getNumUses() >= N.
101///
102bool Value::hasNUsesOrMore(unsigned N) const {
103 use_const_iterator UI = use_begin(), E = use_end();
104
105 for (; N; --N, ++UI)
106 if (UI == E) return false; // Too few.
107
108 return true;
109}
110
Evan Cheng89553cc2008-06-12 21:15:59 +0000111/// isUsedInBasicBlock - Return true if this value is used in the specified
112/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000113bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000114 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
115 const Instruction *User = dyn_cast<Instruction>(*I);
116 if (User && User->getParent() == BB)
117 return true;
118 }
119 return false;
120}
121
Chris Lattnerd36552f2005-02-23 16:51:11 +0000122
Chris Lattner4947e672005-02-01 01:24:21 +0000123/// getNumUses - This method computes the number of uses of this Value. This
124/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
125/// values.
126unsigned Value::getNumUses() const {
127 return (unsigned)std::distance(use_begin(), use_end());
128}
129
Chris Lattnerb6250822007-02-11 00:37:27 +0000130static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000131 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000132 if (Instruction *I = dyn_cast<Instruction>(V)) {
133 if (BasicBlock *P = I->getParent())
134 if (Function *PP = P->getParent())
135 ST = &PP->getValueSymbolTable();
136 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
137 if (Function *P = BB->getParent())
138 ST = &P->getValueSymbolTable();
139 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
140 if (Module *P = GV->getParent())
141 ST = &P->getValueSymbolTable();
142 } else if (Argument *A = dyn_cast<Argument>(V)) {
143 if (Function *P = A->getParent())
144 ST = &P->getValueSymbolTable();
Devang Patel7428d8a2009-07-22 17:43:22 +0000145 } else if (isa<MDString>(V))
146 return true;
147 else {
Chris Lattnerb6250822007-02-11 00:37:27 +0000148 assert(isa<Constant>(V) && "Unknown value type!");
149 return true; // no name is setable for this.
150 }
151 return false;
152}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000153
Chris Lattner5109a882007-08-10 15:34:35 +0000154/// getNameStart - Return a pointer to a null terminated string for this name.
155/// Note that names can have null characters within the string as well as at
156/// their end. This always returns a non-null pointer.
157const char *Value::getNameStart() const {
158 if (Name == 0) return "";
159 return Name->getKeyData();
160}
161
162/// getNameLen - Return the length of the string, correctly handling nul
163/// characters embedded into them.
164unsigned Value::getNameLen() const {
Chris Lattner2be9ec52007-09-28 20:09:40 +0000165 return Name ? Name->getKeyLength() : 0;
Chris Lattner5109a882007-08-10 15:34:35 +0000166}
167
168
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000169std::string Value::getNameStr() const {
Daniel Dunbar987ec562009-07-26 00:17:14 +0000170 return getName().str();
Chris Lattner32ab6432007-02-12 05:18:08 +0000171}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000172
Daniel Dunbard786b512009-07-26 00:34:27 +0000173void Value::setName(const Twine &NewName) {
Daniel Dunbar4975db62009-07-25 04:41:11 +0000174 SmallString<32> NameData;
Daniel Dunbard786b512009-07-26 00:34:27 +0000175 NewName.toVector(NameData);
Chris Lattner1a5de582007-02-12 18:52:59 +0000176
Daniel Dunbard786b512009-07-26 00:34:27 +0000177 const char *NameStr = NameData.data();
178 unsigned NameLen = NameData.size();
179
Chris Lattner1a5de582007-02-12 18:52:59 +0000180 if (NameLen == 0 && !hasName()) return;
Jeff Cohenb622c112007-03-05 00:00:42 +0000181 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000182
Chris Lattnerffb37782005-03-06 02:14:28 +0000183 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000184 ValueSymbolTable *ST;
185 if (getSymTab(this, ST))
186 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000187
Chris Lattner32ab6432007-02-12 05:18:08 +0000188 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000189 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000190 // Free the name for this value.
191 Name->Destroy();
192 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000193 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000194 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000195
196 if (Name) {
197 // Name isn't changing?
198 if (NameLen == Name->getKeyLength() &&
199 !memcmp(Name->getKeyData(), NameStr, NameLen))
200 return;
201 Name->Destroy();
202 }
203
204 // NOTE: Could optimize for the case the name is shrinking to not deallocate
205 // then reallocated.
206
207 // Create the new name.
208 Name = ValueName::Create(NameStr, NameStr+NameLen);
209 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000210 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000211 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000212
213 // NOTE: Could optimize for the case the name is shrinking to not deallocate
214 // then reallocated.
215 if (hasName()) {
216 // Name isn't changing?
Chris Lattner1a5de582007-02-12 18:52:59 +0000217 if (NameLen == Name->getKeyLength() &&
218 !memcmp(Name->getKeyData(), NameStr, NameLen))
Chris Lattner32ab6432007-02-12 05:18:08 +0000219 return;
220
221 // Remove old name.
222 ST->removeValueName(Name);
223 Name->Destroy();
224 Name = 0;
225
Chris Lattner1a5de582007-02-12 18:52:59 +0000226 if (NameLen == 0)
227 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000228 }
229
230 // Name is changing to something new.
Daniel Dunbarf01154c2009-07-23 18:50:53 +0000231 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000232}
233
Chris Lattner1a5de582007-02-12 18:52:59 +0000234
Chris Lattnerb6250822007-02-11 00:37:27 +0000235/// takeName - transfer the name from V to this value, setting V's name to
236/// empty. It is an error to call V->takeName(V).
237void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000238 ValueSymbolTable *ST = 0;
239 // If this value has a name, drop it.
240 if (hasName()) {
241 // Get the symtab this is in.
242 if (getSymTab(this, ST)) {
243 // We can't set a name on this value, but we need to clear V's name if
244 // it has one.
Daniel Dunbard786b512009-07-26 00:34:27 +0000245 if (V->hasName()) V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000246 return; // Cannot set a name on this value (e.g. constant).
247 }
248
249 // Remove old name.
250 if (ST)
251 ST->removeValueName(Name);
252 Name->Destroy();
253 Name = 0;
254 }
255
256 // Now we know that this has no name.
257
258 // If V has no name either, we're done.
259 if (!V->hasName()) return;
260
261 // Get this's symtab if we didn't before.
262 if (!ST) {
263 if (getSymTab(this, ST)) {
264 // Clear V's name.
Daniel Dunbard786b512009-07-26 00:34:27 +0000265 V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000266 return; // Cannot set a name on this value (e.g. constant).
267 }
268 }
269
270 // Get V's ST, this should always succed, because V has a name.
271 ValueSymbolTable *VST;
272 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000273 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000274
275 // If these values are both in the same symtab, we can do this very fast.
276 // This works even if both values have no symtab yet.
277 if (ST == VST) {
278 // Take the name!
279 Name = V->Name;
280 V->Name = 0;
281 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000282 return;
283 }
284
Chris Lattnercf835ff2007-02-15 20:01:43 +0000285 // Otherwise, things are slightly more complex. Remove V's name from VST and
286 // then reinsert it into ST.
287
288 if (VST)
289 VST->removeValueName(V->Name);
290 Name = V->Name;
291 V->Name = 0;
292 Name->setValue(this);
293
294 if (ST)
295 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000296}
297
298
Chris Lattner9f158122003-08-29 05:09:37 +0000299// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
300// except that it doesn't have all of the asserts. The asserts fail because we
301// are half-way done resolving types, which causes some types to exist as two
302// different Type*'s at the same time. This is a sledgehammer to work around
303// this problem.
304//
305void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000306 // Notify all ValueHandles (if present) that this value is going away.
307 if (HasValueHandle)
308 ValueHandleBase::ValueIsRAUWd(this, New);
309
Chris Lattner4947e672005-02-01 01:24:21 +0000310 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000311 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000312 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000313 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000314 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000315 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000316 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000317 continue;
318 }
Chris Lattner9f158122003-08-29 05:09:37 +0000319 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000320
321 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000322 }
323}
324
Chris Lattner079edeb2003-10-16 16:53:07 +0000325void Value::replaceAllUsesWith(Value *New) {
326 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
327 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
328 assert(New->getType() == getType() &&
329 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000330
Chris Lattner079edeb2003-10-16 16:53:07 +0000331 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332}
333
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000334Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000335 if (!isa<PointerType>(getType()))
336 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000337 Value *V = this;
338 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000339 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000340 if (!GEP->hasAllZeroIndices())
341 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000342 V = GEP->getPointerOperand();
343 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
344 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000345 } else {
346 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000347 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000348 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
349 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000350}
351
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000352Value *Value::getUnderlyingObject() {
353 if (!isa<PointerType>(getType()))
354 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000355 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000356 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000357 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000358 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000359 V = GEP->getPointerOperand();
360 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
361 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000362 } else {
363 return V;
364 }
365 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000366 } while (--MaxLookup);
367 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000368}
369
Chris Lattner027d7262008-12-02 18:33:11 +0000370/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000371/// return the value in the PHI node corresponding to PredBB. If not, return
372/// ourself. This is useful if you want to know the value something has in a
373/// predecessor block.
374Value *Value::DoPHITranslation(const BasicBlock *CurBB,
375 const BasicBlock *PredBB) {
376 PHINode *PN = dyn_cast<PHINode>(this);
377 if (PN && PN->getParent() == CurBB)
378 return PN->getIncomingValueForBlock(PredBB);
379 return this;
380}
381
Owen Anderson47db9412009-07-22 00:24:57 +0000382LLVMContext &Value::getContext() const { return VTy->getContext(); }
383
Chris Lattner90234f32009-03-31 22:11:05 +0000384//===----------------------------------------------------------------------===//
385// ValueHandleBase Class
386//===----------------------------------------------------------------------===//
387
388/// ValueHandles - This map keeps track of all of the value handles that are
389/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
390/// not a value has an entry in this map.
391typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
392static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000393static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000394
Dan Gohman3f025952009-05-04 17:25:21 +0000395/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
396/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000397void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
398 assert(List && "Handle list is null?");
399
400 // Splice ourselves into the list.
401 Next = *List;
402 *List = this;
403 setPrevPtr(List);
404 if (Next) {
405 Next->setPrevPtr(&Next);
406 assert(VP == Next->VP && "Added to wrong list?");
407 }
408}
409
410/// AddToUseList - Add this ValueHandle to the use list for VP.
411void ValueHandleBase::AddToUseList() {
412 assert(VP && "Null pointer doesn't have a use list!");
413 if (VP->HasValueHandle) {
414 // If this value already has a ValueHandle, then it must be in the
415 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000416 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000417 ValueHandleBase *&Entry = (*ValueHandles)[VP];
418 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000419 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000420 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000421 }
422
423 // Ok, it doesn't have any handles yet, so we must insert it into the
424 // DenseMap. However, doing this insertion could cause the DenseMap to
425 // reallocate itself, which would invalidate all of the PrevP pointers that
426 // point into the old table. Handle this by checking for reallocation and
427 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000428 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000429 ValueHandlesTy &Handles = *ValueHandles;
430 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
431
432 ValueHandleBase *&Entry = Handles[VP];
433 assert(Entry == 0 && "Value really did already have handles?");
434 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000435 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000436
437 // If reallocation didn't happen or if this was the first insertion, don't
438 // walk the table.
439 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000440 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000441 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000442 }
Chris Lattner90234f32009-03-31 22:11:05 +0000443
444 // Okay, reallocation did happen. Fix the Prev Pointers.
445 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
446 I != E; ++I) {
447 assert(I->second && I->first == I->second->VP && "List invariant broken!");
448 I->second->setPrevPtr(&I->second);
449 }
450}
451
452/// RemoveFromUseList - Remove this ValueHandle from its current use list.
453void ValueHandleBase::RemoveFromUseList() {
454 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
455
456 // Unlink this from its use list.
457 ValueHandleBase **PrevPtr = getPrevPtr();
458 assert(*PrevPtr == this && "List invariant broken");
459
460 *PrevPtr = Next;
461 if (Next) {
462 assert(Next->getPrevPtr() == &Next && "List invariant broken");
463 Next->setPrevPtr(PrevPtr);
464 return;
465 }
466
467 // If the Next pointer was null, then it is possible that this was the last
468 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
469 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000470 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000471 ValueHandlesTy &Handles = *ValueHandles;
472 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
473 Handles.erase(VP);
474 VP->HasValueHandle = false;
475 }
476}
477
478
479void ValueHandleBase::ValueIsDeleted(Value *V) {
480 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
481
482 // Get the linked list base, which is guaranteed to exist since the
483 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000484 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000485 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000486 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000487 assert(Entry && "Value bit set but no entries exist");
488
489 while (Entry) {
490 // Advance pointer to avoid invalidation.
491 ValueHandleBase *ThisNode = Entry;
492 Entry = Entry->Next;
493
494 switch (ThisNode->getKind()) {
495 case Assert:
496#ifndef NDEBUG // Only in -g mode...
Daniel Dunbarb99eac82009-07-24 10:05:20 +0000497 errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
498 << "\n";
Chris Lattner90234f32009-03-31 22:11:05 +0000499#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000500 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000501 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000502 case Weak:
503 // Weak just goes to null, which will unlink it from the list.
504 ThisNode->operator=(0);
505 break;
506 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000507 // Forward to the subclass's implementation.
508 static_cast<CallbackVH*>(ThisNode)->deleted();
509 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000510 }
511 }
512
513 // All callbacks and weak references should be dropped by now.
514 assert(!V->HasValueHandle && "All references to V were not removed?");
515}
516
517
518void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
519 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
520 assert(Old != New && "Changing value into itself!");
521
522 // Get the linked list base, which is guaranteed to exist since the
523 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000524 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000525 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000526 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000527 assert(Entry && "Value bit set but no entries exist");
528
529 while (Entry) {
530 // Advance pointer to avoid invalidation.
531 ValueHandleBase *ThisNode = Entry;
532 Entry = Entry->Next;
533
534 switch (ThisNode->getKind()) {
535 case Assert:
536 // Asserting handle does not follow RAUW implicitly.
537 break;
538 case Weak:
539 // Weak goes to the new value, which will unlink it from Old's list.
540 ThisNode->operator=(New);
541 break;
542 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000543 // Forward to the subclass's implementation.
544 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
545 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000546 }
547 }
548}
549
Dan Gohman745ad442009-05-02 21:10:48 +0000550/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
551/// more than once.
552CallbackVH::~CallbackVH() {}
553
Chris Lattner9c1b5022008-12-02 07:16:45 +0000554
Chris Lattner2f7c9632001-06-06 20:29:01 +0000555//===----------------------------------------------------------------------===//
556// User Class
557//===----------------------------------------------------------------------===//
558
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559// replaceUsesOfWith - Replaces all references to the "From" definition with
560// references to the "To" definition.
561//
562void User::replaceUsesOfWith(Value *From, Value *To) {
563 if (From == To) return; // Duh what?
564
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000565 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000566 "Cannot call User::replaceUsesofWith on a constant!");
567
Chris Lattnera073acb2001-07-07 08:36:50 +0000568 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
569 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000570 // The side effects of this setOperand call include linking to
571 // "To", adding "this" to the uses list of To, and
572 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000573 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000574 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575}