LLVM 23.0.0git
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"
22#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/InstrTypes.h"
25#include "llvm/IR/Instruction.h"
27#include "llvm/IR/PassManager.h"
30#include "llvm/Pass.h"
35#include <cassert>
36
37using namespace llvm;
38using namespace llvm::PatternMatch;
39
40static cl::opt<bool>
41 VerifyAssumptionCache("verify-assumption-cache", cl::Hidden,
42 cl::desc("Enable verification of assumption cache"),
43 cl::init(false));
44
46AssumptionCache::getOrInsertAffectedValues(Value *V) {
47 // Try using find_as first to avoid creating extra value handles just for the
48 // purpose of doing the lookup.
49 auto AVI = AffectedValues.find_as(V);
50 if (AVI != AffectedValues.end())
51 return AVI->second;
52
53 return AffectedValues[AffectedValueCallbackVH(V, this)];
54}
55
57 OperandBundleUse Bundle, function_ref<void(Value *)> InsertAffected) {
58 auto AddAffectedVal = [&](Value *V) {
60 InsertAffected(V);
61 };
62
63 if (Bundle.getTagName() == "separate_storage") {
64 assert(Bundle.Inputs.size() == 2 && "separate_storage must have two args");
65 AddAffectedVal(getUnderlyingObject(Bundle.Inputs[0]));
66 AddAffectedVal(getUnderlyingObject(Bundle.Inputs[1]));
67 } else if (Bundle.Inputs.size() > ABA_WasOn &&
68 Bundle.getTagName() != IgnoreBundleTag)
69 AddAffectedVal(Bundle.Inputs[ABA_WasOn]);
70}
71
72static void
75 // Note: This code must be kept in-sync with the code in
76 // computeKnownBitsFromAssume in ValueTracking.
77
78 auto InsertAffected = [&Affected](Value *V) {
80 };
81
82 auto AddAffectedVal = [&Affected](Value *V, unsigned Idx) {
84 Affected.push_back({V, Idx});
85 }
86 };
87
88 for (unsigned Idx = 0; Idx != CI->getNumOperandBundles(); Idx++)
90 CI->getOperandBundleAt(Idx),
91 [&](Value *V) { Affected.push_back({V, Idx}); });
92
93 Value *Cond = CI->getArgOperand(0);
94 findValuesAffectedByCondition(Cond, /*IsAssume=*/true, InsertAffected);
95
96 if (TTI) {
97 const Value *Ptr;
98 unsigned AS;
99 std::tie(Ptr, AS) = TTI->getPredicatedAddrSpace(Cond);
100 if (Ptr)
101 AddAffectedVal(const_cast<Value *>(Ptr->stripInBoundsOffsets()),
103 }
104}
105
108 findAffectedValues(CI, TTI, Affected);
109
110 for (auto &AV : Affected) {
111 auto &AVV = getOrInsertAffectedValues(AV.Assume);
112 if (llvm::none_of(AVV, [&](ResultElem &Elem) {
113 return Elem.Assume == CI && Elem.Index == AV.Index;
114 }))
115 AVV.push_back({CI, AV.Index});
116 }
117}
118
119void AssumptionCache::removeAffectedValues(AssumeInst *CI) {
121 findAffectedValues(CI, TTI, Affected);
122
123 // If a value appears more than once in an AssumeInst e.g., 'ptr %arg1' in:
124 // call void @llvm.assume(i1 true)
125 // [ "dereferenceable"(ptr %arg1, i64 1),
126 // "align"(ptr %arg1, i64 8) ]
127 // it will appear multiple times in Affected, but we may (depending on
128 // how the results in AffectedValues.find_as(AV.Assume) are ordered)
129 // nullify multiple instances of Elem.Assume during one iteration of the
130 // 'for (auto &AV : Affected)' loop below. The next iteration of that for
131 // loop may then find only a match to a different AssumeInst, resulting in
132 // an assertion failure. Avoid this by counting the number of expected
133 // matches.
134#ifndef NDEBUG
135 DenseMap<Value *, int> ExpectedMatches;
136 for (auto &AV : Affected)
137 if (AffectedValues.find_as(AV.Assume) != AffectedValues.end())
138 ExpectedMatches[AV.Assume]++;
139#endif
140
141 for (auto &AV : Affected) {
142 auto AVI = AffectedValues.find_as(AV.Assume);
143 if (AVI == AffectedValues.end())
144 continue;
145 bool Found = false;
146 bool HasNonnull = false;
147 for (ResultElem &Elem : AVI->second) {
148 if (Elem.Assume == CI) {
149 Found = true;
150 Elem.Assume = nullptr;
151
152#ifndef NDEBUG
153 ExpectedMatches[AV.Assume]--;
154#endif
155 assert(ExpectedMatches[AV.Assume] >= 0);
156 // After ExpectedMatches[AV.Assume] == 0, we still need to iterate
157 // through this loop to determine the value of HasNonnull, to avoid
158 // prematurely calling AffectedValues.erase(AVI).
159 }
160 HasNonnull |= !!Elem.Assume;
161 if (HasNonnull && Found)
162 break;
163 }
164
165 assert(ExpectedMatches[AV.Assume] == 0 ||
166 Found && "already unregistered or incorrect cache state");
167
168 if (!HasNonnull)
169 AffectedValues.erase(AVI);
170 }
171
172 assert(
173 none_of(Affected, [&](auto &AV) { return ExpectedMatches[AV.Assume]; }) &&
174 "already unregistered or incorrect cache state");
175}
176
178 removeAffectedValues(CI);
179 llvm::erase(AssumeHandles, CI);
180}
181
183 removeAffectedValues(cast<AssumeInst>(Handle));
184 Handle = New;
186}
187
189 AC->AffectedValues.erase(getValPtr());
190 // 'this' now dangles!
191}
192
193void AssumptionCache::transferAffectedValuesInCache(Value *OV, Value *NV) {
194 auto &NAVV = getOrInsertAffectedValues(NV);
195 auto AVI = AffectedValues.find(OV);
196 if (AVI == AffectedValues.end())
197 return;
198
199 for (auto &A : AVI->second)
200 if (!llvm::is_contained(NAVV, A))
201 NAVV.push_back(A);
202 AffectedValues.erase(OV);
203}
204
205void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
206 if (!isa<Instruction>(NV) && !isa<Argument>(NV))
207 return;
208
209 // Any assumptions that affected this value now affect the new value.
210
211 AC->transferAffectedValuesInCache(getValPtr(), NV);
212 // 'this' now might dangle! If the AffectedValues map was resized to add an
213 // entry for NV then this object might have been destroyed in favor of some
214 // copy in the grown map.
215}
216
217void AssumptionCache::scanFunction() {
218 assert(!Scanned && "Tried to scan the function twice!");
219 assert(AssumeHandles.empty() && "Already have assumes when scanning!");
220
221 // Go through all instructions in all blocks, add all calls to @llvm.assume
222 // to this cache.
223 for (BasicBlock &B : F)
224 for (Instruction &I : B)
225 if (isa<AssumeInst>(&I))
226 AssumeHandles.push_back(&I);
227
228 // Mark the scan as complete.
229 Scanned = true;
230
231 // Update affected values.
232 for (auto &A : AssumeHandles)
234}
235
237 // If we haven't scanned the function yet, just drop this assumption. It will
238 // be found when we scan later.
239 if (!Scanned)
240 return;
241
242 AssumeHandles.push_back(CI);
243
244#ifndef NDEBUG
245 assert(CI->getParent() &&
246 "Cannot register @llvm.assume call not in a basic block");
247 assert(&F == CI->getParent()->getParent() &&
248 "Cannot register @llvm.assume call not in this function");
249
250 // We expect the number of assumptions to be small, so in an asserts build
251 // check that we don't accumulate duplicates and that all assumptions point
252 // to the same function.
253 SmallPtrSet<Value *, 16> AssumptionSet;
254 for (auto &VH : AssumeHandles) {
255 if (!VH)
256 continue;
257
259 "Cached assumption not inside this function!");
261 "Cached something other than a call to @llvm.assume!");
262 assert(AssumptionSet.insert(VH).second &&
263 "Cache contains multiple copies of a call!");
264 }
265#endif
266
268}
269
275
276AnalysisKey AssumptionAnalysis::Key;
277
281
282 OS << "Cached assumptions for function: " << F.getName() << "\n";
283 for (auto &VH : AC.assumptions()) {
284 if (!VH)
285 continue;
286
287 auto *Assume = cast<CallInst>(VH);
288 if (!Assume->hasOperandBundles()) {
289 OS << " " << *Assume->getArgOperand(0) << "\n";
290 continue;
291 }
292
293 assert(match(Assume->getArgOperand(0), m_One()) &&
294 "assume must have trivial cond");
295 OS << " [ ";
296 ListSeparator LS;
297 for (const OperandBundleUse &BU : Assume->operand_bundles()) {
298 OS << LS << '"' << BU.getTagName() << "\"(";
299 interleaveComma(BU.Inputs, OS,
300 [&](const Use &Input) { Input->printAsOperand(OS); });
301 OS << ')';
302 }
303 OS << " ]\n";
304 }
305
306 return PreservedAnalyses::all();
307}
308
310 auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
311 if (I != ACT->AssumptionCaches.end())
312 ACT->AssumptionCaches.erase(I);
313 // 'this' now dangles!
314}
315
317 // We probe the function map twice to try and avoid creating a value handle
318 // around the function in common cases. This makes insertion a bit slower,
319 // but if we have to insert we're going to scan the whole function so that
320 // shouldn't matter.
321 auto I = AssumptionCaches.find_as(&F);
322 if (I != AssumptionCaches.end())
323 return *I->second;
324
326 auto *TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
327
328 // Ok, build a new cache by scanning the function, insert it and the value
329 // handle into our map, and return the newly populated cache.
330 auto IP = AssumptionCaches.insert(std::make_pair(
331 FunctionCallbackVH(&F, this), std::make_unique<AssumptionCache>(F, TTI)));
332 assert(IP.second && "Scanning function already in the map?");
333 return *IP.first->second;
334}
335
337 auto I = AssumptionCaches.find_as(&F);
338 if (I != AssumptionCaches.end())
339 return I->second.get();
340 return nullptr;
341}
342
344 // FIXME: In the long term the verifier should not be controllable with a
345 // flag. We should either fix all passes to correctly update the assumption
346 // cache and enable the verifier unconditionally or somehow arrange for the
347 // assumption list to be updated automatically by passes.
349 return;
350
352 for (const auto &I : AssumptionCaches) {
353 for (auto &VH : I.second->assumptions())
354 if (VH)
355 AssumptionSet.insert(cast<CallInst>(VH));
356
357 for (const BasicBlock &B : cast<Function>(*I.first))
358 for (const Instruction &II : B)
360 !AssumptionSet.count(cast<CallInst>(&II)))
361 report_fatal_error("Assumption in scanned function not in cache");
362 }
363}
364
366
368
370
371INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
372 "Assumption Cache Tracker", false, true)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void findAffectedValues(CallBase *CI, TargetTransformInfo *TTI, SmallVectorImpl< AssumptionCache::ResultElem > &Affected)
static cl::opt< bool > VerifyAssumptionCache("verify-assumption-cache", cl::Hidden, cl::desc("Enable verification of assumption cache"), cl::init(false))
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
const SmallVectorImpl< MachineOperand > & Cond
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
This pass exposes codegen information to IR-level passes.
The Input class is used to parse a yaml document into in-memory structs and vectors.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This represents the llvm.assume intrinsic.
A function analysis which provides an AssumptionCache.
LLVM_ABI AssumptionCache run(Function &F, FunctionAnalysisManager &)
An immutable pass that tracks lazily created AssumptionCache objects.
AssumptionCache * lookupAssumptionCache(Function &F)
Return the cached assumptions for a function if it has already been scanned.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
A cache of @llvm.assume calls within a function.
static LLVM_ABI void findValuesAffectedByOperandBundle(OperandBundleUse Bundle, function_ref< void(Value *)> InsertAffected)
Determine which values are affected by this assume operand bundle.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM_ABI void replaceAssumption(WeakVH &Handle, AssumeInst *New)
Replace the assumption referenced by Handle (must be a valid handle for a registered assumption) with...
LLVM_ABI void updateAffectedValues(AssumeInst *CI)
Update the cache of values being affected by this assumption (i.e.
MutableArrayRef< WeakVH > assumptions()
Access the list of assumption handles currently tracked for this function.
LLVM_ABI void unregisterAssumption(AssumeInst *CI)
Remove an @llvm.assume intrinsic from this function's cache if it has been added to the cache earlier...
AssumptionCache(Function &F, TargetTransformInfo *TTI=nullptr)
Construct an AssumptionCache from a function by scanning all of its instructions.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
virtual void deleted()
Callback for Value destruction.
ImmutablePass(char &pid)
Definition Pass.h:287
A helper class to return the specified delimiter string after the first invocation of operator String...
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Value * getValPtr() const
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI const Value * stripInBoundsOffsets(function_ref< void(const Value *)> Func=[](const Value *) {}) const
Strip off pointer casts and inbounds GEPs.
Definition Value.cpp:828
A nullable Value handle that is nullable.
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
bool match(Val *V, const Pattern &P)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
constexpr StringRef IgnoreBundleTag
Tag in operand bundle indicating that this bundle should be ignored.
void interleaveComma(const Container &c, StreamT &os, UnaryFunctor each_fn)
Definition STLExtras.h:2313
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2200
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
TargetTransformInfo TTI
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
unsigned Index
contains either ExprResultIdx or the index of the operand bundle containing the knowledge.
A lightweight accessor for an operand bundle meant to be passed around by value.
StringRef getTagName() const
Return the tag of this operand bundle as a string.
ArrayRef< Use > Inputs