Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +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/PatternMatch.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | using namespace llvm; |
| 24 | using namespace llvm::PatternMatch; |
| 25 | |
| 26 | void AssumptionCache::scanFunction() { |
| 27 | assert(!Scanned && "Tried to scan the function twice!"); |
| 28 | assert(AssumeHandles.empty() && "Already have assumes when scanning!"); |
| 29 | |
| 30 | // Go through all instructions in all blocks, add all calls to @llvm.assume |
| 31 | // to this cache. |
| 32 | for (BasicBlock &B : F) |
| 33 | for (Instruction &II : B) |
| 34 | if (match(&II, m_Intrinsic<Intrinsic::assume>())) |
| 35 | AssumeHandles.push_back(&II); |
| 36 | |
| 37 | // Mark the scan as complete. |
| 38 | Scanned = true; |
| 39 | } |
| 40 | |
| 41 | void AssumptionCache::registerAssumption(CallInst *CI) { |
| 42 | assert(match(CI, m_Intrinsic<Intrinsic::assume>()) && |
| 43 | "Registered call does not call @llvm.assume"); |
| 44 | |
| 45 | // If we haven't scanned the function yet, just drop this assumption. It will |
| 46 | // be found when we scan later. |
| 47 | if (!Scanned) |
| 48 | return; |
| 49 | |
| 50 | AssumeHandles.push_back(CI); |
| 51 | |
| 52 | #ifndef NDEBUG |
| 53 | assert(CI->getParent() && |
| 54 | "Cannot register @llvm.assume call not in a basic block"); |
| 55 | assert(&F == CI->getParent()->getParent() && |
| 56 | "Cannot register @llvm.assume call not in this function"); |
| 57 | |
| 58 | // We expect the number of assumptions to be small, so in an asserts build |
| 59 | // check that we don't accumulate duplicates and that all assumptions point |
| 60 | // to the same function. |
| 61 | SmallPtrSet<Value *, 16> AssumptionSet; |
| 62 | for (auto &VH : AssumeHandles) { |
| 63 | if (!VH) |
| 64 | continue; |
| 65 | |
| 66 | assert(&F == cast<Instruction>(VH)->getParent()->getParent() && |
| 67 | "Cached assumption not inside this function!"); |
| 68 | assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) && |
| 69 | "Cached something other than a call to @llvm.assume!"); |
| 70 | assert(AssumptionSet.insert(VH).second && |
| 71 | "Cache contains multiple copies of a call!"); |
| 72 | } |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | void AssumptionCacheTracker::FunctionCallbackVH::deleted() { |
| 77 | auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr())); |
| 78 | if (I != ACT->AssumptionCaches.end()) |
| 79 | ACT->AssumptionCaches.erase(I); |
| 80 | // 'this' now dangles! |
| 81 | } |
| 82 | |
| 83 | AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) { |
| 84 | // We probe the function map twice to try and avoid creating a value handle |
| 85 | // around the function in common cases. This makes insertion a bit slower, |
| 86 | // but if we have to insert we're going to scan the whole function so that |
| 87 | // shouldn't matter. |
| 88 | auto I = AssumptionCaches.find_as(&F); |
| 89 | if (I != AssumptionCaches.end()) |
| 90 | return *I->second; |
| 91 | |
| 92 | // Ok, build a new cache by scanning the function, insert it and the value |
| 93 | // handle into our map, and return the newly populated cache. |
| 94 | auto IP = AssumptionCaches.insert(std::make_pair( |
| 95 | FunctionCallbackVH(&F, this), llvm::make_unique<AssumptionCache>(F))); |
| 96 | assert(IP.second && "Scanning function already in the map?"); |
| 97 | return *IP.first->second; |
| 98 | } |
| 99 | |
| 100 | void AssumptionCacheTracker::verifyAnalysis() const { |
| 101 | #ifndef NDEBUG |
| 102 | SmallPtrSet<const CallInst *, 4> AssumptionSet; |
| 103 | for (const auto &I : AssumptionCaches) { |
| 104 | for (auto &VH : I.second->assumptions()) |
| 105 | if (VH) |
| 106 | AssumptionSet.insert(cast<CallInst>(VH)); |
| 107 | |
| 108 | for (const BasicBlock &B : cast<Function>(*I.first)) |
| 109 | for (const Instruction &II : B) |
| 110 | if (match(&II, m_Intrinsic<Intrinsic::assume>())) |
| 111 | assert(AssumptionSet.count(cast<CallInst>(&II)) && |
| 112 | "Assumption in scanned function not in cache"); |
| 113 | } |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) { |
| 118 | initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry()); |
| 119 | } |
| 120 | |
| 121 | AssumptionCacheTracker::~AssumptionCacheTracker() {} |
| 122 | |
| 123 | INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker", |
| 124 | "Assumption Cache Tracker", false, true) |
| 125 | char AssumptionCacheTracker::ID = 0; |