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