blob: 775ce1ddc84cb35ee2a41dddba05ed1a10c415e3 [file] [log] [blame]
Stephen Hines37ed9c12014-12-01 14:51:49 -08001//===- AssumptionTracker.cpp - Track @llvm.assume -------------------------===//
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/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"
23using namespace llvm;
24using namespace llvm::PatternMatch;
25
26void AssumptionTracker::FunctionCallbackVH::deleted() {
27 AT->forgetCachedAssumptions(cast<Function>(getValPtr()));
28 // 'this' now dangles!
29}
30
31void AssumptionTracker::forgetCachedAssumptions(Function *F) {
32 auto I = CachedAssumeCalls.find_as(F);
33 if (I != CachedAssumeCalls.end())
34 CachedAssumeCalls.erase(I);
35}
36
37void 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?");
42
43 I->second->erase(*this);
44 // 'this' now dangles!
45}
46
47AssumptionTracker::FunctionCallsMap::iterator
48AssumptionTracker::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?");
52
53 FunctionCallsMap::iterator I = IP.first;
54
55 // Go through all instructions in all blocks, add all calls to @llvm.assume
56 // to our cache.
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));
61
62 return I;
63}
64
65void AssumptionTracker::verifyAnalysis() const {
66#ifndef NDEBUG
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");
73 }
74 }
75 }
76#endif
77}
78
79void 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");
84
85 Function *F = CI->getParent()->getParent();
86 assert(F && "Cannot register @llvm.assume call not in a function");
87
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.
95 return;
96 }
97
98 I->second->insert(CallCallbackVH(CI, this));
99}
100
101AssumptionTracker::AssumptionTracker() : ImmutablePass(ID) {
102 initializeAssumptionTrackerPass(*PassRegistry::getPassRegistry());
103}
104
105AssumptionTracker::~AssumptionTracker() {}
106
107INITIALIZE_PASS(AssumptionTracker, "assumption-tracker", "Assumption Tracker",
108 false, true)
109char AssumptionTracker::ID = 0;
110