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;
28 VerifyAssumptionCache("verify-assumption-cache", cl::Hidden,
29 cl::desc("Enable verification of assumption cache"),
32 SmallVector<WeakVH, 1> &AssumptionCache::getOrInsertAffectedValues(Value *V) {
33 // Try using find_as first to avoid creating extra value handles just for the
34 // purpose of doing the lookup.
35 auto AVI = AffectedValues.find_as(V);
36 if (AVI != AffectedValues.end())
39 auto AVIP = AffectedValues.insert({
40 AffectedValueCallbackVH(V, this), SmallVector<WeakVH, 1>()});
41 return AVIP.first->second;
44 void AssumptionCache::updateAffectedValues(CallInst *CI) {
45 // Note: This code must be kept in-sync with the code in
46 // computeKnownBitsFromAssume in ValueTracking.
48 SmallVector<Value *, 16> Affected;
49 auto AddAffected = [&Affected](Value *V) {
50 if (isa<Argument>(V)) {
51 Affected.push_back(V);
52 } else if (auto *I = dyn_cast<Instruction>(V)) {
53 Affected.push_back(I);
55 // Peek through unary operators to find the source of the condition.
57 if (match(I, m_BitCast(m_Value(Op))) ||
58 match(I, m_PtrToInt(m_Value(Op))) ||
59 match(I, m_Not(m_Value(Op)))) {
60 if (isa<Instruction>(Op) || isa<Argument>(Op))
61 Affected.push_back(Op);
66 Value *Cond = CI->getArgOperand(0), *A, *B;
69 CmpInst::Predicate Pred;
70 if (match(Cond, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
74 if (Pred == ICmpInst::ICMP_EQ) {
75 // For equality comparisons, we handle the case of bit inversion.
76 auto AddAffectedFromEq = [&AddAffected](Value *V) {
78 if (match(V, m_Not(m_Value(A)))) {
85 // (A & B) or (A | B) or (A ^ B).
87 m_CombineOr(m_And(m_Value(A), m_Value(B)),
88 m_CombineOr(m_Or(m_Value(A), m_Value(B)),
89 m_Xor(m_Value(A), m_Value(B)))))) {
92 // (A << C) or (A >>_s C) or (A >>_u C) where C is some constant.
94 m_CombineOr(m_Shl(m_Value(A), m_ConstantInt(C)),
95 m_CombineOr(m_LShr(m_Value(A), m_ConstantInt(C)),
97 m_ConstantInt(C)))))) {
102 AddAffectedFromEq(A);
103 AddAffectedFromEq(B);
107 for (auto &AV : Affected) {
108 auto &AVV = getOrInsertAffectedValues(AV);
109 if (std::find(AVV.begin(), AVV.end(), CI) == AVV.end())
114 void AssumptionCache::AffectedValueCallbackVH::deleted() {
115 auto AVI = AC->AffectedValues.find(getValPtr());
116 if (AVI != AC->AffectedValues.end())
117 AC->AffectedValues.erase(AVI);
118 // 'this' now dangles!
121 void AssumptionCache::copyAffectedValuesInCache(Value *OV, Value *NV) {
122 auto &NAVV = getOrInsertAffectedValues(NV);
123 auto AVI = AffectedValues.find(OV);
124 if (AVI == AffectedValues.end())
127 for (auto &A : AVI->second)
128 if (std::find(NAVV.begin(), NAVV.end(), A) == NAVV.end())
132 void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
133 if (!isa<Instruction>(NV) && !isa<Argument>(NV))
136 // Any assumptions that affected this value now affect the new value.
138 AC->copyAffectedValuesInCache(getValPtr(), NV);
139 // 'this' now might dangle! If the AffectedValues map was resized to add an
140 // entry for NV then this object might have been destroyed in favor of some
141 // copy in the grown map.
144 void AssumptionCache::scanFunction() {
145 assert(!Scanned && "Tried to scan the function twice!");
146 assert(AssumeHandles.empty() && "Already have assumes when scanning!");
148 // Go through all instructions in all blocks, add all calls to @llvm.assume
150 for (BasicBlock &B : F)
151 for (Instruction &II : B)
152 if (match(&II, m_Intrinsic<Intrinsic::assume>()))
153 AssumeHandles.push_back(&II);
155 // Mark the scan as complete.
158 // Update affected values.
159 for (auto &A : AssumeHandles)
160 updateAffectedValues(cast<CallInst>(A));
163 void AssumptionCache::registerAssumption(CallInst *CI) {
164 assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
165 "Registered call does not call @llvm.assume");
167 // If we haven't scanned the function yet, just drop this assumption. It will
168 // be found when we scan later.
172 AssumeHandles.push_back(CI);
175 assert(CI->getParent() &&
176 "Cannot register @llvm.assume call not in a basic block");
177 assert(&F == CI->getParent()->getParent() &&
178 "Cannot register @llvm.assume call not in this function");
180 // We expect the number of assumptions to be small, so in an asserts build
181 // check that we don't accumulate duplicates and that all assumptions point
182 // to the same function.
183 SmallPtrSet<Value *, 16> AssumptionSet;
184 for (auto &VH : AssumeHandles) {
188 assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
189 "Cached assumption not inside this function!");
190 assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
191 "Cached something other than a call to @llvm.assume!");
192 assert(AssumptionSet.insert(VH).second &&
193 "Cache contains multiple copies of a call!");
197 updateAffectedValues(CI);
200 AnalysisKey AssumptionAnalysis::Key;
202 PreservedAnalyses AssumptionPrinterPass::run(Function &F,
203 FunctionAnalysisManager &AM) {
204 AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
206 OS << "Cached assumptions for function: " << F.getName() << "\n";
207 for (auto &VH : AC.assumptions())
209 OS << " " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
211 return PreservedAnalyses::all();
214 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
215 auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
216 if (I != ACT->AssumptionCaches.end())
217 ACT->AssumptionCaches.erase(I);
218 // 'this' now dangles!
221 AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
222 // We probe the function map twice to try and avoid creating a value handle
223 // around the function in common cases. This makes insertion a bit slower,
224 // but if we have to insert we're going to scan the whole function so that
226 auto I = AssumptionCaches.find_as(&F);
227 if (I != AssumptionCaches.end())
230 // Ok, build a new cache by scanning the function, insert it and the value
231 // handle into our map, and return the newly populated cache.
232 auto IP = AssumptionCaches.insert(std::make_pair(
233 FunctionCallbackVH(&F, this), llvm::make_unique<AssumptionCache>(F)));
234 assert(IP.second && "Scanning function already in the map?");
235 return *IP.first->second;
238 void AssumptionCacheTracker::verifyAnalysis() const {
239 // FIXME: In the long term the verifier should not be controllable with a
240 // flag. We should either fix all passes to correctly update the assumption
241 // cache and enable the verifier unconditionally or somehow arrange for the
242 // assumption list to be updated automatically by passes.
243 if (!VerifyAssumptionCache)
246 SmallPtrSet<const CallInst *, 4> AssumptionSet;
247 for (const auto &I : AssumptionCaches) {
248 for (auto &VH : I.second->assumptions())
250 AssumptionSet.insert(cast<CallInst>(VH));
252 for (const BasicBlock &B : cast<Function>(*I.first))
253 for (const Instruction &II : B)
254 if (match(&II, m_Intrinsic<Intrinsic::assume>()) &&
255 !AssumptionSet.count(cast<CallInst>(&II)))
256 report_fatal_error("Assumption in scanned function not in cache");
260 AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
261 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
264 AssumptionCacheTracker::~AssumptionCacheTracker() {}
266 INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
267 "Assumption Cache Tracker", false, true)
268 char AssumptionCacheTracker::ID = 0;