1 //===- AssumptionCache.cpp - Cache finding @llvm.assume calls -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a pass that keeps track of @llvm.assume intrinsics in
10 // the functions of a module.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/AssumptionCache.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/InstrTypes.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/IR/PatternMatch.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <utility>
35 
36 using namespace llvm;
37 using namespace llvm::PatternMatch;
38 
39 static cl::opt<bool>
40     VerifyAssumptionCache("verify-assumption-cache", cl::Hidden,
41                           cl::desc("Enable verification of assumption cache"),
42                           cl::init(false));
43 
44 SmallVector<WeakTrackingVH, 1> &
45 AssumptionCache::getOrInsertAffectedValues(Value *V) {
46   // Try using find_as first to avoid creating extra value handles just for the
47   // purpose of doing the lookup.
48   auto AVI = AffectedValues.find_as(V);
49   if (AVI != AffectedValues.end())
50     return AVI->second;
51 
52   auto AVIP = AffectedValues.insert(
53       {AffectedValueCallbackVH(V, this), SmallVector<WeakTrackingVH, 1>()});
54   return AVIP.first->second;
55 }
56 
57 static void findAffectedValues(CallInst *CI,
58                                SmallVectorImpl<Value *> &Affected) {
59   // Note: This code must be kept in-sync with the code in
60   // computeKnownBitsFromAssume in ValueTracking.
61 
62   auto AddAffected = [&Affected](Value *V) {
63     if (isa<Argument>(V)) {
64       Affected.push_back(V);
65     } else if (auto *I = dyn_cast<Instruction>(V)) {
66       Affected.push_back(I);
67 
68       // Peek through unary operators to find the source of the condition.
69       Value *Op;
70       if (match(I, m_BitCast(m_Value(Op))) ||
71           match(I, m_PtrToInt(m_Value(Op))) ||
72           match(I, m_Not(m_Value(Op)))) {
73         if (isa<Instruction>(Op) || isa<Argument>(Op))
74           Affected.push_back(Op);
75       }
76     }
77   };
78 
79   Value *Cond = CI->getArgOperand(0), *A, *B;
80   AddAffected(Cond);
81 
82   CmpInst::Predicate Pred;
83   if (match(Cond, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
84     AddAffected(A);
85     AddAffected(B);
86 
87     if (Pred == ICmpInst::ICMP_EQ) {
88       // For equality comparisons, we handle the case of bit inversion.
89       auto AddAffectedFromEq = [&AddAffected](Value *V) {
90         Value *A;
91         if (match(V, m_Not(m_Value(A)))) {
92           AddAffected(A);
93           V = A;
94         }
95 
96         Value *B;
97         ConstantInt *C;
98         // (A & B) or (A | B) or (A ^ B).
99         if (match(V, m_BitwiseLogic(m_Value(A), m_Value(B)))) {
100           AddAffected(A);
101           AddAffected(B);
102         // (A << C) or (A >>_s C) or (A >>_u C) where C is some constant.
103         } else if (match(V, m_Shift(m_Value(A), m_ConstantInt(C)))) {
104           AddAffected(A);
105         }
106       };
107 
108       AddAffectedFromEq(A);
109       AddAffectedFromEq(B);
110     }
111   }
112 }
113 
114 void AssumptionCache::updateAffectedValues(CallInst *CI) {
115   SmallVector<Value *, 16> Affected;
116   findAffectedValues(CI, Affected);
117 
118   for (auto &AV : Affected) {
119     auto &AVV = getOrInsertAffectedValues(AV);
120     if (std::find(AVV.begin(), AVV.end(), CI) == AVV.end())
121       AVV.push_back(CI);
122   }
123 }
124 
125 void AssumptionCache::unregisterAssumption(CallInst *CI) {
126   SmallVector<Value *, 16> Affected;
127   findAffectedValues(CI, Affected);
128 
129   for (auto &AV : Affected) {
130     auto AVI = AffectedValues.find_as(AV);
131     if (AVI != AffectedValues.end())
132       AffectedValues.erase(AVI);
133   }
134 
135   AssumeHandles.erase(
136       remove_if(AssumeHandles, [CI](WeakTrackingVH &VH) { return CI == VH; }),
137       AssumeHandles.end());
138 }
139 
140 void AssumptionCache::AffectedValueCallbackVH::deleted() {
141   auto AVI = AC->AffectedValues.find(getValPtr());
142   if (AVI != AC->AffectedValues.end())
143     AC->AffectedValues.erase(AVI);
144   // 'this' now dangles!
145 }
146 
147 void AssumptionCache::transferAffectedValuesInCache(Value *OV, Value *NV) {
148   auto &NAVV = getOrInsertAffectedValues(NV);
149   auto AVI = AffectedValues.find(OV);
150   if (AVI == AffectedValues.end())
151     return;
152 
153   for (auto &A : AVI->second)
154     if (std::find(NAVV.begin(), NAVV.end(), A) == NAVV.end())
155       NAVV.push_back(A);
156   AffectedValues.erase(OV);
157 }
158 
159 void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
160   if (!isa<Instruction>(NV) && !isa<Argument>(NV))
161     return;
162 
163   // Any assumptions that affected this value now affect the new value.
164 
165   AC->transferAffectedValuesInCache(getValPtr(), NV);
166   // 'this' now might dangle! If the AffectedValues map was resized to add an
167   // entry for NV then this object might have been destroyed in favor of some
168   // copy in the grown map.
169 }
170 
171 void AssumptionCache::scanFunction() {
172   assert(!Scanned && "Tried to scan the function twice!");
173   assert(AssumeHandles.empty() && "Already have assumes when scanning!");
174 
175   // Go through all instructions in all blocks, add all calls to @llvm.assume
176   // to this cache.
177   for (BasicBlock &B : F)
178     for (Instruction &II : B)
179       if (match(&II, m_Intrinsic<Intrinsic::assume>()))
180         AssumeHandles.push_back(&II);
181 
182   // Mark the scan as complete.
183   Scanned = true;
184 
185   // Update affected values.
186   for (auto &A : AssumeHandles)
187     updateAffectedValues(cast<CallInst>(A));
188 }
189 
190 void AssumptionCache::registerAssumption(CallInst *CI) {
191   assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
192          "Registered call does not call @llvm.assume");
193 
194   // If we haven't scanned the function yet, just drop this assumption. It will
195   // be found when we scan later.
196   if (!Scanned)
197     return;
198 
199   AssumeHandles.push_back(CI);
200 
201 #ifndef NDEBUG
202   assert(CI->getParent() &&
203          "Cannot register @llvm.assume call not in a basic block");
204   assert(&F == CI->getParent()->getParent() &&
205          "Cannot register @llvm.assume call not in this function");
206 
207   // We expect the number of assumptions to be small, so in an asserts build
208   // check that we don't accumulate duplicates and that all assumptions point
209   // to the same function.
210   SmallPtrSet<Value *, 16> AssumptionSet;
211   for (auto &VH : AssumeHandles) {
212     if (!VH)
213       continue;
214 
215     assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
216            "Cached assumption not inside this function!");
217     assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
218            "Cached something other than a call to @llvm.assume!");
219     assert(AssumptionSet.insert(VH).second &&
220            "Cache contains multiple copies of a call!");
221   }
222 #endif
223 
224   updateAffectedValues(CI);
225 }
226 
227 AnalysisKey AssumptionAnalysis::Key;
228 
229 PreservedAnalyses AssumptionPrinterPass::run(Function &F,
230                                              FunctionAnalysisManager &AM) {
231   AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
232 
233   OS << "Cached assumptions for function: " << F.getName() << "\n";
234   for (auto &VH : AC.assumptions())
235     if (VH)
236       OS << "  " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
237 
238   return PreservedAnalyses::all();
239 }
240 
241 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
242   auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
243   if (I != ACT->AssumptionCaches.end())
244     ACT->AssumptionCaches.erase(I);
245   // 'this' now dangles!
246 }
247 
248 AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
249   // We probe the function map twice to try and avoid creating a value handle
250   // around the function in common cases. This makes insertion a bit slower,
251   // but if we have to insert we're going to scan the whole function so that
252   // shouldn't matter.
253   auto I = AssumptionCaches.find_as(&F);
254   if (I != AssumptionCaches.end())
255     return *I->second;
256 
257   // Ok, build a new cache by scanning the function, insert it and the value
258   // handle into our map, and return the newly populated cache.
259   auto IP = AssumptionCaches.insert(std::make_pair(
260       FunctionCallbackVH(&F, this), std::make_unique<AssumptionCache>(F)));
261   assert(IP.second && "Scanning function already in the map?");
262   return *IP.first->second;
263 }
264 
265 AssumptionCache *AssumptionCacheTracker::lookupAssumptionCache(Function &F) {
266   auto I = AssumptionCaches.find_as(&F);
267   if (I != AssumptionCaches.end())
268     return I->second.get();
269   return nullptr;
270 }
271 
272 void AssumptionCacheTracker::verifyAnalysis() const {
273   // FIXME: In the long term the verifier should not be controllable with a
274   // flag. We should either fix all passes to correctly update the assumption
275   // cache and enable the verifier unconditionally or somehow arrange for the
276   // assumption list to be updated automatically by passes.
277   if (!VerifyAssumptionCache)
278     return;
279 
280   SmallPtrSet<const CallInst *, 4> AssumptionSet;
281   for (const auto &I : AssumptionCaches) {
282     for (auto &VH : I.second->assumptions())
283       if (VH)
284         AssumptionSet.insert(cast<CallInst>(VH));
285 
286     for (const BasicBlock &B : cast<Function>(*I.first))
287       for (const Instruction &II : B)
288         if (match(&II, m_Intrinsic<Intrinsic::assume>()) &&
289             !AssumptionSet.count(cast<CallInst>(&II)))
290           report_fatal_error("Assumption in scanned function not in cache");
291   }
292 }
293 
294 AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
295   initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
296 }
297 
298 AssumptionCacheTracker::~AssumptionCacheTracker() = default;
299 
300 char AssumptionCacheTracker::ID = 0;
301 
302 INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
303                 "Assumption Cache Tracker", false, true)
304