blob: 3ff27890dc385b164c4f17db28cdb45fca0381b9 [file] [log] [blame]
Daniel Jasperf5123fe2016-12-19 08:32:13 +00001//===- 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/PassManager.h"
22#include "llvm/IR/PatternMatch.h"
23#include "llvm/Support/Debug.h"
24using namespace llvm;
25using namespace llvm::PatternMatch;
26
Peter Collingbourne9421c2d2017-02-15 21:10:09 +000027static cl::opt<bool>
28 VerifyAssumptionCache("verify-assumption-cache", cl::Hidden,
29 cl::desc("Enable verification of assumption cache"),
30 cl::init(false));
31
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000032SmallVector<WeakTrackingVH, 1> &
33AssumptionCache::getOrInsertAffectedValues(Value *V) {
Hal Finkel8a9a7832017-01-11 13:24:24 +000034 // Try using find_as first to avoid creating extra value handles just for the
35 // purpose of doing the lookup.
36 auto AVI = AffectedValues.find_as(V);
37 if (AVI != AffectedValues.end())
38 return AVI->second;
39
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000040 auto AVIP = AffectedValues.insert(
41 {AffectedValueCallbackVH(V, this), SmallVector<WeakTrackingVH, 1>()});
Hal Finkel8a9a7832017-01-11 13:24:24 +000042 return AVIP.first->second;
43}
44
45void AssumptionCache::updateAffectedValues(CallInst *CI) {
46 // Note: This code must be kept in-sync with the code in
47 // computeKnownBitsFromAssume in ValueTracking.
48
49 SmallVector<Value *, 16> Affected;
50 auto AddAffected = [&Affected](Value *V) {
51 if (isa<Argument>(V)) {
52 Affected.push_back(V);
53 } else if (auto *I = dyn_cast<Instruction>(V)) {
54 Affected.push_back(I);
55
Sanjay Patel96669962017-01-17 18:15:49 +000056 // Peek through unary operators to find the source of the condition.
57 Value *Op;
58 if (match(I, m_BitCast(m_Value(Op))) ||
59 match(I, m_PtrToInt(m_Value(Op))) ||
60 match(I, m_Not(m_Value(Op)))) {
Hal Finkel8a9a7832017-01-11 13:24:24 +000061 if (isa<Instruction>(Op) || isa<Argument>(Op))
62 Affected.push_back(Op);
63 }
64 }
65 };
66
67 Value *Cond = CI->getArgOperand(0), *A, *B;
68 AddAffected(Cond);
69
70 CmpInst::Predicate Pred;
71 if (match(Cond, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
72 AddAffected(A);
73 AddAffected(B);
74
75 if (Pred == ICmpInst::ICMP_EQ) {
76 // For equality comparisons, we handle the case of bit inversion.
77 auto AddAffectedFromEq = [&AddAffected](Value *V) {
78 Value *A;
79 if (match(V, m_Not(m_Value(A)))) {
80 AddAffected(A);
81 V = A;
82 }
83
84 Value *B;
85 ConstantInt *C;
86 // (A & B) or (A | B) or (A ^ B).
Craig Topper8bec6a42017-06-24 06:27:14 +000087 if (match(V, m_BitwiseLogic(m_Value(A), m_Value(B)))) {
Hal Finkel8a9a7832017-01-11 13:24:24 +000088 AddAffected(A);
89 AddAffected(B);
90 // (A << C) or (A >>_s C) or (A >>_u C) where C is some constant.
Craig Topper8bec6a42017-06-24 06:27:14 +000091 } else if (match(V, m_Shift(m_Value(A), m_ConstantInt(C)))) {
Hal Finkel8a9a7832017-01-11 13:24:24 +000092 AddAffected(A);
93 }
94 };
95
96 AddAffectedFromEq(A);
97 AddAffectedFromEq(B);
98 }
99 }
100
101 for (auto &AV : Affected) {
Hal Finkelc29d5f12017-01-16 15:22:01 +0000102 auto &AVV = getOrInsertAffectedValues(AV);
Hal Finkel8a9a7832017-01-11 13:24:24 +0000103 if (std::find(AVV.begin(), AVV.end(), CI) == AVV.end())
104 AVV.push_back(CI);
105 }
106}
107
108void AssumptionCache::AffectedValueCallbackVH::deleted() {
109 auto AVI = AC->AffectedValues.find(getValPtr());
110 if (AVI != AC->AffectedValues.end())
111 AC->AffectedValues.erase(AVI);
112 // 'this' now dangles!
113}
114
Hal Finkelc29d5f12017-01-16 15:22:01 +0000115void AssumptionCache::copyAffectedValuesInCache(Value *OV, Value *NV) {
116 auto &NAVV = getOrInsertAffectedValues(NV);
117 auto AVI = AffectedValues.find(OV);
118 if (AVI == AffectedValues.end())
119 return;
120
121 for (auto &A : AVI->second)
122 if (std::find(NAVV.begin(), NAVV.end(), A) == NAVV.end())
123 NAVV.push_back(A);
124}
125
Hal Finkel8a9a7832017-01-11 13:24:24 +0000126void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
127 if (!isa<Instruction>(NV) && !isa<Argument>(NV))
128 return;
129
130 // Any assumptions that affected this value now affect the new value.
131
Hal Finkelc29d5f12017-01-16 15:22:01 +0000132 AC->copyAffectedValuesInCache(getValPtr(), NV);
133 // 'this' now might dangle! If the AffectedValues map was resized to add an
134 // entry for NV then this object might have been destroyed in favor of some
135 // copy in the grown map.
Hal Finkel8a9a7832017-01-11 13:24:24 +0000136}
137
Daniel Jasperf5123fe2016-12-19 08:32:13 +0000138void AssumptionCache::scanFunction() {
139 assert(!Scanned && "Tried to scan the function twice!");
140 assert(AssumeHandles.empty() && "Already have assumes when scanning!");
141
142 // Go through all instructions in all blocks, add all calls to @llvm.assume
143 // to this cache.
144 for (BasicBlock &B : F)
145 for (Instruction &II : B)
146 if (match(&II, m_Intrinsic<Intrinsic::assume>()))
147 AssumeHandles.push_back(&II);
148
149 // Mark the scan as complete.
150 Scanned = true;
Hal Finkel8a9a7832017-01-11 13:24:24 +0000151
152 // Update affected values.
153 for (auto &A : AssumeHandles)
154 updateAffectedValues(cast<CallInst>(A));
Daniel Jasperf5123fe2016-12-19 08:32:13 +0000155}
156
157void AssumptionCache::registerAssumption(CallInst *CI) {
158 assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
159 "Registered call does not call @llvm.assume");
160
161 // If we haven't scanned the function yet, just drop this assumption. It will
162 // be found when we scan later.
163 if (!Scanned)
164 return;
165
166 AssumeHandles.push_back(CI);
167
168#ifndef NDEBUG
169 assert(CI->getParent() &&
170 "Cannot register @llvm.assume call not in a basic block");
171 assert(&F == CI->getParent()->getParent() &&
172 "Cannot register @llvm.assume call not in this function");
173
174 // We expect the number of assumptions to be small, so in an asserts build
175 // check that we don't accumulate duplicates and that all assumptions point
176 // to the same function.
177 SmallPtrSet<Value *, 16> AssumptionSet;
178 for (auto &VH : AssumeHandles) {
179 if (!VH)
180 continue;
181
182 assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
183 "Cached assumption not inside this function!");
184 assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
185 "Cached something other than a call to @llvm.assume!");
186 assert(AssumptionSet.insert(VH).second &&
187 "Cache contains multiple copies of a call!");
188 }
189#endif
Hal Finkel8a9a7832017-01-11 13:24:24 +0000190
191 updateAffectedValues(CI);
Daniel Jasperf5123fe2016-12-19 08:32:13 +0000192}
193
194AnalysisKey AssumptionAnalysis::Key;
195
196PreservedAnalyses AssumptionPrinterPass::run(Function &F,
197 FunctionAnalysisManager &AM) {
198 AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
199
200 OS << "Cached assumptions for function: " << F.getName() << "\n";
201 for (auto &VH : AC.assumptions())
202 if (VH)
203 OS << " " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
204
205 return PreservedAnalyses::all();
206}
207
208void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
209 auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
210 if (I != ACT->AssumptionCaches.end())
211 ACT->AssumptionCaches.erase(I);
212 // 'this' now dangles!
213}
214
215AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
216 // We probe the function map twice to try and avoid creating a value handle
217 // around the function in common cases. This makes insertion a bit slower,
218 // but if we have to insert we're going to scan the whole function so that
219 // shouldn't matter.
220 auto I = AssumptionCaches.find_as(&F);
221 if (I != AssumptionCaches.end())
222 return *I->second;
223
224 // Ok, build a new cache by scanning the function, insert it and the value
225 // handle into our map, and return the newly populated cache.
226 auto IP = AssumptionCaches.insert(std::make_pair(
227 FunctionCallbackVH(&F, this), llvm::make_unique<AssumptionCache>(F)));
228 assert(IP.second && "Scanning function already in the map?");
229 return *IP.first->second;
230}
231
232void AssumptionCacheTracker::verifyAnalysis() const {
Peter Collingbourne9421c2d2017-02-15 21:10:09 +0000233 // FIXME: In the long term the verifier should not be controllable with a
234 // flag. We should either fix all passes to correctly update the assumption
235 // cache and enable the verifier unconditionally or somehow arrange for the
236 // assumption list to be updated automatically by passes.
237 if (!VerifyAssumptionCache)
238 return;
239
Daniel Jasperf5123fe2016-12-19 08:32:13 +0000240 SmallPtrSet<const CallInst *, 4> AssumptionSet;
241 for (const auto &I : AssumptionCaches) {
242 for (auto &VH : I.second->assumptions())
243 if (VH)
244 AssumptionSet.insert(cast<CallInst>(VH));
245
246 for (const BasicBlock &B : cast<Function>(*I.first))
247 for (const Instruction &II : B)
Peter Collingbourne9421c2d2017-02-15 21:10:09 +0000248 if (match(&II, m_Intrinsic<Intrinsic::assume>()) &&
249 !AssumptionSet.count(cast<CallInst>(&II)))
250 report_fatal_error("Assumption in scanned function not in cache");
Daniel Jasperf5123fe2016-12-19 08:32:13 +0000251 }
Daniel Jasperf5123fe2016-12-19 08:32:13 +0000252}
253
254AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
255 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
256}
257
258AssumptionCacheTracker::~AssumptionCacheTracker() {}
259
260INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
261 "Assumption Cache Tracker", false, true)
262char AssumptionCacheTracker::ID = 0;