blob: fa82bac60e3b1b4716f43877587bfc08ba5ba671 [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
Daniel Dunbar32285942009-07-26 00:51:56 +0000154StringRef Value::getName() const {
155 if (!Name) return StringRef();
156 return Name->getKey();
Chris Lattner5109a882007-08-10 15:34:35 +0000157}
158
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000159std::string Value::getNameStr() const {
Daniel Dunbar987ec562009-07-26 00:17:14 +0000160 return getName().str();
Chris Lattner32ab6432007-02-12 05:18:08 +0000161}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000162
Daniel Dunbard786b512009-07-26 00:34:27 +0000163void Value::setName(const Twine &NewName) {
Daniel Dunbar4975db62009-07-25 04:41:11 +0000164 SmallString<32> NameData;
Daniel Dunbard786b512009-07-26 00:34:27 +0000165 NewName.toVector(NameData);
Chris Lattner1a5de582007-02-12 18:52:59 +0000166
Daniel Dunbard786b512009-07-26 00:34:27 +0000167 const char *NameStr = NameData.data();
168 unsigned NameLen = NameData.size();
169
Daniel Dunbara32c9992009-07-26 00:42:33 +0000170 // Name isn't changing?
171 if (getName() == StringRef(NameStr, NameLen))
172 return;
173
Jeff Cohenb622c112007-03-05 00:00:42 +0000174 assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000175
Chris Lattnerffb37782005-03-06 02:14:28 +0000176 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000177 ValueSymbolTable *ST;
178 if (getSymTab(this, ST))
179 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000180
Chris Lattner32ab6432007-02-12 05:18:08 +0000181 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000182 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000183 // Free the name for this value.
184 Name->Destroy();
185 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000186 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000187 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000188
Daniel Dunbara32c9992009-07-26 00:42:33 +0000189 if (Name)
Chris Lattner1a5de582007-02-12 18:52:59 +0000190 Name->Destroy();
Chris Lattner1a5de582007-02-12 18:52:59 +0000191
192 // NOTE: Could optimize for the case the name is shrinking to not deallocate
193 // then reallocated.
194
195 // Create the new name.
196 Name = ValueName::Create(NameStr, NameStr+NameLen);
197 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000198 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000199 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000200
201 // NOTE: Could optimize for the case the name is shrinking to not deallocate
202 // then reallocated.
203 if (hasName()) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000204 // Remove old name.
205 ST->removeValueName(Name);
206 Name->Destroy();
207 Name = 0;
208
Chris Lattner1a5de582007-02-12 18:52:59 +0000209 if (NameLen == 0)
210 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000211 }
212
213 // Name is changing to something new.
Daniel Dunbarf01154c2009-07-23 18:50:53 +0000214 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000215}
216
Chris Lattner1a5de582007-02-12 18:52:59 +0000217
Chris Lattnerb6250822007-02-11 00:37:27 +0000218/// takeName - transfer the name from V to this value, setting V's name to
219/// empty. It is an error to call V->takeName(V).
220void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000221 ValueSymbolTable *ST = 0;
222 // If this value has a name, drop it.
223 if (hasName()) {
224 // Get the symtab this is in.
225 if (getSymTab(this, ST)) {
226 // We can't set a name on this value, but we need to clear V's name if
227 // it has one.
Daniel Dunbard786b512009-07-26 00:34:27 +0000228 if (V->hasName()) V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000229 return; // Cannot set a name on this value (e.g. constant).
230 }
231
232 // Remove old name.
233 if (ST)
234 ST->removeValueName(Name);
235 Name->Destroy();
236 Name = 0;
237 }
238
239 // Now we know that this has no name.
240
241 // If V has no name either, we're done.
242 if (!V->hasName()) return;
243
244 // Get this's symtab if we didn't before.
245 if (!ST) {
246 if (getSymTab(this, ST)) {
247 // Clear V's name.
Daniel Dunbard786b512009-07-26 00:34:27 +0000248 V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000249 return; // Cannot set a name on this value (e.g. constant).
250 }
251 }
252
253 // Get V's ST, this should always succed, because V has a name.
254 ValueSymbolTable *VST;
255 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000256 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000257
258 // If these values are both in the same symtab, we can do this very fast.
259 // This works even if both values have no symtab yet.
260 if (ST == VST) {
261 // Take the name!
262 Name = V->Name;
263 V->Name = 0;
264 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000265 return;
266 }
267
Chris Lattnercf835ff2007-02-15 20:01:43 +0000268 // Otherwise, things are slightly more complex. Remove V's name from VST and
269 // then reinsert it into ST.
270
271 if (VST)
272 VST->removeValueName(V->Name);
273 Name = V->Name;
274 V->Name = 0;
275 Name->setValue(this);
276
277 if (ST)
278 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000279}
280
281
Chris Lattner9f158122003-08-29 05:09:37 +0000282// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
283// except that it doesn't have all of the asserts. The asserts fail because we
284// are half-way done resolving types, which causes some types to exist as two
285// different Type*'s at the same time. This is a sledgehammer to work around
286// this problem.
287//
288void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000289 // Notify all ValueHandles (if present) that this value is going away.
290 if (HasValueHandle)
291 ValueHandleBase::ValueIsRAUWd(this, New);
292
Chris Lattner4947e672005-02-01 01:24:21 +0000293 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000294 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000295 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000296 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000297 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000298 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000299 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000300 continue;
301 }
Chris Lattner9f158122003-08-29 05:09:37 +0000302 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000303
304 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000305 }
306}
307
Chris Lattner079edeb2003-10-16 16:53:07 +0000308void Value::replaceAllUsesWith(Value *New) {
309 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
310 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
311 assert(New->getType() == getType() &&
312 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000313
Chris Lattner079edeb2003-10-16 16:53:07 +0000314 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315}
316
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000317Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000318 if (!isa<PointerType>(getType()))
319 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000320 Value *V = this;
321 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000322 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000323 if (!GEP->hasAllZeroIndices())
324 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000325 V = GEP->getPointerOperand();
326 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
327 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000328 } else {
329 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000330 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000331 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
332 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000333}
334
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000335Value *Value::getUnderlyingObject() {
336 if (!isa<PointerType>(getType()))
337 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000338 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000339 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000340 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000341 if (GEPOperator *GEP = dyn_cast<GEPOperator>(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;
347 }
348 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000349 } while (--MaxLookup);
350 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000351}
352
Chris Lattner027d7262008-12-02 18:33:11 +0000353/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000354/// return the value in the PHI node corresponding to PredBB. If not, return
355/// ourself. This is useful if you want to know the value something has in a
356/// predecessor block.
357Value *Value::DoPHITranslation(const BasicBlock *CurBB,
358 const BasicBlock *PredBB) {
359 PHINode *PN = dyn_cast<PHINode>(this);
360 if (PN && PN->getParent() == CurBB)
361 return PN->getIncomingValueForBlock(PredBB);
362 return this;
363}
364
Owen Anderson47db9412009-07-22 00:24:57 +0000365LLVMContext &Value::getContext() const { return VTy->getContext(); }
366
Chris Lattner90234f32009-03-31 22:11:05 +0000367//===----------------------------------------------------------------------===//
368// ValueHandleBase Class
369//===----------------------------------------------------------------------===//
370
371/// ValueHandles - This map keeps track of all of the value handles that are
372/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
373/// not a value has an entry in this map.
374typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
375static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000376static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000377
Dan Gohman3f025952009-05-04 17:25:21 +0000378/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
379/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000380void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
381 assert(List && "Handle list is null?");
382
383 // Splice ourselves into the list.
384 Next = *List;
385 *List = this;
386 setPrevPtr(List);
387 if (Next) {
388 Next->setPrevPtr(&Next);
389 assert(VP == Next->VP && "Added to wrong list?");
390 }
391}
392
393/// AddToUseList - Add this ValueHandle to the use list for VP.
394void ValueHandleBase::AddToUseList() {
395 assert(VP && "Null pointer doesn't have a use list!");
396 if (VP->HasValueHandle) {
397 // If this value already has a ValueHandle, then it must be in the
398 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000399 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000400 ValueHandleBase *&Entry = (*ValueHandles)[VP];
401 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000402 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000403 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000404 }
405
406 // Ok, it doesn't have any handles yet, so we must insert it into the
407 // DenseMap. However, doing this insertion could cause the DenseMap to
408 // reallocate itself, which would invalidate all of the PrevP pointers that
409 // point into the old table. Handle this by checking for reallocation and
410 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000411 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000412 ValueHandlesTy &Handles = *ValueHandles;
413 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
414
415 ValueHandleBase *&Entry = Handles[VP];
416 assert(Entry == 0 && "Value really did already have handles?");
417 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000418 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000419
420 // If reallocation didn't happen or if this was the first insertion, don't
421 // walk the table.
422 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000423 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000424 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000425 }
Chris Lattner90234f32009-03-31 22:11:05 +0000426
427 // Okay, reallocation did happen. Fix the Prev Pointers.
428 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
429 I != E; ++I) {
430 assert(I->second && I->first == I->second->VP && "List invariant broken!");
431 I->second->setPrevPtr(&I->second);
432 }
433}
434
435/// RemoveFromUseList - Remove this ValueHandle from its current use list.
436void ValueHandleBase::RemoveFromUseList() {
437 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
438
439 // Unlink this from its use list.
440 ValueHandleBase **PrevPtr = getPrevPtr();
441 assert(*PrevPtr == this && "List invariant broken");
442
443 *PrevPtr = Next;
444 if (Next) {
445 assert(Next->getPrevPtr() == &Next && "List invariant broken");
446 Next->setPrevPtr(PrevPtr);
447 return;
448 }
449
450 // If the Next pointer was null, then it is possible that this was the last
451 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
452 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000453 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000454 ValueHandlesTy &Handles = *ValueHandles;
455 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
456 Handles.erase(VP);
457 VP->HasValueHandle = false;
458 }
459}
460
461
462void ValueHandleBase::ValueIsDeleted(Value *V) {
463 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
464
465 // Get the linked list base, which is guaranteed to exist since the
466 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000467 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000468 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000469 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000470 assert(Entry && "Value bit set but no entries exist");
471
472 while (Entry) {
473 // Advance pointer to avoid invalidation.
474 ValueHandleBase *ThisNode = Entry;
475 Entry = Entry->Next;
476
477 switch (ThisNode->getKind()) {
478 case Assert:
479#ifndef NDEBUG // Only in -g mode...
Daniel Dunbarb99eac82009-07-24 10:05:20 +0000480 errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
481 << "\n";
Chris Lattner90234f32009-03-31 22:11:05 +0000482#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000483 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000484 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000485 case Weak:
486 // Weak just goes to null, which will unlink it from the list.
487 ThisNode->operator=(0);
488 break;
489 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000490 // Forward to the subclass's implementation.
491 static_cast<CallbackVH*>(ThisNode)->deleted();
492 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000493 }
494 }
495
496 // All callbacks and weak references should be dropped by now.
497 assert(!V->HasValueHandle && "All references to V were not removed?");
498}
499
500
501void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
502 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
503 assert(Old != New && "Changing value into itself!");
504
505 // Get the linked list base, which is guaranteed to exist since the
506 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000507 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000508 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000509 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000510 assert(Entry && "Value bit set but no entries exist");
511
512 while (Entry) {
513 // Advance pointer to avoid invalidation.
514 ValueHandleBase *ThisNode = Entry;
515 Entry = Entry->Next;
516
517 switch (ThisNode->getKind()) {
518 case Assert:
519 // Asserting handle does not follow RAUW implicitly.
520 break;
521 case Weak:
522 // Weak goes to the new value, which will unlink it from Old's list.
523 ThisNode->operator=(New);
524 break;
525 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000526 // Forward to the subclass's implementation.
527 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
528 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000529 }
530 }
531}
532
Dan Gohman745ad442009-05-02 21:10:48 +0000533/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
534/// more than once.
535CallbackVH::~CallbackVH() {}
536
Chris Lattner9c1b5022008-12-02 07:16:45 +0000537
Chris Lattner2f7c9632001-06-06 20:29:01 +0000538//===----------------------------------------------------------------------===//
539// User Class
540//===----------------------------------------------------------------------===//
541
Chris Lattner2f7c9632001-06-06 20:29:01 +0000542// replaceUsesOfWith - Replaces all references to the "From" definition with
543// references to the "To" definition.
544//
545void User::replaceUsesOfWith(Value *From, Value *To) {
546 if (From == To) return; // Duh what?
547
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000548 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000549 "Cannot call User::replaceUsesofWith on a constant!");
550
Chris Lattnera073acb2001-07-07 08:36:50 +0000551 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
552 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553 // The side effects of this setOperand call include linking to
554 // "To", adding "this" to the uses list of To, and
555 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000556 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000557 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000558}