LLVM 23.0.0git
VPlanPatternMatch.h
Go to the documentation of this file.
1//===- VPlanPatternMatch.h - Match on VPValues and recipes ------*- 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//
9// This file provides a simple and efficient mechanism for performing general
10// tree-based pattern matches on the VPlan values and recipes, based on
11// LLVM's IR pattern matchers.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORM_VECTORIZE_VPLANPATTERNMATCH_H
16#define LLVM_TRANSFORM_VECTORIZE_VPLANPATTERNMATCH_H
17
18#include "VPlan.h"
19
21
22template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
23 return P.match(V);
24}
25
26/// A match functor that can be used as a UnaryPredicate in functional
27/// algorithms like all_of.
28template <typename Val, typename Pattern> auto match_fn(const Pattern &P) {
30}
31
32template <typename Pattern> bool match(VPUser *U, const Pattern &P) {
33 auto *R = dyn_cast<VPRecipeBase>(U);
34 return R && match(R, P);
35}
36
37/// Match functor for VPUser.
38template <typename Pattern> auto match_fn(const Pattern &P) {
40}
41
42template <typename Pattern> bool match(VPSingleDefRecipe *R, const Pattern &P) {
43 return P.match(static_cast<const VPRecipeBase *>(R));
44}
45
46template <typename... Classes> struct class_match {
47 template <typename ITy> bool match(ITy *V) const {
48 return isa<Classes...>(V);
49 }
50};
51
52/// Match an arbitrary VPValue and ignore it.
54
55template <typename Class> struct bind_ty {
56 Class *&VR;
57
58 bind_ty(Class *&V) : VR(V) {}
59
60 template <typename ITy> bool match(ITy *V) const {
61 if (auto *CV = dyn_cast<Class>(V)) {
62 VR = CV;
63 return true;
64 }
65 return false;
66 }
67};
68
69/// Match a specified VPValue.
71 const VPValue *Val;
72
73 specificval_ty(const VPValue *V) : Val(V) {}
74
75 bool match(VPValue *VPV) const { return VPV == Val; }
76};
77
78inline specificval_ty m_Specific(const VPValue *VPV) { return VPV; }
79
80/// Stores a reference to the VPValue *, not the VPValue * itself,
81/// thus can be used in commutative matchers.
83 VPValue *const &Val;
84
85 deferredval_ty(VPValue *const &V) : Val(V) {}
86
87 bool match(VPValue *const V) const { return V == Val; }
88};
89
90/// Like m_Specific(), but works if the specific value to match is determined
91/// as part of the same match() expression. For example:
92/// m_Mul(m_VPValue(X), m_Specific(X)) is incorrect, because m_Specific() will
93/// bind X before the pattern match starts.
94/// m_Mul(m_VPValue(X), m_Deferred(X)) is correct, and will check against
95/// whichever value m_VPValue(X) populated.
96inline deferredval_ty m_Deferred(VPValue *const &V) { return V; }
97
98/// Match an integer constant or vector of constants if Pred::isValue returns
99/// true for the APInt. \p BitWidth optionally specifies the bitwidth the
100/// matched constant must have. If it is 0, the matched constant can have any
101/// bitwidth.
102template <typename Pred, unsigned BitWidth = 0> struct int_pred_ty {
103 Pred P;
104
105 int_pred_ty(Pred P) : P(std::move(P)) {}
106 int_pred_ty() : P() {}
107
108 bool match(VPValue *VPV) const {
109 auto *VPI = dyn_cast<VPInstruction>(VPV);
110 if (VPI && VPI->getOpcode() == VPInstruction::Broadcast)
111 VPV = VPI->getOperand(0);
112 auto *CI = dyn_cast<VPConstantInt>(VPV);
113 if (!CI)
114 return false;
115
116 if (BitWidth != 0 && CI->getBitWidth() != BitWidth)
117 return false;
118 return P.isValue(CI->getAPInt());
119 }
120};
121
122/// Match a specified integer value or vector of all elements of that
123/// value. \p BitWidth optionally specifies the bitwidth the matched constant
124/// must have. If it is 0, the matched constant can have any bitwidth.
127
129
130 bool isValue(const APInt &C) const { return APInt::isSameValue(Val, C); }
131};
132
133template <unsigned Bitwidth = 0>
135
139
143
147
149 bool isValue(const APInt &C) const { return C.isAllOnes(); }
150};
151
152/// Match an integer or vector with all bits set.
153/// For vectors, this includes constants with undefined elements.
157
159 bool isValue(const APInt &C) const { return C.isZero(); }
160};
161
162struct is_one {
163 bool isValue(const APInt &C) const { return C.isOne(); }
164};
165
166/// Match an integer 0 or a vector with all elements equal to 0.
167/// For vectors, this includes constants with undefined elements.
171
172/// Match an integer 1 or a vector with all elements equal to 1.
173/// For vectors, this includes constants with undefined elements.
175
177 const APInt *&Res;
178
179 bind_apint(const APInt *&Res) : Res(Res) {}
180
181 bool match(VPValue *VPV) const {
182 auto *CI = dyn_cast<VPConstantInt>(VPV);
183 if (!CI)
184 return false;
185 Res = &CI->getAPInt();
186 return true;
187 }
188};
189
190inline bind_apint m_APInt(const APInt *&C) { return C; }
191
194
196
197 bool match(VPValue *VPV) const {
198 const APInt *APConst;
199 if (!bind_apint(APConst).match(VPV))
200 return false;
201 if (auto C = APConst->tryZExtValue()) {
202 Res = *C;
203 return true;
204 }
205 return false;
206 }
207};
208
209/// Match a plain integer constant no wider than 64-bits, capturing it if we
210/// match.
212
213/// Matching combinators
214template <typename LTy, typename RTy> struct match_combine_or {
215 LTy L;
216 RTy R;
217
218 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
219
220 template <typename ITy> bool match(ITy *V) const {
221 return L.match(V) || R.match(V);
222 }
223};
224
225template <typename LTy, typename RTy> struct match_combine_and {
226 LTy L;
227 RTy R;
228
229 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
230
231 template <typename ITy> bool match(ITy *V) const {
232 return L.match(V) && R.match(V);
233 }
234};
235
236/// Combine two pattern matchers matching L || R
237template <typename LTy, typename RTy>
238inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
239 return match_combine_or<LTy, RTy>(L, R);
240}
241
242/// Combine two pattern matchers matching L && R
243template <typename LTy, typename RTy>
244inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
245 return match_combine_and<LTy, RTy>(L, R);
246}
247
248/// Match a VPValue, capturing it if we match.
249inline bind_ty<VPValue> m_VPValue(VPValue *&V) { return V; }
250
251/// Match a VPIRValue.
252inline bind_ty<VPIRValue> m_VPIRValue(VPIRValue *&V) { return V; }
253
254/// Match a VPInstruction, capturing if we match.
256
257template <typename Ops_t, unsigned Opcode, bool Commutative,
258 typename... RecipeTys>
260 Ops_t Ops;
261
262 template <typename... OpTy> Recipe_match(OpTy... Ops) : Ops(Ops...) {
263 static_assert(std::tuple_size<Ops_t>::value == sizeof...(Ops) &&
264 "number of operands in constructor doesn't match Ops_t");
265 static_assert((!Commutative || std::tuple_size<Ops_t>::value == 2) &&
266 "only binary ops can be commutative");
267 }
268
269 bool match(const VPValue *V) const {
270 auto *DefR = V->getDefiningRecipe();
271 return DefR && match(DefR);
272 }
273
274 bool match(const VPSingleDefRecipe *R) const {
275 return match(static_cast<const VPRecipeBase *>(R));
276 }
277
278 bool match(const VPRecipeBase *R) const {
279 if (std::tuple_size_v<Ops_t> == 0) {
280 auto *VPI = dyn_cast<VPInstruction>(R);
281 return VPI && VPI->getOpcode() == Opcode;
282 }
283
284 if ((!matchRecipeAndOpcode<RecipeTys>(R) && ...))
285 return false;
286
287 if (R->getNumOperands() != std::tuple_size_v<Ops_t>) {
288 [[maybe_unused]] auto *RepR = dyn_cast<VPReplicateRecipe>(R);
291 (RepR && std::tuple_size_v<Ops_t> ==
292 RepR->getNumOperands() - RepR->isPredicated())) &&
293 "non-variadic recipe with matched opcode does not have the "
294 "expected number of operands");
295 return false;
296 }
297
298 auto IdxSeq = std::make_index_sequence<std::tuple_size<Ops_t>::value>();
299 if (all_of_tuple_elements(IdxSeq, [R](auto Op, unsigned Idx) {
300 return Op.match(R->getOperand(Idx));
301 }))
302 return true;
303
304 return Commutative &&
305 all_of_tuple_elements(IdxSeq, [R](auto Op, unsigned Idx) {
306 return Op.match(R->getOperand(R->getNumOperands() - Idx - 1));
307 });
308 }
309
310private:
311 template <typename RecipeTy>
312 static bool matchRecipeAndOpcode(const VPRecipeBase *R) {
313 auto *DefR = dyn_cast<RecipeTy>(R);
314 // Check for recipes that do not have opcodes.
315 if constexpr (std::is_same_v<RecipeTy, VPScalarIVStepsRecipe> ||
316 std::is_same_v<RecipeTy, VPCanonicalIVPHIRecipe> ||
317 std::is_same_v<RecipeTy, VPDerivedIVRecipe> ||
318 std::is_same_v<RecipeTy, VPVectorEndPointerRecipe>)
319 return DefR;
320 else
321 return DefR && DefR->getOpcode() == Opcode;
322 }
323
324 /// Helper to check if predicate \p P holds on all tuple elements in Ops using
325 /// the provided index sequence.
326 template <typename Fn, std::size_t... Is>
327 bool all_of_tuple_elements(std::index_sequence<Is...>, Fn P) const {
328 return (P(std::get<Is>(Ops), Is) && ...);
329 }
330};
331
332template <unsigned Opcode, typename... OpTys>
334 Recipe_match<std::tuple<OpTys...>, Opcode, /*Commutative*/ false,
337
338template <unsigned Opcode, typename... OpTys>
340 Recipe_match<std::tuple<OpTys...>, Opcode, /*Commutative*/ true,
342
343template <unsigned Opcode, typename... OpTys>
344using VPInstruction_match = Recipe_match<std::tuple<OpTys...>, Opcode,
345 /*Commutative*/ false, VPInstruction>;
346
347template <unsigned Opcode, typename... OpTys>
348inline VPInstruction_match<Opcode, OpTys...>
349m_VPInstruction(const OpTys &...Ops) {
350 return VPInstruction_match<Opcode, OpTys...>(Ops...);
351}
352
353/// BuildVector is matches only its opcode, w/o matching its operands as the
354/// number of operands is not fixed.
358
359template <typename Op0_t>
361m_Freeze(const Op0_t &Op0) {
363}
364
368
369template <typename Op0_t>
371m_BranchOnCond(const Op0_t &Op0) {
373}
374
379
380template <typename Op0_t, typename Op1_t>
382m_BranchOnTwoConds(const Op0_t &Op0, const Op1_t &Op1) {
384}
385
386template <typename Op0_t>
388m_Broadcast(const Op0_t &Op0) {
390}
391
392template <typename Op0_t>
394m_EVL(const Op0_t &Op0) {
396}
397
398template <typename Op0_t>
403
404template <typename Op0_t, typename Op1_t>
406m_ExtractElement(const Op0_t &Op0, const Op1_t &Op1) {
408}
409
410template <typename Op0_t, typename Op1_t>
412m_ExtractLane(const Op0_t &Op0, const Op1_t &Op1) {
414}
415
416template <typename Op0_t>
421
422template <typename Op0_t>
428}
429
430template <typename Op0_t>
435
436template <typename Op0_t, typename Op1_t, typename Op2_t>
438m_ActiveLaneMask(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
440}
441
445
446template <typename Op0_t, typename Op1_t>
448m_BranchOnCount(const Op0_t &Op0, const Op1_t &Op1) {
450}
451
455
456template <typename Op0_t>
458m_AnyOf(const Op0_t &Op0) {
460}
461
462template <typename Op0_t>
467
468template <typename Op0_t>
470m_LastActiveLane(const Op0_t &Op0) {
472}
473
474template <typename Op0_t>
479
480/// Match FindIV result pattern:
481/// select(icmp ne ComputeReductionResult(ReducedIV), Sentinel),
482/// ComputeReductionResult(ReducedIV), Start.
483template <typename Op0_t, typename Op1_t>
484inline bool matchFindIVResult(VPInstruction *VPI, Op0_t ReducedIV, Op1_t Start) {
486 m_ComputeReductionResult(ReducedIV),
487 m_VPValue()),
488 m_ComputeReductionResult(ReducedIV), Start));
489}
490
491template <typename Op0_t, typename Op1_t, typename Op2_t>
493 Op2_t>
494m_ComputeAnyOfResult(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
496}
497
498template <typename Op0_t>
500m_Reverse(const Op0_t &Op0) {
502}
503
507
508template <unsigned Opcode, typename Op0_t>
509inline AllRecipe_match<Opcode, Op0_t> m_Unary(const Op0_t &Op0) {
511}
512
513template <typename Op0_t>
517
518template <typename Op0_t>
520m_TruncOrSelf(const Op0_t &Op0) {
521 return m_CombineOr(m_Trunc(Op0), Op0);
522}
523
524template <typename Op0_t>
528
529template <typename Op0_t>
533
534template <typename Op0_t>
538
539template <typename Op0_t>
542m_ZExtOrSExt(const Op0_t &Op0) {
543 return m_CombineOr(m_ZExt(Op0), m_SExt(Op0));
544}
545
546template <typename Op0_t>
548m_ZExtOrSelf(const Op0_t &Op0) {
549 return m_CombineOr(m_ZExt(Op0), Op0);
550}
551
552template <unsigned Opcode, typename Op0_t, typename Op1_t>
554 const Op1_t &Op1) {
556}
557
558template <unsigned Opcode, typename Op0_t, typename Op1_t>
560m_c_Binary(const Op0_t &Op0, const Op1_t &Op1) {
562}
563
564template <typename Op0_t, typename Op1_t>
566 const Op1_t &Op1) {
568}
569
570template <typename Op0_t, typename Op1_t>
572m_c_Add(const Op0_t &Op0, const Op1_t &Op1) {
574}
575
576template <typename Op0_t, typename Op1_t>
578 const Op1_t &Op1) {
580}
581
582template <typename Op0_t, typename Op1_t>
584 const Op1_t &Op1) {
586}
587
588template <typename Op0_t, typename Op1_t>
590m_c_Mul(const Op0_t &Op0, const Op1_t &Op1) {
592}
593
594template <typename Op0_t, typename Op1_t>
596m_FMul(const Op0_t &Op0, const Op1_t &Op1) {
598}
599
600template <typename Op0_t, typename Op1_t>
602m_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
604}
605
606/// Match a binary AND operation.
607template <typename Op0_t, typename Op1_t>
609m_c_BinaryAnd(const Op0_t &Op0, const Op1_t &Op1) {
611}
612
613/// Match a binary OR operation. Note that while conceptually the operands can
614/// be matched commutatively, \p Commutative defaults to false in line with the
615/// IR-based pattern matching infrastructure. Use m_c_BinaryOr for a commutative
616/// version of the matcher.
617template <typename Op0_t, typename Op1_t>
619m_BinaryOr(const Op0_t &Op0, const Op1_t &Op1) {
621}
622
623template <typename Op0_t, typename Op1_t>
625m_c_BinaryOr(const Op0_t &Op0, const Op1_t &Op1) {
627}
628
629/// Cmp_match is a variant of BinaryRecipe_match that also binds the comparison
630/// predicate. Opcodes must either be Instruction::ICmp or Instruction::FCmp, or
631/// both.
632template <typename Op0_t, typename Op1_t, unsigned... Opcodes>
633struct Cmp_match {
634 static_assert((sizeof...(Opcodes) == 1 || sizeof...(Opcodes) == 2) &&
635 "Expected one or two opcodes");
636 static_assert(
637 ((Opcodes == Instruction::ICmp || Opcodes == Instruction::FCmp) && ...) &&
638 "Expected a compare instruction opcode");
639
641 Op0_t Op0;
643
644 Cmp_match(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1)
645 : Predicate(&Pred), Op0(Op0), Op1(Op1) {}
646 Cmp_match(const Op0_t &Op0, const Op1_t &Op1) : Op0(Op0), Op1(Op1) {}
647
648 bool match(const VPValue *V) const {
649 auto *DefR = V->getDefiningRecipe();
650 return DefR && match(DefR);
651 }
652
653 bool match(const VPRecipeBase *V) const {
654 if ((m_Binary<Opcodes>(Op0, Op1).match(V) || ...)) {
655 if (Predicate)
656 *Predicate = cast<VPRecipeWithIRFlags>(V)->getPredicate();
657 return true;
658 }
659 return false;
660 }
661};
662
663/// SpecificCmp_match is a variant of Cmp_match that matches the comparison
664/// predicate, instead of binding it.
665template <typename Op0_t, typename Op1_t, unsigned... Opcodes>
668 Op0_t Op0;
670
671 SpecificCmp_match(CmpPredicate Pred, const Op0_t &LHS, const Op1_t &RHS)
672 : Predicate(Pred), Op0(LHS), Op1(RHS) {}
673
674 bool match(const VPValue *V) const {
675 auto *DefR = V->getDefiningRecipe();
676 return DefR && match(DefR);
677 }
678
679 bool match(const VPRecipeBase *V) const {
680 CmpPredicate CurrentPred;
681 return Cmp_match<Op0_t, Op1_t, Opcodes...>(CurrentPred, Op0, Op1)
682 .match(V) &&
684 }
685};
686
687template <typename Op0_t, typename Op1_t>
689 const Op1_t &Op1) {
691}
692
693template <typename Op0_t, typename Op1_t>
694inline Cmp_match<Op0_t, Op1_t, Instruction::ICmp>
695m_ICmp(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1) {
696 return Cmp_match<Op0_t, Op1_t, Instruction::ICmp>(Pred, Op0, Op1);
697}
698
699template <typename Op0_t, typename Op1_t>
700inline SpecificCmp_match<Op0_t, Op1_t, Instruction::ICmp>
701m_SpecificICmp(CmpPredicate MatchPred, const Op0_t &Op0, const Op1_t &Op1) {
703 Op1);
704}
705
706template <typename Op0_t, typename Op1_t>
707inline Cmp_match<Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp>
708m_Cmp(const Op0_t &Op0, const Op1_t &Op1) {
710 Op1);
711}
712
713template <typename Op0_t, typename Op1_t>
714inline Cmp_match<Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp>
715m_Cmp(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1) {
717 Pred, Op0, Op1);
718}
719
720template <typename Op0_t, typename Op1_t>
721inline SpecificCmp_match<Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp>
722m_SpecificCmp(CmpPredicate MatchPred, const Op0_t &Op0, const Op1_t &Op1) {
724 MatchPred, Op0, Op1);
725}
726
727template <typename Op0_t, typename Op1_t>
729 Recipe_match<std::tuple<Op0_t, Op1_t>, Instruction::GetElementPtr,
730 /*Commutative*/ false, VPReplicateRecipe, VPWidenGEPRecipe>,
734
735template <typename Op0_t, typename Op1_t>
737 const Op1_t &Op1) {
738 return m_CombineOr(
739 Recipe_match<std::tuple<Op0_t, Op1_t>, Instruction::GetElementPtr,
740 /*Commutative*/ false, VPReplicateRecipe, VPWidenGEPRecipe>(
741 Op0, Op1),
745 Op1)));
746}
747
748template <typename Op0_t, typename Op1_t, typename Op2_t>
750m_Select(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
752 {Op0, Op1, Op2});
753}
754
755template <typename Op0_t>
758 Instruction::Xor, int_pred_ty<is_all_ones>, Op0_t>>
763
764template <typename Op0_t, typename Op1_t>
765inline match_combine_or<
768m_LogicalAnd(const Op0_t &Op0, const Op1_t &Op1) {
769 return m_CombineOr(
771 m_Select(Op0, Op1, m_False()));
772}
773
774template <typename Op0_t, typename Op1_t>
776m_LogicalOr(const Op0_t &Op0, const Op1_t &Op1) {
777 return m_Select(Op0, m_True(), Op1);
778}
779
780template <typename Op0_t, typename Op1_t, typename Op2_t>
782 false, VPScalarIVStepsRecipe>;
783
784template <typename Op0_t, typename Op1_t, typename Op2_t>
786m_ScalarIVSteps(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
787 return VPScalarIVSteps_match<Op0_t, Op1_t, Op2_t>({Op0, Op1, Op2});
788}
789
790template <typename Op0_t, typename Op1_t, typename Op2_t>
793
794template <typename Op0_t, typename Op1_t, typename Op2_t>
796m_DerivedIV(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
797 return VPDerivedIV_match<Op0_t, Op1_t, Op2_t>({Op0, Op1, Op2});
798}
799
800template <typename Addr_t, typename Mask_t> struct Load_match {
801 Addr_t Addr;
802 Mask_t Mask;
803
804 Load_match(Addr_t Addr, Mask_t Mask) : Addr(Addr), Mask(Mask) {}
805
806 template <typename OpTy> bool match(const OpTy *V) const {
807 auto *Load = dyn_cast<VPWidenLoadRecipe>(V);
808 if (!Load || !Addr.match(Load->getAddr()) || !Load->isMasked() ||
809 !Mask.match(Load->getMask()))
810 return false;
811 return true;
812 }
813};
814
815/// Match a (possibly reversed) masked load.
816template <typename Addr_t, typename Mask_t>
817inline Load_match<Addr_t, Mask_t> m_MaskedLoad(const Addr_t &Addr,
818 const Mask_t &Mask) {
819 return Load_match<Addr_t, Mask_t>(Addr, Mask);
820}
821
822template <typename Addr_t, typename Val_t, typename Mask_t> struct Store_match {
823 Addr_t Addr;
824 Val_t Val;
825 Mask_t Mask;
826
827 Store_match(Addr_t Addr, Val_t Val, Mask_t Mask)
828 : Addr(Addr), Val(Val), Mask(Mask) {}
829
830 template <typename OpTy> bool match(const OpTy *V) const {
831 auto *Store = dyn_cast<VPWidenStoreRecipe>(V);
832 if (!Store || !Addr.match(Store->getAddr()) ||
833 !Val.match(Store->getStoredValue()) || !Store->isMasked() ||
834 !Mask.match(Store->getMask()))
835 return false;
836 return true;
837 }
838};
839
840/// Match a (possibly reversed) masked store.
841template <typename Addr_t, typename Val_t, typename Mask_t>
842inline Store_match<Addr_t, Val_t, Mask_t>
843m_MaskedStore(const Addr_t &Addr, const Val_t &Val, const Mask_t &Mask) {
844 return Store_match<Addr_t, Val_t, Mask_t>(Addr, Val, Mask);
845}
846
847template <typename Op0_t, typename Op1_t>
850 /*Commutative*/ false, VPVectorEndPointerRecipe>;
851
852template <typename Op0_t, typename Op1_t>
857
858/// Match a call argument at a given argument index.
859template <typename Opnd_t> struct Argument_match {
860 /// Call argument index to match.
861 unsigned OpI;
862 Opnd_t Val;
863
864 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
865
866 template <typename OpTy> bool match(OpTy *V) const {
867 if (const auto *R = dyn_cast<VPWidenIntrinsicRecipe>(V))
868 return Val.match(R->getOperand(OpI));
869 if (const auto *R = dyn_cast<VPWidenCallRecipe>(V))
870 return Val.match(R->getOperand(OpI));
871 if (const auto *R = dyn_cast<VPReplicateRecipe>(V))
872 if (R->getOpcode() == Instruction::Call)
873 return Val.match(R->getOperand(OpI));
874 if (const auto *R = dyn_cast<VPInstruction>(V))
875 if (R->getOpcode() == Instruction::Call)
876 return Val.match(R->getOperand(OpI));
877 return false;
878 }
879};
880
881/// Match a call argument.
882template <unsigned OpI, typename Opnd_t>
883inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
884 return Argument_match<Opnd_t>(OpI, Op);
885}
886
887/// Intrinsic matchers.
889 unsigned ID;
890
891 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
892
893 template <typename OpTy> bool match(OpTy *V) const {
894 if (const auto *R = dyn_cast<VPWidenIntrinsicRecipe>(V))
895 return R->getVectorIntrinsicID() == ID;
896 if (const auto *R = dyn_cast<VPWidenCallRecipe>(V))
897 return R->getCalledScalarFunction()->getIntrinsicID() == ID;
898
899 auto MatchCalleeIntrinsic = [&](VPValue *CalleeOp) {
900 if (!isa<VPIRValue>(CalleeOp))
901 return false;
902 auto *F = cast<Function>(CalleeOp->getLiveInIRValue());
903 return F->getIntrinsicID() == ID;
904 };
905 if (const auto *R = dyn_cast<VPReplicateRecipe>(V))
906 if (R->getOpcode() == Instruction::Call) {
907 // The mask is always the last operand if predicated.
908 return MatchCalleeIntrinsic(
909 R->getOperand(R->getNumOperands() - 1 - R->isPredicated()));
910 }
911 if (const auto *R = dyn_cast<VPInstruction>(V))
912 if (R->getOpcode() == Instruction::Call)
913 return MatchCalleeIntrinsic(R->getOperand(R->getNumOperands() - 1));
914 return false;
915 }
916};
917
918/// Intrinsic matches are combinations of ID matchers, and argument
919/// matchers. Higher arity matcher are defined recursively in terms of and-ing
920/// them with lower arity matchers. Here's some convenient typedefs for up to
921/// several arguments, and more can be added as needed
922template <typename T0 = void, typename T1 = void, typename T2 = void,
923 typename T3 = void>
924struct m_Intrinsic_Ty;
925template <typename T0> struct m_Intrinsic_Ty<T0> {
927};
928template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
929 using Ty =
931};
932template <typename T0, typename T1, typename T2>
937template <typename T0, typename T1, typename T2, typename T3>
942
943/// Match intrinsic calls like this:
944/// m_Intrinsic<Intrinsic::fabs>(m_VPValue(X), ...)
945template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
946 return IntrinsicID_match(IntrID);
947}
948
949/// Match intrinsic calls with a runtime intrinsic ID.
951 return IntrinsicID_match(IntrID);
952}
953
954template <Intrinsic::ID IntrID, typename T0>
955inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
957}
958
959template <Intrinsic::ID IntrID, typename T0, typename T1>
960inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
961 const T1 &Op1) {
963}
964
965template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
966inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
967m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
968 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
969}
970
971template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
972 typename T3>
974m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
975 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
976}
977
979
980/// Match a GEP recipe (VPWidenGEPRecipe, VPInstruction, or VPReplicateRecipe)
981/// and bind the source element type and operands.
985
988
989 template <typename ITy> bool match(ITy *V) const {
990 return matchRecipeAndBind<VPWidenGEPRecipe>(V) ||
991 matchRecipeAndBind<VPInstruction>(V) ||
992 matchRecipeAndBind<VPReplicateRecipe>(V);
993 }
994
995private:
996 template <typename RecipeTy> bool matchRecipeAndBind(const VPValue *V) const {
997 auto *DefR = dyn_cast<RecipeTy>(V);
998 if (!DefR)
999 return false;
1000
1001 if constexpr (std::is_same_v<RecipeTy, VPWidenGEPRecipe>) {
1002 SourceElementType = DefR->getSourceElementType();
1003 } else if (DefR->getOpcode() == Instruction::GetElementPtr) {
1004 SourceElementType = cast<GetElementPtrInst>(DefR->getUnderlyingInstr())
1005 ->getSourceElementType();
1006 } else if constexpr (std::is_same_v<RecipeTy, VPInstruction>) {
1007 if (DefR->getOpcode() == VPInstruction::PtrAdd) {
1008 // PtrAdd is a byte-offset GEP with i8 element type.
1009 LLVMContext &Ctx = DefR->getParent()->getPlan()->getContext();
1011 } else {
1012 return false;
1013 }
1014 } else {
1015 return false;
1016 }
1017
1018 Operands = ArrayRef<VPValue *>(DefR->op_begin(), DefR->op_end());
1019 return true;
1020 }
1021};
1022
1023/// Match a GEP recipe with any number of operands and bind source element type
1024/// and operands.
1025inline GetElementPtr_match m_GetElementPtr(Type *&SourceElementType,
1026 ArrayRef<VPValue *> &Operands) {
1027 return GetElementPtr_match(SourceElementType, Operands);
1028}
1029
1030template <typename SubPattern_t> struct OneUse_match {
1031 SubPattern_t SubPattern;
1032
1033 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
1034
1035 template <typename OpTy> bool match(OpTy *V) {
1036 return V->hasOneUse() && SubPattern.match(V);
1037 }
1038};
1039
1040template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
1041 return SubPattern;
1042}
1043
1044} // namespace llvm::VPlanPatternMatch
1045
1046#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define T
#define T1
MachineInstr unsigned OpIdx
#define P(N)
This file contains the declarations of the Vectorization Plan base classes:
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1561
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
@ ICMP_NE
not equal
Definition InstrTypes.h:698
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
A recipe for converting the input value IV value to the corresponding value of an IV with different s...
Definition VPlan.h:3825
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1141
static unsigned getNumOperandsForOpcode(unsigned Opcode)
Return the number of operands determined by the opcode of the VPInstruction.
@ ComputeAnyOfResult
Compute the final result of a AnyOf reduction with select(cmp(),x,y), where one of (x,...
Definition VPlan.h:1188
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:387
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:3041
A recipe for handling phi nodes of integer and floating-point inductions, producing their scalar valu...
Definition VPlan.h:3897
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition VPlan.h:588
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:258
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:46
A recipe to compute a pointer to the last element of each part of a widened memory access for widened...
Definition VPlan.h:1991
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1680
A recipe for handling GEP instructions.
Definition VPlan.h:1928
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1632
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
VPInstruction_match< VPInstruction::ExtractLastLane, VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > > m_ExtractLastLaneOfLastPart(const Op0_t &Op0)
AllRecipe_match< Instruction::Select, Op0_t, Op1_t, Op2_t > m_Select(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
VPInstruction_match< Instruction::Freeze, Op0_t > m_Freeze(const Op0_t &Op0)
AllRecipe_commutative_match< Instruction::And, Op0_t, Op1_t > m_c_BinaryAnd(const Op0_t &Op0, const Op1_t &Op1)
Match a binary AND operation.
AllRecipe_match< Instruction::ZExt, Op0_t > m_ZExt(const Op0_t &Op0)
AllRecipe_match< Instruction::Or, Op0_t, Op1_t > m_BinaryOr(const Op0_t &Op0, const Op1_t &Op1)
Match a binary OR operation.
int_pred_ty< is_specific_int, Bitwidth > specific_intval
Store_match< Addr_t, Val_t, Mask_t > m_MaskedStore(const Addr_t &Addr, const Val_t &Val, const Mask_t &Mask)
Match a (possibly reversed) masked store.
int_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
AllRecipe_match< Instruction::FMul, Op0_t, Op1_t > m_FMul(const Op0_t &Op0, const Op1_t &Op1)
SpecificCmp_match< Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp > m_SpecificCmp(CmpPredicate MatchPred, const Op0_t &Op0, const Op1_t &Op1)
match_combine_or< VPInstruction_match< VPInstruction::Not, Op0_t >, AllRecipe_commutative_match< Instruction::Xor, int_pred_ty< is_all_ones >, Op0_t > > m_Not(const Op0_t &Op0)
VPInstruction_match< VPInstruction::AnyOf > m_AnyOf()
int_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
AllRecipe_commutative_match< Opcode, Op0_t, Op1_t > m_c_Binary(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_commutative_match< Instruction::Add, Op0_t, Op1_t > m_c_Add(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_commutative_match< Instruction::Or, Op0_t, Op1_t > m_c_BinaryOr(const Op0_t &Op0, const Op1_t &Op1)
bool matchFindIVResult(VPInstruction *VPI, Op0_t ReducedIV, Op1_t Start)
Match FindIV result pattern: select(icmp ne ComputeReductionResult(ReducedIV), Sentinel),...
VPInstruction_match< VPInstruction::ComputeReductionResult, Op0_t > m_ComputeReductionResult(const Op0_t &Op0)
VPInstruction_match< VPInstruction::StepVector > m_StepVector()
match_combine_or< AllRecipe_match< Instruction::ZExt, Op0_t >, AllRecipe_match< Instruction::SExt, Op0_t > > m_ZExtOrSExt(const Op0_t &Op0)
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
SpecificCmp_match< Op0_t, Op1_t, Instruction::ICmp > m_SpecificICmp(CmpPredicate MatchPred, const Op0_t &Op0, const Op1_t &Op1)
VPScalarIVSteps_match< Op0_t, Op1_t, Op2_t > m_ScalarIVSteps(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
AllRecipe_match< Instruction::Add, Op0_t, Op1_t > m_Add(const Op0_t &Op0, const Op1_t &Op1)
GEPLikeRecipe_match< Op0_t, Op1_t > m_GetElementPtr(const Op0_t &Op0, const Op1_t &Op1)
Recipe_match< std::tuple< OpTys... >, Opcode, false, VPInstruction > VPInstruction_match
VPInstruction_match< VPInstruction::BranchOnTwoConds > m_BranchOnTwoConds()
AllRecipe_match< Opcode, Op0_t, Op1_t > m_Binary(const Op0_t &Op0, const Op1_t &Op1)
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
VPInstruction_match< VPInstruction::LastActiveLane, Op0_t > m_LastActiveLane(const Op0_t &Op0)
AllRecipe_match< Opcode, Op0_t > m_Unary(const Op0_t &Op0)
Load_match< Addr_t, Mask_t > m_MaskedLoad(const Addr_t &Addr, const Mask_t &Mask)
Match a (possibly reversed) masked load.
match_combine_or< AllRecipe_match< Instruction::Trunc, Op0_t >, Op0_t > m_TruncOrSelf(const Op0_t &Op0)
AllRecipe_match< Instruction::FPExt, Op0_t > m_FPExt(const Op0_t &Op0)
AllRecipe_commutative_match< Instruction::Mul, Op0_t, Op1_t > m_c_Mul(const Op0_t &Op0, const Op1_t &Op1)
Cmp_match< Op0_t, Op1_t, Instruction::ICmp > m_ICmp(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_match< Instruction::Mul, Op0_t, Op1_t > m_Mul(const Op0_t &Op0, const Op1_t &Op1)
specificval_ty m_Specific(const VPValue *VPV)
match_combine_or< Recipe_match< std::tuple< Op0_t, Op1_t >, Instruction::GetElementPtr, false, VPReplicateRecipe, VPWidenGEPRecipe >, match_combine_or< VPInstruction_match< VPInstruction::PtrAdd, Op0_t, Op1_t >, VPInstruction_match< VPInstruction::WidePtrAdd, Op0_t, Op1_t > > > GEPLikeRecipe_match
VPInstruction_match< Instruction::ExtractElement, Op0_t, Op1_t > m_ExtractElement(const Op0_t &Op0, const Op1_t &Op1)
specific_intval< 1 > m_False()
VPDerivedIV_match< Op0_t, Op1_t, Op2_t > m_DerivedIV(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
VPInstruction_match< VPInstruction::ExtractLastLane, Op0_t > m_ExtractLastLane(const Op0_t &Op0)
specific_intval< 0 > m_SpecificInt(uint64_t V)
VPInstruction_match< VPInstruction::ActiveLaneMask, Op0_t, Op1_t, Op2_t > m_ActiveLaneMask(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
VPInstruction_match< VPInstruction::BranchOnCount > m_BranchOnCount()
bind_ty< VPIRValue > m_VPIRValue(VPIRValue *&V)
Match a VPIRValue.
Recipe_match< std::tuple< Op0_t, Op1_t, Op2_t >, 0, false, VPDerivedIVRecipe > VPDerivedIV_match
AllRecipe_match< Instruction::Sub, Op0_t, Op1_t > m_Sub(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_match< Instruction::SExt, Op0_t > m_SExt(const Op0_t &Op0)
specific_intval< 1 > m_True()
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_VPValue(X), ...)
Recipe_match< std::tuple< OpTys... >, Opcode, true, VPWidenRecipe, VPReplicateRecipe, VPInstruction > AllRecipe_commutative_match
deferredval_ty m_Deferred(VPValue *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
VectorEndPointerRecipe_match< Op0_t, Op1_t > m_VecEndPtr(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > m_ExtractLastPart(const Op0_t &Op0)
VPInstruction_match< VPInstruction::Broadcast, Op0_t > m_Broadcast(const Op0_t &Op0)
bool match(Val *V, const Pattern &P)
class_match< VPValue > m_VPValue()
Match an arbitrary VPValue and ignore it.
OneUse_match< T > m_OneUse(const T &SubPattern)
VPInstruction_match< VPInstruction::ExplicitVectorLength, Op0_t > m_EVL(const Op0_t &Op0)
VPInstruction_match< VPInstruction::BuildVector > m_BuildVector()
BuildVector is matches only its opcode, w/o matching its operands as the number of operands is not fi...
AllRecipe_match< Instruction::Trunc, Op0_t > m_Trunc(const Op0_t &Op0)
VPInstruction_match< VPInstruction::ExtractPenultimateElement, Op0_t > m_ExtractPenultimateElement(const Op0_t &Op0)
Recipe_match< std::tuple< Op0_t, Op1_t >, 0, false, VPVectorEndPointerRecipe > VectorEndPointerRecipe_match
match_combine_or< AllRecipe_match< Instruction::ZExt, Op0_t >, Op0_t > m_ZExtOrSelf(const Op0_t &Op0)
VPInstruction_match< VPInstruction::FirstActiveLane, Op0_t > m_FirstActiveLane(const Op0_t &Op0)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match a call argument.
bind_ty< VPInstruction > m_VPInstruction(VPInstruction *&V)
Match a VPInstruction, capturing if we match.
VPInstruction_match< VPInstruction::ComputeAnyOfResult, Op0_t, Op1_t, Op2_t > m_ComputeAnyOfResult(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
AllRecipe_match< Instruction::UDiv, Op0_t, Op1_t > m_UDiv(const Op0_t &Op0, const Op1_t &Op1)
Recipe_match< std::tuple< Op0_t, Op1_t, Op2_t >, 0, false, VPScalarIVStepsRecipe > VPScalarIVSteps_match
int_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Recipe_match< std::tuple< OpTys... >, Opcode, false, VPWidenRecipe, VPReplicateRecipe, VPWidenCastRecipe, VPInstruction > AllRecipe_match
VPInstruction_match< VPInstruction::BranchOnCond > m_BranchOnCond()
VPInstruction_match< VPInstruction::ExtractLane, Op0_t, Op1_t > m_ExtractLane(const Op0_t &Op0, const Op1_t &Op1)
bind_apint m_APInt(const APInt *&C)
VPInstruction_match< VPInstruction::Reverse, Op0_t > m_Reverse(const Op0_t &Op0)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr auto bind_back(FnT &&Fn, BindArgsT &&...BindArgs)
C++23 bind_back.
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
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1915
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
Intrinsic matches are combinations of ID matchers, and argument matchers.
A VPValue representing a live-in from the input IR or a constant.
Definition VPlanValue.h:183
Match a call argument at a given argument index.
unsigned OpI
Call argument index to match.
Argument_match(unsigned OpIdx, const Opnd_t &V)
Cmp_match is a variant of BinaryRecipe_match that also binds the comparison predicate.
Cmp_match(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1)
Cmp_match(const Op0_t &Op0, const Op1_t &Op1)
bool match(const VPValue *V) const
bool match(const VPRecipeBase *V) const
Match a GEP recipe (VPWidenGEPRecipe, VPInstruction, or VPReplicateRecipe) and bind the source elemen...
GetElementPtr_match(Type *&SourceElementType, ArrayRef< VPValue * > &Operands)
Load_match(Addr_t Addr, Mask_t Mask)
bool match(const VPSingleDefRecipe *R) const
bool match(const VPValue *V) const
bool match(const VPRecipeBase *R) const
SpecificCmp_match is a variant of Cmp_match that matches the comparison predicate,...
SpecificCmp_match(CmpPredicate Pred, const Op0_t &LHS, const Op1_t &RHS)
bool match(const VPRecipeBase *V) const
Store_match(Addr_t Addr, Val_t Val, Mask_t Mask)
Stores a reference to the VPValue *, not the VPValue * itself, thus can be used in commutative matche...
Match an integer constant or vector of constants if Pred::isValue returns true for the APInt.
bool isValue(const APInt &C) const
Match a specified integer value or vector of all elements of that value.
match_combine_and< typename m_Intrinsic_Ty< T0, T1 >::Ty, Argument_match< T2 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0 >::Ty, Argument_match< T1 > > Ty
match_combine_and< IntrinsicID_match, Argument_match< T0 > > Ty
Intrinsic matches are combinations of ID matchers, and argument matchers.
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
match_combine_and(const LTy &Left, const RTy &Right)
match_combine_or(const LTy &Left, const RTy &Right)