LLVM 23.0.0git
PatternMatch.h
Go to the documentation of this file.
1//===- PatternMatch.h - Match on the LLVM IR --------------------*- 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 LLVM IR. The power of these routines is
11// that it allows you to write concise patterns that are expressive and easy to
12// understand. The other major advantage of this is that it allows you to
13// trivially capture/bind elements in the pattern to variables. For example,
14// you can do something like this:
15//
16// Value *Exp = ...
17// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
18// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19// m_And(m_Value(Y), m_ConstantInt(C2))))) {
20// ... Pattern is matched and variables are bound ...
21// }
22//
23// This is primarily useful to things like the instruction combiner, but can
24// also be useful for static analysis tools or code generators.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_IR_PATTERNMATCH_H
29#define LLVM_IR_PATTERNMATCH_H
30
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/InstrTypes.h"
37#include "llvm/IR/Instruction.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Operator.h"
42#include "llvm/IR/Value.h"
44#include <cstdint>
45
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return P.match(V);
51}
52
53/// A match functor that can be used as a UnaryPredicate in functional
54/// algorithms like all_of.
55template <typename Val = const Value, typename Pattern>
56auto match_fn(const Pattern &P) {
58}
59
60template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
61 return P.match(Mask);
62}
63
64template <typename SubPattern_t> struct OneUse_match {
65 SubPattern_t SubPattern;
66
67 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
68
69 template <typename OpTy> bool match(OpTy *V) const {
70 return V->hasOneUse() && SubPattern.match(V);
71 }
72};
73
74template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
75 return SubPattern;
76}
77
78template <typename SubPattern_t> struct AllowReassoc_match {
79 SubPattern_t SubPattern;
80
81 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
82
83 template <typename OpTy> bool match(OpTy *V) const {
84 auto *I = dyn_cast<FPMathOperator>(V);
85 return I && I->hasAllowReassoc() && SubPattern.match(I);
86 }
87};
88
89template <typename T>
90inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
91 return SubPattern;
92}
93
94template <typename Class> struct class_match {
95 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
96};
97
98/// Match an arbitrary value and ignore it.
100
101/// Match an arbitrary unary operation and ignore it.
105
106/// Match an arbitrary binary operation and ignore it.
110
111/// Matches any compare instruction and ignore it.
113
115 static bool check(const Value *V) {
116 if (isa<UndefValue>(V))
117 return true;
118
119 const auto *CA = dyn_cast<ConstantAggregate>(V);
120 if (!CA)
121 return false;
122
125
126 // Either UndefValue, PoisonValue, or an aggregate that only contains
127 // these is accepted by matcher.
128 // CheckValue returns false if CA cannot satisfy this constraint.
129 auto CheckValue = [&](const ConstantAggregate *CA) {
130 for (const Value *Op : CA->operand_values()) {
131 if (isa<UndefValue>(Op))
132 continue;
133
134 const auto *CA = dyn_cast<ConstantAggregate>(Op);
135 if (!CA)
136 return false;
137 if (Seen.insert(CA).second)
138 Worklist.emplace_back(CA);
139 }
140
141 return true;
142 };
143
144 if (!CheckValue(CA))
145 return false;
146
147 while (!Worklist.empty()) {
148 if (!CheckValue(Worklist.pop_back_val()))
149 return false;
150 }
151 return true;
152 }
153 template <typename ITy> bool match(ITy *V) const { return check(V); }
154};
155
156/// Match an arbitrary undef constant. This matches poison as well.
157/// If this is an aggregate and contains a non-aggregate element that is
158/// neither undef nor poison, the aggregate is not matched.
159inline auto m_Undef() { return undef_match(); }
160
161/// Match an arbitrary UndefValue constant.
165
166/// Match an arbitrary poison constant.
170
171/// Match an arbitrary Constant and ignore it.
173
174/// Match an arbitrary ConstantInt and ignore it.
178
179/// Match an arbitrary ConstantFP and ignore it.
183
185 template <typename ITy> bool match(ITy *V) const {
186 auto *C = dyn_cast<Constant>(V);
187 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
188 }
189};
190
191/// Match a constant expression or a constant that contains a constant
192/// expression.
194
195template <typename SubPattern_t> struct Splat_match {
196 SubPattern_t SubPattern;
197 Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
198
199 template <typename OpTy> bool match(OpTy *V) const {
200 if (auto *C = dyn_cast<Constant>(V)) {
201 auto *Splat = C->getSplatValue();
202 return Splat ? SubPattern.match(Splat) : false;
203 }
204 // TODO: Extend to other cases (e.g. shufflevectors).
205 return false;
206 }
207};
208
209/// Match a constant splat. TODO: Extend this to non-constant splats.
210template <typename T>
211inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
212 return SubPattern;
213}
214
215/// Match an arbitrary basic block value and ignore it.
219
220/// Inverting matcher
221template <typename Ty> struct match_unless {
222 Ty M;
223
224 match_unless(const Ty &Matcher) : M(Matcher) {}
225
226 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
227};
228
229/// Match if the inner matcher does *NOT* match.
230template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
231 return match_unless<Ty>(M);
232}
233
234/// Matching combinators
235template <typename LTy, typename RTy> struct match_combine_or {
236 LTy L;
237 RTy R;
238
239 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
240
241 template <typename ITy> bool match(ITy *V) const {
242 if (L.match(V))
243 return true;
244 if (R.match(V))
245 return true;
246 return false;
247 }
248};
249
250template <typename LTy, typename RTy> struct match_combine_and {
251 LTy L;
252 RTy R;
253
254 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
255
256 template <typename ITy> bool match(ITy *V) const {
257 if (L.match(V))
258 if (R.match(V))
259 return true;
260 return false;
261 }
262};
263
264/// Combine two pattern matchers matching L || R
265template <typename LTy, typename RTy>
266inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
267 return match_combine_or<LTy, RTy>(L, R);
268}
269
270/// Combine two pattern matchers matching L && R
271template <typename LTy, typename RTy>
272inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
273 return match_combine_and<LTy, RTy>(L, R);
274}
275
276template <typename APTy> struct ap_match {
277 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
279 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
280
281 const APTy *&Res;
283
284 ap_match(const APTy *&Res, bool AllowPoison)
286
287 template <typename ITy> bool match(ITy *V) const {
288 if (auto *CI = dyn_cast<ConstantTy>(V)) {
289 Res = &CI->getValue();
290 return true;
291 }
292 if (V->getType()->isVectorTy())
293 if (const auto *C = dyn_cast<Constant>(V))
294 if (auto *CI =
295 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
296 Res = &CI->getValue();
297 return true;
298 }
299 return false;
300 }
301};
302
303/// Match a ConstantInt or splatted ConstantVector, binding the
304/// specified pointer to the contained APInt.
305inline ap_match<APInt> m_APInt(const APInt *&Res) {
306 // Forbid poison by default to maintain previous behavior.
307 return ap_match<APInt>(Res, /* AllowPoison */ false);
308}
309
310/// Match APInt while allowing poison in splat vector constants.
312 return ap_match<APInt>(Res, /* AllowPoison */ true);
313}
314
315/// Match APInt while forbidding poison in splat vector constants.
317 return ap_match<APInt>(Res, /* AllowPoison */ false);
318}
319
320/// Match a ConstantFP or splatted ConstantVector, binding the
321/// specified pointer to the contained APFloat.
323 // Forbid undefs by default to maintain previous behavior.
324 return ap_match<APFloat>(Res, /* AllowPoison */ false);
325}
326
327/// Match APFloat while allowing poison in splat vector constants.
329 return ap_match<APFloat>(Res, /* AllowPoison */ true);
330}
331
332/// Match APFloat while forbidding poison in splat vector constants.
334 return ap_match<APFloat>(Res, /* AllowPoison */ false);
335}
336
337template <int64_t Val> struct constantint_match {
338 template <typename ITy> bool match(ITy *V) const {
339 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
340 const APInt &CIV = CI->getValue();
341 if (Val >= 0)
342 return CIV == static_cast<uint64_t>(Val);
343 // If Val is negative, and CI is shorter than it, truncate to the right
344 // number of bits. If it is larger, then we have to sign extend. Just
345 // compare their negated values.
346 return -CIV == -Val;
347 }
348 return false;
349 }
350};
351
352/// Match a ConstantInt with a specific value.
353template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
354 return constantint_match<Val>();
355}
356
357/// This helper class is used to match constant scalars, vector splats,
358/// and fixed width vectors that satisfy a specified predicate.
359/// For fixed width vector constants, poison elements are ignored if AllowPoison
360/// is true.
361template <typename Predicate, typename ConstantVal, bool AllowPoison>
362struct cstval_pred_ty : public Predicate {
363 const Constant **Res = nullptr;
364 template <typename ITy> bool match_impl(ITy *V) const {
365 if (const auto *CV = dyn_cast<ConstantVal>(V))
366 return this->isValue(CV->getValue());
367 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
368 if (const auto *C = dyn_cast<Constant>(V)) {
369 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
370 return this->isValue(CV->getValue());
371
372 // Number of elements of a scalable vector unknown at compile time
373 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
374 if (!FVTy)
375 return false;
376
377 // Non-splat vector constant: check each element for a match.
378 unsigned NumElts = FVTy->getNumElements();
379 assert(NumElts != 0 && "Constant vector with no elements?");
380 bool HasNonPoisonElements = false;
381 for (unsigned i = 0; i != NumElts; ++i) {
382 Constant *Elt = C->getAggregateElement(i);
383 if (!Elt)
384 return false;
385 if (AllowPoison && isa<PoisonValue>(Elt))
386 continue;
387 auto *CV = dyn_cast<ConstantVal>(Elt);
388 if (!CV || !this->isValue(CV->getValue()))
389 return false;
390 HasNonPoisonElements = true;
391 }
392 return HasNonPoisonElements;
393 }
394 }
395 return false;
396 }
397
398 template <typename ITy> bool match(ITy *V) const {
399 if (this->match_impl(V)) {
400 if (Res)
401 *Res = cast<Constant>(V);
402 return true;
403 }
404 return false;
405 }
406};
407
408/// specialization of cstval_pred_ty for ConstantInt
409template <typename Predicate, bool AllowPoison = true>
411
412/// specialization of cstval_pred_ty for ConstantFP
413template <typename Predicate>
415 /*AllowPoison=*/true>;
416
417/// This helper class is used to match scalar and vector constants that
418/// satisfy a specified predicate, and bind them to an APInt.
419template <typename Predicate> struct api_pred_ty : public Predicate {
420 const APInt *&Res;
421
422 api_pred_ty(const APInt *&R) : Res(R) {}
423
424 template <typename ITy> bool match(ITy *V) const {
425 if (const auto *CI = dyn_cast<ConstantInt>(V))
426 if (this->isValue(CI->getValue())) {
427 Res = &CI->getValue();
428 return true;
429 }
430 if (V->getType()->isVectorTy())
431 if (const auto *C = dyn_cast<Constant>(V))
432 if (auto *CI = dyn_cast_or_null<ConstantInt>(
433 C->getSplatValue(/*AllowPoison=*/true)))
434 if (this->isValue(CI->getValue())) {
435 Res = &CI->getValue();
436 return true;
437 }
438
439 return false;
440 }
441};
442
443/// This helper class is used to match scalar and vector constants that
444/// satisfy a specified predicate, and bind them to an APFloat.
445/// Poison is allowed in splat vector constants.
446template <typename Predicate> struct apf_pred_ty : public Predicate {
447 const APFloat *&Res;
448
449 apf_pred_ty(const APFloat *&R) : Res(R) {}
450
451 template <typename ITy> bool match(ITy *V) const {
452 if (const auto *CI = dyn_cast<ConstantFP>(V))
453 if (this->isValue(CI->getValue())) {
454 Res = &CI->getValue();
455 return true;
456 }
457 if (V->getType()->isVectorTy())
458 if (const auto *C = dyn_cast<Constant>(V))
459 if (auto *CI = dyn_cast_or_null<ConstantFP>(
460 C->getSplatValue(/* AllowPoison */ true)))
461 if (this->isValue(CI->getValue())) {
462 Res = &CI->getValue();
463 return true;
464 }
465
466 return false;
467 }
468};
469
470///////////////////////////////////////////////////////////////////////////////
471//
472// Encapsulate constant value queries for use in templated predicate matchers.
473// This allows checking if constants match using compound predicates and works
474// with vector constants, possibly with relaxed constraints. For example, ignore
475// undef values.
476//
477///////////////////////////////////////////////////////////////////////////////
478
479template <typename APTy> struct custom_checkfn {
480 function_ref<bool(const APTy &)> CheckFn;
481 bool isValue(const APTy &C) const { return CheckFn(C); }
482};
483
484/// Match an integer or vector where CheckFn(ele) for each element is true.
485/// For vectors, poison elements are assumed to match.
487m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
488 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
489}
490
492m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
493 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
494}
495
496/// Match a float or vector where CheckFn(ele) for each element is true.
497/// For vectors, poison elements are assumed to match.
499m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
500 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
501}
502
504m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
505 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
506}
507
509 bool isValue(const APInt &C) const { return true; }
510};
511/// Match an integer or vector with any integral constant.
512/// For vectors, this includes constants with undefined elements.
516
518 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
519};
520
524
526 bool isValue(const APInt &C) const { return C.isAllOnes(); }
527};
528/// Match an integer or vector with all bits set.
529/// For vectors, this includes constants with undefined elements.
533
537
539 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
540};
541/// Match an integer or vector with values having all bits except for the high
542/// bit set (0x7f...).
543/// For vectors, this includes constants with undefined elements.
548 return V;
549}
550
552 bool isValue(const APInt &C) const { return C.isNegative(); }
553};
554/// Match an integer or vector of negative values.
555/// For vectors, this includes constants with undefined elements.
559inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
560
562 bool isValue(const APInt &C) const { return C.isNonNegative(); }
563};
564/// Match an integer or vector of non-negative values.
565/// For vectors, this includes constants with undefined elements.
569inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
570
572 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
573};
574/// Match an integer or vector of strictly positive values.
575/// For vectors, this includes constants with undefined elements.
580 return V;
581}
582
584 bool isValue(const APInt &C) const { return C.isNonPositive(); }
585};
586/// Match an integer or vector of non-positive values.
587/// For vectors, this includes constants with undefined elements.
591inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
592
593struct is_one {
594 bool isValue(const APInt &C) const { return C.isOne(); }
595};
596/// Match an integer 1 or a vector with all elements equal to 1.
597/// For vectors, this includes constants with undefined elements.
599
601 bool isValue(const APInt &C) const { return C.isZero(); }
602};
603/// Match an integer 0 or a vector with all elements equal to 0.
604/// For vectors, this includes constants with undefined elements.
608
610 bool isValue(const APInt &C) const { return !C.isZero(); }
611};
612/// Match a non-zero integer or a vector with all non-zero elements.
613/// For vectors, this includes constants with undefined elements.
617
618struct is_zero {
619 template <typename ITy> bool match(ITy *V) const {
620 auto *C = dyn_cast<Constant>(V);
621 // FIXME: this should be able to do something for scalable vectors
622 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
623 }
624};
625/// Match any null constant or a vector with all elements equal to 0.
626/// For vectors, this includes constants with undefined elements.
627inline is_zero m_Zero() { return is_zero(); }
628
629struct is_power2 {
630 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
631};
632/// Match an integer or vector power-of-2.
633/// For vectors, this includes constants with undefined elements.
635inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
636
638 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
639};
640/// Match a integer or vector negated power-of-2.
641/// For vectors, this includes constants with undefined elements.
646 return V;
647}
648
650 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
651};
652/// Match a integer or vector negated power-of-2.
653/// For vectors, this includes constants with undefined elements.
659 return V;
660}
661
663 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
664};
665/// Match an integer or vector of 0 or power-of-2 values.
666/// For vectors, this includes constants with undefined elements.
671 return V;
672}
673
675 bool isValue(const APInt &C) const { return C.isSignMask(); }
676};
677/// Match an integer or vector with only the sign bit(s) set.
678/// For vectors, this includes constants with undefined elements.
682
684 bool isValue(const APInt &C) const { return C.isMask(); }
685};
686/// Match an integer or vector with only the low bit(s) set.
687/// For vectors, this includes constants with undefined elements.
691inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
692
694 bool isValue(const APInt &C) const { return !C || C.isMask(); }
695};
696/// Match an integer or vector with only the low bit(s) set.
697/// For vectors, this includes constants with undefined elements.
702 return V;
703}
704
707 const APInt *Thr;
708 bool isValue(const APInt &C) const {
709 return ICmpInst::compare(C, *Thr, Pred);
710 }
711};
712/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
713/// to Threshold. For vectors, this includes constants with undefined elements.
717 P.Pred = Predicate;
718 P.Thr = &Threshold;
719 return P;
720}
721
722struct is_nan {
723 bool isValue(const APFloat &C) const { return C.isNaN(); }
724};
725/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
726/// For vectors, this includes constants with undefined elements.
728
729struct is_nonnan {
730 bool isValue(const APFloat &C) const { return !C.isNaN(); }
731};
732/// Match a non-NaN FP constant.
733/// For vectors, this includes constants with undefined elements.
737
738struct is_inf {
739 bool isValue(const APFloat &C) const { return C.isInfinity(); }
740};
741/// Match a positive or negative infinity FP constant.
742/// For vectors, this includes constants with undefined elements.
744
745struct is_noninf {
746 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
747};
748/// Match a non-infinity FP constant, i.e. finite or NaN.
749/// For vectors, this includes constants with undefined elements.
753
754struct is_finite {
755 bool isValue(const APFloat &C) const { return C.isFinite(); }
756};
757/// Match a finite FP constant, i.e. not infinity or NaN.
758/// For vectors, this includes constants with undefined elements.
762inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
763
765 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
766};
767/// Match a finite non-zero FP constant.
768/// For vectors, this includes constants with undefined elements.
773 return V;
774}
775
777 bool isValue(const APFloat &C) const { return C.isZero(); }
778};
779/// Match a floating-point negative zero or positive zero.
780/// For vectors, this includes constants with undefined elements.
784
786 bool isValue(const APFloat &C) const { return C.isPosZero(); }
787};
788/// Match a floating-point positive zero.
789/// For vectors, this includes constants with undefined elements.
793
795 bool isValue(const APFloat &C) const { return C.isNegZero(); }
796};
797/// Match a floating-point negative zero.
798/// For vectors, this includes constants with undefined elements.
802
804 bool isValue(const APFloat &C) const { return C.isNonZero(); }
805};
806/// Match a floating-point non-zero.
807/// For vectors, this includes constants with undefined elements.
811
813 bool isValue(const APFloat &C) const {
814 return !C.isDenormal() && C.isNonZero();
815 }
816};
817
818/// Match a floating-point non-zero that is not a denormal.
819/// For vectors, this includes constants with undefined elements.
823
824///////////////////////////////////////////////////////////////////////////////
825
826template <typename Class> struct bind_ty {
827 Class *&VR;
828
829 bind_ty(Class *&V) : VR(V) {}
830
831 template <typename ITy> bool match(ITy *V) const {
832 if (auto *CV = dyn_cast<Class>(V)) {
833 VR = CV;
834 return true;
835 }
836 return false;
837 }
838};
839
840/// Check whether the value has the given Class and matches the nested
841/// pattern. Capture it into the provided variable if successful.
842template <typename Class, typename MatchTy> struct bind_and_match_ty {
843 Class *&VR;
844 MatchTy Match;
845
846 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
847
848 template <typename ITy> bool match(ITy *V) const {
849 auto *CV = dyn_cast<Class>(V);
850 if (CV && Match.match(V)) {
851 VR = CV;
852 return true;
853 }
854 return false;
855 }
856};
857
858/// Match a value, capturing it if we match.
859inline bind_ty<Value> m_Value(Value *&V) { return V; }
860inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
861
862/// Match against the nested pattern, and capture the value if we match.
863template <typename MatchTy>
865 const MatchTy &Match) {
866 return {V, Match};
867}
868
869/// Match against the nested pattern, and capture the value if we match.
870template <typename MatchTy>
872 const MatchTy &Match) {
873 return {V, Match};
874}
875
876/// Match an instruction, capturing it if we match.
879 return I;
880}
881
882/// Match against the nested pattern, and capture the instruction if we match.
883template <typename MatchTy>
885m_Instruction(Instruction *&I, const MatchTy &Match) {
886 return {I, Match};
887}
888template <typename MatchTy>
890m_Instruction(const Instruction *&I, const MatchTy &Match) {
891 return {I, Match};
892}
893
894/// Match a unary operator, capturing it if we match.
897 return I;
898}
899/// Match a binary operator, capturing it if we match.
902 return I;
903}
904/// Match a with overflow intrinsic, capturing it if we match.
910 return I;
911}
912
913/// Match an UndefValue, capturing the value if we match.
915
916/// Match a Constant, capturing the value if we match.
918
919/// Match a ConstantInt, capturing the value if we match.
921
922/// Match a ConstantFP, capturing the value if we match.
924
925/// Match a ConstantExpr, capturing the value if we match.
927
928/// Match a basic block value, capturing it if we match.
931 return V;
932}
933
934// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
935// and use m_Unless(m_ConstantExpr).
937 template <typename ITy> static bool isImmConstant(ITy *V) {
938 if (auto *CV = dyn_cast<Constant>(V)) {
939 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
940 return true;
941
942 if (CV->getType()->isVectorTy()) {
943 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
944 if (!isa<ConstantExpr>(Splat) &&
945 !Splat->containsConstantExpression()) {
946 return true;
947 }
948 }
949 }
950 }
951 return false;
952 }
953};
954
956 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
957};
958
959/// Match an arbitrary immediate Constant and ignore it.
961
964
966
967 template <typename ITy> bool match(ITy *V) const {
968 if (isImmConstant(V)) {
969 VR = cast<Constant>(V);
970 return true;
971 }
972 return false;
973 }
974};
975
976/// Match an immediate Constant, capturing the value if we match.
980
981/// Match a specified Value*.
983 const Value *Val;
984
985 specificval_ty(const Value *V) : Val(V) {}
986
987 template <typename ITy> bool match(ITy *V) const { return V == Val; }
988};
989
990/// Match if we have a specific specified value.
991inline specificval_ty m_Specific(const Value *V) { return V; }
992
993/// Stores a reference to the Value *, not the Value * itself,
994/// thus can be used in commutative matchers.
995template <typename Class> struct deferredval_ty {
996 Class *const &Val;
997
998 deferredval_ty(Class *const &V) : Val(V) {}
999
1000 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
1001};
1002
1003/// Like m_Specific(), but works if the specific value to match is determined
1004/// as part of the same match() expression. For example:
1005/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
1006/// bind X before the pattern match starts.
1007/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
1008/// whichever value m_Value(X) populated.
1009inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
1011 return V;
1012}
1013
1014/// Match a specified floating point value or vector of all elements of
1015/// that value.
1017 double Val;
1018
1019 specific_fpval(double V) : Val(V) {}
1020
1021 template <typename ITy> bool match(ITy *V) const {
1022 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1023 return CFP->isExactlyValue(Val);
1024 if (V->getType()->isVectorTy())
1025 if (const auto *C = dyn_cast<Constant>(V))
1026 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1027 return CFP->isExactlyValue(Val);
1028 return false;
1029 }
1030};
1031
1032/// Match a specific floating point value or vector with all elements
1033/// equal to the value.
1034inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1035
1036/// Match a float 1.0 or vector with all elements equal to 1.0.
1037inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1038
1041
1043
1044 template <typename ITy> bool match(ITy *V) const {
1045 const APInt *ConstInt;
1046 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1047 return false;
1048 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
1049 if (!ZExtVal)
1050 return false;
1051 VR = *ZExtVal;
1052 return true;
1053 }
1054};
1055
1056/// Match a specified integer value or vector of all elements of that
1057/// value.
1058template <bool AllowPoison> struct specific_intval {
1059 const APInt &Val;
1060
1061 specific_intval(const APInt &V) : Val(V) {}
1062
1063 template <typename ITy> bool match(ITy *V) const {
1064 const auto *CI = dyn_cast<ConstantInt>(V);
1065 if (!CI && V->getType()->isVectorTy())
1066 if (const auto *C = dyn_cast<Constant>(V))
1067 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1068
1069 return CI && APInt::isSameValue(CI->getValue(), Val);
1070 }
1071};
1072
1073template <bool AllowPoison> struct specific_intval64 {
1075
1077
1078 template <typename ITy> bool match(ITy *V) const {
1079 const auto *CI = dyn_cast<ConstantInt>(V);
1080 if (!CI && V->getType()->isVectorTy())
1081 if (const auto *C = dyn_cast<Constant>(V))
1082 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1083
1084 return CI && CI->getValue() == Val;
1085 }
1086};
1087
1088/// Match a specific integer value or vector with all elements equal to
1089/// the value.
1091 return specific_intval<false>(V);
1092}
1093
1097
1101
1105
1106/// Match a ConstantInt and bind to its value. This does not match
1107/// ConstantInts wider than 64-bits.
1109
1110/// Match a specified basic block value.
1113
1115
1116 template <typename ITy> bool match(ITy *V) const {
1117 const auto *BB = dyn_cast<BasicBlock>(V);
1118 return BB && BB == Val;
1119 }
1120};
1121
1122/// Match a specific basic block value.
1124 return specific_bbval(BB);
1125}
1126
1127/// A commutative-friendly version of m_Specific().
1129 return BB;
1130}
1132m_Deferred(const BasicBlock *const &BB) {
1133 return BB;
1134}
1135
1136//===----------------------------------------------------------------------===//
1137// Matcher for any binary operator.
1138//
1139template <typename LHS_t, typename RHS_t, bool Commutable = false>
1143
1144 // The evaluation order is always stable, regardless of Commutability.
1145 // The LHS is always matched first.
1146 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1147
1148 template <typename OpTy> bool match(OpTy *V) const {
1149 if (auto *I = dyn_cast<BinaryOperator>(V))
1150 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1151 (Commutable && L.match(I->getOperand(1)) &&
1152 R.match(I->getOperand(0)));
1153 return false;
1154 }
1155};
1156
1157template <typename LHS, typename RHS>
1158inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1159 return AnyBinaryOp_match<LHS, RHS>(L, R);
1160}
1161
1162//===----------------------------------------------------------------------===//
1163// Matcher for any unary operator.
1164// TODO fuse unary, binary matcher into n-ary matcher
1165//
1166template <typename OP_t> struct AnyUnaryOp_match {
1167 OP_t X;
1168
1169 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1170
1171 template <typename OpTy> bool match(OpTy *V) const {
1172 if (auto *I = dyn_cast<UnaryOperator>(V))
1173 return X.match(I->getOperand(0));
1174 return false;
1175 }
1176};
1177
1178template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1179 return AnyUnaryOp_match<OP_t>(X);
1180}
1181
1182//===----------------------------------------------------------------------===//
1183// Matchers for specific binary operators.
1184//
1185
1186template <typename LHS_t, typename RHS_t, unsigned Opcode,
1187 bool Commutable = false>
1191
1192 // The evaluation order is always stable, regardless of Commutability.
1193 // The LHS is always matched first.
1194 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1195
1196 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1197 if (V->getValueID() == Value::InstructionVal + Opc) {
1198 auto *I = cast<BinaryOperator>(V);
1199 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1200 (Commutable && L.match(I->getOperand(1)) &&
1201 R.match(I->getOperand(0)));
1202 }
1203 return false;
1204 }
1205
1206 template <typename OpTy> bool match(OpTy *V) const {
1207 return match(Opcode, V);
1208 }
1209};
1210
1211template <typename LHS, typename RHS>
1216
1217template <typename LHS, typename RHS>
1222
1223template <typename LHS, typename RHS>
1228
1229template <typename LHS, typename RHS>
1234
1235template <typename Op_t> struct FNeg_match {
1236 Op_t X;
1237
1238 FNeg_match(const Op_t &Op) : X(Op) {}
1239 template <typename OpTy> bool match(OpTy *V) const {
1240 auto *FPMO = dyn_cast<FPMathOperator>(V);
1241 if (!FPMO)
1242 return false;
1243
1244 if (FPMO->getOpcode() == Instruction::FNeg)
1245 return X.match(FPMO->getOperand(0));
1246
1247 if (FPMO->getOpcode() == Instruction::FSub) {
1248 if (FPMO->hasNoSignedZeros()) {
1249 // With 'nsz', any zero goes.
1250 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1251 return false;
1252 } else {
1253 // Without 'nsz', we need fsub -0.0, X exactly.
1254 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1255 return false;
1256 }
1257
1258 return X.match(FPMO->getOperand(1));
1259 }
1260
1261 return false;
1262 }
1263};
1264
1265/// Match 'fneg X' as 'fsub -0.0, X'.
1266template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1267 return FNeg_match<OpTy>(X);
1268}
1269
1270/// Match 'fneg X' as 'fsub +-0.0, X'.
1271template <typename RHS>
1272inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1273m_FNegNSZ(const RHS &X) {
1274 return m_FSub(m_AnyZeroFP(), X);
1275}
1276
1277template <typename LHS, typename RHS>
1282
1283template <typename LHS, typename RHS>
1288
1289template <typename LHS, typename RHS>
1294
1295template <typename LHS, typename RHS>
1300
1301template <typename LHS, typename RHS>
1306
1307template <typename LHS, typename RHS>
1312
1313template <typename LHS, typename RHS>
1318
1319template <typename LHS, typename RHS>
1324
1325template <typename LHS, typename RHS>
1330
1331template <typename LHS, typename RHS>
1336
1337template <typename LHS, typename RHS>
1342
1343template <typename LHS, typename RHS>
1348
1349template <typename LHS, typename RHS>
1354
1355template <typename LHS, typename RHS>
1360
1361template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1364
1366
1367 template <typename OpTy> bool match(OpTy *V) const {
1368 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1369 if (Op->getOpcode() == Opcode)
1370 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1371 L.match(Op->getOperand(0));
1372 }
1373 // Interpreted as shiftop V, 0
1374 R = 0;
1375 return L.match(V);
1376 }
1377};
1378
1379/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1380template <typename LHS>
1385
1386/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1387template <typename LHS>
1392
1393/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1394template <typename LHS>
1399
1400template <typename LHS_t, typename RHS_t, unsigned Opcode,
1401 unsigned WrapFlags = 0, bool Commutable = false>
1405
1407 : L(LHS), R(RHS) {}
1408
1409 template <typename OpTy> bool match(OpTy *V) const {
1410 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1411 if (Op->getOpcode() != Opcode)
1412 return false;
1414 !Op->hasNoUnsignedWrap())
1415 return false;
1416 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1417 !Op->hasNoSignedWrap())
1418 return false;
1419 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1420 (Commutable && L.match(Op->getOperand(1)) &&
1421 R.match(Op->getOperand(0)));
1422 }
1423 return false;
1424 }
1425};
1426
1427template <typename LHS, typename RHS>
1428inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1430m_NSWAdd(const LHS &L, const RHS &R) {
1431 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1433 R);
1434}
1435template <typename LHS, typename RHS>
1436inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1438m_c_NSWAdd(const LHS &L, const RHS &R) {
1439 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1441 true>(L, R);
1442}
1443template <typename LHS, typename RHS>
1444inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1446m_NSWSub(const LHS &L, const RHS &R) {
1447 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1449 R);
1450}
1451template <typename LHS, typename RHS>
1452inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1454m_NSWMul(const LHS &L, const RHS &R) {
1455 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1457 R);
1458}
1459template <typename LHS, typename RHS>
1460inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1462m_NSWShl(const LHS &L, const RHS &R) {
1463 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1465 R);
1466}
1467
1468template <typename LHS, typename RHS>
1469inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1471m_NUWAdd(const LHS &L, const RHS &R) {
1472 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1474 L, R);
1475}
1476
1477template <typename LHS, typename RHS>
1479 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1480m_c_NUWAdd(const LHS &L, const RHS &R) {
1481 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1483 true>(L, R);
1484}
1485
1486template <typename LHS, typename RHS>
1487inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1489m_NUWSub(const LHS &L, const RHS &R) {
1490 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1492 L, R);
1493}
1494template <typename LHS, typename RHS>
1495inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1497m_NUWMul(const LHS &L, const RHS &R) {
1498 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1500 L, R);
1501}
1502template <typename LHS, typename RHS>
1503inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1505m_NUWShl(const LHS &L, const RHS &R) {
1506 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1508 L, R);
1509}
1510
1511template <typename LHS_t, typename RHS_t, bool Commutable = false>
1513 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1514 unsigned Opcode;
1515
1517 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1518
1519 template <typename OpTy> bool match(OpTy *V) const {
1521 }
1522};
1523
1524/// Matches a specific opcode.
1525template <typename LHS, typename RHS>
1526inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1527 const RHS &R) {
1528 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1529}
1530
1531template <typename LHS, typename RHS, bool Commutable = false>
1535
1536 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1537
1538 template <typename OpTy> bool match(OpTy *V) const {
1539 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1540 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1541 if (!PDI->isDisjoint())
1542 return false;
1543 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1544 (Commutable && L.match(PDI->getOperand(1)) &&
1545 R.match(PDI->getOperand(0)));
1546 }
1547 return false;
1548 }
1549};
1550
1551template <typename LHS, typename RHS>
1553 return DisjointOr_match<LHS, RHS>(L, R);
1554}
1555
1556template <typename LHS, typename RHS>
1558 const RHS &R) {
1560}
1561
1562/// Match either "add" or "or disjoint".
1563template <typename LHS, typename RHS>
1566m_AddLike(const LHS &L, const RHS &R) {
1567 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1568}
1569
1570/// Match either "add nsw" or "or disjoint"
1571template <typename LHS, typename RHS>
1572inline match_combine_or<
1573 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1576m_NSWAddLike(const LHS &L, const RHS &R) {
1577 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1578}
1579
1580/// Match either "add nuw" or "or disjoint"
1581template <typename LHS, typename RHS>
1582inline match_combine_or<
1583 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1586m_NUWAddLike(const LHS &L, const RHS &R) {
1587 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1588}
1589
1590template <typename LHS, typename RHS>
1594
1595 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1596
1597 template <typename OpTy> bool match(OpTy *V) const {
1598 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1599 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1600 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1601 ; // Pass
1602 else if (Op->getOpcode() != Instruction::Xor)
1603 return false;
1604 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1605 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1606 }
1607 return false;
1608 }
1609};
1610
1611/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1612/// Only commutative matcher as the `sub` will need to swap the L and R.
1613template <typename LHS, typename RHS>
1614inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1615 return XorLike_match<LHS, RHS>(L, R);
1616}
1617
1618//===----------------------------------------------------------------------===//
1619// Class that matches a group of binary opcodes.
1620//
1621template <typename LHS_t, typename RHS_t, typename Predicate,
1622 bool Commutable = false>
1623struct BinOpPred_match : Predicate {
1626
1627 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1628
1629 template <typename OpTy> bool match(OpTy *V) const {
1630 if (auto *I = dyn_cast<Instruction>(V))
1631 return this->isOpType(I->getOpcode()) &&
1632 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1633 (Commutable && L.match(I->getOperand(1)) &&
1634 R.match(I->getOperand(0))));
1635 return false;
1636 }
1637};
1638
1640 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1641};
1642
1644 bool isOpType(unsigned Opcode) const {
1645 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1646 }
1647};
1648
1650 bool isOpType(unsigned Opcode) const {
1651 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1652 }
1653};
1654
1656 bool isOpType(unsigned Opcode) const {
1657 return Instruction::isBitwiseLogicOp(Opcode);
1658 }
1659};
1660
1662 bool isOpType(unsigned Opcode) const {
1663 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1664 }
1665};
1666
1668 bool isOpType(unsigned Opcode) const {
1669 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1670 }
1671};
1672
1673/// Matches shift operations.
1674template <typename LHS, typename RHS>
1676 const RHS &R) {
1678}
1679
1680/// Matches logical shift operations.
1681template <typename LHS, typename RHS>
1686
1687/// Matches logical shift operations.
1688template <typename LHS, typename RHS>
1690m_LogicalShift(const LHS &L, const RHS &R) {
1692}
1693
1694/// Matches bitwise logic operations.
1695template <typename LHS, typename RHS>
1697m_BitwiseLogic(const LHS &L, const RHS &R) {
1699}
1700
1701/// Matches bitwise logic operations in either order.
1702template <typename LHS, typename RHS>
1707
1708/// Matches integer division operations.
1709template <typename LHS, typename RHS>
1711 const RHS &R) {
1713}
1714
1715/// Matches integer remainder operations.
1716template <typename LHS, typename RHS>
1718 const RHS &R) {
1720}
1721
1722//===----------------------------------------------------------------------===//
1723// Class that matches exact binary ops.
1724//
1725template <typename SubPattern_t> struct Exact_match {
1726 SubPattern_t SubPattern;
1727
1728 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1729
1730 template <typename OpTy> bool match(OpTy *V) const {
1731 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1732 return PEO->isExact() && SubPattern.match(V);
1733 return false;
1734 }
1735};
1736
1737template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1738 return SubPattern;
1739}
1740
1741//===----------------------------------------------------------------------===//
1742// Matchers for CmpInst classes
1743//
1744
1745template <typename LHS_t, typename RHS_t, typename Class,
1746 bool Commutable = false>
1751
1752 // The evaluation order is always stable, regardless of Commutability.
1753 // The LHS is always matched first.
1755 : Predicate(&Pred), L(LHS), R(RHS) {}
1757 : Predicate(nullptr), L(LHS), R(RHS) {}
1758
1759 template <typename OpTy> bool match(OpTy *V) const {
1760 if (auto *I = dyn_cast<Class>(V)) {
1761 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1762 if (Predicate)
1764 return true;
1765 }
1766 if (Commutable && L.match(I->getOperand(1)) &&
1767 R.match(I->getOperand(0))) {
1768 if (Predicate)
1770 return true;
1771 }
1772 }
1773 return false;
1774 }
1775};
1776
1777template <typename LHS, typename RHS>
1779 const RHS &R) {
1780 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1781}
1782
1783template <typename LHS, typename RHS>
1785 const LHS &L, const RHS &R) {
1786 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1787}
1788
1789template <typename LHS, typename RHS>
1791 const LHS &L, const RHS &R) {
1792 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1793}
1794
1795template <typename LHS, typename RHS>
1798}
1799
1800template <typename LHS, typename RHS>
1803}
1804
1805template <typename LHS, typename RHS>
1808}
1809
1810// Same as CmpClass, but instead of saving Pred as out output variable, match a
1811// specific input pred for equality.
1812template <typename LHS_t, typename RHS_t, typename Class,
1813 bool Commutable = false>
1818
1820 : Predicate(Pred), L(LHS), R(RHS) {}
1821
1822 template <typename OpTy> bool match(OpTy *V) const {
1823 if (auto *I = dyn_cast<Class>(V)) {
1825 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1826 return true;
1827 if constexpr (Commutable) {
1830 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1831 return true;
1832 }
1833 }
1834
1835 return false;
1836 }
1837};
1838
1839template <typename LHS, typename RHS>
1841m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1842 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1843}
1844
1845template <typename LHS, typename RHS>
1847m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1848 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1849}
1850
1851template <typename LHS, typename RHS>
1853m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1855}
1856
1857template <typename LHS, typename RHS>
1859m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1860 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1861}
1862
1863//===----------------------------------------------------------------------===//
1864// Matchers for instructions with a given opcode and number of operands.
1865//
1866
1867/// Matches instructions with Opcode and three operands.
1868template <typename T0, unsigned Opcode> struct OneOps_match {
1870
1871 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1872
1873 template <typename OpTy> bool match(OpTy *V) const {
1874 if (V->getValueID() == Value::InstructionVal + Opcode) {
1875 auto *I = cast<Instruction>(V);
1876 return Op1.match(I->getOperand(0));
1877 }
1878 return false;
1879 }
1880};
1881
1882/// Matches instructions with Opcode and three operands.
1883template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1886
1887 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1888
1889 template <typename OpTy> bool match(OpTy *V) const {
1890 if (V->getValueID() == Value::InstructionVal + Opcode) {
1891 auto *I = cast<Instruction>(V);
1892 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1893 }
1894 return false;
1895 }
1896};
1897
1898/// Matches instructions with Opcode and three operands.
1899template <typename T0, typename T1, typename T2, unsigned Opcode,
1900 bool CommutableOp2Op3 = false>
1905
1906 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1907 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1908
1909 template <typename OpTy> bool match(OpTy *V) const {
1910 if (V->getValueID() == Value::InstructionVal + Opcode) {
1911 auto *I = cast<Instruction>(V);
1912 if (!Op1.match(I->getOperand(0)))
1913 return false;
1914 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1915 return true;
1916 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1917 Op3.match(I->getOperand(1));
1918 }
1919 return false;
1920 }
1921};
1922
1923/// Matches instructions with Opcode and any number of operands
1924template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1925 std::tuple<OperandTypes...> Operands;
1926
1927 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1928
1929 // Operand matching works by recursively calling match_operands, matching the
1930 // operands left to right. The first version is called for each operand but
1931 // the last, for which the second version is called. The second version of
1932 // match_operands is also used to match each individual operand.
1933 template <int Idx, int Last>
1934 std::enable_if_t<Idx != Last, bool>
1938
1939 template <int Idx, int Last>
1940 std::enable_if_t<Idx == Last, bool>
1942 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1943 }
1944
1945 template <typename OpTy> bool match(OpTy *V) const {
1946 if (V->getValueID() == Value::InstructionVal + Opcode) {
1947 auto *I = cast<Instruction>(V);
1948 return I->getNumOperands() == sizeof...(OperandTypes) &&
1949 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1950 }
1951 return false;
1952 }
1953};
1954
1955/// Matches SelectInst.
1956template <typename Cond, typename LHS, typename RHS>
1958m_Select(const Cond &C, const LHS &L, const RHS &R) {
1960}
1961
1962/// This matches a select of two constants, e.g.:
1963/// m_SelectCst<-1, 0>(m_Value(V))
1964template <int64_t L, int64_t R, typename Cond>
1966 Instruction::Select>
1969}
1970
1971/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1972template <typename LHS, typename RHS>
1973inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1974m_c_Select(const LHS &L, const RHS &R) {
1975 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1976 true>(m_Value(), L, R);
1977}
1978
1979/// Matches FreezeInst.
1980template <typename OpTy>
1984
1985/// Matches InsertElementInst.
1986template <typename Val_t, typename Elt_t, typename Idx_t>
1988m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1990 Val, Elt, Idx);
1991}
1992
1993/// Matches ExtractElementInst.
1994template <typename Val_t, typename Idx_t>
1996m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1998}
1999
2000/// Matches shuffle.
2001template <typename T0, typename T1, typename T2> struct Shuffle_match {
2005
2006 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
2007 : Op1(Op1), Op2(Op2), Mask(Mask) {}
2008
2009 template <typename OpTy> bool match(OpTy *V) const {
2010 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
2011 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
2012 Mask.match(I->getShuffleMask());
2013 }
2014 return false;
2015 }
2016};
2017
2018struct m_Mask {
2021 bool match(ArrayRef<int> Mask) const {
2022 MaskRef = Mask;
2023 return true;
2024 }
2025};
2026
2028 bool match(ArrayRef<int> Mask) const {
2029 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2030 }
2031};
2032
2036 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2037};
2038
2042 bool match(ArrayRef<int> Mask) const {
2043 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2044 if (First == Mask.end())
2045 return false;
2046 SplatIndex = *First;
2047 return all_of(Mask,
2048 [First](int Elem) { return Elem == *First || Elem == -1; });
2049 }
2050};
2051
2052template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2053 PointerOpTy PointerOp;
2054 OffsetOpTy OffsetOp;
2055
2056 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2058
2059 template <typename OpTy> bool match(OpTy *V) const {
2060 auto *GEP = dyn_cast<GEPOperator>(V);
2061 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2062 PointerOp.match(GEP->getPointerOperand()) &&
2063 OffsetOp.match(GEP->idx_begin()->get());
2064 }
2065};
2066
2067/// Matches ShuffleVectorInst independently of mask value.
2068template <typename V1_t, typename V2_t>
2070m_Shuffle(const V1_t &v1, const V2_t &v2) {
2072}
2073
2074template <typename V1_t, typename V2_t, typename Mask_t>
2076m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2078}
2079
2080/// Matches LoadInst.
2081template <typename OpTy>
2085
2086/// Matches StoreInst.
2087template <typename ValueOpTy, typename PointerOpTy>
2089m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2091 PointerOp);
2092}
2093
2094/// Matches GetElementPtrInst.
2095template <typename... OperandTypes>
2096inline auto m_GEP(const OperandTypes &...Ops) {
2097 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2098}
2099
2100/// Matches GEP with i8 source element type
2101template <typename PointerOpTy, typename OffsetOpTy>
2103m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2105}
2106
2107//===----------------------------------------------------------------------===//
2108// Matchers for CastInst classes
2109//
2110
2111template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2112 Op_t Op;
2113
2114 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2115
2116 template <typename OpTy> bool match(OpTy *V) const {
2117 if (auto *O = dyn_cast<Operator>(V))
2118 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2119 return false;
2120 }
2121};
2122
2123template <typename Op_t, typename Class> struct CastInst_match {
2124 Op_t Op;
2125
2126 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2127
2128 template <typename OpTy> bool match(OpTy *V) const {
2129 if (auto *I = dyn_cast<Class>(V))
2130 return Op.match(I->getOperand(0));
2131 return false;
2132 }
2133};
2134
2135template <typename Op_t> struct PtrToIntSameSize_match {
2137 Op_t Op;
2138
2139 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2140 : DL(DL), Op(OpMatch) {}
2141
2142 template <typename OpTy> bool match(OpTy *V) const {
2143 if (auto *O = dyn_cast<Operator>(V))
2144 return O->getOpcode() == Instruction::PtrToInt &&
2145 DL.getTypeSizeInBits(O->getType()) ==
2146 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2147 Op.match(O->getOperand(0));
2148 return false;
2149 }
2150};
2151
2152template <typename Op_t> struct NNegZExt_match {
2153 Op_t Op;
2154
2155 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2156
2157 template <typename OpTy> bool match(OpTy *V) const {
2158 if (auto *I = dyn_cast<ZExtInst>(V))
2159 return I->hasNonNeg() && Op.match(I->getOperand(0));
2160 return false;
2161 }
2162};
2163
2164template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2165 Op_t Op;
2166
2167 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2168
2169 template <typename OpTy> bool match(OpTy *V) const {
2170 if (auto *I = dyn_cast<TruncInst>(V))
2171 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2172 Op.match(I->getOperand(0));
2173 return false;
2174 }
2175};
2176
2177/// Matches BitCast.
2178template <typename OpTy>
2183
2184template <typename Op_t> struct ElementWiseBitCast_match {
2185 Op_t Op;
2186
2187 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2188
2189 template <typename OpTy> bool match(OpTy *V) const {
2190 auto *I = dyn_cast<BitCastInst>(V);
2191 if (!I)
2192 return false;
2193 Type *SrcType = I->getSrcTy();
2194 Type *DstType = I->getType();
2195 // Make sure the bitcast doesn't change between scalar and vector and
2196 // doesn't change the number of vector elements.
2197 if (SrcType->isVectorTy() != DstType->isVectorTy())
2198 return false;
2199 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2200 SrcVecTy && SrcVecTy->getElementCount() !=
2201 cast<VectorType>(DstType)->getElementCount())
2202 return false;
2203 return Op.match(I->getOperand(0));
2204 }
2205};
2206
2207template <typename OpTy>
2211
2212/// Matches PtrToInt.
2213template <typename OpTy>
2218
2219template <typename OpTy>
2224
2225/// Matches PtrToAddr.
2226template <typename OpTy>
2231
2232/// Matches PtrToInt or PtrToAddr.
2233template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2235}
2236
2237/// Matches IntToPtr.
2238template <typename OpTy>
2243
2244/// Matches any cast or self. Used to ignore casts.
2245template <typename OpTy>
2247m_CastOrSelf(const OpTy &Op) {
2249}
2250
2251/// Matches Trunc.
2252template <typename OpTy>
2256
2257/// Matches trunc nuw.
2258template <typename OpTy>
2263
2264/// Matches trunc nsw.
2265template <typename OpTy>
2270
2271template <typename OpTy>
2273m_TruncOrSelf(const OpTy &Op) {
2274 return m_CombineOr(m_Trunc(Op), Op);
2275}
2276
2277/// Matches SExt.
2278template <typename OpTy>
2282
2283/// Matches ZExt.
2284template <typename OpTy>
2288
2289template <typename OpTy>
2291 return NNegZExt_match<OpTy>(Op);
2292}
2293
2294template <typename OpTy>
2296m_ZExtOrSelf(const OpTy &Op) {
2297 return m_CombineOr(m_ZExt(Op), Op);
2298}
2299
2300template <typename OpTy>
2302m_SExtOrSelf(const OpTy &Op) {
2303 return m_CombineOr(m_SExt(Op), Op);
2304}
2305
2306/// Match either "sext" or "zext nneg".
2307template <typename OpTy>
2309m_SExtLike(const OpTy &Op) {
2310 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2311}
2312
2313template <typename OpTy>
2316m_ZExtOrSExt(const OpTy &Op) {
2317 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2318}
2319
2320template <typename OpTy>
2323 OpTy>
2325 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2326}
2327
2328template <typename OpTy>
2331 OpTy>
2334}
2335
2336template <typename OpTy>
2340
2341template <typename OpTy>
2345
2346template <typename OpTy>
2350
2351template <typename OpTy>
2355
2356template <typename OpTy>
2360
2361template <typename OpTy>
2365
2366//===----------------------------------------------------------------------===//
2367// Matchers for control flow.
2368//
2369
2370struct br_match {
2372
2374
2375 template <typename OpTy> bool match(OpTy *V) const {
2376 if (auto *BI = dyn_cast<BranchInst>(V))
2377 if (BI->isUnconditional()) {
2378 Succ = BI->getSuccessor(0);
2379 return true;
2380 }
2381 return false;
2382 }
2383};
2384
2385inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2386
2387template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2389 Cond_t Cond;
2390 TrueBlock_t T;
2391 FalseBlock_t F;
2392
2393 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2394 : Cond(C), T(t), F(f) {}
2395
2396 template <typename OpTy> bool match(OpTy *V) const {
2397 if (auto *BI = dyn_cast<BranchInst>(V))
2398 if (BI->isConditional() && Cond.match(BI->getCondition()))
2399 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2400 return false;
2401 }
2402};
2403
2404template <typename Cond_t>
2410
2411template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2413m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2415}
2416
2417//===----------------------------------------------------------------------===//
2418// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2419//
2420
2421template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2422 bool Commutable = false>
2424 using PredType = Pred_t;
2427
2428 // The evaluation order is always stable, regardless of Commutability.
2429 // The LHS is always matched first.
2430 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2431
2432 template <typename OpTy> bool match(OpTy *V) const {
2433 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2434 Intrinsic::ID IID = II->getIntrinsicID();
2435 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2436 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2437 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2438 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2439 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2440 return (L.match(LHS) && R.match(RHS)) ||
2441 (Commutable && L.match(RHS) && R.match(LHS));
2442 }
2443 }
2444 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2445 auto *SI = dyn_cast<SelectInst>(V);
2446 if (!SI)
2447 return false;
2448 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2449 if (!Cmp)
2450 return false;
2451 // At this point we have a select conditioned on a comparison. Check that
2452 // it is the values returned by the select that are being compared.
2453 auto *TrueVal = SI->getTrueValue();
2454 auto *FalseVal = SI->getFalseValue();
2455 auto *LHS = Cmp->getOperand(0);
2456 auto *RHS = Cmp->getOperand(1);
2457 if ((TrueVal != LHS || FalseVal != RHS) &&
2458 (TrueVal != RHS || FalseVal != LHS))
2459 return false;
2460 typename CmpInst_t::Predicate Pred =
2461 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2462 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2463 if (!Pred_t::match(Pred))
2464 return false;
2465 // It does! Bind the operands.
2466 return (L.match(LHS) && R.match(RHS)) ||
2467 (Commutable && L.match(RHS) && R.match(LHS));
2468 }
2469};
2470
2471/// Helper class for identifying signed max predicates.
2473 static bool match(ICmpInst::Predicate Pred) {
2474 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2475 }
2476};
2477
2478/// Helper class for identifying signed min predicates.
2480 static bool match(ICmpInst::Predicate Pred) {
2481 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2482 }
2483};
2484
2485/// Helper class for identifying unsigned max predicates.
2487 static bool match(ICmpInst::Predicate Pred) {
2488 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2489 }
2490};
2491
2492/// Helper class for identifying unsigned min predicates.
2494 static bool match(ICmpInst::Predicate Pred) {
2495 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2496 }
2497};
2498
2499/// Helper class for identifying ordered max predicates.
2501 static bool match(FCmpInst::Predicate Pred) {
2502 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2503 }
2504};
2505
2506/// Helper class for identifying ordered min predicates.
2508 static bool match(FCmpInst::Predicate Pred) {
2509 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2510 }
2511};
2512
2513/// Helper class for identifying unordered max predicates.
2515 static bool match(FCmpInst::Predicate Pred) {
2516 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2517 }
2518};
2519
2520/// Helper class for identifying unordered min predicates.
2522 static bool match(FCmpInst::Predicate Pred) {
2523 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2524 }
2525};
2526
2527template <typename LHS, typename RHS>
2532
2533template <typename LHS, typename RHS>
2538
2539template <typename LHS, typename RHS>
2544
2545template <typename LHS, typename RHS>
2550
2551template <typename LHS, typename RHS>
2552inline match_combine_or<
2557m_MaxOrMin(const LHS &L, const RHS &R) {
2558 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2559 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2560}
2561
2562/// Match an 'ordered' floating point maximum function.
2563/// Floating point has one special value 'NaN'. Therefore, there is no total
2564/// order. However, if we can ignore the 'NaN' value (for example, because of a
2565/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2566/// semantics. In the presence of 'NaN' we have to preserve the original
2567/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2568///
2569/// max(L, R) iff L and R are not NaN
2570/// m_OrdFMax(L, R) = R iff L or R are NaN
2571template <typename LHS, typename RHS>
2576
2577/// Match an 'ordered' floating point minimum function.
2578/// Floating point has one special value 'NaN'. Therefore, there is no total
2579/// order. However, if we can ignore the 'NaN' value (for example, because of a
2580/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2581/// semantics. In the presence of 'NaN' we have to preserve the original
2582/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2583///
2584/// min(L, R) iff L and R are not NaN
2585/// m_OrdFMin(L, R) = R iff L or R are NaN
2586template <typename LHS, typename RHS>
2591
2592/// Match an 'unordered' floating point maximum function.
2593/// Floating point has one special value 'NaN'. Therefore, there is no total
2594/// order. However, if we can ignore the 'NaN' value (for example, because of a
2595/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2596/// semantics. In the presence of 'NaN' we have to preserve the original
2597/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2598///
2599/// max(L, R) iff L and R are not NaN
2600/// m_UnordFMax(L, R) = L iff L or R are NaN
2601template <typename LHS, typename RHS>
2603m_UnordFMax(const LHS &L, const RHS &R) {
2605}
2606
2607/// Match an 'unordered' floating point minimum function.
2608/// Floating point has one special value 'NaN'. Therefore, there is no total
2609/// order. However, if we can ignore the 'NaN' value (for example, because of a
2610/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2611/// semantics. In the presence of 'NaN' we have to preserve the original
2612/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2613///
2614/// min(L, R) iff L and R are not NaN
2615/// m_UnordFMin(L, R) = L iff L or R are NaN
2616template <typename LHS, typename RHS>
2618m_UnordFMin(const LHS &L, const RHS &R) {
2620}
2621
2622/// Match an 'ordered' or 'unordered' floating point maximum function.
2623/// Floating point has one special value 'NaN'. Therefore, there is no total
2624/// order. However, if we can ignore the 'NaN' value (for example, because of a
2625/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2626/// semantics.
2627template <typename LHS, typename RHS>
2634
2635/// Match an 'ordered' or 'unordered' floating point minimum function.
2636/// Floating point has one special value 'NaN'. Therefore, there is no total
2637/// order. However, if we can ignore the 'NaN' value (for example, because of a
2638/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2639/// semantics.
2640template <typename LHS, typename RHS>
2647
2648/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2649/// NOTE: we first match the 'Not' (by matching '-1'),
2650/// and only then match the inner matcher!
2651template <typename ValTy>
2652inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2653m_Not(const ValTy &V) {
2654 return m_c_Xor(m_AllOnes(), V);
2655}
2656
2657template <typename ValTy>
2658inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2659 true>
2660m_NotForbidPoison(const ValTy &V) {
2661 return m_c_Xor(m_AllOnesForbidPoison(), V);
2662}
2663
2664//===----------------------------------------------------------------------===//
2665// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2666// Note that S might be matched to other instructions than AddInst.
2667//
2668
2669template <typename LHS_t, typename RHS_t, typename Sum_t>
2673 Sum_t S;
2674
2675 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2676 : L(L), R(R), S(S) {}
2677
2678 template <typename OpTy> bool match(OpTy *V) const {
2679 Value *ICmpLHS, *ICmpRHS;
2680 CmpPredicate Pred;
2681 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2682 return false;
2683
2684 Value *AddLHS, *AddRHS;
2685 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2686
2687 // (a + b) u< a, (a + b) u< b
2688 if (Pred == ICmpInst::ICMP_ULT)
2689 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2690 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2691
2692 // a >u (a + b), b >u (a + b)
2693 if (Pred == ICmpInst::ICMP_UGT)
2694 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2695 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2696
2697 Value *Op1;
2698 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2699 // (~a) <u b
2700 if (Pred == ICmpInst::ICMP_ULT) {
2701 if (XorExpr.match(ICmpLHS))
2702 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2703 }
2704 // b > u (~a)
2705 if (Pred == ICmpInst::ICMP_UGT) {
2706 if (XorExpr.match(ICmpRHS))
2707 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2708 }
2709
2710 // Match special-case for increment-by-1.
2711 if (Pred == ICmpInst::ICMP_EQ) {
2712 // (a + 1) == 0
2713 // (1 + a) == 0
2714 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2715 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2716 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2717 // 0 == (a + 1)
2718 // 0 == (1 + a)
2719 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2720 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2721 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2722 }
2723
2724 return false;
2725 }
2726};
2727
2728/// Match an icmp instruction checking for unsigned overflow on addition.
2729///
2730/// S is matched to the addition whose result is being checked for overflow, and
2731/// L and R are matched to the LHS and RHS of S.
2732template <typename LHS_t, typename RHS_t, typename Sum_t>
2734m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2736}
2737
2738template <typename Opnd_t> struct Argument_match {
2739 unsigned OpI;
2740 Opnd_t Val;
2741
2742 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2743
2744 template <typename OpTy> bool match(OpTy *V) const {
2745 // FIXME: Should likely be switched to use `CallBase`.
2746 if (const auto *CI = dyn_cast<CallInst>(V))
2747 return Val.match(CI->getArgOperand(OpI));
2748 return false;
2749 }
2750};
2751
2752/// Match an argument.
2753template <unsigned OpI, typename Opnd_t>
2754inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2755 return Argument_match<Opnd_t>(OpI, Op);
2756}
2757
2758/// Intrinsic matchers.
2760 unsigned ID;
2761
2763
2764 template <typename OpTy> bool match(OpTy *V) const {
2765 if (const auto *CI = dyn_cast<CallInst>(V))
2766 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2767 return F->getIntrinsicID() == ID;
2768 return false;
2769 }
2770};
2771
2772/// Intrinsic matches are combinations of ID matchers, and argument
2773/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2774/// them with lower arity matchers. Here's some convenient typedefs for up to
2775/// several arguments, and more can be added as needed
2776template <typename T0 = void, typename T1 = void, typename T2 = void,
2777 typename T3 = void, typename T4 = void, typename T5 = void,
2778 typename T6 = void, typename T7 = void, typename T8 = void,
2779 typename T9 = void, typename T10 = void>
2781template <typename T0> struct m_Intrinsic_Ty<T0> {
2783};
2784template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2785 using Ty =
2787};
2788template <typename T0, typename T1, typename T2>
2793template <typename T0, typename T1, typename T2, typename T3>
2798
2799template <typename T0, typename T1, typename T2, typename T3, typename T4>
2804
2805template <typename T0, typename T1, typename T2, typename T3, typename T4,
2806 typename T5>
2811
2812/// Match intrinsic calls like this:
2813/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2814template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2815 return IntrinsicID_match(IntrID);
2816}
2817
2818/// Matches MaskedLoad Intrinsic.
2819template <typename Opnd0, typename Opnd1, typename Opnd2>
2821m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2822 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2823}
2824
2825/// Matches MaskedStore Intrinsic.
2826template <typename Opnd0, typename Opnd1, typename Opnd2>
2828m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2829 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2830}
2831
2832/// Matches MaskedGather Intrinsic.
2833template <typename Opnd0, typename Opnd1, typename Opnd2>
2835m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2836 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2837}
2838
2839template <Intrinsic::ID IntrID, typename T0>
2840inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2842}
2843
2844template <Intrinsic::ID IntrID, typename T0, typename T1>
2845inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2846 const T1 &Op1) {
2848}
2849
2850template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2851inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2852m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2853 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2854}
2855
2856template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2857 typename T3>
2859m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2860 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2861}
2862
2863template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2864 typename T3, typename T4>
2866m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2867 const T4 &Op4) {
2868 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2869 m_Argument<4>(Op4));
2870}
2871
2872template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2873 typename T3, typename T4, typename T5>
2875m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2876 const T4 &Op4, const T5 &Op5) {
2877 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2878 m_Argument<5>(Op5));
2879}
2880
2881// Helper intrinsic matching specializations.
2882template <typename Opnd0>
2883inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2885}
2886
2887template <typename Opnd0>
2888inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2890}
2891
2892template <typename Opnd0>
2893inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2894 return m_Intrinsic<Intrinsic::fabs>(Op0);
2895}
2896
2897template <typename Opnd0>
2898inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2900}
2901
2902template <typename Opnd0, typename Opnd1>
2903inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2904 const Opnd1 &Op1) {
2905 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2906}
2907
2908template <typename Opnd0, typename Opnd1>
2909inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2910 const Opnd1 &Op1) {
2911 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2912}
2913
2914template <typename Opnd0, typename Opnd1>
2916m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2917 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2918}
2919
2920template <typename Opnd0, typename Opnd1>
2921inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2922 const Opnd1 &Op1) {
2923 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2924}
2925
2926template <typename Opnd0, typename Opnd1>
2927inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2928 const Opnd1 &Op1) {
2929 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2930}
2931
2932template <typename Opnd0, typename Opnd1>
2934m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2935 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2936}
2937
2938template <typename Opnd0, typename Opnd1, typename Opnd2>
2940m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2941 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2942}
2943
2944template <typename Opnd0, typename Opnd1, typename Opnd2>
2946m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2947 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2948}
2949
2950template <typename Opnd0>
2951inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2952 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2953}
2954
2955template <typename Opnd0, typename Opnd1>
2956inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2957 const Opnd1 &Op1) {
2958 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2959}
2960
2961template <typename Opnd0>
2962inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2964}
2965
2966template <typename Opnd0, typename Opnd1, typename Opnd2>
2968m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2969 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
2970}
2971
2972//===----------------------------------------------------------------------===//
2973// Matchers for two-operands operators with the operators in either order
2974//
2975
2976/// Matches a BinaryOperator with LHS and RHS in either order.
2977template <typename LHS, typename RHS>
2980}
2981
2982/// Matches an ICmp with a predicate over LHS and RHS in either order.
2983/// Swaps the predicate if operands are commuted.
2984template <typename LHS, typename RHS>
2986m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2988}
2989
2990template <typename LHS, typename RHS>
2995
2996/// Matches a specific opcode with LHS and RHS in either order.
2997template <typename LHS, typename RHS>
2999m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3000 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3001}
3002
3003/// Matches a Add with LHS and RHS in either order.
3004template <typename LHS, typename RHS>
3009
3010/// Matches a Mul with LHS and RHS in either order.
3011template <typename LHS, typename RHS>
3016
3017/// Matches an And with LHS and RHS in either order.
3018template <typename LHS, typename RHS>
3023
3024/// Matches an Or with LHS and RHS in either order.
3025template <typename LHS, typename RHS>
3030
3031/// Matches an Xor with LHS and RHS in either order.
3032template <typename LHS, typename RHS>
3037
3038/// Matches a 'Neg' as 'sub 0, V'.
3039template <typename ValTy>
3040inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3041m_Neg(const ValTy &V) {
3042 return m_Sub(m_ZeroInt(), V);
3043}
3044
3045/// Matches a 'Neg' as 'sub nsw 0, V'.
3046template <typename ValTy>
3048 Instruction::Sub,
3050m_NSWNeg(const ValTy &V) {
3051 return m_NSWSub(m_ZeroInt(), V);
3052}
3053
3054/// Matches an SMin with LHS and RHS in either order.
3055template <typename LHS, typename RHS>
3057m_c_SMin(const LHS &L, const RHS &R) {
3059}
3060/// Matches an SMax with LHS and RHS in either order.
3061template <typename LHS, typename RHS>
3063m_c_SMax(const LHS &L, const RHS &R) {
3065}
3066/// Matches a UMin with LHS and RHS in either order.
3067template <typename LHS, typename RHS>
3069m_c_UMin(const LHS &L, const RHS &R) {
3071}
3072/// Matches a UMax with LHS and RHS in either order.
3073template <typename LHS, typename RHS>
3075m_c_UMax(const LHS &L, const RHS &R) {
3077}
3078
3079template <typename LHS, typename RHS>
3080inline match_combine_or<
3085m_c_MaxOrMin(const LHS &L, const RHS &R) {
3086 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3087 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3088}
3089
3090template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3094
3095 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3096
3097 template <typename OpTy> bool match(OpTy *V) const {
3098 const auto *II = dyn_cast<IntrinsicInst>(V);
3099 if (!II || II->getIntrinsicID() != IntrID)
3100 return false;
3101 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3102 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3103 }
3104};
3105
3106template <Intrinsic::ID IntrID, typename T0, typename T1>
3108m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3110}
3111
3112/// Matches FAdd with LHS and RHS in either order.
3113template <typename LHS, typename RHS>
3115m_c_FAdd(const LHS &L, const RHS &R) {
3117}
3118
3119/// Matches FMul with LHS and RHS in either order.
3120template <typename LHS, typename RHS>
3122m_c_FMul(const LHS &L, const RHS &R) {
3124}
3125
3126template <typename Opnd_t> struct Signum_match {
3127 Opnd_t Val;
3128 Signum_match(const Opnd_t &V) : Val(V) {}
3129
3130 template <typename OpTy> bool match(OpTy *V) const {
3131 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3132 if (TypeSize == 0)
3133 return false;
3134
3135 unsigned ShiftWidth = TypeSize - 1;
3136 Value *Op;
3137
3138 // This is the representation of signum we match:
3139 //
3140 // signum(x) == (x >> 63) | (-x >>u 63)
3141 //
3142 // An i1 value is its own signum, so it's correct to match
3143 //
3144 // signum(x) == (x >> 0) | (-x >>u 0)
3145 //
3146 // for i1 values.
3147
3148 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3149 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3150 auto Signum = m_c_Or(LHS, RHS);
3151
3152 return Signum.match(V) && Val.match(Op);
3153 }
3154};
3155
3156/// Matches a signum pattern.
3157///
3158/// signum(x) =
3159/// x > 0 -> 1
3160/// x == 0 -> 0
3161/// x < 0 -> -1
3162template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3163 return Signum_match<Val_t>(V);
3164}
3165
3166template <int Ind, typename Opnd_t> struct ExtractValue_match {
3167 Opnd_t Val;
3168 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3169
3170 template <typename OpTy> bool match(OpTy *V) const {
3171 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3172 // If Ind is -1, don't inspect indices
3173 if (Ind != -1 &&
3174 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3175 return false;
3176 return Val.match(I->getAggregateOperand());
3177 }
3178 return false;
3179 }
3180};
3181
3182/// Match a single index ExtractValue instruction.
3183/// For example m_ExtractValue<1>(...)
3184template <int Ind, typename Val_t>
3188
3189/// Match an ExtractValue instruction with any index.
3190/// For example m_ExtractValue(...)
3191template <typename Val_t>
3192inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3193 return ExtractValue_match<-1, Val_t>(V);
3194}
3195
3196/// Matcher for a single index InsertValue instruction.
3197template <int Ind, typename T0, typename T1> struct InsertValue_match {
3200
3201 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3202
3203 template <typename OpTy> bool match(OpTy *V) const {
3204 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3205 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3206 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3207 }
3208 return false;
3209 }
3210};
3211
3212/// Matches a single index InsertValue instruction.
3213template <int Ind, typename Val_t, typename Elt_t>
3215 const Elt_t &Elt) {
3216 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3217}
3218
3219/// Matches a call to `llvm.vscale()`.
3221
3222template <typename Opnd0, typename Opnd1>
3224m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3226}
3227
3228template <typename Opnd>
3232
3233template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3237
3238 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3239
3240 template <typename T> bool match(T *V) const {
3241 auto *I = dyn_cast<Instruction>(V);
3242 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3243 return false;
3244
3245 if (I->getOpcode() == Opcode) {
3246 auto *Op0 = I->getOperand(0);
3247 auto *Op1 = I->getOperand(1);
3248 return (L.match(Op0) && R.match(Op1)) ||
3249 (Commutable && L.match(Op1) && R.match(Op0));
3250 }
3251
3252 if (auto *Select = dyn_cast<SelectInst>(I)) {
3253 auto *Cond = Select->getCondition();
3254 auto *TVal = Select->getTrueValue();
3255 auto *FVal = Select->getFalseValue();
3256
3257 // Don't match a scalar select of bool vectors.
3258 // Transforms expect a single type for operands if this matches.
3259 if (Cond->getType() != Select->getType())
3260 return false;
3261
3262 if (Opcode == Instruction::And) {
3263 auto *C = dyn_cast<Constant>(FVal);
3264 if (C && C->isNullValue())
3265 return (L.match(Cond) && R.match(TVal)) ||
3266 (Commutable && L.match(TVal) && R.match(Cond));
3267 } else {
3268 assert(Opcode == Instruction::Or);
3269 auto *C = dyn_cast<Constant>(TVal);
3270 if (C && C->isOneValue())
3271 return (L.match(Cond) && R.match(FVal)) ||
3272 (Commutable && L.match(FVal) && R.match(Cond));
3273 }
3274 }
3275
3276 return false;
3277 }
3278};
3279
3280/// Matches L && R either in the form of L & R or L ? R : false.
3281/// Note that the latter form is poison-blocking.
3282template <typename LHS, typename RHS>
3287
3288/// Matches L && R where L and R are arbitrary values.
3289inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3290
3291/// Matches L && R with LHS and RHS in either order.
3292template <typename LHS, typename RHS>
3294m_c_LogicalAnd(const LHS &L, const RHS &R) {
3296}
3297
3298/// Matches L || R either in the form of L | R or L ? true : R.
3299/// Note that the latter form is poison-blocking.
3300template <typename LHS, typename RHS>
3305
3306/// Matches L || R where L and R are arbitrary values.
3307inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3308
3309/// Matches L || R with LHS and RHS in either order.
3310template <typename LHS, typename RHS>
3312m_c_LogicalOr(const LHS &L, const RHS &R) {
3314}
3315
3316/// Matches either L && R or L || R,
3317/// either one being in the either binary or logical form.
3318/// Note that the latter form is poison-blocking.
3319template <typename LHS, typename RHS, bool Commutable = false>
3325
3326/// Matches either L && R or L || R where L and R are arbitrary values.
3327inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3328
3329/// Matches either L && R or L || R with LHS and RHS in either order.
3330template <typename LHS, typename RHS>
3331inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3332 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3333}
3334
3335} // end namespace PatternMatch
3336} // end namespace llvm
3337
3338#endif // LLVM_IR_PATTERNMATCH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static constexpr unsigned long long mask(BlockVerifier::State S)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define T1
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
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:1567
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
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...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h:413
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1130
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
bool isShift() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
'undef' values are things that do not have specified contents.
Definition Constants.h:1445
LLVM Value Representation.
Definition Value.h:75
Base class of all SIMD vector types.
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
ShiftLike_match< LHS, Instruction::LShr > m_LShrOrSelf(const LHS &L, uint64_t &R)
Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, true > m_c_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, CastInst >, OpTy > m_CastOrSelf(const OpTy &Op)
Matches any cast or self. Used to ignore casts.
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
CommutativeBinaryIntrinsic_match< IntrID, T0, T1 > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
AllowReassoc_match< T > m_AllowReassoc(const T &SubPattern)
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
ap_match< APFloat > m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_c_XorLike(const LHS &L, const RHS &R)
Match either (xor L, R), (xor R, L) or (sub nuw R, L) iff R.isMask() Only commutative matcher as the ...
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
ap_match< APFloat > m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R, either one being in the either binary or logical form.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > >, OpTy > m_ZExtOrSExtOrSelf(const OpTy &Op)
OneUse_match< T > m_OneUse(const T &SubPattern)
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
Splat_match< T > m_ConstantSplat(const T &SubPattern)
Match a constant splat. TODO: Extend this to non-constant splats.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
ap_match< APInt > m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
cst_pred_ty< is_non_zero_int > m_NonZeroInt()
Match a non-zero integer or a vector with all non-zero elements.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
class_match< ConstantFP > m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_not_denormal_fp > m_NonZeroNotDenormalFP()
Match a floating-point non-zero that is not a denormal.
cst_pred_ty< is_all_ones, false > m_AllOnesForbidPoison()
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
class_match< UndefValue > m_UndefValue()
Match an arbitrary UndefValue constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
ShiftLike_match< LHS, Instruction::AShr > m_AShrOrSelf(const LHS &L, uint64_t &R)
Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedGather Intrinsic.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
ThreeOps_match< Cond, constantint_match< L >, constantint_match< R >, Instruction::Select > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.: m_SelectCst<-1, 0>(m_Value(V))
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
SpecificCmpClass_match< LHS, RHS, ICmpInst, true > m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, TruncInst > >, OpTy > m_ZExtOrTruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
m_Intrinsic_Ty< Opnd >::Ty m_Deinterleave2(const Opnd &Op)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
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.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
AllowReassoc_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3, T4 >::Ty, Argument_match< T5 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3 >::Ty, Argument_match< T4 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
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.
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
match_combine_and(const LTy &Left, const RTy &Right)
match_combine_or(const LTy &Left, const RTy &Right)
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying signed max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying signed min predicates.
static bool match(ICmpInst::Predicate Pred)
Match a specified basic block value.
Match a specified floating point value or vector of all elements of that value.
Match a specified integer value or vector of all elements of that value.
Match a specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)