blob: d734e4ea8183e50e7fd82a1be339d080a8df753e [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner722272d2009-03-31 22:11:05 +000010// This file implements the Value, ValueHandle, and User classes.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000014#include "llvm/IR/Value.h"
Owen Anderson4d919432009-08-18 18:28:58 +000015#include "LLVMContextImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SmallString.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Constant.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070021#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/InstrTypes.h"
23#include "llvm/IR/Instructions.h"
Stephen Hines36b56882014-04-23 16:57:46 -070024#include "llvm/IR/LeakDetector.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000025#include "llvm/IR/Module.h"
26#include "llvm/IR/Operator.h"
Stephen Hines36b56882014-04-23 16:57:46 -070027#include "llvm/IR/ValueHandle.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/ValueSymbolTable.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000029#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner722272d2009-03-31 22:11:05 +000031#include "llvm/Support/ManagedStatic.h"
Chris Lattner00950542001-06-06 20:29:01 +000032#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner00950542001-06-06 20:29:01 +000035//===----------------------------------------------------------------------===//
36// Value Class
37//===----------------------------------------------------------------------===//
38
Chris Lattnerdb125cf2011-07-18 04:54:35 +000039static inline Type *checkType(Type *Ty) {
Chris Lattner82d18aa2001-12-13 00:41:27 +000040 assert(Ty && "Value defined with a null type: Error!");
Chris Lattner1afcace2011-07-09 17:41:24 +000041 return const_cast<Type*>(Ty);
Chris Lattner82d18aa2001-12-13 00:41:27 +000042}
43
Chris Lattnerdb125cf2011-07-18 04:54:35 +000044Value::Value(Type *ty, unsigned scid)
Chris Lattner3990b122009-12-28 23:41:32 +000045 : SubclassID(scid), HasValueHandle(0),
Chris Lattner1afcace2011-07-09 17:41:24 +000046 SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)),
Stephen Hinesdce4a402014-05-29 02:49:00 -070047 UseList(nullptr), Name(nullptr) {
Chris Lattner1afcace2011-07-09 17:41:24 +000048 // FIXME: Why isn't this in the subclass gunk??
Richard Smith488fdce2012-12-20 04:11:02 +000049 // Note, we cannot call isa<CallInst> before the CallInst has been
50 // constructed.
51 if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
Chris Lattner1afcace2011-07-09 17:41:24 +000052 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
53 "invalid CallInst type!");
Richard Smith488fdce2012-12-20 04:11:02 +000054 else if (SubclassID != BasicBlockVal &&
55 (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
Chris Lattner1afcace2011-07-09 17:41:24 +000056 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
Chris Lattnera9e77812004-07-06 17:44:17 +000057 "Cannot create non-first-class values except for constants!");
Chris Lattner00950542001-06-06 20:29:01 +000058}
59
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000060Value::~Value() {
Chris Lattner722272d2009-03-31 22:11:05 +000061 // Notify all ValueHandles (if present) that this value is going away.
62 if (HasValueHandle)
63 ValueHandleBase::ValueIsDeleted(this);
Daniel Dunbarce99a6e2009-09-20 04:03:34 +000064
Chris Lattner00950542001-06-06 20:29:01 +000065#ifndef NDEBUG // Only in -g mode...
Chris Lattneree976f32001-06-11 15:04:40 +000066 // Check to make sure that there are no uses of this value that are still
67 // around when the value is destroyed. If there are, then we have a dangling
68 // reference and something is wrong. This code is here to print out what is
Misha Brukmanfd939082005-04-21 23:48:37 +000069 // still being referenced. The value in question should be printed as
Chris Lattneree976f32001-06-11 15:04:40 +000070 // a <badref>
71 //
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000072 if (!use_empty()) {
Benjamin Kramera7b0cb72011-11-15 16:27:03 +000073 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000074 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
David Greene6892c7a2010-01-05 01:30:09 +000075 dbgs() << "Use still stuck around after Def is destroyed:"
Bill Wendling2e3def12006-11-17 08:03:48 +000076 << **I << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000077 }
78#endif
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000079 assert(use_empty() && "Uses remain when a value is destroyed!");
Chris Lattner4f95d2b2009-08-04 23:07:12 +000080
81 // If this value is named, destroy the name. This should not be in a symtab
82 // at this point.
Bill Wendling3ecb4472012-04-10 20:12:16 +000083 if (Name && SubclassID != MDStringVal)
Chris Lattner4f95d2b2009-08-04 23:07:12 +000084 Name->Destroy();
Daniel Dunbarce99a6e2009-09-20 04:03:34 +000085
Chris Lattner4f95d2b2009-08-04 23:07:12 +000086 // There should be no uses of this object anymore, remove it.
87 LeakDetector::removeGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +000088}
89
Chris Lattner29d1ca62005-02-01 01:24:21 +000090/// hasNUses - Return true if this Value has exactly N users.
91///
92bool Value::hasNUses(unsigned N) const {
Gabor Greif60ad7812010-03-25 23:06:16 +000093 const_use_iterator UI = use_begin(), E = use_end();
Chris Lattner29d1ca62005-02-01 01:24:21 +000094
95 for (; N; --N, ++UI)
96 if (UI == E) return false; // Too few.
97 return UI == E;
98}
99
Chris Lattner8daf0562005-02-23 16:51:11 +0000100/// hasNUsesOrMore - Return true if this value has N users or more. This is
101/// logically equivalent to getNumUses() >= N.
102///
103bool Value::hasNUsesOrMore(unsigned N) const {
Gabor Greif60ad7812010-03-25 23:06:16 +0000104 const_use_iterator UI = use_begin(), E = use_end();
Chris Lattner8daf0562005-02-23 16:51:11 +0000105
106 for (; N; --N, ++UI)
107 if (UI == E) return false; // Too few.
108
109 return true;
110}
111
Evan Cheng502a4f52008-06-12 21:15:59 +0000112/// isUsedInBasicBlock - Return true if this value is used in the specified
113/// basic block.
Bill Wendlingc4f72dd2008-09-25 22:42:01 +0000114bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
Jakob Stoklund Olesena88d9742013-05-14 23:45:56 +0000115 // This can be computed either by scanning the instructions in BB, or by
116 // scanning the use list of this Value. Both lists can be very long, but
117 // usually one is quite short.
118 //
119 // Scan both lists simultaneously until one is exhausted. This limits the
120 // search to the shorter list.
121 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
Stephen Hines36b56882014-04-23 16:57:46 -0700122 const_user_iterator UI = user_begin(), UE = user_end();
Jakob Stoklund Olesena88d9742013-05-14 23:45:56 +0000123 for (; BI != BE && UI != UE; ++BI, ++UI) {
124 // Scan basic block: Check if this Value is used by the instruction at BI.
125 if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end())
Benjamin Kramer4da7f902011-12-05 17:23:27 +0000126 return true;
Jakob Stoklund Olesena88d9742013-05-14 23:45:56 +0000127 // Scan use list: Check if the use at UI is in BB.
128 const Instruction *User = dyn_cast<Instruction>(*UI);
Evan Cheng502a4f52008-06-12 21:15:59 +0000129 if (User && User->getParent() == BB)
130 return true;
131 }
132 return false;
133}
134
Chris Lattner8daf0562005-02-23 16:51:11 +0000135
Chris Lattner29d1ca62005-02-01 01:24:21 +0000136/// getNumUses - This method computes the number of uses of this Value. This
137/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
138/// values.
139unsigned Value::getNumUses() const {
140 return (unsigned)std::distance(use_begin(), use_end());
141}
142
Chris Lattner72168112007-02-11 00:37:27 +0000143static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700144 ST = nullptr;
Chris Lattner72168112007-02-11 00:37:27 +0000145 if (Instruction *I = dyn_cast<Instruction>(V)) {
146 if (BasicBlock *P = I->getParent())
147 if (Function *PP = P->getParent())
148 ST = &PP->getValueSymbolTable();
149 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000150 if (Function *P = BB->getParent())
Chris Lattner72168112007-02-11 00:37:27 +0000151 ST = &P->getValueSymbolTable();
152 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000153 if (Module *P = GV->getParent())
Chris Lattner72168112007-02-11 00:37:27 +0000154 ST = &P->getValueSymbolTable();
155 } else if (Argument *A = dyn_cast<Argument>(V)) {
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000156 if (Function *P = A->getParent())
Chris Lattner72168112007-02-11 00:37:27 +0000157 ST = &P->getValueSymbolTable();
Devang Patele54abc92009-07-22 17:43:22 +0000158 } else if (isa<MDString>(V))
159 return true;
160 else {
Chris Lattner72168112007-02-11 00:37:27 +0000161 assert(isa<Constant>(V) && "Unknown value type!");
162 return true; // no name is setable for this.
163 }
164 return false;
165}
Chris Lattner28eca8b2002-10-09 23:12:59 +0000166
Daniel Dunbar499027f2009-07-26 00:51:56 +0000167StringRef Value::getName() const {
Daniel Dunbar4fa49902009-07-26 09:22:02 +0000168 // Make sure the empty string is still a C string. For historical reasons,
169 // some clients want to call .data() on the result and expect it to be null
170 // terminated.
171 if (!Name) return StringRef("", 0);
Daniel Dunbar499027f2009-07-26 00:51:56 +0000172 return Name->getKey();
Chris Lattner71996e72007-08-10 15:34:35 +0000173}
174
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000175void Value::setName(const Twine &NewName) {
Bill Wendling3ecb4472012-04-10 20:12:16 +0000176 assert(SubclassID != MDStringVal &&
177 "Cannot set the name of MDString with this method!");
178
Daniel Dunbar51499322009-08-19 23:37:23 +0000179 // Fast path for common IRBuilder case of setName("") when there is no name.
180 if (NewName.isTriviallyEmpty() && !hasName())
181 return;
182
Daniel Dunbare4760042009-08-19 05:08:06 +0000183 SmallString<256> NameData;
Benjamin Kramerb357e062010-01-13 12:45:23 +0000184 StringRef NameRef = NewName.toStringRef(NameData);
Stephen Hines36b56882014-04-23 16:57:46 -0700185 assert(NameRef.find_first_of(0) == StringRef::npos &&
186 "Null bytes are not allowed in names");
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000187
Daniel Dunbar07f69032009-07-26 00:42:33 +0000188 // Name isn't changing?
Benjamin Kramerb357e062010-01-13 12:45:23 +0000189 if (getName() == NameRef)
Daniel Dunbar07f69032009-07-26 00:42:33 +0000190 return;
191
Benjamin Kramerf0127052010-01-05 13:12:22 +0000192 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000193
Chris Lattner04cb8002005-03-06 02:14:28 +0000194 // Get the symbol table to update for this object.
Chris Lattner72168112007-02-11 00:37:27 +0000195 ValueSymbolTable *ST;
196 if (getSymTab(this, ST))
197 return; // Cannot set a name on this value (e.g. constant).
Chris Lattner0d1e4072005-03-05 19:51:50 +0000198
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000199 if (Function *F = dyn_cast<Function>(this))
200 getContext().pImpl->IntrinsicIDCache.erase(F);
201
Chris Lattnerdec628e2007-02-12 05:18:08 +0000202 if (!ST) { // No symbol table to update? Just do the change.
Benjamin Kramerb357e062010-01-13 12:45:23 +0000203 if (NameRef.empty()) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000204 // Free the name for this value.
205 Name->Destroy();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700206 Name = nullptr;
Chris Lattner042ad362007-02-12 18:52:59 +0000207 return;
Chris Lattner04cb8002005-03-06 02:14:28 +0000208 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000209
Daniel Dunbar07f69032009-07-26 00:42:33 +0000210 if (Name)
Chris Lattner042ad362007-02-12 18:52:59 +0000211 Name->Destroy();
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000212
Chris Lattner042ad362007-02-12 18:52:59 +0000213 // NOTE: Could optimize for the case the name is shrinking to not deallocate
214 // then reallocated.
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000215
Chris Lattner042ad362007-02-12 18:52:59 +0000216 // Create the new name.
Benjamin Kramerb357e062010-01-13 12:45:23 +0000217 Name = ValueName::Create(NameRef.begin(), NameRef.end());
Chris Lattner042ad362007-02-12 18:52:59 +0000218 Name->setValue(this);
Chris Lattnerdec628e2007-02-12 05:18:08 +0000219 return;
Chris Lattner04cb8002005-03-06 02:14:28 +0000220 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000221
Chris Lattnerdec628e2007-02-12 05:18:08 +0000222 // NOTE: Could optimize for the case the name is shrinking to not deallocate
223 // then reallocated.
224 if (hasName()) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000225 // Remove old name.
226 ST->removeValueName(Name);
227 Name->Destroy();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700228 Name = nullptr;
Chris Lattnerdec628e2007-02-12 05:18:08 +0000229
Benjamin Kramerb357e062010-01-13 12:45:23 +0000230 if (NameRef.empty())
Chris Lattner042ad362007-02-12 18:52:59 +0000231 return;
Chris Lattnerdec628e2007-02-12 05:18:08 +0000232 }
233
234 // Name is changing to something new.
Benjamin Kramerb357e062010-01-13 12:45:23 +0000235 Name = ST->createValueName(NameRef, this);
Chris Lattner0d1e4072005-03-05 19:51:50 +0000236}
237
Chris Lattner042ad362007-02-12 18:52:59 +0000238
Chris Lattner72168112007-02-11 00:37:27 +0000239/// takeName - transfer the name from V to this value, setting V's name to
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000240/// empty. It is an error to call V->takeName(V).
Chris Lattner72168112007-02-11 00:37:27 +0000241void Value::takeName(Value *V) {
Bill Wendling3ecb4472012-04-10 20:12:16 +0000242 assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
243
Stephen Hinesdce4a402014-05-29 02:49:00 -0700244 ValueSymbolTable *ST = nullptr;
Chris Lattner0878c312007-02-15 20:01:43 +0000245 // If this value has a name, drop it.
246 if (hasName()) {
247 // Get the symtab this is in.
248 if (getSymTab(this, ST)) {
249 // We can't set a name on this value, but we need to clear V's name if
250 // it has one.
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000251 if (V->hasName()) V->setName("");
Chris Lattner0878c312007-02-15 20:01:43 +0000252 return; // Cannot set a name on this value (e.g. constant).
253 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000254
Chris Lattner0878c312007-02-15 20:01:43 +0000255 // Remove old name.
256 if (ST)
257 ST->removeValueName(Name);
258 Name->Destroy();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700259 Name = nullptr;
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000260 }
261
Chris Lattner0878c312007-02-15 20:01:43 +0000262 // Now we know that this has no name.
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000263
Chris Lattner0878c312007-02-15 20:01:43 +0000264 // If V has no name either, we're done.
265 if (!V->hasName()) return;
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000266
Chris Lattner0878c312007-02-15 20:01:43 +0000267 // Get this's symtab if we didn't before.
268 if (!ST) {
269 if (getSymTab(this, ST)) {
270 // Clear V's name.
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000271 V->setName("");
Chris Lattner0878c312007-02-15 20:01:43 +0000272 return; // Cannot set a name on this value (e.g. constant).
273 }
274 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000275
Chris Lattner0878c312007-02-15 20:01:43 +0000276 // Get V's ST, this should always succed, because V has a name.
277 ValueSymbolTable *VST;
278 bool Failure = getSymTab(V, VST);
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000279 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000280
Chris Lattner0878c312007-02-15 20:01:43 +0000281 // If these values are both in the same symtab, we can do this very fast.
282 // This works even if both values have no symtab yet.
283 if (ST == VST) {
284 // Take the name!
285 Name = V->Name;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700286 V->Name = nullptr;
Chris Lattner0878c312007-02-15 20:01:43 +0000287 Name->setValue(this);
Chris Lattnerf41916e2007-02-11 01:04:09 +0000288 return;
289 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000290
Chris Lattner0878c312007-02-15 20:01:43 +0000291 // Otherwise, things are slightly more complex. Remove V's name from VST and
292 // then reinsert it into ST.
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000293
Chris Lattner0878c312007-02-15 20:01:43 +0000294 if (VST)
295 VST->removeValueName(V->Name);
296 Name = V->Name;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700297 V->Name = nullptr;
Chris Lattner0878c312007-02-15 20:01:43 +0000298 Name->setValue(this);
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000299
Chris Lattner0878c312007-02-15 20:01:43 +0000300 if (ST)
301 ST->reinsertValue(this);
Chris Lattner72168112007-02-11 00:37:27 +0000302}
303
Stephen Hinesdce4a402014-05-29 02:49:00 -0700304static GlobalObject &findReplacementForAliasUse(Value &C) {
305 if (auto *GO = dyn_cast<GlobalObject>(&C))
306 return *GO;
307 if (auto *GA = dyn_cast<GlobalAlias>(&C))
308 return *GA->getAliasee();
309 auto *CE = cast<ConstantExpr>(&C);
310 assert(CE->getOpcode() == Instruction::BitCast ||
311 CE->getOpcode() == Instruction::GetElementPtr ||
312 CE->getOpcode() == Instruction::AddrSpaceCast);
313 if (CE->getOpcode() == Instruction::GetElementPtr)
314 assert(cast<GEPOperator>(CE)->hasAllZeroIndices());
315 return findReplacementForAliasUse(*CE->getOperand(0));
316}
317
318static void replaceAliasUseWith(Use &U, Value *New) {
319 GlobalObject &Replacement = findReplacementForAliasUse(*New);
320 assert(&cast<GlobalObject>(*U) != &Replacement &&
321 "replaceAliasUseWith cannot form an alias cycle");
322 U.set(&Replacement);
323}
324
325#ifndef NDEBUG
326static bool contains(SmallPtrSet<ConstantExpr *, 4> &Cache, ConstantExpr *Expr,
327 Constant *C) {
328 if (!Cache.insert(Expr))
329 return false;
330
331 for (auto &O : Expr->operands()) {
332 if (O == C)
333 return true;
334 auto *CE = dyn_cast<ConstantExpr>(O);
335 if (!CE)
336 continue;
337 if (contains(Cache, CE, C))
338 return true;
339 }
340 return false;
341}
342
343static bool contains(Value *Expr, Value *V) {
344 if (Expr == V)
345 return true;
346
347 auto *C = dyn_cast<Constant>(V);
348 if (!C)
349 return false;
350
351 auto *CE = dyn_cast<ConstantExpr>(Expr);
352 if (!CE)
353 return false;
354
355 SmallPtrSet<ConstantExpr *, 4> Cache;
356 return contains(Cache, CE, C);
357}
358#endif
Chris Lattner72168112007-02-11 00:37:27 +0000359
Chris Lattner678f9e02011-07-15 06:18:52 +0000360void Value::replaceAllUsesWith(Value *New) {
361 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700362 assert(!contains(New, this) &&
363 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
Chris Lattner678f9e02011-07-15 06:18:52 +0000364 assert(New->getType() == getType() &&
365 "replaceAllUses of value with new value of different type!");
366
Chris Lattner722272d2009-03-31 22:11:05 +0000367 // Notify all ValueHandles (if present) that this value is going away.
368 if (HasValueHandle)
369 ValueHandleBase::ValueIsRAUWd(this, New);
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000370
Chris Lattner29d1ca62005-02-01 01:24:21 +0000371 while (!use_empty()) {
Gabor Greif6f426652008-09-19 15:13:20 +0000372 Use &U = *UseList;
Chris Lattner2bc065b2003-08-29 05:09:37 +0000373 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
Chris Lattner2d691332007-08-21 00:21:07 +0000374 // constant because they are uniqued.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700375 if (auto *C = dyn_cast<Constant>(U.getUser())) {
376 if (isa<GlobalAlias>(C)) {
377 replaceAliasUseWith(U, New);
378 continue;
379 }
Chris Lattner2d691332007-08-21 00:21:07 +0000380 if (!isa<GlobalValue>(C)) {
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +0000381 C->replaceUsesOfWithOnConstant(this, New, &U);
Chris Lattner2d691332007-08-21 00:21:07 +0000382 continue;
383 }
Chris Lattner2bc065b2003-08-29 05:09:37 +0000384 }
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000385
Chris Lattner2d691332007-08-21 00:21:07 +0000386 U.set(New);
Chris Lattner2bc065b2003-08-29 05:09:37 +0000387 }
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000388
Jay Foad95c3e482011-06-23 09:09:15 +0000389 if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
390 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
Chris Lattner2bc065b2003-08-29 05:09:37 +0000391}
392
Chandler Carruth84dfc322012-03-10 08:39:09 +0000393namespace {
394// Various metrics for how much to strip off of pointers.
395enum PointerStripKind {
396 PSK_ZeroIndices,
Rafael Espindolaeaf14782013-05-06 01:48:55 +0000397 PSK_ZeroIndicesAndAliases,
Chandler Carruth274d3772012-03-14 23:19:53 +0000398 PSK_InBoundsConstantIndices,
399 PSK_InBounds
Chandler Carruth84dfc322012-03-10 08:39:09 +0000400};
401
402template <PointerStripKind StripKind>
403static Value *stripPointerCastsAndOffsets(Value *V) {
404 if (!V->getType()->isPointerTy())
405 return V;
Dan Gohman50f424c2010-06-28 21:16:52 +0000406
407 // Even though we don't look through PHI nodes, we could be called on an
408 // instruction in an unreachable block, which may be on a cycle.
409 SmallPtrSet<Value *, 4> Visited;
410
Dan Gohman50f424c2010-06-28 21:16:52 +0000411 Visited.insert(V);
Duncan Sandsf08bf112008-12-29 21:06:19 +0000412 do {
Dan Gohman016de812009-07-17 23:55:56 +0000413 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Chandler Carruth84dfc322012-03-10 08:39:09 +0000414 switch (StripKind) {
Rafael Espindolaeaf14782013-05-06 01:48:55 +0000415 case PSK_ZeroIndicesAndAliases:
Chandler Carruth84dfc322012-03-10 08:39:09 +0000416 case PSK_ZeroIndices:
417 if (!GEP->hasAllZeroIndices())
418 return V;
419 break;
Chandler Carruth274d3772012-03-14 23:19:53 +0000420 case PSK_InBoundsConstantIndices:
Chandler Carruth84dfc322012-03-10 08:39:09 +0000421 if (!GEP->hasAllConstantIndices())
422 return V;
Chandler Carruth274d3772012-03-14 23:19:53 +0000423 // fallthrough
Chandler Carruth84dfc322012-03-10 08:39:09 +0000424 case PSK_InBounds:
425 if (!GEP->isInBounds())
426 return V;
427 break;
Chandler Carruth84dfc322012-03-10 08:39:09 +0000428 }
Dan Gohman016de812009-07-17 23:55:56 +0000429 V = GEP->getPointerOperand();
Matt Arsenault59d3ae62013-11-15 01:34:59 +0000430 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
431 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
Dan Gohman016de812009-07-17 23:55:56 +0000432 V = cast<Operator>(V)->getOperand(0);
Dan Gohmanaac1bfb2009-08-27 17:55:13 +0000433 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
Rafael Espindolaeaf14782013-05-06 01:48:55 +0000434 if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
Dan Gohmanaac1bfb2009-08-27 17:55:13 +0000435 return V;
436 V = GA->getAliasee();
Duncan Sandsf08bf112008-12-29 21:06:19 +0000437 } else {
438 return V;
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000439 }
Duncan Sands1df98592010-02-16 11:11:14 +0000440 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
Dan Gohman50f424c2010-06-28 21:16:52 +0000441 } while (Visited.insert(V));
442
443 return V;
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000444}
Chandler Carruth84dfc322012-03-10 08:39:09 +0000445} // namespace
446
447Value *Value::stripPointerCasts() {
Rafael Espindolaeaf14782013-05-06 01:48:55 +0000448 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
449}
450
451Value *Value::stripPointerCastsNoFollowAliases() {
Chandler Carruth84dfc322012-03-10 08:39:09 +0000452 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
453}
454
Chandler Carruth274d3772012-03-14 23:19:53 +0000455Value *Value::stripInBoundsConstantOffsets() {
456 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
Chandler Carruth84dfc322012-03-10 08:39:09 +0000457}
458
Chandler Carruthf73826b2013-08-22 11:25:11 +0000459Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
460 APInt &Offset) {
461 if (!getType()->isPointerTy())
462 return this;
463
464 assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
465 getType())->getAddressSpace()) &&
466 "The offset must have exactly as many bits as our pointer.");
467
468 // Even though we don't look through PHI nodes, we could be called on an
469 // instruction in an unreachable block, which may be on a cycle.
470 SmallPtrSet<Value *, 4> Visited;
471 Visited.insert(this);
472 Value *V = this;
473 do {
474 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
475 if (!GEP->isInBounds())
476 return V;
Chandler Carruthc99a0d82013-08-25 10:46:39 +0000477 APInt GEPOffset(Offset);
478 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
Chandler Carruthf73826b2013-08-22 11:25:11 +0000479 return V;
Chandler Carruthc99a0d82013-08-25 10:46:39 +0000480 Offset = GEPOffset;
Chandler Carruthf73826b2013-08-22 11:25:11 +0000481 V = GEP->getPointerOperand();
482 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
483 V = cast<Operator>(V)->getOperand(0);
484 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
485 V = GA->getAliasee();
486 } else {
487 return V;
488 }
489 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
490 } while (Visited.insert(V));
491
492 return V;
493}
494
Chandler Carruth84dfc322012-03-10 08:39:09 +0000495Value *Value::stripInBoundsOffsets() {
496 return stripPointerCastsAndOffsets<PSK_InBounds>(this);
497}
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000498
Dan Gohman4d70a292010-11-11 21:23:25 +0000499/// isDereferenceablePointer - Test if this value is always a pointer to
Nick Lewycky67b64642010-11-11 21:51:44 +0000500/// allocated and suitably aligned memory for a simple load or store.
Nick Lewycky37abc482012-01-23 00:05:17 +0000501static bool isDereferenceablePointer(const Value *V,
502 SmallPtrSet<const Value *, 32> &Visited) {
Dan Gohman4d70a292010-11-11 21:23:25 +0000503 // Note that it is not safe to speculate into a malloc'd region because
504 // malloc may return null.
505 // It's also not always safe to follow a bitcast, for example:
506 // bitcast i8* (alloca i8) to i32*
507 // would result in a 4-byte load from a 1-byte alloca. Some cases could
Micah Villmow3574eca2012-10-08 16:38:25 +0000508 // be handled using DataLayout to check sizes and alignments though.
Dan Gohman4d70a292010-11-11 21:23:25 +0000509
510 // These are obviously ok.
Nick Lewycky37abc482012-01-23 00:05:17 +0000511 if (isa<AllocaInst>(V)) return true;
Dan Gohman4d70a292010-11-11 21:23:25 +0000512
513 // Global variables which can't collapse to null are ok.
Nick Lewycky37abc482012-01-23 00:05:17 +0000514 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Dan Gohman4d70a292010-11-11 21:23:25 +0000515 return !GV->hasExternalWeakLinkage();
516
Chris Lattner3928af62011-01-23 21:15:29 +0000517 // byval arguments are ok.
Nick Lewycky37abc482012-01-23 00:05:17 +0000518 if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner3928af62011-01-23 21:15:29 +0000519 return A->hasByValAttr();
Nick Lewycky37abc482012-01-23 00:05:17 +0000520
Dan Gohman4d70a292010-11-11 21:23:25 +0000521 // For GEPs, determine if the indexing lands within the allocated object.
Nick Lewycky37abc482012-01-23 00:05:17 +0000522 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Dan Gohman4d70a292010-11-11 21:23:25 +0000523 // Conservatively require that the base pointer be fully dereferenceable.
Nick Lewycky37abc482012-01-23 00:05:17 +0000524 if (!Visited.insert(GEP->getOperand(0)))
525 return false;
526 if (!isDereferenceablePointer(GEP->getOperand(0), Visited))
Dan Gohman4d70a292010-11-11 21:23:25 +0000527 return false;
528 // Check the indices.
529 gep_type_iterator GTI = gep_type_begin(GEP);
530 for (User::const_op_iterator I = GEP->op_begin()+1,
531 E = GEP->op_end(); I != E; ++I) {
532 Value *Index = *I;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000533 Type *Ty = *GTI++;
Dan Gohman4d70a292010-11-11 21:23:25 +0000534 // Struct indices can't be out of bounds.
535 if (isa<StructType>(Ty))
536 continue;
537 ConstantInt *CI = dyn_cast<ConstantInt>(Index);
538 if (!CI)
539 return false;
540 // Zero is always ok.
541 if (CI->isZero())
542 continue;
543 // Check to see that it's within the bounds of an array.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000544 ArrayType *ATy = dyn_cast<ArrayType>(Ty);
Dan Gohman4d70a292010-11-11 21:23:25 +0000545 if (!ATy)
546 return false;
547 if (CI->getValue().getActiveBits() > 64)
548 return false;
549 if (CI->getZExtValue() >= ATy->getNumElements())
550 return false;
551 }
552 // Indices check out; this is dereferenceable.
553 return true;
554 }
555
556 // If we don't know, assume the worst.
557 return false;
558}
559
Nick Lewycky37abc482012-01-23 00:05:17 +0000560/// isDereferenceablePointer - Test if this value is always a pointer to
561/// allocated and suitably aligned memory for a simple load or store.
562bool Value::isDereferenceablePointer() const {
563 SmallPtrSet<const Value *, 32> Visited;
564 return ::isDereferenceablePointer(this, Visited);
565}
566
Chris Lattner01c8e022008-12-02 18:33:11 +0000567/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
Chris Lattnerc7f7c1d2008-12-02 07:16:45 +0000568/// return the value in the PHI node corresponding to PredBB. If not, return
569/// ourself. This is useful if you want to know the value something has in a
570/// predecessor block.
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000571Value *Value::DoPHITranslation(const BasicBlock *CurBB,
Chris Lattnerc7f7c1d2008-12-02 07:16:45 +0000572 const BasicBlock *PredBB) {
573 PHINode *PN = dyn_cast<PHINode>(this);
574 if (PN && PN->getParent() == CurBB)
575 return PN->getIncomingValueForBlock(PredBB);
576 return this;
577}
578
Owen Andersone922c022009-07-22 00:24:57 +0000579LLVMContext &Value::getContext() const { return VTy->getContext(); }
580
Chris Lattner722272d2009-03-31 22:11:05 +0000581//===----------------------------------------------------------------------===//
582// ValueHandleBase Class
583//===----------------------------------------------------------------------===//
584
Mike Stumpfe095f32009-05-04 18:40:41 +0000585/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
586/// List is known to point into the existing use list.
Chris Lattner722272d2009-03-31 22:11:05 +0000587void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
588 assert(List && "Handle list is null?");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000589
Chris Lattner722272d2009-03-31 22:11:05 +0000590 // Splice ourselves into the list.
591 Next = *List;
592 *List = this;
593 setPrevPtr(List);
594 if (Next) {
595 Next->setPrevPtr(&Next);
Bill Wendling5252c432012-04-08 10:16:43 +0000596 assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
Chris Lattner722272d2009-03-31 22:11:05 +0000597 }
598}
599
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000600void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
601 assert(List && "Must insert after existing node");
602
603 Next = List->Next;
604 setPrevPtr(&List->Next);
605 List->Next = this;
606 if (Next)
607 Next->setPrevPtr(&Next);
608}
609
Chris Lattner722272d2009-03-31 22:11:05 +0000610/// AddToUseList - Add this ValueHandle to the use list for VP.
611void ValueHandleBase::AddToUseList() {
Bill Wendling5252c432012-04-08 10:16:43 +0000612 assert(VP.getPointer() && "Null pointer doesn't have a use list!");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000613
Bill Wendling5252c432012-04-08 10:16:43 +0000614 LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000615
Bill Wendling5252c432012-04-08 10:16:43 +0000616 if (VP.getPointer()->HasValueHandle) {
Chris Lattner722272d2009-03-31 22:11:05 +0000617 // If this value already has a ValueHandle, then it must be in the
618 // ValueHandles map already.
Bill Wendling5252c432012-04-08 10:16:43 +0000619 ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
Stephen Hinesdce4a402014-05-29 02:49:00 -0700620 assert(Entry && "Value doesn't have any handles?");
Owen Andersonf2aac282009-06-17 17:36:57 +0000621 AddToExistingUseList(&Entry);
Owen Andersonf2aac282009-06-17 17:36:57 +0000622 return;
Chris Lattner722272d2009-03-31 22:11:05 +0000623 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000624
Chris Lattner722272d2009-03-31 22:11:05 +0000625 // Ok, it doesn't have any handles yet, so we must insert it into the
626 // DenseMap. However, doing this insertion could cause the DenseMap to
627 // reallocate itself, which would invalidate all of the PrevP pointers that
628 // point into the old table. Handle this by checking for reallocation and
629 // updating the stale pointers only if needed.
Owen Anderson4d919432009-08-18 18:28:58 +0000630 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
Chris Lattner722272d2009-03-31 22:11:05 +0000631 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000632
Bill Wendling5252c432012-04-08 10:16:43 +0000633 ValueHandleBase *&Entry = Handles[VP.getPointer()];
Stephen Hinesdce4a402014-05-29 02:49:00 -0700634 assert(!Entry && "Value really did already have handles?");
Chris Lattner722272d2009-03-31 22:11:05 +0000635 AddToExistingUseList(&Entry);
Bill Wendling5252c432012-04-08 10:16:43 +0000636 VP.getPointer()->HasValueHandle = true;
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000637
Chris Lattner722272d2009-03-31 22:11:05 +0000638 // If reallocation didn't happen or if this was the first insertion, don't
639 // walk the table.
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000640 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Owen Andersonf2aac282009-06-17 17:36:57 +0000641 Handles.size() == 1) {
Chris Lattner722272d2009-03-31 22:11:05 +0000642 return;
Owen Andersonf2aac282009-06-17 17:36:57 +0000643 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000644
Chris Lattner722272d2009-03-31 22:11:05 +0000645 // Okay, reallocation did happen. Fix the Prev Pointers.
Owen Anderson4d919432009-08-18 18:28:58 +0000646 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
647 E = Handles.end(); I != E; ++I) {
Bill Wendling5252c432012-04-08 10:16:43 +0000648 assert(I->second && I->first == I->second->VP.getPointer() &&
649 "List invariant broken!");
Chris Lattner722272d2009-03-31 22:11:05 +0000650 I->second->setPrevPtr(&I->second);
651 }
652}
653
654/// RemoveFromUseList - Remove this ValueHandle from its current use list.
655void ValueHandleBase::RemoveFromUseList() {
Bill Wendling5252c432012-04-08 10:16:43 +0000656 assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
657 "Pointer doesn't have a use list!");
Chris Lattner722272d2009-03-31 22:11:05 +0000658
659 // Unlink this from its use list.
660 ValueHandleBase **PrevPtr = getPrevPtr();
661 assert(*PrevPtr == this && "List invariant broken");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000662
Chris Lattner722272d2009-03-31 22:11:05 +0000663 *PrevPtr = Next;
664 if (Next) {
665 assert(Next->getPrevPtr() == &Next && "List invariant broken");
666 Next->setPrevPtr(PrevPtr);
667 return;
668 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000669
Chris Lattner722272d2009-03-31 22:11:05 +0000670 // If the Next pointer was null, then it is possible that this was the last
671 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
672 // map.
Bill Wendling5252c432012-04-08 10:16:43 +0000673 LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
Owen Anderson4d919432009-08-18 18:28:58 +0000674 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
Chris Lattner722272d2009-03-31 22:11:05 +0000675 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
Bill Wendling5252c432012-04-08 10:16:43 +0000676 Handles.erase(VP.getPointer());
677 VP.getPointer()->HasValueHandle = false;
Chris Lattner722272d2009-03-31 22:11:05 +0000678 }
679}
680
681
682void ValueHandleBase::ValueIsDeleted(Value *V) {
683 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
684
685 // Get the linked list base, which is guaranteed to exist since the
686 // HasValueHandle flag is set.
Owen Anderson4d919432009-08-18 18:28:58 +0000687 LLVMContextImpl *pImpl = V->getContext().pImpl;
688 ValueHandleBase *Entry = pImpl->ValueHandles[V];
Chris Lattner722272d2009-03-31 22:11:05 +0000689 assert(Entry && "Value bit set but no entries exist");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000690
Duncan Sandsffe8c882010-07-24 12:09:22 +0000691 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
692 // and remove themselves from the list without breaking our iteration. This
693 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
694 // Note that we deliberately do not the support the case when dropping a value
695 // handle results in a new value handle being permanently added to the list
696 // (as might occur in theory for CallbackVH's): the new value handle will not
Duncan Sands2c110462010-07-27 06:53:14 +0000697 // be processed and the checking code will mete out righteous punishment if
Duncan Sandsffe8c882010-07-24 12:09:22 +0000698 // the handle is still present once we have finished processing all the other
699 // value handles (it is fine to momentarily add then remove a value handle).
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000700 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
701 Iterator.RemoveFromUseList();
702 Iterator.AddToExistingUseListAfter(Entry);
703 assert(Entry->Next == &Iterator && "Loop invariant broken.");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000704
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000705 switch (Entry->getKind()) {
Chris Lattner722272d2009-03-31 22:11:05 +0000706 case Assert:
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000707 break;
Daniel Dunbare5b18362009-09-22 02:02:33 +0000708 case Tracking:
709 // Mark that this value has been deleted by setting it to an invalid Value
710 // pointer.
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000711 Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
Daniel Dunbare5b18362009-09-22 02:02:33 +0000712 break;
Chris Lattner722272d2009-03-31 22:11:05 +0000713 case Weak:
714 // Weak just goes to null, which will unlink it from the list.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700715 Entry->operator=(nullptr);
Chris Lattner722272d2009-03-31 22:11:05 +0000716 break;
717 case Callback:
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000718 // Forward to the subclass's implementation.
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000719 static_cast<CallbackVH*>(Entry)->deleted();
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000720 break;
Chris Lattner722272d2009-03-31 22:11:05 +0000721 }
722 }
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000723
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000724 // All callbacks, weak references, and assertingVHs should be dropped by now.
725 if (V->HasValueHandle) {
726#ifndef NDEBUG // Only in +Asserts mode...
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000727 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000728 << "\n";
729 if (pImpl->ValueHandles[V]->getKind() == Assert)
730 llvm_unreachable("An asserting value handle still pointed to this"
731 " value!");
732
733#endif
734 llvm_unreachable("All references to V were not removed?");
735 }
Chris Lattner722272d2009-03-31 22:11:05 +0000736}
737
738
739void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
740 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
741 assert(Old != New && "Changing value into itself!");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000742
Chris Lattner722272d2009-03-31 22:11:05 +0000743 // Get the linked list base, which is guaranteed to exist since the
744 // HasValueHandle flag is set.
Owen Anderson4d919432009-08-18 18:28:58 +0000745 LLVMContextImpl *pImpl = Old->getContext().pImpl;
746 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
747
Chris Lattner722272d2009-03-31 22:11:05 +0000748 assert(Entry && "Value bit set but no entries exist");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000749
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000750 // We use a local ValueHandleBase as an iterator so that
751 // ValueHandles can add and remove themselves from the list without
752 // breaking our iteration. This is not really an AssertingVH; we
753 // just have to give ValueHandleBase some kind.
754 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
755 Iterator.RemoveFromUseList();
756 Iterator.AddToExistingUseListAfter(Entry);
757 assert(Entry->Next == &Iterator && "Loop invariant broken.");
Daniel Dunbarce99a6e2009-09-20 04:03:34 +0000758
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000759 switch (Entry->getKind()) {
Chris Lattner722272d2009-03-31 22:11:05 +0000760 case Assert:
761 // Asserting handle does not follow RAUW implicitly.
762 break;
Daniel Dunbare5b18362009-09-22 02:02:33 +0000763 case Tracking:
764 // Tracking goes to new value like a WeakVH. Note that this may make it
765 // something incompatible with its templated type. We don't want to have a
766 // virtual (or inline) interface to handle this though, so instead we make
Daniel Dunbar460a7862009-09-22 10:30:34 +0000767 // the TrackingVH accessors guarantee that a client never sees this value.
Daniel Dunbare5b18362009-09-22 02:02:33 +0000768
769 // FALLTHROUGH
Chris Lattner722272d2009-03-31 22:11:05 +0000770 case Weak:
771 // Weak goes to the new value, which will unlink it from Old's list.
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000772 Entry->operator=(New);
Chris Lattner722272d2009-03-31 22:11:05 +0000773 break;
774 case Callback:
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000775 // Forward to the subclass's implementation.
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000776 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000777 break;
Chris Lattner722272d2009-03-31 22:11:05 +0000778 }
779 }
Duncan Sands2c110462010-07-27 06:53:14 +0000780
781#ifndef NDEBUG
782 // If any new tracking or weak value handles were added while processing the
783 // list, then complain about it now.
784 if (Old->HasValueHandle)
785 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
786 switch (Entry->getKind()) {
787 case Tracking:
788 case Weak:
789 dbgs() << "After RAUW from " << *Old->getType() << " %"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000790 << Old->getName() << " to " << *New->getType() << " %"
791 << New->getName() << "\n";
Duncan Sands2c110462010-07-27 06:53:14 +0000792 llvm_unreachable("A tracking or weak value handle still pointed to the"
793 " old value!\n");
794 default:
795 break;
796 }
797#endif
Chris Lattner722272d2009-03-31 22:11:05 +0000798}
799
Juergen Ributzka35436252013-11-19 00:57:56 +0000800// Pin the vtable to this file.
801void CallbackVH::anchor() {}