xref: /llvm-project-15.0.7/llvm/lib/IR/Value.cpp (revision 257a3536)
1ef860a24SChandler Carruth //===-- Value.cpp - Implement the Value class -----------------------------===//
2ef860a24SChandler Carruth //
3ef860a24SChandler Carruth //                     The LLVM Compiler Infrastructure
4ef860a24SChandler Carruth //
5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source
6ef860a24SChandler Carruth // License. See LICENSE.TXT for details.
7ef860a24SChandler Carruth //
8ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
9ef860a24SChandler Carruth //
10ef860a24SChandler Carruth // This file implements the Value, ValueHandle, and User classes.
11ef860a24SChandler Carruth //
12ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
13ef860a24SChandler Carruth 
149fb823bbSChandler Carruth #include "llvm/IR/Value.h"
15ef860a24SChandler Carruth #include "LLVMContextImpl.h"
16ef860a24SChandler Carruth #include "llvm/ADT/DenseMap.h"
17ef860a24SChandler Carruth #include "llvm/ADT/SmallString.h"
18b0407ba0SHal Finkel #include "llvm/IR/CallSite.h"
199fb823bbSChandler Carruth #include "llvm/IR/Constant.h"
209fb823bbSChandler Carruth #include "llvm/IR/Constants.h"
212e42c34dSHal Finkel #include "llvm/IR/DataLayout.h"
229fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h"
2303eb0de9SChandler Carruth #include "llvm/IR/GetElementPtrTypeIterator.h"
249fb823bbSChandler Carruth #include "llvm/IR/InstrTypes.h"
259fb823bbSChandler Carruth #include "llvm/IR/Instructions.h"
26a7343d65SRamkumar Ramachandra #include "llvm/IR/IntrinsicInst.h"
279fb823bbSChandler Carruth #include "llvm/IR/Module.h"
289fb823bbSChandler Carruth #include "llvm/IR/Operator.h"
29a7343d65SRamkumar Ramachandra #include "llvm/IR/Statepoint.h"
304220e9c1SChandler Carruth #include "llvm/IR/ValueHandle.h"
319fb823bbSChandler Carruth #include "llvm/IR/ValueSymbolTable.h"
32ef860a24SChandler Carruth #include "llvm/Support/Debug.h"
33ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h"
34ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h"
35799003bfSBenjamin Kramer #include "llvm/Support/raw_ostream.h"
36ef860a24SChandler Carruth #include <algorithm>
37ef860a24SChandler Carruth using namespace llvm;
38ef860a24SChandler Carruth 
39ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
40ef860a24SChandler Carruth //                                Value Class
41ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
42ef860a24SChandler Carruth static inline Type *checkType(Type *Ty) {
43ef860a24SChandler Carruth   assert(Ty && "Value defined with a null type: Error!");
4416bf89ecSReid Kleckner   return Ty;
45ef860a24SChandler Carruth }
46ef860a24SChandler Carruth 
47ef860a24SChandler Carruth Value::Value(Type *ty, unsigned scid)
487349ab93SOwen Anderson     : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid),
497349ab93SOwen Anderson       HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
50b4eede2cSPete Cooper       NumUserOperands(0), IsUsedByMD(false), HasName(false) {
51ef860a24SChandler Carruth   // FIXME: Why isn't this in the subclass gunk??
52ef860a24SChandler Carruth   // Note, we cannot call isa<CallInst> before the CallInst has been
53ef860a24SChandler Carruth   // constructed.
54ef860a24SChandler Carruth   if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
55ef860a24SChandler Carruth     assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
56ef860a24SChandler Carruth            "invalid CallInst type!");
57ef860a24SChandler Carruth   else if (SubclassID != BasicBlockVal &&
58ef860a24SChandler Carruth            (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
59ef860a24SChandler Carruth     assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
60ef860a24SChandler Carruth            "Cannot create non-first-class values except for constants!");
61ef860a24SChandler Carruth }
62ef860a24SChandler Carruth 
63ef860a24SChandler Carruth Value::~Value() {
64ef860a24SChandler Carruth   // Notify all ValueHandles (if present) that this value is going away.
65ef860a24SChandler Carruth   if (HasValueHandle)
66ef860a24SChandler Carruth     ValueHandleBase::ValueIsDeleted(this);
675bf8fef5SDuncan P. N. Exon Smith   if (isUsedByMetadata())
685bf8fef5SDuncan P. N. Exon Smith     ValueAsMetadata::handleDeletion(this);
69ef860a24SChandler Carruth 
70ef860a24SChandler Carruth #ifndef NDEBUG      // Only in -g mode...
71ef860a24SChandler Carruth   // Check to make sure that there are no uses of this value that are still
72ef860a24SChandler Carruth   // around when the value is destroyed.  If there are, then we have a dangling
73b93fb82bSAndrew Kaylor   // reference and something is wrong.  This code is here to print out where
74b93fb82bSAndrew Kaylor   // the value is still being referenced.
75ef860a24SChandler Carruth   //
76ef860a24SChandler Carruth   if (!use_empty()) {
77ef860a24SChandler Carruth     dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
78b93fb82bSAndrew Kaylor     for (auto *U : users())
79b93fb82bSAndrew Kaylor       dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
80ef860a24SChandler Carruth   }
81ef860a24SChandler Carruth #endif
82ef860a24SChandler Carruth   assert(use_empty() && "Uses remain when a value is destroyed!");
83ef860a24SChandler Carruth 
84ef860a24SChandler Carruth   // If this value is named, destroy the name.  This should not be in a symtab
85ef860a24SChandler Carruth   // at this point.
865bf8fef5SDuncan P. N. Exon Smith   destroyValueName();
87ef860a24SChandler Carruth }
88ef860a24SChandler Carruth 
895bf8fef5SDuncan P. N. Exon Smith void Value::destroyValueName() {
905bf8fef5SDuncan P. N. Exon Smith   ValueName *Name = getValueName();
915bf8fef5SDuncan P. N. Exon Smith   if (Name)
925bf8fef5SDuncan P. N. Exon Smith     Name->Destroy();
935bf8fef5SDuncan P. N. Exon Smith   setValueName(nullptr);
945bf8fef5SDuncan P. N. Exon Smith }
955bf8fef5SDuncan P. N. Exon Smith 
96ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const {
97ef860a24SChandler Carruth   const_use_iterator UI = use_begin(), E = use_end();
98ef860a24SChandler Carruth 
99ef860a24SChandler Carruth   for (; N; --N, ++UI)
100ef860a24SChandler Carruth     if (UI == E) return false;  // Too few.
101ef860a24SChandler Carruth   return UI == E;
102ef860a24SChandler Carruth }
103ef860a24SChandler Carruth 
104ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const {
105ef860a24SChandler Carruth   const_use_iterator UI = use_begin(), E = use_end();
106ef860a24SChandler Carruth 
107ef860a24SChandler Carruth   for (; N; --N, ++UI)
108ef860a24SChandler Carruth     if (UI == E) return false;  // Too few.
109ef860a24SChandler Carruth 
110ef860a24SChandler Carruth   return true;
111ef860a24SChandler Carruth }
112ef860a24SChandler Carruth 
113ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
1140925b24dSJakob Stoklund Olesen   // This can be computed either by scanning the instructions in BB, or by
1150925b24dSJakob Stoklund Olesen   // scanning the use list of this Value. Both lists can be very long, but
1160925b24dSJakob Stoklund Olesen   // usually one is quite short.
1170925b24dSJakob Stoklund Olesen   //
1180925b24dSJakob Stoklund Olesen   // Scan both lists simultaneously until one is exhausted. This limits the
1190925b24dSJakob Stoklund Olesen   // search to the shorter list.
1200925b24dSJakob Stoklund Olesen   BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
121cdf47884SChandler Carruth   const_user_iterator UI = user_begin(), UE = user_end();
1220925b24dSJakob Stoklund Olesen   for (; BI != BE && UI != UE; ++BI, ++UI) {
1230925b24dSJakob Stoklund Olesen     // Scan basic block: Check if this Value is used by the instruction at BI.
1240925b24dSJakob Stoklund Olesen     if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end())
125ef860a24SChandler Carruth       return true;
1260925b24dSJakob Stoklund Olesen     // Scan use list: Check if the use at UI is in BB.
1270925b24dSJakob Stoklund Olesen     const Instruction *User = dyn_cast<Instruction>(*UI);
128ef860a24SChandler Carruth     if (User && User->getParent() == BB)
129ef860a24SChandler Carruth       return true;
130ef860a24SChandler Carruth   }
131ef860a24SChandler Carruth   return false;
132ef860a24SChandler Carruth }
133ef860a24SChandler Carruth 
134ef860a24SChandler Carruth unsigned Value::getNumUses() const {
135ef860a24SChandler Carruth   return (unsigned)std::distance(use_begin(), use_end());
136ef860a24SChandler Carruth }
137ef860a24SChandler Carruth 
138ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
139c620761cSCraig Topper   ST = nullptr;
140ef860a24SChandler Carruth   if (Instruction *I = dyn_cast<Instruction>(V)) {
141ef860a24SChandler Carruth     if (BasicBlock *P = I->getParent())
142ef860a24SChandler Carruth       if (Function *PP = P->getParent())
143ef860a24SChandler Carruth         ST = &PP->getValueSymbolTable();
144ef860a24SChandler Carruth   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
145ef860a24SChandler Carruth     if (Function *P = BB->getParent())
146ef860a24SChandler Carruth       ST = &P->getValueSymbolTable();
147ef860a24SChandler Carruth   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
148ef860a24SChandler Carruth     if (Module *P = GV->getParent())
149ef860a24SChandler Carruth       ST = &P->getValueSymbolTable();
150ef860a24SChandler Carruth   } else if (Argument *A = dyn_cast<Argument>(V)) {
151ef860a24SChandler Carruth     if (Function *P = A->getParent())
152ef860a24SChandler Carruth       ST = &P->getValueSymbolTable();
1535bf8fef5SDuncan P. N. Exon Smith   } else {
154ef860a24SChandler Carruth     assert(isa<Constant>(V) && "Unknown value type!");
155ef860a24SChandler Carruth     return true;  // no name is setable for this.
156ef860a24SChandler Carruth   }
157ef860a24SChandler Carruth   return false;
158ef860a24SChandler Carruth }
159ef860a24SChandler Carruth 
1607349ab93SOwen Anderson ValueName *Value::getValueName() const {
1617349ab93SOwen Anderson   if (!HasName) return nullptr;
1627349ab93SOwen Anderson 
1637349ab93SOwen Anderson   LLVMContext &Ctx = getContext();
1647349ab93SOwen Anderson   auto I = Ctx.pImpl->ValueNames.find(this);
1657349ab93SOwen Anderson   assert(I != Ctx.pImpl->ValueNames.end() &&
1667349ab93SOwen Anderson          "No name entry found!");
1677349ab93SOwen Anderson 
1687349ab93SOwen Anderson   return I->second;
1697349ab93SOwen Anderson }
1707349ab93SOwen Anderson 
1717349ab93SOwen Anderson void Value::setValueName(ValueName *VN) {
1727349ab93SOwen Anderson   LLVMContext &Ctx = getContext();
1737349ab93SOwen Anderson 
1747349ab93SOwen Anderson   assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
1757349ab93SOwen Anderson          "HasName bit out of sync!");
1767349ab93SOwen Anderson 
1777349ab93SOwen Anderson   if (!VN) {
1787349ab93SOwen Anderson     if (HasName)
1797349ab93SOwen Anderson       Ctx.pImpl->ValueNames.erase(this);
1807349ab93SOwen Anderson     HasName = false;
1817349ab93SOwen Anderson     return;
1827349ab93SOwen Anderson   }
1837349ab93SOwen Anderson 
1847349ab93SOwen Anderson   HasName = true;
1857349ab93SOwen Anderson   Ctx.pImpl->ValueNames[this] = VN;
1867349ab93SOwen Anderson }
1877349ab93SOwen Anderson 
188ef860a24SChandler Carruth StringRef Value::getName() const {
189ef860a24SChandler Carruth   // Make sure the empty string is still a C string. For historical reasons,
190ef860a24SChandler Carruth   // some clients want to call .data() on the result and expect it to be null
191ef860a24SChandler Carruth   // terminated.
1927349ab93SOwen Anderson   if (!hasName())
1935bf8fef5SDuncan P. N. Exon Smith     return StringRef("", 0);
1945bf8fef5SDuncan P. N. Exon Smith   return getValueName()->getKey();
195ef860a24SChandler Carruth }
196ef860a24SChandler Carruth 
197a7c0c18cSPete Cooper void Value::setNameImpl(const Twine &NewName) {
198ef860a24SChandler Carruth   // Fast path for common IRBuilder case of setName("") when there is no name.
199ef860a24SChandler Carruth   if (NewName.isTriviallyEmpty() && !hasName())
200ef860a24SChandler Carruth     return;
201ef860a24SChandler Carruth 
202ef860a24SChandler Carruth   SmallString<256> NameData;
203ef860a24SChandler Carruth   StringRef NameRef = NewName.toStringRef(NameData);
20409d689f9SRafael Espindola   assert(NameRef.find_first_of(0) == StringRef::npos &&
20509d689f9SRafael Espindola          "Null bytes are not allowed in names");
206ef860a24SChandler Carruth 
207ef860a24SChandler Carruth   // Name isn't changing?
208ef860a24SChandler Carruth   if (getName() == NameRef)
209ef860a24SChandler Carruth     return;
210ef860a24SChandler Carruth 
211ef860a24SChandler Carruth   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
212ef860a24SChandler Carruth 
213ef860a24SChandler Carruth   // Get the symbol table to update for this object.
214ef860a24SChandler Carruth   ValueSymbolTable *ST;
215ef860a24SChandler Carruth   if (getSymTab(this, ST))
216ef860a24SChandler Carruth     return;  // Cannot set a name on this value (e.g. constant).
217ef860a24SChandler Carruth 
218ef860a24SChandler Carruth   if (!ST) { // No symbol table to update?  Just do the change.
219ef860a24SChandler Carruth     if (NameRef.empty()) {
220ef860a24SChandler Carruth       // Free the name for this value.
2215bf8fef5SDuncan P. N. Exon Smith       destroyValueName();
222ef860a24SChandler Carruth       return;
223ef860a24SChandler Carruth     }
224ef860a24SChandler Carruth 
225ef860a24SChandler Carruth     // NOTE: Could optimize for the case the name is shrinking to not deallocate
226ef860a24SChandler Carruth     // then reallocated.
2275bf8fef5SDuncan P. N. Exon Smith     destroyValueName();
228ef860a24SChandler Carruth 
229ef860a24SChandler Carruth     // Create the new name.
2305bf8fef5SDuncan P. N. Exon Smith     setValueName(ValueName::Create(NameRef));
2315bf8fef5SDuncan P. N. Exon Smith     getValueName()->setValue(this);
232ef860a24SChandler Carruth     return;
233ef860a24SChandler Carruth   }
234ef860a24SChandler Carruth 
235ef860a24SChandler Carruth   // NOTE: Could optimize for the case the name is shrinking to not deallocate
236ef860a24SChandler Carruth   // then reallocated.
237ef860a24SChandler Carruth   if (hasName()) {
238ef860a24SChandler Carruth     // Remove old name.
2395bf8fef5SDuncan P. N. Exon Smith     ST->removeValueName(getValueName());
2405bf8fef5SDuncan P. N. Exon Smith     destroyValueName();
241ef860a24SChandler Carruth 
242ef860a24SChandler Carruth     if (NameRef.empty())
243ef860a24SChandler Carruth       return;
244ef860a24SChandler Carruth   }
245ef860a24SChandler Carruth 
246ef860a24SChandler Carruth   // Name is changing to something new.
2475bf8fef5SDuncan P. N. Exon Smith   setValueName(ST->createValueName(NameRef, this));
248ef860a24SChandler Carruth }
249ef860a24SChandler Carruth 
250a7c0c18cSPete Cooper void Value::setName(const Twine &NewName) {
251a7c0c18cSPete Cooper   setNameImpl(NewName);
252a7c0c18cSPete Cooper   if (Function *F = dyn_cast<Function>(this))
253a7c0c18cSPete Cooper     F->recalculateIntrinsicID();
254a7c0c18cSPete Cooper }
255a7c0c18cSPete Cooper 
256ef860a24SChandler Carruth void Value::takeName(Value *V) {
257c620761cSCraig Topper   ValueSymbolTable *ST = nullptr;
258ef860a24SChandler Carruth   // If this value has a name, drop it.
259ef860a24SChandler Carruth   if (hasName()) {
260ef860a24SChandler Carruth     // Get the symtab this is in.
261ef860a24SChandler Carruth     if (getSymTab(this, ST)) {
262ef860a24SChandler Carruth       // We can't set a name on this value, but we need to clear V's name if
263ef860a24SChandler Carruth       // it has one.
264ef860a24SChandler Carruth       if (V->hasName()) V->setName("");
265ef860a24SChandler Carruth       return;  // Cannot set a name on this value (e.g. constant).
266ef860a24SChandler Carruth     }
267ef860a24SChandler Carruth 
268ef860a24SChandler Carruth     // Remove old name.
269ef860a24SChandler Carruth     if (ST)
2705bf8fef5SDuncan P. N. Exon Smith       ST->removeValueName(getValueName());
2715bf8fef5SDuncan P. N. Exon Smith     destroyValueName();
272ef860a24SChandler Carruth   }
273ef860a24SChandler Carruth 
274ef860a24SChandler Carruth   // Now we know that this has no name.
275ef860a24SChandler Carruth 
276ef860a24SChandler Carruth   // If V has no name either, we're done.
277ef860a24SChandler Carruth   if (!V->hasName()) return;
278ef860a24SChandler Carruth 
279ef860a24SChandler Carruth   // Get this's symtab if we didn't before.
280ef860a24SChandler Carruth   if (!ST) {
281ef860a24SChandler Carruth     if (getSymTab(this, ST)) {
282ef860a24SChandler Carruth       // Clear V's name.
283ef860a24SChandler Carruth       V->setName("");
284ef860a24SChandler Carruth       return;  // Cannot set a name on this value (e.g. constant).
285ef860a24SChandler Carruth     }
286ef860a24SChandler Carruth   }
287ef860a24SChandler Carruth 
288ef860a24SChandler Carruth   // Get V's ST, this should always succed, because V has a name.
289ef860a24SChandler Carruth   ValueSymbolTable *VST;
290ef860a24SChandler Carruth   bool Failure = getSymTab(V, VST);
291ef860a24SChandler Carruth   assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
292ef860a24SChandler Carruth 
293ef860a24SChandler Carruth   // If these values are both in the same symtab, we can do this very fast.
294ef860a24SChandler Carruth   // This works even if both values have no symtab yet.
295ef860a24SChandler Carruth   if (ST == VST) {
296ef860a24SChandler Carruth     // Take the name!
2975bf8fef5SDuncan P. N. Exon Smith     setValueName(V->getValueName());
2985bf8fef5SDuncan P. N. Exon Smith     V->setValueName(nullptr);
2995bf8fef5SDuncan P. N. Exon Smith     getValueName()->setValue(this);
300ef860a24SChandler Carruth     return;
301ef860a24SChandler Carruth   }
302ef860a24SChandler Carruth 
303ef860a24SChandler Carruth   // Otherwise, things are slightly more complex.  Remove V's name from VST and
304ef860a24SChandler Carruth   // then reinsert it into ST.
305ef860a24SChandler Carruth 
306ef860a24SChandler Carruth   if (VST)
3075bf8fef5SDuncan P. N. Exon Smith     VST->removeValueName(V->getValueName());
3085bf8fef5SDuncan P. N. Exon Smith   setValueName(V->getValueName());
3095bf8fef5SDuncan P. N. Exon Smith   V->setValueName(nullptr);
3105bf8fef5SDuncan P. N. Exon Smith   getValueName()->setValue(this);
311ef860a24SChandler Carruth 
312ef860a24SChandler Carruth   if (ST)
313ef860a24SChandler Carruth     ST->reinsertValue(this);
314ef860a24SChandler Carruth }
315ef860a24SChandler Carruth 
3167e2b7567SRafael Espindola #ifndef NDEBUG
317*257a3536SRafael Espindola void Value::assertModuleIsMaterialized() const {
318*257a3536SRafael Espindola   const GlobalValue *GV = dyn_cast<GlobalValue>(this);
319*257a3536SRafael Espindola   if (!GV)
320*257a3536SRafael Espindola     return;
321*257a3536SRafael Espindola   const Module *M = GV->getParent();
322*257a3536SRafael Espindola   if (!M)
323*257a3536SRafael Espindola     return;
324*257a3536SRafael Espindola   assert(M->isMaterialized());
325*257a3536SRafael Espindola }
326*257a3536SRafael Espindola 
32771b7b68bSCraig Topper static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
3287e2b7567SRafael Espindola                      Constant *C) {
32970573dcdSDavid Blaikie   if (!Cache.insert(Expr).second)
3307e2b7567SRafael Espindola     return false;
3317e2b7567SRafael Espindola 
3327e2b7567SRafael Espindola   for (auto &O : Expr->operands()) {
3337e2b7567SRafael Espindola     if (O == C)
3347e2b7567SRafael Espindola       return true;
3357e2b7567SRafael Espindola     auto *CE = dyn_cast<ConstantExpr>(O);
3367e2b7567SRafael Espindola     if (!CE)
3377e2b7567SRafael Espindola       continue;
3387e2b7567SRafael Espindola     if (contains(Cache, CE, C))
3397e2b7567SRafael Espindola       return true;
3407e2b7567SRafael Espindola   }
3417e2b7567SRafael Espindola   return false;
3427e2b7567SRafael Espindola }
3437e2b7567SRafael Espindola 
3447e2b7567SRafael Espindola static bool contains(Value *Expr, Value *V) {
3457e2b7567SRafael Espindola   if (Expr == V)
3467e2b7567SRafael Espindola     return true;
3477e2b7567SRafael Espindola 
3487e2b7567SRafael Espindola   auto *C = dyn_cast<Constant>(V);
3497e2b7567SRafael Espindola   if (!C)
3507e2b7567SRafael Espindola     return false;
3517e2b7567SRafael Espindola 
3527e2b7567SRafael Espindola   auto *CE = dyn_cast<ConstantExpr>(Expr);
3537e2b7567SRafael Espindola   if (!CE)
3547e2b7567SRafael Espindola     return false;
3557e2b7567SRafael Espindola 
3567e2b7567SRafael Espindola   SmallPtrSet<ConstantExpr *, 4> Cache;
3577e2b7567SRafael Espindola   return contains(Cache, CE, C);
3587e2b7567SRafael Espindola }
3597e2b7567SRafael Espindola #endif
360ef860a24SChandler Carruth 
361ef860a24SChandler Carruth void Value::replaceAllUsesWith(Value *New) {
362ef860a24SChandler Carruth   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
3637e2b7567SRafael Espindola   assert(!contains(New, this) &&
3647e2b7567SRafael Espindola          "this->replaceAllUsesWith(expr(this)) is NOT valid!");
365ef860a24SChandler Carruth   assert(New->getType() == getType() &&
366ef860a24SChandler Carruth          "replaceAllUses of value with new value of different type!");
367ef860a24SChandler Carruth 
368ef860a24SChandler Carruth   // Notify all ValueHandles (if present) that this value is going away.
369ef860a24SChandler Carruth   if (HasValueHandle)
370ef860a24SChandler Carruth     ValueHandleBase::ValueIsRAUWd(this, New);
3715bf8fef5SDuncan P. N. Exon Smith   if (isUsedByMetadata())
3725bf8fef5SDuncan P. N. Exon Smith     ValueAsMetadata::handleRAUW(this, New);
373ef860a24SChandler Carruth 
374ef860a24SChandler Carruth   while (!use_empty()) {
375ef860a24SChandler Carruth     Use &U = *UseList;
376ef860a24SChandler Carruth     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
377ef860a24SChandler Carruth     // constant because they are uniqued.
3786b238633SRafael Espindola     if (auto *C = dyn_cast<Constant>(U.getUser())) {
379ef860a24SChandler Carruth       if (!isa<GlobalValue>(C)) {
3805815b1fdSPete Cooper         C->handleOperandChange(this, New, &U);
381ef860a24SChandler Carruth         continue;
382ef860a24SChandler Carruth       }
383ef860a24SChandler Carruth     }
384ef860a24SChandler Carruth 
385ef860a24SChandler Carruth     U.set(New);
386ef860a24SChandler Carruth   }
387ef860a24SChandler Carruth 
388ef860a24SChandler Carruth   if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
389ef860a24SChandler Carruth     BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
390ef860a24SChandler Carruth }
391ef860a24SChandler Carruth 
392ec6217c9SGerolf Hoflehner // Like replaceAllUsesWith except it does not handle constants or basic blocks.
393ec6217c9SGerolf Hoflehner // This routine leaves uses within BB.
394ec6217c9SGerolf Hoflehner void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
395ec6217c9SGerolf Hoflehner   assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
396ec6217c9SGerolf Hoflehner   assert(!contains(New, this) &&
397ec6217c9SGerolf Hoflehner          "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
398ec6217c9SGerolf Hoflehner   assert(New->getType() == getType() &&
399ec6217c9SGerolf Hoflehner          "replaceUses of value with new value of different type!");
400ec6217c9SGerolf Hoflehner   assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
401ec6217c9SGerolf Hoflehner 
402ec6217c9SGerolf Hoflehner   use_iterator UI = use_begin(), E = use_end();
403ec6217c9SGerolf Hoflehner   for (; UI != E;) {
404ec6217c9SGerolf Hoflehner     Use &U = *UI;
405ec6217c9SGerolf Hoflehner     ++UI;
406ec6217c9SGerolf Hoflehner     auto *Usr = dyn_cast<Instruction>(U.getUser());
407ec6217c9SGerolf Hoflehner     if (Usr && Usr->getParent() == BB)
408ec6217c9SGerolf Hoflehner       continue;
409ec6217c9SGerolf Hoflehner     U.set(New);
410ec6217c9SGerolf Hoflehner   }
411ec6217c9SGerolf Hoflehner   return;
412ec6217c9SGerolf Hoflehner }
413ec6217c9SGerolf Hoflehner 
414ef860a24SChandler Carruth namespace {
415ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers.
416ef860a24SChandler Carruth enum PointerStripKind {
417ef860a24SChandler Carruth   PSK_ZeroIndices,
418c229a4ffSRafael Espindola   PSK_ZeroIndicesAndAliases,
419ef860a24SChandler Carruth   PSK_InBoundsConstantIndices,
420ef860a24SChandler Carruth   PSK_InBounds
421ef860a24SChandler Carruth };
422ef860a24SChandler Carruth 
423ef860a24SChandler Carruth template <PointerStripKind StripKind>
424ef860a24SChandler Carruth static Value *stripPointerCastsAndOffsets(Value *V) {
425ef860a24SChandler Carruth   if (!V->getType()->isPointerTy())
426ef860a24SChandler Carruth     return V;
427ef860a24SChandler Carruth 
428ef860a24SChandler Carruth   // Even though we don't look through PHI nodes, we could be called on an
429ef860a24SChandler Carruth   // instruction in an unreachable block, which may be on a cycle.
430ef860a24SChandler Carruth   SmallPtrSet<Value *, 4> Visited;
431ef860a24SChandler Carruth 
432ef860a24SChandler Carruth   Visited.insert(V);
433ef860a24SChandler Carruth   do {
434ef860a24SChandler Carruth     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
435ef860a24SChandler Carruth       switch (StripKind) {
436c229a4ffSRafael Espindola       case PSK_ZeroIndicesAndAliases:
437ef860a24SChandler Carruth       case PSK_ZeroIndices:
438ef860a24SChandler Carruth         if (!GEP->hasAllZeroIndices())
439ef860a24SChandler Carruth           return V;
440ef860a24SChandler Carruth         break;
441ef860a24SChandler Carruth       case PSK_InBoundsConstantIndices:
442ef860a24SChandler Carruth         if (!GEP->hasAllConstantIndices())
443ef860a24SChandler Carruth           return V;
444ef860a24SChandler Carruth         // fallthrough
445ef860a24SChandler Carruth       case PSK_InBounds:
446ef860a24SChandler Carruth         if (!GEP->isInBounds())
447ef860a24SChandler Carruth           return V;
448ef860a24SChandler Carruth         break;
449ef860a24SChandler Carruth       }
450ef860a24SChandler Carruth       V = GEP->getPointerOperand();
451b03bd4d9SMatt Arsenault     } else if (Operator::getOpcode(V) == Instruction::BitCast ||
452b03bd4d9SMatt Arsenault                Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
453ef860a24SChandler Carruth       V = cast<Operator>(V)->getOperand(0);
454ef860a24SChandler Carruth     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
455c229a4ffSRafael Espindola       if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
456ef860a24SChandler Carruth         return V;
457ef860a24SChandler Carruth       V = GA->getAliasee();
458ef860a24SChandler Carruth     } else {
459ef860a24SChandler Carruth       return V;
460ef860a24SChandler Carruth     }
461ef860a24SChandler Carruth     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
46270573dcdSDavid Blaikie   } while (Visited.insert(V).second);
463ef860a24SChandler Carruth 
464ef860a24SChandler Carruth   return V;
465ef860a24SChandler Carruth }
466ef860a24SChandler Carruth } // namespace
467ef860a24SChandler Carruth 
468ef860a24SChandler Carruth Value *Value::stripPointerCasts() {
469c229a4ffSRafael Espindola   return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
470c229a4ffSRafael Espindola }
471c229a4ffSRafael Espindola 
472c229a4ffSRafael Espindola Value *Value::stripPointerCastsNoFollowAliases() {
473ef860a24SChandler Carruth   return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
474ef860a24SChandler Carruth }
475ef860a24SChandler Carruth 
476ef860a24SChandler Carruth Value *Value::stripInBoundsConstantOffsets() {
477ef860a24SChandler Carruth   return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
478ef860a24SChandler Carruth }
479ef860a24SChandler Carruth 
480989e6308SChandler Carruth Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
481989e6308SChandler Carruth                                                         APInt &Offset) {
482989e6308SChandler Carruth   if (!getType()->isPointerTy())
483989e6308SChandler Carruth     return this;
484989e6308SChandler Carruth 
485989e6308SChandler Carruth   assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
486989e6308SChandler Carruth                                      getType())->getAddressSpace()) &&
487989e6308SChandler Carruth          "The offset must have exactly as many bits as our pointer.");
488989e6308SChandler Carruth 
489989e6308SChandler Carruth   // Even though we don't look through PHI nodes, we could be called on an
490989e6308SChandler Carruth   // instruction in an unreachable block, which may be on a cycle.
491989e6308SChandler Carruth   SmallPtrSet<Value *, 4> Visited;
492989e6308SChandler Carruth   Visited.insert(this);
493989e6308SChandler Carruth   Value *V = this;
494989e6308SChandler Carruth   do {
495989e6308SChandler Carruth     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
496989e6308SChandler Carruth       if (!GEP->isInBounds())
497989e6308SChandler Carruth         return V;
498ba689b33SChandler Carruth       APInt GEPOffset(Offset);
499ba689b33SChandler Carruth       if (!GEP->accumulateConstantOffset(DL, GEPOffset))
500989e6308SChandler Carruth         return V;
501ba689b33SChandler Carruth       Offset = GEPOffset;
502989e6308SChandler Carruth       V = GEP->getPointerOperand();
503d63df510SPhilip Reames     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
504989e6308SChandler Carruth       V = cast<Operator>(V)->getOperand(0);
505989e6308SChandler Carruth     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
506989e6308SChandler Carruth       V = GA->getAliasee();
507989e6308SChandler Carruth     } else {
508989e6308SChandler Carruth       return V;
509989e6308SChandler Carruth     }
510989e6308SChandler Carruth     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
51170573dcdSDavid Blaikie   } while (Visited.insert(V).second);
512989e6308SChandler Carruth 
513989e6308SChandler Carruth   return V;
514989e6308SChandler Carruth }
515989e6308SChandler Carruth 
516ef860a24SChandler Carruth Value *Value::stripInBoundsOffsets() {
517ef860a24SChandler Carruth   return stripPointerCastsAndOffsets<PSK_InBounds>(this);
518ef860a24SChandler Carruth }
519ef860a24SChandler Carruth 
520ef860a24SChandler Carruth Value *Value::DoPHITranslation(const BasicBlock *CurBB,
521ef860a24SChandler Carruth                                const BasicBlock *PredBB) {
522ef860a24SChandler Carruth   PHINode *PN = dyn_cast<PHINode>(this);
523ef860a24SChandler Carruth   if (PN && PN->getParent() == CurBB)
524ef860a24SChandler Carruth     return PN->getIncomingValueForBlock(PredBB);
525ef860a24SChandler Carruth   return this;
526ef860a24SChandler Carruth }
527ef860a24SChandler Carruth 
528ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); }
529ef860a24SChandler Carruth 
5303441ffe9SDuncan P. N. Exon Smith void Value::reverseUseList() {
5313441ffe9SDuncan P. N. Exon Smith   if (!UseList || !UseList->Next)
5323441ffe9SDuncan P. N. Exon Smith     // No need to reverse 0 or 1 uses.
5333441ffe9SDuncan P. N. Exon Smith     return;
5343441ffe9SDuncan P. N. Exon Smith 
5353441ffe9SDuncan P. N. Exon Smith   Use *Head = UseList;
5363441ffe9SDuncan P. N. Exon Smith   Use *Current = UseList->Next;
5373441ffe9SDuncan P. N. Exon Smith   Head->Next = nullptr;
5383441ffe9SDuncan P. N. Exon Smith   while (Current) {
5393441ffe9SDuncan P. N. Exon Smith     Use *Next = Current->Next;
5403441ffe9SDuncan P. N. Exon Smith     Current->Next = Head;
5413441ffe9SDuncan P. N. Exon Smith     Head->setPrev(&Current->Next);
5423441ffe9SDuncan P. N. Exon Smith     Head = Current;
5433441ffe9SDuncan P. N. Exon Smith     Current = Next;
5443441ffe9SDuncan P. N. Exon Smith   }
5453441ffe9SDuncan P. N. Exon Smith   UseList = Head;
5463441ffe9SDuncan P. N. Exon Smith   Head->setPrev(&UseList);
5473441ffe9SDuncan P. N. Exon Smith }
5483441ffe9SDuncan P. N. Exon Smith 
549ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
550ef860a24SChandler Carruth //                             ValueHandleBase Class
551ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
552ef860a24SChandler Carruth 
553ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
554ef860a24SChandler Carruth   assert(List && "Handle list is null?");
555ef860a24SChandler Carruth 
556ef860a24SChandler Carruth   // Splice ourselves into the list.
557ef860a24SChandler Carruth   Next = *List;
558ef860a24SChandler Carruth   *List = this;
559ef860a24SChandler Carruth   setPrevPtr(List);
560ef860a24SChandler Carruth   if (Next) {
561ef860a24SChandler Carruth     Next->setPrevPtr(&Next);
562f4ea3d3dSChandler Carruth     assert(V == Next->V && "Added to wrong list?");
563ef860a24SChandler Carruth   }
564ef860a24SChandler Carruth }
565ef860a24SChandler Carruth 
566ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
567ef860a24SChandler Carruth   assert(List && "Must insert after existing node");
568ef860a24SChandler Carruth 
569ef860a24SChandler Carruth   Next = List->Next;
570ef860a24SChandler Carruth   setPrevPtr(&List->Next);
571ef860a24SChandler Carruth   List->Next = this;
572ef860a24SChandler Carruth   if (Next)
573ef860a24SChandler Carruth     Next->setPrevPtr(&Next);
574ef860a24SChandler Carruth }
575ef860a24SChandler Carruth 
576ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() {
577f4ea3d3dSChandler Carruth   assert(V && "Null pointer doesn't have a use list!");
578ef860a24SChandler Carruth 
579f4ea3d3dSChandler Carruth   LLVMContextImpl *pImpl = V->getContext().pImpl;
580ef860a24SChandler Carruth 
581f4ea3d3dSChandler Carruth   if (V->HasValueHandle) {
582ef860a24SChandler Carruth     // If this value already has a ValueHandle, then it must be in the
583ef860a24SChandler Carruth     // ValueHandles map already.
584f4ea3d3dSChandler Carruth     ValueHandleBase *&Entry = pImpl->ValueHandles[V];
5852617dcceSCraig Topper     assert(Entry && "Value doesn't have any handles?");
586ef860a24SChandler Carruth     AddToExistingUseList(&Entry);
587ef860a24SChandler Carruth     return;
588ef860a24SChandler Carruth   }
589ef860a24SChandler Carruth 
590ef860a24SChandler Carruth   // Ok, it doesn't have any handles yet, so we must insert it into the
591ef860a24SChandler Carruth   // DenseMap.  However, doing this insertion could cause the DenseMap to
592ef860a24SChandler Carruth   // reallocate itself, which would invalidate all of the PrevP pointers that
593ef860a24SChandler Carruth   // point into the old table.  Handle this by checking for reallocation and
594ef860a24SChandler Carruth   // updating the stale pointers only if needed.
595ef860a24SChandler Carruth   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
596ef860a24SChandler Carruth   const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
597ef860a24SChandler Carruth 
598f4ea3d3dSChandler Carruth   ValueHandleBase *&Entry = Handles[V];
5992617dcceSCraig Topper   assert(!Entry && "Value really did already have handles?");
600ef860a24SChandler Carruth   AddToExistingUseList(&Entry);
601f4ea3d3dSChandler Carruth   V->HasValueHandle = true;
602ef860a24SChandler Carruth 
603ef860a24SChandler Carruth   // If reallocation didn't happen or if this was the first insertion, don't
604ef860a24SChandler Carruth   // walk the table.
605ef860a24SChandler Carruth   if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
606ef860a24SChandler Carruth       Handles.size() == 1) {
607ef860a24SChandler Carruth     return;
608ef860a24SChandler Carruth   }
609ef860a24SChandler Carruth 
610ef860a24SChandler Carruth   // Okay, reallocation did happen.  Fix the Prev Pointers.
611ef860a24SChandler Carruth   for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
612ef860a24SChandler Carruth        E = Handles.end(); I != E; ++I) {
613f4ea3d3dSChandler Carruth     assert(I->second && I->first == I->second->V &&
614ef860a24SChandler Carruth            "List invariant broken!");
615ef860a24SChandler Carruth     I->second->setPrevPtr(&I->second);
616ef860a24SChandler Carruth   }
617ef860a24SChandler Carruth }
618ef860a24SChandler Carruth 
619ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() {
620f4ea3d3dSChandler Carruth   assert(V && V->HasValueHandle &&
621ef860a24SChandler Carruth          "Pointer doesn't have a use list!");
622ef860a24SChandler Carruth 
623ef860a24SChandler Carruth   // Unlink this from its use list.
624ef860a24SChandler Carruth   ValueHandleBase **PrevPtr = getPrevPtr();
625ef860a24SChandler Carruth   assert(*PrevPtr == this && "List invariant broken");
626ef860a24SChandler Carruth 
627ef860a24SChandler Carruth   *PrevPtr = Next;
628ef860a24SChandler Carruth   if (Next) {
629ef860a24SChandler Carruth     assert(Next->getPrevPtr() == &Next && "List invariant broken");
630ef860a24SChandler Carruth     Next->setPrevPtr(PrevPtr);
631ef860a24SChandler Carruth     return;
632ef860a24SChandler Carruth   }
633ef860a24SChandler Carruth 
634ef860a24SChandler Carruth   // If the Next pointer was null, then it is possible that this was the last
635ef860a24SChandler Carruth   // ValueHandle watching VP.  If so, delete its entry from the ValueHandles
636ef860a24SChandler Carruth   // map.
637f4ea3d3dSChandler Carruth   LLVMContextImpl *pImpl = V->getContext().pImpl;
638ef860a24SChandler Carruth   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
639ef860a24SChandler Carruth   if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
640f4ea3d3dSChandler Carruth     Handles.erase(V);
641f4ea3d3dSChandler Carruth     V->HasValueHandle = false;
642ef860a24SChandler Carruth   }
643ef860a24SChandler Carruth }
644ef860a24SChandler Carruth 
645ef860a24SChandler Carruth 
646ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) {
647ef860a24SChandler Carruth   assert(V->HasValueHandle && "Should only be called if ValueHandles present");
648ef860a24SChandler Carruth 
649ef860a24SChandler Carruth   // Get the linked list base, which is guaranteed to exist since the
650ef860a24SChandler Carruth   // HasValueHandle flag is set.
651ef860a24SChandler Carruth   LLVMContextImpl *pImpl = V->getContext().pImpl;
652ef860a24SChandler Carruth   ValueHandleBase *Entry = pImpl->ValueHandles[V];
653ef860a24SChandler Carruth   assert(Entry && "Value bit set but no entries exist");
654ef860a24SChandler Carruth 
655ef860a24SChandler Carruth   // We use a local ValueHandleBase as an iterator so that ValueHandles can add
656ef860a24SChandler Carruth   // and remove themselves from the list without breaking our iteration.  This
657ef860a24SChandler Carruth   // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
658ef860a24SChandler Carruth   // Note that we deliberately do not the support the case when dropping a value
659ef860a24SChandler Carruth   // handle results in a new value handle being permanently added to the list
660ef860a24SChandler Carruth   // (as might occur in theory for CallbackVH's): the new value handle will not
661ef860a24SChandler Carruth   // be processed and the checking code will mete out righteous punishment if
662ef860a24SChandler Carruth   // the handle is still present once we have finished processing all the other
663ef860a24SChandler Carruth   // value handles (it is fine to momentarily add then remove a value handle).
664ef860a24SChandler Carruth   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
665ef860a24SChandler Carruth     Iterator.RemoveFromUseList();
666ef860a24SChandler Carruth     Iterator.AddToExistingUseListAfter(Entry);
667ef860a24SChandler Carruth     assert(Entry->Next == &Iterator && "Loop invariant broken.");
668ef860a24SChandler Carruth 
669ef860a24SChandler Carruth     switch (Entry->getKind()) {
670ef860a24SChandler Carruth     case Assert:
671ef860a24SChandler Carruth       break;
672ef860a24SChandler Carruth     case Tracking:
673ef860a24SChandler Carruth       // Mark that this value has been deleted by setting it to an invalid Value
674ef860a24SChandler Carruth       // pointer.
675ef860a24SChandler Carruth       Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
676ef860a24SChandler Carruth       break;
677ef860a24SChandler Carruth     case Weak:
678ef860a24SChandler Carruth       // Weak just goes to null, which will unlink it from the list.
679c620761cSCraig Topper       Entry->operator=(nullptr);
680ef860a24SChandler Carruth       break;
681ef860a24SChandler Carruth     case Callback:
682ef860a24SChandler Carruth       // Forward to the subclass's implementation.
683ef860a24SChandler Carruth       static_cast<CallbackVH*>(Entry)->deleted();
684ef860a24SChandler Carruth       break;
685ef860a24SChandler Carruth     }
686ef860a24SChandler Carruth   }
687ef860a24SChandler Carruth 
688ef860a24SChandler Carruth   // All callbacks, weak references, and assertingVHs should be dropped by now.
689ef860a24SChandler Carruth   if (V->HasValueHandle) {
690ef860a24SChandler Carruth #ifndef NDEBUG      // Only in +Asserts mode...
691ef860a24SChandler Carruth     dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
692ef860a24SChandler Carruth            << "\n";
693ef860a24SChandler Carruth     if (pImpl->ValueHandles[V]->getKind() == Assert)
694ef860a24SChandler Carruth       llvm_unreachable("An asserting value handle still pointed to this"
695ef860a24SChandler Carruth                        " value!");
696ef860a24SChandler Carruth 
697ef860a24SChandler Carruth #endif
698ef860a24SChandler Carruth     llvm_unreachable("All references to V were not removed?");
699ef860a24SChandler Carruth   }
700ef860a24SChandler Carruth }
701ef860a24SChandler Carruth 
702ef860a24SChandler Carruth 
703ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
704ef860a24SChandler Carruth   assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
705ef860a24SChandler Carruth   assert(Old != New && "Changing value into itself!");
706c1892e2dSFrederic Riss   assert(Old->getType() == New->getType() &&
707c1892e2dSFrederic Riss          "replaceAllUses of value with new value of different type!");
708ef860a24SChandler Carruth 
709ef860a24SChandler Carruth   // Get the linked list base, which is guaranteed to exist since the
710ef860a24SChandler Carruth   // HasValueHandle flag is set.
711ef860a24SChandler Carruth   LLVMContextImpl *pImpl = Old->getContext().pImpl;
712ef860a24SChandler Carruth   ValueHandleBase *Entry = pImpl->ValueHandles[Old];
713ef860a24SChandler Carruth 
714ef860a24SChandler Carruth   assert(Entry && "Value bit set but no entries exist");
715ef860a24SChandler Carruth 
716ef860a24SChandler Carruth   // We use a local ValueHandleBase as an iterator so that
717ef860a24SChandler Carruth   // ValueHandles can add and remove themselves from the list without
718ef860a24SChandler Carruth   // breaking our iteration.  This is not really an AssertingVH; we
719ef860a24SChandler Carruth   // just have to give ValueHandleBase some kind.
720ef860a24SChandler Carruth   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
721ef860a24SChandler Carruth     Iterator.RemoveFromUseList();
722ef860a24SChandler Carruth     Iterator.AddToExistingUseListAfter(Entry);
723ef860a24SChandler Carruth     assert(Entry->Next == &Iterator && "Loop invariant broken.");
724ef860a24SChandler Carruth 
725ef860a24SChandler Carruth     switch (Entry->getKind()) {
726ef860a24SChandler Carruth     case Assert:
727ef860a24SChandler Carruth       // Asserting handle does not follow RAUW implicitly.
728ef860a24SChandler Carruth       break;
729ef860a24SChandler Carruth     case Tracking:
730ef860a24SChandler Carruth       // Tracking goes to new value like a WeakVH. Note that this may make it
731ef860a24SChandler Carruth       // something incompatible with its templated type. We don't want to have a
732ef860a24SChandler Carruth       // virtual (or inline) interface to handle this though, so instead we make
733ef860a24SChandler Carruth       // the TrackingVH accessors guarantee that a client never sees this value.
734ef860a24SChandler Carruth 
735ef860a24SChandler Carruth       // FALLTHROUGH
736ef860a24SChandler Carruth     case Weak:
737ef860a24SChandler Carruth       // Weak goes to the new value, which will unlink it from Old's list.
738ef860a24SChandler Carruth       Entry->operator=(New);
739ef860a24SChandler Carruth       break;
740ef860a24SChandler Carruth     case Callback:
741ef860a24SChandler Carruth       // Forward to the subclass's implementation.
742ef860a24SChandler Carruth       static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
743ef860a24SChandler Carruth       break;
744ef860a24SChandler Carruth     }
745ef860a24SChandler Carruth   }
746ef860a24SChandler Carruth 
747ef860a24SChandler Carruth #ifndef NDEBUG
748ef860a24SChandler Carruth   // If any new tracking or weak value handles were added while processing the
749ef860a24SChandler Carruth   // list, then complain about it now.
750ef860a24SChandler Carruth   if (Old->HasValueHandle)
751ef860a24SChandler Carruth     for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
752ef860a24SChandler Carruth       switch (Entry->getKind()) {
753ef860a24SChandler Carruth       case Tracking:
754ef860a24SChandler Carruth       case Weak:
755ef860a24SChandler Carruth         dbgs() << "After RAUW from " << *Old->getType() << " %"
756ef860a24SChandler Carruth                << Old->getName() << " to " << *New->getType() << " %"
757ef860a24SChandler Carruth                << New->getName() << "\n";
758ef860a24SChandler Carruth         llvm_unreachable("A tracking or weak value handle still pointed to the"
759ef860a24SChandler Carruth                          " old value!\n");
760ef860a24SChandler Carruth       default:
761ef860a24SChandler Carruth         break;
762ef860a24SChandler Carruth       }
763ef860a24SChandler Carruth #endif
764ef860a24SChandler Carruth }
765ef860a24SChandler Carruth 
766d12ccbd3SJuergen Ributzka // Pin the vtable to this file.
767d12ccbd3SJuergen Ributzka void CallbackVH::anchor() {}
768