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/IntrinsicInst.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_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
72 inline class_match<ConstantInt> m_ConstantInt() {
73 return class_match<ConstantInt>();
75 /// m_Undef() - Match an arbitrary undef constant.
76 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
78 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
80 /// Matching combinators
81 template<typename LTy, typename RTy>
82 struct match_combine_or {
86 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
88 template<typename ITy>
98 template<typename LTy, typename RTy>
99 struct match_combine_and {
103 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
105 template<typename ITy>
114 /// Combine two pattern matchers matching L || R
115 template<typename LTy, typename RTy>
116 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
117 return match_combine_or<LTy, RTy>(L, R);
120 /// Combine two pattern matchers matching L && R
121 template<typename LTy, typename RTy>
122 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
123 return match_combine_and<LTy, RTy>(L, R);
127 template<typename ITy>
129 if (const Constant *C = dyn_cast<Constant>(V))
130 return C->isNullValue();
135 /// m_Zero() - 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>
142 if (const Constant *C = dyn_cast<Constant>(V))
143 return C->isNegativeZeroValue();
148 /// m_NegZero() - Match an arbitrary zero/null constant. This includes
149 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
150 /// floating point constants, this will match negative zero but not positive
152 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
154 /// m_AnyZero() - Match an arbitrary zero/null constant. This includes
155 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
156 /// floating point constants, this will match negative zero and positive zero
157 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
158 return m_CombineOr(m_Zero(), m_NegZero());
163 apint_match(const APInt *&R) : Res(R) {}
164 template<typename ITy>
166 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
167 Res = &CI->getValue();
170 if (V->getType()->isVectorTy())
171 if (const Constant *C = dyn_cast<Constant>(V))
172 if (ConstantInt *CI =
173 dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
174 Res = &CI->getValue();
181 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
182 /// specified pointer to the contained APInt.
183 inline apint_match m_APInt(const APInt *&Res) { return Res; }
186 template<int64_t Val>
187 struct constantint_match {
188 template<typename ITy>
190 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
191 const APInt &CIV = CI->getValue();
193 return CIV == static_cast<uint64_t>(Val);
194 // If Val is negative, and CI is shorter than it, truncate to the right
195 // number of bits. If it is larger, then we have to sign extend. Just
196 // compare their negated values.
203 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
204 template<int64_t Val>
205 inline constantint_match<Val> m_ConstantInt() {
206 return constantint_match<Val>();
209 /// cst_pred_ty - This helper class is used to match scalar and vector constants
210 /// that satisfy a specified predicate.
211 template<typename Predicate>
212 struct cst_pred_ty : public Predicate {
213 template<typename ITy>
215 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
216 return this->isValue(CI->getValue());
217 if (V->getType()->isVectorTy())
218 if (const Constant *C = dyn_cast<Constant>(V))
219 if (const ConstantInt *CI =
220 dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
221 return this->isValue(CI->getValue());
226 /// api_pred_ty - This helper class is used to match scalar and vector constants
227 /// that satisfy a specified predicate, and bind them to an APInt.
228 template<typename Predicate>
229 struct api_pred_ty : public Predicate {
231 api_pred_ty(const APInt *&R) : Res(R) {}
232 template<typename ITy>
234 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
235 if (this->isValue(CI->getValue())) {
236 Res = &CI->getValue();
239 if (V->getType()->isVectorTy())
240 if (const Constant *C = dyn_cast<Constant>(V))
241 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
242 if (this->isValue(CI->getValue())) {
243 Res = &CI->getValue();
253 bool isValue(const APInt &C) { return C == 1; }
256 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
257 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
258 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
261 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
264 /// m_AllOnes() - Match an integer or vector with all bits set to true.
265 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
266 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
269 bool isValue(const APInt &C) { return C.isSignBit(); }
272 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
273 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
274 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
277 bool isValue(const APInt &C) { return C.isPowerOf2(); }
280 /// m_Power2() - Match an integer or vector power of 2.
281 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
282 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
284 template<typename Class>
287 bind_ty(Class *&V) : VR(V) {}
289 template<typename ITy>
291 if (Class *CV = dyn_cast<Class>(V)) {
299 /// m_Value - Match a value, capturing it if we match.
300 inline bind_ty<Value> m_Value(Value *&V) { return V; }
302 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
303 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
305 /// m_Constant - Match a Constant, capturing the value if we match.
306 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
308 /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
309 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
311 /// specificval_ty - Match a specified Value*.
312 struct specificval_ty {
314 specificval_ty(const Value *V) : Val(V) {}
316 template<typename ITy>
322 /// m_Specific - Match if we have a specific specified value.
323 inline specificval_ty m_Specific(const Value *V) { return V; }
325 /// Match a specified floating point value or vector of all elements of that
327 struct specific_fpval {
329 specific_fpval(double V) : Val(V) {}
331 template<typename ITy>
333 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
334 return CFP->isExactlyValue(Val);
335 if (V->getType()->isVectorTy())
336 if (const Constant *C = dyn_cast<Constant>(V))
337 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
338 return CFP->isExactlyValue(Val);
343 /// Match a specific floating point value or vector with all elements equal to
345 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
347 /// Match a float 1.0 or vector with all elements equal to 1.0.
348 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
350 struct bind_const_intval_ty {
352 bind_const_intval_ty(uint64_t &V) : VR(V) {}
354 template<typename ITy>
356 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
357 if (CV->getBitWidth() <= 64) {
358 VR = CV->getZExtValue();
365 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
366 /// match ConstantInts wider than 64-bits.
367 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
369 //===----------------------------------------------------------------------===//
370 // Matchers for specific binary operators.
373 template<typename LHS_t, typename RHS_t, unsigned Opcode>
374 struct BinaryOp_match {
378 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
380 template<typename OpTy>
381 bool match(OpTy *V) {
382 if (V->getValueID() == Value::InstructionVal + Opcode) {
383 BinaryOperator *I = cast<BinaryOperator>(V);
384 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
386 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
387 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
388 R.match(CE->getOperand(1));
393 template<typename LHS, typename RHS>
394 inline BinaryOp_match<LHS, RHS, Instruction::Add>
395 m_Add(const LHS &L, const RHS &R) {
396 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
399 template<typename LHS, typename RHS>
400 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
401 m_FAdd(const LHS &L, const RHS &R) {
402 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
405 template<typename LHS, typename RHS>
406 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
407 m_Sub(const LHS &L, const RHS &R) {
408 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
411 template<typename LHS, typename RHS>
412 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
413 m_FSub(const LHS &L, const RHS &R) {
414 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
417 template<typename LHS, typename RHS>
418 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
419 m_Mul(const LHS &L, const RHS &R) {
420 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
423 template<typename LHS, typename RHS>
424 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
425 m_FMul(const LHS &L, const RHS &R) {
426 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
429 template<typename LHS, typename RHS>
430 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
431 m_UDiv(const LHS &L, const RHS &R) {
432 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
435 template<typename LHS, typename RHS>
436 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
437 m_SDiv(const LHS &L, const RHS &R) {
438 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
441 template<typename LHS, typename RHS>
442 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
443 m_FDiv(const LHS &L, const RHS &R) {
444 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
447 template<typename LHS, typename RHS>
448 inline BinaryOp_match<LHS, RHS, Instruction::URem>
449 m_URem(const LHS &L, const RHS &R) {
450 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
453 template<typename LHS, typename RHS>
454 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
455 m_SRem(const LHS &L, const RHS &R) {
456 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
459 template<typename LHS, typename RHS>
460 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
461 m_FRem(const LHS &L, const RHS &R) {
462 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
465 template<typename LHS, typename RHS>
466 inline BinaryOp_match<LHS, RHS, Instruction::And>
467 m_And(const LHS &L, const RHS &R) {
468 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
471 template<typename LHS, typename RHS>
472 inline BinaryOp_match<LHS, RHS, Instruction::Or>
473 m_Or(const LHS &L, const RHS &R) {
474 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
477 template<typename LHS, typename RHS>
478 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
479 m_Xor(const LHS &L, const RHS &R) {
480 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
483 template<typename LHS, typename RHS>
484 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
485 m_Shl(const LHS &L, const RHS &R) {
486 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
489 template<typename LHS, typename RHS>
490 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
491 m_LShr(const LHS &L, const RHS &R) {
492 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
495 template<typename LHS, typename RHS>
496 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
497 m_AShr(const LHS &L, const RHS &R) {
498 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
501 template<typename LHS_t, typename RHS_t, unsigned Opcode, unsigned WrapFlags = 0>
502 struct OverflowingBinaryOp_match {
506 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
508 template<typename OpTy>
509 bool match(OpTy *V) {
510 if (OverflowingBinaryOperator *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
511 if (Op->getOpcode() != Opcode)
513 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
514 !Op->hasNoUnsignedWrap())
516 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
517 !Op->hasNoSignedWrap())
519 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
525 template <typename LHS, typename RHS>
526 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
527 OverflowingBinaryOperator::NoSignedWrap>
528 m_NSWAdd(const LHS &L, const RHS &R) {
529 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
530 OverflowingBinaryOperator::NoSignedWrap>(
533 template <typename LHS, typename RHS>
534 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
535 OverflowingBinaryOperator::NoSignedWrap>
536 m_NSWSub(const LHS &L, const RHS &R) {
537 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
538 OverflowingBinaryOperator::NoSignedWrap>(
541 template <typename LHS, typename RHS>
542 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
543 OverflowingBinaryOperator::NoSignedWrap>
544 m_NSWMul(const LHS &L, const RHS &R) {
545 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
546 OverflowingBinaryOperator::NoSignedWrap>(
549 template <typename LHS, typename RHS>
550 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
551 OverflowingBinaryOperator::NoSignedWrap>
552 m_NSWShl(const LHS &L, const RHS &R) {
553 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
554 OverflowingBinaryOperator::NoSignedWrap>(
558 template <typename LHS, typename RHS>
559 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
560 OverflowingBinaryOperator::NoUnsignedWrap>
561 m_NUWAdd(const LHS &L, const RHS &R) {
562 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
563 OverflowingBinaryOperator::NoUnsignedWrap>(
566 template <typename LHS, typename RHS>
567 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
568 OverflowingBinaryOperator::NoUnsignedWrap>
569 m_NUWSub(const LHS &L, const RHS &R) {
570 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
571 OverflowingBinaryOperator::NoUnsignedWrap>(
574 template <typename LHS, typename RHS>
575 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
576 OverflowingBinaryOperator::NoUnsignedWrap>
577 m_NUWMul(const LHS &L, const RHS &R) {
578 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
579 OverflowingBinaryOperator::NoUnsignedWrap>(
582 template <typename LHS, typename RHS>
583 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
584 OverflowingBinaryOperator::NoUnsignedWrap>
585 m_NUWShl(const LHS &L, const RHS &R) {
586 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
587 OverflowingBinaryOperator::NoUnsignedWrap>(
591 //===----------------------------------------------------------------------===//
592 // Class that matches two different binary ops.
594 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
595 struct BinOp2_match {
599 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
601 template<typename OpTy>
602 bool match(OpTy *V) {
603 if (V->getValueID() == Value::InstructionVal + Opc1 ||
604 V->getValueID() == Value::InstructionVal + Opc2) {
605 BinaryOperator *I = cast<BinaryOperator>(V);
606 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
608 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
609 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
610 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
615 /// m_Shr - Matches LShr or AShr.
616 template<typename LHS, typename RHS>
617 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
618 m_Shr(const LHS &L, const RHS &R) {
619 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
622 /// m_LogicalShift - Matches LShr or Shl.
623 template<typename LHS, typename RHS>
624 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
625 m_LogicalShift(const LHS &L, const RHS &R) {
626 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
629 /// m_IDiv - Matches UDiv and SDiv.
630 template<typename LHS, typename RHS>
631 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
632 m_IDiv(const LHS &L, const RHS &R) {
633 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
636 //===----------------------------------------------------------------------===//
637 // Class that matches exact binary ops.
639 template<typename SubPattern_t>
641 SubPattern_t SubPattern;
643 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
645 template<typename OpTy>
646 bool match(OpTy *V) {
647 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
648 return PEO->isExact() && SubPattern.match(V);
654 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
656 //===----------------------------------------------------------------------===//
657 // Matchers for CmpInst classes
660 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
661 struct CmpClass_match {
662 PredicateTy &Predicate;
666 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
667 : Predicate(Pred), L(LHS), R(RHS) {}
669 template<typename OpTy>
670 bool match(OpTy *V) {
671 if (Class *I = dyn_cast<Class>(V))
672 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
673 Predicate = I->getPredicate();
680 template<typename LHS, typename RHS>
681 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
682 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
683 return CmpClass_match<LHS, RHS,
684 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
687 template<typename LHS, typename RHS>
688 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
689 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
690 return CmpClass_match<LHS, RHS,
691 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
694 //===----------------------------------------------------------------------===//
695 // Matchers for SelectInst classes
698 template<typename Cond_t, typename LHS_t, typename RHS_t>
699 struct SelectClass_match {
704 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
706 : C(Cond), L(LHS), R(RHS) {}
708 template<typename OpTy>
709 bool match(OpTy *V) {
710 if (SelectInst *I = dyn_cast<SelectInst>(V))
711 return C.match(I->getOperand(0)) &&
712 L.match(I->getOperand(1)) &&
713 R.match(I->getOperand(2));
718 template<typename Cond, typename LHS, typename RHS>
719 inline SelectClass_match<Cond, LHS, RHS>
720 m_Select(const Cond &C, const LHS &L, const RHS &R) {
721 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
724 /// m_SelectCst - This matches a select of two constants, e.g.:
725 /// m_SelectCst<-1, 0>(m_Value(V))
726 template<int64_t L, int64_t R, typename Cond>
727 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
728 m_SelectCst(const Cond &C) {
729 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
733 //===----------------------------------------------------------------------===//
734 // Matchers for CastInst classes
737 template<typename Op_t, unsigned Opcode>
738 struct CastClass_match {
741 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
743 template<typename OpTy>
744 bool match(OpTy *V) {
745 if (Operator *O = dyn_cast<Operator>(V))
746 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
752 template<typename OpTy>
753 inline CastClass_match<OpTy, Instruction::BitCast>
754 m_BitCast(const OpTy &Op) {
755 return CastClass_match<OpTy, Instruction::BitCast>(Op);
759 template<typename OpTy>
760 inline CastClass_match<OpTy, Instruction::PtrToInt>
761 m_PtrToInt(const OpTy &Op) {
762 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
766 template<typename OpTy>
767 inline CastClass_match<OpTy, Instruction::Trunc>
768 m_Trunc(const OpTy &Op) {
769 return CastClass_match<OpTy, Instruction::Trunc>(Op);
773 template<typename OpTy>
774 inline CastClass_match<OpTy, Instruction::SExt>
775 m_SExt(const OpTy &Op) {
776 return CastClass_match<OpTy, Instruction::SExt>(Op);
780 template<typename OpTy>
781 inline CastClass_match<OpTy, Instruction::ZExt>
782 m_ZExt(const OpTy &Op) {
783 return CastClass_match<OpTy, Instruction::ZExt>(Op);
787 template<typename OpTy>
788 inline CastClass_match<OpTy, Instruction::UIToFP>
789 m_UIToFP(const OpTy &Op) {
790 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
794 template<typename OpTy>
795 inline CastClass_match<OpTy, Instruction::SIToFP>
796 m_SIToFP(const OpTy &Op) {
797 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
800 //===----------------------------------------------------------------------===//
801 // Matchers for unary operators
804 template<typename LHS_t>
808 not_match(const LHS_t &LHS) : L(LHS) {}
810 template<typename OpTy>
811 bool match(OpTy *V) {
812 if (Operator *O = dyn_cast<Operator>(V))
813 if (O->getOpcode() == Instruction::Xor)
814 return matchIfNot(O->getOperand(0), O->getOperand(1));
818 bool matchIfNot(Value *LHS, Value *RHS) {
819 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
821 isa<ConstantVector>(RHS)) &&
822 cast<Constant>(RHS)->isAllOnesValue() &&
827 template<typename LHS>
828 inline not_match<LHS> m_Not(const LHS &L) { return L; }
831 template<typename LHS_t>
835 neg_match(const LHS_t &LHS) : L(LHS) {}
837 template<typename OpTy>
838 bool match(OpTy *V) {
839 if (Operator *O = dyn_cast<Operator>(V))
840 if (O->getOpcode() == Instruction::Sub)
841 return matchIfNeg(O->getOperand(0), O->getOperand(1));
845 bool matchIfNeg(Value *LHS, Value *RHS) {
846 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
847 isa<ConstantAggregateZero>(LHS)) &&
852 /// m_Neg - Match an integer negate.
853 template<typename LHS>
854 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
857 template<typename LHS_t>
861 fneg_match(const LHS_t &LHS) : L(LHS) {}
863 template<typename OpTy>
864 bool match(OpTy *V) {
865 if (Operator *O = dyn_cast<Operator>(V))
866 if (O->getOpcode() == Instruction::FSub)
867 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
871 bool matchIfFNeg(Value *LHS, Value *RHS) {
872 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
873 return C->isNegativeZeroValue() && L.match(RHS);
878 /// m_FNeg - Match a floating point negate.
879 template<typename LHS>
880 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
883 //===----------------------------------------------------------------------===//
884 // Matchers for control flow.
889 br_match(BasicBlock *&Succ)
893 template<typename OpTy>
894 bool match(OpTy *V) {
895 if (BranchInst *BI = dyn_cast<BranchInst>(V))
896 if (BI->isUnconditional()) {
897 Succ = BI->getSuccessor(0);
904 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
906 template<typename Cond_t>
910 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
911 : Cond(C), T(t), F(f) {
914 template<typename OpTy>
915 bool match(OpTy *V) {
916 if (BranchInst *BI = dyn_cast<BranchInst>(V))
917 if (BI->isConditional() && Cond.match(BI->getCondition())) {
918 T = BI->getSuccessor(0);
919 F = BI->getSuccessor(1);
926 template<typename Cond_t>
927 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
928 return brc_match<Cond_t>(C, T, F);
932 //===----------------------------------------------------------------------===//
933 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
936 template<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
937 struct MaxMin_match {
941 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
944 template<typename OpTy>
945 bool match(OpTy *V) {
946 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
947 SelectInst *SI = dyn_cast<SelectInst>(V);
950 CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
953 // At this point we have a select conditioned on a comparison. Check that
954 // it is the values returned by the select that are being compared.
955 Value *TrueVal = SI->getTrueValue();
956 Value *FalseVal = SI->getFalseValue();
957 Value *LHS = Cmp->getOperand(0);
958 Value *RHS = Cmp->getOperand(1);
959 if ((TrueVal != LHS || FalseVal != RHS) &&
960 (TrueVal != RHS || FalseVal != LHS))
962 typename CmpInst_t::Predicate Pred = LHS == TrueVal ?
963 Cmp->getPredicate() : Cmp->getSwappedPredicate();
964 // Does "(x pred y) ? x : y" represent the desired max/min operation?
965 if (!Pred_t::match(Pred))
967 // It does! Bind the operands.
968 return L.match(LHS) && R.match(RHS);
972 /// smax_pred_ty - Helper class for identifying signed max predicates.
973 struct smax_pred_ty {
974 static bool match(ICmpInst::Predicate Pred) {
975 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
979 /// smin_pred_ty - Helper class for identifying signed min predicates.
980 struct smin_pred_ty {
981 static bool match(ICmpInst::Predicate Pred) {
982 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
986 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
987 struct umax_pred_ty {
988 static bool match(ICmpInst::Predicate Pred) {
989 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
993 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
994 struct umin_pred_ty {
995 static bool match(ICmpInst::Predicate Pred) {
996 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1000 /// ofmax_pred_ty - Helper class for identifying ordered max predicates.
1001 struct ofmax_pred_ty {
1002 static bool match(FCmpInst::Predicate Pred) {
1003 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1007 /// ofmin_pred_ty - Helper class for identifying ordered min predicates.
1008 struct ofmin_pred_ty {
1009 static bool match(FCmpInst::Predicate Pred) {
1010 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1014 /// ufmax_pred_ty - Helper class for identifying unordered max predicates.
1015 struct ufmax_pred_ty {
1016 static bool match(FCmpInst::Predicate Pred) {
1017 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1021 /// ufmin_pred_ty - Helper class for identifying unordered min predicates.
1022 struct ufmin_pred_ty {
1023 static bool match(FCmpInst::Predicate Pred) {
1024 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1028 template<typename LHS, typename RHS>
1029 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>
1030 m_SMax(const LHS &L, const RHS &R) {
1031 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1034 template<typename LHS, typename RHS>
1035 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>
1036 m_SMin(const LHS &L, const RHS &R) {
1037 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1040 template<typename LHS, typename RHS>
1041 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>
1042 m_UMax(const LHS &L, const RHS &R) {
1043 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1046 template<typename LHS, typename RHS>
1047 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>
1048 m_UMin(const LHS &L, const RHS &R) {
1049 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1052 /// \brief Match an 'ordered' floating point maximum function.
1053 /// Floating point has one special value 'NaN'. Therefore, there is no total
1054 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1055 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1056 /// semantics. In the presence of 'NaN' we have to preserve the original
1057 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1059 /// max(L, R) iff L and R are not NaN
1060 /// m_OrdFMax(L, R) = R iff L or R are NaN
1061 template<typename LHS, typename RHS>
1062 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>
1063 m_OrdFMax(const LHS &L, const RHS &R) {
1064 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1067 /// \brief Match an 'ordered' floating point minimum function.
1068 /// Floating point has one special value 'NaN'. Therefore, there is no total
1069 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1070 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1071 /// semantics. In the presence of 'NaN' we have to preserve the original
1072 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1074 /// max(L, R) iff L and R are not NaN
1075 /// m_OrdFMin(L, R) = R iff L or R are NaN
1076 template<typename LHS, typename RHS>
1077 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>
1078 m_OrdFMin(const LHS &L, const RHS &R) {
1079 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1082 /// \brief Match an 'unordered' floating point maximum function.
1083 /// Floating point has one special value 'NaN'. Therefore, there is no total
1084 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1085 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1086 /// semantics. In the presence of 'NaN' we have to preserve the original
1087 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1089 /// max(L, R) iff L and R are not NaN
1090 /// m_UnordFMin(L, R) = L iff L or R are NaN
1091 template<typename LHS, typename RHS>
1092 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1093 m_UnordFMax(const LHS &L, const RHS &R) {
1094 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1097 /// \brief Match an 'unordered' floating point minimum function.
1098 /// Floating point has one special value 'NaN'. Therefore, there is no total
1099 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1100 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1101 /// semantics. In the presence of 'NaN' we have to preserve the original
1102 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1104 /// max(L, R) iff L and R are not NaN
1105 /// m_UnordFMin(L, R) = L iff L or R are NaN
1106 template<typename LHS, typename RHS>
1107 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1108 m_UnordFMin(const LHS &L, const RHS &R) {
1109 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1112 template<typename Opnd_t>
1113 struct Argument_match {
1116 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
1118 template<typename OpTy>
1119 bool match(OpTy *V) {
1121 return CS.isCall() && Val.match(CS.getArgument(OpI));
1125 /// Match an argument
1126 template<unsigned OpI, typename Opnd_t>
1127 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1128 return Argument_match<Opnd_t>(OpI, Op);
1131 /// Intrinsic matchers.
1132 struct IntrinsicID_match {
1134 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) { }
1136 template<typename OpTy>
1137 bool match(OpTy *V) {
1138 IntrinsicInst *II = dyn_cast<IntrinsicInst>(V);
1139 return II && II->getIntrinsicID() == ID;
1143 /// Intrinsic matches are combinations of ID matchers, and argument
1144 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1145 /// them with lower arity matchers. Here's some convenient typedefs for up to
1146 /// several arguments, and more can be added as needed
1147 template <typename T0 = void, typename T1 = void, typename T2 = void,
1148 typename T3 = void, typename T4 = void, typename T5 = void,
1149 typename T6 = void, typename T7 = void, typename T8 = void,
1150 typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
1151 template <typename T0>
1152 struct m_Intrinsic_Ty<T0> {
1153 typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
1155 template <typename T0, typename T1>
1156 struct m_Intrinsic_Ty<T0, T1> {
1157 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
1158 Argument_match<T1> > Ty;
1160 template <typename T0, typename T1, typename T2>
1161 struct m_Intrinsic_Ty<T0, T1, T2> {
1162 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1163 Argument_match<T2> > Ty;
1165 template <typename T0, typename T1, typename T2, typename T3>
1166 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1167 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1168 Argument_match<T3> > Ty;
1171 /// Match intrinsic calls like this:
1172 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1173 template <Intrinsic::ID IntrID>
1174 inline IntrinsicID_match
1175 m_Intrinsic() { return IntrinsicID_match(IntrID); }
1177 template<Intrinsic::ID IntrID, typename T0>
1178 inline typename m_Intrinsic_Ty<T0>::Ty
1179 m_Intrinsic(const T0 &Op0) {
1180 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1183 template<Intrinsic::ID IntrID, typename T0, typename T1>
1184 inline typename m_Intrinsic_Ty<T0, T1>::Ty
1185 m_Intrinsic(const T0 &Op0, const T1 &Op1) {
1186 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1189 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1190 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1191 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1192 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1195 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2, typename T3>
1196 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1197 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1198 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1201 // Helper intrinsic matching specializations
1202 template<typename Opnd0>
1203 inline typename m_Intrinsic_Ty<Opnd0>::Ty
1204 m_BSwap(const Opnd0 &Op0) {
1205 return m_Intrinsic<Intrinsic::bswap>(Op0);
1208 } // end namespace PatternMatch
1209 } // end namespace llvm