1*0b57cec5SDimitry Andric //===- AssumptionCache.cpp - Cache finding @llvm.assume calls -------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9bdd1243dSDimitry Andric // This file contains a pass that keeps track of @llvm.assume and
10bdd1243dSDimitry Andric // @llvm.experimental.guard intrinsics in the functions of a module.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
17*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
1881ad6265SDimitry Andric #include "llvm/Analysis/AssumeBundleQueries.h"
19349cc55cSDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
21*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
22*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
23*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
24*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
25*0b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
26*0b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
27480093f4SDimitry Andric #include "llvm/InitializePasses.h"
28*0b57cec5SDimitry Andric #include "llvm/Pass.h"
29*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
30*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
31*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
32*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
33*0b57cec5SDimitry Andric #include <cassert>
34*0b57cec5SDimitry Andric #include <utility>
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric using namespace llvm;
37*0b57cec5SDimitry Andric using namespace llvm::PatternMatch;
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric static cl::opt<bool>
40*0b57cec5SDimitry Andric     VerifyAssumptionCache("verify-assumption-cache", cl::Hidden,
41*0b57cec5SDimitry Andric                           cl::desc("Enable verification of assumption cache"),
42*0b57cec5SDimitry Andric                           cl::init(false));
43*0b57cec5SDimitry Andric 
445ffd83dbSDimitry Andric SmallVector<AssumptionCache::ResultElem, 1> &
45*0b57cec5SDimitry Andric AssumptionCache::getOrInsertAffectedValues(Value *V) {
46*0b57cec5SDimitry Andric   // Try using find_as first to avoid creating extra value handles just for the
47*0b57cec5SDimitry Andric   // purpose of doing the lookup.
48*0b57cec5SDimitry Andric   auto AVI = AffectedValues.find_as(V);
49*0b57cec5SDimitry Andric   if (AVI != AffectedValues.end())
50*0b57cec5SDimitry Andric     return AVI->second;
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   auto AVIP = AffectedValues.insert(
535ffd83dbSDimitry Andric       {AffectedValueCallbackVH(V, this), SmallVector<ResultElem, 1>()});
54*0b57cec5SDimitry Andric   return AVIP.first->second;
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric 
575ffd83dbSDimitry Andric static void
58349cc55cSDimitry Andric findAffectedValues(CallBase *CI, TargetTransformInfo *TTI,
595ffd83dbSDimitry Andric                    SmallVectorImpl<AssumptionCache::ResultElem> &Affected) {
60*0b57cec5SDimitry Andric   // Note: This code must be kept in-sync with the code in
61*0b57cec5SDimitry Andric   // computeKnownBitsFromAssume in ValueTracking.
62*0b57cec5SDimitry Andric 
635ffd83dbSDimitry Andric   auto AddAffected = [&Affected](Value *V, unsigned Idx =
645ffd83dbSDimitry Andric                                                AssumptionCache::ExprResultIdx) {
65*0b57cec5SDimitry Andric     if (isa<Argument>(V)) {
665ffd83dbSDimitry Andric       Affected.push_back({V, Idx});
67*0b57cec5SDimitry Andric     } else if (auto *I = dyn_cast<Instruction>(V)) {
685ffd83dbSDimitry Andric       Affected.push_back({I, Idx});
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric       // Peek through unary operators to find the source of the condition.
71*0b57cec5SDimitry Andric       Value *Op;
72*0b57cec5SDimitry Andric       if (match(I, m_BitCast(m_Value(Op))) ||
735ffd83dbSDimitry Andric           match(I, m_PtrToInt(m_Value(Op))) || match(I, m_Not(m_Value(Op)))) {
74*0b57cec5SDimitry Andric         if (isa<Instruction>(Op) || isa<Argument>(Op))
755ffd83dbSDimitry Andric           Affected.push_back({Op, Idx});
76*0b57cec5SDimitry Andric       }
77*0b57cec5SDimitry Andric     }
78*0b57cec5SDimitry Andric   };
79*0b57cec5SDimitry Andric 
805ffd83dbSDimitry Andric   for (unsigned Idx = 0; Idx != CI->getNumOperandBundles(); Idx++) {
815ffd83dbSDimitry Andric     if (CI->getOperandBundleAt(Idx).Inputs.size() > ABA_WasOn &&
825ffd83dbSDimitry Andric         CI->getOperandBundleAt(Idx).getTagName() != IgnoreBundleTag)
835ffd83dbSDimitry Andric       AddAffected(CI->getOperandBundleAt(Idx).Inputs[ABA_WasOn], Idx);
845ffd83dbSDimitry Andric   }
855ffd83dbSDimitry Andric 
86*0b57cec5SDimitry Andric   Value *Cond = CI->getArgOperand(0), *A, *B;
87*0b57cec5SDimitry Andric   AddAffected(Cond);
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric   CmpInst::Predicate Pred;
90*0b57cec5SDimitry Andric   if (match(Cond, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
91*0b57cec5SDimitry Andric     AddAffected(A);
92*0b57cec5SDimitry Andric     AddAffected(B);
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric     if (Pred == ICmpInst::ICMP_EQ) {
95*0b57cec5SDimitry Andric       // For equality comparisons, we handle the case of bit inversion.
96*0b57cec5SDimitry Andric       auto AddAffectedFromEq = [&AddAffected](Value *V) {
97*0b57cec5SDimitry Andric         Value *A;
98*0b57cec5SDimitry Andric         if (match(V, m_Not(m_Value(A)))) {
99*0b57cec5SDimitry Andric           AddAffected(A);
100*0b57cec5SDimitry Andric           V = A;
101*0b57cec5SDimitry Andric         }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric         Value *B;
104*0b57cec5SDimitry Andric         // (A & B) or (A | B) or (A ^ B).
105*0b57cec5SDimitry Andric         if (match(V, m_BitwiseLogic(m_Value(A), m_Value(B)))) {
106*0b57cec5SDimitry Andric           AddAffected(A);
107*0b57cec5SDimitry Andric           AddAffected(B);
108*0b57cec5SDimitry Andric           // (A << C) or (A >>_s C) or (A >>_u C) where C is some constant.
109e8d8bef9SDimitry Andric         } else if (match(V, m_Shift(m_Value(A), m_ConstantInt()))) {
110*0b57cec5SDimitry Andric           AddAffected(A);
111*0b57cec5SDimitry Andric         }
112*0b57cec5SDimitry Andric       };
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric       AddAffectedFromEq(A);
115*0b57cec5SDimitry Andric       AddAffectedFromEq(B);
116bdd1243dSDimitry Andric     } else if (Pred == ICmpInst::ICMP_NE) {
117bdd1243dSDimitry Andric       Value *X, *Y;
118bdd1243dSDimitry Andric       // Handle (a & b != 0). If a/b is a power of 2 we can use this
119bdd1243dSDimitry Andric       // information.
120bdd1243dSDimitry Andric       if (match(A, m_And(m_Value(X), m_Value(Y))) && match(B, m_Zero())) {
121bdd1243dSDimitry Andric         AddAffected(X);
122bdd1243dSDimitry Andric         AddAffected(Y);
123*0b57cec5SDimitry Andric       }
124bdd1243dSDimitry Andric     } else if (Pred == ICmpInst::ICMP_ULT) {
125e8d8bef9SDimitry Andric       Value *X;
126e8d8bef9SDimitry Andric       // Handle (A + C1) u< C2, which is the canonical form of A > C3 && A < C4,
127e8d8bef9SDimitry Andric       // and recognized by LVI at least.
128bdd1243dSDimitry Andric       if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
129e8d8bef9SDimitry Andric           match(B, m_ConstantInt()))
130e8d8bef9SDimitry Andric         AddAffected(X);
131*0b57cec5SDimitry Andric     }
132bdd1243dSDimitry Andric   }
133349cc55cSDimitry Andric 
134349cc55cSDimitry Andric   if (TTI) {
135349cc55cSDimitry Andric     const Value *Ptr;
136349cc55cSDimitry Andric     unsigned AS;
137349cc55cSDimitry Andric     std::tie(Ptr, AS) = TTI->getPredicatedAddrSpace(Cond);
138349cc55cSDimitry Andric     if (Ptr)
139349cc55cSDimitry Andric       AddAffected(const_cast<Value *>(Ptr->stripInBoundsOffsets()));
140349cc55cSDimitry Andric   }
141*0b57cec5SDimitry Andric }
142*0b57cec5SDimitry Andric 
143bdd1243dSDimitry Andric void AssumptionCache::updateAffectedValues(CondGuardInst *CI) {
1445ffd83dbSDimitry Andric   SmallVector<AssumptionCache::ResultElem, 16> Affected;
145349cc55cSDimitry Andric   findAffectedValues(CI, TTI, Affected);
146*0b57cec5SDimitry Andric 
147*0b57cec5SDimitry Andric   for (auto &AV : Affected) {
1485ffd83dbSDimitry Andric     auto &AVV = getOrInsertAffectedValues(AV.Assume);
149349cc55cSDimitry Andric     if (llvm::none_of(AVV, [&](ResultElem &Elem) {
1505ffd83dbSDimitry Andric           return Elem.Assume == CI && Elem.Index == AV.Index;
151349cc55cSDimitry Andric         }))
1525ffd83dbSDimitry Andric       AVV.push_back({CI, AV.Index});
153*0b57cec5SDimitry Andric   }
154*0b57cec5SDimitry Andric }
155*0b57cec5SDimitry Andric 
156bdd1243dSDimitry Andric void AssumptionCache::unregisterAssumption(CondGuardInst *CI) {
1575ffd83dbSDimitry Andric   SmallVector<AssumptionCache::ResultElem, 16> Affected;
158349cc55cSDimitry Andric   findAffectedValues(CI, TTI, Affected);
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric   for (auto &AV : Affected) {
1615ffd83dbSDimitry Andric     auto AVI = AffectedValues.find_as(AV.Assume);
1625ffd83dbSDimitry Andric     if (AVI == AffectedValues.end())
1635ffd83dbSDimitry Andric       continue;
1645ffd83dbSDimitry Andric     bool Found = false;
1655ffd83dbSDimitry Andric     bool HasNonnull = false;
1665ffd83dbSDimitry Andric     for (ResultElem &Elem : AVI->second) {
1675ffd83dbSDimitry Andric       if (Elem.Assume == CI) {
1685ffd83dbSDimitry Andric         Found = true;
1695ffd83dbSDimitry Andric         Elem.Assume = nullptr;
1705ffd83dbSDimitry Andric       }
1715ffd83dbSDimitry Andric       HasNonnull |= !!Elem.Assume;
1725ffd83dbSDimitry Andric       if (HasNonnull && Found)
1735ffd83dbSDimitry Andric         break;
1745ffd83dbSDimitry Andric     }
1755ffd83dbSDimitry Andric     assert(Found && "already unregistered or incorrect cache state");
1765ffd83dbSDimitry Andric     if (!HasNonnull)
177*0b57cec5SDimitry Andric       AffectedValues.erase(AVI);
178*0b57cec5SDimitry Andric   }
1798bcb0991SDimitry Andric 
180e8d8bef9SDimitry Andric   erase_value(AssumeHandles, CI);
181*0b57cec5SDimitry Andric }
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric void AssumptionCache::AffectedValueCallbackVH::deleted() {
184e8d8bef9SDimitry Andric   AC->AffectedValues.erase(getValPtr());
185*0b57cec5SDimitry Andric   // 'this' now dangles!
186*0b57cec5SDimitry Andric }
187*0b57cec5SDimitry Andric 
1888bcb0991SDimitry Andric void AssumptionCache::transferAffectedValuesInCache(Value *OV, Value *NV) {
189*0b57cec5SDimitry Andric   auto &NAVV = getOrInsertAffectedValues(NV);
190*0b57cec5SDimitry Andric   auto AVI = AffectedValues.find(OV);
191*0b57cec5SDimitry Andric   if (AVI == AffectedValues.end())
192*0b57cec5SDimitry Andric     return;
193*0b57cec5SDimitry Andric 
194*0b57cec5SDimitry Andric   for (auto &A : AVI->second)
195e8d8bef9SDimitry Andric     if (!llvm::is_contained(NAVV, A))
196*0b57cec5SDimitry Andric       NAVV.push_back(A);
1978bcb0991SDimitry Andric   AffectedValues.erase(OV);
198*0b57cec5SDimitry Andric }
199*0b57cec5SDimitry Andric 
200*0b57cec5SDimitry Andric void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
201*0b57cec5SDimitry Andric   if (!isa<Instruction>(NV) && !isa<Argument>(NV))
202*0b57cec5SDimitry Andric     return;
203*0b57cec5SDimitry Andric 
204*0b57cec5SDimitry Andric   // Any assumptions that affected this value now affect the new value.
205*0b57cec5SDimitry Andric 
2068bcb0991SDimitry Andric   AC->transferAffectedValuesInCache(getValPtr(), NV);
207*0b57cec5SDimitry Andric   // 'this' now might dangle! If the AffectedValues map was resized to add an
208*0b57cec5SDimitry Andric   // entry for NV then this object might have been destroyed in favor of some
209*0b57cec5SDimitry Andric   // copy in the grown map.
210*0b57cec5SDimitry Andric }
211*0b57cec5SDimitry Andric 
212*0b57cec5SDimitry Andric void AssumptionCache::scanFunction() {
213*0b57cec5SDimitry Andric   assert(!Scanned && "Tried to scan the function twice!");
214*0b57cec5SDimitry Andric   assert(AssumeHandles.empty() && "Already have assumes when scanning!");
215*0b57cec5SDimitry Andric 
216*0b57cec5SDimitry Andric   // Go through all instructions in all blocks, add all calls to @llvm.assume
217*0b57cec5SDimitry Andric   // to this cache.
218*0b57cec5SDimitry Andric   for (BasicBlock &B : F)
219fe6060f1SDimitry Andric     for (Instruction &I : B)
220bdd1243dSDimitry Andric       if (isa<CondGuardInst>(&I))
221fe6060f1SDimitry Andric         AssumeHandles.push_back({&I, ExprResultIdx});
222*0b57cec5SDimitry Andric 
223*0b57cec5SDimitry Andric   // Mark the scan as complete.
224*0b57cec5SDimitry Andric   Scanned = true;
225*0b57cec5SDimitry Andric 
226*0b57cec5SDimitry Andric   // Update affected values.
227*0b57cec5SDimitry Andric   for (auto &A : AssumeHandles)
228bdd1243dSDimitry Andric     updateAffectedValues(cast<CondGuardInst>(A));
229*0b57cec5SDimitry Andric }
230*0b57cec5SDimitry Andric 
231bdd1243dSDimitry Andric void AssumptionCache::registerAssumption(CondGuardInst *CI) {
232*0b57cec5SDimitry Andric   // If we haven't scanned the function yet, just drop this assumption. It will
233*0b57cec5SDimitry Andric   // be found when we scan later.
234*0b57cec5SDimitry Andric   if (!Scanned)
235*0b57cec5SDimitry Andric     return;
236*0b57cec5SDimitry Andric 
2375ffd83dbSDimitry Andric   AssumeHandles.push_back({CI, ExprResultIdx});
238*0b57cec5SDimitry Andric 
239*0b57cec5SDimitry Andric #ifndef NDEBUG
240*0b57cec5SDimitry Andric   assert(CI->getParent() &&
241bdd1243dSDimitry Andric          "Cannot a register CondGuardInst not in a basic block");
242*0b57cec5SDimitry Andric   assert(&F == CI->getParent()->getParent() &&
243bdd1243dSDimitry Andric          "Cannot a register CondGuardInst not in this function");
244*0b57cec5SDimitry Andric 
245*0b57cec5SDimitry Andric   // We expect the number of assumptions to be small, so in an asserts build
246*0b57cec5SDimitry Andric   // check that we don't accumulate duplicates and that all assumptions point
247*0b57cec5SDimitry Andric   // to the same function.
248*0b57cec5SDimitry Andric   SmallPtrSet<Value *, 16> AssumptionSet;
249*0b57cec5SDimitry Andric   for (auto &VH : AssumeHandles) {
250*0b57cec5SDimitry Andric     if (!VH)
251*0b57cec5SDimitry Andric       continue;
252*0b57cec5SDimitry Andric 
253*0b57cec5SDimitry Andric     assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
254*0b57cec5SDimitry Andric            "Cached assumption not inside this function!");
255bdd1243dSDimitry Andric     assert(isa<CondGuardInst>(VH) &&
256bdd1243dSDimitry Andric            "Cached something other than CondGuardInst!");
257*0b57cec5SDimitry Andric     assert(AssumptionSet.insert(VH).second &&
258*0b57cec5SDimitry Andric            "Cache contains multiple copies of a call!");
259*0b57cec5SDimitry Andric   }
260*0b57cec5SDimitry Andric #endif
261*0b57cec5SDimitry Andric 
262*0b57cec5SDimitry Andric   updateAffectedValues(CI);
263*0b57cec5SDimitry Andric }
264*0b57cec5SDimitry Andric 
265349cc55cSDimitry Andric AssumptionCache AssumptionAnalysis::run(Function &F,
266349cc55cSDimitry Andric                                         FunctionAnalysisManager &FAM) {
267349cc55cSDimitry Andric   auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
268349cc55cSDimitry Andric   return AssumptionCache(F, &TTI);
269349cc55cSDimitry Andric }
270349cc55cSDimitry Andric 
271*0b57cec5SDimitry Andric AnalysisKey AssumptionAnalysis::Key;
272*0b57cec5SDimitry Andric 
273*0b57cec5SDimitry Andric PreservedAnalyses AssumptionPrinterPass::run(Function &F,
274*0b57cec5SDimitry Andric                                              FunctionAnalysisManager &AM) {
275*0b57cec5SDimitry Andric   AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
276*0b57cec5SDimitry Andric 
277*0b57cec5SDimitry Andric   OS << "Cached assumptions for function: " << F.getName() << "\n";
278*0b57cec5SDimitry Andric   for (auto &VH : AC.assumptions())
279*0b57cec5SDimitry Andric     if (VH)
280*0b57cec5SDimitry Andric       OS << "  " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
281*0b57cec5SDimitry Andric 
282*0b57cec5SDimitry Andric   return PreservedAnalyses::all();
283*0b57cec5SDimitry Andric }
284*0b57cec5SDimitry Andric 
285*0b57cec5SDimitry Andric void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
286*0b57cec5SDimitry Andric   auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
287*0b57cec5SDimitry Andric   if (I != ACT->AssumptionCaches.end())
288*0b57cec5SDimitry Andric     ACT->AssumptionCaches.erase(I);
289*0b57cec5SDimitry Andric   // 'this' now dangles!
290*0b57cec5SDimitry Andric }
291*0b57cec5SDimitry Andric 
292*0b57cec5SDimitry Andric AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
293*0b57cec5SDimitry Andric   // We probe the function map twice to try and avoid creating a value handle
294*0b57cec5SDimitry Andric   // around the function in common cases. This makes insertion a bit slower,
295*0b57cec5SDimitry Andric   // but if we have to insert we're going to scan the whole function so that
296*0b57cec5SDimitry Andric   // shouldn't matter.
297*0b57cec5SDimitry Andric   auto I = AssumptionCaches.find_as(&F);
298*0b57cec5SDimitry Andric   if (I != AssumptionCaches.end())
299*0b57cec5SDimitry Andric     return *I->second;
300*0b57cec5SDimitry Andric 
301349cc55cSDimitry Andric   auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
302349cc55cSDimitry Andric   auto *TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
303349cc55cSDimitry Andric 
304*0b57cec5SDimitry Andric   // Ok, build a new cache by scanning the function, insert it and the value
305*0b57cec5SDimitry Andric   // handle into our map, and return the newly populated cache.
306*0b57cec5SDimitry Andric   auto IP = AssumptionCaches.insert(std::make_pair(
307349cc55cSDimitry Andric       FunctionCallbackVH(&F, this), std::make_unique<AssumptionCache>(F, TTI)));
308*0b57cec5SDimitry Andric   assert(IP.second && "Scanning function already in the map?");
309*0b57cec5SDimitry Andric   return *IP.first->second;
310*0b57cec5SDimitry Andric }
311*0b57cec5SDimitry Andric 
312*0b57cec5SDimitry Andric AssumptionCache *AssumptionCacheTracker::lookupAssumptionCache(Function &F) {
313*0b57cec5SDimitry Andric   auto I = AssumptionCaches.find_as(&F);
314*0b57cec5SDimitry Andric   if (I != AssumptionCaches.end())
315*0b57cec5SDimitry Andric     return I->second.get();
316*0b57cec5SDimitry Andric   return nullptr;
317*0b57cec5SDimitry Andric }
318*0b57cec5SDimitry Andric 
319*0b57cec5SDimitry Andric void AssumptionCacheTracker::verifyAnalysis() const {
320*0b57cec5SDimitry Andric   // FIXME: In the long term the verifier should not be controllable with a
321*0b57cec5SDimitry Andric   // flag. We should either fix all passes to correctly update the assumption
322*0b57cec5SDimitry Andric   // cache and enable the verifier unconditionally or somehow arrange for the
323*0b57cec5SDimitry Andric   // assumption list to be updated automatically by passes.
324*0b57cec5SDimitry Andric   if (!VerifyAssumptionCache)
325*0b57cec5SDimitry Andric     return;
326*0b57cec5SDimitry Andric 
327*0b57cec5SDimitry Andric   SmallPtrSet<const CallInst *, 4> AssumptionSet;
328*0b57cec5SDimitry Andric   for (const auto &I : AssumptionCaches) {
329*0b57cec5SDimitry Andric     for (auto &VH : I.second->assumptions())
330*0b57cec5SDimitry Andric       if (VH)
331*0b57cec5SDimitry Andric         AssumptionSet.insert(cast<CallInst>(VH));
332*0b57cec5SDimitry Andric 
333*0b57cec5SDimitry Andric     for (const BasicBlock &B : cast<Function>(*I.first))
334*0b57cec5SDimitry Andric       for (const Instruction &II : B)
335*0b57cec5SDimitry Andric         if (match(&II, m_Intrinsic<Intrinsic::assume>()) &&
336*0b57cec5SDimitry Andric             !AssumptionSet.count(cast<CallInst>(&II)))
337*0b57cec5SDimitry Andric           report_fatal_error("Assumption in scanned function not in cache");
338*0b57cec5SDimitry Andric   }
339*0b57cec5SDimitry Andric }
340*0b57cec5SDimitry Andric 
341*0b57cec5SDimitry Andric AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
342*0b57cec5SDimitry Andric   initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
343*0b57cec5SDimitry Andric }
344*0b57cec5SDimitry Andric 
345*0b57cec5SDimitry Andric AssumptionCacheTracker::~AssumptionCacheTracker() = default;
346*0b57cec5SDimitry Andric 
347*0b57cec5SDimitry Andric char AssumptionCacheTracker::ID = 0;
348*0b57cec5SDimitry Andric 
349*0b57cec5SDimitry Andric INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
350*0b57cec5SDimitry Andric                 "Assumption Cache Tracker", false, true)
351