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