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 Class> struct class_match {
47 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
48};
49
50/// Match an arbitrary VPValue and ignore it.
52
53template <typename Class> struct bind_ty {
54 Class *&VR;
55
56 bind_ty(Class *&V) : VR(V) {}
57
58 template <typename ITy> bool match(ITy *V) const {
59 if (auto *CV = dyn_cast<Class>(V)) {
60 VR = CV;
61 return true;
62 }
63 return false;
64 }
65};
66
67/// Match a specified VPValue.
69 const VPValue *Val;
70
71 specificval_ty(const VPValue *V) : Val(V) {}
72
73 bool match(VPValue *VPV) const { return VPV == Val; }
74};
75
76inline specificval_ty m_Specific(const VPValue *VPV) { return VPV; }
77
78/// Stores a reference to the VPValue *, not the VPValue * itself,
79/// thus can be used in commutative matchers.
81 VPValue *const &Val;
82
83 deferredval_ty(VPValue *const &V) : Val(V) {}
84
85 bool match(VPValue *const V) const { return V == Val; }
86};
87
88/// Like m_Specific(), but works if the specific value to match is determined
89/// as part of the same match() expression. For example:
90/// m_Mul(m_VPValue(X), m_Specific(X)) is incorrect, because m_Specific() will
91/// bind X before the pattern match starts.
92/// m_Mul(m_VPValue(X), m_Deferred(X)) is correct, and will check against
93/// whichever value m_VPValue(X) populated.
94inline deferredval_ty m_Deferred(VPValue *const &V) { return V; }
95
96/// Match an integer constant or vector of constants if Pred::isValue returns
97/// true for the APInt. \p BitWidth optionally specifies the bitwidth the
98/// matched constant must have. If it is 0, the matched constant can have any
99/// bitwidth.
100template <typename Pred, unsigned BitWidth = 0> struct int_pred_ty {
101 Pred P;
102
103 int_pred_ty(Pred P) : P(std::move(P)) {}
104 int_pred_ty() : P() {}
105
106 bool match(VPValue *VPV) const {
107 auto *VPI = dyn_cast<VPInstruction>(VPV);
108 if (VPI && VPI->getOpcode() == VPInstruction::Broadcast)
109 VPV = VPI->getOperand(0);
110 auto *CI = dyn_cast<VPConstantInt>(VPV);
111 if (!CI)
112 return false;
113
114 if (BitWidth != 0 && CI->getBitWidth() != BitWidth)
115 return false;
116 return P.isValue(CI->getAPInt());
117 }
118};
119
120/// Match a specified integer value or vector of all elements of that
121/// value. \p BitWidth optionally specifies the bitwidth the matched constant
122/// must have. If it is 0, the matched constant can have any bitwidth.
125
127
128 bool isValue(const APInt &C) const { return APInt::isSameValue(Val, C); }
129};
130
131template <unsigned Bitwidth = 0>
133
137
141
145
147 bool isValue(const APInt &C) const { return C.isAllOnes(); }
148};
149
150/// Match an integer or vector with all bits set.
151/// For vectors, this includes constants with undefined elements.
155
157 bool isValue(const APInt &C) const { return C.isZero(); }
158};
159
160struct is_one {
161 bool isValue(const APInt &C) const { return C.isOne(); }
162};
163
164/// Match an integer 0 or a vector with all elements equal to 0.
165/// For vectors, this includes constants with undefined elements.
169
170/// Match an integer 1 or a vector with all elements equal to 1.
171/// For vectors, this includes constants with undefined elements.
173
175 const APInt *&Res;
176
177 bind_apint(const APInt *&Res) : Res(Res) {}
178
179 bool match(VPValue *VPV) const {
180 auto *CI = dyn_cast<VPConstantInt>(VPV);
181 if (!CI)
182 return false;
183 Res = &CI->getAPInt();
184 return true;
185 }
186};
187
188inline bind_apint m_APInt(const APInt *&C) { return C; }
189
192
194
195 bool match(VPValue *VPV) const {
196 const APInt *APConst;
197 if (!bind_apint(APConst).match(VPV))
198 return false;
199 if (auto C = APConst->tryZExtValue()) {
200 Res = *C;
201 return true;
202 }
203 return false;
204 }
205};
206
207/// Match a plain integer constant no wider than 64-bits, capturing it if we
208/// match.
210
211/// Matching combinators
212template <typename LTy, typename RTy> struct match_combine_or {
213 LTy L;
214 RTy R;
215
216 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
217
218 template <typename ITy> bool match(ITy *V) const {
219 return L.match(V) || R.match(V);
220 }
221};
222
223template <typename LTy, typename RTy> struct match_combine_and {
224 LTy L;
225 RTy R;
226
227 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
228
229 template <typename ITy> bool match(ITy *V) const {
230 return L.match(V) && R.match(V);
231 }
232};
233
234/// Combine two pattern matchers matching L || R
235template <typename LTy, typename RTy>
236inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
237 return match_combine_or<LTy, RTy>(L, R);
238}
239
240/// Combine two pattern matchers matching L && R
241template <typename LTy, typename RTy>
242inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
243 return match_combine_and<LTy, RTy>(L, R);
244}
245
246/// Match a VPValue, capturing it if we match.
247inline bind_ty<VPValue> m_VPValue(VPValue *&V) { return V; }
248
249/// Match a VPInstruction, capturing if we match.
251
252template <typename Ops_t, unsigned Opcode, bool Commutative,
253 typename... RecipeTys>
255 Ops_t Ops;
256
257 template <typename... OpTy> Recipe_match(OpTy... Ops) : Ops(Ops...) {
258 static_assert(std::tuple_size<Ops_t>::value == sizeof...(Ops) &&
259 "number of operands in constructor doesn't match Ops_t");
260 static_assert((!Commutative || std::tuple_size<Ops_t>::value == 2) &&
261 "only binary ops can be commutative");
262 }
263
264 bool match(const VPValue *V) const {
265 auto *DefR = V->getDefiningRecipe();
266 return DefR && match(DefR);
267 }
268
269 bool match(const VPSingleDefRecipe *R) const {
270 return match(static_cast<const VPRecipeBase *>(R));
271 }
272
273 bool match(const VPRecipeBase *R) const {
274 if (std::tuple_size_v<Ops_t> == 0) {
275 auto *VPI = dyn_cast<VPInstruction>(R);
276 return VPI && VPI->getOpcode() == Opcode;
277 }
278
279 if ((!matchRecipeAndOpcode<RecipeTys>(R) && ...))
280 return false;
281
282 if (R->getNumOperands() != std::tuple_size_v<Ops_t>) {
283 [[maybe_unused]] auto *RepR = dyn_cast<VPReplicateRecipe>(R);
286 (RepR && std::tuple_size_v<Ops_t> ==
287 RepR->getNumOperands() - RepR->isPredicated())) &&
288 "non-variadic recipe with matched opcode does not have the "
289 "expected number of operands");
290 return false;
291 }
292
293 auto IdxSeq = std::make_index_sequence<std::tuple_size<Ops_t>::value>();
294 if (all_of_tuple_elements(IdxSeq, [R](auto Op, unsigned Idx) {
295 return Op.match(R->getOperand(Idx));
296 }))
297 return true;
298
299 return Commutative &&
300 all_of_tuple_elements(IdxSeq, [R](auto Op, unsigned Idx) {
301 return Op.match(R->getOperand(R->getNumOperands() - Idx - 1));
302 });
303 }
304
305private:
306 template <typename RecipeTy>
307 static bool matchRecipeAndOpcode(const VPRecipeBase *R) {
308 auto *DefR = dyn_cast<RecipeTy>(R);
309 // Check for recipes that do not have opcodes.
310 if constexpr (std::is_same_v<RecipeTy, VPScalarIVStepsRecipe> ||
311 std::is_same_v<RecipeTy, VPCanonicalIVPHIRecipe> ||
312 std::is_same_v<RecipeTy, VPDerivedIVRecipe> ||
313 std::is_same_v<RecipeTy, VPVectorEndPointerRecipe>)
314 return DefR;
315 else
316 return DefR && DefR->getOpcode() == Opcode;
317 }
318
319 /// Helper to check if predicate \p P holds on all tuple elements in Ops using
320 /// the provided index sequence.
321 template <typename Fn, std::size_t... Is>
322 bool all_of_tuple_elements(std::index_sequence<Is...>, Fn P) const {
323 return (P(std::get<Is>(Ops), Is) && ...);
324 }
325};
326
327template <unsigned Opcode, typename... OpTys>
329 Recipe_match<std::tuple<OpTys...>, Opcode, /*Commutative*/ false,
332
333template <unsigned Opcode, typename... OpTys>
335 Recipe_match<std::tuple<OpTys...>, Opcode, /*Commutative*/ true,
337
338template <unsigned Opcode, typename... OpTys>
339using VPInstruction_match = Recipe_match<std::tuple<OpTys...>, Opcode,
340 /*Commutative*/ false, VPInstruction>;
341
342template <unsigned Opcode, typename... OpTys>
343inline VPInstruction_match<Opcode, OpTys...>
344m_VPInstruction(const OpTys &...Ops) {
345 return VPInstruction_match<Opcode, OpTys...>(Ops...);
346}
347
348/// BuildVector is matches only its opcode, w/o matching its operands as the
349/// number of operands is not fixed.
353
354template <typename Op0_t>
356m_Freeze(const Op0_t &Op0) {
358}
359
363
364template <typename Op0_t>
366m_BranchOnCond(const Op0_t &Op0) {
368}
369
374
375template <typename Op0_t, typename Op1_t>
377m_BranchOnTwoConds(const Op0_t &Op0, const Op1_t &Op1) {
379}
380
381template <typename Op0_t>
383m_Broadcast(const Op0_t &Op0) {
385}
386
387template <typename Op0_t>
389m_EVL(const Op0_t &Op0) {
391}
392
393template <typename Op0_t>
398
399template <typename Op0_t, typename Op1_t>
401m_ExtractElement(const Op0_t &Op0, const Op1_t &Op1) {
403}
404
405template <typename Op0_t, typename Op1_t>
407m_ExtractLane(const Op0_t &Op0, const Op1_t &Op1) {
409}
410
411template <typename Op0_t>
416
417template <typename Op0_t>
423}
424
425template <typename Op0_t>
430
431template <typename Op0_t, typename Op1_t, typename Op2_t>
433m_ActiveLaneMask(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
435}
436
440
441template <typename Op0_t, typename Op1_t>
443m_BranchOnCount(const Op0_t &Op0, const Op1_t &Op1) {
445}
446
450
451template <typename Op0_t>
453m_AnyOf(const Op0_t &Op0) {
455}
456
457template <typename Op0_t>
462
463template <typename Op0_t>
465m_LastActiveLane(const Op0_t &Op0) {
467}
468
469template <typename Op0_t>
474
475template <typename Op0_t, typename Op1_t, typename Op2_t>
477 Op2_t>
478m_ComputeAnyOfResult(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
480}
481
482template <typename Op0_t, typename Op1_t, typename Op2_t>
484 Op2_t>
485m_ComputeFindIVResult(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
487}
488
489template <typename Op0_t>
491m_Reverse(const Op0_t &Op0) {
493}
494
498
499template <unsigned Opcode, typename Op0_t>
500inline AllRecipe_match<Opcode, Op0_t> m_Unary(const Op0_t &Op0) {
502}
503
504template <typename Op0_t>
508
509template <typename Op0_t>
511m_TruncOrSelf(const Op0_t &Op0) {
512 return m_CombineOr(m_Trunc(Op0), Op0);
513}
514
515template <typename Op0_t>
519
520template <typename Op0_t>
524
525template <typename Op0_t>
528m_ZExtOrSExt(const Op0_t &Op0) {
529 return m_CombineOr(m_ZExt(Op0), m_SExt(Op0));
530}
531
532template <typename Op0_t>
534m_ZExtOrSelf(const Op0_t &Op0) {
535 return m_CombineOr(m_ZExt(Op0), Op0);
536}
537
538template <unsigned Opcode, typename Op0_t, typename Op1_t>
540 const Op1_t &Op1) {
542}
543
544template <unsigned Opcode, typename Op0_t, typename Op1_t>
546m_c_Binary(const Op0_t &Op0, const Op1_t &Op1) {
548}
549
550template <typename Op0_t, typename Op1_t>
552 const Op1_t &Op1) {
554}
555
556template <typename Op0_t, typename Op1_t>
558m_c_Add(const Op0_t &Op0, const Op1_t &Op1) {
560}
561
562template <typename Op0_t, typename Op1_t>
564 const Op1_t &Op1) {
566}
567
568template <typename Op0_t, typename Op1_t>
570 const Op1_t &Op1) {
572}
573
574template <typename Op0_t, typename Op1_t>
576m_c_Mul(const Op0_t &Op0, const Op1_t &Op1) {
578}
579
580template <typename Op0_t, typename Op1_t>
582m_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
584}
585
586/// Match a binary AND operation.
587template <typename Op0_t, typename Op1_t>
589m_c_BinaryAnd(const Op0_t &Op0, const Op1_t &Op1) {
591}
592
593/// Match a binary OR operation. Note that while conceptually the operands can
594/// be matched commutatively, \p Commutative defaults to false in line with the
595/// IR-based pattern matching infrastructure. Use m_c_BinaryOr for a commutative
596/// version of the matcher.
597template <typename Op0_t, typename Op1_t>
599m_BinaryOr(const Op0_t &Op0, const Op1_t &Op1) {
601}
602
603template <typename Op0_t, typename Op1_t>
605m_c_BinaryOr(const Op0_t &Op0, const Op1_t &Op1) {
607}
608
609/// Cmp_match is a variant of BinaryRecipe_match that also binds the comparison
610/// predicate. Opcodes must either be Instruction::ICmp or Instruction::FCmp, or
611/// both.
612template <typename Op0_t, typename Op1_t, unsigned... Opcodes>
613struct Cmp_match {
614 static_assert((sizeof...(Opcodes) == 1 || sizeof...(Opcodes) == 2) &&
615 "Expected one or two opcodes");
616 static_assert(
617 ((Opcodes == Instruction::ICmp || Opcodes == Instruction::FCmp) && ...) &&
618 "Expected a compare instruction opcode");
619
621 Op0_t Op0;
623
624 Cmp_match(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1)
625 : Predicate(&Pred), Op0(Op0), Op1(Op1) {}
626 Cmp_match(const Op0_t &Op0, const Op1_t &Op1) : Op0(Op0), Op1(Op1) {}
627
628 bool match(const VPValue *V) const {
629 auto *DefR = V->getDefiningRecipe();
630 return DefR && match(DefR);
631 }
632
633 bool match(const VPRecipeBase *V) const {
634 if ((m_Binary<Opcodes>(Op0, Op1).match(V) || ...)) {
635 if (Predicate)
636 *Predicate = cast<VPRecipeWithIRFlags>(V)->getPredicate();
637 return true;
638 }
639 return false;
640 }
641};
642
643/// SpecificCmp_match is a variant of Cmp_match that matches the comparison
644/// predicate, instead of binding it.
645template <typename Op0_t, typename Op1_t, unsigned... Opcodes>
648 Op0_t Op0;
650
651 SpecificCmp_match(CmpPredicate Pred, const Op0_t &LHS, const Op1_t &RHS)
652 : Predicate(Pred), Op0(LHS), Op1(RHS) {}
653
654 bool match(const VPValue *V) const {
655 auto *DefR = V->getDefiningRecipe();
656 return DefR && match(DefR);
657 }
658
659 bool match(const VPRecipeBase *V) const {
660 CmpPredicate CurrentPred;
661 return Cmp_match<Op0_t, Op1_t, Opcodes...>(CurrentPred, Op0, Op1)
662 .match(V) &&
664 }
665};
666
667template <typename Op0_t, typename Op1_t>
669 const Op1_t &Op1) {
671}
672
673template <typename Op0_t, typename Op1_t>
674inline Cmp_match<Op0_t, Op1_t, Instruction::ICmp>
675m_ICmp(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1) {
676 return Cmp_match<Op0_t, Op1_t, Instruction::ICmp>(Pred, Op0, Op1);
677}
678
679template <typename Op0_t, typename Op1_t>
680inline SpecificCmp_match<Op0_t, Op1_t, Instruction::ICmp>
681m_SpecificICmp(CmpPredicate MatchPred, const Op0_t &Op0, const Op1_t &Op1) {
683 Op1);
684}
685
686template <typename Op0_t, typename Op1_t>
687inline Cmp_match<Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp>
688m_Cmp(const Op0_t &Op0, const Op1_t &Op1) {
690 Op1);
691}
692
693template <typename Op0_t, typename Op1_t>
694inline Cmp_match<Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp>
695m_Cmp(CmpPredicate &Pred, const Op0_t &Op0, const Op1_t &Op1) {
697 Pred, Op0, Op1);
698}
699
700template <typename Op0_t, typename Op1_t>
701inline SpecificCmp_match<Op0_t, Op1_t, Instruction::ICmp, Instruction::FCmp>
702m_SpecificCmp(CmpPredicate MatchPred, const Op0_t &Op0, const Op1_t &Op1) {
704 MatchPred, Op0, Op1);
705}
706
707template <typename Op0_t, typename Op1_t>
709 Recipe_match<std::tuple<Op0_t, Op1_t>, Instruction::GetElementPtr,
710 /*Commutative*/ false, VPReplicateRecipe, VPWidenGEPRecipe>,
714
715template <typename Op0_t, typename Op1_t>
717 const Op1_t &Op1) {
718 return m_CombineOr(
719 Recipe_match<std::tuple<Op0_t, Op1_t>, Instruction::GetElementPtr,
720 /*Commutative*/ false, VPReplicateRecipe, VPWidenGEPRecipe>(
721 Op0, Op1),
725 Op1)));
726}
727
728template <typename Op0_t, typename Op1_t, typename Op2_t>
730m_Select(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
732 {Op0, Op1, Op2});
733}
734
735template <typename Op0_t>
738 Instruction::Xor, int_pred_ty<is_all_ones>, Op0_t>>
743
744template <typename Op0_t, typename Op1_t>
745inline match_combine_or<
748m_LogicalAnd(const Op0_t &Op0, const Op1_t &Op1) {
749 return m_CombineOr(
751 m_Select(Op0, Op1, m_False()));
752}
753
754template <typename Op0_t, typename Op1_t>
756m_LogicalOr(const Op0_t &Op0, const Op1_t &Op1) {
757 return m_Select(Op0, m_True(), Op1);
758}
759
760template <typename Op0_t, typename Op1_t, typename Op2_t>
762 false, VPScalarIVStepsRecipe>;
763
764template <typename Op0_t, typename Op1_t, typename Op2_t>
766m_ScalarIVSteps(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
767 return VPScalarIVSteps_match<Op0_t, Op1_t, Op2_t>({Op0, Op1, Op2});
768}
769
770template <typename Op0_t, typename Op1_t, typename Op2_t>
773
774template <typename Op0_t, typename Op1_t, typename Op2_t>
776m_DerivedIV(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
777 return VPDerivedIV_match<Op0_t, Op1_t, Op2_t>({Op0, Op1, Op2});
778}
779
780template <typename Addr_t, typename Mask_t> struct Load_match {
781 Addr_t Addr;
782 Mask_t Mask;
783
784 Load_match(Addr_t Addr, Mask_t Mask) : Addr(Addr), Mask(Mask) {}
785
786 template <typename OpTy> bool match(const OpTy *V) const {
787 auto *Load = dyn_cast<VPWidenLoadRecipe>(V);
788 if (!Load || !Addr.match(Load->getAddr()) || !Load->isMasked() ||
789 !Mask.match(Load->getMask()))
790 return false;
791 return true;
792 }
793};
794
795/// Match a (possibly reversed) masked load.
796template <typename Addr_t, typename Mask_t>
797inline Load_match<Addr_t, Mask_t> m_MaskedLoad(const Addr_t &Addr,
798 const Mask_t &Mask) {
799 return Load_match<Addr_t, Mask_t>(Addr, Mask);
800}
801
802template <typename Addr_t, typename Val_t, typename Mask_t> struct Store_match {
803 Addr_t Addr;
804 Val_t Val;
805 Mask_t Mask;
806
807 Store_match(Addr_t Addr, Val_t Val, Mask_t Mask)
808 : Addr(Addr), Val(Val), Mask(Mask) {}
809
810 template <typename OpTy> bool match(const OpTy *V) const {
811 auto *Store = dyn_cast<VPWidenStoreRecipe>(V);
812 if (!Store || !Addr.match(Store->getAddr()) ||
813 !Val.match(Store->getStoredValue()) || !Store->isMasked() ||
814 !Mask.match(Store->getMask()))
815 return false;
816 return true;
817 }
818};
819
820/// Match a (possibly reversed) masked store.
821template <typename Addr_t, typename Val_t, typename Mask_t>
822inline Store_match<Addr_t, Val_t, Mask_t>
823m_MaskedStore(const Addr_t &Addr, const Val_t &Val, const Mask_t &Mask) {
824 return Store_match<Addr_t, Val_t, Mask_t>(Addr, Val, Mask);
825}
826
827template <typename Op0_t, typename Op1_t>
830 /*Commutative*/ false, VPVectorEndPointerRecipe>;
831
832template <typename Op0_t, typename Op1_t>
837
838/// Match a call argument at a given argument index.
839template <typename Opnd_t> struct Argument_match {
840 /// Call argument index to match.
841 unsigned OpI;
842 Opnd_t Val;
843
844 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
845
846 template <typename OpTy> bool match(OpTy *V) const {
847 if (const auto *R = dyn_cast<VPWidenIntrinsicRecipe>(V))
848 return Val.match(R->getOperand(OpI));
849 if (const auto *R = dyn_cast<VPWidenCallRecipe>(V))
850 return Val.match(R->getOperand(OpI));
851 if (const auto *R = dyn_cast<VPReplicateRecipe>(V))
852 if (R->getOpcode() == Instruction::Call)
853 return Val.match(R->getOperand(OpI));
854 if (const auto *R = dyn_cast<VPInstruction>(V))
855 if (R->getOpcode() == Instruction::Call)
856 return Val.match(R->getOperand(OpI));
857 return false;
858 }
859};
860
861/// Match a call argument.
862template <unsigned OpI, typename Opnd_t>
863inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
864 return Argument_match<Opnd_t>(OpI, Op);
865}
866
867/// Intrinsic matchers.
869 unsigned ID;
870
871 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
872
873 template <typename OpTy> bool match(OpTy *V) const {
874 if (const auto *R = dyn_cast<VPWidenIntrinsicRecipe>(V))
875 return R->getVectorIntrinsicID() == ID;
876 if (const auto *R = dyn_cast<VPWidenCallRecipe>(V))
877 return R->getCalledScalarFunction()->getIntrinsicID() == ID;
878
879 auto MatchCalleeIntrinsic = [&](VPValue *CalleeOp) {
880 if (!isa<VPIRValue>(CalleeOp))
881 return false;
882 auto *F = cast<Function>(CalleeOp->getLiveInIRValue());
883 return F->getIntrinsicID() == ID;
884 };
885 if (const auto *R = dyn_cast<VPReplicateRecipe>(V))
886 if (R->getOpcode() == Instruction::Call) {
887 // The mask is always the last operand if predicated.
888 return MatchCalleeIntrinsic(
889 R->getOperand(R->getNumOperands() - 1 - R->isPredicated()));
890 }
891 if (const auto *R = dyn_cast<VPInstruction>(V))
892 if (R->getOpcode() == Instruction::Call)
893 return MatchCalleeIntrinsic(R->getOperand(R->getNumOperands() - 1));
894 return false;
895 }
896};
897
898/// Intrinsic matches are combinations of ID matchers, and argument
899/// matchers. Higher arity matcher are defined recursively in terms of and-ing
900/// them with lower arity matchers. Here's some convenient typedefs for up to
901/// several arguments, and more can be added as needed
902template <typename T0 = void, typename T1 = void, typename T2 = void,
903 typename T3 = void>
904struct m_Intrinsic_Ty;
905template <typename T0> struct m_Intrinsic_Ty<T0> {
907};
908template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
909 using Ty =
911};
912template <typename T0, typename T1, typename T2>
917template <typename T0, typename T1, typename T2, typename T3>
922
923/// Match intrinsic calls like this:
924/// m_Intrinsic<Intrinsic::fabs>(m_VPValue(X), ...)
925template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
926 return IntrinsicID_match(IntrID);
927}
928
929/// Match intrinsic calls with a runtime intrinsic ID.
931 return IntrinsicID_match(IntrID);
932}
933
934template <Intrinsic::ID IntrID, typename T0>
935inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
937}
938
939template <Intrinsic::ID IntrID, typename T0, typename T1>
940inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
941 const T1 &Op1) {
943}
944
945template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
946inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
947m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
948 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
949}
950
951template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
952 typename T3>
954m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
955 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
956}
957
959 template <typename ITy> bool match(ITy *V) const {
961 }
962};
963
965
966template <typename SubPattern_t> struct OneUse_match {
967 SubPattern_t SubPattern;
968
969 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
970
971 template <typename OpTy> bool match(OpTy *V) {
972 return V->hasOneUse() && SubPattern.match(V);
973 }
974};
975
976template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
977 return SubPattern;
978}
979
980} // namespace llvm::VPlanPatternMatch
981
982#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
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...
A recipe for converting the input value IV value to the corresponding value of an IV with different s...
Definition VPlan.h:3763
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1086
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:1133
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:2982
A recipe for handling phi nodes of integer and floating-point inductions, producing their scalar valu...
Definition VPlan.h:3831
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition VPlan.h:531
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:253
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:47
A recipe to compute a pointer to the last element of each part of a widened memory access for widened...
Definition VPlan.h:1934
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1625
A recipe for handling GEP instructions.
Definition VPlan.h:1871
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1577
@ 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.
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)
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::ComputeFindIVResult, Op0_t, Op1_t, Op2_t > m_ComputeFindIVResult(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
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_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()
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
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.
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
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)