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 a binary operator, capturing it if we match.
299 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
301 /// \brief Match a ConstantInt, capturing the value if we match.
302 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
304 /// \brief Match a Constant, capturing the value if we match.
305 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
307 /// \brief Match a ConstantFP, capturing the value if we match.
308 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
310 /// \brief Match a specified Value*.
311 struct specificval_ty {
313 specificval_ty(const Value *V) : Val(V) {}
315 template <typename ITy> bool match(ITy *V) { return V == Val; }
318 /// \brief Match if we have a specific specified value.
319 inline specificval_ty m_Specific(const Value *V) { return V; }
321 /// \brief Match a specified floating point value or vector of all elements of
323 struct specific_fpval {
325 specific_fpval(double V) : Val(V) {}
327 template <typename ITy> bool match(ITy *V) {
328 if (const auto *CFP = dyn_cast<ConstantFP>(V))
329 return CFP->isExactlyValue(Val);
330 if (V->getType()->isVectorTy())
331 if (const auto *C = dyn_cast<Constant>(V))
332 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
333 return CFP->isExactlyValue(Val);
338 /// \brief Match a specific floating point value or vector with all elements
339 /// equal to the value.
340 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
342 /// \brief Match a float 1.0 or vector with all elements equal to 1.0.
343 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
345 struct bind_const_intval_ty {
347 bind_const_intval_ty(uint64_t &V) : VR(V) {}
349 template <typename ITy> bool match(ITy *V) {
350 if (const auto *CV = dyn_cast<ConstantInt>(V))
351 if (CV->getBitWidth() <= 64) {
352 VR = CV->getZExtValue();
359 /// \brief Match a specified integer value or vector of all elements of that
361 struct specific_intval {
363 specific_intval(uint64_t V) : Val(V) {}
365 template <typename ITy> bool match(ITy *V) {
366 const auto *CI = dyn_cast<ConstantInt>(V);
367 if (!CI && V->getType()->isVectorTy())
368 if (const auto *C = dyn_cast<Constant>(V))
369 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
371 if (CI && CI->getBitWidth() <= 64)
372 return CI->getZExtValue() == Val;
378 /// \brief Match a specific integer value or vector with all elements equal to
380 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
382 /// \brief Match a ConstantInt and bind to its value. This does not match
383 /// ConstantInts wider than 64-bits.
384 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
386 //===----------------------------------------------------------------------===//
387 // Matcher for any binary operator.
389 template <typename LHS_t, typename RHS_t> struct AnyBinaryOp_match {
393 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
395 template <typename OpTy> bool match(OpTy *V) {
396 if (auto *I = dyn_cast<BinaryOperator>(V))
397 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
402 template <typename LHS, typename RHS>
403 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
404 return AnyBinaryOp_match<LHS, RHS>(L, R);
407 //===----------------------------------------------------------------------===//
408 // Matchers for specific binary operators.
411 template <typename LHS_t, typename RHS_t, unsigned Opcode>
412 struct BinaryOp_match {
416 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
418 template <typename OpTy> bool match(OpTy *V) {
419 if (V->getValueID() == Value::InstructionVal + Opcode) {
420 auto *I = cast<BinaryOperator>(V);
421 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
423 if (auto *CE = dyn_cast<ConstantExpr>(V))
424 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
425 R.match(CE->getOperand(1));
430 template <typename LHS, typename RHS>
431 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
433 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
436 template <typename LHS, typename RHS>
437 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
439 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
442 template <typename LHS, typename RHS>
443 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
445 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
448 template <typename LHS, typename RHS>
449 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
451 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
454 template <typename LHS, typename RHS>
455 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
457 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
460 template <typename LHS, typename RHS>
461 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
463 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
466 template <typename LHS, typename RHS>
467 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
469 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
472 template <typename LHS, typename RHS>
473 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
475 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
478 template <typename LHS, typename RHS>
479 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
481 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
484 template <typename LHS, typename RHS>
485 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
487 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
490 template <typename LHS, typename RHS>
491 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
493 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
496 template <typename LHS, typename RHS>
497 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
499 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
502 template <typename LHS, typename RHS>
503 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
505 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
508 template <typename LHS, typename RHS>
509 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
511 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
514 template <typename LHS, typename RHS>
515 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
517 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
520 template <typename LHS, typename RHS>
521 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
523 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
526 template <typename LHS, typename RHS>
527 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
529 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
532 template <typename LHS, typename RHS>
533 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
535 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
538 template <typename LHS_t, typename RHS_t, unsigned Opcode,
539 unsigned WrapFlags = 0>
540 struct OverflowingBinaryOp_match {
544 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
547 template <typename OpTy> bool match(OpTy *V) {
548 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
549 if (Op->getOpcode() != Opcode)
551 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
552 !Op->hasNoUnsignedWrap())
554 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
555 !Op->hasNoSignedWrap())
557 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
563 template <typename LHS, typename RHS>
564 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
565 OverflowingBinaryOperator::NoSignedWrap>
566 m_NSWAdd(const LHS &L, const RHS &R) {
567 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
568 OverflowingBinaryOperator::NoSignedWrap>(
571 template <typename LHS, typename RHS>
572 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
573 OverflowingBinaryOperator::NoSignedWrap>
574 m_NSWSub(const LHS &L, const RHS &R) {
575 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
576 OverflowingBinaryOperator::NoSignedWrap>(
579 template <typename LHS, typename RHS>
580 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
581 OverflowingBinaryOperator::NoSignedWrap>
582 m_NSWMul(const LHS &L, const RHS &R) {
583 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
584 OverflowingBinaryOperator::NoSignedWrap>(
587 template <typename LHS, typename RHS>
588 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
589 OverflowingBinaryOperator::NoSignedWrap>
590 m_NSWShl(const LHS &L, const RHS &R) {
591 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
592 OverflowingBinaryOperator::NoSignedWrap>(
596 template <typename LHS, typename RHS>
597 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
598 OverflowingBinaryOperator::NoUnsignedWrap>
599 m_NUWAdd(const LHS &L, const RHS &R) {
600 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
601 OverflowingBinaryOperator::NoUnsignedWrap>(
604 template <typename LHS, typename RHS>
605 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
606 OverflowingBinaryOperator::NoUnsignedWrap>
607 m_NUWSub(const LHS &L, const RHS &R) {
608 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
609 OverflowingBinaryOperator::NoUnsignedWrap>(
612 template <typename LHS, typename RHS>
613 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
614 OverflowingBinaryOperator::NoUnsignedWrap>
615 m_NUWMul(const LHS &L, const RHS &R) {
616 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
617 OverflowingBinaryOperator::NoUnsignedWrap>(
620 template <typename LHS, typename RHS>
621 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
622 OverflowingBinaryOperator::NoUnsignedWrap>
623 m_NUWShl(const LHS &L, const RHS &R) {
624 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
625 OverflowingBinaryOperator::NoUnsignedWrap>(
629 //===----------------------------------------------------------------------===//
630 // Class that matches two different binary ops.
632 template <typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
633 struct BinOp2_match {
637 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
639 template <typename OpTy> bool match(OpTy *V) {
640 if (V->getValueID() == Value::InstructionVal + Opc1 ||
641 V->getValueID() == Value::InstructionVal + Opc2) {
642 auto *I = cast<BinaryOperator>(V);
643 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
645 if (auto *CE = dyn_cast<ConstantExpr>(V))
646 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
647 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
652 /// \brief Matches LShr or AShr.
653 template <typename LHS, typename RHS>
654 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
655 m_Shr(const LHS &L, const RHS &R) {
656 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
659 /// \brief Matches LShr or Shl.
660 template <typename LHS, typename RHS>
661 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
662 m_LogicalShift(const LHS &L, const RHS &R) {
663 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
666 /// \brief Matches UDiv and SDiv.
667 template <typename LHS, typename RHS>
668 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
669 m_IDiv(const LHS &L, const RHS &R) {
670 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
673 //===----------------------------------------------------------------------===//
674 // Class that matches exact binary ops.
676 template <typename SubPattern_t> struct Exact_match {
677 SubPattern_t SubPattern;
679 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
681 template <typename OpTy> bool match(OpTy *V) {
682 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
683 return PEO->isExact() && SubPattern.match(V);
688 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
692 //===----------------------------------------------------------------------===//
693 // Matchers for CmpInst classes
696 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
697 struct CmpClass_match {
698 PredicateTy &Predicate;
702 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
703 : Predicate(Pred), L(LHS), R(RHS) {}
705 template <typename OpTy> bool match(OpTy *V) {
706 if (Class *I = dyn_cast<Class>(V))
707 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
708 Predicate = I->getPredicate();
715 template <typename LHS, typename RHS>
716 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
717 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
718 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
721 template <typename LHS, typename RHS>
722 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
723 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
724 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
727 template <typename LHS, typename RHS>
728 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
729 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
730 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
733 //===----------------------------------------------------------------------===//
734 // Matchers for SelectInst classes
737 template <typename Cond_t, typename LHS_t, typename RHS_t>
738 struct SelectClass_match {
743 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
744 : C(Cond), L(LHS), R(RHS) {}
746 template <typename OpTy> bool match(OpTy *V) {
747 if (auto *I = dyn_cast<SelectInst>(V))
748 return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
749 R.match(I->getOperand(2));
754 template <typename Cond, typename LHS, typename RHS>
755 inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
757 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
760 /// \brief This matches a select of two constants, e.g.:
761 /// m_SelectCst<-1, 0>(m_Value(V))
762 template <int64_t L, int64_t R, typename Cond>
763 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R>>
764 m_SelectCst(const Cond &C) {
765 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
768 //===----------------------------------------------------------------------===//
769 // Matchers for CastInst classes
772 template <typename Op_t, unsigned Opcode> struct CastClass_match {
775 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
777 template <typename OpTy> bool match(OpTy *V) {
778 if (auto *O = dyn_cast<Operator>(V))
779 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
784 /// \brief Matches BitCast.
785 template <typename OpTy>
786 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
787 return CastClass_match<OpTy, Instruction::BitCast>(Op);
790 /// \brief Matches PtrToInt.
791 template <typename OpTy>
792 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
793 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
796 /// \brief Matches Trunc.
797 template <typename OpTy>
798 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
799 return CastClass_match<OpTy, Instruction::Trunc>(Op);
802 /// \brief Matches SExt.
803 template <typename OpTy>
804 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
805 return CastClass_match<OpTy, Instruction::SExt>(Op);
808 /// \brief Matches ZExt.
809 template <typename OpTy>
810 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
811 return CastClass_match<OpTy, Instruction::ZExt>(Op);
814 /// \brief Matches UIToFP.
815 template <typename OpTy>
816 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
817 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
820 /// \brief Matches SIToFP.
821 template <typename OpTy>
822 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
823 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
826 //===----------------------------------------------------------------------===//
827 // Matchers for unary operators
830 template <typename LHS_t> struct not_match {
833 not_match(const LHS_t &LHS) : L(LHS) {}
835 template <typename OpTy> bool match(OpTy *V) {
836 if (auto *O = dyn_cast<Operator>(V))
837 if (O->getOpcode() == Instruction::Xor)
838 return matchIfNot(O->getOperand(0), O->getOperand(1));
843 bool matchIfNot(Value *LHS, Value *RHS) {
844 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
846 isa<ConstantVector>(RHS)) &&
847 cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
851 template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }
853 template <typename LHS_t> struct neg_match {
856 neg_match(const LHS_t &LHS) : L(LHS) {}
858 template <typename OpTy> bool match(OpTy *V) {
859 if (auto *O = dyn_cast<Operator>(V))
860 if (O->getOpcode() == Instruction::Sub)
861 return matchIfNeg(O->getOperand(0), O->getOperand(1));
866 bool matchIfNeg(Value *LHS, Value *RHS) {
867 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
868 isa<ConstantAggregateZero>(LHS)) &&
873 /// \brief Match an integer negate.
874 template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
876 template <typename LHS_t> struct fneg_match {
879 fneg_match(const LHS_t &LHS) : L(LHS) {}
881 template <typename OpTy> bool match(OpTy *V) {
882 if (auto *O = dyn_cast<Operator>(V))
883 if (O->getOpcode() == Instruction::FSub)
884 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
889 bool matchIfFNeg(Value *LHS, Value *RHS) {
890 if (const auto *C = dyn_cast<ConstantFP>(LHS))
891 return C->isNegativeZeroValue() && L.match(RHS);
896 /// \brief Match a floating point negate.
897 template <typename LHS> inline fneg_match<LHS> m_FNeg(const LHS &L) {
901 //===----------------------------------------------------------------------===//
902 // Matchers for control flow.
907 br_match(BasicBlock *&Succ) : Succ(Succ) {}
909 template <typename OpTy> bool match(OpTy *V) {
910 if (auto *BI = dyn_cast<BranchInst>(V))
911 if (BI->isUnconditional()) {
912 Succ = BI->getSuccessor(0);
919 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
921 template <typename Cond_t> struct brc_match {
924 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
925 : Cond(C), T(t), F(f) {}
927 template <typename OpTy> bool match(OpTy *V) {
928 if (auto *BI = dyn_cast<BranchInst>(V))
929 if (BI->isConditional() && Cond.match(BI->getCondition())) {
930 T = BI->getSuccessor(0);
931 F = BI->getSuccessor(1);
938 template <typename Cond_t>
939 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
940 return brc_match<Cond_t>(C, T, F);
943 //===----------------------------------------------------------------------===//
944 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
947 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
948 struct MaxMin_match {
952 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
954 template <typename OpTy> bool match(OpTy *V) {
955 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
956 auto *SI = dyn_cast<SelectInst>(V);
959 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
962 // At this point we have a select conditioned on a comparison. Check that
963 // it is the values returned by the select that are being compared.
964 Value *TrueVal = SI->getTrueValue();
965 Value *FalseVal = SI->getFalseValue();
966 Value *LHS = Cmp->getOperand(0);
967 Value *RHS = Cmp->getOperand(1);
968 if ((TrueVal != LHS || FalseVal != RHS) &&
969 (TrueVal != RHS || FalseVal != LHS))
971 typename CmpInst_t::Predicate Pred =
972 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate();
973 // Does "(x pred y) ? x : y" represent the desired max/min operation?
974 if (!Pred_t::match(Pred))
976 // It does! Bind the operands.
977 return L.match(LHS) && R.match(RHS);
981 /// \brief Helper class for identifying signed max predicates.
982 struct smax_pred_ty {
983 static bool match(ICmpInst::Predicate Pred) {
984 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
988 /// \brief Helper class for identifying signed min predicates.
989 struct smin_pred_ty {
990 static bool match(ICmpInst::Predicate Pred) {
991 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
995 /// \brief Helper class for identifying unsigned max predicates.
996 struct umax_pred_ty {
997 static bool match(ICmpInst::Predicate Pred) {
998 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1002 /// \brief Helper class for identifying unsigned min predicates.
1003 struct umin_pred_ty {
1004 static bool match(ICmpInst::Predicate Pred) {
1005 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1009 /// \brief Helper class for identifying ordered max predicates.
1010 struct ofmax_pred_ty {
1011 static bool match(FCmpInst::Predicate Pred) {
1012 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1016 /// \brief Helper class for identifying ordered min predicates.
1017 struct ofmin_pred_ty {
1018 static bool match(FCmpInst::Predicate Pred) {
1019 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1023 /// \brief Helper class for identifying unordered max predicates.
1024 struct ufmax_pred_ty {
1025 static bool match(FCmpInst::Predicate Pred) {
1026 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1030 /// \brief Helper class for identifying unordered min predicates.
1031 struct ufmin_pred_ty {
1032 static bool match(FCmpInst::Predicate Pred) {
1033 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1037 template <typename LHS, typename RHS>
1038 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1040 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1043 template <typename LHS, typename RHS>
1044 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1046 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1049 template <typename LHS, typename RHS>
1050 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1052 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1055 template <typename LHS, typename RHS>
1056 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1058 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1061 /// \brief Match an 'ordered' floating point maximum function.
1062 /// Floating point has one special value 'NaN'. Therefore, there is no total
1063 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1064 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1065 /// semantics. In the presence of 'NaN' we have to preserve the original
1066 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1068 /// max(L, R) iff L and R are not NaN
1069 /// m_OrdFMax(L, R) = R iff L or R are NaN
1070 template <typename LHS, typename RHS>
1071 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1073 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1076 /// \brief Match an 'ordered' floating point minimum function.
1077 /// Floating point has one special value 'NaN'. Therefore, there is no total
1078 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1079 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1080 /// semantics. In the presence of 'NaN' we have to preserve the original
1081 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1083 /// max(L, R) iff L and R are not NaN
1084 /// m_OrdFMin(L, R) = R iff L or R are NaN
1085 template <typename LHS, typename RHS>
1086 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1088 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1091 /// \brief Match an 'unordered' floating point maximum function.
1092 /// Floating point has one special value 'NaN'. Therefore, there is no total
1093 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1094 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1095 /// semantics. In the presence of 'NaN' we have to preserve the original
1096 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1098 /// max(L, R) iff L and R are not NaN
1099 /// m_UnordFMin(L, R) = L iff L or R are NaN
1100 template <typename LHS, typename RHS>
1101 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1102 m_UnordFMax(const LHS &L, const RHS &R) {
1103 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1106 /// \brief Match an 'unordered' floating point minimum function.
1107 /// Floating point has one special value 'NaN'. Therefore, there is no total
1108 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1109 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1110 /// semantics. In the presence of 'NaN' we have to preserve the original
1111 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1113 /// max(L, R) iff L and R are not NaN
1114 /// m_UnordFMin(L, R) = L iff L or R are NaN
1115 template <typename LHS, typename RHS>
1116 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1117 m_UnordFMin(const LHS &L, const RHS &R) {
1118 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1121 template <typename Opnd_t> struct Argument_match {
1124 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1126 template <typename OpTy> bool match(OpTy *V) {
1128 return CS.isCall() && Val.match(CS.getArgument(OpI));
1132 /// \brief Match an argument.
1133 template <unsigned OpI, typename Opnd_t>
1134 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1135 return Argument_match<Opnd_t>(OpI, Op);
1138 /// \brief Intrinsic matchers.
1139 struct IntrinsicID_match {
1141 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1143 template <typename OpTy> bool match(OpTy *V) {
1144 if (const auto *CI = dyn_cast<CallInst>(V))
1145 if (const auto *F = CI->getCalledFunction())
1146 return F->getIntrinsicID() == ID;
1151 /// Intrinsic matches are combinations of ID matchers, and argument
1152 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1153 /// them with lower arity matchers. Here's some convenient typedefs for up to
1154 /// several arguments, and more can be added as needed
1155 template <typename T0 = void, typename T1 = void, typename T2 = void,
1156 typename T3 = void, typename T4 = void, typename T5 = void,
1157 typename T6 = void, typename T7 = void, typename T8 = void,
1158 typename T9 = void, typename T10 = void>
1159 struct m_Intrinsic_Ty;
1160 template <typename T0> struct m_Intrinsic_Ty<T0> {
1161 typedef match_combine_and<IntrinsicID_match, Argument_match<T0>> Ty;
1163 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1164 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>
1167 template <typename T0, typename T1, typename T2>
1168 struct m_Intrinsic_Ty<T0, T1, T2> {
1169 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1170 Argument_match<T2>> Ty;
1172 template <typename T0, typename T1, typename T2, typename T3>
1173 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1174 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1175 Argument_match<T3>> Ty;
1178 /// \brief Match intrinsic calls like this:
1179 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1180 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1181 return IntrinsicID_match(IntrID);
1184 template <Intrinsic::ID IntrID, typename T0>
1185 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1186 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1189 template <Intrinsic::ID IntrID, typename T0, typename T1>
1190 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1192 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1195 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1196 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1197 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1198 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1201 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1203 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1204 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1205 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1208 // Helper intrinsic matching specializations.
1209 template <typename Opnd0>
1210 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1211 return m_Intrinsic<Intrinsic::bswap>(Op0);
1214 template <typename Opnd0, typename Opnd1>
1215 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1217 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1220 template <typename Opnd0, typename Opnd1>
1221 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1223 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1226 } // end namespace PatternMatch
1227 } // end namespace llvm