LLVM 23.0.0git
AssumeBundleQueries.cpp
Go to the documentation of this file.
1//===- AssumeBundleQueries.cpp - tool to query assume bundles ---*- C++ -*-===//
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
10#include "llvm/ADT/Statistic.h"
13#include "llvm/IR/Instruction.h"
18
19#define DEBUG_TYPE "assume-queries"
20
21using namespace llvm;
22using namespace llvm::PatternMatch;
23
24STATISTIC(NumAssumeQueries, "Number of Queries into an assume assume bundles");
26 NumUsefullAssumeQueries,
27 "Number of Queries into an assume assume bundles that were satisfied");
28
29DEBUG_COUNTER(AssumeQueryCounter, "assume-queries-counter",
30 "Controls which assumes gets created");
31
32static bool bundleHasArgument(const CallBase::BundleOpInfo &BOI, unsigned Idx) {
33 return BOI.End - BOI.Begin > Idx;
34}
35
37 const CallBase::BundleOpInfo &BOI,
38 unsigned Idx) {
39 assert(bundleHasArgument(BOI, Idx) && "index out of range");
40 return (Assume.op_begin() + BOI.Begin + Idx)->get();
41}
42
44 for (auto &Bundles : Assume.bundle_op_infos()) {
45 std::pair<Value *, Attribute::AttrKind> Key{
46 nullptr, Attribute::getAttrKindFromName(Bundles.Tag->getKey())};
47 if (bundleHasArgument(Bundles, ABA_WasOn))
48 Key.first = getValueFromBundleOpInfo(Assume, Bundles, ABA_WasOn);
49
50 if (Key.first == nullptr && Key.second == Attribute::None)
51 continue;
52 if (!bundleHasArgument(Bundles, ABA_Argument)) {
53 Result[Key][&Assume] = {0, 0};
54 continue;
55 }
56 auto *CI = dyn_cast<ConstantInt>(
57 getValueFromBundleOpInfo(Assume, Bundles, ABA_Argument));
58 if (!CI)
59 continue;
60 uint64_t Val = CI->getZExtValue();
61 auto [It, Inserted] = Result[Key].try_emplace(&Assume);
62 if (Inserted) {
63 It->second = {Val, Val};
64 continue;
65 }
66 auto &MinMax = It->second;
67 MinMax.Min = std::min(Val, MinMax.Min);
68 MinMax.Max = std::max(Val, MinMax.Max);
69 }
70}
71
74 const CallBase::BundleOpInfo &BOI) {
75 RetainedKnowledge Result;
76 if (!DebugCounter::shouldExecute(AssumeQueryCounter))
77 return Result;
78
79 Result.AttrKind = Attribute::getAttrKindFromName(BOI.Tag->getKey());
81 Result.WasOn = getValueFromBundleOpInfo(Assume, BOI, ABA_WasOn);
82 auto GetArgOr = [&](unsigned Idx, uint64_t Default) -> uint64_t {
83 if (auto *ConstInt = dyn_cast<ConstantInt>(
84 getValueFromBundleOpInfo(Assume, BOI, ABA_Argument + Idx)))
85 return ConstInt->getZExtValue();
86 return Default;
87 };
88 if (BOI.End - BOI.Begin > ABA_Argument) {
89 switch (Result.AttrKind) {
90 case Attribute::Alignment:
91 Result.ArgValue = GetArgOr(0, 1);
92 break;
93 case Attribute::Dereferenceable:
94 case Attribute::DereferenceableOrNull:
95 Result.ArgValue = GetArgOr(0, 0);
96 break;
97 case Attribute::None:
98 Result.ArgValue = 0;
99 break;
100 default:
101 llvm_unreachable("Attribute kind does not support argument");
102 }
103 }
104 Result.IRArgValue = bundleHasArgument(BOI, ABA_Argument)
106 : nullptr;
107 if (Result.AttrKind == Attribute::Alignment)
108 if (BOI.End - BOI.Begin > ABA_Argument + 1)
109 Result.ArgValue = MinAlign(Result.ArgValue, GetArgOr(1, 1));
110 return Result;
111}
112
114 return none_of(Assume.bundle_op_infos(),
115 [](const CallBase::BundleOpInfo &BOI) {
116 return BOI.Tag->getKey() != IgnoreBundleTag;
117 });
118}
119
121 if (!match(U->getUser(),
123 return nullptr;
124 auto *Intr = cast<IntrinsicInst>(U->getUser());
125 return &Intr->getBundleOpInfoForOperand(U->getOperandNo());
126}
127
132 if (!Bundle)
135 getKnowledgeFromBundle(*cast<AssumeInst>(U->getUser()), *Bundle);
136 if (llvm::is_contained(AttrKinds, RK.AttrKind))
137 return RK;
139}
140
144 AssumptionCache &AC,
146 const CallBase::BundleOpInfo *)>
147 Filter) {
148 NumAssumeQueries++;
149 for (AssumptionCache::ResultElem &Elem : AC.assumptionsFor(V)) {
150 auto *II = cast_or_null<AssumeInst>(Elem.Assume);
151 if (!II || Elem.Index == AssumptionCache::ExprResultIdx)
152 continue;
154 *II, II->bundle_op_info_begin()[Elem.Index])) {
155 if (V != RK.WasOn)
156 continue;
157 if (is_contained(AttrKinds, RK.AttrKind) &&
158 Filter(RK, II, &II->bundle_op_info_begin()[Elem.Index])) {
159 NumUsefullAssumeQueries++;
160 return RK;
161 }
162 }
163 }
164
166}
167
169 const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
170 AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT) {
171 return getKnowledgeForValue(V, AttrKinds, AC,
172 [&](auto, Instruction *I, auto) {
173 return isValidAssumeForContext(I, CtxI, DT);
174 });
175}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static CallInst::BundleOpInfo * getBundleFromUse(const Use *U)
static Value * getValueFromBundleOpInfo(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI, unsigned Idx)
static bool bundleHasArgument(const CallBase::BundleOpInfo &BOI, unsigned Idx)
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
static LLVM_ABI Attribute::AttrKind getAttrKindFromName(StringRef AttrName)
@ None
No attributes have been set.
Definition Attributes.h:126
static bool shouldExecute(CounterInfo &Counter)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
StringRef getKey() const
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI RetainedKnowledge getKnowledgeForValue(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, function_ref< bool(RetainedKnowledge, Instruction *, const CallBase::BundleOpInfo *)> Filter=[](auto...) { return true;})
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and it match...
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isAssumeWithEmptyBundle(const AssumeInst &Assume)
Return true iff the operand bundles of the provided llvm.assume doesn't contain any valuable informat...
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition MathExtras.h:357
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
DenseMap< RetainedKnowledgeKey, Assume2KnowledgeMap > RetainedKnowledgeMap
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 RetainedKnowledge getKnowledgeFromUse(const Use *U, ArrayRef< Attribute::AttrKind > AttrKinds)
Return a valid Knowledge associated to the Use U if its Attribute kind is in AttrKinds.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
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
LLVM_ABI void fillMapFromAssume(AssumeInst &Assume, RetainedKnowledgeMap &Result)
Insert into the map all the informations contained in the operand bundles of the llvm....
@ Default
The result value is uniform if and only if all operands are uniform.
Definition Uniformity.h:20
Used to keep track of an operand bundle.
StringMapEntry< uint32_t > * Tag
The operand bundle tag, interned by LLVMContextImpl::getOrInsertBundleTag.
uint32_t End
The index in the Use& vector where operands for this operand bundle ends.
uint32_t Begin
The index in the Use& vector where operands for this operand bundle starts.
Represent one information held inside an operand bundle of an llvm.assume.
Attribute::AttrKind AttrKind
static RetainedKnowledge none()