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>
42 bool match(Val *V, const Pattern &P) {
43 return const_cast<Pattern&>(P).match(V);
47 template<typename SubPattern_t>
49 SubPattern_t SubPattern;
51 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
53 template<typename OpTy>
55 return V->hasOneUse() && SubPattern.match(V);
60 inline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; }
63 template<typename Class>
65 template<typename ITy>
66 bool match(ITy *V) { return isa<Class>(V); }
69 /// m_Value() - Match an arbitrary value and ignore it.
70 inline class_match<Value> m_Value() { return class_match<Value>(); }
71 /// m_BinOp() - Match an arbitrary binary operation and ignore it.
72 inline class_match<BinaryOperator> m_BinOp() {
73 return class_match<BinaryOperator>();
75 /// m_Cmp() - Matches any compare instruction and ignore it.
76 inline class_match<CmpInst> m_Cmp() {
77 return class_match<CmpInst>();
79 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
80 inline class_match<ConstantInt> m_ConstantInt() {
81 return class_match<ConstantInt>();
83 /// m_Undef() - Match an arbitrary undef constant.
84 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
86 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
88 /// Matching combinators
89 template<typename LTy, typename RTy>
90 struct match_combine_or {
94 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
96 template<typename ITy>
106 template<typename LTy, typename RTy>
107 struct match_combine_and {
111 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
113 template<typename ITy>
122 /// Combine two pattern matchers matching L || R
123 template<typename LTy, typename RTy>
124 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
125 return match_combine_or<LTy, RTy>(L, R);
128 /// Combine two pattern matchers matching L && R
129 template<typename LTy, typename RTy>
130 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
131 return match_combine_and<LTy, RTy>(L, R);
135 template<typename ITy>
137 if (const Constant *C = dyn_cast<Constant>(V))
138 return C->isNullValue();
143 /// m_Zero() - Match an arbitrary zero/null constant. This includes
144 /// zero_initializer for vectors and ConstantPointerNull for pointers.
145 inline match_zero m_Zero() { return match_zero(); }
147 struct match_neg_zero {
148 template<typename ITy>
150 if (const Constant *C = dyn_cast<Constant>(V))
151 return C->isNegativeZeroValue();
156 /// m_NegZero() - Match an arbitrary zero/null constant. This includes
157 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
158 /// floating point constants, this will match negative zero but not positive
160 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
162 /// m_AnyZero() - Match an arbitrary zero/null constant. This includes
163 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
164 /// floating point constants, this will match negative zero and positive zero
165 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
166 return m_CombineOr(m_Zero(), m_NegZero());
171 apint_match(const APInt *&R) : Res(R) {}
172 template<typename ITy>
174 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
175 Res = &CI->getValue();
178 if (V->getType()->isVectorTy())
179 if (const Constant *C = dyn_cast<Constant>(V))
180 if (ConstantInt *CI =
181 dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
182 Res = &CI->getValue();
189 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
190 /// specified pointer to the contained APInt.
191 inline apint_match m_APInt(const APInt *&Res) { return Res; }
194 template<int64_t Val>
195 struct constantint_match {
196 template<typename ITy>
198 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
199 const APInt &CIV = CI->getValue();
201 return CIV == static_cast<uint64_t>(Val);
202 // If Val is negative, and CI is shorter than it, truncate to the right
203 // number of bits. If it is larger, then we have to sign extend. Just
204 // compare their negated values.
211 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
212 template<int64_t Val>
213 inline constantint_match<Val> m_ConstantInt() {
214 return constantint_match<Val>();
217 /// cst_pred_ty - This helper class is used to match scalar and vector constants
218 /// that satisfy a specified predicate.
219 template<typename Predicate>
220 struct cst_pred_ty : public Predicate {
221 template<typename ITy>
223 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
224 return this->isValue(CI->getValue());
225 if (V->getType()->isVectorTy())
226 if (const Constant *C = dyn_cast<Constant>(V))
227 if (const ConstantInt *CI =
228 dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
229 return this->isValue(CI->getValue());
234 /// api_pred_ty - This helper class is used to match scalar and vector constants
235 /// that satisfy a specified predicate, and bind them to an APInt.
236 template<typename Predicate>
237 struct api_pred_ty : public Predicate {
239 api_pred_ty(const APInt *&R) : Res(R) {}
240 template<typename ITy>
242 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
243 if (this->isValue(CI->getValue())) {
244 Res = &CI->getValue();
247 if (V->getType()->isVectorTy())
248 if (const Constant *C = dyn_cast<Constant>(V))
249 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
250 if (this->isValue(CI->getValue())) {
251 Res = &CI->getValue();
261 bool isValue(const APInt &C) { return C == 1; }
264 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
265 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
266 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
269 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
272 /// m_AllOnes() - Match an integer or vector with all bits set to true.
273 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
274 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
277 bool isValue(const APInt &C) { return C.isSignBit(); }
280 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
281 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
282 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
285 bool isValue(const APInt &C) { return C.isPowerOf2(); }
288 /// m_Power2() - Match an integer or vector power of 2.
289 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
290 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
292 template<typename Class>
295 bind_ty(Class *&V) : VR(V) {}
297 template<typename ITy>
299 if (Class *CV = dyn_cast<Class>(V)) {
307 /// m_Value - Match a value, capturing it if we match.
308 inline bind_ty<Value> m_Value(Value *&V) { return V; }
310 /// m_BinOp - Match a instruction, capturing it if we match.
311 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
313 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
314 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
316 /// m_Constant - Match a Constant, capturing the value if we match.
317 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
319 /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
320 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
322 /// specificval_ty - Match a specified Value*.
323 struct specificval_ty {
325 specificval_ty(const Value *V) : Val(V) {}
327 template<typename ITy>
333 /// m_Specific - Match if we have a specific specified value.
334 inline specificval_ty m_Specific(const Value *V) { return V; }
336 /// Match a specified floating point value or vector of all elements of that
338 struct specific_fpval {
340 specific_fpval(double V) : Val(V) {}
342 template<typename ITy>
344 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
345 return CFP->isExactlyValue(Val);
346 if (V->getType()->isVectorTy())
347 if (const Constant *C = dyn_cast<Constant>(V))
348 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
349 return CFP->isExactlyValue(Val);
354 /// Match a specific floating point value or vector with all elements equal to
356 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
358 /// Match a float 1.0 or vector with all elements equal to 1.0.
359 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
361 struct bind_const_intval_ty {
363 bind_const_intval_ty(uint64_t &V) : VR(V) {}
365 template<typename ITy>
367 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
368 if (CV->getBitWidth() <= 64) {
369 VR = CV->getZExtValue();
376 /// Match a specified integer value or vector of all elements of that value.
377 struct specific_intval {
379 specific_intval(uint64_t V) : Val(V) {}
381 template<typename ITy>
383 ConstantInt *CI = dyn_cast<ConstantInt>(V);
384 if (!CI && V->getType()->isVectorTy())
385 if (const auto *C = dyn_cast<Constant>(V))
386 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
388 if (CI && CI->getBitWidth() <= 64)
389 return CI->getZExtValue() == Val;
395 /// Match a specific integer value or vector with all elements equal to the
397 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
399 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
400 /// match ConstantInts wider than 64-bits.
401 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
403 //===----------------------------------------------------------------------===//
404 // Matcher for any binary operator.
406 template<typename LHS_t, typename RHS_t>
407 struct AnyBinaryOp_match {
411 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
413 template<typename OpTy>
414 bool match(OpTy *V) {
415 if (auto *I = dyn_cast<BinaryOperator>(V))
416 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
421 template<typename LHS, typename RHS>
422 inline AnyBinaryOp_match<LHS, RHS>
423 m_BinOp(const LHS &L, const RHS &R) {
424 return AnyBinaryOp_match<LHS, RHS>(L, R);
427 //===----------------------------------------------------------------------===//
428 // Matchers for specific binary operators.
431 template<typename LHS_t, typename RHS_t, unsigned Opcode>
432 struct BinaryOp_match {
436 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
438 template<typename OpTy>
439 bool match(OpTy *V) {
440 if (V->getValueID() == Value::InstructionVal + Opcode) {
441 BinaryOperator *I = cast<BinaryOperator>(V);
442 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
444 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
445 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
446 R.match(CE->getOperand(1));
451 template<typename LHS, typename RHS>
452 inline BinaryOp_match<LHS, RHS, Instruction::Add>
453 m_Add(const LHS &L, const RHS &R) {
454 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
457 template<typename LHS, typename RHS>
458 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
459 m_FAdd(const LHS &L, const RHS &R) {
460 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
463 template<typename LHS, typename RHS>
464 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
465 m_Sub(const LHS &L, const RHS &R) {
466 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
469 template<typename LHS, typename RHS>
470 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
471 m_FSub(const LHS &L, const RHS &R) {
472 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
475 template<typename LHS, typename RHS>
476 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
477 m_Mul(const LHS &L, const RHS &R) {
478 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
481 template<typename LHS, typename RHS>
482 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
483 m_FMul(const LHS &L, const RHS &R) {
484 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
487 template<typename LHS, typename RHS>
488 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
489 m_UDiv(const LHS &L, const RHS &R) {
490 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
493 template<typename LHS, typename RHS>
494 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
495 m_SDiv(const LHS &L, const RHS &R) {
496 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
499 template<typename LHS, typename RHS>
500 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
501 m_FDiv(const LHS &L, const RHS &R) {
502 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
505 template<typename LHS, typename RHS>
506 inline BinaryOp_match<LHS, RHS, Instruction::URem>
507 m_URem(const LHS &L, const RHS &R) {
508 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
511 template<typename LHS, typename RHS>
512 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
513 m_SRem(const LHS &L, const RHS &R) {
514 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
517 template<typename LHS, typename RHS>
518 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
519 m_FRem(const LHS &L, const RHS &R) {
520 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
523 template<typename LHS, typename RHS>
524 inline BinaryOp_match<LHS, RHS, Instruction::And>
525 m_And(const LHS &L, const RHS &R) {
526 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
529 template<typename LHS, typename RHS>
530 inline BinaryOp_match<LHS, RHS, Instruction::Or>
531 m_Or(const LHS &L, const RHS &R) {
532 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
535 template<typename LHS, typename RHS>
536 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
537 m_Xor(const LHS &L, const RHS &R) {
538 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
541 template<typename LHS, typename RHS>
542 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
543 m_Shl(const LHS &L, const RHS &R) {
544 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
547 template<typename LHS, typename RHS>
548 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
549 m_LShr(const LHS &L, const RHS &R) {
550 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
553 template<typename LHS, typename RHS>
554 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
555 m_AShr(const LHS &L, const RHS &R) {
556 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
559 template<typename LHS_t, typename RHS_t, unsigned Opcode, unsigned WrapFlags = 0>
560 struct OverflowingBinaryOp_match {
564 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
566 template<typename OpTy>
567 bool match(OpTy *V) {
568 if (OverflowingBinaryOperator *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
569 if (Op->getOpcode() != Opcode)
571 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
572 !Op->hasNoUnsignedWrap())
574 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
575 !Op->hasNoSignedWrap())
577 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
583 template <typename LHS, typename RHS>
584 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
585 OverflowingBinaryOperator::NoSignedWrap>
586 m_NSWAdd(const LHS &L, const RHS &R) {
587 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
588 OverflowingBinaryOperator::NoSignedWrap>(
591 template <typename LHS, typename RHS>
592 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
593 OverflowingBinaryOperator::NoSignedWrap>
594 m_NSWSub(const LHS &L, const RHS &R) {
595 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
596 OverflowingBinaryOperator::NoSignedWrap>(
599 template <typename LHS, typename RHS>
600 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
601 OverflowingBinaryOperator::NoSignedWrap>
602 m_NSWMul(const LHS &L, const RHS &R) {
603 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
604 OverflowingBinaryOperator::NoSignedWrap>(
607 template <typename LHS, typename RHS>
608 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
609 OverflowingBinaryOperator::NoSignedWrap>
610 m_NSWShl(const LHS &L, const RHS &R) {
611 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
612 OverflowingBinaryOperator::NoSignedWrap>(
616 template <typename LHS, typename RHS>
617 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
618 OverflowingBinaryOperator::NoUnsignedWrap>
619 m_NUWAdd(const LHS &L, const RHS &R) {
620 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
621 OverflowingBinaryOperator::NoUnsignedWrap>(
624 template <typename LHS, typename RHS>
625 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
626 OverflowingBinaryOperator::NoUnsignedWrap>
627 m_NUWSub(const LHS &L, const RHS &R) {
628 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
629 OverflowingBinaryOperator::NoUnsignedWrap>(
632 template <typename LHS, typename RHS>
633 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
634 OverflowingBinaryOperator::NoUnsignedWrap>
635 m_NUWMul(const LHS &L, const RHS &R) {
636 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
637 OverflowingBinaryOperator::NoUnsignedWrap>(
640 template <typename LHS, typename RHS>
641 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
642 OverflowingBinaryOperator::NoUnsignedWrap>
643 m_NUWShl(const LHS &L, const RHS &R) {
644 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
645 OverflowingBinaryOperator::NoUnsignedWrap>(
649 //===----------------------------------------------------------------------===//
650 // Class that matches two different binary ops.
652 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
653 struct BinOp2_match {
657 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
659 template<typename OpTy>
660 bool match(OpTy *V) {
661 if (V->getValueID() == Value::InstructionVal + Opc1 ||
662 V->getValueID() == Value::InstructionVal + Opc2) {
663 BinaryOperator *I = cast<BinaryOperator>(V);
664 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
666 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
667 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
668 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
673 /// m_Shr - Matches LShr or AShr.
674 template<typename LHS, typename RHS>
675 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
676 m_Shr(const LHS &L, const RHS &R) {
677 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
680 /// m_LogicalShift - Matches LShr or Shl.
681 template<typename LHS, typename RHS>
682 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
683 m_LogicalShift(const LHS &L, const RHS &R) {
684 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
687 /// m_IDiv - Matches UDiv and SDiv.
688 template<typename LHS, typename RHS>
689 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
690 m_IDiv(const LHS &L, const RHS &R) {
691 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
694 //===----------------------------------------------------------------------===//
695 // Class that matches exact binary ops.
697 template<typename SubPattern_t>
699 SubPattern_t SubPattern;
701 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
703 template<typename OpTy>
704 bool match(OpTy *V) {
705 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
706 return PEO->isExact() && SubPattern.match(V);
712 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
714 //===----------------------------------------------------------------------===//
715 // Matchers for CmpInst classes
718 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
719 struct CmpClass_match {
720 PredicateTy &Predicate;
724 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
725 : Predicate(Pred), L(LHS), R(RHS) {}
727 template<typename OpTy>
728 bool match(OpTy *V) {
729 if (Class *I = dyn_cast<Class>(V))
730 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
731 Predicate = I->getPredicate();
738 template<typename LHS, typename RHS>
739 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
740 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
741 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
744 template<typename LHS, typename RHS>
745 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
746 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
747 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
750 template<typename LHS, typename RHS>
751 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
752 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
753 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
756 //===----------------------------------------------------------------------===//
757 // Matchers for SelectInst classes
760 template<typename Cond_t, typename LHS_t, typename RHS_t>
761 struct SelectClass_match {
766 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
768 : C(Cond), L(LHS), R(RHS) {}
770 template<typename OpTy>
771 bool match(OpTy *V) {
772 if (SelectInst *I = dyn_cast<SelectInst>(V))
773 return C.match(I->getOperand(0)) &&
774 L.match(I->getOperand(1)) &&
775 R.match(I->getOperand(2));
780 template<typename Cond, typename LHS, typename RHS>
781 inline SelectClass_match<Cond, LHS, RHS>
782 m_Select(const Cond &C, const LHS &L, const RHS &R) {
783 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
786 /// m_SelectCst - This matches a select of two constants, e.g.:
787 /// m_SelectCst<-1, 0>(m_Value(V))
788 template<int64_t L, int64_t R, typename Cond>
789 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
790 m_SelectCst(const Cond &C) {
791 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
795 //===----------------------------------------------------------------------===//
796 // Matchers for CastInst classes
799 template<typename Op_t, unsigned Opcode>
800 struct CastClass_match {
803 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
805 template<typename OpTy>
806 bool match(OpTy *V) {
807 if (Operator *O = dyn_cast<Operator>(V))
808 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
814 template<typename OpTy>
815 inline CastClass_match<OpTy, Instruction::BitCast>
816 m_BitCast(const OpTy &Op) {
817 return CastClass_match<OpTy, Instruction::BitCast>(Op);
821 template<typename OpTy>
822 inline CastClass_match<OpTy, Instruction::PtrToInt>
823 m_PtrToInt(const OpTy &Op) {
824 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
828 template<typename OpTy>
829 inline CastClass_match<OpTy, Instruction::Trunc>
830 m_Trunc(const OpTy &Op) {
831 return CastClass_match<OpTy, Instruction::Trunc>(Op);
835 template<typename OpTy>
836 inline CastClass_match<OpTy, Instruction::SExt>
837 m_SExt(const OpTy &Op) {
838 return CastClass_match<OpTy, Instruction::SExt>(Op);
842 template<typename OpTy>
843 inline CastClass_match<OpTy, Instruction::ZExt>
844 m_ZExt(const OpTy &Op) {
845 return CastClass_match<OpTy, Instruction::ZExt>(Op);
849 template<typename OpTy>
850 inline CastClass_match<OpTy, Instruction::UIToFP>
851 m_UIToFP(const OpTy &Op) {
852 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
856 template<typename OpTy>
857 inline CastClass_match<OpTy, Instruction::SIToFP>
858 m_SIToFP(const OpTy &Op) {
859 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
862 //===----------------------------------------------------------------------===//
863 // Matchers for unary operators
866 template<typename LHS_t>
870 not_match(const LHS_t &LHS) : L(LHS) {}
872 template<typename OpTy>
873 bool match(OpTy *V) {
874 if (Operator *O = dyn_cast<Operator>(V))
875 if (O->getOpcode() == Instruction::Xor)
876 return matchIfNot(O->getOperand(0), O->getOperand(1));
880 bool matchIfNot(Value *LHS, Value *RHS) {
881 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
883 isa<ConstantVector>(RHS)) &&
884 cast<Constant>(RHS)->isAllOnesValue() &&
889 template<typename LHS>
890 inline not_match<LHS> m_Not(const LHS &L) { return L; }
893 template<typename LHS_t>
897 neg_match(const LHS_t &LHS) : L(LHS) {}
899 template<typename OpTy>
900 bool match(OpTy *V) {
901 if (Operator *O = dyn_cast<Operator>(V))
902 if (O->getOpcode() == Instruction::Sub)
903 return matchIfNeg(O->getOperand(0), O->getOperand(1));
907 bool matchIfNeg(Value *LHS, Value *RHS) {
908 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
909 isa<ConstantAggregateZero>(LHS)) &&
914 /// m_Neg - Match an integer negate.
915 template<typename LHS>
916 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
919 template<typename LHS_t>
923 fneg_match(const LHS_t &LHS) : L(LHS) {}
925 template<typename OpTy>
926 bool match(OpTy *V) {
927 if (Operator *O = dyn_cast<Operator>(V))
928 if (O->getOpcode() == Instruction::FSub)
929 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
933 bool matchIfFNeg(Value *LHS, Value *RHS) {
934 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
935 return C->isNegativeZeroValue() && L.match(RHS);
940 /// m_FNeg - Match a floating point negate.
941 template<typename LHS>
942 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
945 //===----------------------------------------------------------------------===//
946 // Matchers for control flow.
951 br_match(BasicBlock *&Succ)
955 template<typename OpTy>
956 bool match(OpTy *V) {
957 if (BranchInst *BI = dyn_cast<BranchInst>(V))
958 if (BI->isUnconditional()) {
959 Succ = BI->getSuccessor(0);
966 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
968 template<typename Cond_t>
972 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
973 : Cond(C), T(t), F(f) {
976 template<typename OpTy>
977 bool match(OpTy *V) {
978 if (BranchInst *BI = dyn_cast<BranchInst>(V))
979 if (BI->isConditional() && Cond.match(BI->getCondition())) {
980 T = BI->getSuccessor(0);
981 F = BI->getSuccessor(1);
988 template<typename Cond_t>
989 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
990 return brc_match<Cond_t>(C, T, F);
994 //===----------------------------------------------------------------------===//
995 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
998 template<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
999 struct MaxMin_match {
1003 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
1006 template<typename OpTy>
1007 bool match(OpTy *V) {
1008 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1009 SelectInst *SI = dyn_cast<SelectInst>(V);
1012 CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1015 // At this point we have a select conditioned on a comparison. Check that
1016 // it is the values returned by the select that are being compared.
1017 Value *TrueVal = SI->getTrueValue();
1018 Value *FalseVal = SI->getFalseValue();
1019 Value *LHS = Cmp->getOperand(0);
1020 Value *RHS = Cmp->getOperand(1);
1021 if ((TrueVal != LHS || FalseVal != RHS) &&
1022 (TrueVal != RHS || FalseVal != LHS))
1024 typename CmpInst_t::Predicate Pred = LHS == TrueVal ?
1025 Cmp->getPredicate() : Cmp->getSwappedPredicate();
1026 // Does "(x pred y) ? x : y" represent the desired max/min operation?
1027 if (!Pred_t::match(Pred))
1029 // It does! Bind the operands.
1030 return L.match(LHS) && R.match(RHS);
1034 /// smax_pred_ty - Helper class for identifying signed max predicates.
1035 struct smax_pred_ty {
1036 static bool match(ICmpInst::Predicate Pred) {
1037 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1041 /// smin_pred_ty - Helper class for identifying signed min predicates.
1042 struct smin_pred_ty {
1043 static bool match(ICmpInst::Predicate Pred) {
1044 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1048 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
1049 struct umax_pred_ty {
1050 static bool match(ICmpInst::Predicate Pred) {
1051 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1055 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
1056 struct umin_pred_ty {
1057 static bool match(ICmpInst::Predicate Pred) {
1058 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1062 /// ofmax_pred_ty - Helper class for identifying ordered max predicates.
1063 struct ofmax_pred_ty {
1064 static bool match(FCmpInst::Predicate Pred) {
1065 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1069 /// ofmin_pred_ty - Helper class for identifying ordered min predicates.
1070 struct ofmin_pred_ty {
1071 static bool match(FCmpInst::Predicate Pred) {
1072 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1076 /// ufmax_pred_ty - Helper class for identifying unordered max predicates.
1077 struct ufmax_pred_ty {
1078 static bool match(FCmpInst::Predicate Pred) {
1079 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1083 /// ufmin_pred_ty - Helper class for identifying unordered min predicates.
1084 struct ufmin_pred_ty {
1085 static bool match(FCmpInst::Predicate Pred) {
1086 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1090 template<typename LHS, typename RHS>
1091 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>
1092 m_SMax(const LHS &L, const RHS &R) {
1093 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1096 template<typename LHS, typename RHS>
1097 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>
1098 m_SMin(const LHS &L, const RHS &R) {
1099 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1102 template<typename LHS, typename RHS>
1103 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>
1104 m_UMax(const LHS &L, const RHS &R) {
1105 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1108 template<typename LHS, typename RHS>
1109 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>
1110 m_UMin(const LHS &L, const RHS &R) {
1111 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1114 /// \brief Match an 'ordered' floating point maximum function.
1115 /// Floating point has one special value 'NaN'. Therefore, there is no total
1116 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1117 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1118 /// semantics. In the presence of 'NaN' we have to preserve the original
1119 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1121 /// max(L, R) iff L and R are not NaN
1122 /// m_OrdFMax(L, R) = R iff L or R are NaN
1123 template<typename LHS, typename RHS>
1124 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>
1125 m_OrdFMax(const LHS &L, const RHS &R) {
1126 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1129 /// \brief Match an 'ordered' floating point minimum function.
1130 /// Floating point has one special value 'NaN'. Therefore, there is no total
1131 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1132 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1133 /// semantics. In the presence of 'NaN' we have to preserve the original
1134 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1136 /// max(L, R) iff L and R are not NaN
1137 /// m_OrdFMin(L, R) = R iff L or R are NaN
1138 template<typename LHS, typename RHS>
1139 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>
1140 m_OrdFMin(const LHS &L, const RHS &R) {
1141 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1144 /// \brief Match an 'unordered' floating point maximum function.
1145 /// Floating point has one special value 'NaN'. Therefore, there is no total
1146 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1147 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1148 /// semantics. In the presence of 'NaN' we have to preserve the original
1149 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1151 /// max(L, R) iff L and R are not NaN
1152 /// m_UnordFMin(L, R) = L iff L or R are NaN
1153 template<typename LHS, typename RHS>
1154 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1155 m_UnordFMax(const LHS &L, const RHS &R) {
1156 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1159 /// \brief Match an 'unordered' floating point minimum function.
1160 /// Floating point has one special value 'NaN'. Therefore, there is no total
1161 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1162 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1163 /// semantics. In the presence of 'NaN' we have to preserve the original
1164 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1166 /// max(L, R) iff L and R are not NaN
1167 /// m_UnordFMin(L, R) = L iff L or R are NaN
1168 template<typename LHS, typename RHS>
1169 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1170 m_UnordFMin(const LHS &L, const RHS &R) {
1171 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1174 template<typename Opnd_t>
1175 struct Argument_match {
1178 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
1180 template<typename OpTy>
1181 bool match(OpTy *V) {
1183 return CS.isCall() && Val.match(CS.getArgument(OpI));
1187 /// Match an argument
1188 template<unsigned OpI, typename Opnd_t>
1189 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1190 return Argument_match<Opnd_t>(OpI, Op);
1193 /// Intrinsic matchers.
1194 struct IntrinsicID_match {
1196 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) { }
1198 template<typename OpTy>
1199 bool match(OpTy *V) {
1200 if (const CallInst *CI = dyn_cast<CallInst>(V))
1201 if (const Function *F = CI->getCalledFunction())
1202 return F->getIntrinsicID() == ID;
1207 /// Intrinsic matches are combinations of ID matchers, and argument
1208 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1209 /// them with lower arity matchers. Here's some convenient typedefs for up to
1210 /// several arguments, and more can be added as needed
1211 template <typename T0 = void, typename T1 = void, typename T2 = void,
1212 typename T3 = void, typename T4 = void, typename T5 = void,
1213 typename T6 = void, typename T7 = void, typename T8 = void,
1214 typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
1215 template <typename T0>
1216 struct m_Intrinsic_Ty<T0> {
1217 typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
1219 template <typename T0, typename T1>
1220 struct m_Intrinsic_Ty<T0, T1> {
1221 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
1222 Argument_match<T1> > Ty;
1224 template <typename T0, typename T1, typename T2>
1225 struct m_Intrinsic_Ty<T0, T1, T2> {
1226 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1227 Argument_match<T2> > Ty;
1229 template <typename T0, typename T1, typename T2, typename T3>
1230 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1231 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1232 Argument_match<T3> > Ty;
1235 /// Match intrinsic calls like this:
1236 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1237 template <Intrinsic::ID IntrID>
1238 inline IntrinsicID_match
1239 m_Intrinsic() { return IntrinsicID_match(IntrID); }
1241 template<Intrinsic::ID IntrID, typename T0>
1242 inline typename m_Intrinsic_Ty<T0>::Ty
1243 m_Intrinsic(const T0 &Op0) {
1244 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1247 template<Intrinsic::ID IntrID, typename T0, typename T1>
1248 inline typename m_Intrinsic_Ty<T0, T1>::Ty
1249 m_Intrinsic(const T0 &Op0, const T1 &Op1) {
1250 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1253 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1254 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1255 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1256 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1259 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2, typename T3>
1260 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1261 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1262 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1265 // Helper intrinsic matching specializations
1266 template<typename Opnd0>
1267 inline typename m_Intrinsic_Ty<Opnd0>::Ty
1268 m_BSwap(const Opnd0 &Op0) {
1269 return m_Intrinsic<Intrinsic::bswap>(Op0);
1272 template<typename Opnd0, typename Opnd1>
1273 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1274 m_FMin(const Opnd0 &Op0, const Opnd1 &Op1) {
1275 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1278 template<typename Opnd0, typename Opnd1>
1279 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1280 m_FMax(const Opnd0 &Op0, const Opnd1 &Op1) {
1281 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1284 } // end namespace PatternMatch
1285 } // end namespace llvm