1 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides a simple and efficient mechanism for performing general
11 // tree-based pattern matches on the LLVM IR. The power of these routines is
12 // that it allows you to write concise patterns that are expressive and easy to
13 // understand. The other major advantage of this is that it allows you to
14 // trivially capture/bind elements in the pattern to variables. For example,
15 // you can do something like this:
18 // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
19 // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
20 // m_And(m_Value(Y), m_ConstantInt(C2))))) {
21 // ... Pattern is matched and variables are bound ...
24 // This is primarily useful to things like the instruction combiner, but can
25 // also be useful for static analysis tools or code generators.
27 //===----------------------------------------------------------------------===//
29 #ifndef LLVM_IR_PATTERNMATCH_H
30 #define LLVM_IR_PATTERNMATCH_H
32 #include "llvm/ADT/APFloat.h"
33 #include "llvm/ADT/APInt.h"
34 #include "llvm/IR/Constant.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/InstrTypes.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/IR/Operator.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/Support/Casting.h"
46 namespace PatternMatch {
48 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
49 return const_cast<Pattern &>(P).match(V);
52 template <typename SubPattern_t> struct OneUse_match {
53 SubPattern_t SubPattern;
55 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
57 template <typename OpTy> bool match(OpTy *V) {
58 return V->hasOneUse() && SubPattern.match(V);
62 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
66 template <typename Class> struct class_match {
67 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
70 /// Match an arbitrary value and ignore it.
71 inline class_match<Value> m_Value() { return class_match<Value>(); }
73 /// Match an arbitrary binary operation and ignore it.
74 inline class_match<BinaryOperator> m_BinOp() {
75 return class_match<BinaryOperator>();
78 /// Matches any compare instruction and ignore it.
79 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
81 /// Match an arbitrary ConstantInt and ignore it.
82 inline class_match<ConstantInt> m_ConstantInt() {
83 return class_match<ConstantInt>();
86 /// Match an arbitrary undef constant.
87 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
89 /// Match an arbitrary Constant and ignore it.
90 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
92 /// Matching combinators
93 template <typename LTy, typename RTy> struct match_combine_or {
97 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
99 template <typename ITy> bool match(ITy *V) {
108 template <typename LTy, typename RTy> struct match_combine_and {
112 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
114 template <typename ITy> bool match(ITy *V) {
122 /// Combine two pattern matchers matching L || R
123 template <typename LTy, typename RTy>
124 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
125 return match_combine_or<LTy, RTy>(L, R);
128 /// Combine two pattern matchers matching L && R
129 template <typename LTy, typename RTy>
130 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
131 return match_combine_and<LTy, RTy>(L, R);
137 apint_match(const APInt *&R) : Res(R) {}
139 template <typename ITy> bool match(ITy *V) {
140 if (auto *CI = dyn_cast<ConstantInt>(V)) {
141 Res = &CI->getValue();
144 if (V->getType()->isVectorTy())
145 if (const auto *C = dyn_cast<Constant>(V))
146 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
147 Res = &CI->getValue();
153 // Either constexpr if or renaming ConstantFP::getValueAPF to
154 // ConstantFP::getValue is needed to do it via single template
155 // function for both apint/apfloat.
156 struct apfloat_match {
158 apfloat_match(const APFloat *&R) : Res(R) {}
159 template <typename ITy> bool match(ITy *V) {
160 if (auto *CI = dyn_cast<ConstantFP>(V)) {
161 Res = &CI->getValueAPF();
164 if (V->getType()->isVectorTy())
165 if (const auto *C = dyn_cast<Constant>(V))
166 if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) {
167 Res = &CI->getValueAPF();
174 /// Match a ConstantInt or splatted ConstantVector, binding the
175 /// specified pointer to the contained APInt.
176 inline apint_match m_APInt(const APInt *&Res) { return Res; }
178 /// Match a ConstantFP or splatted ConstantVector, binding the
179 /// specified pointer to the contained APFloat.
180 inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
182 template <int64_t Val> struct constantint_match {
183 template <typename ITy> bool match(ITy *V) {
184 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
185 const APInt &CIV = CI->getValue();
187 return CIV == static_cast<uint64_t>(Val);
188 // If Val is negative, and CI is shorter than it, truncate to the right
189 // number of bits. If it is larger, then we have to sign extend. Just
190 // compare their negated values.
197 /// Match a ConstantInt with a specific value.
198 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
199 return constantint_match<Val>();
202 /// This helper class is used to match scalar and vector integer constants that
203 /// satisfy a specified predicate.
204 /// For vector constants, undefined elements are ignored.
205 template <typename Predicate> struct cst_pred_ty : public Predicate {
206 template <typename ITy> bool match(ITy *V) {
207 if (const auto *CI = dyn_cast<ConstantInt>(V))
208 return this->isValue(CI->getValue());
209 if (V->getType()->isVectorTy()) {
210 if (const auto *C = dyn_cast<Constant>(V)) {
211 if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
212 return this->isValue(CI->getValue());
214 // Non-splat vector constant: check each element for a match.
215 unsigned NumElts = V->getType()->getVectorNumElements();
216 assert(NumElts != 0 && "Constant vector with no elements?");
217 bool HasNonUndefElements = false;
218 for (unsigned i = 0; i != NumElts; ++i) {
219 Constant *Elt = C->getAggregateElement(i);
222 if (isa<UndefValue>(Elt))
224 auto *CI = dyn_cast<ConstantInt>(Elt);
225 if (!CI || !this->isValue(CI->getValue()))
227 HasNonUndefElements = true;
229 return HasNonUndefElements;
236 /// This helper class is used to match scalar and vector constants that
237 /// satisfy a specified predicate, and bind them to an APInt.
238 template <typename Predicate> struct api_pred_ty : public Predicate {
241 api_pred_ty(const APInt *&R) : Res(R) {}
243 template <typename ITy> bool match(ITy *V) {
244 if (const auto *CI = dyn_cast<ConstantInt>(V))
245 if (this->isValue(CI->getValue())) {
246 Res = &CI->getValue();
249 if (V->getType()->isVectorTy())
250 if (const auto *C = dyn_cast<Constant>(V))
251 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
252 if (this->isValue(CI->getValue())) {
253 Res = &CI->getValue();
261 /// This helper class is used to match scalar and vector floating-point
262 /// constants that satisfy a specified predicate.
263 /// For vector constants, undefined elements are ignored.
264 template <typename Predicate> struct cstfp_pred_ty : public Predicate {
265 template <typename ITy> bool match(ITy *V) {
266 if (const auto *CF = dyn_cast<ConstantFP>(V))
267 return this->isValue(CF->getValueAPF());
268 if (V->getType()->isVectorTy()) {
269 if (const auto *C = dyn_cast<Constant>(V)) {
270 if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
271 return this->isValue(CF->getValueAPF());
273 // Non-splat vector constant: check each element for a match.
274 unsigned NumElts = V->getType()->getVectorNumElements();
275 assert(NumElts != 0 && "Constant vector with no elements?");
276 bool HasNonUndefElements = false;
277 for (unsigned i = 0; i != NumElts; ++i) {
278 Constant *Elt = C->getAggregateElement(i);
281 if (isa<UndefValue>(Elt))
283 auto *CF = dyn_cast<ConstantFP>(Elt);
284 if (!CF || !this->isValue(CF->getValueAPF()))
286 HasNonUndefElements = true;
288 return HasNonUndefElements;
295 ///////////////////////////////////////////////////////////////////////////////
297 // Encapsulate constant value queries for use in templated predicate matchers.
298 // This allows checking if constants match using compound predicates and works
299 // with vector constants, possibly with relaxed constraints. For example, ignore
302 ///////////////////////////////////////////////////////////////////////////////
305 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
307 /// Match an integer or vector with all bits set.
308 /// For vectors, this includes constants with undefined elements.
309 inline cst_pred_ty<is_all_ones> m_AllOnes() {
310 return cst_pred_ty<is_all_ones>();
313 struct is_maxsignedvalue {
314 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
316 /// Match an integer or vector with values having all bits except for the high
317 /// bit set (0x7f...).
318 /// For vectors, this includes constants with undefined elements.
319 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
320 return cst_pred_ty<is_maxsignedvalue>();
322 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
327 bool isValue(const APInt &C) { return C.isNegative(); }
329 /// Match an integer or vector of negative values.
330 /// For vectors, this includes constants with undefined elements.
331 inline cst_pred_ty<is_negative> m_Negative() {
332 return cst_pred_ty<is_negative>();
334 inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
338 struct is_nonnegative {
339 bool isValue(const APInt &C) { return C.isNonNegative(); }
341 /// Match an integer or vector of nonnegative values.
342 /// For vectors, this includes constants with undefined elements.
343 inline cst_pred_ty<is_nonnegative> m_NonNegative() {
344 return cst_pred_ty<is_nonnegative>();
346 inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
351 bool isValue(const APInt &C) { return C.isOneValue(); }
353 /// Match an integer 1 or a vector with all elements equal to 1.
354 /// For vectors, this includes constants with undefined elements.
355 inline cst_pred_ty<is_one> m_One() {
356 return cst_pred_ty<is_one>();
360 bool isValue(const APInt &C) { return C.isNullValue(); }
362 /// Match an integer 0 or a vector with all elements equal to 0.
363 /// For vectors, this includes constants with undefined elements.
364 inline cst_pred_ty<is_zero_int> m_ZeroInt() {
365 return cst_pred_ty<is_zero_int>();
369 template <typename ITy> bool match(ITy *V) {
370 auto *C = dyn_cast<Constant>(V);
371 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
374 /// Match any null constant or a vector with all elements equal to 0.
375 /// For vectors, this includes constants with undefined elements.
376 inline is_zero m_Zero() {
381 bool isValue(const APInt &C) { return C.isPowerOf2(); }
383 /// Match an integer or vector power-of-2.
384 /// For vectors, this includes constants with undefined elements.
385 inline cst_pred_ty<is_power2> m_Power2() {
386 return cst_pred_ty<is_power2>();
388 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
392 struct is_power2_or_zero {
393 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
395 /// Match an integer or vector of 0 or power-of-2 values.
396 /// For vectors, this includes constants with undefined elements.
397 inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
398 return cst_pred_ty<is_power2_or_zero>();
400 inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
404 struct is_sign_mask {
405 bool isValue(const APInt &C) { return C.isSignMask(); }
407 /// Match an integer or vector with only the sign bit(s) set.
408 /// For vectors, this includes constants with undefined elements.
409 inline cst_pred_ty<is_sign_mask> m_SignMask() {
410 return cst_pred_ty<is_sign_mask>();
413 struct is_lowbit_mask {
414 bool isValue(const APInt &C) { return C.isMask(); }
416 /// Match an integer or vector with only the low bit(s) set.
417 /// For vectors, this includes constants with undefined elements.
418 inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
419 return cst_pred_ty<is_lowbit_mask>();
423 bool isValue(const APFloat &C) { return C.isNaN(); }
425 /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
426 /// For vectors, this includes constants with undefined elements.
427 inline cstfp_pred_ty<is_nan> m_NaN() {
428 return cstfp_pred_ty<is_nan>();
431 struct is_any_zero_fp {
432 bool isValue(const APFloat &C) { return C.isZero(); }
434 /// Match a floating-point negative zero or positive zero.
435 /// For vectors, this includes constants with undefined elements.
436 inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
437 return cstfp_pred_ty<is_any_zero_fp>();
440 struct is_pos_zero_fp {
441 bool isValue(const APFloat &C) { return C.isPosZero(); }
443 /// Match a floating-point positive zero.
444 /// For vectors, this includes constants with undefined elements.
445 inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
446 return cstfp_pred_ty<is_pos_zero_fp>();
449 struct is_neg_zero_fp {
450 bool isValue(const APFloat &C) { return C.isNegZero(); }
452 /// Match a floating-point negative zero.
453 /// For vectors, this includes constants with undefined elements.
454 inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
455 return cstfp_pred_ty<is_neg_zero_fp>();
458 ///////////////////////////////////////////////////////////////////////////////
460 template <typename Class> struct bind_ty {
463 bind_ty(Class *&V) : VR(V) {}
465 template <typename ITy> bool match(ITy *V) {
466 if (auto *CV = dyn_cast<Class>(V)) {
474 /// Match a value, capturing it if we match.
475 inline bind_ty<Value> m_Value(Value *&V) { return V; }
476 inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
478 /// Match an instruction, capturing it if we match.
479 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
480 /// Match a binary operator, capturing it if we match.
481 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
483 /// Match a ConstantInt, capturing the value if we match.
484 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
486 /// Match a Constant, capturing the value if we match.
487 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
489 /// Match a ConstantFP, capturing the value if we match.
490 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
492 /// Match a specified Value*.
493 struct specificval_ty {
496 specificval_ty(const Value *V) : Val(V) {}
498 template <typename ITy> bool match(ITy *V) { return V == Val; }
501 /// Match if we have a specific specified value.
502 inline specificval_ty m_Specific(const Value *V) { return V; }
504 /// Stores a reference to the Value *, not the Value * itself,
505 /// thus can be used in commutative matchers.
506 template <typename Class> struct deferredval_ty {
509 deferredval_ty(Class *const &V) : Val(V) {}
511 template <typename ITy> bool match(ITy *const V) { return V == Val; }
514 /// A commutative-friendly version of m_Specific().
515 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
516 inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
520 /// Match a specified floating point value or vector of all elements of
522 struct specific_fpval {
525 specific_fpval(double V) : Val(V) {}
527 template <typename ITy> bool match(ITy *V) {
528 if (const auto *CFP = dyn_cast<ConstantFP>(V))
529 return CFP->isExactlyValue(Val);
530 if (V->getType()->isVectorTy())
531 if (const auto *C = dyn_cast<Constant>(V))
532 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
533 return CFP->isExactlyValue(Val);
538 /// Match a specific floating point value or vector with all elements
539 /// equal to the value.
540 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
542 /// Match a float 1.0 or vector with all elements equal to 1.0.
543 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
545 struct bind_const_intval_ty {
548 bind_const_intval_ty(uint64_t &V) : VR(V) {}
550 template <typename ITy> bool match(ITy *V) {
551 if (const auto *CV = dyn_cast<ConstantInt>(V))
552 if (CV->getValue().ule(UINT64_MAX)) {
553 VR = CV->getZExtValue();
560 /// Match a specified integer value or vector of all elements of that
562 struct specific_intval {
565 specific_intval(uint64_t V) : Val(V) {}
567 template <typename ITy> bool match(ITy *V) {
568 const auto *CI = dyn_cast<ConstantInt>(V);
569 if (!CI && V->getType()->isVectorTy())
570 if (const auto *C = dyn_cast<Constant>(V))
571 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
573 return CI && CI->getValue() == Val;
577 /// Match a specific integer value or vector with all elements equal to
579 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
581 /// Match a ConstantInt and bind to its value. This does not match
582 /// ConstantInts wider than 64-bits.
583 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
585 //===----------------------------------------------------------------------===//
586 // Matcher for any binary operator.
588 template <typename LHS_t, typename RHS_t, bool Commutable = false>
589 struct AnyBinaryOp_match {
593 // The evaluation order is always stable, regardless of Commutability.
594 // The LHS is always matched first.
595 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
597 template <typename OpTy> bool match(OpTy *V) {
598 if (auto *I = dyn_cast<BinaryOperator>(V))
599 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
600 (Commutable && L.match(I->getOperand(1)) &&
601 R.match(I->getOperand(0)));
606 template <typename LHS, typename RHS>
607 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
608 return AnyBinaryOp_match<LHS, RHS>(L, R);
611 //===----------------------------------------------------------------------===//
612 // Matchers for specific binary operators.
615 template <typename LHS_t, typename RHS_t, unsigned Opcode,
616 bool Commutable = false>
617 struct BinaryOp_match {
621 // The evaluation order is always stable, regardless of Commutability.
622 // The LHS is always matched first.
623 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
625 template <typename OpTy> bool match(OpTy *V) {
626 if (V->getValueID() == Value::InstructionVal + Opcode) {
627 auto *I = cast<BinaryOperator>(V);
628 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
629 (Commutable && L.match(I->getOperand(1)) &&
630 R.match(I->getOperand(0)));
632 if (auto *CE = dyn_cast<ConstantExpr>(V))
633 return CE->getOpcode() == Opcode &&
634 ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
635 (Commutable && L.match(CE->getOperand(1)) &&
636 R.match(CE->getOperand(0))));
641 template <typename LHS, typename RHS>
642 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
644 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
647 template <typename LHS, typename RHS>
648 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
650 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
653 template <typename LHS, typename RHS>
654 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
656 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
659 template <typename LHS, typename RHS>
660 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
662 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
665 template <typename Op_t> struct FNeg_match {
668 FNeg_match(const Op_t &Op) : X(Op) {}
669 template <typename OpTy> bool match(OpTy *V) {
670 auto *FPMO = dyn_cast<FPMathOperator>(V);
671 if (!FPMO || FPMO->getOpcode() != Instruction::FSub)
673 if (FPMO->hasNoSignedZeros()) {
674 // With 'nsz', any zero goes.
675 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
678 // Without 'nsz', we need fsub -0.0, X exactly.
679 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
682 return X.match(FPMO->getOperand(1));
686 /// Match 'fneg X' as 'fsub -0.0, X'.
687 template <typename OpTy>
688 inline FNeg_match<OpTy>
689 m_FNeg(const OpTy &X) {
690 return FNeg_match<OpTy>(X);
693 /// Match 'fneg X' as 'fsub +-0.0, X'.
694 template <typename RHS>
695 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
696 m_FNegNSZ(const RHS &X) {
697 return m_FSub(m_AnyZeroFP(), X);
700 template <typename LHS, typename RHS>
701 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
703 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
706 template <typename LHS, typename RHS>
707 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
709 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
712 template <typename LHS, typename RHS>
713 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
715 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
718 template <typename LHS, typename RHS>
719 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
721 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
724 template <typename LHS, typename RHS>
725 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
727 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
730 template <typename LHS, typename RHS>
731 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
733 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
736 template <typename LHS, typename RHS>
737 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
739 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
742 template <typename LHS, typename RHS>
743 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
745 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
748 template <typename LHS, typename RHS>
749 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
751 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
754 template <typename LHS, typename RHS>
755 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
757 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
760 template <typename LHS, typename RHS>
761 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
763 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
766 template <typename LHS, typename RHS>
767 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
769 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
772 template <typename LHS, typename RHS>
773 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
775 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
778 template <typename LHS, typename RHS>
779 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
781 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
784 template <typename LHS_t, typename RHS_t, unsigned Opcode,
785 unsigned WrapFlags = 0>
786 struct OverflowingBinaryOp_match {
790 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
793 template <typename OpTy> bool match(OpTy *V) {
794 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
795 if (Op->getOpcode() != Opcode)
797 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
798 !Op->hasNoUnsignedWrap())
800 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
801 !Op->hasNoSignedWrap())
803 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
809 template <typename LHS, typename RHS>
810 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
811 OverflowingBinaryOperator::NoSignedWrap>
812 m_NSWAdd(const LHS &L, const RHS &R) {
813 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
814 OverflowingBinaryOperator::NoSignedWrap>(
817 template <typename LHS, typename RHS>
818 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
819 OverflowingBinaryOperator::NoSignedWrap>
820 m_NSWSub(const LHS &L, const RHS &R) {
821 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
822 OverflowingBinaryOperator::NoSignedWrap>(
825 template <typename LHS, typename RHS>
826 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
827 OverflowingBinaryOperator::NoSignedWrap>
828 m_NSWMul(const LHS &L, const RHS &R) {
829 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
830 OverflowingBinaryOperator::NoSignedWrap>(
833 template <typename LHS, typename RHS>
834 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
835 OverflowingBinaryOperator::NoSignedWrap>
836 m_NSWShl(const LHS &L, const RHS &R) {
837 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
838 OverflowingBinaryOperator::NoSignedWrap>(
842 template <typename LHS, typename RHS>
843 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
844 OverflowingBinaryOperator::NoUnsignedWrap>
845 m_NUWAdd(const LHS &L, const RHS &R) {
846 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
847 OverflowingBinaryOperator::NoUnsignedWrap>(
850 template <typename LHS, typename RHS>
851 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
852 OverflowingBinaryOperator::NoUnsignedWrap>
853 m_NUWSub(const LHS &L, const RHS &R) {
854 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
855 OverflowingBinaryOperator::NoUnsignedWrap>(
858 template <typename LHS, typename RHS>
859 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
860 OverflowingBinaryOperator::NoUnsignedWrap>
861 m_NUWMul(const LHS &L, const RHS &R) {
862 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
863 OverflowingBinaryOperator::NoUnsignedWrap>(
866 template <typename LHS, typename RHS>
867 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
868 OverflowingBinaryOperator::NoUnsignedWrap>
869 m_NUWShl(const LHS &L, const RHS &R) {
870 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
871 OverflowingBinaryOperator::NoUnsignedWrap>(
875 //===----------------------------------------------------------------------===//
876 // Class that matches a group of binary opcodes.
878 template <typename LHS_t, typename RHS_t, typename Predicate>
879 struct BinOpPred_match : Predicate {
883 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
885 template <typename OpTy> bool match(OpTy *V) {
886 if (auto *I = dyn_cast<Instruction>(V))
887 return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
888 R.match(I->getOperand(1));
889 if (auto *CE = dyn_cast<ConstantExpr>(V))
890 return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
891 R.match(CE->getOperand(1));
897 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
900 struct is_right_shift_op {
901 bool isOpType(unsigned Opcode) {
902 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
906 struct is_logical_shift_op {
907 bool isOpType(unsigned Opcode) {
908 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
912 struct is_bitwiselogic_op {
913 bool isOpType(unsigned Opcode) {
914 return Instruction::isBitwiseLogicOp(Opcode);
919 bool isOpType(unsigned Opcode) {
920 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
924 /// Matches shift operations.
925 template <typename LHS, typename RHS>
926 inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
928 return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
931 /// Matches logical shift operations.
932 template <typename LHS, typename RHS>
933 inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
935 return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
938 /// Matches logical shift operations.
939 template <typename LHS, typename RHS>
940 inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
941 m_LogicalShift(const LHS &L, const RHS &R) {
942 return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
945 /// Matches bitwise logic operations.
946 template <typename LHS, typename RHS>
947 inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
948 m_BitwiseLogic(const LHS &L, const RHS &R) {
949 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
952 /// Matches integer division operations.
953 template <typename LHS, typename RHS>
954 inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
956 return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
959 //===----------------------------------------------------------------------===//
960 // Class that matches exact binary ops.
962 template <typename SubPattern_t> struct Exact_match {
963 SubPattern_t SubPattern;
965 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
967 template <typename OpTy> bool match(OpTy *V) {
968 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
969 return PEO->isExact() && SubPattern.match(V);
974 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
978 //===----------------------------------------------------------------------===//
979 // Matchers for CmpInst classes
982 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
983 bool Commutable = false>
984 struct CmpClass_match {
985 PredicateTy &Predicate;
989 // The evaluation order is always stable, regardless of Commutability.
990 // The LHS is always matched first.
991 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
992 : Predicate(Pred), L(LHS), R(RHS) {}
994 template <typename OpTy> bool match(OpTy *V) {
995 if (auto *I = dyn_cast<Class>(V))
996 if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
997 (Commutable && L.match(I->getOperand(1)) &&
998 R.match(I->getOperand(0)))) {
999 Predicate = I->getPredicate();
1006 template <typename LHS, typename RHS>
1007 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
1008 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1009 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
1012 template <typename LHS, typename RHS>
1013 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
1014 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1015 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
1018 template <typename LHS, typename RHS>
1019 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
1020 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1021 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
1024 //===----------------------------------------------------------------------===//
1025 // Matchers for instructions with a given opcode and number of operands.
1028 /// Matches instructions with Opcode and three operands.
1029 template <typename T0, unsigned Opcode> struct OneOps_match {
1032 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1034 template <typename OpTy> bool match(OpTy *V) {
1035 if (V->getValueID() == Value::InstructionVal + Opcode) {
1036 auto *I = cast<Instruction>(V);
1037 return Op1.match(I->getOperand(0));
1043 /// Matches instructions with Opcode and three operands.
1044 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1048 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1050 template <typename OpTy> bool match(OpTy *V) {
1051 if (V->getValueID() == Value::InstructionVal + Opcode) {
1052 auto *I = cast<Instruction>(V);
1053 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1059 /// Matches instructions with Opcode and three operands.
1060 template <typename T0, typename T1, typename T2, unsigned Opcode>
1061 struct ThreeOps_match {
1066 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1067 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1069 template <typename OpTy> bool match(OpTy *V) {
1070 if (V->getValueID() == Value::InstructionVal + Opcode) {
1071 auto *I = cast<Instruction>(V);
1072 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1073 Op3.match(I->getOperand(2));
1079 /// Matches SelectInst.
1080 template <typename Cond, typename LHS, typename RHS>
1081 inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
1082 m_Select(const Cond &C, const LHS &L, const RHS &R) {
1083 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
1086 /// This matches a select of two constants, e.g.:
1087 /// m_SelectCst<-1, 0>(m_Value(V))
1088 template <int64_t L, int64_t R, typename Cond>
1089 inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1090 Instruction::Select>
1091 m_SelectCst(const Cond &C) {
1092 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1095 /// Matches InsertElementInst.
1096 template <typename Val_t, typename Elt_t, typename Idx_t>
1097 inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
1098 m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1099 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1103 /// Matches ExtractElementInst.
1104 template <typename Val_t, typename Idx_t>
1105 inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
1106 m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
1107 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
1110 /// Matches ShuffleVectorInst.
1111 template <typename V1_t, typename V2_t, typename Mask_t>
1112 inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>
1113 m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
1114 return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2,
1118 /// Matches LoadInst.
1119 template <typename OpTy>
1120 inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1121 return OneOps_match<OpTy, Instruction::Load>(Op);
1124 /// Matches StoreInst.
1125 template <typename ValueOpTy, typename PointerOpTy>
1126 inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
1127 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1128 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1132 //===----------------------------------------------------------------------===//
1133 // Matchers for CastInst classes
1136 template <typename Op_t, unsigned Opcode> struct CastClass_match {
1139 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1141 template <typename OpTy> bool match(OpTy *V) {
1142 if (auto *O = dyn_cast<Operator>(V))
1143 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1148 /// Matches BitCast.
1149 template <typename OpTy>
1150 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
1151 return CastClass_match<OpTy, Instruction::BitCast>(Op);
1154 /// Matches PtrToInt.
1155 template <typename OpTy>
1156 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
1157 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
1161 template <typename OpTy>
1162 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1163 return CastClass_match<OpTy, Instruction::Trunc>(Op);
1167 template <typename OpTy>
1168 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1169 return CastClass_match<OpTy, Instruction::SExt>(Op);
1173 template <typename OpTy>
1174 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1175 return CastClass_match<OpTy, Instruction::ZExt>(Op);
1178 template <typename OpTy>
1179 inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1180 CastClass_match<OpTy, Instruction::SExt>>
1181 m_ZExtOrSExt(const OpTy &Op) {
1182 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1186 template <typename OpTy>
1187 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1188 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
1192 template <typename OpTy>
1193 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1194 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
1198 template <typename OpTy>
1199 inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
1200 return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
1204 template <typename OpTy>
1205 inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1206 return CastClass_match<OpTy, Instruction::FPExt>(Op);
1209 //===----------------------------------------------------------------------===//
1210 // Matchers for control flow.
1216 br_match(BasicBlock *&Succ) : Succ(Succ) {}
1218 template <typename OpTy> bool match(OpTy *V) {
1219 if (auto *BI = dyn_cast<BranchInst>(V))
1220 if (BI->isUnconditional()) {
1221 Succ = BI->getSuccessor(0);
1228 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1230 template <typename Cond_t> struct brc_match {
1232 BasicBlock *&T, *&F;
1234 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
1235 : Cond(C), T(t), F(f) {}
1237 template <typename OpTy> bool match(OpTy *V) {
1238 if (auto *BI = dyn_cast<BranchInst>(V))
1239 if (BI->isConditional() && Cond.match(BI->getCondition())) {
1240 T = BI->getSuccessor(0);
1241 F = BI->getSuccessor(1);
1248 template <typename Cond_t>
1249 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1250 return brc_match<Cond_t>(C, T, F);
1253 //===----------------------------------------------------------------------===//
1254 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1257 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1258 bool Commutable = false>
1259 struct MaxMin_match {
1263 // The evaluation order is always stable, regardless of Commutability.
1264 // The LHS is always matched first.
1265 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1267 template <typename OpTy> bool match(OpTy *V) {
1268 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1269 auto *SI = dyn_cast<SelectInst>(V);
1272 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1275 // At this point we have a select conditioned on a comparison. Check that
1276 // it is the values returned by the select that are being compared.
1277 Value *TrueVal = SI->getTrueValue();
1278 Value *FalseVal = SI->getFalseValue();
1279 Value *LHS = Cmp->getOperand(0);
1280 Value *RHS = Cmp->getOperand(1);
1281 if ((TrueVal != LHS || FalseVal != RHS) &&
1282 (TrueVal != RHS || FalseVal != LHS))
1284 typename CmpInst_t::Predicate Pred =
1285 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1286 // Does "(x pred y) ? x : y" represent the desired max/min operation?
1287 if (!Pred_t::match(Pred))
1289 // It does! Bind the operands.
1290 return (L.match(LHS) && R.match(RHS)) ||
1291 (Commutable && L.match(RHS) && R.match(LHS));
1295 /// Helper class for identifying signed max predicates.
1296 struct smax_pred_ty {
1297 static bool match(ICmpInst::Predicate Pred) {
1298 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1302 /// Helper class for identifying signed min predicates.
1303 struct smin_pred_ty {
1304 static bool match(ICmpInst::Predicate Pred) {
1305 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1309 /// Helper class for identifying unsigned max predicates.
1310 struct umax_pred_ty {
1311 static bool match(ICmpInst::Predicate Pred) {
1312 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1316 /// Helper class for identifying unsigned min predicates.
1317 struct umin_pred_ty {
1318 static bool match(ICmpInst::Predicate Pred) {
1319 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1323 /// Helper class for identifying ordered max predicates.
1324 struct ofmax_pred_ty {
1325 static bool match(FCmpInst::Predicate Pred) {
1326 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1330 /// Helper class for identifying ordered min predicates.
1331 struct ofmin_pred_ty {
1332 static bool match(FCmpInst::Predicate Pred) {
1333 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1337 /// Helper class for identifying unordered max predicates.
1338 struct ufmax_pred_ty {
1339 static bool match(FCmpInst::Predicate Pred) {
1340 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1344 /// Helper class for identifying unordered min predicates.
1345 struct ufmin_pred_ty {
1346 static bool match(FCmpInst::Predicate Pred) {
1347 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1351 template <typename LHS, typename RHS>
1352 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1354 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1357 template <typename LHS, typename RHS>
1358 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1360 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1363 template <typename LHS, typename RHS>
1364 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1366 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1369 template <typename LHS, typename RHS>
1370 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1372 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1375 /// Match an 'ordered' floating point maximum function.
1376 /// Floating point has one special value 'NaN'. Therefore, there is no total
1377 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1378 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1379 /// semantics. In the presence of 'NaN' we have to preserve the original
1380 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1382 /// max(L, R) iff L and R are not NaN
1383 /// m_OrdFMax(L, R) = R iff L or R are NaN
1384 template <typename LHS, typename RHS>
1385 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1387 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1390 /// Match an 'ordered' floating point minimum function.
1391 /// Floating point has one special value 'NaN'. Therefore, there is no total
1392 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1393 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1394 /// semantics. In the presence of 'NaN' we have to preserve the original
1395 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1397 /// min(L, R) iff L and R are not NaN
1398 /// m_OrdFMin(L, R) = R iff L or R are NaN
1399 template <typename LHS, typename RHS>
1400 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1402 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1405 /// Match an 'unordered' floating point maximum function.
1406 /// Floating point has one special value 'NaN'. Therefore, there is no total
1407 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1408 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1409 /// semantics. In the presence of 'NaN' we have to preserve the original
1410 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1412 /// max(L, R) iff L and R are not NaN
1413 /// m_UnordFMax(L, R) = L iff L or R are NaN
1414 template <typename LHS, typename RHS>
1415 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1416 m_UnordFMax(const LHS &L, const RHS &R) {
1417 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1420 /// Match an 'unordered' floating point minimum function.
1421 /// Floating point has one special value 'NaN'. Therefore, there is no total
1422 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1423 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1424 /// semantics. In the presence of 'NaN' we have to preserve the original
1425 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1427 /// min(L, R) iff L and R are not NaN
1428 /// m_UnordFMin(L, R) = L iff L or R are NaN
1429 template <typename LHS, typename RHS>
1430 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1431 m_UnordFMin(const LHS &L, const RHS &R) {
1432 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1435 //===----------------------------------------------------------------------===//
1436 // Matchers for overflow check patterns: e.g. (a + b) u< a
1439 template <typename LHS_t, typename RHS_t, typename Sum_t>
1440 struct UAddWithOverflow_match {
1445 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1446 : L(L), R(R), S(S) {}
1448 template <typename OpTy> bool match(OpTy *V) {
1449 Value *ICmpLHS, *ICmpRHS;
1450 ICmpInst::Predicate Pred;
1451 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1454 Value *AddLHS, *AddRHS;
1455 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1457 // (a + b) u< a, (a + b) u< b
1458 if (Pred == ICmpInst::ICMP_ULT)
1459 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1460 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1462 // a >u (a + b), b >u (a + b)
1463 if (Pred == ICmpInst::ICMP_UGT)
1464 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1465 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1471 /// Match an icmp instruction checking for unsigned overflow on addition.
1473 /// S is matched to the addition whose result is being checked for overflow, and
1474 /// L and R are matched to the LHS and RHS of S.
1475 template <typename LHS_t, typename RHS_t, typename Sum_t>
1476 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
1477 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1478 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
1481 template <typename Opnd_t> struct Argument_match {
1485 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1487 template <typename OpTy> bool match(OpTy *V) {
1488 // FIXME: Should likely be switched to use `CallBase`.
1489 if (const auto *CI = dyn_cast<CallInst>(V))
1490 return Val.match(CI->getArgOperand(OpI));
1495 /// Match an argument.
1496 template <unsigned OpI, typename Opnd_t>
1497 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1498 return Argument_match<Opnd_t>(OpI, Op);
1501 /// Intrinsic matchers.
1502 struct IntrinsicID_match {
1505 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1507 template <typename OpTy> bool match(OpTy *V) {
1508 if (const auto *CI = dyn_cast<CallInst>(V))
1509 if (const auto *F = CI->getCalledFunction())
1510 return F->getIntrinsicID() == ID;
1515 /// Intrinsic matches are combinations of ID matchers, and argument
1516 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1517 /// them with lower arity matchers. Here's some convenient typedefs for up to
1518 /// several arguments, and more can be added as needed
1519 template <typename T0 = void, typename T1 = void, typename T2 = void,
1520 typename T3 = void, typename T4 = void, typename T5 = void,
1521 typename T6 = void, typename T7 = void, typename T8 = void,
1522 typename T9 = void, typename T10 = void>
1523 struct m_Intrinsic_Ty;
1524 template <typename T0> struct m_Intrinsic_Ty<T0> {
1525 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
1527 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1529 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
1531 template <typename T0, typename T1, typename T2>
1532 struct m_Intrinsic_Ty<T0, T1, T2> {
1534 match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1535 Argument_match<T2>>;
1537 template <typename T0, typename T1, typename T2, typename T3>
1538 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1540 match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1541 Argument_match<T3>>;
1544 /// Match intrinsic calls like this:
1545 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1546 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1547 return IntrinsicID_match(IntrID);
1550 template <Intrinsic::ID IntrID, typename T0>
1551 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1552 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1555 template <Intrinsic::ID IntrID, typename T0, typename T1>
1556 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1558 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1561 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1562 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1563 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1564 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1567 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1569 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1570 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1571 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1574 // Helper intrinsic matching specializations.
1575 template <typename Opnd0>
1576 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1577 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1580 template <typename Opnd0>
1581 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1582 return m_Intrinsic<Intrinsic::bswap>(Op0);
1585 template <typename Opnd0>
1586 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
1587 return m_Intrinsic<Intrinsic::fabs>(Op0);
1590 template <typename Opnd0>
1591 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
1592 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
1595 template <typename Opnd0, typename Opnd1>
1596 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1598 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1601 template <typename Opnd0, typename Opnd1>
1602 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1604 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1607 //===----------------------------------------------------------------------===//
1608 // Matchers for two-operands operators with the operators in either order
1611 /// Matches a BinaryOperator with LHS and RHS in either order.
1612 template <typename LHS, typename RHS>
1613 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
1614 return AnyBinaryOp_match<LHS, RHS, true>(L, R);
1617 /// Matches an ICmp with a predicate over LHS and RHS in either order.
1618 /// Does not swap the predicate.
1619 template <typename LHS, typename RHS>
1620 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
1621 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1622 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
1626 /// Matches a Add with LHS and RHS in either order.
1627 template <typename LHS, typename RHS>
1628 inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
1630 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
1633 /// Matches a Mul with LHS and RHS in either order.
1634 template <typename LHS, typename RHS>
1635 inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
1637 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
1640 /// Matches an And with LHS and RHS in either order.
1641 template <typename LHS, typename RHS>
1642 inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
1644 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
1647 /// Matches an Or with LHS and RHS in either order.
1648 template <typename LHS, typename RHS>
1649 inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
1651 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
1654 /// Matches an Xor with LHS and RHS in either order.
1655 template <typename LHS, typename RHS>
1656 inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
1658 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
1661 /// Matches a 'Neg' as 'sub 0, V'.
1662 template <typename ValTy>
1663 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
1664 m_Neg(const ValTy &V) {
1665 return m_Sub(m_ZeroInt(), V);
1668 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1669 template <typename ValTy>
1670 inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1671 m_Not(const ValTy &V) {
1672 return m_c_Xor(V, m_AllOnes());
1675 /// Matches an SMin with LHS and RHS in either order.
1676 template <typename LHS, typename RHS>
1677 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
1678 m_c_SMin(const LHS &L, const RHS &R) {
1679 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
1681 /// Matches an SMax with LHS and RHS in either order.
1682 template <typename LHS, typename RHS>
1683 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
1684 m_c_SMax(const LHS &L, const RHS &R) {
1685 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
1687 /// Matches a UMin with LHS and RHS in either order.
1688 template <typename LHS, typename RHS>
1689 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
1690 m_c_UMin(const LHS &L, const RHS &R) {
1691 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
1693 /// Matches a UMax with LHS and RHS in either order.
1694 template <typename LHS, typename RHS>
1695 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
1696 m_c_UMax(const LHS &L, const RHS &R) {
1697 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
1700 /// Matches FAdd with LHS and RHS in either order.
1701 template <typename LHS, typename RHS>
1702 inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
1703 m_c_FAdd(const LHS &L, const RHS &R) {
1704 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
1707 /// Matches FMul with LHS and RHS in either order.
1708 template <typename LHS, typename RHS>
1709 inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
1710 m_c_FMul(const LHS &L, const RHS &R) {
1711 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
1714 template <typename Opnd_t> struct Signum_match {
1716 Signum_match(const Opnd_t &V) : Val(V) {}
1718 template <typename OpTy> bool match(OpTy *V) {
1719 unsigned TypeSize = V->getType()->getScalarSizeInBits();
1723 unsigned ShiftWidth = TypeSize - 1;
1724 Value *OpL = nullptr, *OpR = nullptr;
1726 // This is the representation of signum we match:
1728 // signum(x) == (x >> 63) | (-x >>u 63)
1730 // An i1 value is its own signum, so it's correct to match
1732 // signum(x) == (x >> 0) | (-x >>u 0)
1736 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
1737 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
1738 auto Signum = m_Or(LHS, RHS);
1740 return Signum.match(V) && OpL == OpR && Val.match(OpL);
1744 /// Matches a signum pattern.
1750 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1751 return Signum_match<Val_t>(V);
1754 } // end namespace PatternMatch
1755 } // end namespace llvm
1757 #endif // LLVM_IR_PATTERNMATCH_H