LLVM API Documentation

PatternMatch.h
Go to the documentation of this file.
00001 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides a simple and efficient mechanism for performing general
00011 // tree-based pattern matches on the LLVM IR.  The power of these routines is
00012 // that it allows you to write concise patterns that are expressive and easy to
00013 // understand.  The other major advantage of this is that it allows you to
00014 // trivially capture/bind elements in the pattern to variables.  For example,
00015 // you can do something like this:
00016 //
00017 //  Value *Exp = ...
00018 //  Value *X, *Y;  ConstantInt *C1, *C2;      // (X & C1) | (Y & C2)
00019 //  if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
00020 //                      m_And(m_Value(Y), m_ConstantInt(C2))))) {
00021 //    ... Pattern is matched and variables are bound ...
00022 //  }
00023 //
00024 // This is primarily useful to things like the instruction combiner, but can
00025 // also be useful for static analysis tools or code generators.
00026 //
00027 //===----------------------------------------------------------------------===//
00028 
00029 #ifndef LLVM_IR_PATTERNMATCH_H
00030 #define LLVM_IR_PATTERNMATCH_H
00031 
00032 #include "llvm/IR/CallSite.h"
00033 #include "llvm/IR/Constants.h"
00034 #include "llvm/IR/Instructions.h"
00035 #include "llvm/IR/Intrinsics.h"
00036 #include "llvm/IR/Operator.h"
00037 
00038 namespace llvm {
00039 namespace PatternMatch {
00040 
00041 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
00042   return const_cast<Pattern &>(P).match(V);
00043 }
00044 
00045 template <typename SubPattern_t> struct OneUse_match {
00046   SubPattern_t SubPattern;
00047 
00048   OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
00049 
00050   template <typename OpTy> bool match(OpTy *V) {
00051     return V->hasOneUse() && SubPattern.match(V);
00052   }
00053 };
00054 
00055 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
00056   return SubPattern;
00057 }
00058 
00059 template <typename Class> struct class_match {
00060   template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
00061 };
00062 
00063 /// \brief Match an arbitrary value and ignore it.
00064 inline class_match<Value> m_Value() { return class_match<Value>(); }
00065 
00066 /// \brief Match an arbitrary binary operation and ignore it.
00067 inline class_match<BinaryOperator> m_BinOp() {
00068   return class_match<BinaryOperator>();
00069 }
00070 
00071 /// \brief Matches any compare instruction and ignore it.
00072 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
00073 
00074 /// \brief Match an arbitrary ConstantInt and ignore it.
00075 inline class_match<ConstantInt> m_ConstantInt() {
00076   return class_match<ConstantInt>();
00077 }
00078 
00079 /// \brief Match an arbitrary undef constant.
00080 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
00081 
00082 /// \brief Match an arbitrary Constant and ignore it.
00083 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
00084 
00085 /// Matching combinators
00086 template <typename LTy, typename RTy> struct match_combine_or {
00087   LTy L;
00088   RTy R;
00089 
00090   match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
00091 
00092   template <typename ITy> bool match(ITy *V) {
00093     if (L.match(V))
00094       return true;
00095     if (R.match(V))
00096       return true;
00097     return false;
00098   }
00099 };
00100 
00101 template <typename LTy, typename RTy> struct match_combine_and {
00102   LTy L;
00103   RTy R;
00104 
00105   match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
00106 
00107   template <typename ITy> bool match(ITy *V) {
00108     if (L.match(V))
00109       if (R.match(V))
00110         return true;
00111     return false;
00112   }
00113 };
00114 
00115 /// Combine two pattern matchers matching L || R
00116 template <typename LTy, typename RTy>
00117 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
00118   return match_combine_or<LTy, RTy>(L, R);
00119 }
00120 
00121 /// Combine two pattern matchers matching L && R
00122 template <typename LTy, typename RTy>
00123 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
00124   return match_combine_and<LTy, RTy>(L, R);
00125 }
00126 
00127 struct match_zero {
00128   template <typename ITy> bool match(ITy *V) {
00129     if (const auto *C = dyn_cast<Constant>(V))
00130       return C->isNullValue();
00131     return false;
00132   }
00133 };
00134 
00135 /// \brief Match an arbitrary zero/null constant.  This includes
00136 /// zero_initializer for vectors and ConstantPointerNull for pointers.
00137 inline match_zero m_Zero() { return match_zero(); }
00138 
00139 struct match_neg_zero {
00140   template <typename ITy> bool match(ITy *V) {
00141     if (const auto *C = dyn_cast<Constant>(V))
00142       return C->isNegativeZeroValue();
00143     return false;
00144   }
00145 };
00146 
00147 /// \brief Match an arbitrary zero/null constant.  This includes
00148 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
00149 /// floating point constants, this will match negative zero but not positive
00150 /// zero
00151 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
00152 
00153 /// \brief - Match an arbitrary zero/null constant.  This includes
00154 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
00155 /// floating point constants, this will match negative zero and positive zero
00156 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
00157   return m_CombineOr(m_Zero(), m_NegZero());
00158 }
00159 
00160 struct apint_match {
00161   const APInt *&Res;
00162   apint_match(const APInt *&R) : Res(R) {}
00163   template <typename ITy> bool match(ITy *V) {
00164     if (auto *CI = dyn_cast<ConstantInt>(V)) {
00165       Res = &CI->getValue();
00166       return true;
00167     }
00168     if (V->getType()->isVectorTy())
00169       if (const auto *C = dyn_cast<Constant>(V))
00170         if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
00171           Res = &CI->getValue();
00172           return true;
00173         }
00174     return false;
00175   }
00176 };
00177 
00178 /// \brief Match a ConstantInt or splatted ConstantVector, binding the
00179 /// specified pointer to the contained APInt.
00180 inline apint_match m_APInt(const APInt *&Res) { return Res; }
00181 
00182 template <int64_t Val> struct constantint_match {
00183   template <typename ITy> bool match(ITy *V) {
00184     if (const auto *CI = dyn_cast<ConstantInt>(V)) {
00185       const APInt &CIV = CI->getValue();
00186       if (Val >= 0)
00187         return CIV == static_cast<uint64_t>(Val);
00188       // If Val is negative, and CI is shorter than it, truncate to the right
00189       // number of bits.  If it is larger, then we have to sign extend.  Just
00190       // compare their negated values.
00191       return -CIV == -Val;
00192     }
00193     return false;
00194   }
00195 };
00196 
00197 /// \brief Match a ConstantInt with a specific value.
00198 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
00199   return constantint_match<Val>();
00200 }
00201 
00202 /// \brief This helper class is used to match scalar and vector constants that
00203 /// satisfy a specified predicate.
00204 template <typename Predicate> struct cst_pred_ty : public Predicate {
00205   template <typename ITy> bool match(ITy *V) {
00206     if (const auto *CI = dyn_cast<ConstantInt>(V))
00207       return this->isValue(CI->getValue());
00208     if (V->getType()->isVectorTy())
00209       if (const auto *C = dyn_cast<Constant>(V))
00210         if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
00211           return this->isValue(CI->getValue());
00212     return false;
00213   }
00214 };
00215 
00216 /// \brief This helper class is used to match scalar and vector constants that
00217 /// satisfy a specified predicate, and bind them to an APInt.
00218 template <typename Predicate> struct api_pred_ty : public Predicate {
00219   const APInt *&Res;
00220   api_pred_ty(const APInt *&R) : Res(R) {}
00221   template <typename ITy> bool match(ITy *V) {
00222     if (const auto *CI = dyn_cast<ConstantInt>(V))
00223       if (this->isValue(CI->getValue())) {
00224         Res = &CI->getValue();
00225         return true;
00226       }
00227     if (V->getType()->isVectorTy())
00228       if (const auto *C = dyn_cast<Constant>(V))
00229         if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
00230           if (this->isValue(CI->getValue())) {
00231             Res = &CI->getValue();
00232             return true;
00233           }
00234 
00235     return false;
00236   }
00237 };
00238 
00239 struct is_one {
00240   bool isValue(const APInt &C) { return C == 1; }
00241 };
00242 
00243 /// \brief Match an integer 1 or a vector with all elements equal to 1.
00244 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
00245 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
00246 
00247 struct is_all_ones {
00248   bool isValue(const APInt &C) { return C.isAllOnesValue(); }
00249 };
00250 
00251 /// \brief Match an integer or vector with all bits set to true.
00252 inline cst_pred_ty<is_all_ones> m_AllOnes() {
00253   return cst_pred_ty<is_all_ones>();
00254 }
00255 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
00256 
00257 struct is_sign_bit {
00258   bool isValue(const APInt &C) { return C.isSignBit(); }
00259 };
00260 
00261 /// \brief Match an integer or vector with only the sign bit(s) set.
00262 inline cst_pred_ty<is_sign_bit> m_SignBit() {
00263   return cst_pred_ty<is_sign_bit>();
00264 }
00265 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
00266 
00267 struct is_power2 {
00268   bool isValue(const APInt &C) { return C.isPowerOf2(); }
00269 };
00270 
00271 /// \brief Match an integer or vector power of 2.
00272 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
00273 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
00274 
00275 struct is_maxsignedvalue {
00276   bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
00277 };
00278 
00279 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { return cst_pred_ty<is_maxsignedvalue>(); }
00280 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { return V; }
00281 
00282 template <typename Class> struct bind_ty {
00283   Class *&VR;
00284   bind_ty(Class *&V) : VR(V) {}
00285 
00286   template <typename ITy> bool match(ITy *V) {
00287     if (auto *CV = dyn_cast<Class>(V)) {
00288       VR = CV;
00289       return true;
00290     }
00291     return false;
00292   }
00293 };
00294 
00295 /// \brief Match a value, capturing it if we match.
00296 inline bind_ty<Value> m_Value(Value *&V) { return V; }
00297 
00298 /// \brief Match a binary operator, capturing it if we match.
00299 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
00300 
00301 /// \brief Match a ConstantInt, capturing the value if we match.
00302 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
00303 
00304 /// \brief Match a Constant, capturing the value if we match.
00305 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
00306 
00307 /// \brief Match a ConstantFP, capturing the value if we match.
00308 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
00309 
00310 /// \brief Match a specified Value*.
00311 struct specificval_ty {
00312   const Value *Val;
00313   specificval_ty(const Value *V) : Val(V) {}
00314 
00315   template <typename ITy> bool match(ITy *V) { return V == Val; }
00316 };
00317 
00318 /// \brief Match if we have a specific specified value.
00319 inline specificval_ty m_Specific(const Value *V) { return V; }
00320 
00321 /// \brief Match a specified floating point value or vector of all elements of
00322 /// that value.
00323 struct specific_fpval {
00324   double Val;
00325   specific_fpval(double V) : Val(V) {}
00326 
00327   template <typename ITy> bool match(ITy *V) {
00328     if (const auto *CFP = dyn_cast<ConstantFP>(V))
00329       return CFP->isExactlyValue(Val);
00330     if (V->getType()->isVectorTy())
00331       if (const auto *C = dyn_cast<Constant>(V))
00332         if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
00333           return CFP->isExactlyValue(Val);
00334     return false;
00335   }
00336 };
00337 
00338 /// \brief Match a specific floating point value or vector with all elements
00339 /// equal to the value.
00340 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
00341 
00342 /// \brief Match a float 1.0 or vector with all elements equal to 1.0.
00343 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
00344 
00345 struct bind_const_intval_ty {
00346   uint64_t &VR;
00347   bind_const_intval_ty(uint64_t &V) : VR(V) {}
00348 
00349   template <typename ITy> bool match(ITy *V) {
00350     if (const auto *CV = dyn_cast<ConstantInt>(V))
00351       if (CV->getBitWidth() <= 64) {
00352         VR = CV->getZExtValue();
00353         return true;
00354       }
00355     return false;
00356   }
00357 };
00358 
00359 /// \brief Match a specified integer value or vector of all elements of that
00360 // value.
00361 struct specific_intval {
00362   uint64_t Val;
00363   specific_intval(uint64_t V) : Val(V) {}
00364 
00365   template <typename ITy> bool match(ITy *V) {
00366     const auto *CI = dyn_cast<ConstantInt>(V);
00367     if (!CI && V->getType()->isVectorTy())
00368       if (const auto *C = dyn_cast<Constant>(V))
00369         CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
00370 
00371     if (CI && CI->getBitWidth() <= 64)
00372       return CI->getZExtValue() == Val;
00373 
00374     return false;
00375   }
00376 };
00377 
00378 /// \brief Match a specific integer value or vector with all elements equal to
00379 /// the value.
00380 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
00381 
00382 /// \brief Match a ConstantInt and bind to its value.  This does not match
00383 /// ConstantInts wider than 64-bits.
00384 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
00385 
00386 //===----------------------------------------------------------------------===//
00387 // Matcher for any binary operator.
00388 //
00389 template <typename LHS_t, typename RHS_t> struct AnyBinaryOp_match {
00390   LHS_t L;
00391   RHS_t R;
00392 
00393   AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00394 
00395   template <typename OpTy> bool match(OpTy *V) {
00396     if (auto *I = dyn_cast<BinaryOperator>(V))
00397       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
00398     return false;
00399   }
00400 };
00401 
00402 template <typename LHS, typename RHS>
00403 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
00404   return AnyBinaryOp_match<LHS, RHS>(L, R);
00405 }
00406 
00407 //===----------------------------------------------------------------------===//
00408 // Matchers for specific binary operators.
00409 //
00410 
00411 template <typename LHS_t, typename RHS_t, unsigned Opcode>
00412 struct BinaryOp_match {
00413   LHS_t L;
00414   RHS_t R;
00415 
00416   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00417 
00418   template <typename OpTy> bool match(OpTy *V) {
00419     if (V->getValueID() == Value::InstructionVal + Opcode) {
00420       auto *I = cast<BinaryOperator>(V);
00421       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
00422     }
00423     if (auto *CE = dyn_cast<ConstantExpr>(V))
00424       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
00425              R.match(CE->getOperand(1));
00426     return false;
00427   }
00428 };
00429 
00430 template <typename LHS, typename RHS>
00431 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
00432                                                         const RHS &R) {
00433   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
00434 }
00435 
00436 template <typename LHS, typename RHS>
00437 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
00438                                                           const RHS &R) {
00439   return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
00440 }
00441 
00442 template <typename LHS, typename RHS>
00443 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
00444                                                         const RHS &R) {
00445   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
00446 }
00447 
00448 template <typename LHS, typename RHS>
00449 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
00450                                                           const RHS &R) {
00451   return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
00452 }
00453 
00454 template <typename LHS, typename RHS>
00455 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
00456                                                         const RHS &R) {
00457   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
00458 }
00459 
00460 template <typename LHS, typename RHS>
00461 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
00462                                                           const RHS &R) {
00463   return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
00464 }
00465 
00466 template <typename LHS, typename RHS>
00467 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
00468                                                           const RHS &R) {
00469   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
00470 }
00471 
00472 template <typename LHS, typename RHS>
00473 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
00474                                                           const RHS &R) {
00475   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
00476 }
00477 
00478 template <typename LHS, typename RHS>
00479 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
00480                                                           const RHS &R) {
00481   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
00482 }
00483 
00484 template <typename LHS, typename RHS>
00485 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
00486                                                           const RHS &R) {
00487   return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
00488 }
00489 
00490 template <typename LHS, typename RHS>
00491 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
00492                                                           const RHS &R) {
00493   return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
00494 }
00495 
00496 template <typename LHS, typename RHS>
00497 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
00498                                                           const RHS &R) {
00499   return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
00500 }
00501 
00502 template <typename LHS, typename RHS>
00503 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
00504                                                         const RHS &R) {
00505   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
00506 }
00507 
00508 template <typename LHS, typename RHS>
00509 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
00510                                                       const RHS &R) {
00511   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
00512 }
00513 
00514 template <typename LHS, typename RHS>
00515 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
00516                                                         const RHS &R) {
00517   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
00518 }
00519 
00520 template <typename LHS, typename RHS>
00521 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
00522                                                         const RHS &R) {
00523   return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
00524 }
00525 
00526 template <typename LHS, typename RHS>
00527 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
00528                                                           const RHS &R) {
00529   return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
00530 }
00531 
00532 template <typename LHS, typename RHS>
00533 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
00534                                                           const RHS &R) {
00535   return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
00536 }
00537 
00538 template <typename LHS_t, typename RHS_t, unsigned Opcode,
00539           unsigned WrapFlags = 0>
00540 struct OverflowingBinaryOp_match {
00541   LHS_t L;
00542   RHS_t R;
00543 
00544   OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
00545       : L(LHS), R(RHS) {}
00546 
00547   template <typename OpTy> bool match(OpTy *V) {
00548     if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
00549       if (Op->getOpcode() != Opcode)
00550         return false;
00551       if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
00552           !Op->hasNoUnsignedWrap())
00553         return false;
00554       if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
00555           !Op->hasNoSignedWrap())
00556         return false;
00557       return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
00558     }
00559     return false;
00560   }
00561 };
00562 
00563 template <typename LHS, typename RHS>
00564 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00565                                  OverflowingBinaryOperator::NoSignedWrap>
00566 m_NSWAdd(const LHS &L, const RHS &R) {
00567   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00568                                    OverflowingBinaryOperator::NoSignedWrap>(
00569       L, R);
00570 }
00571 template <typename LHS, typename RHS>
00572 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00573                                  OverflowingBinaryOperator::NoSignedWrap>
00574 m_NSWSub(const LHS &L, const RHS &R) {
00575   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00576                                    OverflowingBinaryOperator::NoSignedWrap>(
00577       L, R);
00578 }
00579 template <typename LHS, typename RHS>
00580 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00581                                  OverflowingBinaryOperator::NoSignedWrap>
00582 m_NSWMul(const LHS &L, const RHS &R) {
00583   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00584                                    OverflowingBinaryOperator::NoSignedWrap>(
00585       L, R);
00586 }
00587 template <typename LHS, typename RHS>
00588 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00589                                  OverflowingBinaryOperator::NoSignedWrap>
00590 m_NSWShl(const LHS &L, const RHS &R) {
00591   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00592                                    OverflowingBinaryOperator::NoSignedWrap>(
00593       L, R);
00594 }
00595 
00596 template <typename LHS, typename RHS>
00597 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00598                                  OverflowingBinaryOperator::NoUnsignedWrap>
00599 m_NUWAdd(const LHS &L, const RHS &R) {
00600   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00601                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00602       L, R);
00603 }
00604 template <typename LHS, typename RHS>
00605 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00606                                  OverflowingBinaryOperator::NoUnsignedWrap>
00607 m_NUWSub(const LHS &L, const RHS &R) {
00608   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00609                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00610       L, R);
00611 }
00612 template <typename LHS, typename RHS>
00613 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00614                                  OverflowingBinaryOperator::NoUnsignedWrap>
00615 m_NUWMul(const LHS &L, const RHS &R) {
00616   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00617                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00618       L, R);
00619 }
00620 template <typename LHS, typename RHS>
00621 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00622                                  OverflowingBinaryOperator::NoUnsignedWrap>
00623 m_NUWShl(const LHS &L, const RHS &R) {
00624   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00625                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00626       L, R);
00627 }
00628 
00629 //===----------------------------------------------------------------------===//
00630 // Class that matches two different binary ops.
00631 //
00632 template <typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
00633 struct BinOp2_match {
00634   LHS_t L;
00635   RHS_t R;
00636 
00637   BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00638 
00639   template <typename OpTy> bool match(OpTy *V) {
00640     if (V->getValueID() == Value::InstructionVal + Opc1 ||
00641         V->getValueID() == Value::InstructionVal + Opc2) {
00642       auto *I = cast<BinaryOperator>(V);
00643       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
00644     }
00645     if (auto *CE = dyn_cast<ConstantExpr>(V))
00646       return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
00647              L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
00648     return false;
00649   }
00650 };
00651 
00652 /// \brief Matches LShr or AShr.
00653 template <typename LHS, typename RHS>
00654 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
00655 m_Shr(const LHS &L, const RHS &R) {
00656   return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
00657 }
00658 
00659 /// \brief Matches LShr or Shl.
00660 template <typename LHS, typename RHS>
00661 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
00662 m_LogicalShift(const LHS &L, const RHS &R) {
00663   return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
00664 }
00665 
00666 /// \brief Matches UDiv and SDiv.
00667 template <typename LHS, typename RHS>
00668 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
00669 m_IDiv(const LHS &L, const RHS &R) {
00670   return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
00671 }
00672 
00673 //===----------------------------------------------------------------------===//
00674 // Class that matches exact binary ops.
00675 //
00676 template <typename SubPattern_t> struct Exact_match {
00677   SubPattern_t SubPattern;
00678 
00679   Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
00680 
00681   template <typename OpTy> bool match(OpTy *V) {
00682     if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
00683       return PEO->isExact() && SubPattern.match(V);
00684     return false;
00685   }
00686 };
00687 
00688 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
00689   return SubPattern;
00690 }
00691 
00692 //===----------------------------------------------------------------------===//
00693 // Matchers for CmpInst classes
00694 //
00695 
00696 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
00697 struct CmpClass_match {
00698   PredicateTy &Predicate;
00699   LHS_t L;
00700   RHS_t R;
00701 
00702   CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
00703       : Predicate(Pred), L(LHS), R(RHS) {}
00704 
00705   template <typename OpTy> bool match(OpTy *V) {
00706     if (Class *I = dyn_cast<Class>(V))
00707       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
00708         Predicate = I->getPredicate();
00709         return true;
00710       }
00711     return false;
00712   }
00713 };
00714 
00715 template <typename LHS, typename RHS>
00716 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
00717 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
00718   return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
00719 }
00720 
00721 template <typename LHS, typename RHS>
00722 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
00723 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
00724   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
00725 }
00726 
00727 template <typename LHS, typename RHS>
00728 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
00729 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
00730   return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
00731 }
00732 
00733 //===----------------------------------------------------------------------===//
00734 // Matchers for SelectInst classes
00735 //
00736 
00737 template <typename Cond_t, typename LHS_t, typename RHS_t>
00738 struct SelectClass_match {
00739   Cond_t C;
00740   LHS_t L;
00741   RHS_t R;
00742 
00743   SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
00744       : C(Cond), L(LHS), R(RHS) {}
00745 
00746   template <typename OpTy> bool match(OpTy *V) {
00747     if (auto *I = dyn_cast<SelectInst>(V))
00748       return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
00749              R.match(I->getOperand(2));
00750     return false;
00751   }
00752 };
00753 
00754 template <typename Cond, typename LHS, typename RHS>
00755 inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
00756                                                   const RHS &R) {
00757   return SelectClass_match<Cond, LHS, RHS>(C, L, R);
00758 }
00759 
00760 /// \brief This matches a select of two constants, e.g.:
00761 /// m_SelectCst<-1, 0>(m_Value(V))
00762 template <int64_t L, int64_t R, typename Cond>
00763 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R>>
00764 m_SelectCst(const Cond &C) {
00765   return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
00766 }
00767 
00768 //===----------------------------------------------------------------------===//
00769 // Matchers for CastInst classes
00770 //
00771 
00772 template <typename Op_t, unsigned Opcode> struct CastClass_match {
00773   Op_t Op;
00774 
00775   CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
00776 
00777   template <typename OpTy> bool match(OpTy *V) {
00778     if (auto *O = dyn_cast<Operator>(V))
00779       return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
00780     return false;
00781   }
00782 };
00783 
00784 /// \brief Matches BitCast.
00785 template <typename OpTy>
00786 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
00787   return CastClass_match<OpTy, Instruction::BitCast>(Op);
00788 }
00789 
00790 /// \brief Matches PtrToInt.
00791 template <typename OpTy>
00792 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
00793   return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
00794 }
00795 
00796 /// \brief Matches Trunc.
00797 template <typename OpTy>
00798 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
00799   return CastClass_match<OpTy, Instruction::Trunc>(Op);
00800 }
00801 
00802 /// \brief Matches SExt.
00803 template <typename OpTy>
00804 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
00805   return CastClass_match<OpTy, Instruction::SExt>(Op);
00806 }
00807 
00808 /// \brief Matches ZExt.
00809 template <typename OpTy>
00810 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
00811   return CastClass_match<OpTy, Instruction::ZExt>(Op);
00812 }
00813 
00814 /// \brief Matches UIToFP.
00815 template <typename OpTy>
00816 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
00817   return CastClass_match<OpTy, Instruction::UIToFP>(Op);
00818 }
00819 
00820 /// \brief Matches SIToFP.
00821 template <typename OpTy>
00822 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
00823   return CastClass_match<OpTy, Instruction::SIToFP>(Op);
00824 }
00825 
00826 //===----------------------------------------------------------------------===//
00827 // Matchers for unary operators
00828 //
00829 
00830 template <typename LHS_t> struct not_match {
00831   LHS_t L;
00832 
00833   not_match(const LHS_t &LHS) : L(LHS) {}
00834 
00835   template <typename OpTy> bool match(OpTy *V) {
00836     if (auto *O = dyn_cast<Operator>(V))
00837       if (O->getOpcode() == Instruction::Xor)
00838         return matchIfNot(O->getOperand(0), O->getOperand(1));
00839     return false;
00840   }
00841 
00842 private:
00843   bool matchIfNot(Value *LHS, Value *RHS) {
00844     return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
00845             // FIXME: Remove CV.
00846             isa<ConstantVector>(RHS)) &&
00847            cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
00848   }
00849 };
00850 
00851 template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }
00852 
00853 template <typename LHS_t> struct neg_match {
00854   LHS_t L;
00855 
00856   neg_match(const LHS_t &LHS) : L(LHS) {}
00857 
00858   template <typename OpTy> bool match(OpTy *V) {
00859     if (auto *O = dyn_cast<Operator>(V))
00860       if (O->getOpcode() == Instruction::Sub)
00861         return matchIfNeg(O->getOperand(0), O->getOperand(1));
00862     return false;
00863   }
00864 
00865 private:
00866   bool matchIfNeg(Value *LHS, Value *RHS) {
00867     return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
00868             isa<ConstantAggregateZero>(LHS)) &&
00869            L.match(RHS);
00870   }
00871 };
00872 
00873 /// \brief Match an integer negate.
00874 template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
00875 
00876 template <typename LHS_t> struct fneg_match {
00877   LHS_t L;
00878 
00879   fneg_match(const LHS_t &LHS) : L(LHS) {}
00880 
00881   template <typename OpTy> bool match(OpTy *V) {
00882     if (auto *O = dyn_cast<Operator>(V))
00883       if (O->getOpcode() == Instruction::FSub)
00884         return matchIfFNeg(O->getOperand(0), O->getOperand(1));
00885     return false;
00886   }
00887 
00888 private:
00889   bool matchIfFNeg(Value *LHS, Value *RHS) {
00890     if (const auto *C = dyn_cast<ConstantFP>(LHS))
00891       return C->isNegativeZeroValue() && L.match(RHS);
00892     return false;
00893   }
00894 };
00895 
00896 /// \brief Match a floating point negate.
00897 template <typename LHS> inline fneg_match<LHS> m_FNeg(const LHS &L) {
00898   return L;
00899 }
00900 
00901 //===----------------------------------------------------------------------===//
00902 // Matchers for control flow.
00903 //
00904 
00905 struct br_match {
00906   BasicBlock *&Succ;
00907   br_match(BasicBlock *&Succ) : Succ(Succ) {}
00908 
00909   template <typename OpTy> bool match(OpTy *V) {
00910     if (auto *BI = dyn_cast<BranchInst>(V))
00911       if (BI->isUnconditional()) {
00912         Succ = BI->getSuccessor(0);
00913         return true;
00914       }
00915     return false;
00916   }
00917 };
00918 
00919 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
00920 
00921 template <typename Cond_t> struct brc_match {
00922   Cond_t Cond;
00923   BasicBlock *&T, *&F;
00924   brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
00925       : Cond(C), T(t), F(f) {}
00926 
00927   template <typename OpTy> bool match(OpTy *V) {
00928     if (auto *BI = dyn_cast<BranchInst>(V))
00929       if (BI->isConditional() && Cond.match(BI->getCondition())) {
00930         T = BI->getSuccessor(0);
00931         F = BI->getSuccessor(1);
00932         return true;
00933       }
00934     return false;
00935   }
00936 };
00937 
00938 template <typename Cond_t>
00939 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
00940   return brc_match<Cond_t>(C, T, F);
00941 }
00942 
00943 //===----------------------------------------------------------------------===//
00944 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
00945 //
00946 
00947 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
00948 struct MaxMin_match {
00949   LHS_t L;
00950   RHS_t R;
00951 
00952   MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00953 
00954   template <typename OpTy> bool match(OpTy *V) {
00955     // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
00956     auto *SI = dyn_cast<SelectInst>(V);
00957     if (!SI)
00958       return false;
00959     auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
00960     if (!Cmp)
00961       return false;
00962     // At this point we have a select conditioned on a comparison.  Check that
00963     // it is the values returned by the select that are being compared.
00964     Value *TrueVal = SI->getTrueValue();
00965     Value *FalseVal = SI->getFalseValue();
00966     Value *LHS = Cmp->getOperand(0);
00967     Value *RHS = Cmp->getOperand(1);
00968     if ((TrueVal != LHS || FalseVal != RHS) &&
00969         (TrueVal != RHS || FalseVal != LHS))
00970       return false;
00971     typename CmpInst_t::Predicate Pred =
00972         LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate();
00973     // Does "(x pred y) ? x : y" represent the desired max/min operation?
00974     if (!Pred_t::match(Pred))
00975       return false;
00976     // It does!  Bind the operands.
00977     return L.match(LHS) && R.match(RHS);
00978   }
00979 };
00980 
00981 /// \brief Helper class for identifying signed max predicates.
00982 struct smax_pred_ty {
00983   static bool match(ICmpInst::Predicate Pred) {
00984     return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
00985   }
00986 };
00987 
00988 /// \brief Helper class for identifying signed min predicates.
00989 struct smin_pred_ty {
00990   static bool match(ICmpInst::Predicate Pred) {
00991     return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
00992   }
00993 };
00994 
00995 /// \brief Helper class for identifying unsigned max predicates.
00996 struct umax_pred_ty {
00997   static bool match(ICmpInst::Predicate Pred) {
00998     return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
00999   }
01000 };
01001 
01002 /// \brief Helper class for identifying unsigned min predicates.
01003 struct umin_pred_ty {
01004   static bool match(ICmpInst::Predicate Pred) {
01005     return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
01006   }
01007 };
01008 
01009 /// \brief Helper class for identifying ordered max predicates.
01010 struct ofmax_pred_ty {
01011   static bool match(FCmpInst::Predicate Pred) {
01012     return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
01013   }
01014 };
01015 
01016 /// \brief Helper class for identifying ordered min predicates.
01017 struct ofmin_pred_ty {
01018   static bool match(FCmpInst::Predicate Pred) {
01019     return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
01020   }
01021 };
01022 
01023 /// \brief Helper class for identifying unordered max predicates.
01024 struct ufmax_pred_ty {
01025   static bool match(FCmpInst::Predicate Pred) {
01026     return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
01027   }
01028 };
01029 
01030 /// \brief Helper class for identifying unordered min predicates.
01031 struct ufmin_pred_ty {
01032   static bool match(FCmpInst::Predicate Pred) {
01033     return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
01034   }
01035 };
01036 
01037 template <typename LHS, typename RHS>
01038 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
01039                                                              const RHS &R) {
01040   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
01041 }
01042 
01043 template <typename LHS, typename RHS>
01044 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
01045                                                              const RHS &R) {
01046   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
01047 }
01048 
01049 template <typename LHS, typename RHS>
01050 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
01051                                                              const RHS &R) {
01052   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
01053 }
01054 
01055 template <typename LHS, typename RHS>
01056 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
01057                                                              const RHS &R) {
01058   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
01059 }
01060 
01061 /// \brief Match an 'ordered' floating point maximum function.
01062 /// Floating point has one special value 'NaN'. Therefore, there is no total
01063 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01064 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
01065 /// semantics. In the presence of 'NaN' we have to preserve the original
01066 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
01067 ///
01068 ///                         max(L, R)  iff L and R are not NaN
01069 ///  m_OrdFMax(L, R) =      R          iff L or R are NaN
01070 template <typename LHS, typename RHS>
01071 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
01072                                                                  const RHS &R) {
01073   return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
01074 }
01075 
01076 /// \brief Match an 'ordered' floating point minimum function.
01077 /// Floating point has one special value 'NaN'. Therefore, there is no total
01078 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01079 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
01080 /// semantics. In the presence of 'NaN' we have to preserve the original
01081 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
01082 ///
01083 ///                         max(L, R)  iff L and R are not NaN
01084 ///  m_OrdFMin(L, R) =      R          iff L or R are NaN
01085 template <typename LHS, typename RHS>
01086 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
01087                                                                  const RHS &R) {
01088   return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
01089 }
01090 
01091 /// \brief Match an 'unordered' floating point maximum function.
01092 /// Floating point has one special value 'NaN'. Therefore, there is no total
01093 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01094 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
01095 /// semantics. In the presence of 'NaN' we have to preserve the original
01096 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
01097 ///
01098 ///                         max(L, R)  iff L and R are not NaN
01099 ///  m_UnordFMin(L, R) =    L          iff L or R are NaN
01100 template <typename LHS, typename RHS>
01101 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
01102 m_UnordFMax(const LHS &L, const RHS &R) {
01103   return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
01104 }
01105 
01106 /// \brief Match an 'unordered' floating point minimum function.
01107 /// Floating point has one special value 'NaN'. Therefore, there is no total
01108 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01109 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
01110 /// semantics. In the presence of 'NaN' we have to preserve the original
01111 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
01112 ///
01113 ///                          max(L, R)  iff L and R are not NaN
01114 ///  m_UnordFMin(L, R) =     L          iff L or R are NaN
01115 template <typename LHS, typename RHS>
01116 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
01117 m_UnordFMin(const LHS &L, const RHS &R) {
01118   return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
01119 }
01120 
01121 template <typename Opnd_t> struct Argument_match {
01122   unsigned OpI;
01123   Opnd_t Val;
01124   Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
01125 
01126   template <typename OpTy> bool match(OpTy *V) {
01127     CallSite CS(V);
01128     return CS.isCall() && Val.match(CS.getArgument(OpI));
01129   }
01130 };
01131 
01132 /// \brief Match an argument.
01133 template <unsigned OpI, typename Opnd_t>
01134 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
01135   return Argument_match<Opnd_t>(OpI, Op);
01136 }
01137 
01138 /// \brief Intrinsic matchers.
01139 struct IntrinsicID_match {
01140   unsigned ID;
01141   IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
01142 
01143   template <typename OpTy> bool match(OpTy *V) {
01144     if (const auto *CI = dyn_cast<CallInst>(V))
01145       if (const auto *F = CI->getCalledFunction())
01146         return F->getIntrinsicID() == ID;
01147     return false;
01148   }
01149 };
01150 
01151 /// Intrinsic matches are combinations of ID matchers, and argument
01152 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
01153 /// them with lower arity matchers. Here's some convenient typedefs for up to
01154 /// several arguments, and more can be added as needed
01155 template <typename T0 = void, typename T1 = void, typename T2 = void,
01156           typename T3 = void, typename T4 = void, typename T5 = void,
01157           typename T6 = void, typename T7 = void, typename T8 = void,
01158           typename T9 = void, typename T10 = void>
01159 struct m_Intrinsic_Ty;
01160 template <typename T0> struct m_Intrinsic_Ty<T0> {
01161   typedef match_combine_and<IntrinsicID_match, Argument_match<T0>> Ty;
01162 };
01163 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
01164   typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>
01165       Ty;
01166 };
01167 template <typename T0, typename T1, typename T2>
01168 struct m_Intrinsic_Ty<T0, T1, T2> {
01169   typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
01170                             Argument_match<T2>> Ty;
01171 };
01172 template <typename T0, typename T1, typename T2, typename T3>
01173 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
01174   typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
01175                             Argument_match<T3>> Ty;
01176 };
01177 
01178 /// \brief Match intrinsic calls like this:
01179 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
01180 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
01181   return IntrinsicID_match(IntrID);
01182 }
01183 
01184 template <Intrinsic::ID IntrID, typename T0>
01185 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
01186   return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
01187 }
01188 
01189 template <Intrinsic::ID IntrID, typename T0, typename T1>
01190 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
01191                                                        const T1 &Op1) {
01192   return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
01193 }
01194 
01195 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
01196 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
01197 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
01198   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
01199 }
01200 
01201 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
01202           typename T3>
01203 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
01204 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
01205   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
01206 }
01207 
01208 // Helper intrinsic matching specializations.
01209 template <typename Opnd0>
01210 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
01211   return m_Intrinsic<Intrinsic::bswap>(Op0);
01212 }
01213 
01214 template <typename Opnd0, typename Opnd1>
01215 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
01216                                                         const Opnd1 &Op1) {
01217   return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
01218 }
01219 
01220 template <typename Opnd0, typename Opnd1>
01221 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
01222                                                         const Opnd1 &Op1) {
01223   return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
01224 }
01225 
01226 } // end namespace PatternMatch
01227 } // end namespace llvm
01228 
01229 #endif