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