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 // FIXME: Remove this.
102 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
103 if (ConstantInt *CI =
104 dyn_cast_or_null<ConstantInt>(CV->getSplatValue())) {
105 Res = &CI->getValue();
108 if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(V))
109 if (ConstantInt *CI =
110 dyn_cast_or_null<ConstantInt>(CV->getSplatValue())) {
111 Res = &CI->getValue();
118 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
119 /// specified pointer to the contained APInt.
120 inline apint_match m_APInt(const APInt *&Res) { return Res; }
123 template<int64_t Val>
124 struct constantint_match {
125 template<typename ITy>
127 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
128 const APInt &CIV = CI->getValue();
130 return CIV == static_cast<uint64_t>(Val);
131 // If Val is negative, and CI is shorter than it, truncate to the right
132 // number of bits. If it is larger, then we have to sign extend. Just
133 // compare their negated values.
140 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
141 template<int64_t Val>
142 inline constantint_match<Val> m_ConstantInt() {
143 return constantint_match<Val>();
146 /// cst_pred_ty - This helper class is used to match scalar and vector constants
147 /// that satisfy a specified predicate.
148 template<typename Predicate>
149 struct cst_pred_ty : public Predicate {
150 template<typename ITy>
152 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
153 return this->isValue(CI->getValue());
154 // FIXME: Remove this.
155 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
156 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()))
157 return this->isValue(CI->getValue());
158 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(V))
159 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()))
160 return this->isValue(CI->getValue());
165 /// api_pred_ty - This helper class is used to match scalar and vector constants
166 /// that satisfy a specified predicate, and bind them to an APInt.
167 template<typename Predicate>
168 struct api_pred_ty : public Predicate {
170 api_pred_ty(const APInt *&R) : Res(R) {}
171 template<typename ITy>
173 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
174 if (this->isValue(CI->getValue())) {
175 Res = &CI->getValue();
180 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
181 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()))
182 if (this->isValue(CI->getValue())) {
183 Res = &CI->getValue();
187 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(V))
188 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()))
189 if (this->isValue(CI->getValue())) {
190 Res = &CI->getValue();
200 bool isValue(const APInt &C) { return C == 1; }
203 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
204 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
205 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
208 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
211 /// m_AllOnes() - Match an integer or vector with all bits set to true.
212 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
213 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
216 bool isValue(const APInt &C) { return C.isSignBit(); }
219 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
220 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
221 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
224 bool isValue(const APInt &C) { return C.isPowerOf2(); }
227 /// m_Power2() - Match an integer or vector power of 2.
228 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
229 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
231 template<typename Class>
234 bind_ty(Class *&V) : VR(V) {}
236 template<typename ITy>
238 if (Class *CV = dyn_cast<Class>(V)) {
246 /// m_Value - Match a value, capturing it if we match.
247 inline bind_ty<Value> m_Value(Value *&V) { return V; }
249 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
250 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
252 /// m_Constant - Match a Constant, capturing the value if we match.
253 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
255 /// specificval_ty - Match a specified Value*.
256 struct specificval_ty {
258 specificval_ty(const Value *V) : Val(V) {}
260 template<typename ITy>
266 /// m_Specific - Match if we have a specific specified value.
267 inline specificval_ty m_Specific(const Value *V) { return V; }
269 struct bind_const_intval_ty {
271 bind_const_intval_ty(uint64_t &V) : VR(V) {}
273 template<typename ITy>
275 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
276 if (CV->getBitWidth() <= 64) {
277 VR = CV->getZExtValue();
284 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
285 /// match ConstantInts wider than 64-bits.
286 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
288 //===----------------------------------------------------------------------===//
289 // Matchers for specific binary operators.
292 template<typename LHS_t, typename RHS_t, unsigned Opcode>
293 struct BinaryOp_match {
297 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
299 template<typename OpTy>
300 bool match(OpTy *V) {
301 if (V->getValueID() == Value::InstructionVal + Opcode) {
302 BinaryOperator *I = cast<BinaryOperator>(V);
303 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
305 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
306 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
307 R.match(CE->getOperand(1));
312 template<typename LHS, typename RHS>
313 inline BinaryOp_match<LHS, RHS, Instruction::Add>
314 m_Add(const LHS &L, const RHS &R) {
315 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
318 template<typename LHS, typename RHS>
319 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
320 m_FAdd(const LHS &L, const RHS &R) {
321 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
324 template<typename LHS, typename RHS>
325 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
326 m_Sub(const LHS &L, const RHS &R) {
327 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
330 template<typename LHS, typename RHS>
331 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
332 m_FSub(const LHS &L, const RHS &R) {
333 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
336 template<typename LHS, typename RHS>
337 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
338 m_Mul(const LHS &L, const RHS &R) {
339 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
342 template<typename LHS, typename RHS>
343 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
344 m_FMul(const LHS &L, const RHS &R) {
345 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
348 template<typename LHS, typename RHS>
349 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
350 m_UDiv(const LHS &L, const RHS &R) {
351 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
354 template<typename LHS, typename RHS>
355 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
356 m_SDiv(const LHS &L, const RHS &R) {
357 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
360 template<typename LHS, typename RHS>
361 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
362 m_FDiv(const LHS &L, const RHS &R) {
363 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
366 template<typename LHS, typename RHS>
367 inline BinaryOp_match<LHS, RHS, Instruction::URem>
368 m_URem(const LHS &L, const RHS &R) {
369 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
372 template<typename LHS, typename RHS>
373 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
374 m_SRem(const LHS &L, const RHS &R) {
375 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
378 template<typename LHS, typename RHS>
379 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
380 m_FRem(const LHS &L, const RHS &R) {
381 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
384 template<typename LHS, typename RHS>
385 inline BinaryOp_match<LHS, RHS, Instruction::And>
386 m_And(const LHS &L, const RHS &R) {
387 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
390 template<typename LHS, typename RHS>
391 inline BinaryOp_match<LHS, RHS, Instruction::Or>
392 m_Or(const LHS &L, const RHS &R) {
393 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
396 template<typename LHS, typename RHS>
397 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
398 m_Xor(const LHS &L, const RHS &R) {
399 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
402 template<typename LHS, typename RHS>
403 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
404 m_Shl(const LHS &L, const RHS &R) {
405 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
408 template<typename LHS, typename RHS>
409 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
410 m_LShr(const LHS &L, const RHS &R) {
411 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
414 template<typename LHS, typename RHS>
415 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
416 m_AShr(const LHS &L, const RHS &R) {
417 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
420 //===----------------------------------------------------------------------===//
421 // Class that matches two different binary ops.
423 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
424 struct BinOp2_match {
428 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
430 template<typename OpTy>
431 bool match(OpTy *V) {
432 if (V->getValueID() == Value::InstructionVal + Opc1 ||
433 V->getValueID() == Value::InstructionVal + Opc2) {
434 BinaryOperator *I = cast<BinaryOperator>(V);
435 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
437 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
438 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
439 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
444 /// m_Shr - Matches LShr or AShr.
445 template<typename LHS, typename RHS>
446 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
447 m_Shr(const LHS &L, const RHS &R) {
448 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
451 /// m_LogicalShift - Matches LShr or Shl.
452 template<typename LHS, typename RHS>
453 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
454 m_LogicalShift(const LHS &L, const RHS &R) {
455 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
458 /// m_IDiv - Matches UDiv and SDiv.
459 template<typename LHS, typename RHS>
460 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
461 m_IDiv(const LHS &L, const RHS &R) {
462 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
465 //===----------------------------------------------------------------------===//
466 // Class that matches exact binary ops.
468 template<typename SubPattern_t>
470 SubPattern_t SubPattern;
472 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
474 template<typename OpTy>
475 bool match(OpTy *V) {
476 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
477 return PEO->isExact() && SubPattern.match(V);
483 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
485 //===----------------------------------------------------------------------===//
486 // Matchers for CmpInst classes
489 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
490 struct CmpClass_match {
491 PredicateTy &Predicate;
495 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
496 : Predicate(Pred), L(LHS), R(RHS) {}
498 template<typename OpTy>
499 bool match(OpTy *V) {
500 if (Class *I = dyn_cast<Class>(V))
501 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
502 Predicate = I->getPredicate();
509 template<typename LHS, typename RHS>
510 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
511 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
512 return CmpClass_match<LHS, RHS,
513 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
516 template<typename LHS, typename RHS>
517 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
518 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
519 return CmpClass_match<LHS, RHS,
520 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
523 //===----------------------------------------------------------------------===//
524 // Matchers for SelectInst classes
527 template<typename Cond_t, typename LHS_t, typename RHS_t>
528 struct SelectClass_match {
533 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
535 : C(Cond), L(LHS), R(RHS) {}
537 template<typename OpTy>
538 bool match(OpTy *V) {
539 if (SelectInst *I = dyn_cast<SelectInst>(V))
540 return C.match(I->getOperand(0)) &&
541 L.match(I->getOperand(1)) &&
542 R.match(I->getOperand(2));
547 template<typename Cond, typename LHS, typename RHS>
548 inline SelectClass_match<Cond, LHS, RHS>
549 m_Select(const Cond &C, const LHS &L, const RHS &R) {
550 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
553 /// m_SelectCst - This matches a select of two constants, e.g.:
554 /// m_SelectCst<-1, 0>(m_Value(V))
555 template<int64_t L, int64_t R, typename Cond>
556 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
557 m_SelectCst(const Cond &C) {
558 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
562 //===----------------------------------------------------------------------===//
563 // Matchers for CastInst classes
566 template<typename Op_t, unsigned Opcode>
567 struct CastClass_match {
570 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
572 template<typename OpTy>
573 bool match(OpTy *V) {
574 if (Operator *O = dyn_cast<Operator>(V))
575 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
581 template<typename OpTy>
582 inline CastClass_match<OpTy, Instruction::BitCast>
583 m_BitCast(const OpTy &Op) {
584 return CastClass_match<OpTy, Instruction::BitCast>(Op);
588 template<typename OpTy>
589 inline CastClass_match<OpTy, Instruction::PtrToInt>
590 m_PtrToInt(const OpTy &Op) {
591 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
595 template<typename OpTy>
596 inline CastClass_match<OpTy, Instruction::Trunc>
597 m_Trunc(const OpTy &Op) {
598 return CastClass_match<OpTy, Instruction::Trunc>(Op);
602 template<typename OpTy>
603 inline CastClass_match<OpTy, Instruction::SExt>
604 m_SExt(const OpTy &Op) {
605 return CastClass_match<OpTy, Instruction::SExt>(Op);
609 template<typename OpTy>
610 inline CastClass_match<OpTy, Instruction::ZExt>
611 m_ZExt(const OpTy &Op) {
612 return CastClass_match<OpTy, Instruction::ZExt>(Op);
616 //===----------------------------------------------------------------------===//
617 // Matchers for unary operators
620 template<typename LHS_t>
624 not_match(const LHS_t &LHS) : L(LHS) {}
626 template<typename OpTy>
627 bool match(OpTy *V) {
628 if (Operator *O = dyn_cast<Operator>(V))
629 if (O->getOpcode() == Instruction::Xor)
630 return matchIfNot(O->getOperand(0), O->getOperand(1));
634 bool matchIfNot(Value *LHS, Value *RHS) {
635 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
637 isa<ConstantVector>(RHS)) &&
638 cast<Constant>(RHS)->isAllOnesValue() &&
643 template<typename LHS>
644 inline not_match<LHS> m_Not(const LHS &L) { return L; }
647 template<typename LHS_t>
651 neg_match(const LHS_t &LHS) : L(LHS) {}
653 template<typename OpTy>
654 bool match(OpTy *V) {
655 if (Operator *O = dyn_cast<Operator>(V))
656 if (O->getOpcode() == Instruction::Sub)
657 return matchIfNeg(O->getOperand(0), O->getOperand(1));
661 bool matchIfNeg(Value *LHS, Value *RHS) {
662 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
663 isa<ConstantAggregateZero>(LHS)) &&
668 /// m_Neg - Match an integer negate.
669 template<typename LHS>
670 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
673 template<typename LHS_t>
677 fneg_match(const LHS_t &LHS) : L(LHS) {}
679 template<typename OpTy>
680 bool match(OpTy *V) {
681 if (Operator *O = dyn_cast<Operator>(V))
682 if (O->getOpcode() == Instruction::FSub)
683 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
687 bool matchIfFNeg(Value *LHS, Value *RHS) {
688 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
689 return C->isNegativeZeroValue() && L.match(RHS);
694 /// m_FNeg - Match a floating point negate.
695 template<typename LHS>
696 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
699 //===----------------------------------------------------------------------===//
700 // Matchers for control flow.
703 template<typename Cond_t>
707 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
708 : Cond(C), T(t), F(f) {
711 template<typename OpTy>
712 bool match(OpTy *V) {
713 if (BranchInst *BI = dyn_cast<BranchInst>(V))
714 if (BI->isConditional() && Cond.match(BI->getCondition())) {
715 T = BI->getSuccessor(0);
716 F = BI->getSuccessor(1);
723 template<typename Cond_t>
724 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
725 return brc_match<Cond_t>(C, T, F);
729 //===----------------------------------------------------------------------===//
730 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
733 template<typename LHS_t, typename RHS_t, typename Pred_t>
734 struct MaxMin_match {
738 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
741 template<typename OpTy>
742 bool match(OpTy *V) {
743 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
744 SelectInst *SI = dyn_cast<SelectInst>(V);
747 ICmpInst *Cmp = dyn_cast<ICmpInst>(SI->getCondition());
750 // At this point we have a select conditioned on a comparison. Check that
751 // it is the values returned by the select that are being compared.
752 Value *TrueVal = SI->getTrueValue();
753 Value *FalseVal = SI->getFalseValue();
754 Value *LHS = Cmp->getOperand(0);
755 Value *RHS = Cmp->getOperand(1);
756 if ((TrueVal != LHS || FalseVal != RHS) &&
757 (TrueVal != RHS || FalseVal != LHS))
759 ICmpInst::Predicate Pred = LHS == TrueVal ?
760 Cmp->getPredicate() : Cmp->getSwappedPredicate();
761 // Does "(x pred y) ? x : y" represent the desired max/min operation?
762 if (!Pred_t::match(Pred))
764 // It does! Bind the operands.
765 return L.match(LHS) && R.match(RHS);
769 /// smax_pred_ty - Helper class for identifying signed max predicates.
770 struct smax_pred_ty {
771 static bool match(ICmpInst::Predicate Pred) {
772 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
776 /// smin_pred_ty - Helper class for identifying signed min predicates.
777 struct smin_pred_ty {
778 static bool match(ICmpInst::Predicate Pred) {
779 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
783 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
784 struct umax_pred_ty {
785 static bool match(ICmpInst::Predicate Pred) {
786 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
790 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
791 struct umin_pred_ty {
792 static bool match(ICmpInst::Predicate Pred) {
793 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
797 template<typename LHS, typename RHS>
798 inline MaxMin_match<LHS, RHS, smax_pred_ty>
799 m_SMax(const LHS &L, const RHS &R) {
800 return MaxMin_match<LHS, RHS, smax_pred_ty>(L, R);
803 template<typename LHS, typename RHS>
804 inline MaxMin_match<LHS, RHS, smin_pred_ty>
805 m_SMin(const LHS &L, const RHS &R) {
806 return MaxMin_match<LHS, RHS, smin_pred_ty>(L, R);
809 template<typename LHS, typename RHS>
810 inline MaxMin_match<LHS, RHS, umax_pred_ty>
811 m_UMax(const LHS &L, const RHS &R) {
812 return MaxMin_match<LHS, RHS, umax_pred_ty>(L, R);
815 template<typename LHS, typename RHS>
816 inline MaxMin_match<LHS, RHS, umin_pred_ty>
817 m_UMin(const LHS &L, const RHS &R) {
818 return MaxMin_match<LHS, RHS, umin_pred_ty>(L, R);
821 } // end namespace PatternMatch
822 } // end namespace llvm