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 // 9*0b57cec5SDimitry Andric // This file contains a pass that keeps track of @llvm.assume intrinsics in 10*0b57cec5SDimitry Andric // 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" 18*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 19*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 20*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 21*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 22*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 23*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 24*0b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 25*0b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h" 26480093f4SDimitry Andric #include "llvm/InitializePasses.h" 27*0b57cec5SDimitry Andric #include "llvm/Pass.h" 28*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 29*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 30*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 31*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 32*0b57cec5SDimitry Andric #include <algorithm> 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 44*0b57cec5SDimitry Andric SmallVector<WeakTrackingVH, 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( 53*0b57cec5SDimitry Andric {AffectedValueCallbackVH(V, this), SmallVector<WeakTrackingVH, 1>()}); 54*0b57cec5SDimitry Andric return AVIP.first->second; 55*0b57cec5SDimitry Andric } 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric static void findAffectedValues(CallInst *CI, 58*0b57cec5SDimitry Andric SmallVectorImpl<Value *> &Affected) { 59*0b57cec5SDimitry Andric // Note: This code must be kept in-sync with the code in 60*0b57cec5SDimitry Andric // computeKnownBitsFromAssume in ValueTracking. 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric auto AddAffected = [&Affected](Value *V) { 63*0b57cec5SDimitry Andric if (isa<Argument>(V)) { 64*0b57cec5SDimitry Andric Affected.push_back(V); 65*0b57cec5SDimitry Andric } else if (auto *I = dyn_cast<Instruction>(V)) { 66*0b57cec5SDimitry Andric Affected.push_back(I); 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric // Peek through unary operators to find the source of the condition. 69*0b57cec5SDimitry Andric Value *Op; 70*0b57cec5SDimitry Andric if (match(I, m_BitCast(m_Value(Op))) || 71*0b57cec5SDimitry Andric match(I, m_PtrToInt(m_Value(Op))) || 72*0b57cec5SDimitry Andric match(I, m_Not(m_Value(Op)))) { 73*0b57cec5SDimitry Andric if (isa<Instruction>(Op) || isa<Argument>(Op)) 74*0b57cec5SDimitry Andric Affected.push_back(Op); 75*0b57cec5SDimitry Andric } 76*0b57cec5SDimitry Andric } 77*0b57cec5SDimitry Andric }; 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric Value *Cond = CI->getArgOperand(0), *A, *B; 80*0b57cec5SDimitry Andric AddAffected(Cond); 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric CmpInst::Predicate Pred; 83*0b57cec5SDimitry Andric if (match(Cond, m_ICmp(Pred, m_Value(A), m_Value(B)))) { 84*0b57cec5SDimitry Andric AddAffected(A); 85*0b57cec5SDimitry Andric AddAffected(B); 86*0b57cec5SDimitry Andric 87*0b57cec5SDimitry Andric if (Pred == ICmpInst::ICMP_EQ) { 88*0b57cec5SDimitry Andric // For equality comparisons, we handle the case of bit inversion. 89*0b57cec5SDimitry Andric auto AddAffectedFromEq = [&AddAffected](Value *V) { 90*0b57cec5SDimitry Andric Value *A; 91*0b57cec5SDimitry Andric if (match(V, m_Not(m_Value(A)))) { 92*0b57cec5SDimitry Andric AddAffected(A); 93*0b57cec5SDimitry Andric V = A; 94*0b57cec5SDimitry Andric } 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric Value *B; 97*0b57cec5SDimitry Andric ConstantInt *C; 98*0b57cec5SDimitry Andric // (A & B) or (A | B) or (A ^ B). 99*0b57cec5SDimitry Andric if (match(V, m_BitwiseLogic(m_Value(A), m_Value(B)))) { 100*0b57cec5SDimitry Andric AddAffected(A); 101*0b57cec5SDimitry Andric AddAffected(B); 102*0b57cec5SDimitry Andric // (A << C) or (A >>_s C) or (A >>_u C) where C is some constant. 103*0b57cec5SDimitry Andric } else if (match(V, m_Shift(m_Value(A), m_ConstantInt(C)))) { 104*0b57cec5SDimitry Andric AddAffected(A); 105*0b57cec5SDimitry Andric } 106*0b57cec5SDimitry Andric }; 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric AddAffectedFromEq(A); 109*0b57cec5SDimitry Andric AddAffectedFromEq(B); 110*0b57cec5SDimitry Andric } 111*0b57cec5SDimitry Andric } 112*0b57cec5SDimitry Andric } 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric void AssumptionCache::updateAffectedValues(CallInst *CI) { 115*0b57cec5SDimitry Andric SmallVector<Value *, 16> Affected; 116*0b57cec5SDimitry Andric findAffectedValues(CI, Affected); 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric for (auto &AV : Affected) { 119*0b57cec5SDimitry Andric auto &AVV = getOrInsertAffectedValues(AV); 120*0b57cec5SDimitry Andric if (std::find(AVV.begin(), AVV.end(), CI) == AVV.end()) 121*0b57cec5SDimitry Andric AVV.push_back(CI); 122*0b57cec5SDimitry Andric } 123*0b57cec5SDimitry Andric } 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric void AssumptionCache::unregisterAssumption(CallInst *CI) { 126*0b57cec5SDimitry Andric SmallVector<Value *, 16> Affected; 127*0b57cec5SDimitry Andric findAffectedValues(CI, Affected); 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric for (auto &AV : Affected) { 130*0b57cec5SDimitry Andric auto AVI = AffectedValues.find_as(AV); 131*0b57cec5SDimitry Andric if (AVI != AffectedValues.end()) 132*0b57cec5SDimitry Andric AffectedValues.erase(AVI); 133*0b57cec5SDimitry Andric } 1348bcb0991SDimitry Andric 1358bcb0991SDimitry Andric AssumeHandles.erase( 1368bcb0991SDimitry Andric remove_if(AssumeHandles, [CI](WeakTrackingVH &VH) { return CI == VH; }), 1378bcb0991SDimitry Andric AssumeHandles.end()); 138*0b57cec5SDimitry Andric } 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric void AssumptionCache::AffectedValueCallbackVH::deleted() { 141*0b57cec5SDimitry Andric auto AVI = AC->AffectedValues.find(getValPtr()); 142*0b57cec5SDimitry Andric if (AVI != AC->AffectedValues.end()) 143*0b57cec5SDimitry Andric AC->AffectedValues.erase(AVI); 144*0b57cec5SDimitry Andric // 'this' now dangles! 145*0b57cec5SDimitry Andric } 146*0b57cec5SDimitry Andric 1478bcb0991SDimitry Andric void AssumptionCache::transferAffectedValuesInCache(Value *OV, Value *NV) { 148*0b57cec5SDimitry Andric auto &NAVV = getOrInsertAffectedValues(NV); 149*0b57cec5SDimitry Andric auto AVI = AffectedValues.find(OV); 150*0b57cec5SDimitry Andric if (AVI == AffectedValues.end()) 151*0b57cec5SDimitry Andric return; 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric for (auto &A : AVI->second) 154*0b57cec5SDimitry Andric if (std::find(NAVV.begin(), NAVV.end(), A) == NAVV.end()) 155*0b57cec5SDimitry Andric NAVV.push_back(A); 1568bcb0991SDimitry Andric AffectedValues.erase(OV); 157*0b57cec5SDimitry Andric } 158*0b57cec5SDimitry Andric 159*0b57cec5SDimitry Andric void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) { 160*0b57cec5SDimitry Andric if (!isa<Instruction>(NV) && !isa<Argument>(NV)) 161*0b57cec5SDimitry Andric return; 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric // Any assumptions that affected this value now affect the new value. 164*0b57cec5SDimitry Andric 1658bcb0991SDimitry Andric AC->transferAffectedValuesInCache(getValPtr(), NV); 166*0b57cec5SDimitry Andric // 'this' now might dangle! If the AffectedValues map was resized to add an 167*0b57cec5SDimitry Andric // entry for NV then this object might have been destroyed in favor of some 168*0b57cec5SDimitry Andric // copy in the grown map. 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric void AssumptionCache::scanFunction() { 172*0b57cec5SDimitry Andric assert(!Scanned && "Tried to scan the function twice!"); 173*0b57cec5SDimitry Andric assert(AssumeHandles.empty() && "Already have assumes when scanning!"); 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric // Go through all instructions in all blocks, add all calls to @llvm.assume 176*0b57cec5SDimitry Andric // to this cache. 177*0b57cec5SDimitry Andric for (BasicBlock &B : F) 178*0b57cec5SDimitry Andric for (Instruction &II : B) 179*0b57cec5SDimitry Andric if (match(&II, m_Intrinsic<Intrinsic::assume>())) 180*0b57cec5SDimitry Andric AssumeHandles.push_back(&II); 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric // Mark the scan as complete. 183*0b57cec5SDimitry Andric Scanned = true; 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric // Update affected values. 186*0b57cec5SDimitry Andric for (auto &A : AssumeHandles) 187*0b57cec5SDimitry Andric updateAffectedValues(cast<CallInst>(A)); 188*0b57cec5SDimitry Andric } 189*0b57cec5SDimitry Andric 190*0b57cec5SDimitry Andric void AssumptionCache::registerAssumption(CallInst *CI) { 191*0b57cec5SDimitry Andric assert(match(CI, m_Intrinsic<Intrinsic::assume>()) && 192*0b57cec5SDimitry Andric "Registered call does not call @llvm.assume"); 193*0b57cec5SDimitry Andric 194*0b57cec5SDimitry Andric // If we haven't scanned the function yet, just drop this assumption. It will 195*0b57cec5SDimitry Andric // be found when we scan later. 196*0b57cec5SDimitry Andric if (!Scanned) 197*0b57cec5SDimitry Andric return; 198*0b57cec5SDimitry Andric 199*0b57cec5SDimitry Andric AssumeHandles.push_back(CI); 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric #ifndef NDEBUG 202*0b57cec5SDimitry Andric assert(CI->getParent() && 203*0b57cec5SDimitry Andric "Cannot register @llvm.assume call not in a basic block"); 204*0b57cec5SDimitry Andric assert(&F == CI->getParent()->getParent() && 205*0b57cec5SDimitry Andric "Cannot register @llvm.assume call not in this function"); 206*0b57cec5SDimitry Andric 207*0b57cec5SDimitry Andric // We expect the number of assumptions to be small, so in an asserts build 208*0b57cec5SDimitry Andric // check that we don't accumulate duplicates and that all assumptions point 209*0b57cec5SDimitry Andric // to the same function. 210*0b57cec5SDimitry Andric SmallPtrSet<Value *, 16> AssumptionSet; 211*0b57cec5SDimitry Andric for (auto &VH : AssumeHandles) { 212*0b57cec5SDimitry Andric if (!VH) 213*0b57cec5SDimitry Andric continue; 214*0b57cec5SDimitry Andric 215*0b57cec5SDimitry Andric assert(&F == cast<Instruction>(VH)->getParent()->getParent() && 216*0b57cec5SDimitry Andric "Cached assumption not inside this function!"); 217*0b57cec5SDimitry Andric assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) && 218*0b57cec5SDimitry Andric "Cached something other than a call to @llvm.assume!"); 219*0b57cec5SDimitry Andric assert(AssumptionSet.insert(VH).second && 220*0b57cec5SDimitry Andric "Cache contains multiple copies of a call!"); 221*0b57cec5SDimitry Andric } 222*0b57cec5SDimitry Andric #endif 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric updateAffectedValues(CI); 225*0b57cec5SDimitry Andric } 226*0b57cec5SDimitry Andric 227*0b57cec5SDimitry Andric AnalysisKey AssumptionAnalysis::Key; 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric PreservedAnalyses AssumptionPrinterPass::run(Function &F, 230*0b57cec5SDimitry Andric FunctionAnalysisManager &AM) { 231*0b57cec5SDimitry Andric AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F); 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric OS << "Cached assumptions for function: " << F.getName() << "\n"; 234*0b57cec5SDimitry Andric for (auto &VH : AC.assumptions()) 235*0b57cec5SDimitry Andric if (VH) 236*0b57cec5SDimitry Andric OS << " " << *cast<CallInst>(VH)->getArgOperand(0) << "\n"; 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric return PreservedAnalyses::all(); 239*0b57cec5SDimitry Andric } 240*0b57cec5SDimitry Andric 241*0b57cec5SDimitry Andric void AssumptionCacheTracker::FunctionCallbackVH::deleted() { 242*0b57cec5SDimitry Andric auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr())); 243*0b57cec5SDimitry Andric if (I != ACT->AssumptionCaches.end()) 244*0b57cec5SDimitry Andric ACT->AssumptionCaches.erase(I); 245*0b57cec5SDimitry Andric // 'this' now dangles! 246*0b57cec5SDimitry Andric } 247*0b57cec5SDimitry Andric 248*0b57cec5SDimitry Andric AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) { 249*0b57cec5SDimitry Andric // We probe the function map twice to try and avoid creating a value handle 250*0b57cec5SDimitry Andric // around the function in common cases. This makes insertion a bit slower, 251*0b57cec5SDimitry Andric // but if we have to insert we're going to scan the whole function so that 252*0b57cec5SDimitry Andric // shouldn't matter. 253*0b57cec5SDimitry Andric auto I = AssumptionCaches.find_as(&F); 254*0b57cec5SDimitry Andric if (I != AssumptionCaches.end()) 255*0b57cec5SDimitry Andric return *I->second; 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric // Ok, build a new cache by scanning the function, insert it and the value 258*0b57cec5SDimitry Andric // handle into our map, and return the newly populated cache. 259*0b57cec5SDimitry Andric auto IP = AssumptionCaches.insert(std::make_pair( 2608bcb0991SDimitry Andric FunctionCallbackVH(&F, this), std::make_unique<AssumptionCache>(F))); 261*0b57cec5SDimitry Andric assert(IP.second && "Scanning function already in the map?"); 262*0b57cec5SDimitry Andric return *IP.first->second; 263*0b57cec5SDimitry Andric } 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric AssumptionCache *AssumptionCacheTracker::lookupAssumptionCache(Function &F) { 266*0b57cec5SDimitry Andric auto I = AssumptionCaches.find_as(&F); 267*0b57cec5SDimitry Andric if (I != AssumptionCaches.end()) 268*0b57cec5SDimitry Andric return I->second.get(); 269*0b57cec5SDimitry Andric return nullptr; 270*0b57cec5SDimitry Andric } 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric void AssumptionCacheTracker::verifyAnalysis() const { 273*0b57cec5SDimitry Andric // FIXME: In the long term the verifier should not be controllable with a 274*0b57cec5SDimitry Andric // flag. We should either fix all passes to correctly update the assumption 275*0b57cec5SDimitry Andric // cache and enable the verifier unconditionally or somehow arrange for the 276*0b57cec5SDimitry Andric // assumption list to be updated automatically by passes. 277*0b57cec5SDimitry Andric if (!VerifyAssumptionCache) 278*0b57cec5SDimitry Andric return; 279*0b57cec5SDimitry Andric 280*0b57cec5SDimitry Andric SmallPtrSet<const CallInst *, 4> AssumptionSet; 281*0b57cec5SDimitry Andric for (const auto &I : AssumptionCaches) { 282*0b57cec5SDimitry Andric for (auto &VH : I.second->assumptions()) 283*0b57cec5SDimitry Andric if (VH) 284*0b57cec5SDimitry Andric AssumptionSet.insert(cast<CallInst>(VH)); 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric for (const BasicBlock &B : cast<Function>(*I.first)) 287*0b57cec5SDimitry Andric for (const Instruction &II : B) 288*0b57cec5SDimitry Andric if (match(&II, m_Intrinsic<Intrinsic::assume>()) && 289*0b57cec5SDimitry Andric !AssumptionSet.count(cast<CallInst>(&II))) 290*0b57cec5SDimitry Andric report_fatal_error("Assumption in scanned function not in cache"); 291*0b57cec5SDimitry Andric } 292*0b57cec5SDimitry Andric } 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) { 295*0b57cec5SDimitry Andric initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry()); 296*0b57cec5SDimitry Andric } 297*0b57cec5SDimitry Andric 298*0b57cec5SDimitry Andric AssumptionCacheTracker::~AssumptionCacheTracker() = default; 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric char AssumptionCacheTracker::ID = 0; 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker", 303*0b57cec5SDimitry Andric "Assumption Cache Tracker", false, true) 304