1 //===- AssumptionCache.cpp - Cache finding @llvm.assume calls -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that keeps track of @llvm.assume intrinsics in
11 // the functions of a module.
13 //===----------------------------------------------------------------------===//
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"
25 using namespace llvm::PatternMatch;
27 void AssumptionCache::scanFunction() {
28 assert(!Scanned && "Tried to scan the function twice!");
29 assert(AssumeHandles.empty() && "Already have assumes when scanning!");
31 // Go through all instructions in all blocks, add all calls to @llvm.assume
33 for (BasicBlock &B : F)
34 for (Instruction &II : B)
35 if (match(&II, m_Intrinsic<Intrinsic::assume>()))
36 AssumeHandles.push_back(&II);
38 // Mark the scan as complete.
42 void AssumptionCache::registerAssumption(CallInst *CI) {
43 assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
44 "Registered call does not call @llvm.assume");
46 // If we haven't scanned the function yet, just drop this assumption. It will
47 // be found when we scan later.
51 AssumeHandles.push_back(CI);
54 assert(CI->getParent() &&
55 "Cannot register @llvm.assume call not in a basic block");
56 assert(&F == CI->getParent()->getParent() &&
57 "Cannot register @llvm.assume call not in this function");
59 // We expect the number of assumptions to be small, so in an asserts build
60 // check that we don't accumulate duplicates and that all assumptions point
61 // to the same function.
62 SmallPtrSet<Value *, 16> AssumptionSet;
63 for (auto &VH : AssumeHandles) {
67 assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
68 "Cached assumption not inside this function!");
69 assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
70 "Cached something other than a call to @llvm.assume!");
71 assert(AssumptionSet.insert(VH).second &&
72 "Cache contains multiple copies of a call!");
77 AnalysisKey AssumptionAnalysis::Key;
79 PreservedAnalyses AssumptionPrinterPass::run(Function &F,
80 FunctionAnalysisManager &AM) {
81 AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
83 OS << "Cached assumptions for function: " << F.getName() << "\n";
84 for (auto &VH : AC.assumptions())
86 OS << " " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
88 return PreservedAnalyses::all();
91 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
92 auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
93 if (I != ACT->AssumptionCaches.end())
94 ACT->AssumptionCaches.erase(I);
95 // 'this' now dangles!
98 AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
99 // We probe the function map twice to try and avoid creating a value handle
100 // around the function in common cases. This makes insertion a bit slower,
101 // but if we have to insert we're going to scan the whole function so that
103 auto I = AssumptionCaches.find_as(&F);
104 if (I != AssumptionCaches.end())
107 // Ok, build a new cache by scanning the function, insert it and the value
108 // handle into our map, and return the newly populated cache.
109 auto IP = AssumptionCaches.insert(std::make_pair(
110 FunctionCallbackVH(&F, this), llvm::make_unique<AssumptionCache>(F)));
111 assert(IP.second && "Scanning function already in the map?");
112 return *IP.first->second;
115 void AssumptionCacheTracker::verifyAnalysis() const {
117 SmallPtrSet<const CallInst *, 4> AssumptionSet;
118 for (const auto &I : AssumptionCaches) {
119 for (auto &VH : I.second->assumptions())
121 AssumptionSet.insert(cast<CallInst>(VH));
123 for (const BasicBlock &B : cast<Function>(*I.first))
124 for (const Instruction &II : B)
125 if (match(&II, m_Intrinsic<Intrinsic::assume>()))
126 assert(AssumptionSet.count(cast<CallInst>(&II)) &&
127 "Assumption in scanned function not in cache");
132 AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
133 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
136 AssumptionCacheTracker::~AssumptionCacheTracker() {}
138 INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
139 "Assumption Cache Tracker", false, true)
140 char AssumptionCacheTracker::ID = 0;