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/IR/CallSite.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/Operator.h"
39 namespace PatternMatch {
41 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
42 return const_cast<Pattern &>(P).match(V);
45 template <typename SubPattern_t> struct OneUse_match {
46 SubPattern_t SubPattern;
48 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
50 template <typename OpTy> bool match(OpTy *V) {
51 return V->hasOneUse() && SubPattern.match(V);
55 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
59 template <typename Class> struct class_match {
60 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
63 /// \brief Match an arbitrary value and ignore it.
64 inline class_match<Value> m_Value() { return class_match<Value>(); }
66 /// \brief Match an arbitrary binary operation and ignore it.
67 inline class_match<BinaryOperator> m_BinOp() {
68 return class_match<BinaryOperator>();
71 /// \brief Matches any compare instruction and ignore it.
72 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
74 /// \brief Match an arbitrary ConstantInt and ignore it.
75 inline class_match<ConstantInt> m_ConstantInt() {
76 return class_match<ConstantInt>();
79 /// \brief Match an arbitrary undef constant.
80 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
82 /// \brief Match an arbitrary Constant and ignore it.
83 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
85 /// Matching combinators
86 template <typename LTy, typename RTy> struct match_combine_or {
90 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
92 template <typename ITy> bool match(ITy *V) {
101 template <typename LTy, typename RTy> struct match_combine_and {
105 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
107 template <typename ITy> bool match(ITy *V) {
115 /// Combine two pattern matchers matching L || R
116 template <typename LTy, typename RTy>
117 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
118 return match_combine_or<LTy, RTy>(L, R);
121 /// Combine two pattern matchers matching L && R
122 template <typename LTy, typename RTy>
123 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
124 return match_combine_and<LTy, RTy>(L, R);
128 template <typename ITy> bool match(ITy *V) {
129 if (const auto *C = dyn_cast<Constant>(V))
130 return C->isNullValue();
135 /// \brief Match an arbitrary zero/null constant. This includes
136 /// zero_initializer for vectors and ConstantPointerNull for pointers.
137 inline match_zero m_Zero() { return match_zero(); }
139 struct match_neg_zero {
140 template <typename ITy> bool match(ITy *V) {
141 if (const auto *C = dyn_cast<Constant>(V))
142 return C->isNegativeZeroValue();
147 /// \brief Match an arbitrary zero/null constant. This includes
148 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
149 /// floating point constants, this will match negative zero but not positive
151 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
153 /// \brief - Match an arbitrary zero/null constant. This includes
154 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
155 /// floating point constants, this will match negative zero and positive zero
156 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
157 return m_CombineOr(m_Zero(), m_NegZero());
162 apint_match(const APInt *&R) : Res(R) {}
163 template <typename ITy> bool match(ITy *V) {
164 if (auto *CI = dyn_cast<ConstantInt>(V)) {
165 Res = &CI->getValue();
168 if (V->getType()->isVectorTy())
169 if (const auto *C = dyn_cast<Constant>(V))
170 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
171 Res = &CI->getValue();
178 /// \brief Match a ConstantInt or splatted ConstantVector, binding the
179 /// specified pointer to the contained APInt.
180 inline apint_match m_APInt(const APInt *&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 /// \brief Match a ConstantInt with a specific value.
198 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
199 return constantint_match<Val>();
202 /// \brief This helper class is used to match scalar and vector constants that
203 /// satisfy a specified predicate.
204 template <typename Predicate> struct cst_pred_ty : public Predicate {
205 template <typename ITy> bool match(ITy *V) {
206 if (const auto *CI = dyn_cast<ConstantInt>(V))
207 return this->isValue(CI->getValue());
208 if (V->getType()->isVectorTy())
209 if (const auto *C = dyn_cast<Constant>(V))
210 if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
211 return this->isValue(CI->getValue());
216 /// \brief This helper class is used to match scalar and vector constants that
217 /// satisfy a specified predicate, and bind them to an APInt.
218 template <typename Predicate> struct api_pred_ty : public Predicate {
220 api_pred_ty(const APInt *&R) : Res(R) {}
221 template <typename ITy> bool match(ITy *V) {
222 if (const auto *CI = dyn_cast<ConstantInt>(V))
223 if (this->isValue(CI->getValue())) {
224 Res = &CI->getValue();
227 if (V->getType()->isVectorTy())
228 if (const auto *C = dyn_cast<Constant>(V))
229 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
230 if (this->isValue(CI->getValue())) {
231 Res = &CI->getValue();
240 bool isValue(const APInt &C) { return C == 1; }
243 /// \brief Match an integer 1 or a vector with all elements equal to 1.
244 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
245 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
248 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
251 /// \brief Match an integer or vector with all bits set to true.
252 inline cst_pred_ty<is_all_ones> m_AllOnes() {
253 return cst_pred_ty<is_all_ones>();
255 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
258 bool isValue(const APInt &C) { return C.isSignBit(); }
261 /// \brief Match an integer or vector with only the sign bit(s) set.
262 inline cst_pred_ty<is_sign_bit> m_SignBit() {
263 return cst_pred_ty<is_sign_bit>();
265 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
268 bool isValue(const APInt &C) { return C.isPowerOf2(); }
271 /// \brief Match an integer or vector power of 2.
272 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
273 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
275 struct is_maxsignedvalue {
276 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
279 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { return cst_pred_ty<is_maxsignedvalue>(); }
280 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { return V; }
282 template <typename Class> struct bind_ty {
284 bind_ty(Class *&V) : VR(V) {}
286 template <typename ITy> bool match(ITy *V) {
287 if (auto *CV = dyn_cast<Class>(V)) {
295 /// \brief Match a value, capturing it if we match.
296 inline bind_ty<Value> m_Value(Value *&V) { return V; }
298 /// \brief Match an instruction, capturing it if we match.
299 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
301 /// \brief Match a binary operator, capturing it if we match.
302 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
304 /// \brief Match a ConstantInt, capturing the value if we match.
305 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
307 /// \brief Match a Constant, capturing the value if we match.
308 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
310 /// \brief Match a ConstantFP, capturing the value if we match.
311 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
313 /// \brief Match a specified Value*.
314 struct specificval_ty {
316 specificval_ty(const Value *V) : Val(V) {}
318 template <typename ITy> bool match(ITy *V) { return V == Val; }
321 /// \brief Match if we have a specific specified value.
322 inline specificval_ty m_Specific(const Value *V) { return V; }
324 /// \brief Match a specified floating point value or vector of all elements of
326 struct specific_fpval {
328 specific_fpval(double V) : Val(V) {}
330 template <typename ITy> bool match(ITy *V) {
331 if (const auto *CFP = dyn_cast<ConstantFP>(V))
332 return CFP->isExactlyValue(Val);
333 if (V->getType()->isVectorTy())
334 if (const auto *C = dyn_cast<Constant>(V))
335 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
336 return CFP->isExactlyValue(Val);
341 /// \brief Match a specific floating point value or vector with all elements
342 /// equal to the value.
343 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
345 /// \brief Match a float 1.0 or vector with all elements equal to 1.0.
346 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
348 struct bind_const_intval_ty {
350 bind_const_intval_ty(uint64_t &V) : VR(V) {}
352 template <typename ITy> bool match(ITy *V) {
353 if (const auto *CV = dyn_cast<ConstantInt>(V))
354 if (CV->getBitWidth() <= 64) {
355 VR = CV->getZExtValue();
362 /// \brief Match a specified integer value or vector of all elements of that
364 struct specific_intval {
366 specific_intval(uint64_t V) : Val(V) {}
368 template <typename ITy> bool match(ITy *V) {
369 const auto *CI = dyn_cast<ConstantInt>(V);
370 if (!CI && V->getType()->isVectorTy())
371 if (const auto *C = dyn_cast<Constant>(V))
372 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
374 if (CI && CI->getBitWidth() <= 64)
375 return CI->getZExtValue() == Val;
381 /// \brief Match a specific integer value or vector with all elements equal to
383 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
385 /// \brief Match a ConstantInt and bind to its value. This does not match
386 /// ConstantInts wider than 64-bits.
387 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
389 //===----------------------------------------------------------------------===//
390 // Matcher for any binary operator.
392 template <typename LHS_t, typename RHS_t> struct AnyBinaryOp_match {
396 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
398 template <typename OpTy> bool match(OpTy *V) {
399 if (auto *I = dyn_cast<BinaryOperator>(V))
400 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
405 template <typename LHS, typename RHS>
406 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
407 return AnyBinaryOp_match<LHS, RHS>(L, R);
410 //===----------------------------------------------------------------------===//
411 // Matchers for specific binary operators.
414 template <typename LHS_t, typename RHS_t, unsigned Opcode>
415 struct BinaryOp_match {
419 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
421 template <typename OpTy> bool match(OpTy *V) {
422 if (V->getValueID() == Value::InstructionVal + Opcode) {
423 auto *I = cast<BinaryOperator>(V);
424 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
426 if (auto *CE = dyn_cast<ConstantExpr>(V))
427 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
428 R.match(CE->getOperand(1));
433 template <typename LHS, typename RHS>
434 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
436 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
439 template <typename LHS, typename RHS>
440 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
442 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
445 template <typename LHS, typename RHS>
446 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
448 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
451 template <typename LHS, typename RHS>
452 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
454 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
457 template <typename LHS, typename RHS>
458 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
460 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
463 template <typename LHS, typename RHS>
464 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
466 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
469 template <typename LHS, typename RHS>
470 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
472 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
475 template <typename LHS, typename RHS>
476 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
478 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
481 template <typename LHS, typename RHS>
482 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
484 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
487 template <typename LHS, typename RHS>
488 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
490 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
493 template <typename LHS, typename RHS>
494 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
496 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
499 template <typename LHS, typename RHS>
500 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
502 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
505 template <typename LHS, typename RHS>
506 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
508 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
511 template <typename LHS, typename RHS>
512 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
514 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
517 template <typename LHS, typename RHS>
518 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
520 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
523 template <typename LHS, typename RHS>
524 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
526 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
529 template <typename LHS, typename RHS>
530 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
532 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
535 template <typename LHS, typename RHS>
536 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
538 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
541 template <typename LHS_t, typename RHS_t, unsigned Opcode,
542 unsigned WrapFlags = 0>
543 struct OverflowingBinaryOp_match {
547 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
550 template <typename OpTy> bool match(OpTy *V) {
551 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
552 if (Op->getOpcode() != Opcode)
554 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
555 !Op->hasNoUnsignedWrap())
557 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
558 !Op->hasNoSignedWrap())
560 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
566 template <typename LHS, typename RHS>
567 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
568 OverflowingBinaryOperator::NoSignedWrap>
569 m_NSWAdd(const LHS &L, const RHS &R) {
570 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
571 OverflowingBinaryOperator::NoSignedWrap>(
574 template <typename LHS, typename RHS>
575 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
576 OverflowingBinaryOperator::NoSignedWrap>
577 m_NSWSub(const LHS &L, const RHS &R) {
578 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
579 OverflowingBinaryOperator::NoSignedWrap>(
582 template <typename LHS, typename RHS>
583 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
584 OverflowingBinaryOperator::NoSignedWrap>
585 m_NSWMul(const LHS &L, const RHS &R) {
586 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
587 OverflowingBinaryOperator::NoSignedWrap>(
590 template <typename LHS, typename RHS>
591 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
592 OverflowingBinaryOperator::NoSignedWrap>
593 m_NSWShl(const LHS &L, const RHS &R) {
594 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
595 OverflowingBinaryOperator::NoSignedWrap>(
599 template <typename LHS, typename RHS>
600 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
601 OverflowingBinaryOperator::NoUnsignedWrap>
602 m_NUWAdd(const LHS &L, const RHS &R) {
603 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
604 OverflowingBinaryOperator::NoUnsignedWrap>(
607 template <typename LHS, typename RHS>
608 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
609 OverflowingBinaryOperator::NoUnsignedWrap>
610 m_NUWSub(const LHS &L, const RHS &R) {
611 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
612 OverflowingBinaryOperator::NoUnsignedWrap>(
615 template <typename LHS, typename RHS>
616 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
617 OverflowingBinaryOperator::NoUnsignedWrap>
618 m_NUWMul(const LHS &L, const RHS &R) {
619 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
620 OverflowingBinaryOperator::NoUnsignedWrap>(
623 template <typename LHS, typename RHS>
624 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
625 OverflowingBinaryOperator::NoUnsignedWrap>
626 m_NUWShl(const LHS &L, const RHS &R) {
627 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
628 OverflowingBinaryOperator::NoUnsignedWrap>(
632 //===----------------------------------------------------------------------===//
633 // Class that matches two different binary ops.
635 template <typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
636 struct BinOp2_match {
640 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
642 template <typename OpTy> bool match(OpTy *V) {
643 if (V->getValueID() == Value::InstructionVal + Opc1 ||
644 V->getValueID() == Value::InstructionVal + Opc2) {
645 auto *I = cast<BinaryOperator>(V);
646 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
648 if (auto *CE = dyn_cast<ConstantExpr>(V))
649 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
650 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
655 /// \brief Matches LShr or AShr.
656 template <typename LHS, typename RHS>
657 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
658 m_Shr(const LHS &L, const RHS &R) {
659 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
662 /// \brief Matches LShr or Shl.
663 template <typename LHS, typename RHS>
664 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
665 m_LogicalShift(const LHS &L, const RHS &R) {
666 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
669 /// \brief Matches UDiv and SDiv.
670 template <typename LHS, typename RHS>
671 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
672 m_IDiv(const LHS &L, const RHS &R) {
673 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
676 //===----------------------------------------------------------------------===//
677 // Class that matches exact binary ops.
679 template <typename SubPattern_t> struct Exact_match {
680 SubPattern_t SubPattern;
682 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
684 template <typename OpTy> bool match(OpTy *V) {
685 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
686 return PEO->isExact() && SubPattern.match(V);
691 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
695 //===----------------------------------------------------------------------===//
696 // Matchers for CmpInst classes
699 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
700 struct CmpClass_match {
701 PredicateTy &Predicate;
705 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
706 : Predicate(Pred), L(LHS), R(RHS) {}
708 template <typename OpTy> bool match(OpTy *V) {
709 if (Class *I = dyn_cast<Class>(V))
710 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
711 Predicate = I->getPredicate();
718 template <typename LHS, typename RHS>
719 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
720 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
721 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
724 template <typename LHS, typename RHS>
725 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
726 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
727 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
730 template <typename LHS, typename RHS>
731 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
732 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
733 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
736 //===----------------------------------------------------------------------===//
737 // Matchers for SelectInst classes
740 template <typename Cond_t, typename LHS_t, typename RHS_t>
741 struct SelectClass_match {
746 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
747 : C(Cond), L(LHS), R(RHS) {}
749 template <typename OpTy> bool match(OpTy *V) {
750 if (auto *I = dyn_cast<SelectInst>(V))
751 return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
752 R.match(I->getOperand(2));
757 template <typename Cond, typename LHS, typename RHS>
758 inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
760 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
763 /// \brief This matches a select of two constants, e.g.:
764 /// m_SelectCst<-1, 0>(m_Value(V))
765 template <int64_t L, int64_t R, typename Cond>
766 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R>>
767 m_SelectCst(const Cond &C) {
768 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
771 //===----------------------------------------------------------------------===//
772 // Matchers for CastInst classes
775 template <typename Op_t, unsigned Opcode> struct CastClass_match {
778 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
780 template <typename OpTy> bool match(OpTy *V) {
781 if (auto *O = dyn_cast<Operator>(V))
782 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
787 /// \brief Matches BitCast.
788 template <typename OpTy>
789 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
790 return CastClass_match<OpTy, Instruction::BitCast>(Op);
793 /// \brief Matches PtrToInt.
794 template <typename OpTy>
795 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
796 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
799 /// \brief Matches Trunc.
800 template <typename OpTy>
801 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
802 return CastClass_match<OpTy, Instruction::Trunc>(Op);
805 /// \brief Matches SExt.
806 template <typename OpTy>
807 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
808 return CastClass_match<OpTy, Instruction::SExt>(Op);
811 /// \brief Matches ZExt.
812 template <typename OpTy>
813 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
814 return CastClass_match<OpTy, Instruction::ZExt>(Op);
817 /// \brief Matches UIToFP.
818 template <typename OpTy>
819 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
820 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
823 /// \brief Matches SIToFP.
824 template <typename OpTy>
825 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
826 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
829 //===----------------------------------------------------------------------===//
830 // Matchers for unary operators
833 template <typename LHS_t> struct not_match {
836 not_match(const LHS_t &LHS) : L(LHS) {}
838 template <typename OpTy> bool match(OpTy *V) {
839 if (auto *O = dyn_cast<Operator>(V))
840 if (O->getOpcode() == Instruction::Xor)
841 return matchIfNot(O->getOperand(0), O->getOperand(1));
846 bool matchIfNot(Value *LHS, Value *RHS) {
847 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
849 isa<ConstantVector>(RHS)) &&
850 cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
854 template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }
856 template <typename LHS_t> struct neg_match {
859 neg_match(const LHS_t &LHS) : L(LHS) {}
861 template <typename OpTy> bool match(OpTy *V) {
862 if (auto *O = dyn_cast<Operator>(V))
863 if (O->getOpcode() == Instruction::Sub)
864 return matchIfNeg(O->getOperand(0), O->getOperand(1));
869 bool matchIfNeg(Value *LHS, Value *RHS) {
870 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
871 isa<ConstantAggregateZero>(LHS)) &&
876 /// \brief Match an integer negate.
877 template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
879 template <typename LHS_t> struct fneg_match {
882 fneg_match(const LHS_t &LHS) : L(LHS) {}
884 template <typename OpTy> bool match(OpTy *V) {
885 if (auto *O = dyn_cast<Operator>(V))
886 if (O->getOpcode() == Instruction::FSub)
887 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
892 bool matchIfFNeg(Value *LHS, Value *RHS) {
893 if (const auto *C = dyn_cast<ConstantFP>(LHS))
894 return C->isNegativeZeroValue() && L.match(RHS);
899 /// \brief Match a floating point negate.
900 template <typename LHS> inline fneg_match<LHS> m_FNeg(const LHS &L) {
904 //===----------------------------------------------------------------------===//
905 // Matchers for control flow.
910 br_match(BasicBlock *&Succ) : Succ(Succ) {}
912 template <typename OpTy> bool match(OpTy *V) {
913 if (auto *BI = dyn_cast<BranchInst>(V))
914 if (BI->isUnconditional()) {
915 Succ = BI->getSuccessor(0);
922 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
924 template <typename Cond_t> struct brc_match {
927 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
928 : Cond(C), T(t), F(f) {}
930 template <typename OpTy> bool match(OpTy *V) {
931 if (auto *BI = dyn_cast<BranchInst>(V))
932 if (BI->isConditional() && Cond.match(BI->getCondition())) {
933 T = BI->getSuccessor(0);
934 F = BI->getSuccessor(1);
941 template <typename Cond_t>
942 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
943 return brc_match<Cond_t>(C, T, F);
946 //===----------------------------------------------------------------------===//
947 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
950 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
951 struct MaxMin_match {
955 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
957 template <typename OpTy> bool match(OpTy *V) {
958 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
959 auto *SI = dyn_cast<SelectInst>(V);
962 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
965 // At this point we have a select conditioned on a comparison. Check that
966 // it is the values returned by the select that are being compared.
967 Value *TrueVal = SI->getTrueValue();
968 Value *FalseVal = SI->getFalseValue();
969 Value *LHS = Cmp->getOperand(0);
970 Value *RHS = Cmp->getOperand(1);
971 if ((TrueVal != LHS || FalseVal != RHS) &&
972 (TrueVal != RHS || FalseVal != LHS))
974 typename CmpInst_t::Predicate Pred =
975 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate();
976 // Does "(x pred y) ? x : y" represent the desired max/min operation?
977 if (!Pred_t::match(Pred))
979 // It does! Bind the operands.
980 return L.match(LHS) && R.match(RHS);
984 /// \brief Helper class for identifying signed max predicates.
985 struct smax_pred_ty {
986 static bool match(ICmpInst::Predicate Pred) {
987 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
991 /// \brief Helper class for identifying signed min predicates.
992 struct smin_pred_ty {
993 static bool match(ICmpInst::Predicate Pred) {
994 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
998 /// \brief Helper class for identifying unsigned max predicates.
999 struct umax_pred_ty {
1000 static bool match(ICmpInst::Predicate Pred) {
1001 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1005 /// \brief Helper class for identifying unsigned min predicates.
1006 struct umin_pred_ty {
1007 static bool match(ICmpInst::Predicate Pred) {
1008 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1012 /// \brief Helper class for identifying ordered max predicates.
1013 struct ofmax_pred_ty {
1014 static bool match(FCmpInst::Predicate Pred) {
1015 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1019 /// \brief Helper class for identifying ordered min predicates.
1020 struct ofmin_pred_ty {
1021 static bool match(FCmpInst::Predicate Pred) {
1022 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1026 /// \brief Helper class for identifying unordered max predicates.
1027 struct ufmax_pred_ty {
1028 static bool match(FCmpInst::Predicate Pred) {
1029 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1033 /// \brief Helper class for identifying unordered min predicates.
1034 struct ufmin_pred_ty {
1035 static bool match(FCmpInst::Predicate Pred) {
1036 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1040 template <typename LHS, typename RHS>
1041 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1043 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1046 template <typename LHS, typename RHS>
1047 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1049 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1052 template <typename LHS, typename RHS>
1053 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1055 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1058 template <typename LHS, typename RHS>
1059 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1061 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1064 /// \brief Match an 'ordered' floating point maximum function.
1065 /// Floating point has one special value 'NaN'. Therefore, there is no total
1066 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1067 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1068 /// semantics. In the presence of 'NaN' we have to preserve the original
1069 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1071 /// max(L, R) iff L and R are not NaN
1072 /// m_OrdFMax(L, R) = R iff L or R are NaN
1073 template <typename LHS, typename RHS>
1074 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1076 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1079 /// \brief Match an 'ordered' floating point minimum function.
1080 /// Floating point has one special value 'NaN'. Therefore, there is no total
1081 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1082 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1083 /// semantics. In the presence of 'NaN' we have to preserve the original
1084 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1086 /// max(L, R) iff L and R are not NaN
1087 /// m_OrdFMin(L, R) = R iff L or R are NaN
1088 template <typename LHS, typename RHS>
1089 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1091 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1094 /// \brief Match an 'unordered' floating point maximum function.
1095 /// Floating point has one special value 'NaN'. Therefore, there is no total
1096 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1097 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1098 /// semantics. In the presence of 'NaN' we have to preserve the original
1099 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1101 /// max(L, R) iff L and R are not NaN
1102 /// m_UnordFMin(L, R) = L iff L or R are NaN
1103 template <typename LHS, typename RHS>
1104 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1105 m_UnordFMax(const LHS &L, const RHS &R) {
1106 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1109 //===----------------------------------------------------------------------===//
1110 // Matchers for overflow check patterns: e.g. (a + b) u< a
1113 template <typename LHS_t, typename RHS_t, typename Sum_t>
1114 struct UAddWithOverflow_match {
1119 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1120 : L(L), R(R), S(S) {}
1122 template <typename OpTy> bool match(OpTy *V) {
1123 Value *ICmpLHS, *ICmpRHS;
1124 ICmpInst::Predicate Pred;
1125 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1128 Value *AddLHS, *AddRHS;
1129 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1131 // (a + b) u< a, (a + b) u< b
1132 if (Pred == ICmpInst::ICMP_ULT)
1133 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1134 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1136 // a >u (a + b), b >u (a + b)
1137 if (Pred == ICmpInst::ICMP_UGT)
1138 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1139 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1145 /// \brief Match an icmp instruction checking for unsigned overflow on addition.
1147 /// S is matched to the addition whose result is being checked for overflow, and
1148 /// L and R are matched to the LHS and RHS of S.
1149 template <typename LHS_t, typename RHS_t, typename Sum_t>
1150 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
1151 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1152 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
1155 /// \brief Match an 'unordered' floating point minimum function.
1156 /// Floating point has one special value 'NaN'. Therefore, there is no total
1157 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1158 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1159 /// semantics. In the presence of 'NaN' we have to preserve the original
1160 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1162 /// max(L, R) iff L and R are not NaN
1163 /// m_UnordFMin(L, R) = L iff L or R are NaN
1164 template <typename LHS, typename RHS>
1165 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1166 m_UnordFMin(const LHS &L, const RHS &R) {
1167 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1170 template <typename Opnd_t> struct Argument_match {
1173 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1175 template <typename OpTy> bool match(OpTy *V) {
1177 return CS.isCall() && Val.match(CS.getArgument(OpI));
1181 /// \brief Match an argument.
1182 template <unsigned OpI, typename Opnd_t>
1183 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1184 return Argument_match<Opnd_t>(OpI, Op);
1187 /// \brief Intrinsic matchers.
1188 struct IntrinsicID_match {
1190 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1192 template <typename OpTy> bool match(OpTy *V) {
1193 if (const auto *CI = dyn_cast<CallInst>(V))
1194 if (const auto *F = CI->getCalledFunction())
1195 return F->getIntrinsicID() == ID;
1200 /// Intrinsic matches are combinations of ID matchers, and argument
1201 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1202 /// them with lower arity matchers. Here's some convenient typedefs for up to
1203 /// several arguments, and more can be added as needed
1204 template <typename T0 = void, typename T1 = void, typename T2 = void,
1205 typename T3 = void, typename T4 = void, typename T5 = void,
1206 typename T6 = void, typename T7 = void, typename T8 = void,
1207 typename T9 = void, typename T10 = void>
1208 struct m_Intrinsic_Ty;
1209 template <typename T0> struct m_Intrinsic_Ty<T0> {
1210 typedef match_combine_and<IntrinsicID_match, Argument_match<T0>> Ty;
1212 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1213 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>
1216 template <typename T0, typename T1, typename T2>
1217 struct m_Intrinsic_Ty<T0, T1, T2> {
1218 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1219 Argument_match<T2>> Ty;
1221 template <typename T0, typename T1, typename T2, typename T3>
1222 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1223 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1224 Argument_match<T3>> Ty;
1227 /// \brief Match intrinsic calls like this:
1228 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1229 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1230 return IntrinsicID_match(IntrID);
1233 template <Intrinsic::ID IntrID, typename T0>
1234 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1235 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1238 template <Intrinsic::ID IntrID, typename T0, typename T1>
1239 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1241 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1244 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1245 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1246 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1247 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1250 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1252 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1253 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1254 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1257 // Helper intrinsic matching specializations.
1258 template <typename Opnd0>
1259 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1260 return m_Intrinsic<Intrinsic::bswap>(Op0);
1263 template <typename Opnd0, typename Opnd1>
1264 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1266 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1269 template <typename Opnd0, typename Opnd1>
1270 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1272 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1275 } // end namespace PatternMatch
1276 } // end namespace llvm