LLVM 22.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 StringRef AttrName, uint64_t *ArgVal) {
46 "this attribute doesn't exist");
47 assert((ArgVal == nullptr || Attribute::isIntAttrKind(
49 "requested value for an attribute that has no argument");
50 if (Assume.bundle_op_infos().empty())
51 return false;
52
53 for (auto &BOI : Assume.bundle_op_infos()) {
54 if (BOI.Tag->getKey() != AttrName)
55 continue;
56 if (IsOn && (BOI.End - BOI.Begin <= ABA_WasOn ||
57 IsOn != getValueFromBundleOpInfo(Assume, BOI, ABA_WasOn)))
58 continue;
59 if (ArgVal) {
60 assert(BOI.End - BOI.Begin > ABA_Argument);
61 *ArgVal =
62 cast<ConstantInt>(getValueFromBundleOpInfo(Assume, BOI, ABA_Argument))
63 ->getZExtValue();
64 }
65 return true;
66 }
67 return false;
68}
69
71 for (auto &Bundles : Assume.bundle_op_infos()) {
72 std::pair<Value *, Attribute::AttrKind> Key{
73 nullptr, Attribute::getAttrKindFromName(Bundles.Tag->getKey())};
74 if (bundleHasArgument(Bundles, ABA_WasOn))
75 Key.first = getValueFromBundleOpInfo(Assume, Bundles, ABA_WasOn);
76
77 if (Key.first == nullptr && Key.second == Attribute::None)
78 continue;
79 if (!bundleHasArgument(Bundles, ABA_Argument)) {
80 Result[Key][&Assume] = {0, 0};
81 continue;
82 }
83 auto *CI = dyn_cast<ConstantInt>(
84 getValueFromBundleOpInfo(Assume, Bundles, ABA_Argument));
85 if (!CI)
86 continue;
87 uint64_t Val = CI->getZExtValue();
88 auto [It, Inserted] = Result[Key].try_emplace(&Assume);
89 if (Inserted) {
90 It->second = {Val, Val};
91 continue;
92 }
93 auto &MinMax = It->second;
94 MinMax.Min = std::min(Val, MinMax.Min);
95 MinMax.Max = std::max(Val, MinMax.Max);
96 }
97}
98
101 const CallBase::BundleOpInfo &BOI) {
102 RetainedKnowledge Result;
103 if (!DebugCounter::shouldExecute(AssumeQueryCounter))
104 return Result;
105
106 Result.AttrKind = Attribute::getAttrKindFromName(BOI.Tag->getKey());
108 Result.WasOn = getValueFromBundleOpInfo(Assume, BOI, ABA_WasOn);
109 auto GetArgOr1 = [&](unsigned Idx) -> uint64_t {
110 if (auto *ConstInt = dyn_cast<ConstantInt>(
112 return ConstInt->getZExtValue();
113 return 1;
114 };
115 if (BOI.End - BOI.Begin > ABA_Argument)
116 Result.ArgValue = GetArgOr1(0);
117 Result.IRArgValue = bundleHasArgument(BOI, ABA_Argument)
119 : nullptr;
120 if (Result.AttrKind == Attribute::Alignment)
121 if (BOI.End - BOI.Begin > ABA_Argument + 1)
122 Result.ArgValue = MinAlign(Result.ArgValue, GetArgOr1(1));
123 return Result;
124}
125
127 unsigned Idx) {
128 CallBase::BundleOpInfo BOI = Assume.getBundleOpInfoForOperand(Idx);
129 return getKnowledgeFromBundle(Assume, BOI);
130}
131
133 return none_of(Assume.bundle_op_infos(),
134 [](const CallBase::BundleOpInfo &BOI) {
135 return BOI.Tag->getKey() != IgnoreBundleTag;
136 });
137}
138
140 if (!match(U->getUser(),
141 m_Intrinsic<Intrinsic::assume>(m_Unless(m_Specific(U->get())))))
142 return nullptr;
143 auto *Intr = cast<IntrinsicInst>(U->getUser());
144 return &Intr->getBundleOpInfoForOperand(U->getOperandNo());
145}
146
151 if (!Bundle)
154 getKnowledgeFromBundle(*cast<AssumeInst>(U->getUser()), *Bundle);
155 if (llvm::is_contained(AttrKinds, RK.AttrKind))
156 return RK;
158}
159
163 AssumptionCache &AC,
165 const CallBase::BundleOpInfo *)>
166 Filter) {
167 NumAssumeQueries++;
168 for (AssumptionCache::ResultElem &Elem : AC.assumptionsFor(V)) {
169 auto *II = cast_or_null<AssumeInst>(Elem.Assume);
170 if (!II || Elem.Index == AssumptionCache::ExprResultIdx)
171 continue;
173 *II, II->bundle_op_info_begin()[Elem.Index])) {
174 if (V != RK.WasOn)
175 continue;
176 if (is_contained(AttrKinds, RK.AttrKind) &&
177 Filter(RK, II, &II->bundle_op_info_begin()[Elem.Index])) {
178 NumUsefullAssumeQueries++;
179 return RK;
180 }
181 }
182 }
183
185}
186
188 const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
189 AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT) {
190 return getKnowledgeForValue(V, AttrKinds, AC,
191 [&](auto, Instruction *I, auto) {
192 return isValidAssumeForContext(I, CtxI, DT);
193 });
194}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Intr
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)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
Definition: DebugCounter.h:194
#define I(x, y, z)
Definition: MD5.cpp:58
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:167
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
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)
Definition: Attributes.cpp:313
static LLVM_ABI bool isExistingAttribute(StringRef Name)
Return true if the provided string matches the IR name of an attribute.
Definition: Attributes.cpp:336
@ None
No attributes have been set.
Definition: Attributes.h:90
static bool isIntAttrKind(AttrKind Kind)
Definition: Attributes.h:104
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:88
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
StringRef getKey() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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.
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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,...
LLVM_ABI RetainedKnowledge getKnowledgeFromOperandInAssume(AssumeInst &Assume, unsigned Idx)
Retreive the information help by Assume on the operand at index Idx.
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:362
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
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:1758
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_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...
LLVM_ABI bool hasAttributeInAssume(AssumeInst &Assume, Value *IsOn, StringRef AttrName, uint64_t *ArgVal=nullptr)
Query the operand bundle of an llvm.assume to find a single attribute of the specified kind applied o...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
LLVM_ABI void fillMapFromAssume(AssumeInst &Assume, RetainedKnowledgeMap &Result)
Insert into the map all the informations contained in the operand bundles of the llvm....
Used to keep track of an operand bundle.
Definition: InstrTypes.h:2169
StringMapEntry< uint32_t > * Tag
The operand bundle tag, interned by LLVMContextImpl::getOrInsertBundleTag.
Definition: InstrTypes.h:2172
uint32_t End
The index in the Use& vector where operands for this operand bundle ends.
Definition: InstrTypes.h:2180
uint32_t Begin
The index in the Use& vector where operands for this operand bundle starts.
Definition: InstrTypes.h:2176
Represent one information held inside an operand bundle of an llvm.assume.
Attribute::AttrKind AttrKind
static RetainedKnowledge none()