LLVM  10.0.0
AssumptionCache.cpp
Go to the documentation of this file.
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 
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"
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 
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 
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 
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 
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 
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 
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 
266  auto I = AssumptionCaches.find_as(&F);
267  if (I != AssumptionCaches.end())
268  return I->second.get();
269  return nullptr;
270 }
271 
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.
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 
296 }
297 
299 
301 
302 INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
303  "Assumption Cache Tracker", false, true)
static void findAffectedValues(CallInst *CI, SmallVectorImpl< Value *> &Affected)
uint64_t CallInst * C
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:71
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DiagnosticInfoOptimizationBase::Argument NV
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void push_back(const T &Elt)
Definition: SmallVector.h:211
This class represents a function call, abstracting a target machine&#39;s calling convention.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
F(f)
MutableArrayRef< WeakTrackingVH > assumptions()
Access the list of assumption handles currently tracked for this function.
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1246
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
void unregisterAssumption(CallInst *CI)
Remove an @llvm.assume intrinsic from this function&#39;s cache if it has been added to the cache earlier...
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:200
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:82
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:732
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1222
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1203
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:33
A function analysis which provides an AssumptionCache.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:253
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
void updateAffectedValues(CallInst *CI)
Update the cache of values being affected by this assumption (i.e.
AssumptionCache * lookupAssumptionCache(Function &F)
Return the cached assumptions for a function if it has already been scanned.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:467
void registerAssumption(CallInst *CI)
Add an @llvm.assume intrinsic to this function&#39;s cache.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
static const Function * getParent(const Value *V)
static cl::opt< bool > VerifyAssumptionCache("verify-assumption-cache", cl::Hidden, cl::desc("Enable verification of assumption cache"), cl::init(false))
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
A container for analyses that lazily runs them and caches their results.
void initializeAssumptionCacheTrackerPass(PassRegistry &)
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a &#39;Not&#39; as &#39;xor V, -1&#39; or &#39;xor -1, V&#39;.
const BasicBlock * getParent() const
Definition: Instruction.h:66
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)