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 template <typename Class> struct bind_ty {
277 bind_ty(Class *&V) : VR(V) {}
279 template <typename ITy> bool match(ITy *V) {
280 if (auto *CV = dyn_cast<Class>(V)) {
288 /// \brief Match a value, capturing it if we match.
289 inline bind_ty<Value> m_Value(Value *&V) { return V; }
291 /// \brief Match a binary operator, capturing it if we match.
292 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
294 /// \brief Match a ConstantInt, capturing the value if we match.
295 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
297 /// \brief Match a Constant, capturing the value if we match.
298 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
300 /// \brief Match a ConstantFP, capturing the value if we match.
301 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
303 /// \brief Match a specified Value*.
304 struct specificval_ty {
306 specificval_ty(const Value *V) : Val(V) {}
308 template <typename ITy> bool match(ITy *V) { return V == Val; }
311 /// \brief Match if we have a specific specified value.
312 inline specificval_ty m_Specific(const Value *V) { return V; }
314 /// \brief Match a specified floating point value or vector of all elements of
316 struct specific_fpval {
318 specific_fpval(double V) : Val(V) {}
320 template <typename ITy> bool match(ITy *V) {
321 if (const auto *CFP = dyn_cast<ConstantFP>(V))
322 return CFP->isExactlyValue(Val);
323 if (V->getType()->isVectorTy())
324 if (const auto *C = dyn_cast<Constant>(V))
325 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
326 return CFP->isExactlyValue(Val);
331 /// \brief Match a specific floating point value or vector with all elements
332 /// equal to the value.
333 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
335 /// \brief Match a float 1.0 or vector with all elements equal to 1.0.
336 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
338 struct bind_const_intval_ty {
340 bind_const_intval_ty(uint64_t &V) : VR(V) {}
342 template <typename ITy> bool match(ITy *V) {
343 if (const auto *CV = dyn_cast<ConstantInt>(V))
344 if (CV->getBitWidth() <= 64) {
345 VR = CV->getZExtValue();
352 /// \brief Match a specified integer value or vector of all elements of that
354 struct specific_intval {
356 specific_intval(uint64_t V) : Val(V) {}
358 template <typename ITy> bool match(ITy *V) {
359 const auto *CI = dyn_cast<ConstantInt>(V);
360 if (!CI && V->getType()->isVectorTy())
361 if (const auto *C = dyn_cast<Constant>(V))
362 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
364 if (CI && CI->getBitWidth() <= 64)
365 return CI->getZExtValue() == Val;
371 /// \brief Match a specific integer value or vector with all elements equal to
373 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
375 /// \brief Match a ConstantInt and bind to its value. This does not match
376 /// ConstantInts wider than 64-bits.
377 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
379 //===----------------------------------------------------------------------===//
380 // Matcher for any binary operator.
382 template <typename LHS_t, typename RHS_t> struct AnyBinaryOp_match {
386 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
388 template <typename OpTy> bool match(OpTy *V) {
389 if (auto *I = dyn_cast<BinaryOperator>(V))
390 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
395 template <typename LHS, typename RHS>
396 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
397 return AnyBinaryOp_match<LHS, RHS>(L, R);
400 //===----------------------------------------------------------------------===//
401 // Matchers for specific binary operators.
404 template <typename LHS_t, typename RHS_t, unsigned Opcode>
405 struct BinaryOp_match {
409 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
411 template <typename OpTy> bool match(OpTy *V) {
412 if (V->getValueID() == Value::InstructionVal + Opcode) {
413 auto *I = cast<BinaryOperator>(V);
414 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
416 if (auto *CE = dyn_cast<ConstantExpr>(V))
417 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
418 R.match(CE->getOperand(1));
423 template <typename LHS, typename RHS>
424 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
426 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
429 template <typename LHS, typename RHS>
430 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
432 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
435 template <typename LHS, typename RHS>
436 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
438 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
441 template <typename LHS, typename RHS>
442 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
444 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
447 template <typename LHS, typename RHS>
448 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
450 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
453 template <typename LHS, typename RHS>
454 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
456 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
459 template <typename LHS, typename RHS>
460 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
462 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
465 template <typename LHS, typename RHS>
466 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
468 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
471 template <typename LHS, typename RHS>
472 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
474 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
477 template <typename LHS, typename RHS>
478 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
480 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
483 template <typename LHS, typename RHS>
484 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
486 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
489 template <typename LHS, typename RHS>
490 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
492 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
495 template <typename LHS, typename RHS>
496 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
498 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
501 template <typename LHS, typename RHS>
502 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
504 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
507 template <typename LHS, typename RHS>
508 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
510 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
513 template <typename LHS, typename RHS>
514 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
516 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
519 template <typename LHS, typename RHS>
520 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
522 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
525 template <typename LHS, typename RHS>
526 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
528 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
531 template <typename LHS_t, typename RHS_t, unsigned Opcode,
532 unsigned WrapFlags = 0>
533 struct OverflowingBinaryOp_match {
537 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
540 template <typename OpTy> bool match(OpTy *V) {
541 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
542 if (Op->getOpcode() != Opcode)
544 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
545 !Op->hasNoUnsignedWrap())
547 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
548 !Op->hasNoSignedWrap())
550 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
556 template <typename LHS, typename RHS>
557 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
558 OverflowingBinaryOperator::NoSignedWrap>
559 m_NSWAdd(const LHS &L, const RHS &R) {
560 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
561 OverflowingBinaryOperator::NoSignedWrap>(
564 template <typename LHS, typename RHS>
565 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
566 OverflowingBinaryOperator::NoSignedWrap>
567 m_NSWSub(const LHS &L, const RHS &R) {
568 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
569 OverflowingBinaryOperator::NoSignedWrap>(
572 template <typename LHS, typename RHS>
573 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
574 OverflowingBinaryOperator::NoSignedWrap>
575 m_NSWMul(const LHS &L, const RHS &R) {
576 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
577 OverflowingBinaryOperator::NoSignedWrap>(
580 template <typename LHS, typename RHS>
581 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
582 OverflowingBinaryOperator::NoSignedWrap>
583 m_NSWShl(const LHS &L, const RHS &R) {
584 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
585 OverflowingBinaryOperator::NoSignedWrap>(
589 template <typename LHS, typename RHS>
590 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
591 OverflowingBinaryOperator::NoUnsignedWrap>
592 m_NUWAdd(const LHS &L, const RHS &R) {
593 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
594 OverflowingBinaryOperator::NoUnsignedWrap>(
597 template <typename LHS, typename RHS>
598 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
599 OverflowingBinaryOperator::NoUnsignedWrap>
600 m_NUWSub(const LHS &L, const RHS &R) {
601 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
602 OverflowingBinaryOperator::NoUnsignedWrap>(
605 template <typename LHS, typename RHS>
606 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
607 OverflowingBinaryOperator::NoUnsignedWrap>
608 m_NUWMul(const LHS &L, const RHS &R) {
609 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
610 OverflowingBinaryOperator::NoUnsignedWrap>(
613 template <typename LHS, typename RHS>
614 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
615 OverflowingBinaryOperator::NoUnsignedWrap>
616 m_NUWShl(const LHS &L, const RHS &R) {
617 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
618 OverflowingBinaryOperator::NoUnsignedWrap>(
622 //===----------------------------------------------------------------------===//
623 // Class that matches two different binary ops.
625 template <typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
626 struct BinOp2_match {
630 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
632 template <typename OpTy> bool match(OpTy *V) {
633 if (V->getValueID() == Value::InstructionVal + Opc1 ||
634 V->getValueID() == Value::InstructionVal + Opc2) {
635 auto *I = cast<BinaryOperator>(V);
636 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
638 if (auto *CE = dyn_cast<ConstantExpr>(V))
639 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
640 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
645 /// \brief Matches LShr or AShr.
646 template <typename LHS, typename RHS>
647 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
648 m_Shr(const LHS &L, const RHS &R) {
649 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
652 /// \brief Matches LShr or Shl.
653 template <typename LHS, typename RHS>
654 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
655 m_LogicalShift(const LHS &L, const RHS &R) {
656 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
659 /// \brief Matches UDiv and SDiv.
660 template <typename LHS, typename RHS>
661 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
662 m_IDiv(const LHS &L, const RHS &R) {
663 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
666 //===----------------------------------------------------------------------===//
667 // Class that matches exact binary ops.
669 template <typename SubPattern_t> struct Exact_match {
670 SubPattern_t SubPattern;
672 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
674 template <typename OpTy> bool match(OpTy *V) {
675 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
676 return PEO->isExact() && SubPattern.match(V);
681 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
685 //===----------------------------------------------------------------------===//
686 // Matchers for CmpInst classes
689 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
690 struct CmpClass_match {
691 PredicateTy &Predicate;
695 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
696 : Predicate(Pred), L(LHS), R(RHS) {}
698 template <typename OpTy> bool match(OpTy *V) {
699 if (Class *I = dyn_cast<Class>(V))
700 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
701 Predicate = I->getPredicate();
708 template <typename LHS, typename RHS>
709 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
710 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
711 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
714 template <typename LHS, typename RHS>
715 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
716 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
717 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
720 template <typename LHS, typename RHS>
721 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
722 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
723 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
726 //===----------------------------------------------------------------------===//
727 // Matchers for SelectInst classes
730 template <typename Cond_t, typename LHS_t, typename RHS_t>
731 struct SelectClass_match {
736 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
737 : C(Cond), L(LHS), R(RHS) {}
739 template <typename OpTy> bool match(OpTy *V) {
740 if (auto *I = dyn_cast<SelectInst>(V))
741 return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
742 R.match(I->getOperand(2));
747 template <typename Cond, typename LHS, typename RHS>
748 inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
750 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
753 /// \brief This matches a select of two constants, e.g.:
754 /// m_SelectCst<-1, 0>(m_Value(V))
755 template <int64_t L, int64_t R, typename Cond>
756 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R>>
757 m_SelectCst(const Cond &C) {
758 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
761 //===----------------------------------------------------------------------===//
762 // Matchers for CastInst classes
765 template <typename Op_t, unsigned Opcode> struct CastClass_match {
768 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
770 template <typename OpTy> bool match(OpTy *V) {
771 if (auto *O = dyn_cast<Operator>(V))
772 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
777 /// \brief Matches BitCast.
778 template <typename OpTy>
779 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
780 return CastClass_match<OpTy, Instruction::BitCast>(Op);
783 /// \brief Matches PtrToInt.
784 template <typename OpTy>
785 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
786 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
789 /// \brief Matches Trunc.
790 template <typename OpTy>
791 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
792 return CastClass_match<OpTy, Instruction::Trunc>(Op);
795 /// \brief Matches SExt.
796 template <typename OpTy>
797 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
798 return CastClass_match<OpTy, Instruction::SExt>(Op);
801 /// \brief Matches ZExt.
802 template <typename OpTy>
803 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
804 return CastClass_match<OpTy, Instruction::ZExt>(Op);
807 /// \brief Matches UIToFP.
808 template <typename OpTy>
809 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
810 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
813 /// \brief Matches SIToFP.
814 template <typename OpTy>
815 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
816 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
819 //===----------------------------------------------------------------------===//
820 // Matchers for unary operators
823 template <typename LHS_t> struct not_match {
826 not_match(const LHS_t &LHS) : L(LHS) {}
828 template <typename OpTy> bool match(OpTy *V) {
829 if (auto *O = dyn_cast<Operator>(V))
830 if (O->getOpcode() == Instruction::Xor)
831 return matchIfNot(O->getOperand(0), O->getOperand(1));
836 bool matchIfNot(Value *LHS, Value *RHS) {
837 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
839 isa<ConstantVector>(RHS)) &&
840 cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
844 template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }
846 template <typename LHS_t> struct neg_match {
849 neg_match(const LHS_t &LHS) : L(LHS) {}
851 template <typename OpTy> bool match(OpTy *V) {
852 if (auto *O = dyn_cast<Operator>(V))
853 if (O->getOpcode() == Instruction::Sub)
854 return matchIfNeg(O->getOperand(0), O->getOperand(1));
859 bool matchIfNeg(Value *LHS, Value *RHS) {
860 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
861 isa<ConstantAggregateZero>(LHS)) &&
866 /// \brief Match an integer negate.
867 template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
869 template <typename LHS_t> struct fneg_match {
872 fneg_match(const LHS_t &LHS) : L(LHS) {}
874 template <typename OpTy> bool match(OpTy *V) {
875 if (auto *O = dyn_cast<Operator>(V))
876 if (O->getOpcode() == Instruction::FSub)
877 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
882 bool matchIfFNeg(Value *LHS, Value *RHS) {
883 if (const auto *C = dyn_cast<ConstantFP>(LHS))
884 return C->isNegativeZeroValue() && L.match(RHS);
889 /// \brief Match a floating point negate.
890 template <typename LHS> inline fneg_match<LHS> m_FNeg(const LHS &L) {
894 //===----------------------------------------------------------------------===//
895 // Matchers for control flow.
900 br_match(BasicBlock *&Succ) : Succ(Succ) {}
902 template <typename OpTy> bool match(OpTy *V) {
903 if (auto *BI = dyn_cast<BranchInst>(V))
904 if (BI->isUnconditional()) {
905 Succ = BI->getSuccessor(0);
912 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
914 template <typename Cond_t> struct brc_match {
917 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
918 : Cond(C), T(t), F(f) {}
920 template <typename OpTy> bool match(OpTy *V) {
921 if (auto *BI = dyn_cast<BranchInst>(V))
922 if (BI->isConditional() && Cond.match(BI->getCondition())) {
923 T = BI->getSuccessor(0);
924 F = BI->getSuccessor(1);
931 template <typename Cond_t>
932 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
933 return brc_match<Cond_t>(C, T, F);
936 //===----------------------------------------------------------------------===//
937 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
940 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
941 struct MaxMin_match {
945 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
947 template <typename OpTy> bool match(OpTy *V) {
948 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
949 auto *SI = dyn_cast<SelectInst>(V);
952 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
955 // At this point we have a select conditioned on a comparison. Check that
956 // it is the values returned by the select that are being compared.
957 Value *TrueVal = SI->getTrueValue();
958 Value *FalseVal = SI->getFalseValue();
959 Value *LHS = Cmp->getOperand(0);
960 Value *RHS = Cmp->getOperand(1);
961 if ((TrueVal != LHS || FalseVal != RHS) &&
962 (TrueVal != RHS || FalseVal != LHS))
964 typename CmpInst_t::Predicate Pred =
965 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate();
966 // Does "(x pred y) ? x : y" represent the desired max/min operation?
967 if (!Pred_t::match(Pred))
969 // It does! Bind the operands.
970 return L.match(LHS) && R.match(RHS);
974 /// \brief Helper class for identifying signed max predicates.
975 struct smax_pred_ty {
976 static bool match(ICmpInst::Predicate Pred) {
977 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
981 /// \brief Helper class for identifying signed min predicates.
982 struct smin_pred_ty {
983 static bool match(ICmpInst::Predicate Pred) {
984 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
988 /// \brief Helper class for identifying unsigned max predicates.
989 struct umax_pred_ty {
990 static bool match(ICmpInst::Predicate Pred) {
991 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
995 /// \brief Helper class for identifying unsigned min predicates.
996 struct umin_pred_ty {
997 static bool match(ICmpInst::Predicate Pred) {
998 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1002 /// \brief Helper class for identifying ordered max predicates.
1003 struct ofmax_pred_ty {
1004 static bool match(FCmpInst::Predicate Pred) {
1005 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1009 /// \brief Helper class for identifying ordered min predicates.
1010 struct ofmin_pred_ty {
1011 static bool match(FCmpInst::Predicate Pred) {
1012 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1016 /// \brief Helper class for identifying unordered max predicates.
1017 struct ufmax_pred_ty {
1018 static bool match(FCmpInst::Predicate Pred) {
1019 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1023 /// \brief Helper class for identifying unordered min predicates.
1024 struct ufmin_pred_ty {
1025 static bool match(FCmpInst::Predicate Pred) {
1026 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1030 template <typename LHS, typename RHS>
1031 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1033 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1036 template <typename LHS, typename RHS>
1037 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1039 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1042 template <typename LHS, typename RHS>
1043 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1045 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1048 template <typename LHS, typename RHS>
1049 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1051 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1054 /// \brief Match an 'ordered' floating point maximum function.
1055 /// Floating point has one special value 'NaN'. Therefore, there is no total
1056 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1057 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1058 /// semantics. In the presence of 'NaN' we have to preserve the original
1059 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1061 /// max(L, R) iff L and R are not NaN
1062 /// m_OrdFMax(L, R) = R iff L or R are NaN
1063 template <typename LHS, typename RHS>
1064 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1066 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1069 /// \brief Match an 'ordered' floating point minimum function.
1070 /// Floating point has one special value 'NaN'. Therefore, there is no total
1071 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1072 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1073 /// semantics. In the presence of 'NaN' we have to preserve the original
1074 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1076 /// max(L, R) iff L and R are not NaN
1077 /// m_OrdFMin(L, R) = R iff L or R are NaN
1078 template <typename LHS, typename RHS>
1079 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1081 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1084 /// \brief Match an 'unordered' floating point maximum function.
1085 /// Floating point has one special value 'NaN'. Therefore, there is no total
1086 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1087 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1088 /// semantics. In the presence of 'NaN' we have to preserve the original
1089 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1091 /// max(L, R) iff L and R are not NaN
1092 /// m_UnordFMin(L, R) = L iff L or R are NaN
1093 template <typename LHS, typename RHS>
1094 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1095 m_UnordFMax(const LHS &L, const RHS &R) {
1096 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1099 /// \brief Match an 'unordered' floating point minimum function.
1100 /// Floating point has one special value 'NaN'. Therefore, there is no total
1101 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1102 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1103 /// semantics. In the presence of 'NaN' we have to preserve the original
1104 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1106 /// max(L, R) iff L and R are not NaN
1107 /// m_UnordFMin(L, R) = L iff L or R are NaN
1108 template <typename LHS, typename RHS>
1109 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1110 m_UnordFMin(const LHS &L, const RHS &R) {
1111 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1114 template <typename Opnd_t> struct Argument_match {
1117 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1119 template <typename OpTy> bool match(OpTy *V) {
1121 return CS.isCall() && Val.match(CS.getArgument(OpI));
1125 /// \brief Match an argument.
1126 template <unsigned OpI, typename Opnd_t>
1127 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1128 return Argument_match<Opnd_t>(OpI, Op);
1131 /// \brief Intrinsic matchers.
1132 struct IntrinsicID_match {
1134 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1136 template <typename OpTy> bool match(OpTy *V) {
1137 if (const auto *CI = dyn_cast<CallInst>(V))
1138 if (const auto *F = CI->getCalledFunction())
1139 return F->getIntrinsicID() == ID;
1144 /// Intrinsic matches are combinations of ID matchers, and argument
1145 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1146 /// them with lower arity matchers. Here's some convenient typedefs for up to
1147 /// several arguments, and more can be added as needed
1148 template <typename T0 = void, typename T1 = void, typename T2 = void,
1149 typename T3 = void, typename T4 = void, typename T5 = void,
1150 typename T6 = void, typename T7 = void, typename T8 = void,
1151 typename T9 = void, typename T10 = void>
1152 struct m_Intrinsic_Ty;
1153 template <typename T0> struct m_Intrinsic_Ty<T0> {
1154 typedef match_combine_and<IntrinsicID_match, Argument_match<T0>> Ty;
1156 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1157 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>
1160 template <typename T0, typename T1, typename T2>
1161 struct m_Intrinsic_Ty<T0, T1, T2> {
1162 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1163 Argument_match<T2>> Ty;
1165 template <typename T0, typename T1, typename T2, typename T3>
1166 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1167 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1168 Argument_match<T3>> Ty;
1171 /// \brief Match intrinsic calls like this:
1172 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1173 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1174 return IntrinsicID_match(IntrID);
1177 template <Intrinsic::ID IntrID, typename T0>
1178 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1179 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1182 template <Intrinsic::ID IntrID, typename T0, typename T1>
1183 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1185 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1188 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1189 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1190 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1191 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1194 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1196 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1197 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1198 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1201 // Helper intrinsic matching specializations.
1202 template <typename Opnd0>
1203 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1204 return m_Intrinsic<Intrinsic::bswap>(Op0);
1207 template <typename Opnd0, typename Opnd1>
1208 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1210 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1213 template <typename Opnd0, typename Opnd1>
1214 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1216 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1219 } // end namespace PatternMatch
1220 } // end namespace llvm