blob: 8710b9461e89f43755ae5a80a4aa1c0fbf3a8d80 [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 Patela4f43fb2009-07-28 21:49:47 +000021#include "llvm/Metadata.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 Lattner59c08e72009-08-04 04:31:02 +000060 // If this value is named, destroy the name. This should not be in a symtab
61 // at this point.
62 if (Name)
63 Name->Destroy();
64
65 // There should be no uses of this object anymore, remove it.
66 LeakDetector::removeGarbageObject(this);
67
Chris Lattner90234f32009-03-31 22:11:05 +000068 // Notify all ValueHandles (if present) that this value is going away.
69 if (HasValueHandle)
70 ValueHandleBase::ValueIsDeleted(this);
71
Chris Lattner2f7c9632001-06-06 20:29:01 +000072#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000073 // Check to make sure that there are no uses of this value that are still
74 // around when the value is destroyed. If there are, then we have a dangling
75 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000076 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000077 // a <badref>
78 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000079 if (!use_empty()) {
Daniel Dunbarb99eac82009-07-24 10:05:20 +000080 errs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000081 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Daniel Dunbarb99eac82009-07-24 10:05:20 +000082 errs() << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000083 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 }
85#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000086 assert(use_empty() && "Uses remain when a value is destroyed!");
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 Patel18dfdc92009-07-29 17:16:17 +0000145 } else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
146 if (Module *P = N->getParent()) {
147 ST = &P->getValueSymbolTable();
148 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000149 } else if (isa<MDString>(V))
150 return true;
151 else {
Chris Lattnerb6250822007-02-11 00:37:27 +0000152 assert(isa<Constant>(V) && "Unknown value type!");
153 return true; // no name is setable for this.
154 }
155 return false;
156}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000157
Daniel Dunbar32285942009-07-26 00:51:56 +0000158StringRef Value::getName() const {
Daniel Dunbar7cc8f7e2009-07-26 09:22:02 +0000159 // Make sure the empty string is still a C string. For historical reasons,
160 // some clients want to call .data() on the result and expect it to be null
161 // terminated.
162 if (!Name) return StringRef("", 0);
Daniel Dunbar32285942009-07-26 00:51:56 +0000163 return Name->getKey();
Chris Lattner5109a882007-08-10 15:34:35 +0000164}
165
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000166std::string Value::getNameStr() const {
Daniel Dunbar987ec562009-07-26 00:17:14 +0000167 return getName().str();
Chris Lattner32ab6432007-02-12 05:18:08 +0000168}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000169
Daniel Dunbard786b512009-07-26 00:34:27 +0000170void Value::setName(const Twine &NewName) {
Daniel Dunbar4975db62009-07-25 04:41:11 +0000171 SmallString<32> NameData;
Daniel Dunbard786b512009-07-26 00:34:27 +0000172 NewName.toVector(NameData);
Chris Lattner1a5de582007-02-12 18:52:59 +0000173
Daniel Dunbard786b512009-07-26 00:34:27 +0000174 const char *NameStr = NameData.data();
175 unsigned NameLen = NameData.size();
176
Daniel Dunbara32c9992009-07-26 00:42:33 +0000177 // Name isn't changing?
178 if (getName() == StringRef(NameStr, NameLen))
179 return;
180
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
Daniel Dunbara32c9992009-07-26 00:42:33 +0000196 if (Name)
Chris Lattner1a5de582007-02-12 18:52:59 +0000197 Name->Destroy();
Chris Lattner1a5de582007-02-12 18:52:59 +0000198
199 // NOTE: Could optimize for the case the name is shrinking to not deallocate
200 // then reallocated.
201
202 // Create the new name.
203 Name = ValueName::Create(NameStr, NameStr+NameLen);
204 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000205 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000206 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000207
208 // NOTE: Could optimize for the case the name is shrinking to not deallocate
209 // then reallocated.
210 if (hasName()) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000211 // Remove old name.
212 ST->removeValueName(Name);
213 Name->Destroy();
214 Name = 0;
215
Chris Lattner1a5de582007-02-12 18:52:59 +0000216 if (NameLen == 0)
217 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000218 }
219
220 // Name is changing to something new.
Daniel Dunbarf01154c2009-07-23 18:50:53 +0000221 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000222}
223
Chris Lattner1a5de582007-02-12 18:52:59 +0000224
Chris Lattnerb6250822007-02-11 00:37:27 +0000225/// takeName - transfer the name from V to this value, setting V's name to
226/// empty. It is an error to call V->takeName(V).
227void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000228 ValueSymbolTable *ST = 0;
229 // If this value has a name, drop it.
230 if (hasName()) {
231 // Get the symtab this is in.
232 if (getSymTab(this, ST)) {
233 // We can't set a name on this value, but we need to clear V's name if
234 // it has one.
Daniel Dunbard786b512009-07-26 00:34:27 +0000235 if (V->hasName()) V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000236 return; // Cannot set a name on this value (e.g. constant).
237 }
238
239 // Remove old name.
240 if (ST)
241 ST->removeValueName(Name);
242 Name->Destroy();
243 Name = 0;
244 }
245
246 // Now we know that this has no name.
247
248 // If V has no name either, we're done.
249 if (!V->hasName()) return;
250
251 // Get this's symtab if we didn't before.
252 if (!ST) {
253 if (getSymTab(this, ST)) {
254 // Clear V's name.
Daniel Dunbard786b512009-07-26 00:34:27 +0000255 V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000256 return; // Cannot set a name on this value (e.g. constant).
257 }
258 }
259
260 // Get V's ST, this should always succed, because V has a name.
261 ValueSymbolTable *VST;
262 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000263 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000264
265 // If these values are both in the same symtab, we can do this very fast.
266 // This works even if both values have no symtab yet.
267 if (ST == VST) {
268 // Take the name!
269 Name = V->Name;
270 V->Name = 0;
271 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000272 return;
273 }
274
Chris Lattnercf835ff2007-02-15 20:01:43 +0000275 // Otherwise, things are slightly more complex. Remove V's name from VST and
276 // then reinsert it into ST.
277
278 if (VST)
279 VST->removeValueName(V->Name);
280 Name = V->Name;
281 V->Name = 0;
282 Name->setValue(this);
283
284 if (ST)
285 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000286}
287
288
Chris Lattner9f158122003-08-29 05:09:37 +0000289// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
290// except that it doesn't have all of the asserts. The asserts fail because we
291// are half-way done resolving types, which causes some types to exist as two
292// different Type*'s at the same time. This is a sledgehammer to work around
293// this problem.
294//
295void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000296 // Notify all ValueHandles (if present) that this value is going away.
297 if (HasValueHandle)
298 ValueHandleBase::ValueIsRAUWd(this, New);
299
Chris Lattner4947e672005-02-01 01:24:21 +0000300 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000301 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000302 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000303 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000304 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000305 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000306 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000307 continue;
308 }
Chris Lattner9f158122003-08-29 05:09:37 +0000309 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000310
311 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000312 }
313}
314
Chris Lattner079edeb2003-10-16 16:53:07 +0000315void Value::replaceAllUsesWith(Value *New) {
316 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
317 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
318 assert(New->getType() == getType() &&
319 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000320
Chris Lattner079edeb2003-10-16 16:53:07 +0000321 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000322}
323
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000324Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000325 if (!isa<PointerType>(getType()))
326 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000327 Value *V = this;
328 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000329 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000330 if (!GEP->hasAllZeroIndices())
331 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000332 V = GEP->getPointerOperand();
333 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
334 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000335 } else {
336 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000337 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000338 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
339 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000340}
341
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000342Value *Value::getUnderlyingObject() {
343 if (!isa<PointerType>(getType()))
344 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000345 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000346 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000347 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000348 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000349 V = GEP->getPointerOperand();
350 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
351 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000352 } else {
353 return V;
354 }
355 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000356 } while (--MaxLookup);
357 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000358}
359
Chris Lattner027d7262008-12-02 18:33:11 +0000360/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000361/// return the value in the PHI node corresponding to PredBB. If not, return
362/// ourself. This is useful if you want to know the value something has in a
363/// predecessor block.
364Value *Value::DoPHITranslation(const BasicBlock *CurBB,
365 const BasicBlock *PredBB) {
366 PHINode *PN = dyn_cast<PHINode>(this);
367 if (PN && PN->getParent() == CurBB)
368 return PN->getIncomingValueForBlock(PredBB);
369 return this;
370}
371
Owen Anderson47db9412009-07-22 00:24:57 +0000372LLVMContext &Value::getContext() const { return VTy->getContext(); }
373
Chris Lattner90234f32009-03-31 22:11:05 +0000374//===----------------------------------------------------------------------===//
375// ValueHandleBase Class
376//===----------------------------------------------------------------------===//
377
378/// ValueHandles - This map keeps track of all of the value handles that are
379/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
380/// not a value has an entry in this map.
381typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
382static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000383static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000384
Dan Gohman3f025952009-05-04 17:25:21 +0000385/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
386/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000387void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
388 assert(List && "Handle list is null?");
389
390 // Splice ourselves into the list.
391 Next = *List;
392 *List = this;
393 setPrevPtr(List);
394 if (Next) {
395 Next->setPrevPtr(&Next);
396 assert(VP == Next->VP && "Added to wrong list?");
397 }
398}
399
400/// AddToUseList - Add this ValueHandle to the use list for VP.
401void ValueHandleBase::AddToUseList() {
402 assert(VP && "Null pointer doesn't have a use list!");
403 if (VP->HasValueHandle) {
404 // If this value already has a ValueHandle, then it must be in the
405 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000406 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000407 ValueHandleBase *&Entry = (*ValueHandles)[VP];
408 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000409 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000410 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000411 }
412
413 // Ok, it doesn't have any handles yet, so we must insert it into the
414 // DenseMap. However, doing this insertion could cause the DenseMap to
415 // reallocate itself, which would invalidate all of the PrevP pointers that
416 // point into the old table. Handle this by checking for reallocation and
417 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000418 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000419 ValueHandlesTy &Handles = *ValueHandles;
420 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
421
422 ValueHandleBase *&Entry = Handles[VP];
423 assert(Entry == 0 && "Value really did already have handles?");
424 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000425 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000426
427 // If reallocation didn't happen or if this was the first insertion, don't
428 // walk the table.
429 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000430 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000431 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000432 }
Chris Lattner90234f32009-03-31 22:11:05 +0000433
434 // Okay, reallocation did happen. Fix the Prev Pointers.
435 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
436 I != E; ++I) {
437 assert(I->second && I->first == I->second->VP && "List invariant broken!");
438 I->second->setPrevPtr(&I->second);
439 }
440}
441
442/// RemoveFromUseList - Remove this ValueHandle from its current use list.
443void ValueHandleBase::RemoveFromUseList() {
444 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
445
446 // Unlink this from its use list.
447 ValueHandleBase **PrevPtr = getPrevPtr();
448 assert(*PrevPtr == this && "List invariant broken");
449
450 *PrevPtr = Next;
451 if (Next) {
452 assert(Next->getPrevPtr() == &Next && "List invariant broken");
453 Next->setPrevPtr(PrevPtr);
454 return;
455 }
456
457 // If the Next pointer was null, then it is possible that this was the last
458 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
459 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000460 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000461 ValueHandlesTy &Handles = *ValueHandles;
462 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
463 Handles.erase(VP);
464 VP->HasValueHandle = false;
465 }
466}
467
468
469void ValueHandleBase::ValueIsDeleted(Value *V) {
470 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
471
472 // Get the linked list base, which is guaranteed to exist since the
473 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000474 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000475 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000476 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000477 assert(Entry && "Value bit set but no entries exist");
478
479 while (Entry) {
480 // Advance pointer to avoid invalidation.
481 ValueHandleBase *ThisNode = Entry;
482 Entry = Entry->Next;
483
484 switch (ThisNode->getKind()) {
485 case Assert:
486#ifndef NDEBUG // Only in -g mode...
Daniel Dunbarb99eac82009-07-24 10:05:20 +0000487 errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
488 << "\n";
Chris Lattner90234f32009-03-31 22:11:05 +0000489#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000490 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000491 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000492 case Weak:
493 // Weak just goes to null, which will unlink it from the list.
494 ThisNode->operator=(0);
495 break;
496 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000497 // Forward to the subclass's implementation.
498 static_cast<CallbackVH*>(ThisNode)->deleted();
499 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000500 }
501 }
502
503 // All callbacks and weak references should be dropped by now.
504 assert(!V->HasValueHandle && "All references to V were not removed?");
505}
506
507
508void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
509 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
510 assert(Old != New && "Changing value into itself!");
511
512 // Get the linked list base, which is guaranteed to exist since the
513 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000514 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000515 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000516 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000517 assert(Entry && "Value bit set but no entries exist");
518
519 while (Entry) {
520 // Advance pointer to avoid invalidation.
521 ValueHandleBase *ThisNode = Entry;
522 Entry = Entry->Next;
523
524 switch (ThisNode->getKind()) {
525 case Assert:
526 // Asserting handle does not follow RAUW implicitly.
527 break;
528 case Weak:
529 // Weak goes to the new value, which will unlink it from Old's list.
530 ThisNode->operator=(New);
531 break;
532 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000533 // Forward to the subclass's implementation.
534 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
535 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000536 }
537 }
538}
539
Dan Gohman745ad442009-05-02 21:10:48 +0000540/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
541/// more than once.
542CallbackVH::~CallbackVH() {}
543
Chris Lattner9c1b5022008-12-02 07:16:45 +0000544
Chris Lattner2f7c9632001-06-06 20:29:01 +0000545//===----------------------------------------------------------------------===//
546// User Class
547//===----------------------------------------------------------------------===//
548
Chris Lattner2f7c9632001-06-06 20:29:01 +0000549// replaceUsesOfWith - Replaces all references to the "From" definition with
550// references to the "To" definition.
551//
552void User::replaceUsesOfWith(Value *From, Value *To) {
553 if (From == To) return; // Duh what?
554
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000555 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Chris Lattner2c6abea2002-10-09 23:12:59 +0000556 "Cannot call User::replaceUsesofWith on a constant!");
557
Chris Lattnera073acb2001-07-07 08:36:50 +0000558 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
559 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000560 // The side effects of this setOperand call include linking to
561 // "To", adding "this" to the uses list of To, and
562 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000563 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000564 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000565}