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/PatternMatch.h"
22 #include "llvm/Support/Debug.h"
24 using namespace llvm::PatternMatch;
26 void AssumptionCache::scanFunction() {
27 assert(!Scanned && "Tried to scan the function twice!");
28 assert(AssumeHandles.empty() && "Already have assumes when scanning!");
30 // Go through all instructions in all blocks, add all calls to @llvm.assume
32 for (BasicBlock &B : F)
33 for (Instruction &II : B)
34 if (match(&II, m_Intrinsic<Intrinsic::assume>()))
35 AssumeHandles.push_back(&II);
37 // Mark the scan as complete.
41 void AssumptionCache::registerAssumption(CallInst *CI) {
42 assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
43 "Registered call does not call @llvm.assume");
45 // If we haven't scanned the function yet, just drop this assumption. It will
46 // be found when we scan later.
50 AssumeHandles.push_back(CI);
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");
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) {
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!");
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!
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
88 auto I = AssumptionCaches.find_as(&F);
89 if (I != AssumptionCaches.end())
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;
100 void AssumptionCacheTracker::verifyAnalysis() const {
102 SmallPtrSet<const CallInst *, 4> AssumptionSet;
103 for (const auto &I : AssumptionCaches) {
104 for (auto &VH : I.second->assumptions())
106 AssumptionSet.insert(cast<CallInst>(VH));
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");
117 AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
118 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
121 AssumptionCacheTracker::~AssumptionCacheTracker() {}
123 INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
124 "Assumption Cache Tracker", false, true)
125 char AssumptionCacheTracker::ID = 0;