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