blob: 2bdb5b8a79ce2aad29d3be4b9dc5cf88414bfbbb [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))
Owen Anderson55f1c092009-08-13 21:58:54 +000050 assert((VTy->isFirstClassType() ||
51 VTy == Type::getVoidTy(ty->getContext()) ||
Evan Chenga05c07e2008-07-24 00:08:56 +000052 isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
Devang Patel1f00b532008-02-21 01:54:02 +000053 "invalid CallInst type!");
54 else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
Owen Anderson55f1c092009-08-13 21:58:54 +000055 assert((VTy->isFirstClassType() ||
56 VTy == Type::getVoidTy(ty->getContext()) ||
Chris Lattner9df9afd2004-07-07 18:07:46 +000057 isa<OpaqueType>(ty)) &&
Chris Lattnerbea72472004-07-06 17:44:17 +000058 "Cannot create non-first-class values except for constants!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000059}
60
Gordon Henriksen14a55692007-12-10 02:14:30 +000061Value::~Value() {
Chris Lattner90234f32009-03-31 22:11:05 +000062 // Notify all ValueHandles (if present) that this value is going away.
63 if (HasValueHandle)
64 ValueHandleBase::ValueIsDeleted(this);
65
Chris Lattner2f7c9632001-06-06 20:29:01 +000066#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000067 // Check to make sure that there are no uses of this value that are still
68 // around when the value is destroyed. If there are, then we have a dangling
69 // reference and something is wrong. This code is here to print out what is
Misha Brukmanb1c93172005-04-21 23:48:37 +000070 // still being referenced. The value in question should be printed as
Chris Lattner874ddad2001-06-11 15:04:40 +000071 // a <badref>
72 //
Gordon Henriksen14a55692007-12-10 02:14:30 +000073 if (!use_empty()) {
Daniel Dunbarb99eac82009-07-24 10:05:20 +000074 errs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
Gordon Henriksen14a55692007-12-10 02:14:30 +000075 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
Daniel Dunbarb99eac82009-07-24 10:05:20 +000076 errs() << "Use still stuck around after Def is destroyed:"
Bill Wendling6a462f12006-11-17 08:03:48 +000077 << **I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000078 }
79#endif
Gordon Henriksen14a55692007-12-10 02:14:30 +000080 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner694285c2009-08-04 23:07:12 +000081
82 // If this value is named, destroy the name. This should not be in a symtab
83 // at this point.
84 if (Name)
85 Name->Destroy();
86
87 // There should be no uses of this object anymore, remove it.
88 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000089}
90
Chris Lattner4947e672005-02-01 01:24:21 +000091/// hasNUses - Return true if this Value has exactly N users.
92///
93bool Value::hasNUses(unsigned N) const {
94 use_const_iterator UI = use_begin(), E = use_end();
95
96 for (; N; --N, ++UI)
97 if (UI == E) return false; // Too few.
98 return UI == E;
99}
100
Chris Lattnerd36552f2005-02-23 16:51:11 +0000101/// hasNUsesOrMore - Return true if this value has N users or more. This is
102/// logically equivalent to getNumUses() >= N.
103///
104bool Value::hasNUsesOrMore(unsigned N) const {
105 use_const_iterator UI = use_begin(), E = use_end();
106
107 for (; N; --N, ++UI)
108 if (UI == E) return false; // Too few.
109
110 return true;
111}
112
Evan Cheng89553cc2008-06-12 21:15:59 +0000113/// isUsedInBasicBlock - Return true if this value is used in the specified
114/// basic block.
Bill Wendling0c374212008-09-25 22:42:01 +0000115bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Evan Cheng89553cc2008-06-12 21:15:59 +0000116 for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
117 const Instruction *User = dyn_cast<Instruction>(*I);
118 if (User && User->getParent() == BB)
119 return true;
120 }
121 return false;
122}
123
Chris Lattnerd36552f2005-02-23 16:51:11 +0000124
Chris Lattner4947e672005-02-01 01:24:21 +0000125/// getNumUses - This method computes the number of uses of this Value. This
126/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
127/// values.
128unsigned Value::getNumUses() const {
129 return (unsigned)std::distance(use_begin(), use_end());
130}
131
Chris Lattnerb6250822007-02-11 00:37:27 +0000132static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Chris Lattner569c8ac2007-02-11 19:12:18 +0000133 ST = 0;
Chris Lattnerb6250822007-02-11 00:37:27 +0000134 if (Instruction *I = dyn_cast<Instruction>(V)) {
135 if (BasicBlock *P = I->getParent())
136 if (Function *PP = P->getParent())
137 ST = &PP->getValueSymbolTable();
138 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
139 if (Function *P = BB->getParent())
140 ST = &P->getValueSymbolTable();
141 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
142 if (Module *P = GV->getParent())
143 ST = &P->getValueSymbolTable();
144 } else if (Argument *A = dyn_cast<Argument>(V)) {
145 if (Function *P = A->getParent())
146 ST = &P->getValueSymbolTable();
Devang Patel18dfdc92009-07-29 17:16:17 +0000147 } else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
148 if (Module *P = N->getParent()) {
149 ST = &P->getValueSymbolTable();
150 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000151 } else if (isa<MDString>(V))
152 return true;
153 else {
Chris Lattnerb6250822007-02-11 00:37:27 +0000154 assert(isa<Constant>(V) && "Unknown value type!");
155 return true; // no name is setable for this.
156 }
157 return false;
158}
Chris Lattner2c6abea2002-10-09 23:12:59 +0000159
Daniel Dunbar32285942009-07-26 00:51:56 +0000160StringRef Value::getName() const {
Daniel Dunbar7cc8f7e2009-07-26 09:22:02 +0000161 // Make sure the empty string is still a C string. For historical reasons,
162 // some clients want to call .data() on the result and expect it to be null
163 // terminated.
164 if (!Name) return StringRef("", 0);
Daniel Dunbar32285942009-07-26 00:51:56 +0000165 return Name->getKey();
Chris Lattner5109a882007-08-10 15:34:35 +0000166}
167
Chris Lattnerfd27ed92007-02-15 18:53:54 +0000168std::string Value::getNameStr() const {
Daniel Dunbar987ec562009-07-26 00:17:14 +0000169 return getName().str();
Chris Lattner32ab6432007-02-12 05:18:08 +0000170}
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000171
Daniel Dunbard786b512009-07-26 00:34:27 +0000172void Value::setName(const Twine &NewName) {
Daniel Dunbar4975db62009-07-25 04:41:11 +0000173 SmallString<32> NameData;
Daniel Dunbard786b512009-07-26 00:34:27 +0000174 NewName.toVector(NameData);
Chris Lattner1a5de582007-02-12 18:52:59 +0000175
Daniel Dunbard786b512009-07-26 00:34:27 +0000176 const char *NameStr = NameData.data();
177 unsigned NameLen = NameData.size();
178
Daniel Dunbara32c9992009-07-26 00:42:33 +0000179 // Name isn't changing?
180 if (getName() == StringRef(NameStr, NameLen))
181 return;
182
Owen Anderson55f1c092009-08-13 21:58:54 +0000183 assert(getType() != Type::getVoidTy(getContext()) &&
184 "Cannot assign a name to void values!");
Chris Lattner32ab6432007-02-12 05:18:08 +0000185
Chris Lattnerffb37782005-03-06 02:14:28 +0000186 // Get the symbol table to update for this object.
Chris Lattnerb6250822007-02-11 00:37:27 +0000187 ValueSymbolTable *ST;
188 if (getSymTab(this, ST))
189 return; // Cannot set a name on this value (e.g. constant).
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000190
Chris Lattner32ab6432007-02-12 05:18:08 +0000191 if (!ST) { // No symbol table to update? Just do the change.
Chris Lattner1a5de582007-02-12 18:52:59 +0000192 if (NameLen == 0) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000193 // Free the name for this value.
194 Name->Destroy();
195 Name = 0;
Chris Lattner1a5de582007-02-12 18:52:59 +0000196 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000197 }
Chris Lattner1a5de582007-02-12 18:52:59 +0000198
Daniel Dunbara32c9992009-07-26 00:42:33 +0000199 if (Name)
Chris Lattner1a5de582007-02-12 18:52:59 +0000200 Name->Destroy();
Chris Lattner1a5de582007-02-12 18:52:59 +0000201
202 // NOTE: Could optimize for the case the name is shrinking to not deallocate
203 // then reallocated.
204
205 // Create the new name.
206 Name = ValueName::Create(NameStr, NameStr+NameLen);
207 Name->setValue(this);
Chris Lattner32ab6432007-02-12 05:18:08 +0000208 return;
Chris Lattnerffb37782005-03-06 02:14:28 +0000209 }
Chris Lattner32ab6432007-02-12 05:18:08 +0000210
211 // NOTE: Could optimize for the case the name is shrinking to not deallocate
212 // then reallocated.
213 if (hasName()) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000214 // Remove old name.
215 ST->removeValueName(Name);
216 Name->Destroy();
217 Name = 0;
218
Chris Lattner1a5de582007-02-12 18:52:59 +0000219 if (NameLen == 0)
220 return;
Chris Lattner32ab6432007-02-12 05:18:08 +0000221 }
222
223 // Name is changing to something new.
Daniel Dunbarf01154c2009-07-23 18:50:53 +0000224 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
Chris Lattnercdb9bfc2005-03-05 19:51:50 +0000225}
226
Chris Lattner1a5de582007-02-12 18:52:59 +0000227
Chris Lattnerb6250822007-02-11 00:37:27 +0000228/// takeName - transfer the name from V to this value, setting V's name to
229/// empty. It is an error to call V->takeName(V).
230void Value::takeName(Value *V) {
Chris Lattnercf835ff2007-02-15 20:01:43 +0000231 ValueSymbolTable *ST = 0;
232 // If this value has a name, drop it.
233 if (hasName()) {
234 // Get the symtab this is in.
235 if (getSymTab(this, ST)) {
236 // We can't set a name on this value, but we need to clear V's name if
237 // it has one.
Daniel Dunbard786b512009-07-26 00:34:27 +0000238 if (V->hasName()) V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000239 return; // Cannot set a name on this value (e.g. constant).
240 }
241
242 // Remove old name.
243 if (ST)
244 ST->removeValueName(Name);
245 Name->Destroy();
246 Name = 0;
247 }
248
249 // Now we know that this has no name.
250
251 // If V has no name either, we're done.
252 if (!V->hasName()) return;
253
254 // Get this's symtab if we didn't before.
255 if (!ST) {
256 if (getSymTab(this, ST)) {
257 // Clear V's name.
Daniel Dunbard786b512009-07-26 00:34:27 +0000258 V->setName("");
Chris Lattnercf835ff2007-02-15 20:01:43 +0000259 return; // Cannot set a name on this value (e.g. constant).
260 }
261 }
262
263 // Get V's ST, this should always succed, because V has a name.
264 ValueSymbolTable *VST;
265 bool Failure = getSymTab(V, VST);
Chris Lattner106b0462008-06-21 19:47:03 +0000266 assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
Chris Lattnercf835ff2007-02-15 20:01:43 +0000267
268 // If these values are both in the same symtab, we can do this very fast.
269 // This works even if both values have no symtab yet.
270 if (ST == VST) {
271 // Take the name!
272 Name = V->Name;
273 V->Name = 0;
274 Name->setValue(this);
Chris Lattnercba18e32007-02-11 01:04:09 +0000275 return;
276 }
277
Chris Lattnercf835ff2007-02-15 20:01:43 +0000278 // Otherwise, things are slightly more complex. Remove V's name from VST and
279 // then reinsert it into ST.
280
281 if (VST)
282 VST->removeValueName(V->Name);
283 Name = V->Name;
284 V->Name = 0;
285 Name->setValue(this);
286
287 if (ST)
288 ST->reinsertValue(this);
Chris Lattnerb6250822007-02-11 00:37:27 +0000289}
290
291
Chris Lattner9f158122003-08-29 05:09:37 +0000292// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
293// except that it doesn't have all of the asserts. The asserts fail because we
294// are half-way done resolving types, which causes some types to exist as two
295// different Type*'s at the same time. This is a sledgehammer to work around
296// this problem.
297//
298void Value::uncheckedReplaceAllUsesWith(Value *New) {
Chris Lattner90234f32009-03-31 22:11:05 +0000299 // Notify all ValueHandles (if present) that this value is going away.
300 if (HasValueHandle)
301 ValueHandleBase::ValueIsRAUWd(this, New);
302
Chris Lattner4947e672005-02-01 01:24:21 +0000303 while (!use_empty()) {
Gabor Greif715b9d22008-09-19 15:13:20 +0000304 Use &U = *UseList;
Chris Lattner9f158122003-08-29 05:09:37 +0000305 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000306 // constant because they are uniqued.
Chris Lattner079edeb2003-10-16 16:53:07 +0000307 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000308 if (!isa<GlobalValue>(C)) {
Chris Lattner7a1450d2005-10-04 18:13:04 +0000309 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000310 continue;
311 }
Chris Lattner9f158122003-08-29 05:09:37 +0000312 }
Chris Lattner8e5b2c22007-08-21 00:21:07 +0000313
314 U.set(New);
Chris Lattner9f158122003-08-29 05:09:37 +0000315 }
316}
317
Chris Lattner079edeb2003-10-16 16:53:07 +0000318void Value::replaceAllUsesWith(Value *New) {
319 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
320 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
321 assert(New->getType() == getType() &&
322 "replaceAllUses of value with new value of different type!");
Chris Lattner9f158122003-08-29 05:09:37 +0000323
Chris Lattner079edeb2003-10-16 16:53:07 +0000324 uncheckedReplaceAllUsesWith(New);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325}
326
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000327Value *Value::stripPointerCasts() {
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000328 if (!isa<PointerType>(getType()))
329 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000330 Value *V = this;
331 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000332 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Duncan Sandse59fa782008-12-29 21:06:19 +0000333 if (!GEP->hasAllZeroIndices())
334 return V;
Dan Gohmane1019db2009-07-17 23:55:56 +0000335 V = GEP->getPointerOperand();
336 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
337 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000338 } else {
339 return V;
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000340 }
Duncan Sandse59fa782008-12-29 21:06:19 +0000341 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
342 } while (1);
Anton Korobeynikovfc2edad2008-05-07 22:54:15 +0000343}
344
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000345Value *Value::getUnderlyingObject() {
346 if (!isa<PointerType>(getType()))
347 return this;
Duncan Sandse59fa782008-12-29 21:06:19 +0000348 Value *V = this;
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000349 unsigned MaxLookup = 6;
Duncan Sandse59fa782008-12-29 21:06:19 +0000350 do {
Dan Gohmane1019db2009-07-17 23:55:56 +0000351 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohmane1019db2009-07-17 23:55:56 +0000352 V = GEP->getPointerOperand();
353 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
354 V = cast<Operator>(V)->getOperand(0);
Duncan Sandse59fa782008-12-29 21:06:19 +0000355 } else {
356 return V;
357 }
358 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
Nick Lewycky4a7bcf62009-04-15 06:23:41 +0000359 } while (--MaxLookup);
360 return V;
Duncan Sandsd65a4da2008-10-01 15:25:41 +0000361}
362
Chris Lattner027d7262008-12-02 18:33:11 +0000363/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattner9c1b5022008-12-02 07:16:45 +0000364/// return the value in the PHI node corresponding to PredBB. If not, return
365/// ourself. This is useful if you want to know the value something has in a
366/// predecessor block.
367Value *Value::DoPHITranslation(const BasicBlock *CurBB,
368 const BasicBlock *PredBB) {
369 PHINode *PN = dyn_cast<PHINode>(this);
370 if (PN && PN->getParent() == CurBB)
371 return PN->getIncomingValueForBlock(PredBB);
372 return this;
373}
374
Owen Anderson47db9412009-07-22 00:24:57 +0000375LLVMContext &Value::getContext() const { return VTy->getContext(); }
376
Chris Lattner90234f32009-03-31 22:11:05 +0000377//===----------------------------------------------------------------------===//
378// ValueHandleBase Class
379//===----------------------------------------------------------------------===//
380
381/// ValueHandles - This map keeps track of all of the value handles that are
382/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
383/// not a value has an entry in this map.
384typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
385static ManagedStatic<ValueHandlesTy> ValueHandles;
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000386static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
Chris Lattner90234f32009-03-31 22:11:05 +0000387
Dan Gohman3f025952009-05-04 17:25:21 +0000388/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
389/// List is known to point into the existing use list.
Chris Lattner90234f32009-03-31 22:11:05 +0000390void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
391 assert(List && "Handle list is null?");
392
393 // Splice ourselves into the list.
394 Next = *List;
395 *List = this;
396 setPrevPtr(List);
397 if (Next) {
398 Next->setPrevPtr(&Next);
399 assert(VP == Next->VP && "Added to wrong list?");
400 }
401}
402
403/// AddToUseList - Add this ValueHandle to the use list for VP.
404void ValueHandleBase::AddToUseList() {
405 assert(VP && "Null pointer doesn't have a use list!");
406 if (VP->HasValueHandle) {
407 // If this value already has a ValueHandle, then it must be in the
408 // ValueHandles map already.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000409 sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000410 ValueHandleBase *&Entry = (*ValueHandles)[VP];
411 assert(Entry != 0 && "Value doesn't have any handles?");
Owen Anderson4b660a82009-06-17 17:36:57 +0000412 AddToExistingUseList(&Entry);
Owen Anderson4b660a82009-06-17 17:36:57 +0000413 return;
Chris Lattner90234f32009-03-31 22:11:05 +0000414 }
415
416 // Ok, it doesn't have any handles yet, so we must insert it into the
417 // DenseMap. However, doing this insertion could cause the DenseMap to
418 // reallocate itself, which would invalidate all of the PrevP pointers that
419 // point into the old table. Handle this by checking for reallocation and
420 // updating the stale pointers only if needed.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000421 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000422 ValueHandlesTy &Handles = *ValueHandles;
423 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
424
425 ValueHandleBase *&Entry = Handles[VP];
426 assert(Entry == 0 && "Value really did already have handles?");
427 AddToExistingUseList(&Entry);
Dan Gohman3f025952009-05-04 17:25:21 +0000428 VP->HasValueHandle = true;
Chris Lattner90234f32009-03-31 22:11:05 +0000429
430 // If reallocation didn't happen or if this was the first insertion, don't
431 // walk the table.
432 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Anderson4b660a82009-06-17 17:36:57 +0000433 Handles.size() == 1) {
Chris Lattner90234f32009-03-31 22:11:05 +0000434 return;
Owen Anderson4b660a82009-06-17 17:36:57 +0000435 }
Chris Lattner90234f32009-03-31 22:11:05 +0000436
437 // Okay, reallocation did happen. Fix the Prev Pointers.
438 for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
439 I != E; ++I) {
440 assert(I->second && I->first == I->second->VP && "List invariant broken!");
441 I->second->setPrevPtr(&I->second);
442 }
443}
444
445/// RemoveFromUseList - Remove this ValueHandle from its current use list.
446void ValueHandleBase::RemoveFromUseList() {
447 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
448
449 // Unlink this from its use list.
450 ValueHandleBase **PrevPtr = getPrevPtr();
451 assert(*PrevPtr == this && "List invariant broken");
452
453 *PrevPtr = Next;
454 if (Next) {
455 assert(Next->getPrevPtr() == &Next && "List invariant broken");
456 Next->setPrevPtr(PrevPtr);
457 return;
458 }
459
460 // If the Next pointer was null, then it is possible that this was the last
461 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
462 // map.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000463 sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
Chris Lattner90234f32009-03-31 22:11:05 +0000464 ValueHandlesTy &Handles = *ValueHandles;
465 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
466 Handles.erase(VP);
467 VP->HasValueHandle = false;
468 }
469}
470
471
472void ValueHandleBase::ValueIsDeleted(Value *V) {
473 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
474
475 // Get the linked list base, which is guaranteed to exist since the
476 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000477 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000478 ValueHandleBase *Entry = (*ValueHandles)[V];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000479 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000480 assert(Entry && "Value bit set but no entries exist");
481
482 while (Entry) {
483 // Advance pointer to avoid invalidation.
484 ValueHandleBase *ThisNode = Entry;
485 Entry = Entry->Next;
486
487 switch (ThisNode->getKind()) {
488 case Assert:
489#ifndef NDEBUG // Only in -g mode...
Daniel Dunbarb99eac82009-07-24 10:05:20 +0000490 errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
491 << "\n";
Chris Lattner90234f32009-03-31 22:11:05 +0000492#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000493 llvm_unreachable("An asserting value handle still pointed to this"
Jeffrey Yasskind8d725d2009-07-08 22:09:00 +0000494 " value!");
Chris Lattner90234f32009-03-31 22:11:05 +0000495 case Weak:
496 // Weak just goes to null, which will unlink it from the list.
497 ThisNode->operator=(0);
498 break;
499 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000500 // Forward to the subclass's implementation.
501 static_cast<CallbackVH*>(ThisNode)->deleted();
502 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000503 }
504 }
505
506 // All callbacks and weak references should be dropped by now.
507 assert(!V->HasValueHandle && "All references to V were not removed?");
508}
509
510
511void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
512 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
513 assert(Old != New && "Changing value into itself!");
514
515 // Get the linked list base, which is guaranteed to exist since the
516 // HasValueHandle flag is set.
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000517 ValueHandlesLock->reader_acquire();
Chris Lattner90234f32009-03-31 22:11:05 +0000518 ValueHandleBase *Entry = (*ValueHandles)[Old];
Owen Anderson5e1f6d92009-06-18 20:36:21 +0000519 ValueHandlesLock->reader_release();
Chris Lattner90234f32009-03-31 22:11:05 +0000520 assert(Entry && "Value bit set but no entries exist");
521
522 while (Entry) {
523 // Advance pointer to avoid invalidation.
524 ValueHandleBase *ThisNode = Entry;
525 Entry = Entry->Next;
526
527 switch (ThisNode->getKind()) {
528 case Assert:
529 // Asserting handle does not follow RAUW implicitly.
530 break;
531 case Weak:
532 // Weak goes to the new value, which will unlink it from Old's list.
533 ThisNode->operator=(New);
534 break;
535 case Callback:
Dan Gohman745ad442009-05-02 21:10:48 +0000536 // Forward to the subclass's implementation.
537 static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
538 break;
Chris Lattner90234f32009-03-31 22:11:05 +0000539 }
540 }
541}
542
Dan Gohman745ad442009-05-02 21:10:48 +0000543/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
544/// more than once.
545CallbackVH::~CallbackVH() {}
546
Chris Lattner9c1b5022008-12-02 07:16:45 +0000547
Chris Lattner2f7c9632001-06-06 20:29:01 +0000548//===----------------------------------------------------------------------===//
549// User Class
550//===----------------------------------------------------------------------===//
551
Chris Lattner2f7c9632001-06-06 20:29:01 +0000552// replaceUsesOfWith - Replaces all references to the "From" definition with
553// references to the "To" definition.
554//
555void User::replaceUsesOfWith(Value *From, Value *To) {
556 if (From == To) return; // Duh what?
557
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000558 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
Dan Gohman5211b012009-08-11 15:53:15 +0000559 "Cannot call User::replaceUsesOfWith on a constant!");
Chris Lattner2c6abea2002-10-09 23:12:59 +0000560
Chris Lattnera073acb2001-07-07 08:36:50 +0000561 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
562 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000563 // The side effects of this setOperand call include linking to
564 // "To", adding "this" to the uses list of To, and
565 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000566 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000567 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000568}