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