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/Operator.h"
38 namespace PatternMatch {
40 template<typename Val, typename Pattern>
41 bool match(Val *V, const Pattern &P) {
42 return const_cast<Pattern&>(P).match(V);
46 template<typename SubPattern_t>
48 SubPattern_t SubPattern;
50 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
52 template<typename OpTy>
54 return V->hasOneUse() && SubPattern.match(V);
59 inline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; }
62 template<typename Class>
64 template<typename ITy>
65 bool match(ITy *V) { return isa<Class>(V); }
68 /// m_Value() - Match an arbitrary value and ignore it.
69 inline class_match<Value> m_Value() { return class_match<Value>(); }
70 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
71 inline class_match<ConstantInt> m_ConstantInt() {
72 return class_match<ConstantInt>();
74 /// m_Undef() - Match an arbitrary undef constant.
75 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
77 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
79 /// Matching combinators
80 template<typename LTy, typename RTy>
81 struct match_combine_or {
85 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
87 template<typename ITy>
97 template<typename LTy, typename RTy>
98 struct match_combine_and {
102 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
104 template<typename ITy>
113 /// Combine two pattern matchers matching L || R
114 template<typename LTy, typename RTy>
115 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
116 return match_combine_or<LTy, RTy>(L, R);
119 /// Combine two pattern matchers matching L && R
120 template<typename LTy, typename RTy>
121 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
122 return match_combine_and<LTy, RTy>(L, R);
126 template<typename ITy>
128 if (const Constant *C = dyn_cast<Constant>(V))
129 return C->isNullValue();
134 /// m_Zero() - Match an arbitrary zero/null constant. This includes
135 /// zero_initializer for vectors and ConstantPointerNull for pointers.
136 inline match_zero m_Zero() { return match_zero(); }
138 struct match_neg_zero {
139 template<typename ITy>
141 if (const Constant *C = dyn_cast<Constant>(V))
142 return C->isNegativeZeroValue();
147 /// m_NegZero() - Match an arbitrary zero/null constant. This includes
148 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
149 /// floating point constants, this will match negative zero but not positive
151 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
153 /// m_AnyZero() - Match an arbitrary zero/null constant. This includes
154 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
155 /// floating point constants, this will match negative zero and positive zero
156 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
157 return m_CombineOr(m_Zero(), m_NegZero());
162 apint_match(const APInt *&R) : Res(R) {}
163 template<typename ITy>
165 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
166 Res = &CI->getValue();
169 if (V->getType()->isVectorTy())
170 if (const Constant *C = dyn_cast<Constant>(V))
171 if (ConstantInt *CI =
172 dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
173 Res = &CI->getValue();
180 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
181 /// specified pointer to the contained APInt.
182 inline apint_match m_APInt(const APInt *&Res) { return Res; }
185 template<int64_t Val>
186 struct constantint_match {
187 template<typename ITy>
189 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
190 const APInt &CIV = CI->getValue();
192 return CIV == static_cast<uint64_t>(Val);
193 // If Val is negative, and CI is shorter than it, truncate to the right
194 // number of bits. If it is larger, then we have to sign extend. Just
195 // compare their negated values.
202 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
203 template<int64_t Val>
204 inline constantint_match<Val> m_ConstantInt() {
205 return constantint_match<Val>();
208 /// cst_pred_ty - This helper class is used to match scalar and vector constants
209 /// that satisfy a specified predicate.
210 template<typename Predicate>
211 struct cst_pred_ty : public Predicate {
212 template<typename ITy>
214 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
215 return this->isValue(CI->getValue());
216 if (V->getType()->isVectorTy())
217 if (const Constant *C = dyn_cast<Constant>(V))
218 if (const ConstantInt *CI =
219 dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
220 return this->isValue(CI->getValue());
225 /// api_pred_ty - This helper class is used to match scalar and vector constants
226 /// that satisfy a specified predicate, and bind them to an APInt.
227 template<typename Predicate>
228 struct api_pred_ty : public Predicate {
230 api_pred_ty(const APInt *&R) : Res(R) {}
231 template<typename ITy>
233 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
234 if (this->isValue(CI->getValue())) {
235 Res = &CI->getValue();
238 if (V->getType()->isVectorTy())
239 if (const Constant *C = dyn_cast<Constant>(V))
240 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
241 if (this->isValue(CI->getValue())) {
242 Res = &CI->getValue();
252 bool isValue(const APInt &C) { return C == 1; }
255 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
256 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
257 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
260 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
263 /// m_AllOnes() - Match an integer or vector with all bits set to true.
264 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
265 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
268 bool isValue(const APInt &C) { return C.isSignBit(); }
271 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
272 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
273 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
276 bool isValue(const APInt &C) { return C.isPowerOf2(); }
279 /// m_Power2() - Match an integer or vector power of 2.
280 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
281 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
283 template<typename Class>
286 bind_ty(Class *&V) : VR(V) {}
288 template<typename ITy>
290 if (Class *CV = dyn_cast<Class>(V)) {
298 /// m_Value - Match a value, capturing it if we match.
299 inline bind_ty<Value> m_Value(Value *&V) { return V; }
301 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
302 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
304 /// m_Constant - Match a Constant, capturing the value if we match.
305 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
307 /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
308 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
310 /// specificval_ty - Match a specified Value*.
311 struct specificval_ty {
313 specificval_ty(const Value *V) : Val(V) {}
315 template<typename ITy>
321 /// m_Specific - Match if we have a specific specified value.
322 inline specificval_ty m_Specific(const Value *V) { return V; }
324 /// Match a specified floating point value or vector of all elements of that
326 struct specific_fpval {
328 specific_fpval(double V) : Val(V) {}
330 template<typename ITy>
332 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
333 return CFP->isExactlyValue(Val);
334 if (V->getType()->isVectorTy())
335 if (const Constant *C = dyn_cast<Constant>(V))
336 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
337 return CFP->isExactlyValue(Val);
342 /// Match a specific floating point value or vector with all elements equal to
344 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
346 /// Match a float 1.0 or vector with all elements equal to 1.0.
347 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
349 struct bind_const_intval_ty {
351 bind_const_intval_ty(uint64_t &V) : VR(V) {}
353 template<typename ITy>
355 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
356 if (CV->getBitWidth() <= 64) {
357 VR = CV->getZExtValue();
364 /// Match a specified integer value or vector of all elements of that value.
365 struct specific_intval {
367 specific_intval(uint64_t V) : Val(V) {}
369 template<typename ITy>
371 ConstantInt *CI = dyn_cast<ConstantInt>(V);
372 if (!CI && V->getType()->isVectorTy())
373 if (const auto *C = dyn_cast<Constant>(V))
374 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
376 if (CI && CI->getBitWidth() <= 64)
377 return CI->getZExtValue() == Val;
383 /// Match a specific integer value or vector with all elements equal to the
385 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
387 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
388 /// match ConstantInts wider than 64-bits.
389 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
391 //===----------------------------------------------------------------------===//
392 // Matchers for specific binary operators.
395 template<typename LHS_t, typename RHS_t, unsigned Opcode>
396 struct BinaryOp_match {
400 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
402 template<typename OpTy>
403 bool match(OpTy *V) {
404 if (V->getValueID() == Value::InstructionVal + Opcode) {
405 BinaryOperator *I = cast<BinaryOperator>(V);
406 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
408 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
409 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
410 R.match(CE->getOperand(1));
415 template<typename LHS, typename RHS>
416 inline BinaryOp_match<LHS, RHS, Instruction::Add>
417 m_Add(const LHS &L, const RHS &R) {
418 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
421 template<typename LHS, typename RHS>
422 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
423 m_FAdd(const LHS &L, const RHS &R) {
424 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
427 template<typename LHS, typename RHS>
428 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
429 m_Sub(const LHS &L, const RHS &R) {
430 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
433 template<typename LHS, typename RHS>
434 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
435 m_FSub(const LHS &L, const RHS &R) {
436 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
439 template<typename LHS, typename RHS>
440 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
441 m_Mul(const LHS &L, const RHS &R) {
442 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
445 template<typename LHS, typename RHS>
446 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
447 m_FMul(const LHS &L, const RHS &R) {
448 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
451 template<typename LHS, typename RHS>
452 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
453 m_UDiv(const LHS &L, const RHS &R) {
454 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
457 template<typename LHS, typename RHS>
458 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
459 m_SDiv(const LHS &L, const RHS &R) {
460 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
463 template<typename LHS, typename RHS>
464 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
465 m_FDiv(const LHS &L, const RHS &R) {
466 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
469 template<typename LHS, typename RHS>
470 inline BinaryOp_match<LHS, RHS, Instruction::URem>
471 m_URem(const LHS &L, const RHS &R) {
472 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
475 template<typename LHS, typename RHS>
476 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
477 m_SRem(const LHS &L, const RHS &R) {
478 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
481 template<typename LHS, typename RHS>
482 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
483 m_FRem(const LHS &L, const RHS &R) {
484 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
487 template<typename LHS, typename RHS>
488 inline BinaryOp_match<LHS, RHS, Instruction::And>
489 m_And(const LHS &L, const RHS &R) {
490 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
493 template<typename LHS, typename RHS>
494 inline BinaryOp_match<LHS, RHS, Instruction::Or>
495 m_Or(const LHS &L, const RHS &R) {
496 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
499 template<typename LHS, typename RHS>
500 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
501 m_Xor(const LHS &L, const RHS &R) {
502 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
505 template<typename LHS, typename RHS>
506 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
507 m_Shl(const LHS &L, const RHS &R) {
508 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
511 template<typename LHS, typename RHS>
512 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
513 m_LShr(const LHS &L, const RHS &R) {
514 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
517 template<typename LHS, typename RHS>
518 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
519 m_AShr(const LHS &L, const RHS &R) {
520 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
523 template<typename LHS_t, typename RHS_t, unsigned Opcode, unsigned WrapFlags = 0>
524 struct OverflowingBinaryOp_match {
528 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
530 template<typename OpTy>
531 bool match(OpTy *V) {
532 if (OverflowingBinaryOperator *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
533 if (Op->getOpcode() != Opcode)
535 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
536 !Op->hasNoUnsignedWrap())
538 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
539 !Op->hasNoSignedWrap())
541 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
547 template <typename LHS, typename RHS>
548 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
549 OverflowingBinaryOperator::NoSignedWrap>
550 m_NSWAdd(const LHS &L, const RHS &R) {
551 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
552 OverflowingBinaryOperator::NoSignedWrap>(
555 template <typename LHS, typename RHS>
556 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
557 OverflowingBinaryOperator::NoSignedWrap>
558 m_NSWSub(const LHS &L, const RHS &R) {
559 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
560 OverflowingBinaryOperator::NoSignedWrap>(
563 template <typename LHS, typename RHS>
564 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
565 OverflowingBinaryOperator::NoSignedWrap>
566 m_NSWMul(const LHS &L, const RHS &R) {
567 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
568 OverflowingBinaryOperator::NoSignedWrap>(
571 template <typename LHS, typename RHS>
572 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
573 OverflowingBinaryOperator::NoSignedWrap>
574 m_NSWShl(const LHS &L, const RHS &R) {
575 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
576 OverflowingBinaryOperator::NoSignedWrap>(
580 template <typename LHS, typename RHS>
581 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
582 OverflowingBinaryOperator::NoUnsignedWrap>
583 m_NUWAdd(const LHS &L, const RHS &R) {
584 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
585 OverflowingBinaryOperator::NoUnsignedWrap>(
588 template <typename LHS, typename RHS>
589 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
590 OverflowingBinaryOperator::NoUnsignedWrap>
591 m_NUWSub(const LHS &L, const RHS &R) {
592 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
593 OverflowingBinaryOperator::NoUnsignedWrap>(
596 template <typename LHS, typename RHS>
597 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
598 OverflowingBinaryOperator::NoUnsignedWrap>
599 m_NUWMul(const LHS &L, const RHS &R) {
600 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
601 OverflowingBinaryOperator::NoUnsignedWrap>(
604 template <typename LHS, typename RHS>
605 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
606 OverflowingBinaryOperator::NoUnsignedWrap>
607 m_NUWShl(const LHS &L, const RHS &R) {
608 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
609 OverflowingBinaryOperator::NoUnsignedWrap>(
613 //===----------------------------------------------------------------------===//
614 // Class that matches two different binary ops.
616 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
617 struct BinOp2_match {
621 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
623 template<typename OpTy>
624 bool match(OpTy *V) {
625 if (V->getValueID() == Value::InstructionVal + Opc1 ||
626 V->getValueID() == Value::InstructionVal + Opc2) {
627 BinaryOperator *I = cast<BinaryOperator>(V);
628 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
630 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
631 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
632 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
637 /// m_Shr - Matches LShr or AShr.
638 template<typename LHS, typename RHS>
639 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
640 m_Shr(const LHS &L, const RHS &R) {
641 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
644 /// m_LogicalShift - Matches LShr or Shl.
645 template<typename LHS, typename RHS>
646 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
647 m_LogicalShift(const LHS &L, const RHS &R) {
648 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
651 /// m_IDiv - Matches UDiv and SDiv.
652 template<typename LHS, typename RHS>
653 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
654 m_IDiv(const LHS &L, const RHS &R) {
655 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
658 //===----------------------------------------------------------------------===//
659 // Class that matches exact binary ops.
661 template<typename SubPattern_t>
663 SubPattern_t SubPattern;
665 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
667 template<typename OpTy>
668 bool match(OpTy *V) {
669 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
670 return PEO->isExact() && SubPattern.match(V);
676 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
678 //===----------------------------------------------------------------------===//
679 // Matchers for CmpInst classes
682 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
683 struct CmpClass_match {
684 PredicateTy &Predicate;
688 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
689 : Predicate(Pred), L(LHS), R(RHS) {}
691 template<typename OpTy>
692 bool match(OpTy *V) {
693 if (Class *I = dyn_cast<Class>(V))
694 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
695 Predicate = I->getPredicate();
702 template<typename LHS, typename RHS>
703 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
704 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
705 return CmpClass_match<LHS, RHS,
706 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
709 template<typename LHS, typename RHS>
710 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
711 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
712 return CmpClass_match<LHS, RHS,
713 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
716 //===----------------------------------------------------------------------===//
717 // Matchers for SelectInst classes
720 template<typename Cond_t, typename LHS_t, typename RHS_t>
721 struct SelectClass_match {
726 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
728 : C(Cond), L(LHS), R(RHS) {}
730 template<typename OpTy>
731 bool match(OpTy *V) {
732 if (SelectInst *I = dyn_cast<SelectInst>(V))
733 return C.match(I->getOperand(0)) &&
734 L.match(I->getOperand(1)) &&
735 R.match(I->getOperand(2));
740 template<typename Cond, typename LHS, typename RHS>
741 inline SelectClass_match<Cond, LHS, RHS>
742 m_Select(const Cond &C, const LHS &L, const RHS &R) {
743 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
746 /// m_SelectCst - This matches a select of two constants, e.g.:
747 /// m_SelectCst<-1, 0>(m_Value(V))
748 template<int64_t L, int64_t R, typename Cond>
749 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
750 m_SelectCst(const Cond &C) {
751 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
755 //===----------------------------------------------------------------------===//
756 // Matchers for CastInst classes
759 template<typename Op_t, unsigned Opcode>
760 struct CastClass_match {
763 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
765 template<typename OpTy>
766 bool match(OpTy *V) {
767 if (Operator *O = dyn_cast<Operator>(V))
768 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
774 template<typename OpTy>
775 inline CastClass_match<OpTy, Instruction::BitCast>
776 m_BitCast(const OpTy &Op) {
777 return CastClass_match<OpTy, Instruction::BitCast>(Op);
781 template<typename OpTy>
782 inline CastClass_match<OpTy, Instruction::PtrToInt>
783 m_PtrToInt(const OpTy &Op) {
784 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
788 template<typename OpTy>
789 inline CastClass_match<OpTy, Instruction::Trunc>
790 m_Trunc(const OpTy &Op) {
791 return CastClass_match<OpTy, Instruction::Trunc>(Op);
795 template<typename OpTy>
796 inline CastClass_match<OpTy, Instruction::SExt>
797 m_SExt(const OpTy &Op) {
798 return CastClass_match<OpTy, Instruction::SExt>(Op);
802 template<typename OpTy>
803 inline CastClass_match<OpTy, Instruction::ZExt>
804 m_ZExt(const OpTy &Op) {
805 return CastClass_match<OpTy, Instruction::ZExt>(Op);
809 template<typename OpTy>
810 inline CastClass_match<OpTy, Instruction::UIToFP>
811 m_UIToFP(const OpTy &Op) {
812 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
816 template<typename OpTy>
817 inline CastClass_match<OpTy, Instruction::SIToFP>
818 m_SIToFP(const OpTy &Op) {
819 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
822 //===----------------------------------------------------------------------===//
823 // Matchers for unary operators
826 template<typename LHS_t>
830 not_match(const LHS_t &LHS) : L(LHS) {}
832 template<typename OpTy>
833 bool match(OpTy *V) {
834 if (Operator *O = dyn_cast<Operator>(V))
835 if (O->getOpcode() == Instruction::Xor)
836 return matchIfNot(O->getOperand(0), O->getOperand(1));
840 bool matchIfNot(Value *LHS, Value *RHS) {
841 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
843 isa<ConstantVector>(RHS)) &&
844 cast<Constant>(RHS)->isAllOnesValue() &&
849 template<typename LHS>
850 inline not_match<LHS> m_Not(const LHS &L) { return L; }
853 template<typename LHS_t>
857 neg_match(const LHS_t &LHS) : L(LHS) {}
859 template<typename OpTy>
860 bool match(OpTy *V) {
861 if (Operator *O = dyn_cast<Operator>(V))
862 if (O->getOpcode() == Instruction::Sub)
863 return matchIfNeg(O->getOperand(0), O->getOperand(1));
867 bool matchIfNeg(Value *LHS, Value *RHS) {
868 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
869 isa<ConstantAggregateZero>(LHS)) &&
874 /// m_Neg - Match an integer negate.
875 template<typename LHS>
876 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
879 template<typename LHS_t>
883 fneg_match(const LHS_t &LHS) : L(LHS) {}
885 template<typename OpTy>
886 bool match(OpTy *V) {
887 if (Operator *O = dyn_cast<Operator>(V))
888 if (O->getOpcode() == Instruction::FSub)
889 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
893 bool matchIfFNeg(Value *LHS, Value *RHS) {
894 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
895 return C->isNegativeZeroValue() && L.match(RHS);
900 /// m_FNeg - Match a floating point negate.
901 template<typename LHS>
902 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
905 //===----------------------------------------------------------------------===//
906 // Matchers for control flow.
911 br_match(BasicBlock *&Succ)
915 template<typename OpTy>
916 bool match(OpTy *V) {
917 if (BranchInst *BI = dyn_cast<BranchInst>(V))
918 if (BI->isUnconditional()) {
919 Succ = BI->getSuccessor(0);
926 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
928 template<typename Cond_t>
932 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
933 : Cond(C), T(t), F(f) {
936 template<typename OpTy>
937 bool match(OpTy *V) {
938 if (BranchInst *BI = dyn_cast<BranchInst>(V))
939 if (BI->isConditional() && Cond.match(BI->getCondition())) {
940 T = BI->getSuccessor(0);
941 F = BI->getSuccessor(1);
948 template<typename Cond_t>
949 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
950 return brc_match<Cond_t>(C, T, F);
954 //===----------------------------------------------------------------------===//
955 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
958 template<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
959 struct MaxMin_match {
963 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
966 template<typename OpTy>
967 bool match(OpTy *V) {
968 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
969 SelectInst *SI = dyn_cast<SelectInst>(V);
972 CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
975 // At this point we have a select conditioned on a comparison. Check that
976 // it is the values returned by the select that are being compared.
977 Value *TrueVal = SI->getTrueValue();
978 Value *FalseVal = SI->getFalseValue();
979 Value *LHS = Cmp->getOperand(0);
980 Value *RHS = Cmp->getOperand(1);
981 if ((TrueVal != LHS || FalseVal != RHS) &&
982 (TrueVal != RHS || FalseVal != LHS))
984 typename CmpInst_t::Predicate Pred = LHS == TrueVal ?
985 Cmp->getPredicate() : Cmp->getSwappedPredicate();
986 // Does "(x pred y) ? x : y" represent the desired max/min operation?
987 if (!Pred_t::match(Pred))
989 // It does! Bind the operands.
990 return L.match(LHS) && R.match(RHS);
994 /// smax_pred_ty - Helper class for identifying signed max predicates.
995 struct smax_pred_ty {
996 static bool match(ICmpInst::Predicate Pred) {
997 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1001 /// smin_pred_ty - Helper class for identifying signed min predicates.
1002 struct smin_pred_ty {
1003 static bool match(ICmpInst::Predicate Pred) {
1004 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1008 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
1009 struct umax_pred_ty {
1010 static bool match(ICmpInst::Predicate Pred) {
1011 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1015 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
1016 struct umin_pred_ty {
1017 static bool match(ICmpInst::Predicate Pred) {
1018 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1022 /// ofmax_pred_ty - Helper class for identifying ordered max predicates.
1023 struct ofmax_pred_ty {
1024 static bool match(FCmpInst::Predicate Pred) {
1025 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1029 /// ofmin_pred_ty - Helper class for identifying ordered min predicates.
1030 struct ofmin_pred_ty {
1031 static bool match(FCmpInst::Predicate Pred) {
1032 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1036 /// ufmax_pred_ty - Helper class for identifying unordered max predicates.
1037 struct ufmax_pred_ty {
1038 static bool match(FCmpInst::Predicate Pred) {
1039 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1043 /// ufmin_pred_ty - Helper class for identifying unordered min predicates.
1044 struct ufmin_pred_ty {
1045 static bool match(FCmpInst::Predicate Pred) {
1046 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1050 template<typename LHS, typename RHS>
1051 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>
1052 m_SMax(const LHS &L, const RHS &R) {
1053 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1056 template<typename LHS, typename RHS>
1057 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>
1058 m_SMin(const LHS &L, const RHS &R) {
1059 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1062 template<typename LHS, typename RHS>
1063 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>
1064 m_UMax(const LHS &L, const RHS &R) {
1065 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1068 template<typename LHS, typename RHS>
1069 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>
1070 m_UMin(const LHS &L, const RHS &R) {
1071 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1074 /// \brief Match an 'ordered' floating point maximum function.
1075 /// Floating point has one special value 'NaN'. Therefore, there is no total
1076 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1077 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1078 /// semantics. In the presence of 'NaN' we have to preserve the original
1079 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1081 /// max(L, R) iff L and R are not NaN
1082 /// m_OrdFMax(L, R) = R iff L or R are NaN
1083 template<typename LHS, typename RHS>
1084 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>
1085 m_OrdFMax(const LHS &L, const RHS &R) {
1086 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1089 /// \brief Match an 'ordered' floating point minimum function.
1090 /// Floating point has one special value 'NaN'. Therefore, there is no total
1091 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1092 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1093 /// semantics. In the presence of 'NaN' we have to preserve the original
1094 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1096 /// max(L, R) iff L and R are not NaN
1097 /// m_OrdFMin(L, R) = R iff L or R are NaN
1098 template<typename LHS, typename RHS>
1099 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>
1100 m_OrdFMin(const LHS &L, const RHS &R) {
1101 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1104 /// \brief Match an 'unordered' floating point maximum function.
1105 /// Floating point has one special value 'NaN'. Therefore, there is no total
1106 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1107 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1108 /// semantics. In the presence of 'NaN' we have to preserve the original
1109 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1111 /// max(L, R) iff L and R are not NaN
1112 /// m_UnordFMin(L, R) = L iff L or R are NaN
1113 template<typename LHS, typename RHS>
1114 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1115 m_UnordFMax(const LHS &L, const RHS &R) {
1116 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1119 /// \brief Match an 'unordered' floating point minimum function.
1120 /// Floating point has one special value 'NaN'. Therefore, there is no total
1121 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1122 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1123 /// semantics. In the presence of 'NaN' we have to preserve the original
1124 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1126 /// max(L, R) iff L and R are not NaN
1127 /// m_UnordFMin(L, R) = L iff L or R are NaN
1128 template<typename LHS, typename RHS>
1129 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1130 m_UnordFMin(const LHS &L, const RHS &R) {
1131 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1134 template<typename Opnd_t>
1135 struct Argument_match {
1138 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
1140 template<typename OpTy>
1141 bool match(OpTy *V) {
1143 return CS.isCall() && Val.match(CS.getArgument(OpI));
1147 /// Match an argument
1148 template<unsigned OpI, typename Opnd_t>
1149 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1150 return Argument_match<Opnd_t>(OpI, Op);
1153 /// Intrinsic matchers.
1154 struct IntrinsicID_match {
1156 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) { }
1158 template<typename OpTy>
1159 bool match(OpTy *V) {
1160 if (const CallInst *CI = dyn_cast<CallInst>(V))
1161 if (const Function *F = CI->getCalledFunction())
1162 return F->getIntrinsicID() == ID;
1167 /// Intrinsic matches are combinations of ID matchers, and argument
1168 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1169 /// them with lower arity matchers. Here's some convenient typedefs for up to
1170 /// several arguments, and more can be added as needed
1171 template <typename T0 = void, typename T1 = void, typename T2 = void,
1172 typename T3 = void, typename T4 = void, typename T5 = void,
1173 typename T6 = void, typename T7 = void, typename T8 = void,
1174 typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
1175 template <typename T0>
1176 struct m_Intrinsic_Ty<T0> {
1177 typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
1179 template <typename T0, typename T1>
1180 struct m_Intrinsic_Ty<T0, T1> {
1181 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
1182 Argument_match<T1> > Ty;
1184 template <typename T0, typename T1, typename T2>
1185 struct m_Intrinsic_Ty<T0, T1, T2> {
1186 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1187 Argument_match<T2> > Ty;
1189 template <typename T0, typename T1, typename T2, typename T3>
1190 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1191 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1192 Argument_match<T3> > Ty;
1195 /// Match intrinsic calls like this:
1196 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1197 template <Intrinsic::ID IntrID>
1198 inline IntrinsicID_match
1199 m_Intrinsic() { return IntrinsicID_match(IntrID); }
1201 template<Intrinsic::ID IntrID, typename T0>
1202 inline typename m_Intrinsic_Ty<T0>::Ty
1203 m_Intrinsic(const T0 &Op0) {
1204 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1207 template<Intrinsic::ID IntrID, typename T0, typename T1>
1208 inline typename m_Intrinsic_Ty<T0, T1>::Ty
1209 m_Intrinsic(const T0 &Op0, const T1 &Op1) {
1210 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1213 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1214 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1215 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1216 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1219 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2, typename T3>
1220 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1221 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1222 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1225 // Helper intrinsic matching specializations
1226 template<typename Opnd0>
1227 inline typename m_Intrinsic_Ty<Opnd0>::Ty
1228 m_BSwap(const Opnd0 &Op0) {
1229 return m_Intrinsic<Intrinsic::bswap>(Op0);
1232 template<typename Opnd0, typename Opnd1>
1233 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1234 m_FMin(const Opnd0 &Op0, const Opnd1 &Op1) {
1235 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1238 template<typename Opnd0, typename Opnd1>
1239 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1240 m_FMax(const Opnd0 &Op0, const Opnd1 &Op1) {
1241 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1244 } // end namespace PatternMatch
1245 } // end namespace llvm