1 //===- AssumptionTracker.cpp - Track @llvm.assume -------------------------===//
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/AssumptionTracker.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 AssumptionTracker::FunctionCallbackVH::deleted() {
27 AT->forgetCachedAssumptions(cast<Function>(getValPtr()));
28 // 'this' now dangles!
31 void AssumptionTracker::forgetCachedAssumptions(Function *F) {
32 auto I = CachedAssumeCalls.find_as(F);
33 if (I != CachedAssumeCalls.end())
34 CachedAssumeCalls.erase(I);
37 void AssumptionTracker::CallCallbackVH::deleted() {
38 assert(F && "delete callback called on dummy handle");
39 FunctionCallsMap::iterator I = AT->CachedAssumeCalls.find_as(F);
40 assert(I != AT->CachedAssumeCalls.end() &&
41 "Function cleared from the map without removing the values?");
43 I->second->erase(*this);
44 // 'this' now dangles!
47 AssumptionTracker::FunctionCallsMap::iterator
48 AssumptionTracker::scanFunction(Function *F) {
49 auto IP = CachedAssumeCalls.insert(std::make_pair(
50 FunctionCallbackVH(F, this), llvm::make_unique<CallHandleSet>()));
51 assert(IP.second && "Scanning function already in the map?");
53 FunctionCallsMap::iterator I = IP.first;
55 // Go through all instructions in all blocks, add all calls to @llvm.assume
57 for (BasicBlock &B : *F)
58 for (Instruction &II : B)
59 if (match(&II, m_Intrinsic<Intrinsic::assume>()))
60 I->second->insert(CallCallbackVH(&II, this));
65 void AssumptionTracker::verifyAnalysis() const {
67 for (const auto &I : CachedAssumeCalls) {
68 for (const BasicBlock &B : cast<Function>(*I.first))
69 for (const Instruction &II : B) {
70 if (match(&II, m_Intrinsic<Intrinsic::assume>())) {
71 assert(I.second->find_as(&II) != I.second->end() &&
72 "Assumption in scanned function not in cache");
79 void AssumptionTracker::registerAssumption(CallInst *CI) {
80 assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
81 "Registered call does not call @llvm.assume");
82 assert(CI->getParent() &&
83 "Cannot register @llvm.assume call not in a basic block");
85 Function *F = CI->getParent()->getParent();
86 assert(F && "Cannot register @llvm.assume call not in a function");
88 FunctionCallsMap::iterator I = CachedAssumeCalls.find_as(F);
89 if (I == CachedAssumeCalls.end()) {
90 // If this function has not already been scanned, then don't do anything
91 // here. This intrinsic will be found, if it still exists, if the list of
92 // assumptions in this function is requested at some later point. This
93 // maintains the following invariant: if a function is present in the
94 // cache, then its list of assumption intrinsic calls is complete.
98 I->second->insert(CallCallbackVH(CI, this));
101 AssumptionTracker::AssumptionTracker() : ImmutablePass(ID) {
102 initializeAssumptionTrackerPass(*PassRegistry::getPassRegistry());
105 AssumptionTracker::~AssumptionTracker() {}
107 INITIALIZE_PASS(AssumptionTracker, "assumption-tracker", "Assumption Tracker",
109 char AssumptionTracker::ID = 0;