1 //===-- llvm/Support/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_SUPPORT_PATTERNMATCH_H
30 #define LLVM_SUPPORT_PATTERNMATCH_H
32 #include "llvm/Constants.h"
33 #include "llvm/Instructions.h"
34 #include "llvm/Operator.h"
37 namespace PatternMatch {
39 template<typename Val, typename Pattern>
40 bool match(Val *V, const Pattern &P) {
41 return const_cast<Pattern&>(P).match(V);
45 template<typename SubPattern_t>
47 SubPattern_t SubPattern;
49 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
51 template<typename OpTy>
53 return V->hasOneUse() && SubPattern.match(V);
58 inline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; }
61 template<typename Class>
63 template<typename ITy>
64 bool match(ITy *V) { return isa<Class>(V); }
67 /// m_Value() - Match an arbitrary value and ignore it.
68 inline class_match<Value> m_Value() { return class_match<Value>(); }
69 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
70 inline class_match<ConstantInt> m_ConstantInt() {
71 return class_match<ConstantInt>();
73 /// m_Undef() - Match an arbitrary undef constant.
74 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
76 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
79 template<typename ITy>
81 if (const Constant *C = dyn_cast<Constant>(V))
82 return C->isNullValue();
87 /// m_Zero() - Match an arbitrary zero/null constant. This includes
88 /// zero_initializer for vectors and ConstantPointerNull for pointers.
89 inline match_zero m_Zero() { return match_zero(); }
94 apint_match(const APInt *&R) : Res(R) {}
95 template<typename ITy>
97 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
98 Res = &CI->getValue();
101 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
102 if (ConstantInt *CI =
103 dyn_cast_or_null<ConstantInt>(CV->getSplatValue())) {
104 Res = &CI->getValue();
111 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
112 /// specified pointer to the contained APInt.
113 inline apint_match m_APInt(const APInt *&Res) { return Res; }
116 template<int64_t Val>
117 struct constantint_match {
118 template<typename ITy>
120 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
121 const APInt &CIV = CI->getValue();
123 return CIV == static_cast<uint64_t>(Val);
124 // If Val is negative, and CI is shorter than it, truncate to the right
125 // number of bits. If it is larger, then we have to sign extend. Just
126 // compare their negated values.
133 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
134 template<int64_t Val>
135 inline constantint_match<Val> m_ConstantInt() {
136 return constantint_match<Val>();
139 /// cst_pred_ty - This helper class is used to match scalar and vector constants
140 /// that satisfy a specified predicate.
141 template<typename Predicate>
142 struct cst_pred_ty : public Predicate {
143 template<typename ITy>
145 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
146 return this->isValue(CI->getValue());
147 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
148 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()))
149 return this->isValue(CI->getValue());
154 /// api_pred_ty - This helper class is used to match scalar and vector constants
155 /// that satisfy a specified predicate, and bind them to an APInt.
156 template<typename Predicate>
157 struct api_pred_ty : public Predicate {
159 api_pred_ty(const APInt *&R) : Res(R) {}
160 template<typename ITy>
162 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
163 if (this->isValue(CI->getValue())) {
164 Res = &CI->getValue();
167 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
168 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()))
169 if (this->isValue(CI->getValue())) {
170 Res = &CI->getValue();
179 bool isValue(const APInt &C) { return C == 1; }
182 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
183 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
184 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
187 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
190 /// m_AllOnes() - Match an integer or vector with all bits set to true.
191 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
192 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
195 bool isValue(const APInt &C) { return C.isSignBit(); }
198 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
199 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
200 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
203 bool isValue(const APInt &C) { return C.isPowerOf2(); }
206 /// m_Power2() - Match an integer or vector power of 2.
207 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
208 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
210 template<typename Class>
213 bind_ty(Class *&V) : VR(V) {}
215 template<typename ITy>
217 if (Class *CV = dyn_cast<Class>(V)) {
225 /// m_Value - Match a value, capturing it if we match.
226 inline bind_ty<Value> m_Value(Value *&V) { return V; }
228 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
229 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
231 /// m_Constant - Match a Constant, capturing the value if we match.
232 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
234 /// specificval_ty - Match a specified Value*.
235 struct specificval_ty {
237 specificval_ty(const Value *V) : Val(V) {}
239 template<typename ITy>
245 /// m_Specific - Match if we have a specific specified value.
246 inline specificval_ty m_Specific(const Value *V) { return V; }
248 struct bind_const_intval_ty {
250 bind_const_intval_ty(uint64_t &V) : VR(V) {}
252 template<typename ITy>
254 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
255 if (CV->getBitWidth() <= 64) {
256 VR = CV->getZExtValue();
263 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
264 /// match ConstantInts wider than 64-bits.
265 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
267 //===----------------------------------------------------------------------===//
268 // Matchers for specific binary operators.
271 template<typename LHS_t, typename RHS_t, unsigned Opcode>
272 struct BinaryOp_match {
276 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
278 template<typename OpTy>
279 bool match(OpTy *V) {
280 if (V->getValueID() == Value::InstructionVal + Opcode) {
281 BinaryOperator *I = cast<BinaryOperator>(V);
282 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
284 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
285 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
286 R.match(CE->getOperand(1));
291 template<typename LHS, typename RHS>
292 inline BinaryOp_match<LHS, RHS, Instruction::Add>
293 m_Add(const LHS &L, const RHS &R) {
294 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
297 template<typename LHS, typename RHS>
298 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
299 m_FAdd(const LHS &L, const RHS &R) {
300 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
303 template<typename LHS, typename RHS>
304 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
305 m_Sub(const LHS &L, const RHS &R) {
306 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
309 template<typename LHS, typename RHS>
310 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
311 m_FSub(const LHS &L, const RHS &R) {
312 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
315 template<typename LHS, typename RHS>
316 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
317 m_Mul(const LHS &L, const RHS &R) {
318 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
321 template<typename LHS, typename RHS>
322 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
323 m_FMul(const LHS &L, const RHS &R) {
324 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
327 template<typename LHS, typename RHS>
328 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
329 m_UDiv(const LHS &L, const RHS &R) {
330 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
333 template<typename LHS, typename RHS>
334 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
335 m_SDiv(const LHS &L, const RHS &R) {
336 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
339 template<typename LHS, typename RHS>
340 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
341 m_FDiv(const LHS &L, const RHS &R) {
342 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
345 template<typename LHS, typename RHS>
346 inline BinaryOp_match<LHS, RHS, Instruction::URem>
347 m_URem(const LHS &L, const RHS &R) {
348 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
351 template<typename LHS, typename RHS>
352 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
353 m_SRem(const LHS &L, const RHS &R) {
354 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
357 template<typename LHS, typename RHS>
358 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
359 m_FRem(const LHS &L, const RHS &R) {
360 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
363 template<typename LHS, typename RHS>
364 inline BinaryOp_match<LHS, RHS, Instruction::And>
365 m_And(const LHS &L, const RHS &R) {
366 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
369 template<typename LHS, typename RHS>
370 inline BinaryOp_match<LHS, RHS, Instruction::Or>
371 m_Or(const LHS &L, const RHS &R) {
372 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
375 template<typename LHS, typename RHS>
376 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
377 m_Xor(const LHS &L, const RHS &R) {
378 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
381 template<typename LHS, typename RHS>
382 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
383 m_Shl(const LHS &L, const RHS &R) {
384 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
387 template<typename LHS, typename RHS>
388 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
389 m_LShr(const LHS &L, const RHS &R) {
390 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
393 template<typename LHS, typename RHS>
394 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
395 m_AShr(const LHS &L, const RHS &R) {
396 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
399 //===----------------------------------------------------------------------===//
400 // Class that matches two different binary ops.
402 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
403 struct BinOp2_match {
407 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
409 template<typename OpTy>
410 bool match(OpTy *V) {
411 if (V->getValueID() == Value::InstructionVal + Opc1 ||
412 V->getValueID() == Value::InstructionVal + Opc2) {
413 BinaryOperator *I = cast<BinaryOperator>(V);
414 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
416 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
417 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
418 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
423 /// m_Shr - Matches LShr or AShr.
424 template<typename LHS, typename RHS>
425 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
426 m_Shr(const LHS &L, const RHS &R) {
427 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
430 /// m_LogicalShift - Matches LShr or Shl.
431 template<typename LHS, typename RHS>
432 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
433 m_LogicalShift(const LHS &L, const RHS &R) {
434 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
437 /// m_IDiv - Matches UDiv and SDiv.
438 template<typename LHS, typename RHS>
439 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
440 m_IDiv(const LHS &L, const RHS &R) {
441 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
444 //===----------------------------------------------------------------------===//
445 // Matchers for CmpInst classes
448 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
449 struct CmpClass_match {
450 PredicateTy &Predicate;
454 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
455 : Predicate(Pred), L(LHS), R(RHS) {}
457 template<typename OpTy>
458 bool match(OpTy *V) {
459 if (Class *I = dyn_cast<Class>(V))
460 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
461 Predicate = I->getPredicate();
468 template<typename LHS, typename RHS>
469 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
470 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
471 return CmpClass_match<LHS, RHS,
472 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
475 template<typename LHS, typename RHS>
476 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
477 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
478 return CmpClass_match<LHS, RHS,
479 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
482 //===----------------------------------------------------------------------===//
483 // Matchers for SelectInst classes
486 template<typename Cond_t, typename LHS_t, typename RHS_t>
487 struct SelectClass_match {
492 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
494 : C(Cond), L(LHS), R(RHS) {}
496 template<typename OpTy>
497 bool match(OpTy *V) {
498 if (SelectInst *I = dyn_cast<SelectInst>(V))
499 return C.match(I->getOperand(0)) &&
500 L.match(I->getOperand(1)) &&
501 R.match(I->getOperand(2));
506 template<typename Cond, typename LHS, typename RHS>
507 inline SelectClass_match<Cond, LHS, RHS>
508 m_Select(const Cond &C, const LHS &L, const RHS &R) {
509 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
512 /// m_SelectCst - This matches a select of two constants, e.g.:
513 /// m_SelectCst<-1, 0>(m_Value(V))
514 template<int64_t L, int64_t R, typename Cond>
515 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
516 m_SelectCst(const Cond &C) {
517 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
521 //===----------------------------------------------------------------------===//
522 // Matchers for CastInst classes
525 template<typename Op_t, unsigned Opcode>
526 struct CastClass_match {
529 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
531 template<typename OpTy>
532 bool match(OpTy *V) {
533 if (Operator *O = dyn_cast<Operator>(V))
534 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
540 template<typename OpTy>
541 inline CastClass_match<OpTy, Instruction::BitCast>
542 m_BitCast(const OpTy &Op) {
543 return CastClass_match<OpTy, Instruction::BitCast>(Op);
547 template<typename OpTy>
548 inline CastClass_match<OpTy, Instruction::PtrToInt>
549 m_PtrToInt(const OpTy &Op) {
550 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
554 template<typename OpTy>
555 inline CastClass_match<OpTy, Instruction::Trunc>
556 m_Trunc(const OpTy &Op) {
557 return CastClass_match<OpTy, Instruction::Trunc>(Op);
561 template<typename OpTy>
562 inline CastClass_match<OpTy, Instruction::SExt>
563 m_SExt(const OpTy &Op) {
564 return CastClass_match<OpTy, Instruction::SExt>(Op);
568 template<typename OpTy>
569 inline CastClass_match<OpTy, Instruction::ZExt>
570 m_ZExt(const OpTy &Op) {
571 return CastClass_match<OpTy, Instruction::ZExt>(Op);
575 //===----------------------------------------------------------------------===//
576 // Matchers for unary operators
579 template<typename LHS_t>
583 not_match(const LHS_t &LHS) : L(LHS) {}
585 template<typename OpTy>
586 bool match(OpTy *V) {
587 if (Operator *O = dyn_cast<Operator>(V))
588 if (O->getOpcode() == Instruction::Xor)
589 return matchIfNot(O->getOperand(0), O->getOperand(1));
593 bool matchIfNot(Value *LHS, Value *RHS) {
594 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
595 return CI->isAllOnesValue() && L.match(LHS);
596 if (ConstantVector *CV = dyn_cast<ConstantVector>(RHS))
597 return CV->isAllOnesValue() && L.match(LHS);
602 template<typename LHS>
603 inline not_match<LHS> m_Not(const LHS &L) { return L; }
606 template<typename LHS_t>
610 neg_match(const LHS_t &LHS) : L(LHS) {}
612 template<typename OpTy>
613 bool match(OpTy *V) {
614 if (Operator *O = dyn_cast<Operator>(V))
615 if (O->getOpcode() == Instruction::Sub)
616 return matchIfNeg(O->getOperand(0), O->getOperand(1));
620 bool matchIfNeg(Value *LHS, Value *RHS) {
621 if (ConstantInt *C = dyn_cast<ConstantInt>(LHS))
622 return C->isZero() && L.match(RHS);
627 /// m_Neg - Match an integer negate.
628 template<typename LHS>
629 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
632 template<typename LHS_t>
636 fneg_match(const LHS_t &LHS) : L(LHS) {}
638 template<typename OpTy>
639 bool match(OpTy *V) {
640 if (Operator *O = dyn_cast<Operator>(V))
641 if (O->getOpcode() == Instruction::FSub)
642 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
646 bool matchIfFNeg(Value *LHS, Value *RHS) {
647 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
648 return C->isNegativeZeroValue() && L.match(RHS);
653 /// m_FNeg - Match a floating point negate.
654 template<typename LHS>
655 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
658 //===----------------------------------------------------------------------===//
659 // Matchers for control flow.
662 template<typename Cond_t>
666 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
667 : Cond(C), T(t), F(f) {
670 template<typename OpTy>
671 bool match(OpTy *V) {
672 if (BranchInst *BI = dyn_cast<BranchInst>(V))
673 if (BI->isConditional() && Cond.match(BI->getCondition())) {
674 T = BI->getSuccessor(0);
675 F = BI->getSuccessor(1);
682 template<typename Cond_t>
683 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
684 return brc_match<Cond_t>(C, T, F);
688 //===----------------------------------------------------------------------===//
689 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
692 template<typename LHS_t, typename RHS_t, typename Pred_t>
693 struct MaxMin_match {
697 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
700 template<typename OpTy>
701 bool match(OpTy *V) {
702 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
703 SelectInst *SI = dyn_cast<SelectInst>(V);
706 ICmpInst *Cmp = dyn_cast<ICmpInst>(SI->getCondition());
709 // At this point we have a select conditioned on a comparison. Check that
710 // it is the values returned by the select that are being compared.
711 Value *TrueVal = SI->getTrueValue();
712 Value *FalseVal = SI->getFalseValue();
713 Value *LHS = Cmp->getOperand(0);
714 Value *RHS = Cmp->getOperand(1);
715 if ((TrueVal != LHS || FalseVal != RHS) &&
716 (TrueVal != RHS || FalseVal != LHS))
718 ICmpInst::Predicate Pred = LHS == TrueVal ?
719 Cmp->getPredicate() : Cmp->getSwappedPredicate();
720 // Does "(x pred y) ? x : y" represent the desired max/min operation?
721 if (!Pred_t::match(Pred))
723 // It does! Bind the operands.
724 return L.match(LHS) && R.match(RHS);
728 /// smax_pred_ty - Helper class for identifying signed max predicates.
729 struct smax_pred_ty {
730 static bool match(ICmpInst::Predicate Pred) {
731 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
735 /// smin_pred_ty - Helper class for identifying signed min predicates.
736 struct smin_pred_ty {
737 static bool match(ICmpInst::Predicate Pred) {
738 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
742 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
743 struct umax_pred_ty {
744 static bool match(ICmpInst::Predicate Pred) {
745 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
749 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
750 struct umin_pred_ty {
751 static bool match(ICmpInst::Predicate Pred) {
752 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
756 template<typename LHS, typename RHS>
757 inline MaxMin_match<LHS, RHS, smax_pred_ty>
758 m_SMax(const LHS &L, const RHS &R) {
759 return MaxMin_match<LHS, RHS, smax_pred_ty>(L, R);
762 template<typename LHS, typename RHS>
763 inline MaxMin_match<LHS, RHS, smin_pred_ty>
764 m_SMin(const LHS &L, const RHS &R) {
765 return MaxMin_match<LHS, RHS, smin_pred_ty>(L, R);
768 template<typename LHS, typename RHS>
769 inline MaxMin_match<LHS, RHS, umax_pred_ty>
770 m_UMax(const LHS &L, const RHS &R) {
771 return MaxMin_match<LHS, RHS, umax_pred_ty>(L, R);
774 template<typename LHS, typename RHS>
775 inline MaxMin_match<LHS, RHS, umin_pred_ty>
776 m_UMin(const LHS &L, const RHS &R) {
777 return MaxMin_match<LHS, RHS, umin_pred_ty>(L, R);
780 } // end namespace PatternMatch
781 } // end namespace llvm