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_Instruction() - Match an arbitrary instruction and ignore it.
72 inline class_match<Instruction> m_Instruction() {
73 return class_match<Instruction>();
75 /// m_BinOp() - Match an arbitrary binary operation and ignore it.
76 inline class_match<BinaryOperator> m_BinOp() {
77 return class_match<BinaryOperator>();
79 /// m_Cmp() - Matches any compare instruction and ignore it.
80 inline class_match<CmpInst> m_Cmp() {
81 return class_match<CmpInst>();
83 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
84 inline class_match<ConstantInt> m_ConstantInt() {
85 return class_match<ConstantInt>();
87 /// m_Undef() - Match an arbitrary undef constant.
88 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
90 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
92 /// Matching combinators
93 template<typename LTy, typename RTy>
94 struct match_combine_or {
98 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
100 template<typename ITy>
110 template<typename LTy, typename RTy>
111 struct match_combine_and {
115 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
117 template<typename ITy>
126 /// Combine two pattern matchers matching L || R
127 template<typename LTy, typename RTy>
128 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
129 return match_combine_or<LTy, RTy>(L, R);
132 /// Combine two pattern matchers matching L && R
133 template<typename LTy, typename RTy>
134 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
135 return match_combine_and<LTy, RTy>(L, R);
139 template<typename ITy>
141 if (const Constant *C = dyn_cast<Constant>(V))
142 return C->isNullValue();
147 /// m_Zero() - Match an arbitrary zero/null constant. This includes
148 /// zero_initializer for vectors and ConstantPointerNull for pointers.
149 inline match_zero m_Zero() { return match_zero(); }
151 struct match_neg_zero {
152 template<typename ITy>
154 if (const Constant *C = dyn_cast<Constant>(V))
155 return C->isNegativeZeroValue();
160 /// m_NegZero() - Match an arbitrary zero/null constant. This includes
161 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
162 /// floating point constants, this will match negative zero but not positive
164 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
166 /// m_AnyZero() - Match an arbitrary zero/null constant. This includes
167 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
168 /// floating point constants, this will match negative zero and positive zero
169 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
170 return m_CombineOr(m_Zero(), m_NegZero());
175 apint_match(const APInt *&R) : Res(R) {}
176 template<typename ITy>
178 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
179 Res = &CI->getValue();
182 if (V->getType()->isVectorTy())
183 if (const Constant *C = dyn_cast<Constant>(V))
184 if (ConstantInt *CI =
185 dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
186 Res = &CI->getValue();
193 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
194 /// specified pointer to the contained APInt.
195 inline apint_match m_APInt(const APInt *&Res) { return Res; }
198 template<int64_t Val>
199 struct constantint_match {
200 template<typename ITy>
202 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
203 const APInt &CIV = CI->getValue();
205 return CIV == static_cast<uint64_t>(Val);
206 // If Val is negative, and CI is shorter than it, truncate to the right
207 // number of bits. If it is larger, then we have to sign extend. Just
208 // compare their negated values.
215 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
216 template<int64_t Val>
217 inline constantint_match<Val> m_ConstantInt() {
218 return constantint_match<Val>();
221 /// cst_pred_ty - This helper class is used to match scalar and vector constants
222 /// that satisfy a specified predicate.
223 template<typename Predicate>
224 struct cst_pred_ty : public Predicate {
225 template<typename ITy>
227 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
228 return this->isValue(CI->getValue());
229 if (V->getType()->isVectorTy())
230 if (const Constant *C = dyn_cast<Constant>(V))
231 if (const ConstantInt *CI =
232 dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
233 return this->isValue(CI->getValue());
238 /// api_pred_ty - This helper class is used to match scalar and vector constants
239 /// that satisfy a specified predicate, and bind them to an APInt.
240 template<typename Predicate>
241 struct api_pred_ty : public Predicate {
243 api_pred_ty(const APInt *&R) : Res(R) {}
244 template<typename ITy>
246 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
247 if (this->isValue(CI->getValue())) {
248 Res = &CI->getValue();
251 if (V->getType()->isVectorTy())
252 if (const Constant *C = dyn_cast<Constant>(V))
253 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
254 if (this->isValue(CI->getValue())) {
255 Res = &CI->getValue();
265 bool isValue(const APInt &C) { return C == 1; }
268 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
269 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
270 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
273 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
276 /// m_AllOnes() - Match an integer or vector with all bits set to true.
277 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
278 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
281 bool isValue(const APInt &C) { return C.isSignBit(); }
284 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
285 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
286 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
289 bool isValue(const APInt &C) { return C.isPowerOf2(); }
292 /// m_Power2() - Match an integer or vector power of 2.
293 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
294 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
296 template<typename Class>
299 bind_ty(Class *&V) : VR(V) {}
301 template<typename ITy>
303 if (Class *CV = dyn_cast<Class>(V)) {
311 /// m_Value - Match a value, capturing it if we match.
312 inline bind_ty<Value> m_Value(Value *&V) { return V; }
314 /// m_Instruction - Match a instruction, capturing it if we match.
315 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
317 /// m_BinOp - Match a instruction, capturing it if we match.
318 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
320 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
321 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
323 /// m_Constant - Match a Constant, capturing the value if we match.
324 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
326 /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
327 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
329 /// specificval_ty - Match a specified Value*.
330 struct specificval_ty {
332 specificval_ty(const Value *V) : Val(V) {}
334 template<typename ITy>
340 /// m_Specific - Match if we have a specific specified value.
341 inline specificval_ty m_Specific(const Value *V) { return V; }
343 /// Match a specified floating point value or vector of all elements of that
345 struct specific_fpval {
347 specific_fpval(double V) : Val(V) {}
349 template<typename ITy>
351 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
352 return CFP->isExactlyValue(Val);
353 if (V->getType()->isVectorTy())
354 if (const Constant *C = dyn_cast<Constant>(V))
355 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
356 return CFP->isExactlyValue(Val);
361 /// Match a specific floating point value or vector with all elements equal to
363 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
365 /// Match a float 1.0 or vector with all elements equal to 1.0.
366 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
368 struct bind_const_intval_ty {
370 bind_const_intval_ty(uint64_t &V) : VR(V) {}
372 template<typename ITy>
374 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
375 if (CV->getBitWidth() <= 64) {
376 VR = CV->getZExtValue();
383 /// Match a specified integer value or vector of all elements of that value.
384 struct specific_intval {
386 specific_intval(uint64_t V) : Val(V) {}
388 template<typename ITy>
390 ConstantInt *CI = dyn_cast<ConstantInt>(V);
391 if (!CI && V->getType()->isVectorTy())
392 if (const auto *C = dyn_cast<Constant>(V))
393 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
395 if (CI && CI->getBitWidth() <= 64)
396 return CI->getZExtValue() == Val;
402 /// Match a specific integer value or vector with all elements equal to the
404 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
406 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
407 /// match ConstantInts wider than 64-bits.
408 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
410 //===----------------------------------------------------------------------===//
411 // Matcher for any binary operator.
413 template<typename LHS_t, typename RHS_t>
414 struct AnyBinaryOp_match {
418 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
420 template<typename OpTy>
421 bool match(OpTy *V) {
422 if (auto *I = dyn_cast<BinaryOperator>(V))
423 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
428 template<typename LHS, typename RHS>
429 inline AnyBinaryOp_match<LHS, RHS>
430 m_BinOp(const LHS &L, const RHS &R) {
431 return AnyBinaryOp_match<LHS, RHS>(L, R);
434 //===----------------------------------------------------------------------===//
435 // Matchers for specific binary operators.
438 template<typename LHS_t, typename RHS_t, unsigned Opcode>
439 struct BinaryOp_match {
443 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
445 template<typename OpTy>
446 bool match(OpTy *V) {
447 if (V->getValueID() == Value::InstructionVal + Opcode) {
448 BinaryOperator *I = cast<BinaryOperator>(V);
449 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
451 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
452 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
453 R.match(CE->getOperand(1));
458 template<typename LHS, typename RHS>
459 inline BinaryOp_match<LHS, RHS, Instruction::Add>
460 m_Add(const LHS &L, const RHS &R) {
461 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
464 template<typename LHS, typename RHS>
465 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
466 m_FAdd(const LHS &L, const RHS &R) {
467 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
470 template<typename LHS, typename RHS>
471 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
472 m_Sub(const LHS &L, const RHS &R) {
473 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
476 template<typename LHS, typename RHS>
477 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
478 m_FSub(const LHS &L, const RHS &R) {
479 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
482 template<typename LHS, typename RHS>
483 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
484 m_Mul(const LHS &L, const RHS &R) {
485 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
488 template<typename LHS, typename RHS>
489 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
490 m_FMul(const LHS &L, const RHS &R) {
491 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
494 template<typename LHS, typename RHS>
495 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
496 m_UDiv(const LHS &L, const RHS &R) {
497 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
500 template<typename LHS, typename RHS>
501 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
502 m_SDiv(const LHS &L, const RHS &R) {
503 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
506 template<typename LHS, typename RHS>
507 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
508 m_FDiv(const LHS &L, const RHS &R) {
509 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
512 template<typename LHS, typename RHS>
513 inline BinaryOp_match<LHS, RHS, Instruction::URem>
514 m_URem(const LHS &L, const RHS &R) {
515 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
518 template<typename LHS, typename RHS>
519 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
520 m_SRem(const LHS &L, const RHS &R) {
521 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
524 template<typename LHS, typename RHS>
525 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
526 m_FRem(const LHS &L, const RHS &R) {
527 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
530 template<typename LHS, typename RHS>
531 inline BinaryOp_match<LHS, RHS, Instruction::And>
532 m_And(const LHS &L, const RHS &R) {
533 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
536 template<typename LHS, typename RHS>
537 inline BinaryOp_match<LHS, RHS, Instruction::Or>
538 m_Or(const LHS &L, const RHS &R) {
539 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
542 template<typename LHS, typename RHS>
543 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
544 m_Xor(const LHS &L, const RHS &R) {
545 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
548 template<typename LHS, typename RHS>
549 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
550 m_Shl(const LHS &L, const RHS &R) {
551 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
554 template<typename LHS, typename RHS>
555 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
556 m_LShr(const LHS &L, const RHS &R) {
557 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
560 template<typename LHS, typename RHS>
561 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
562 m_AShr(const LHS &L, const RHS &R) {
563 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
566 template<typename LHS_t, typename RHS_t, unsigned Opcode, unsigned WrapFlags = 0>
567 struct OverflowingBinaryOp_match {
571 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
573 template<typename OpTy>
574 bool match(OpTy *V) {
575 if (OverflowingBinaryOperator *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
576 if (Op->getOpcode() != Opcode)
578 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
579 !Op->hasNoUnsignedWrap())
581 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
582 !Op->hasNoSignedWrap())
584 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
590 template <typename LHS, typename RHS>
591 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
592 OverflowingBinaryOperator::NoSignedWrap>
593 m_NSWAdd(const LHS &L, const RHS &R) {
594 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
595 OverflowingBinaryOperator::NoSignedWrap>(
598 template <typename LHS, typename RHS>
599 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
600 OverflowingBinaryOperator::NoSignedWrap>
601 m_NSWSub(const LHS &L, const RHS &R) {
602 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
603 OverflowingBinaryOperator::NoSignedWrap>(
606 template <typename LHS, typename RHS>
607 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
608 OverflowingBinaryOperator::NoSignedWrap>
609 m_NSWMul(const LHS &L, const RHS &R) {
610 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
611 OverflowingBinaryOperator::NoSignedWrap>(
614 template <typename LHS, typename RHS>
615 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
616 OverflowingBinaryOperator::NoSignedWrap>
617 m_NSWShl(const LHS &L, const RHS &R) {
618 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
619 OverflowingBinaryOperator::NoSignedWrap>(
623 template <typename LHS, typename RHS>
624 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
625 OverflowingBinaryOperator::NoUnsignedWrap>
626 m_NUWAdd(const LHS &L, const RHS &R) {
627 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
628 OverflowingBinaryOperator::NoUnsignedWrap>(
631 template <typename LHS, typename RHS>
632 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
633 OverflowingBinaryOperator::NoUnsignedWrap>
634 m_NUWSub(const LHS &L, const RHS &R) {
635 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
636 OverflowingBinaryOperator::NoUnsignedWrap>(
639 template <typename LHS, typename RHS>
640 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
641 OverflowingBinaryOperator::NoUnsignedWrap>
642 m_NUWMul(const LHS &L, const RHS &R) {
643 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
644 OverflowingBinaryOperator::NoUnsignedWrap>(
647 template <typename LHS, typename RHS>
648 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
649 OverflowingBinaryOperator::NoUnsignedWrap>
650 m_NUWShl(const LHS &L, const RHS &R) {
651 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
652 OverflowingBinaryOperator::NoUnsignedWrap>(
656 //===----------------------------------------------------------------------===//
657 // Class that matches two different binary ops.
659 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
660 struct BinOp2_match {
664 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
666 template<typename OpTy>
667 bool match(OpTy *V) {
668 if (V->getValueID() == Value::InstructionVal + Opc1 ||
669 V->getValueID() == Value::InstructionVal + Opc2) {
670 BinaryOperator *I = cast<BinaryOperator>(V);
671 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
673 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
674 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
675 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
680 /// m_Shr - Matches LShr or AShr.
681 template<typename LHS, typename RHS>
682 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
683 m_Shr(const LHS &L, const RHS &R) {
684 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
687 /// m_LogicalShift - Matches LShr or Shl.
688 template<typename LHS, typename RHS>
689 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
690 m_LogicalShift(const LHS &L, const RHS &R) {
691 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
694 /// m_IDiv - Matches UDiv and SDiv.
695 template<typename LHS, typename RHS>
696 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
697 m_IDiv(const LHS &L, const RHS &R) {
698 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
701 //===----------------------------------------------------------------------===//
702 // Class that matches exact binary ops.
704 template<typename SubPattern_t>
706 SubPattern_t SubPattern;
708 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
710 template<typename OpTy>
711 bool match(OpTy *V) {
712 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
713 return PEO->isExact() && SubPattern.match(V);
719 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
721 //===----------------------------------------------------------------------===//
722 // Matchers for CmpInst classes
725 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
726 struct CmpClass_match {
727 PredicateTy &Predicate;
731 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
732 : Predicate(Pred), L(LHS), R(RHS) {}
734 template<typename OpTy>
735 bool match(OpTy *V) {
736 if (Class *I = dyn_cast<Class>(V))
737 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
738 Predicate = I->getPredicate();
745 template<typename LHS, typename RHS>
746 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
747 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
748 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
751 template<typename LHS, typename RHS>
752 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
753 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
754 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
757 template<typename LHS, typename RHS>
758 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
759 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
760 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
763 //===----------------------------------------------------------------------===//
764 // Matchers for SelectInst classes
767 template<typename Cond_t, typename LHS_t, typename RHS_t>
768 struct SelectClass_match {
773 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
775 : C(Cond), L(LHS), R(RHS) {}
777 template<typename OpTy>
778 bool match(OpTy *V) {
779 if (SelectInst *I = dyn_cast<SelectInst>(V))
780 return C.match(I->getOperand(0)) &&
781 L.match(I->getOperand(1)) &&
782 R.match(I->getOperand(2));
787 template<typename Cond, typename LHS, typename RHS>
788 inline SelectClass_match<Cond, LHS, RHS>
789 m_Select(const Cond &C, const LHS &L, const RHS &R) {
790 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
793 /// m_SelectCst - This matches a select of two constants, e.g.:
794 /// m_SelectCst<-1, 0>(m_Value(V))
795 template<int64_t L, int64_t R, typename Cond>
796 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
797 m_SelectCst(const Cond &C) {
798 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
802 //===----------------------------------------------------------------------===//
803 // Matchers for CastInst classes
806 template<typename Op_t, unsigned Opcode>
807 struct CastClass_match {
810 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
812 template<typename OpTy>
813 bool match(OpTy *V) {
814 if (Operator *O = dyn_cast<Operator>(V))
815 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
821 template<typename OpTy>
822 inline CastClass_match<OpTy, Instruction::BitCast>
823 m_BitCast(const OpTy &Op) {
824 return CastClass_match<OpTy, Instruction::BitCast>(Op);
828 template<typename OpTy>
829 inline CastClass_match<OpTy, Instruction::PtrToInt>
830 m_PtrToInt(const OpTy &Op) {
831 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
835 template<typename OpTy>
836 inline CastClass_match<OpTy, Instruction::Trunc>
837 m_Trunc(const OpTy &Op) {
838 return CastClass_match<OpTy, Instruction::Trunc>(Op);
842 template<typename OpTy>
843 inline CastClass_match<OpTy, Instruction::SExt>
844 m_SExt(const OpTy &Op) {
845 return CastClass_match<OpTy, Instruction::SExt>(Op);
849 template<typename OpTy>
850 inline CastClass_match<OpTy, Instruction::ZExt>
851 m_ZExt(const OpTy &Op) {
852 return CastClass_match<OpTy, Instruction::ZExt>(Op);
856 template<typename OpTy>
857 inline CastClass_match<OpTy, Instruction::UIToFP>
858 m_UIToFP(const OpTy &Op) {
859 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
863 template<typename OpTy>
864 inline CastClass_match<OpTy, Instruction::SIToFP>
865 m_SIToFP(const OpTy &Op) {
866 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
869 //===----------------------------------------------------------------------===//
870 // Matchers for unary operators
873 template<typename LHS_t>
877 not_match(const LHS_t &LHS) : L(LHS) {}
879 template<typename OpTy>
880 bool match(OpTy *V) {
881 if (Operator *O = dyn_cast<Operator>(V))
882 if (O->getOpcode() == Instruction::Xor)
883 return matchIfNot(O->getOperand(0), O->getOperand(1));
887 bool matchIfNot(Value *LHS, Value *RHS) {
888 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
890 isa<ConstantVector>(RHS)) &&
891 cast<Constant>(RHS)->isAllOnesValue() &&
896 template<typename LHS>
897 inline not_match<LHS> m_Not(const LHS &L) { return L; }
900 template<typename LHS_t>
904 neg_match(const LHS_t &LHS) : L(LHS) {}
906 template<typename OpTy>
907 bool match(OpTy *V) {
908 if (Operator *O = dyn_cast<Operator>(V))
909 if (O->getOpcode() == Instruction::Sub)
910 return matchIfNeg(O->getOperand(0), O->getOperand(1));
914 bool matchIfNeg(Value *LHS, Value *RHS) {
915 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
916 isa<ConstantAggregateZero>(LHS)) &&
921 /// m_Neg - Match an integer negate.
922 template<typename LHS>
923 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
926 template<typename LHS_t>
930 fneg_match(const LHS_t &LHS) : L(LHS) {}
932 template<typename OpTy>
933 bool match(OpTy *V) {
934 if (Operator *O = dyn_cast<Operator>(V))
935 if (O->getOpcode() == Instruction::FSub)
936 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
940 bool matchIfFNeg(Value *LHS, Value *RHS) {
941 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
942 return C->isNegativeZeroValue() && L.match(RHS);
947 /// m_FNeg - Match a floating point negate.
948 template<typename LHS>
949 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
952 //===----------------------------------------------------------------------===//
953 // Matchers for control flow.
958 br_match(BasicBlock *&Succ)
962 template<typename OpTy>
963 bool match(OpTy *V) {
964 if (BranchInst *BI = dyn_cast<BranchInst>(V))
965 if (BI->isUnconditional()) {
966 Succ = BI->getSuccessor(0);
973 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
975 template<typename Cond_t>
979 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
980 : Cond(C), T(t), F(f) {
983 template<typename OpTy>
984 bool match(OpTy *V) {
985 if (BranchInst *BI = dyn_cast<BranchInst>(V))
986 if (BI->isConditional() && Cond.match(BI->getCondition())) {
987 T = BI->getSuccessor(0);
988 F = BI->getSuccessor(1);
995 template<typename Cond_t>
996 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
997 return brc_match<Cond_t>(C, T, F);
1001 //===----------------------------------------------------------------------===//
1002 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1005 template<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
1006 struct MaxMin_match {
1010 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
1013 template<typename OpTy>
1014 bool match(OpTy *V) {
1015 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1016 SelectInst *SI = dyn_cast<SelectInst>(V);
1019 CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1022 // At this point we have a select conditioned on a comparison. Check that
1023 // it is the values returned by the select that are being compared.
1024 Value *TrueVal = SI->getTrueValue();
1025 Value *FalseVal = SI->getFalseValue();
1026 Value *LHS = Cmp->getOperand(0);
1027 Value *RHS = Cmp->getOperand(1);
1028 if ((TrueVal != LHS || FalseVal != RHS) &&
1029 (TrueVal != RHS || FalseVal != LHS))
1031 typename CmpInst_t::Predicate Pred = LHS == TrueVal ?
1032 Cmp->getPredicate() : Cmp->getSwappedPredicate();
1033 // Does "(x pred y) ? x : y" represent the desired max/min operation?
1034 if (!Pred_t::match(Pred))
1036 // It does! Bind the operands.
1037 return L.match(LHS) && R.match(RHS);
1041 /// smax_pred_ty - Helper class for identifying signed max predicates.
1042 struct smax_pred_ty {
1043 static bool match(ICmpInst::Predicate Pred) {
1044 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1048 /// smin_pred_ty - Helper class for identifying signed min predicates.
1049 struct smin_pred_ty {
1050 static bool match(ICmpInst::Predicate Pred) {
1051 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1055 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
1056 struct umax_pred_ty {
1057 static bool match(ICmpInst::Predicate Pred) {
1058 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1062 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
1063 struct umin_pred_ty {
1064 static bool match(ICmpInst::Predicate Pred) {
1065 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1069 /// ofmax_pred_ty - Helper class for identifying ordered max predicates.
1070 struct ofmax_pred_ty {
1071 static bool match(FCmpInst::Predicate Pred) {
1072 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1076 /// ofmin_pred_ty - Helper class for identifying ordered min predicates.
1077 struct ofmin_pred_ty {
1078 static bool match(FCmpInst::Predicate Pred) {
1079 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1083 /// ufmax_pred_ty - Helper class for identifying unordered max predicates.
1084 struct ufmax_pred_ty {
1085 static bool match(FCmpInst::Predicate Pred) {
1086 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1090 /// ufmin_pred_ty - Helper class for identifying unordered min predicates.
1091 struct ufmin_pred_ty {
1092 static bool match(FCmpInst::Predicate Pred) {
1093 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1097 template<typename LHS, typename RHS>
1098 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>
1099 m_SMax(const LHS &L, const RHS &R) {
1100 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1103 template<typename LHS, typename RHS>
1104 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>
1105 m_SMin(const LHS &L, const RHS &R) {
1106 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1109 template<typename LHS, typename RHS>
1110 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>
1111 m_UMax(const LHS &L, const RHS &R) {
1112 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1115 template<typename LHS, typename RHS>
1116 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>
1117 m_UMin(const LHS &L, const RHS &R) {
1118 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1121 /// \brief Match an 'ordered' floating point maximum function.
1122 /// Floating point has one special value 'NaN'. Therefore, there is no total
1123 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1124 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1125 /// semantics. In the presence of 'NaN' we have to preserve the original
1126 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1128 /// max(L, R) iff L and R are not NaN
1129 /// m_OrdFMax(L, R) = R iff L or R are NaN
1130 template<typename LHS, typename RHS>
1131 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>
1132 m_OrdFMax(const LHS &L, const RHS &R) {
1133 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1136 /// \brief Match an 'ordered' floating point minimum function.
1137 /// Floating point has one special value 'NaN'. Therefore, there is no total
1138 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1139 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1140 /// semantics. In the presence of 'NaN' we have to preserve the original
1141 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1143 /// max(L, R) iff L and R are not NaN
1144 /// m_OrdFMin(L, R) = R iff L or R are NaN
1145 template<typename LHS, typename RHS>
1146 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>
1147 m_OrdFMin(const LHS &L, const RHS &R) {
1148 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1151 /// \brief Match an 'unordered' floating point maximum function.
1152 /// Floating point has one special value 'NaN'. Therefore, there is no total
1153 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1154 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1155 /// semantics. In the presence of 'NaN' we have to preserve the original
1156 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1158 /// max(L, R) iff L and R are not NaN
1159 /// m_UnordFMin(L, R) = L iff L or R are NaN
1160 template<typename LHS, typename RHS>
1161 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1162 m_UnordFMax(const LHS &L, const RHS &R) {
1163 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1166 /// \brief Match an 'unordered' floating point minimum function.
1167 /// Floating point has one special value 'NaN'. Therefore, there is no total
1168 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1169 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1170 /// semantics. In the presence of 'NaN' we have to preserve the original
1171 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1173 /// max(L, R) iff L and R are not NaN
1174 /// m_UnordFMin(L, R) = L iff L or R are NaN
1175 template<typename LHS, typename RHS>
1176 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1177 m_UnordFMin(const LHS &L, const RHS &R) {
1178 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1181 template<typename Opnd_t>
1182 struct Argument_match {
1185 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
1187 template<typename OpTy>
1188 bool match(OpTy *V) {
1190 return CS.isCall() && Val.match(CS.getArgument(OpI));
1194 /// Match an argument
1195 template<unsigned OpI, typename Opnd_t>
1196 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1197 return Argument_match<Opnd_t>(OpI, Op);
1200 /// Intrinsic matchers.
1201 struct IntrinsicID_match {
1203 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) { }
1205 template<typename OpTy>
1206 bool match(OpTy *V) {
1207 if (const CallInst *CI = dyn_cast<CallInst>(V))
1208 if (const Function *F = CI->getCalledFunction())
1209 return F->getIntrinsicID() == ID;
1214 /// Intrinsic matches are combinations of ID matchers, and argument
1215 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1216 /// them with lower arity matchers. Here's some convenient typedefs for up to
1217 /// several arguments, and more can be added as needed
1218 template <typename T0 = void, typename T1 = void, typename T2 = void,
1219 typename T3 = void, typename T4 = void, typename T5 = void,
1220 typename T6 = void, typename T7 = void, typename T8 = void,
1221 typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
1222 template <typename T0>
1223 struct m_Intrinsic_Ty<T0> {
1224 typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
1226 template <typename T0, typename T1>
1227 struct m_Intrinsic_Ty<T0, T1> {
1228 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
1229 Argument_match<T1> > Ty;
1231 template <typename T0, typename T1, typename T2>
1232 struct m_Intrinsic_Ty<T0, T1, T2> {
1233 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1234 Argument_match<T2> > Ty;
1236 template <typename T0, typename T1, typename T2, typename T3>
1237 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1238 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1239 Argument_match<T3> > Ty;
1242 /// Match intrinsic calls like this:
1243 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1244 template <Intrinsic::ID IntrID>
1245 inline IntrinsicID_match
1246 m_Intrinsic() { return IntrinsicID_match(IntrID); }
1248 template<Intrinsic::ID IntrID, typename T0>
1249 inline typename m_Intrinsic_Ty<T0>::Ty
1250 m_Intrinsic(const T0 &Op0) {
1251 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1254 template<Intrinsic::ID IntrID, typename T0, typename T1>
1255 inline typename m_Intrinsic_Ty<T0, T1>::Ty
1256 m_Intrinsic(const T0 &Op0, const T1 &Op1) {
1257 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1260 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1261 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1262 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1263 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1266 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2, typename T3>
1267 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1268 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1269 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1272 // Helper intrinsic matching specializations
1273 template<typename Opnd0>
1274 inline typename m_Intrinsic_Ty<Opnd0>::Ty
1275 m_BSwap(const Opnd0 &Op0) {
1276 return m_Intrinsic<Intrinsic::bswap>(Op0);
1279 template<typename Opnd0, typename Opnd1>
1280 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1281 m_FMin(const Opnd0 &Op0, const Opnd1 &Op1) {
1282 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1285 template<typename Opnd0, typename Opnd1>
1286 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1287 m_FMax(const Opnd0 &Op0, const Opnd1 &Op1) {
1288 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1291 } // end namespace PatternMatch
1292 } // end namespace llvm