10b57cec5SDimitry Andric //===- AssumptionCache.cpp - Cache finding @llvm.assume calls -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
9fe013be4SDimitry Andric // This file contains a pass that keeps track of @llvm.assume intrinsics in
10fe013be4SDimitry Andric // the functions of a module.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
150b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
160b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
170b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
1881ad6265SDimitry Andric #include "llvm/Analysis/AssumeBundleQueries.h"
19349cc55cSDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
20*cdc20ff6SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
210b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
220b57cec5SDimitry Andric #include "llvm/IR/Function.h"
230b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
240b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
250b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
260b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
270b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
28480093f4SDimitry Andric #include "llvm/InitializePasses.h"
290b57cec5SDimitry Andric #include "llvm/Pass.h"
300b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
310b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
320b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
330b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
340b57cec5SDimitry Andric #include <cassert>
350b57cec5SDimitry Andric #include <utility>
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric using namespace llvm::PatternMatch;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric static cl::opt<bool>
410b57cec5SDimitry Andric     VerifyAssumptionCache("verify-assumption-cache", cl::Hidden,
420b57cec5SDimitry Andric                           cl::desc("Enable verification of assumption cache"),
430b57cec5SDimitry Andric                           cl::init(false));
440b57cec5SDimitry Andric 
455ffd83dbSDimitry Andric SmallVector<AssumptionCache::ResultElem, 1> &
getOrInsertAffectedValues(Value * V)460b57cec5SDimitry Andric AssumptionCache::getOrInsertAffectedValues(Value *V) {
470b57cec5SDimitry Andric   // Try using find_as first to avoid creating extra value handles just for the
480b57cec5SDimitry Andric   // purpose of doing the lookup.
490b57cec5SDimitry Andric   auto AVI = AffectedValues.find_as(V);
500b57cec5SDimitry Andric   if (AVI != AffectedValues.end())
510b57cec5SDimitry Andric     return AVI->second;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   auto AVIP = AffectedValues.insert(
545ffd83dbSDimitry Andric       {AffectedValueCallbackVH(V, this), SmallVector<ResultElem, 1>()});
550b57cec5SDimitry Andric   return AVIP.first->second;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
585ffd83dbSDimitry Andric static void
findAffectedValues(CallBase * CI,TargetTransformInfo * TTI,SmallVectorImpl<AssumptionCache::ResultElem> & Affected)59349cc55cSDimitry Andric findAffectedValues(CallBase *CI, TargetTransformInfo *TTI,
605ffd83dbSDimitry Andric                    SmallVectorImpl<AssumptionCache::ResultElem> &Affected) {
610b57cec5SDimitry Andric   // Note: This code must be kept in-sync with the code in
620b57cec5SDimitry Andric   // computeKnownBitsFromAssume in ValueTracking.
630b57cec5SDimitry Andric 
645ffd83dbSDimitry Andric   auto AddAffected = [&Affected](Value *V, unsigned Idx =
655ffd83dbSDimitry Andric                                                AssumptionCache::ExprResultIdx) {
66c9157d92SDimitry Andric     if (isa<Argument>(V) || isa<GlobalValue>(V)) {
675ffd83dbSDimitry Andric       Affected.push_back({V, Idx});
680b57cec5SDimitry Andric     } else if (auto *I = dyn_cast<Instruction>(V)) {
695ffd83dbSDimitry Andric       Affected.push_back({I, Idx});
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric       // Peek through unary operators to find the source of the condition.
720b57cec5SDimitry Andric       Value *Op;
73c9157d92SDimitry Andric       if (match(I, m_PtrToInt(m_Value(Op)))) {
740b57cec5SDimitry Andric         if (isa<Instruction>(Op) || isa<Argument>(Op))
755ffd83dbSDimitry Andric           Affected.push_back({Op, Idx});
760b57cec5SDimitry Andric       }
770b57cec5SDimitry Andric     }
780b57cec5SDimitry Andric   };
790b57cec5SDimitry Andric 
805ffd83dbSDimitry Andric   for (unsigned Idx = 0; Idx != CI->getNumOperandBundles(); Idx++) {
81*cdc20ff6SDimitry Andric     OperandBundleUse Bundle = CI->getOperandBundleAt(Idx);
82*cdc20ff6SDimitry Andric     if (Bundle.getTagName() == "separate_storage") {
83*cdc20ff6SDimitry Andric       assert(Bundle.Inputs.size() == 2 &&
84*cdc20ff6SDimitry Andric              "separate_storage must have two args");
85*cdc20ff6SDimitry Andric       AddAffected(getUnderlyingObject(Bundle.Inputs[0]), Idx);
86*cdc20ff6SDimitry Andric       AddAffected(getUnderlyingObject(Bundle.Inputs[1]), Idx);
87*cdc20ff6SDimitry Andric     } else if (Bundle.Inputs.size() > ABA_WasOn &&
88*cdc20ff6SDimitry Andric                Bundle.getTagName() != IgnoreBundleTag)
89*cdc20ff6SDimitry Andric       AddAffected(Bundle.Inputs[ABA_WasOn], Idx);
905ffd83dbSDimitry Andric   }
915ffd83dbSDimitry Andric 
920b57cec5SDimitry Andric   Value *Cond = CI->getArgOperand(0), *A, *B;
930b57cec5SDimitry Andric   AddAffected(Cond);
94c9157d92SDimitry Andric   if (match(Cond, m_Not(m_Value(A))))
95c9157d92SDimitry Andric     AddAffected(A);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   CmpInst::Predicate Pred;
98fe013be4SDimitry Andric   if (match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B)))) {
990b57cec5SDimitry Andric     AddAffected(A);
1000b57cec5SDimitry Andric     AddAffected(B);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric     if (Pred == ICmpInst::ICMP_EQ) {
103c9157d92SDimitry Andric       if (match(B, m_ConstantInt())) {
104c9157d92SDimitry Andric         Value *X;
105c9157d92SDimitry Andric         // (X & C) or (X | C) or (X ^ C).
106c9157d92SDimitry Andric         // (X << C) or (X >>_s C) or (X >>_u C).
107c9157d92SDimitry Andric         if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
108c9157d92SDimitry Andric             match(A, m_Shift(m_Value(X), m_ConstantInt())))
109bdd1243dSDimitry Andric           AddAffected(X);
1100b57cec5SDimitry Andric       }
111c9157d92SDimitry Andric     } else if (Pred == ICmpInst::ICMP_NE) {
112c9157d92SDimitry Andric       Value *X;
113c9157d92SDimitry Andric       // Handle (X & pow2 != 0).
114c9157d92SDimitry Andric       if (match(A, m_And(m_Value(X), m_Power2())) && match(B, m_Zero()))
115c9157d92SDimitry Andric         AddAffected(X);
116bdd1243dSDimitry Andric     } else if (Pred == ICmpInst::ICMP_ULT) {
117e8d8bef9SDimitry Andric       Value *X;
118e8d8bef9SDimitry Andric       // Handle (A + C1) u< C2, which is the canonical form of A > C3 && A < C4,
119e8d8bef9SDimitry Andric       // and recognized by LVI at least.
120bdd1243dSDimitry Andric       if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
121e8d8bef9SDimitry Andric           match(B, m_ConstantInt()))
122e8d8bef9SDimitry Andric         AddAffected(X);
123fe013be4SDimitry Andric     } else if (CmpInst::isFPPredicate(Pred)) {
124fe013be4SDimitry Andric       // fcmp fneg(x), y
125fe013be4SDimitry Andric       // fcmp fabs(x), y
126fe013be4SDimitry Andric       // fcmp fneg(fabs(x)), y
127fe013be4SDimitry Andric       if (match(A, m_FNeg(m_Value(A))))
128fe013be4SDimitry Andric         AddAffected(A);
129fe013be4SDimitry Andric       if (match(A, m_FAbs(m_Value(A))))
130fe013be4SDimitry Andric         AddAffected(A);
1310b57cec5SDimitry Andric     }
132fe013be4SDimitry Andric   } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
133fe013be4SDimitry Andric                                                             m_Value(B)))) {
134fe013be4SDimitry Andric     AddAffected(A);
135bdd1243dSDimitry Andric   }
136349cc55cSDimitry Andric 
137349cc55cSDimitry Andric   if (TTI) {
138349cc55cSDimitry Andric     const Value *Ptr;
139349cc55cSDimitry Andric     unsigned AS;
140349cc55cSDimitry Andric     std::tie(Ptr, AS) = TTI->getPredicatedAddrSpace(Cond);
141349cc55cSDimitry Andric     if (Ptr)
142349cc55cSDimitry Andric       AddAffected(const_cast<Value *>(Ptr->stripInBoundsOffsets()));
143349cc55cSDimitry Andric   }
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
updateAffectedValues(AssumeInst * CI)146fe013be4SDimitry Andric void AssumptionCache::updateAffectedValues(AssumeInst *CI) {
1475ffd83dbSDimitry Andric   SmallVector<AssumptionCache::ResultElem, 16> Affected;
148349cc55cSDimitry Andric   findAffectedValues(CI, TTI, Affected);
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   for (auto &AV : Affected) {
1515ffd83dbSDimitry Andric     auto &AVV = getOrInsertAffectedValues(AV.Assume);
152349cc55cSDimitry Andric     if (llvm::none_of(AVV, [&](ResultElem &Elem) {
1535ffd83dbSDimitry Andric           return Elem.Assume == CI && Elem.Index == AV.Index;
154349cc55cSDimitry Andric         }))
1555ffd83dbSDimitry Andric       AVV.push_back({CI, AV.Index});
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric 
unregisterAssumption(AssumeInst * CI)159fe013be4SDimitry Andric void AssumptionCache::unregisterAssumption(AssumeInst *CI) {
1605ffd83dbSDimitry Andric   SmallVector<AssumptionCache::ResultElem, 16> Affected;
161349cc55cSDimitry Andric   findAffectedValues(CI, TTI, Affected);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   for (auto &AV : Affected) {
1645ffd83dbSDimitry Andric     auto AVI = AffectedValues.find_as(AV.Assume);
1655ffd83dbSDimitry Andric     if (AVI == AffectedValues.end())
1665ffd83dbSDimitry Andric       continue;
1675ffd83dbSDimitry Andric     bool Found = false;
1685ffd83dbSDimitry Andric     bool HasNonnull = false;
1695ffd83dbSDimitry Andric     for (ResultElem &Elem : AVI->second) {
1705ffd83dbSDimitry Andric       if (Elem.Assume == CI) {
1715ffd83dbSDimitry Andric         Found = true;
1725ffd83dbSDimitry Andric         Elem.Assume = nullptr;
1735ffd83dbSDimitry Andric       }
1745ffd83dbSDimitry Andric       HasNonnull |= !!Elem.Assume;
1755ffd83dbSDimitry Andric       if (HasNonnull && Found)
1765ffd83dbSDimitry Andric         break;
1775ffd83dbSDimitry Andric     }
1785ffd83dbSDimitry Andric     assert(Found && "already unregistered or incorrect cache state");
1795ffd83dbSDimitry Andric     if (!HasNonnull)
1800b57cec5SDimitry Andric       AffectedValues.erase(AVI);
1810b57cec5SDimitry Andric   }
1828bcb0991SDimitry Andric 
183c9157d92SDimitry Andric   llvm::erase(AssumeHandles, CI);
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
deleted()1860b57cec5SDimitry Andric void AssumptionCache::AffectedValueCallbackVH::deleted() {
187e8d8bef9SDimitry Andric   AC->AffectedValues.erase(getValPtr());
1880b57cec5SDimitry Andric   // 'this' now dangles!
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric 
transferAffectedValuesInCache(Value * OV,Value * NV)1918bcb0991SDimitry Andric void AssumptionCache::transferAffectedValuesInCache(Value *OV, Value *NV) {
1920b57cec5SDimitry Andric   auto &NAVV = getOrInsertAffectedValues(NV);
1930b57cec5SDimitry Andric   auto AVI = AffectedValues.find(OV);
1940b57cec5SDimitry Andric   if (AVI == AffectedValues.end())
1950b57cec5SDimitry Andric     return;
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   for (auto &A : AVI->second)
198e8d8bef9SDimitry Andric     if (!llvm::is_contained(NAVV, A))
1990b57cec5SDimitry Andric       NAVV.push_back(A);
2008bcb0991SDimitry Andric   AffectedValues.erase(OV);
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric 
allUsesReplacedWith(Value * NV)2030b57cec5SDimitry Andric void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
2040b57cec5SDimitry Andric   if (!isa<Instruction>(NV) && !isa<Argument>(NV))
2050b57cec5SDimitry Andric     return;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   // Any assumptions that affected this value now affect the new value.
2080b57cec5SDimitry Andric 
2098bcb0991SDimitry Andric   AC->transferAffectedValuesInCache(getValPtr(), NV);
2100b57cec5SDimitry Andric   // 'this' now might dangle! If the AffectedValues map was resized to add an
2110b57cec5SDimitry Andric   // entry for NV then this object might have been destroyed in favor of some
2120b57cec5SDimitry Andric   // copy in the grown map.
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
scanFunction()2150b57cec5SDimitry Andric void AssumptionCache::scanFunction() {
2160b57cec5SDimitry Andric   assert(!Scanned && "Tried to scan the function twice!");
2170b57cec5SDimitry Andric   assert(AssumeHandles.empty() && "Already have assumes when scanning!");
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   // Go through all instructions in all blocks, add all calls to @llvm.assume
2200b57cec5SDimitry Andric   // to this cache.
2210b57cec5SDimitry Andric   for (BasicBlock &B : F)
222fe6060f1SDimitry Andric     for (Instruction &I : B)
223fe013be4SDimitry Andric       if (isa<AssumeInst>(&I))
224fe6060f1SDimitry Andric         AssumeHandles.push_back({&I, ExprResultIdx});
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   // Mark the scan as complete.
2270b57cec5SDimitry Andric   Scanned = true;
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   // Update affected values.
2300b57cec5SDimitry Andric   for (auto &A : AssumeHandles)
231fe013be4SDimitry Andric     updateAffectedValues(cast<AssumeInst>(A));
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
registerAssumption(AssumeInst * CI)234fe013be4SDimitry Andric void AssumptionCache::registerAssumption(AssumeInst *CI) {
2350b57cec5SDimitry Andric   // If we haven't scanned the function yet, just drop this assumption. It will
2360b57cec5SDimitry Andric   // be found when we scan later.
2370b57cec5SDimitry Andric   if (!Scanned)
2380b57cec5SDimitry Andric     return;
2390b57cec5SDimitry Andric 
2405ffd83dbSDimitry Andric   AssumeHandles.push_back({CI, ExprResultIdx});
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric #ifndef NDEBUG
2430b57cec5SDimitry Andric   assert(CI->getParent() &&
244fe013be4SDimitry Andric          "Cannot register @llvm.assume call not in a basic block");
2450b57cec5SDimitry Andric   assert(&F == CI->getParent()->getParent() &&
246fe013be4SDimitry Andric          "Cannot register @llvm.assume call not in this function");
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   // We expect the number of assumptions to be small, so in an asserts build
2490b57cec5SDimitry Andric   // check that we don't accumulate duplicates and that all assumptions point
2500b57cec5SDimitry Andric   // to the same function.
2510b57cec5SDimitry Andric   SmallPtrSet<Value *, 16> AssumptionSet;
2520b57cec5SDimitry Andric   for (auto &VH : AssumeHandles) {
2530b57cec5SDimitry Andric     if (!VH)
2540b57cec5SDimitry Andric       continue;
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric     assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
2570b57cec5SDimitry Andric            "Cached assumption not inside this function!");
258fe013be4SDimitry Andric     assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
259fe013be4SDimitry Andric            "Cached something other than a call to @llvm.assume!");
2600b57cec5SDimitry Andric     assert(AssumptionSet.insert(VH).second &&
2610b57cec5SDimitry Andric            "Cache contains multiple copies of a call!");
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric #endif
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   updateAffectedValues(CI);
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
run(Function & F,FunctionAnalysisManager & FAM)268349cc55cSDimitry Andric AssumptionCache AssumptionAnalysis::run(Function &F,
269349cc55cSDimitry Andric                                         FunctionAnalysisManager &FAM) {
270349cc55cSDimitry Andric   auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
271349cc55cSDimitry Andric   return AssumptionCache(F, &TTI);
272349cc55cSDimitry Andric }
273349cc55cSDimitry Andric 
2740b57cec5SDimitry Andric AnalysisKey AssumptionAnalysis::Key;
2750b57cec5SDimitry Andric 
run(Function & F,FunctionAnalysisManager & AM)2760b57cec5SDimitry Andric PreservedAnalyses AssumptionPrinterPass::run(Function &F,
2770b57cec5SDimitry Andric                                              FunctionAnalysisManager &AM) {
2780b57cec5SDimitry Andric   AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   OS << "Cached assumptions for function: " << F.getName() << "\n";
2810b57cec5SDimitry Andric   for (auto &VH : AC.assumptions())
2820b57cec5SDimitry Andric     if (VH)
2830b57cec5SDimitry Andric       OS << "  " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   return PreservedAnalyses::all();
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
deleted()2880b57cec5SDimitry Andric void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
2890b57cec5SDimitry Andric   auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
2900b57cec5SDimitry Andric   if (I != ACT->AssumptionCaches.end())
2910b57cec5SDimitry Andric     ACT->AssumptionCaches.erase(I);
2920b57cec5SDimitry Andric   // 'this' now dangles!
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric 
getAssumptionCache(Function & F)2950b57cec5SDimitry Andric AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
2960b57cec5SDimitry Andric   // We probe the function map twice to try and avoid creating a value handle
2970b57cec5SDimitry Andric   // around the function in common cases. This makes insertion a bit slower,
2980b57cec5SDimitry Andric   // but if we have to insert we're going to scan the whole function so that
2990b57cec5SDimitry Andric   // shouldn't matter.
3000b57cec5SDimitry Andric   auto I = AssumptionCaches.find_as(&F);
3010b57cec5SDimitry Andric   if (I != AssumptionCaches.end())
3020b57cec5SDimitry Andric     return *I->second;
3030b57cec5SDimitry Andric 
304349cc55cSDimitry Andric   auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
305349cc55cSDimitry Andric   auto *TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
306349cc55cSDimitry Andric 
3070b57cec5SDimitry Andric   // Ok, build a new cache by scanning the function, insert it and the value
3080b57cec5SDimitry Andric   // handle into our map, and return the newly populated cache.
3090b57cec5SDimitry Andric   auto IP = AssumptionCaches.insert(std::make_pair(
310349cc55cSDimitry Andric       FunctionCallbackVH(&F, this), std::make_unique<AssumptionCache>(F, TTI)));
3110b57cec5SDimitry Andric   assert(IP.second && "Scanning function already in the map?");
3120b57cec5SDimitry Andric   return *IP.first->second;
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
lookupAssumptionCache(Function & F)3150b57cec5SDimitry Andric AssumptionCache *AssumptionCacheTracker::lookupAssumptionCache(Function &F) {
3160b57cec5SDimitry Andric   auto I = AssumptionCaches.find_as(&F);
3170b57cec5SDimitry Andric   if (I != AssumptionCaches.end())
3180b57cec5SDimitry Andric     return I->second.get();
3190b57cec5SDimitry Andric   return nullptr;
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric 
verifyAnalysis() const3220b57cec5SDimitry Andric void AssumptionCacheTracker::verifyAnalysis() const {
3230b57cec5SDimitry Andric   // FIXME: In the long term the verifier should not be controllable with a
3240b57cec5SDimitry Andric   // flag. We should either fix all passes to correctly update the assumption
3250b57cec5SDimitry Andric   // cache and enable the verifier unconditionally or somehow arrange for the
3260b57cec5SDimitry Andric   // assumption list to be updated automatically by passes.
3270b57cec5SDimitry Andric   if (!VerifyAssumptionCache)
3280b57cec5SDimitry Andric     return;
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   SmallPtrSet<const CallInst *, 4> AssumptionSet;
3310b57cec5SDimitry Andric   for (const auto &I : AssumptionCaches) {
3320b57cec5SDimitry Andric     for (auto &VH : I.second->assumptions())
3330b57cec5SDimitry Andric       if (VH)
3340b57cec5SDimitry Andric         AssumptionSet.insert(cast<CallInst>(VH));
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric     for (const BasicBlock &B : cast<Function>(*I.first))
3370b57cec5SDimitry Andric       for (const Instruction &II : B)
3380b57cec5SDimitry Andric         if (match(&II, m_Intrinsic<Intrinsic::assume>()) &&
3390b57cec5SDimitry Andric             !AssumptionSet.count(cast<CallInst>(&II)))
3400b57cec5SDimitry Andric           report_fatal_error("Assumption in scanned function not in cache");
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric 
AssumptionCacheTracker()3440b57cec5SDimitry Andric AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
3450b57cec5SDimitry Andric   initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric AssumptionCacheTracker::~AssumptionCacheTracker() = default;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric char AssumptionCacheTracker::ID = 0;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
3530b57cec5SDimitry Andric                 "Assumption Cache Tracker", false, true)
354