LLVM  mainline
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 an instruction, capturing it if we match.
00299 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
00300 
00301 /// \brief Match a binary operator, capturing it if we match.
00302 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
00303 
00304 /// \brief Match a ConstantInt, capturing the value if we match.
00305 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
00306 
00307 /// \brief Match a Constant, capturing the value if we match.
00308 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
00309 
00310 /// \brief Match a ConstantFP, capturing the value if we match.
00311 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
00312 
00313 /// \brief Match a specified Value*.
00314 struct specificval_ty {
00315   const Value *Val;
00316   specificval_ty(const Value *V) : Val(V) {}
00317 
00318   template <typename ITy> bool match(ITy *V) { return V == Val; }
00319 };
00320 
00321 /// \brief Match if we have a specific specified value.
00322 inline specificval_ty m_Specific(const Value *V) { return V; }
00323 
00324 /// \brief Match a specified floating point value or vector of all elements of
00325 /// that value.
00326 struct specific_fpval {
00327   double Val;
00328   specific_fpval(double V) : Val(V) {}
00329 
00330   template <typename ITy> bool match(ITy *V) {
00331     if (const auto *CFP = dyn_cast<ConstantFP>(V))
00332       return CFP->isExactlyValue(Val);
00333     if (V->getType()->isVectorTy())
00334       if (const auto *C = dyn_cast<Constant>(V))
00335         if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
00336           return CFP->isExactlyValue(Val);
00337     return false;
00338   }
00339 };
00340 
00341 /// \brief Match a specific floating point value or vector with all elements
00342 /// equal to the value.
00343 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
00344 
00345 /// \brief Match a float 1.0 or vector with all elements equal to 1.0.
00346 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
00347 
00348 struct bind_const_intval_ty {
00349   uint64_t &VR;
00350   bind_const_intval_ty(uint64_t &V) : VR(V) {}
00351 
00352   template <typename ITy> bool match(ITy *V) {
00353     if (const auto *CV = dyn_cast<ConstantInt>(V))
00354       if (CV->getBitWidth() <= 64) {
00355         VR = CV->getZExtValue();
00356         return true;
00357       }
00358     return false;
00359   }
00360 };
00361 
00362 /// \brief Match a specified integer value or vector of all elements of that
00363 // value.
00364 struct specific_intval {
00365   uint64_t Val;
00366   specific_intval(uint64_t V) : Val(V) {}
00367 
00368   template <typename ITy> bool match(ITy *V) {
00369     const auto *CI = dyn_cast<ConstantInt>(V);
00370     if (!CI && V->getType()->isVectorTy())
00371       if (const auto *C = dyn_cast<Constant>(V))
00372         CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
00373 
00374     if (CI && CI->getBitWidth() <= 64)
00375       return CI->getZExtValue() == Val;
00376 
00377     return false;
00378   }
00379 };
00380 
00381 /// \brief Match a specific integer value or vector with all elements equal to
00382 /// the value.
00383 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
00384 
00385 /// \brief Match a ConstantInt and bind to its value.  This does not match
00386 /// ConstantInts wider than 64-bits.
00387 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
00388 
00389 //===----------------------------------------------------------------------===//
00390 // Matcher for any binary operator.
00391 //
00392 template <typename LHS_t, typename RHS_t> struct AnyBinaryOp_match {
00393   LHS_t L;
00394   RHS_t R;
00395 
00396   AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00397 
00398   template <typename OpTy> bool match(OpTy *V) {
00399     if (auto *I = dyn_cast<BinaryOperator>(V))
00400       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
00401     return false;
00402   }
00403 };
00404 
00405 template <typename LHS, typename RHS>
00406 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
00407   return AnyBinaryOp_match<LHS, RHS>(L, R);
00408 }
00409 
00410 //===----------------------------------------------------------------------===//
00411 // Matchers for specific binary operators.
00412 //
00413 
00414 template <typename LHS_t, typename RHS_t, unsigned Opcode>
00415 struct BinaryOp_match {
00416   LHS_t L;
00417   RHS_t R;
00418 
00419   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00420 
00421   template <typename OpTy> bool match(OpTy *V) {
00422     if (V->getValueID() == Value::InstructionVal + Opcode) {
00423       auto *I = cast<BinaryOperator>(V);
00424       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
00425     }
00426     if (auto *CE = dyn_cast<ConstantExpr>(V))
00427       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
00428              R.match(CE->getOperand(1));
00429     return false;
00430   }
00431 };
00432 
00433 template <typename LHS, typename RHS>
00434 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
00435                                                         const RHS &R) {
00436   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
00437 }
00438 
00439 template <typename LHS, typename RHS>
00440 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
00441                                                           const RHS &R) {
00442   return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
00443 }
00444 
00445 template <typename LHS, typename RHS>
00446 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
00447                                                         const RHS &R) {
00448   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
00449 }
00450 
00451 template <typename LHS, typename RHS>
00452 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
00453                                                           const RHS &R) {
00454   return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
00455 }
00456 
00457 template <typename LHS, typename RHS>
00458 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
00459                                                         const RHS &R) {
00460   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
00461 }
00462 
00463 template <typename LHS, typename RHS>
00464 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
00465                                                           const RHS &R) {
00466   return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
00467 }
00468 
00469 template <typename LHS, typename RHS>
00470 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
00471                                                           const RHS &R) {
00472   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
00473 }
00474 
00475 template <typename LHS, typename RHS>
00476 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
00477                                                           const RHS &R) {
00478   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
00479 }
00480 
00481 template <typename LHS, typename RHS>
00482 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
00483                                                           const RHS &R) {
00484   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
00485 }
00486 
00487 template <typename LHS, typename RHS>
00488 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
00489                                                           const RHS &R) {
00490   return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
00491 }
00492 
00493 template <typename LHS, typename RHS>
00494 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
00495                                                           const RHS &R) {
00496   return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
00497 }
00498 
00499 template <typename LHS, typename RHS>
00500 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
00501                                                           const RHS &R) {
00502   return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
00503 }
00504 
00505 template <typename LHS, typename RHS>
00506 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
00507                                                         const RHS &R) {
00508   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
00509 }
00510 
00511 template <typename LHS, typename RHS>
00512 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
00513                                                       const RHS &R) {
00514   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
00515 }
00516 
00517 template <typename LHS, typename RHS>
00518 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
00519                                                         const RHS &R) {
00520   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
00521 }
00522 
00523 template <typename LHS, typename RHS>
00524 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
00525                                                         const RHS &R) {
00526   return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
00527 }
00528 
00529 template <typename LHS, typename RHS>
00530 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
00531                                                           const RHS &R) {
00532   return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
00533 }
00534 
00535 template <typename LHS, typename RHS>
00536 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
00537                                                           const RHS &R) {
00538   return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
00539 }
00540 
00541 template <typename LHS_t, typename RHS_t, unsigned Opcode,
00542           unsigned WrapFlags = 0>
00543 struct OverflowingBinaryOp_match {
00544   LHS_t L;
00545   RHS_t R;
00546 
00547   OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
00548       : L(LHS), R(RHS) {}
00549 
00550   template <typename OpTy> bool match(OpTy *V) {
00551     if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
00552       if (Op->getOpcode() != Opcode)
00553         return false;
00554       if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
00555           !Op->hasNoUnsignedWrap())
00556         return false;
00557       if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
00558           !Op->hasNoSignedWrap())
00559         return false;
00560       return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
00561     }
00562     return false;
00563   }
00564 };
00565 
00566 template <typename LHS, typename RHS>
00567 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00568                                  OverflowingBinaryOperator::NoSignedWrap>
00569 m_NSWAdd(const LHS &L, const RHS &R) {
00570   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00571                                    OverflowingBinaryOperator::NoSignedWrap>(
00572       L, R);
00573 }
00574 template <typename LHS, typename RHS>
00575 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00576                                  OverflowingBinaryOperator::NoSignedWrap>
00577 m_NSWSub(const LHS &L, const RHS &R) {
00578   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00579                                    OverflowingBinaryOperator::NoSignedWrap>(
00580       L, R);
00581 }
00582 template <typename LHS, typename RHS>
00583 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00584                                  OverflowingBinaryOperator::NoSignedWrap>
00585 m_NSWMul(const LHS &L, const RHS &R) {
00586   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00587                                    OverflowingBinaryOperator::NoSignedWrap>(
00588       L, R);
00589 }
00590 template <typename LHS, typename RHS>
00591 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00592                                  OverflowingBinaryOperator::NoSignedWrap>
00593 m_NSWShl(const LHS &L, const RHS &R) {
00594   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00595                                    OverflowingBinaryOperator::NoSignedWrap>(
00596       L, R);
00597 }
00598 
00599 template <typename LHS, typename RHS>
00600 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00601                                  OverflowingBinaryOperator::NoUnsignedWrap>
00602 m_NUWAdd(const LHS &L, const RHS &R) {
00603   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
00604                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00605       L, R);
00606 }
00607 template <typename LHS, typename RHS>
00608 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00609                                  OverflowingBinaryOperator::NoUnsignedWrap>
00610 m_NUWSub(const LHS &L, const RHS &R) {
00611   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
00612                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00613       L, R);
00614 }
00615 template <typename LHS, typename RHS>
00616 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00617                                  OverflowingBinaryOperator::NoUnsignedWrap>
00618 m_NUWMul(const LHS &L, const RHS &R) {
00619   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
00620                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00621       L, R);
00622 }
00623 template <typename LHS, typename RHS>
00624 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00625                                  OverflowingBinaryOperator::NoUnsignedWrap>
00626 m_NUWShl(const LHS &L, const RHS &R) {
00627   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
00628                                    OverflowingBinaryOperator::NoUnsignedWrap>(
00629       L, R);
00630 }
00631 
00632 //===----------------------------------------------------------------------===//
00633 // Class that matches two different binary ops.
00634 //
00635 template <typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
00636 struct BinOp2_match {
00637   LHS_t L;
00638   RHS_t R;
00639 
00640   BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00641 
00642   template <typename OpTy> bool match(OpTy *V) {
00643     if (V->getValueID() == Value::InstructionVal + Opc1 ||
00644         V->getValueID() == Value::InstructionVal + Opc2) {
00645       auto *I = cast<BinaryOperator>(V);
00646       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
00647     }
00648     if (auto *CE = dyn_cast<ConstantExpr>(V))
00649       return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
00650              L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
00651     return false;
00652   }
00653 };
00654 
00655 /// \brief Matches LShr or AShr.
00656 template <typename LHS, typename RHS>
00657 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
00658 m_Shr(const LHS &L, const RHS &R) {
00659   return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
00660 }
00661 
00662 /// \brief Matches LShr or Shl.
00663 template <typename LHS, typename RHS>
00664 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
00665 m_LogicalShift(const LHS &L, const RHS &R) {
00666   return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
00667 }
00668 
00669 /// \brief Matches UDiv and SDiv.
00670 template <typename LHS, typename RHS>
00671 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
00672 m_IDiv(const LHS &L, const RHS &R) {
00673   return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
00674 }
00675 
00676 //===----------------------------------------------------------------------===//
00677 // Class that matches exact binary ops.
00678 //
00679 template <typename SubPattern_t> struct Exact_match {
00680   SubPattern_t SubPattern;
00681 
00682   Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
00683 
00684   template <typename OpTy> bool match(OpTy *V) {
00685     if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
00686       return PEO->isExact() && SubPattern.match(V);
00687     return false;
00688   }
00689 };
00690 
00691 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
00692   return SubPattern;
00693 }
00694 
00695 //===----------------------------------------------------------------------===//
00696 // Matchers for CmpInst classes
00697 //
00698 
00699 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
00700 struct CmpClass_match {
00701   PredicateTy &Predicate;
00702   LHS_t L;
00703   RHS_t R;
00704 
00705   CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
00706       : Predicate(Pred), L(LHS), R(RHS) {}
00707 
00708   template <typename OpTy> bool match(OpTy *V) {
00709     if (Class *I = dyn_cast<Class>(V))
00710       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
00711         Predicate = I->getPredicate();
00712         return true;
00713       }
00714     return false;
00715   }
00716 };
00717 
00718 template <typename LHS, typename RHS>
00719 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
00720 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
00721   return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
00722 }
00723 
00724 template <typename LHS, typename RHS>
00725 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
00726 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
00727   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
00728 }
00729 
00730 template <typename LHS, typename RHS>
00731 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
00732 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
00733   return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
00734 }
00735 
00736 //===----------------------------------------------------------------------===//
00737 // Matchers for SelectInst classes
00738 //
00739 
00740 template <typename Cond_t, typename LHS_t, typename RHS_t>
00741 struct SelectClass_match {
00742   Cond_t C;
00743   LHS_t L;
00744   RHS_t R;
00745 
00746   SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
00747       : C(Cond), L(LHS), R(RHS) {}
00748 
00749   template <typename OpTy> bool match(OpTy *V) {
00750     if (auto *I = dyn_cast<SelectInst>(V))
00751       return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
00752              R.match(I->getOperand(2));
00753     return false;
00754   }
00755 };
00756 
00757 template <typename Cond, typename LHS, typename RHS>
00758 inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
00759                                                   const RHS &R) {
00760   return SelectClass_match<Cond, LHS, RHS>(C, L, R);
00761 }
00762 
00763 /// \brief This matches a select of two constants, e.g.:
00764 /// m_SelectCst<-1, 0>(m_Value(V))
00765 template <int64_t L, int64_t R, typename Cond>
00766 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R>>
00767 m_SelectCst(const Cond &C) {
00768   return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
00769 }
00770 
00771 //===----------------------------------------------------------------------===//
00772 // Matchers for CastInst classes
00773 //
00774 
00775 template <typename Op_t, unsigned Opcode> struct CastClass_match {
00776   Op_t Op;
00777 
00778   CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
00779 
00780   template <typename OpTy> bool match(OpTy *V) {
00781     if (auto *O = dyn_cast<Operator>(V))
00782       return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
00783     return false;
00784   }
00785 };
00786 
00787 /// \brief Matches BitCast.
00788 template <typename OpTy>
00789 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
00790   return CastClass_match<OpTy, Instruction::BitCast>(Op);
00791 }
00792 
00793 /// \brief Matches PtrToInt.
00794 template <typename OpTy>
00795 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
00796   return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
00797 }
00798 
00799 /// \brief Matches Trunc.
00800 template <typename OpTy>
00801 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
00802   return CastClass_match<OpTy, Instruction::Trunc>(Op);
00803 }
00804 
00805 /// \brief Matches SExt.
00806 template <typename OpTy>
00807 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
00808   return CastClass_match<OpTy, Instruction::SExt>(Op);
00809 }
00810 
00811 /// \brief Matches ZExt.
00812 template <typename OpTy>
00813 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
00814   return CastClass_match<OpTy, Instruction::ZExt>(Op);
00815 }
00816 
00817 /// \brief Matches UIToFP.
00818 template <typename OpTy>
00819 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
00820   return CastClass_match<OpTy, Instruction::UIToFP>(Op);
00821 }
00822 
00823 /// \brief Matches SIToFP.
00824 template <typename OpTy>
00825 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
00826   return CastClass_match<OpTy, Instruction::SIToFP>(Op);
00827 }
00828 
00829 //===----------------------------------------------------------------------===//
00830 // Matchers for unary operators
00831 //
00832 
00833 template <typename LHS_t> struct not_match {
00834   LHS_t L;
00835 
00836   not_match(const LHS_t &LHS) : L(LHS) {}
00837 
00838   template <typename OpTy> bool match(OpTy *V) {
00839     if (auto *O = dyn_cast<Operator>(V))
00840       if (O->getOpcode() == Instruction::Xor)
00841         return matchIfNot(O->getOperand(0), O->getOperand(1));
00842     return false;
00843   }
00844 
00845 private:
00846   bool matchIfNot(Value *LHS, Value *RHS) {
00847     return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
00848             // FIXME: Remove CV.
00849             isa<ConstantVector>(RHS)) &&
00850            cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
00851   }
00852 };
00853 
00854 template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }
00855 
00856 template <typename LHS_t> struct neg_match {
00857   LHS_t L;
00858 
00859   neg_match(const LHS_t &LHS) : L(LHS) {}
00860 
00861   template <typename OpTy> bool match(OpTy *V) {
00862     if (auto *O = dyn_cast<Operator>(V))
00863       if (O->getOpcode() == Instruction::Sub)
00864         return matchIfNeg(O->getOperand(0), O->getOperand(1));
00865     return false;
00866   }
00867 
00868 private:
00869   bool matchIfNeg(Value *LHS, Value *RHS) {
00870     return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
00871             isa<ConstantAggregateZero>(LHS)) &&
00872            L.match(RHS);
00873   }
00874 };
00875 
00876 /// \brief Match an integer negate.
00877 template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
00878 
00879 template <typename LHS_t> struct fneg_match {
00880   LHS_t L;
00881 
00882   fneg_match(const LHS_t &LHS) : L(LHS) {}
00883 
00884   template <typename OpTy> bool match(OpTy *V) {
00885     if (auto *O = dyn_cast<Operator>(V))
00886       if (O->getOpcode() == Instruction::FSub)
00887         return matchIfFNeg(O->getOperand(0), O->getOperand(1));
00888     return false;
00889   }
00890 
00891 private:
00892   bool matchIfFNeg(Value *LHS, Value *RHS) {
00893     if (const auto *C = dyn_cast<ConstantFP>(LHS))
00894       return C->isNegativeZeroValue() && L.match(RHS);
00895     return false;
00896   }
00897 };
00898 
00899 /// \brief Match a floating point negate.
00900 template <typename LHS> inline fneg_match<LHS> m_FNeg(const LHS &L) {
00901   return L;
00902 }
00903 
00904 //===----------------------------------------------------------------------===//
00905 // Matchers for control flow.
00906 //
00907 
00908 struct br_match {
00909   BasicBlock *&Succ;
00910   br_match(BasicBlock *&Succ) : Succ(Succ) {}
00911 
00912   template <typename OpTy> bool match(OpTy *V) {
00913     if (auto *BI = dyn_cast<BranchInst>(V))
00914       if (BI->isUnconditional()) {
00915         Succ = BI->getSuccessor(0);
00916         return true;
00917       }
00918     return false;
00919   }
00920 };
00921 
00922 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
00923 
00924 template <typename Cond_t> struct brc_match {
00925   Cond_t Cond;
00926   BasicBlock *&T, *&F;
00927   brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
00928       : Cond(C), T(t), F(f) {}
00929 
00930   template <typename OpTy> bool match(OpTy *V) {
00931     if (auto *BI = dyn_cast<BranchInst>(V))
00932       if (BI->isConditional() && Cond.match(BI->getCondition())) {
00933         T = BI->getSuccessor(0);
00934         F = BI->getSuccessor(1);
00935         return true;
00936       }
00937     return false;
00938   }
00939 };
00940 
00941 template <typename Cond_t>
00942 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
00943   return brc_match<Cond_t>(C, T, F);
00944 }
00945 
00946 //===----------------------------------------------------------------------===//
00947 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
00948 //
00949 
00950 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
00951 struct MaxMin_match {
00952   LHS_t L;
00953   RHS_t R;
00954 
00955   MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
00956 
00957   template <typename OpTy> bool match(OpTy *V) {
00958     // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
00959     auto *SI = dyn_cast<SelectInst>(V);
00960     if (!SI)
00961       return false;
00962     auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
00963     if (!Cmp)
00964       return false;
00965     // At this point we have a select conditioned on a comparison.  Check that
00966     // it is the values returned by the select that are being compared.
00967     Value *TrueVal = SI->getTrueValue();
00968     Value *FalseVal = SI->getFalseValue();
00969     Value *LHS = Cmp->getOperand(0);
00970     Value *RHS = Cmp->getOperand(1);
00971     if ((TrueVal != LHS || FalseVal != RHS) &&
00972         (TrueVal != RHS || FalseVal != LHS))
00973       return false;
00974     typename CmpInst_t::Predicate Pred =
00975         LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate();
00976     // Does "(x pred y) ? x : y" represent the desired max/min operation?
00977     if (!Pred_t::match(Pred))
00978       return false;
00979     // It does!  Bind the operands.
00980     return L.match(LHS) && R.match(RHS);
00981   }
00982 };
00983 
00984 /// \brief Helper class for identifying signed max predicates.
00985 struct smax_pred_ty {
00986   static bool match(ICmpInst::Predicate Pred) {
00987     return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
00988   }
00989 };
00990 
00991 /// \brief Helper class for identifying signed min predicates.
00992 struct smin_pred_ty {
00993   static bool match(ICmpInst::Predicate Pred) {
00994     return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
00995   }
00996 };
00997 
00998 /// \brief Helper class for identifying unsigned max predicates.
00999 struct umax_pred_ty {
01000   static bool match(ICmpInst::Predicate Pred) {
01001     return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
01002   }
01003 };
01004 
01005 /// \brief Helper class for identifying unsigned min predicates.
01006 struct umin_pred_ty {
01007   static bool match(ICmpInst::Predicate Pred) {
01008     return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
01009   }
01010 };
01011 
01012 /// \brief Helper class for identifying ordered max predicates.
01013 struct ofmax_pred_ty {
01014   static bool match(FCmpInst::Predicate Pred) {
01015     return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
01016   }
01017 };
01018 
01019 /// \brief Helper class for identifying ordered min predicates.
01020 struct ofmin_pred_ty {
01021   static bool match(FCmpInst::Predicate Pred) {
01022     return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
01023   }
01024 };
01025 
01026 /// \brief Helper class for identifying unordered max predicates.
01027 struct ufmax_pred_ty {
01028   static bool match(FCmpInst::Predicate Pred) {
01029     return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
01030   }
01031 };
01032 
01033 /// \brief Helper class for identifying unordered min predicates.
01034 struct ufmin_pred_ty {
01035   static bool match(FCmpInst::Predicate Pred) {
01036     return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
01037   }
01038 };
01039 
01040 template <typename LHS, typename RHS>
01041 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
01042                                                              const RHS &R) {
01043   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
01044 }
01045 
01046 template <typename LHS, typename RHS>
01047 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
01048                                                              const RHS &R) {
01049   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
01050 }
01051 
01052 template <typename LHS, typename RHS>
01053 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
01054                                                              const RHS &R) {
01055   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
01056 }
01057 
01058 template <typename LHS, typename RHS>
01059 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
01060                                                              const RHS &R) {
01061   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
01062 }
01063 
01064 /// \brief Match an 'ordered' floating point maximum function.
01065 /// Floating point has one special value 'NaN'. Therefore, there is no total
01066 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01067 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
01068 /// semantics. In the presence of 'NaN' we have to preserve the original
01069 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
01070 ///
01071 ///                         max(L, R)  iff L and R are not NaN
01072 ///  m_OrdFMax(L, R) =      R          iff L or R are NaN
01073 template <typename LHS, typename RHS>
01074 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
01075                                                                  const RHS &R) {
01076   return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
01077 }
01078 
01079 /// \brief Match an 'ordered' floating point minimum function.
01080 /// Floating point has one special value 'NaN'. Therefore, there is no total
01081 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01082 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
01083 /// semantics. In the presence of 'NaN' we have to preserve the original
01084 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
01085 ///
01086 ///                         max(L, R)  iff L and R are not NaN
01087 ///  m_OrdFMin(L, R) =      R          iff L or R are NaN
01088 template <typename LHS, typename RHS>
01089 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
01090                                                                  const RHS &R) {
01091   return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
01092 }
01093 
01094 /// \brief Match an 'unordered' floating point maximum function.
01095 /// Floating point has one special value 'NaN'. Therefore, there is no total
01096 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01097 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
01098 /// semantics. In the presence of 'NaN' we have to preserve the original
01099 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
01100 ///
01101 ///                         max(L, R)  iff L and R are not NaN
01102 ///  m_UnordFMin(L, R) =    L          iff L or R are NaN
01103 template <typename LHS, typename RHS>
01104 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
01105 m_UnordFMax(const LHS &L, const RHS &R) {
01106   return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
01107 }
01108 
01109 //===----------------------------------------------------------------------===//
01110 // Matchers for overflow check patterns: e.g. (a + b) u< a
01111 //
01112 
01113 template <typename LHS_t, typename RHS_t, typename Sum_t>
01114 struct UAddWithOverflow_match {
01115   LHS_t L;
01116   RHS_t R;
01117   Sum_t S;
01118 
01119   UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
01120       : L(L), R(R), S(S) {}
01121 
01122   template <typename OpTy> bool match(OpTy *V) {
01123     Value *ICmpLHS, *ICmpRHS;
01124     ICmpInst::Predicate Pred;
01125     if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
01126       return false;
01127 
01128     Value *AddLHS, *AddRHS;
01129     auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
01130 
01131     // (a + b) u< a, (a + b) u< b
01132     if (Pred == ICmpInst::ICMP_ULT)
01133       if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
01134         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
01135 
01136     // a >u (a + b), b >u (a + b)
01137     if (Pred == ICmpInst::ICMP_UGT)
01138       if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
01139         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
01140 
01141     return false;
01142   }
01143 };
01144 
01145 /// \brief Match an icmp instruction checking for unsigned overflow on addition.
01146 ///
01147 /// S is matched to the addition whose result is being checked for overflow, and
01148 /// L and R are matched to the LHS and RHS of S.
01149 template <typename LHS_t, typename RHS_t, typename Sum_t>
01150 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
01151 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
01152   return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
01153 }
01154 
01155 /// \brief Match an 'unordered' floating point minimum function.
01156 /// Floating point has one special value 'NaN'. Therefore, there is no total
01157 /// order. However, if we can ignore the 'NaN' value (for example, because of a
01158 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
01159 /// semantics. In the presence of 'NaN' we have to preserve the original
01160 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
01161 ///
01162 ///                          max(L, R)  iff L and R are not NaN
01163 ///  m_UnordFMin(L, R) =     L          iff L or R are NaN
01164 template <typename LHS, typename RHS>
01165 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
01166 m_UnordFMin(const LHS &L, const RHS &R) {
01167   return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
01168 }
01169 
01170 template <typename Opnd_t> struct Argument_match {
01171   unsigned OpI;
01172   Opnd_t Val;
01173   Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
01174 
01175   template <typename OpTy> bool match(OpTy *V) {
01176     CallSite CS(V);
01177     return CS.isCall() && Val.match(CS.getArgument(OpI));
01178   }
01179 };
01180 
01181 /// \brief Match an argument.
01182 template <unsigned OpI, typename Opnd_t>
01183 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
01184   return Argument_match<Opnd_t>(OpI, Op);
01185 }
01186 
01187 /// \brief Intrinsic matchers.
01188 struct IntrinsicID_match {
01189   unsigned ID;
01190   IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
01191 
01192   template <typename OpTy> bool match(OpTy *V) {
01193     if (const auto *CI = dyn_cast<CallInst>(V))
01194       if (const auto *F = CI->getCalledFunction())
01195         return F->getIntrinsicID() == ID;
01196     return false;
01197   }
01198 };
01199 
01200 /// Intrinsic matches are combinations of ID matchers, and argument
01201 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
01202 /// them with lower arity matchers. Here's some convenient typedefs for up to
01203 /// several arguments, and more can be added as needed
01204 template <typename T0 = void, typename T1 = void, typename T2 = void,
01205           typename T3 = void, typename T4 = void, typename T5 = void,
01206           typename T6 = void, typename T7 = void, typename T8 = void,
01207           typename T9 = void, typename T10 = void>
01208 struct m_Intrinsic_Ty;
01209 template <typename T0> struct m_Intrinsic_Ty<T0> {
01210   typedef match_combine_and<IntrinsicID_match, Argument_match<T0>> Ty;
01211 };
01212 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
01213   typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>
01214       Ty;
01215 };
01216 template <typename T0, typename T1, typename T2>
01217 struct m_Intrinsic_Ty<T0, T1, T2> {
01218   typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
01219                             Argument_match<T2>> Ty;
01220 };
01221 template <typename T0, typename T1, typename T2, typename T3>
01222 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
01223   typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
01224                             Argument_match<T3>> Ty;
01225 };
01226 
01227 /// \brief Match intrinsic calls like this:
01228 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
01229 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
01230   return IntrinsicID_match(IntrID);
01231 }
01232 
01233 template <Intrinsic::ID IntrID, typename T0>
01234 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
01235   return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
01236 }
01237 
01238 template <Intrinsic::ID IntrID, typename T0, typename T1>
01239 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
01240                                                        const T1 &Op1) {
01241   return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
01242 }
01243 
01244 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
01245 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
01246 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
01247   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
01248 }
01249 
01250 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
01251           typename T3>
01252 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
01253 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
01254   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
01255 }
01256 
01257 // Helper intrinsic matching specializations.
01258 template <typename Opnd0>
01259 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
01260   return m_Intrinsic<Intrinsic::bswap>(Op0);
01261 }
01262 
01263 template <typename Opnd0, typename Opnd1>
01264 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
01265                                                         const Opnd1 &Op1) {
01266   return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
01267 }
01268 
01269 template <typename Opnd0, typename Opnd1>
01270 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
01271                                                         const Opnd1 &Op1) {
01272   return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
01273 }
01274 
01275 } // end namespace PatternMatch
01276 } // end namespace llvm
01277 
01278 #endif