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 if (ConstInt->getActiveBits() > 64)
1049 return false;
1050 VR = ConstInt->getZExtValue();
1051 return true;
1052 }
1053};
1054
1055/// Match a specified integer value or vector of all elements of that
1056/// value.
1057template <bool AllowPoison> struct specific_intval {
1058 const APInt &Val;
1059
1060 specific_intval(const APInt &V) : Val(V) {}
1061
1062 template <typename ITy> bool match(ITy *V) const {
1063 const auto *CI = dyn_cast<ConstantInt>(V);
1064 if (!CI && V->getType()->isVectorTy())
1065 if (const auto *C = dyn_cast<Constant>(V))
1066 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1067
1068 return CI && APInt::isSameValue(CI->getValue(), Val);
1069 }
1070};
1071
1072template <bool AllowPoison> struct specific_intval64 {
1074
1076
1077 template <typename ITy> bool match(ITy *V) const {
1078 const auto *CI = dyn_cast<ConstantInt>(V);
1079 if (!CI && V->getType()->isVectorTy())
1080 if (const auto *C = dyn_cast<Constant>(V))
1081 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1082
1083 return CI && CI->getValue() == Val;
1084 }
1085};
1086
1087/// Match a specific integer value or vector with all elements equal to
1088/// the value.
1090 return specific_intval<false>(V);
1091}
1092
1096
1100
1104
1105/// Match a ConstantInt and bind to its value. This does not match
1106/// ConstantInts wider than 64-bits.
1108
1109/// Match a specified basic block value.
1112
1114
1115 template <typename ITy> bool match(ITy *V) const {
1116 const auto *BB = dyn_cast<BasicBlock>(V);
1117 return BB && BB == Val;
1118 }
1119};
1120
1121/// Match a specific basic block value.
1123 return specific_bbval(BB);
1124}
1125
1126/// A commutative-friendly version of m_Specific().
1128 return BB;
1129}
1131m_Deferred(const BasicBlock *const &BB) {
1132 return BB;
1133}
1134
1135//===----------------------------------------------------------------------===//
1136// Matcher for any binary operator.
1137//
1138template <typename LHS_t, typename RHS_t, bool Commutable = false>
1142
1143 // The evaluation order is always stable, regardless of Commutability.
1144 // The LHS is always matched first.
1145 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1146
1147 template <typename OpTy> bool match(OpTy *V) const {
1148 if (auto *I = dyn_cast<BinaryOperator>(V))
1149 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1150 (Commutable && L.match(I->getOperand(1)) &&
1151 R.match(I->getOperand(0)));
1152 return false;
1153 }
1154};
1155
1156template <typename LHS, typename RHS>
1157inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1158 return AnyBinaryOp_match<LHS, RHS>(L, R);
1159}
1160
1161//===----------------------------------------------------------------------===//
1162// Matcher for any unary operator.
1163// TODO fuse unary, binary matcher into n-ary matcher
1164//
1165template <typename OP_t> struct AnyUnaryOp_match {
1166 OP_t X;
1167
1168 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1169
1170 template <typename OpTy> bool match(OpTy *V) const {
1171 if (auto *I = dyn_cast<UnaryOperator>(V))
1172 return X.match(I->getOperand(0));
1173 return false;
1174 }
1175};
1176
1177template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1178 return AnyUnaryOp_match<OP_t>(X);
1179}
1180
1181//===----------------------------------------------------------------------===//
1182// Matchers for specific binary operators.
1183//
1184
1185template <typename LHS_t, typename RHS_t, unsigned Opcode,
1186 bool Commutable = false>
1190
1191 // The evaluation order is always stable, regardless of Commutability.
1192 // The LHS is always matched first.
1193 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1194
1195 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1196 if (V->getValueID() == Value::InstructionVal + Opc) {
1197 auto *I = cast<BinaryOperator>(V);
1198 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1199 (Commutable && L.match(I->getOperand(1)) &&
1200 R.match(I->getOperand(0)));
1201 }
1202 return false;
1203 }
1204
1205 template <typename OpTy> bool match(OpTy *V) const {
1206 return match(Opcode, V);
1207 }
1208};
1209
1210template <typename LHS, typename RHS>
1215
1216template <typename LHS, typename RHS>
1221
1222template <typename LHS, typename RHS>
1227
1228template <typename LHS, typename RHS>
1233
1234template <typename Op_t> struct FNeg_match {
1235 Op_t X;
1236
1237 FNeg_match(const Op_t &Op) : X(Op) {}
1238 template <typename OpTy> bool match(OpTy *V) const {
1239 auto *FPMO = dyn_cast<FPMathOperator>(V);
1240 if (!FPMO)
1241 return false;
1242
1243 if (FPMO->getOpcode() == Instruction::FNeg)
1244 return X.match(FPMO->getOperand(0));
1245
1246 if (FPMO->getOpcode() == Instruction::FSub) {
1247 if (FPMO->hasNoSignedZeros()) {
1248 // With 'nsz', any zero goes.
1249 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1250 return false;
1251 } else {
1252 // Without 'nsz', we need fsub -0.0, X exactly.
1253 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1254 return false;
1255 }
1256
1257 return X.match(FPMO->getOperand(1));
1258 }
1259
1260 return false;
1261 }
1262};
1263
1264/// Match 'fneg X' as 'fsub -0.0, X'.
1265template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1266 return FNeg_match<OpTy>(X);
1267}
1268
1269/// Match 'fneg X' as 'fsub +-0.0, X'.
1270template <typename RHS>
1271inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1272m_FNegNSZ(const RHS &X) {
1273 return m_FSub(m_AnyZeroFP(), X);
1274}
1275
1276template <typename LHS, typename RHS>
1281
1282template <typename LHS, typename RHS>
1287
1288template <typename LHS, typename RHS>
1293
1294template <typename LHS, typename RHS>
1299
1300template <typename LHS, typename RHS>
1305
1306template <typename LHS, typename RHS>
1311
1312template <typename LHS, typename RHS>
1317
1318template <typename LHS, typename RHS>
1323
1324template <typename LHS, typename RHS>
1329
1330template <typename LHS, typename RHS>
1335
1336template <typename LHS, typename RHS>
1341
1342template <typename LHS, typename RHS>
1347
1348template <typename LHS, typename RHS>
1353
1354template <typename LHS, typename RHS>
1359
1360template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1363
1365
1366 template <typename OpTy> bool match(OpTy *V) const {
1367 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1368 if (Op->getOpcode() == Opcode)
1369 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1370 L.match(Op->getOperand(0));
1371 }
1372 // Interpreted as shiftop V, 0
1373 R = 0;
1374 return L.match(V);
1375 }
1376};
1377
1378/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1379template <typename LHS>
1384
1385/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1386template <typename LHS>
1391
1392/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1393template <typename LHS>
1398
1399template <typename LHS_t, typename RHS_t, unsigned Opcode,
1400 unsigned WrapFlags = 0, bool Commutable = false>
1404
1406 : L(LHS), R(RHS) {}
1407
1408 template <typename OpTy> bool match(OpTy *V) const {
1409 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1410 if (Op->getOpcode() != Opcode)
1411 return false;
1413 !Op->hasNoUnsignedWrap())
1414 return false;
1415 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1416 !Op->hasNoSignedWrap())
1417 return false;
1418 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1419 (Commutable && L.match(Op->getOperand(1)) &&
1420 R.match(Op->getOperand(0)));
1421 }
1422 return false;
1423 }
1424};
1425
1426template <typename LHS, typename RHS>
1427inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1429m_NSWAdd(const LHS &L, const RHS &R) {
1430 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1432 R);
1433}
1434template <typename LHS, typename RHS>
1435inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1437m_c_NSWAdd(const LHS &L, const RHS &R) {
1438 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1440 true>(L, R);
1441}
1442template <typename LHS, typename RHS>
1443inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1445m_NSWSub(const LHS &L, const RHS &R) {
1446 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1448 R);
1449}
1450template <typename LHS, typename RHS>
1451inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1453m_NSWMul(const LHS &L, const RHS &R) {
1454 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1456 R);
1457}
1458template <typename LHS, typename RHS>
1459inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1461m_NSWShl(const LHS &L, const RHS &R) {
1462 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1464 R);
1465}
1466
1467template <typename LHS, typename RHS>
1468inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1470m_NUWAdd(const LHS &L, const RHS &R) {
1471 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1473 L, R);
1474}
1475
1476template <typename LHS, typename RHS>
1478 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1479m_c_NUWAdd(const LHS &L, const RHS &R) {
1480 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1482 true>(L, R);
1483}
1484
1485template <typename LHS, typename RHS>
1486inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1488m_NUWSub(const LHS &L, const RHS &R) {
1489 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1491 L, R);
1492}
1493template <typename LHS, typename RHS>
1494inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1496m_NUWMul(const LHS &L, const RHS &R) {
1497 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1499 L, R);
1500}
1501template <typename LHS, typename RHS>
1502inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1504m_NUWShl(const LHS &L, const RHS &R) {
1505 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1507 L, R);
1508}
1509
1510template <typename LHS_t, typename RHS_t, bool Commutable = false>
1512 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1513 unsigned Opcode;
1514
1516 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1517
1518 template <typename OpTy> bool match(OpTy *V) const {
1520 }
1521};
1522
1523/// Matches a specific opcode.
1524template <typename LHS, typename RHS>
1525inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1526 const RHS &R) {
1527 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1528}
1529
1530template <typename LHS, typename RHS, bool Commutable = false>
1534
1535 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1536
1537 template <typename OpTy> bool match(OpTy *V) const {
1538 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1539 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1540 if (!PDI->isDisjoint())
1541 return false;
1542 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1543 (Commutable && L.match(PDI->getOperand(1)) &&
1544 R.match(PDI->getOperand(0)));
1545 }
1546 return false;
1547 }
1548};
1549
1550template <typename LHS, typename RHS>
1552 return DisjointOr_match<LHS, RHS>(L, R);
1553}
1554
1555template <typename LHS, typename RHS>
1557 const RHS &R) {
1559}
1560
1561/// Match either "add" or "or disjoint".
1562template <typename LHS, typename RHS>
1565m_AddLike(const LHS &L, const RHS &R) {
1566 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1567}
1568
1569/// Match either "add nsw" or "or disjoint"
1570template <typename LHS, typename RHS>
1571inline match_combine_or<
1572 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1575m_NSWAddLike(const LHS &L, const RHS &R) {
1576 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1577}
1578
1579/// Match either "add nuw" or "or disjoint"
1580template <typename LHS, typename RHS>
1581inline match_combine_or<
1582 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1585m_NUWAddLike(const LHS &L, const RHS &R) {
1586 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1587}
1588
1589template <typename LHS, typename RHS>
1593
1594 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1595
1596 template <typename OpTy> bool match(OpTy *V) const {
1597 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1598 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1599 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1600 ; // Pass
1601 else if (Op->getOpcode() != Instruction::Xor)
1602 return false;
1603 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1604 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1605 }
1606 return false;
1607 }
1608};
1609
1610/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1611/// Only commutative matcher as the `sub` will need to swap the L and R.
1612template <typename LHS, typename RHS>
1613inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1614 return XorLike_match<LHS, RHS>(L, R);
1615}
1616
1617//===----------------------------------------------------------------------===//
1618// Class that matches a group of binary opcodes.
1619//
1620template <typename LHS_t, typename RHS_t, typename Predicate,
1621 bool Commutable = false>
1622struct BinOpPred_match : Predicate {
1625
1626 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1627
1628 template <typename OpTy> bool match(OpTy *V) const {
1629 if (auto *I = dyn_cast<Instruction>(V))
1630 return this->isOpType(I->getOpcode()) &&
1631 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1632 (Commutable && L.match(I->getOperand(1)) &&
1633 R.match(I->getOperand(0))));
1634 return false;
1635 }
1636};
1637
1639 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1640};
1641
1643 bool isOpType(unsigned Opcode) const {
1644 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1645 }
1646};
1647
1649 bool isOpType(unsigned Opcode) const {
1650 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1651 }
1652};
1653
1655 bool isOpType(unsigned Opcode) const {
1656 return Instruction::isBitwiseLogicOp(Opcode);
1657 }
1658};
1659
1661 bool isOpType(unsigned Opcode) const {
1662 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1663 }
1664};
1665
1667 bool isOpType(unsigned Opcode) const {
1668 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1669 }
1670};
1671
1672/// Matches shift operations.
1673template <typename LHS, typename RHS>
1675 const RHS &R) {
1677}
1678
1679/// Matches logical shift operations.
1680template <typename LHS, typename RHS>
1685
1686/// Matches logical shift operations.
1687template <typename LHS, typename RHS>
1689m_LogicalShift(const LHS &L, const RHS &R) {
1691}
1692
1693/// Matches bitwise logic operations.
1694template <typename LHS, typename RHS>
1696m_BitwiseLogic(const LHS &L, const RHS &R) {
1698}
1699
1700/// Matches bitwise logic operations in either order.
1701template <typename LHS, typename RHS>
1706
1707/// Matches integer division operations.
1708template <typename LHS, typename RHS>
1710 const RHS &R) {
1712}
1713
1714/// Matches integer remainder operations.
1715template <typename LHS, typename RHS>
1717 const RHS &R) {
1719}
1720
1721//===----------------------------------------------------------------------===//
1722// Class that matches exact binary ops.
1723//
1724template <typename SubPattern_t> struct Exact_match {
1725 SubPattern_t SubPattern;
1726
1727 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1728
1729 template <typename OpTy> bool match(OpTy *V) const {
1730 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1731 return PEO->isExact() && SubPattern.match(V);
1732 return false;
1733 }
1734};
1735
1736template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1737 return SubPattern;
1738}
1739
1740//===----------------------------------------------------------------------===//
1741// Matchers for CmpInst classes
1742//
1743
1744template <typename LHS_t, typename RHS_t, typename Class,
1745 bool Commutable = false>
1750
1751 // The evaluation order is always stable, regardless of Commutability.
1752 // The LHS is always matched first.
1754 : Predicate(&Pred), L(LHS), R(RHS) {}
1756 : Predicate(nullptr), L(LHS), R(RHS) {}
1757
1758 template <typename OpTy> bool match(OpTy *V) const {
1759 if (auto *I = dyn_cast<Class>(V)) {
1760 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1761 if (Predicate)
1763 return true;
1764 }
1765 if (Commutable && L.match(I->getOperand(1)) &&
1766 R.match(I->getOperand(0))) {
1767 if (Predicate)
1769 return true;
1770 }
1771 }
1772 return false;
1773 }
1774};
1775
1776template <typename LHS, typename RHS>
1778 const RHS &R) {
1779 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1780}
1781
1782template <typename LHS, typename RHS>
1784 const LHS &L, const RHS &R) {
1785 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1786}
1787
1788template <typename LHS, typename RHS>
1790 const LHS &L, const RHS &R) {
1791 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1792}
1793
1794template <typename LHS, typename RHS>
1797}
1798
1799template <typename LHS, typename RHS>
1802}
1803
1804template <typename LHS, typename RHS>
1807}
1808
1809// Same as CmpClass, but instead of saving Pred as out output variable, match a
1810// specific input pred for equality.
1811template <typename LHS_t, typename RHS_t, typename Class,
1812 bool Commutable = false>
1817
1819 : Predicate(Pred), L(LHS), R(RHS) {}
1820
1821 template <typename OpTy> bool match(OpTy *V) const {
1822 if (auto *I = dyn_cast<Class>(V)) {
1824 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1825 return true;
1826 if constexpr (Commutable) {
1829 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1830 return true;
1831 }
1832 }
1833
1834 return false;
1835 }
1836};
1837
1838template <typename LHS, typename RHS>
1840m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1841 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1842}
1843
1844template <typename LHS, typename RHS>
1846m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1847 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1848}
1849
1850template <typename LHS, typename RHS>
1852m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1854}
1855
1856template <typename LHS, typename RHS>
1858m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1859 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1860}
1861
1862//===----------------------------------------------------------------------===//
1863// Matchers for instructions with a given opcode and number of operands.
1864//
1865
1866/// Matches instructions with Opcode and three operands.
1867template <typename T0, unsigned Opcode> struct OneOps_match {
1869
1870 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1871
1872 template <typename OpTy> bool match(OpTy *V) const {
1873 if (V->getValueID() == Value::InstructionVal + Opcode) {
1874 auto *I = cast<Instruction>(V);
1875 return Op1.match(I->getOperand(0));
1876 }
1877 return false;
1878 }
1879};
1880
1881/// Matches instructions with Opcode and three operands.
1882template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1885
1886 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1887
1888 template <typename OpTy> bool match(OpTy *V) const {
1889 if (V->getValueID() == Value::InstructionVal + Opcode) {
1890 auto *I = cast<Instruction>(V);
1891 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1892 }
1893 return false;
1894 }
1895};
1896
1897/// Matches instructions with Opcode and three operands.
1898template <typename T0, typename T1, typename T2, unsigned Opcode,
1899 bool CommutableOp2Op3 = false>
1904
1905 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1906 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1907
1908 template <typename OpTy> bool match(OpTy *V) const {
1909 if (V->getValueID() == Value::InstructionVal + Opcode) {
1910 auto *I = cast<Instruction>(V);
1911 if (!Op1.match(I->getOperand(0)))
1912 return false;
1913 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1914 return true;
1915 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1916 Op3.match(I->getOperand(1));
1917 }
1918 return false;
1919 }
1920};
1921
1922/// Matches instructions with Opcode and any number of operands
1923template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1924 std::tuple<OperandTypes...> Operands;
1925
1926 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1927
1928 // Operand matching works by recursively calling match_operands, matching the
1929 // operands left to right. The first version is called for each operand but
1930 // the last, for which the second version is called. The second version of
1931 // match_operands is also used to match each individual operand.
1932 template <int Idx, int Last>
1933 std::enable_if_t<Idx != Last, bool>
1937
1938 template <int Idx, int Last>
1939 std::enable_if_t<Idx == Last, bool>
1941 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1942 }
1943
1944 template <typename OpTy> bool match(OpTy *V) const {
1945 if (V->getValueID() == Value::InstructionVal + Opcode) {
1946 auto *I = cast<Instruction>(V);
1947 return I->getNumOperands() == sizeof...(OperandTypes) &&
1948 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1949 }
1950 return false;
1951 }
1952};
1953
1954/// Matches SelectInst.
1955template <typename Cond, typename LHS, typename RHS>
1957m_Select(const Cond &C, const LHS &L, const RHS &R) {
1959}
1960
1961/// This matches a select of two constants, e.g.:
1962/// m_SelectCst<-1, 0>(m_Value(V))
1963template <int64_t L, int64_t R, typename Cond>
1965 Instruction::Select>
1968}
1969
1970/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1971template <typename LHS, typename RHS>
1972inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1973m_c_Select(const LHS &L, const RHS &R) {
1974 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1975 true>(m_Value(), L, R);
1976}
1977
1978/// Matches FreezeInst.
1979template <typename OpTy>
1983
1984/// Matches InsertElementInst.
1985template <typename Val_t, typename Elt_t, typename Idx_t>
1987m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1989 Val, Elt, Idx);
1990}
1991
1992/// Matches ExtractElementInst.
1993template <typename Val_t, typename Idx_t>
1995m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1997}
1998
1999/// Matches shuffle.
2000template <typename T0, typename T1, typename T2> struct Shuffle_match {
2004
2005 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
2006 : Op1(Op1), Op2(Op2), Mask(Mask) {}
2007
2008 template <typename OpTy> bool match(OpTy *V) const {
2009 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
2010 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
2011 Mask.match(I->getShuffleMask());
2012 }
2013 return false;
2014 }
2015};
2016
2017struct m_Mask {
2020 bool match(ArrayRef<int> Mask) const {
2021 MaskRef = Mask;
2022 return true;
2023 }
2024};
2025
2027 bool match(ArrayRef<int> Mask) const {
2028 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2029 }
2030};
2031
2035 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2036};
2037
2041 bool match(ArrayRef<int> Mask) const {
2042 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2043 if (First == Mask.end())
2044 return false;
2045 SplatIndex = *First;
2046 return all_of(Mask,
2047 [First](int Elem) { return Elem == *First || Elem == -1; });
2048 }
2049};
2050
2051template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2052 PointerOpTy PointerOp;
2053 OffsetOpTy OffsetOp;
2054
2055 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2057
2058 template <typename OpTy> bool match(OpTy *V) const {
2059 auto *GEP = dyn_cast<GEPOperator>(V);
2060 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2061 PointerOp.match(GEP->getPointerOperand()) &&
2062 OffsetOp.match(GEP->idx_begin()->get());
2063 }
2064};
2065
2066/// Matches ShuffleVectorInst independently of mask value.
2067template <typename V1_t, typename V2_t>
2069m_Shuffle(const V1_t &v1, const V2_t &v2) {
2071}
2072
2073template <typename V1_t, typename V2_t, typename Mask_t>
2075m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2077}
2078
2079/// Matches LoadInst.
2080template <typename OpTy>
2084
2085/// Matches StoreInst.
2086template <typename ValueOpTy, typename PointerOpTy>
2088m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2090 PointerOp);
2091}
2092
2093/// Matches GetElementPtrInst.
2094template <typename... OperandTypes>
2095inline auto m_GEP(const OperandTypes &...Ops) {
2096 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2097}
2098
2099/// Matches GEP with i8 source element type
2100template <typename PointerOpTy, typename OffsetOpTy>
2102m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2104}
2105
2106//===----------------------------------------------------------------------===//
2107// Matchers for CastInst classes
2108//
2109
2110template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2111 Op_t Op;
2112
2113 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2114
2115 template <typename OpTy> bool match(OpTy *V) const {
2116 if (auto *O = dyn_cast<Operator>(V))
2117 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2118 return false;
2119 }
2120};
2121
2122template <typename Op_t, typename Class> struct CastInst_match {
2123 Op_t Op;
2124
2125 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2126
2127 template <typename OpTy> bool match(OpTy *V) const {
2128 if (auto *I = dyn_cast<Class>(V))
2129 return Op.match(I->getOperand(0));
2130 return false;
2131 }
2132};
2133
2134template <typename Op_t> struct PtrToIntSameSize_match {
2136 Op_t Op;
2137
2138 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2139 : DL(DL), Op(OpMatch) {}
2140
2141 template <typename OpTy> bool match(OpTy *V) const {
2142 if (auto *O = dyn_cast<Operator>(V))
2143 return O->getOpcode() == Instruction::PtrToInt &&
2144 DL.getTypeSizeInBits(O->getType()) ==
2145 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2146 Op.match(O->getOperand(0));
2147 return false;
2148 }
2149};
2150
2151template <typename Op_t> struct NNegZExt_match {
2152 Op_t Op;
2153
2154 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2155
2156 template <typename OpTy> bool match(OpTy *V) const {
2157 if (auto *I = dyn_cast<ZExtInst>(V))
2158 return I->hasNonNeg() && Op.match(I->getOperand(0));
2159 return false;
2160 }
2161};
2162
2163template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2164 Op_t Op;
2165
2166 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2167
2168 template <typename OpTy> bool match(OpTy *V) const {
2169 if (auto *I = dyn_cast<TruncInst>(V))
2170 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2171 Op.match(I->getOperand(0));
2172 return false;
2173 }
2174};
2175
2176/// Matches BitCast.
2177template <typename OpTy>
2182
2183template <typename Op_t> struct ElementWiseBitCast_match {
2184 Op_t Op;
2185
2186 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2187
2188 template <typename OpTy> bool match(OpTy *V) const {
2189 auto *I = dyn_cast<BitCastInst>(V);
2190 if (!I)
2191 return false;
2192 Type *SrcType = I->getSrcTy();
2193 Type *DstType = I->getType();
2194 // Make sure the bitcast doesn't change between scalar and vector and
2195 // doesn't change the number of vector elements.
2196 if (SrcType->isVectorTy() != DstType->isVectorTy())
2197 return false;
2198 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2199 SrcVecTy && SrcVecTy->getElementCount() !=
2200 cast<VectorType>(DstType)->getElementCount())
2201 return false;
2202 return Op.match(I->getOperand(0));
2203 }
2204};
2205
2206template <typename OpTy>
2210
2211/// Matches PtrToInt.
2212template <typename OpTy>
2217
2218template <typename OpTy>
2223
2224/// Matches PtrToAddr.
2225template <typename OpTy>
2230
2231/// Matches PtrToInt or PtrToAddr.
2232template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2234}
2235
2236/// Matches IntToPtr.
2237template <typename OpTy>
2242
2243/// Matches any cast or self. Used to ignore casts.
2244template <typename OpTy>
2246m_CastOrSelf(const OpTy &Op) {
2248}
2249
2250/// Matches Trunc.
2251template <typename OpTy>
2255
2256/// Matches trunc nuw.
2257template <typename OpTy>
2262
2263/// Matches trunc nsw.
2264template <typename OpTy>
2269
2270template <typename OpTy>
2272m_TruncOrSelf(const OpTy &Op) {
2273 return m_CombineOr(m_Trunc(Op), Op);
2274}
2275
2276/// Matches SExt.
2277template <typename OpTy>
2281
2282/// Matches ZExt.
2283template <typename OpTy>
2287
2288template <typename OpTy>
2290 return NNegZExt_match<OpTy>(Op);
2291}
2292
2293template <typename OpTy>
2295m_ZExtOrSelf(const OpTy &Op) {
2296 return m_CombineOr(m_ZExt(Op), Op);
2297}
2298
2299template <typename OpTy>
2301m_SExtOrSelf(const OpTy &Op) {
2302 return m_CombineOr(m_SExt(Op), Op);
2303}
2304
2305/// Match either "sext" or "zext nneg".
2306template <typename OpTy>
2308m_SExtLike(const OpTy &Op) {
2309 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2310}
2311
2312template <typename OpTy>
2315m_ZExtOrSExt(const OpTy &Op) {
2316 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2317}
2318
2319template <typename OpTy>
2322 OpTy>
2324 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2325}
2326
2327template <typename OpTy>
2330 OpTy>
2333}
2334
2335template <typename OpTy>
2339
2340template <typename OpTy>
2344
2345template <typename OpTy>
2349
2350template <typename OpTy>
2354
2355template <typename OpTy>
2359
2360template <typename OpTy>
2364
2365//===----------------------------------------------------------------------===//
2366// Matchers for control flow.
2367//
2368
2369struct br_match {
2371
2373
2374 template <typename OpTy> bool match(OpTy *V) const {
2375 if (auto *BI = dyn_cast<BranchInst>(V))
2376 if (BI->isUnconditional()) {
2377 Succ = BI->getSuccessor(0);
2378 return true;
2379 }
2380 return false;
2381 }
2382};
2383
2384inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2385
2386template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2388 Cond_t Cond;
2389 TrueBlock_t T;
2390 FalseBlock_t F;
2391
2392 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2393 : Cond(C), T(t), F(f) {}
2394
2395 template <typename OpTy> bool match(OpTy *V) const {
2396 if (auto *BI = dyn_cast<BranchInst>(V))
2397 if (BI->isConditional() && Cond.match(BI->getCondition()))
2398 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2399 return false;
2400 }
2401};
2402
2403template <typename Cond_t>
2409
2410template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2412m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2414}
2415
2416//===----------------------------------------------------------------------===//
2417// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2418//
2419
2420template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2421 bool Commutable = false>
2423 using PredType = Pred_t;
2426
2427 // The evaluation order is always stable, regardless of Commutability.
2428 // The LHS is always matched first.
2429 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2430
2431 template <typename OpTy> bool match(OpTy *V) const {
2432 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2433 Intrinsic::ID IID = II->getIntrinsicID();
2434 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2435 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2436 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2437 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2438 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2439 return (L.match(LHS) && R.match(RHS)) ||
2440 (Commutable && L.match(RHS) && R.match(LHS));
2441 }
2442 }
2443 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2444 auto *SI = dyn_cast<SelectInst>(V);
2445 if (!SI)
2446 return false;
2447 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2448 if (!Cmp)
2449 return false;
2450 // At this point we have a select conditioned on a comparison. Check that
2451 // it is the values returned by the select that are being compared.
2452 auto *TrueVal = SI->getTrueValue();
2453 auto *FalseVal = SI->getFalseValue();
2454 auto *LHS = Cmp->getOperand(0);
2455 auto *RHS = Cmp->getOperand(1);
2456 if ((TrueVal != LHS || FalseVal != RHS) &&
2457 (TrueVal != RHS || FalseVal != LHS))
2458 return false;
2459 typename CmpInst_t::Predicate Pred =
2460 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2461 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2462 if (!Pred_t::match(Pred))
2463 return false;
2464 // It does! Bind the operands.
2465 return (L.match(LHS) && R.match(RHS)) ||
2466 (Commutable && L.match(RHS) && R.match(LHS));
2467 }
2468};
2469
2470/// Helper class for identifying signed max predicates.
2472 static bool match(ICmpInst::Predicate Pred) {
2473 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2474 }
2475};
2476
2477/// Helper class for identifying signed min predicates.
2479 static bool match(ICmpInst::Predicate Pred) {
2480 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2481 }
2482};
2483
2484/// Helper class for identifying unsigned max predicates.
2486 static bool match(ICmpInst::Predicate Pred) {
2487 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2488 }
2489};
2490
2491/// Helper class for identifying unsigned min predicates.
2493 static bool match(ICmpInst::Predicate Pred) {
2494 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2495 }
2496};
2497
2498/// Helper class for identifying ordered max predicates.
2500 static bool match(FCmpInst::Predicate Pred) {
2501 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2502 }
2503};
2504
2505/// Helper class for identifying ordered min predicates.
2507 static bool match(FCmpInst::Predicate Pred) {
2508 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2509 }
2510};
2511
2512/// Helper class for identifying unordered max predicates.
2514 static bool match(FCmpInst::Predicate Pred) {
2515 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2516 }
2517};
2518
2519/// Helper class for identifying unordered min predicates.
2521 static bool match(FCmpInst::Predicate Pred) {
2522 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2523 }
2524};
2525
2526template <typename LHS, typename RHS>
2531
2532template <typename LHS, typename RHS>
2537
2538template <typename LHS, typename RHS>
2543
2544template <typename LHS, typename RHS>
2549
2550template <typename LHS, typename RHS>
2551inline match_combine_or<
2556m_MaxOrMin(const LHS &L, const RHS &R) {
2557 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2558 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2559}
2560
2561/// Match an 'ordered' floating point maximum function.
2562/// Floating point has one special value 'NaN'. Therefore, there is no total
2563/// order. However, if we can ignore the 'NaN' value (for example, because of a
2564/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2565/// semantics. In the presence of 'NaN' we have to preserve the original
2566/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2567///
2568/// max(L, R) iff L and R are not NaN
2569/// m_OrdFMax(L, R) = R iff L or R are NaN
2570template <typename LHS, typename RHS>
2575
2576/// Match an 'ordered' floating point minimum function.
2577/// Floating point has one special value 'NaN'. Therefore, there is no total
2578/// order. However, if we can ignore the 'NaN' value (for example, because of a
2579/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2580/// semantics. In the presence of 'NaN' we have to preserve the original
2581/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2582///
2583/// min(L, R) iff L and R are not NaN
2584/// m_OrdFMin(L, R) = R iff L or R are NaN
2585template <typename LHS, typename RHS>
2590
2591/// Match an 'unordered' floating point maximum function.
2592/// Floating point has one special value 'NaN'. Therefore, there is no total
2593/// order. However, if we can ignore the 'NaN' value (for example, because of a
2594/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2595/// semantics. In the presence of 'NaN' we have to preserve the original
2596/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2597///
2598/// max(L, R) iff L and R are not NaN
2599/// m_UnordFMax(L, R) = L iff L or R are NaN
2600template <typename LHS, typename RHS>
2602m_UnordFMax(const LHS &L, const RHS &R) {
2604}
2605
2606/// Match an 'unordered' floating point minimum function.
2607/// Floating point has one special value 'NaN'. Therefore, there is no total
2608/// order. However, if we can ignore the 'NaN' value (for example, because of a
2609/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2610/// semantics. In the presence of 'NaN' we have to preserve the original
2611/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2612///
2613/// min(L, R) iff L and R are not NaN
2614/// m_UnordFMin(L, R) = L iff L or R are NaN
2615template <typename LHS, typename RHS>
2617m_UnordFMin(const LHS &L, const RHS &R) {
2619}
2620
2621/// Match an 'ordered' or 'unordered' floating point maximum function.
2622/// Floating point has one special value 'NaN'. Therefore, there is no total
2623/// order. However, if we can ignore the 'NaN' value (for example, because of a
2624/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2625/// semantics.
2626template <typename LHS, typename RHS>
2633
2634/// Match an 'ordered' or 'unordered' floating point minimum function.
2635/// Floating point has one special value 'NaN'. Therefore, there is no total
2636/// order. However, if we can ignore the 'NaN' value (for example, because of a
2637/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2638/// semantics.
2639template <typename LHS, typename RHS>
2646
2647/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2648/// NOTE: we first match the 'Not' (by matching '-1'),
2649/// and only then match the inner matcher!
2650template <typename ValTy>
2651inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2652m_Not(const ValTy &V) {
2653 return m_c_Xor(m_AllOnes(), V);
2654}
2655
2656template <typename ValTy>
2657inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2658 true>
2659m_NotForbidPoison(const ValTy &V) {
2660 return m_c_Xor(m_AllOnesForbidPoison(), V);
2661}
2662
2663//===----------------------------------------------------------------------===//
2664// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2665// Note that S might be matched to other instructions than AddInst.
2666//
2667
2668template <typename LHS_t, typename RHS_t, typename Sum_t>
2672 Sum_t S;
2673
2674 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2675 : L(L), R(R), S(S) {}
2676
2677 template <typename OpTy> bool match(OpTy *V) const {
2678 Value *ICmpLHS, *ICmpRHS;
2679 CmpPredicate Pred;
2680 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2681 return false;
2682
2683 Value *AddLHS, *AddRHS;
2684 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2685
2686 // (a + b) u< a, (a + b) u< b
2687 if (Pred == ICmpInst::ICMP_ULT)
2688 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2689 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2690
2691 // a >u (a + b), b >u (a + b)
2692 if (Pred == ICmpInst::ICMP_UGT)
2693 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2694 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2695
2696 Value *Op1;
2697 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2698 // (~a) <u b
2699 if (Pred == ICmpInst::ICMP_ULT) {
2700 if (XorExpr.match(ICmpLHS))
2701 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2702 }
2703 // b > u (~a)
2704 if (Pred == ICmpInst::ICMP_UGT) {
2705 if (XorExpr.match(ICmpRHS))
2706 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2707 }
2708
2709 // Match special-case for increment-by-1.
2710 if (Pred == ICmpInst::ICMP_EQ) {
2711 // (a + 1) == 0
2712 // (1 + a) == 0
2713 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2714 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2715 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2716 // 0 == (a + 1)
2717 // 0 == (1 + a)
2718 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2719 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2720 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2721 }
2722
2723 return false;
2724 }
2725};
2726
2727/// Match an icmp instruction checking for unsigned overflow on addition.
2728///
2729/// S is matched to the addition whose result is being checked for overflow, and
2730/// L and R are matched to the LHS and RHS of S.
2731template <typename LHS_t, typename RHS_t, typename Sum_t>
2733m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2735}
2736
2737template <typename Opnd_t> struct Argument_match {
2738 unsigned OpI;
2739 Opnd_t Val;
2740
2741 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2742
2743 template <typename OpTy> bool match(OpTy *V) const {
2744 // FIXME: Should likely be switched to use `CallBase`.
2745 if (const auto *CI = dyn_cast<CallInst>(V))
2746 return Val.match(CI->getArgOperand(OpI));
2747 return false;
2748 }
2749};
2750
2751/// Match an argument.
2752template <unsigned OpI, typename Opnd_t>
2753inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2754 return Argument_match<Opnd_t>(OpI, Op);
2755}
2756
2757/// Intrinsic matchers.
2759 unsigned ID;
2760
2762
2763 template <typename OpTy> bool match(OpTy *V) const {
2764 if (const auto *CI = dyn_cast<CallInst>(V))
2765 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2766 return F->getIntrinsicID() == ID;
2767 return false;
2768 }
2769};
2770
2771/// Intrinsic matches are combinations of ID matchers, and argument
2772/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2773/// them with lower arity matchers. Here's some convenient typedefs for up to
2774/// several arguments, and more can be added as needed
2775template <typename T0 = void, typename T1 = void, typename T2 = void,
2776 typename T3 = void, typename T4 = void, typename T5 = void,
2777 typename T6 = void, typename T7 = void, typename T8 = void,
2778 typename T9 = void, typename T10 = void>
2780template <typename T0> struct m_Intrinsic_Ty<T0> {
2782};
2783template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2784 using Ty =
2786};
2787template <typename T0, typename T1, typename T2>
2792template <typename T0, typename T1, typename T2, typename T3>
2797
2798template <typename T0, typename T1, typename T2, typename T3, typename T4>
2803
2804template <typename T0, typename T1, typename T2, typename T3, typename T4,
2805 typename T5>
2810
2811/// Match intrinsic calls like this:
2812/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2813template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2814 return IntrinsicID_match(IntrID);
2815}
2816
2817/// Matches MaskedLoad Intrinsic.
2818template <typename Opnd0, typename Opnd1, typename Opnd2>
2820m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2821 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2822}
2823
2824/// Matches MaskedStore Intrinsic.
2825template <typename Opnd0, typename Opnd1, typename Opnd2>
2827m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2828 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2829}
2830
2831/// Matches MaskedGather Intrinsic.
2832template <typename Opnd0, typename Opnd1, typename Opnd2>
2834m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2835 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2836}
2837
2838template <Intrinsic::ID IntrID, typename T0>
2839inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2841}
2842
2843template <Intrinsic::ID IntrID, typename T0, typename T1>
2844inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2845 const T1 &Op1) {
2847}
2848
2849template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2850inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2851m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2852 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2853}
2854
2855template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2856 typename T3>
2858m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2859 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2860}
2861
2862template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2863 typename T3, typename T4>
2865m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2866 const T4 &Op4) {
2867 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2868 m_Argument<4>(Op4));
2869}
2870
2871template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2872 typename T3, typename T4, typename T5>
2874m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2875 const T4 &Op4, const T5 &Op5) {
2876 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2877 m_Argument<5>(Op5));
2878}
2879
2880// Helper intrinsic matching specializations.
2881template <typename Opnd0>
2882inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2884}
2885
2886template <typename Opnd0>
2887inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2889}
2890
2891template <typename Opnd0>
2892inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2893 return m_Intrinsic<Intrinsic::fabs>(Op0);
2894}
2895
2896template <typename Opnd0>
2897inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2899}
2900
2901template <typename Opnd0, typename Opnd1>
2902inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2903 const Opnd1 &Op1) {
2904 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2905}
2906
2907template <typename Opnd0, typename Opnd1>
2908inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2909 const Opnd1 &Op1) {
2910 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2911}
2912
2913template <typename Opnd0, typename Opnd1>
2915m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2916 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2917}
2918
2919template <typename Opnd0, typename Opnd1>
2920inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2921 const Opnd1 &Op1) {
2922 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2923}
2924
2925template <typename Opnd0, typename Opnd1>
2926inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2927 const Opnd1 &Op1) {
2928 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2929}
2930
2931template <typename Opnd0, typename Opnd1>
2933m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2934 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2935}
2936
2937template <typename Opnd0, typename Opnd1, typename Opnd2>
2939m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2940 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2941}
2942
2943template <typename Opnd0, typename Opnd1, typename Opnd2>
2945m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2946 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2947}
2948
2949template <typename Opnd0>
2950inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2951 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2952}
2953
2954template <typename Opnd0, typename Opnd1>
2955inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2956 const Opnd1 &Op1) {
2957 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2958}
2959
2960template <typename Opnd0>
2961inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2963}
2964
2965template <typename Opnd0, typename Opnd1, typename Opnd2>
2967m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2968 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
2969}
2970
2971//===----------------------------------------------------------------------===//
2972// Matchers for two-operands operators with the operators in either order
2973//
2974
2975/// Matches a BinaryOperator with LHS and RHS in either order.
2976template <typename LHS, typename RHS>
2979}
2980
2981/// Matches an ICmp with a predicate over LHS and RHS in either order.
2982/// Swaps the predicate if operands are commuted.
2983template <typename LHS, typename RHS>
2985m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2987}
2988
2989template <typename LHS, typename RHS>
2994
2995/// Matches a specific opcode with LHS and RHS in either order.
2996template <typename LHS, typename RHS>
2998m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2999 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3000}
3001
3002/// Matches a Add with LHS and RHS in either order.
3003template <typename LHS, typename RHS>
3008
3009/// Matches a Mul with LHS and RHS in either order.
3010template <typename LHS, typename RHS>
3015
3016/// Matches an And with LHS and RHS in either order.
3017template <typename LHS, typename RHS>
3022
3023/// Matches an Or with LHS and RHS in either order.
3024template <typename LHS, typename RHS>
3029
3030/// Matches an Xor with LHS and RHS in either order.
3031template <typename LHS, typename RHS>
3036
3037/// Matches a 'Neg' as 'sub 0, V'.
3038template <typename ValTy>
3039inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3040m_Neg(const ValTy &V) {
3041 return m_Sub(m_ZeroInt(), V);
3042}
3043
3044/// Matches a 'Neg' as 'sub nsw 0, V'.
3045template <typename ValTy>
3047 Instruction::Sub,
3049m_NSWNeg(const ValTy &V) {
3050 return m_NSWSub(m_ZeroInt(), V);
3051}
3052
3053/// Matches an SMin with LHS and RHS in either order.
3054template <typename LHS, typename RHS>
3056m_c_SMin(const LHS &L, const RHS &R) {
3058}
3059/// Matches an SMax with LHS and RHS in either order.
3060template <typename LHS, typename RHS>
3062m_c_SMax(const LHS &L, const RHS &R) {
3064}
3065/// Matches a UMin with LHS and RHS in either order.
3066template <typename LHS, typename RHS>
3068m_c_UMin(const LHS &L, const RHS &R) {
3070}
3071/// Matches a UMax with LHS and RHS in either order.
3072template <typename LHS, typename RHS>
3074m_c_UMax(const LHS &L, const RHS &R) {
3076}
3077
3078template <typename LHS, typename RHS>
3079inline match_combine_or<
3084m_c_MaxOrMin(const LHS &L, const RHS &R) {
3085 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3086 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3087}
3088
3089template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3093
3094 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3095
3096 template <typename OpTy> bool match(OpTy *V) const {
3097 const auto *II = dyn_cast<IntrinsicInst>(V);
3098 if (!II || II->getIntrinsicID() != IntrID)
3099 return false;
3100 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3101 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3102 }
3103};
3104
3105template <Intrinsic::ID IntrID, typename T0, typename T1>
3107m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3109}
3110
3111/// Matches FAdd with LHS and RHS in either order.
3112template <typename LHS, typename RHS>
3114m_c_FAdd(const LHS &L, const RHS &R) {
3116}
3117
3118/// Matches FMul with LHS and RHS in either order.
3119template <typename LHS, typename RHS>
3121m_c_FMul(const LHS &L, const RHS &R) {
3123}
3124
3125template <typename Opnd_t> struct Signum_match {
3126 Opnd_t Val;
3127 Signum_match(const Opnd_t &V) : Val(V) {}
3128
3129 template <typename OpTy> bool match(OpTy *V) const {
3130 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3131 if (TypeSize == 0)
3132 return false;
3133
3134 unsigned ShiftWidth = TypeSize - 1;
3135 Value *Op;
3136
3137 // This is the representation of signum we match:
3138 //
3139 // signum(x) == (x >> 63) | (-x >>u 63)
3140 //
3141 // An i1 value is its own signum, so it's correct to match
3142 //
3143 // signum(x) == (x >> 0) | (-x >>u 0)
3144 //
3145 // for i1 values.
3146
3147 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3148 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3149 auto Signum = m_c_Or(LHS, RHS);
3150
3151 return Signum.match(V) && Val.match(Op);
3152 }
3153};
3154
3155/// Matches a signum pattern.
3156///
3157/// signum(x) =
3158/// x > 0 -> 1
3159/// x == 0 -> 0
3160/// x < 0 -> -1
3161template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3162 return Signum_match<Val_t>(V);
3163}
3164
3165template <int Ind, typename Opnd_t> struct ExtractValue_match {
3166 Opnd_t Val;
3167 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3168
3169 template <typename OpTy> bool match(OpTy *V) const {
3170 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3171 // If Ind is -1, don't inspect indices
3172 if (Ind != -1 &&
3173 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3174 return false;
3175 return Val.match(I->getAggregateOperand());
3176 }
3177 return false;
3178 }
3179};
3180
3181/// Match a single index ExtractValue instruction.
3182/// For example m_ExtractValue<1>(...)
3183template <int Ind, typename Val_t>
3187
3188/// Match an ExtractValue instruction with any index.
3189/// For example m_ExtractValue(...)
3190template <typename Val_t>
3191inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3192 return ExtractValue_match<-1, Val_t>(V);
3193}
3194
3195/// Matcher for a single index InsertValue instruction.
3196template <int Ind, typename T0, typename T1> struct InsertValue_match {
3199
3200 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3201
3202 template <typename OpTy> bool match(OpTy *V) const {
3203 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3204 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3205 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3206 }
3207 return false;
3208 }
3209};
3210
3211/// Matches a single index InsertValue instruction.
3212template <int Ind, typename Val_t, typename Elt_t>
3214 const Elt_t &Elt) {
3215 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3216}
3217
3218/// Matches a call to `llvm.vscale()`.
3220
3221template <typename Opnd0, typename Opnd1>
3223m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3225}
3226
3227template <typename Opnd>
3231
3232template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3236
3237 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3238
3239 template <typename T> bool match(T *V) const {
3240 auto *I = dyn_cast<Instruction>(V);
3241 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3242 return false;
3243
3244 if (I->getOpcode() == Opcode) {
3245 auto *Op0 = I->getOperand(0);
3246 auto *Op1 = I->getOperand(1);
3247 return (L.match(Op0) && R.match(Op1)) ||
3248 (Commutable && L.match(Op1) && R.match(Op0));
3249 }
3250
3251 if (auto *Select = dyn_cast<SelectInst>(I)) {
3252 auto *Cond = Select->getCondition();
3253 auto *TVal = Select->getTrueValue();
3254 auto *FVal = Select->getFalseValue();
3255
3256 // Don't match a scalar select of bool vectors.
3257 // Transforms expect a single type for operands if this matches.
3258 if (Cond->getType() != Select->getType())
3259 return false;
3260
3261 if (Opcode == Instruction::And) {
3262 auto *C = dyn_cast<Constant>(FVal);
3263 if (C && C->isNullValue())
3264 return (L.match(Cond) && R.match(TVal)) ||
3265 (Commutable && L.match(TVal) && R.match(Cond));
3266 } else {
3267 assert(Opcode == Instruction::Or);
3268 auto *C = dyn_cast<Constant>(TVal);
3269 if (C && C->isOneValue())
3270 return (L.match(Cond) && R.match(FVal)) ||
3271 (Commutable && L.match(FVal) && R.match(Cond));
3272 }
3273 }
3274
3275 return false;
3276 }
3277};
3278
3279/// Matches L && R either in the form of L & R or L ? R : false.
3280/// Note that the latter form is poison-blocking.
3281template <typename LHS, typename RHS>
3286
3287/// Matches L && R where L and R are arbitrary values.
3288inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3289
3290/// Matches L && R with LHS and RHS in either order.
3291template <typename LHS, typename RHS>
3293m_c_LogicalAnd(const LHS &L, const RHS &R) {
3295}
3296
3297/// Matches L || R either in the form of L | R or L ? true : R.
3298/// Note that the latter form is poison-blocking.
3299template <typename LHS, typename RHS>
3304
3305/// Matches L || R where L and R are arbitrary values.
3306inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3307
3308/// Matches L || R with LHS and RHS in either order.
3309template <typename LHS, typename RHS>
3311m_c_LogicalOr(const LHS &L, const RHS &R) {
3313}
3314
3315/// Matches either L && R or L || R,
3316/// either one being in the either binary or logical form.
3317/// Note that the latter form is poison-blocking.
3318template <typename LHS, typename RHS, bool Commutable = false>
3324
3325/// Matches either L && R or L || R where L and R are arbitrary values.
3326inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3327
3328/// Matches either L && R or L || R with LHS and RHS in either order.
3329template <typename LHS, typename RHS>
3330inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3331 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3332}
3333
3334} // end namespace PatternMatch
3335} // end namespace llvm
3336
3337#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
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1549
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1521
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
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:1430
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:1737
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:1770
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)