1193323Sed//===-- llvm/Support/PatternMatch.h - Match on the LLVM IR ------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file provides a simple and efficient mechanism for performing general 11193323Sed// tree-based pattern matches on the LLVM IR. The power of these routines is 12193323Sed// that it allows you to write concise patterns that are expressive and easy to 13193323Sed// understand. The other major advantage of this is that it allows you to 14193323Sed// trivially capture/bind elements in the pattern to variables. For example, 15193323Sed// you can do something like this: 16193323Sed// 17193323Sed// Value *Exp = ... 18193323Sed// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2) 19193323Sed// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)), 20193323Sed// m_And(m_Value(Y), m_ConstantInt(C2))))) { 21193323Sed// ... Pattern is matched and variables are bound ... 22193323Sed// } 23193323Sed// 24193323Sed// This is primarily useful to things like the instruction combiner, but can 25193323Sed// also be useful for static analysis tools or code generators. 26193323Sed// 27193323Sed//===----------------------------------------------------------------------===// 28193323Sed 29193323Sed#ifndef LLVM_SUPPORT_PATTERNMATCH_H 30193323Sed#define LLVM_SUPPORT_PATTERNMATCH_H 31193323Sed 32252723Sdim#include "llvm/IR/Constants.h" 33252723Sdim#include "llvm/IR/Instructions.h" 34252723Sdim#include "llvm/IR/IntrinsicInst.h" 35252723Sdim#include "llvm/IR/Operator.h" 36252723Sdim#include "llvm/Support/CallSite.h" 37193323Sed 38193323Sednamespace llvm { 39193323Sednamespace PatternMatch { 40193323Sed 41193323Sedtemplate<typename Val, typename Pattern> 42193323Sedbool match(Val *V, const Pattern &P) { 43193323Sed return const_cast<Pattern&>(P).match(V); 44193323Sed} 45193323Sed 46252723Sdim 47221345Sdimtemplate<typename SubPattern_t> 48221345Sdimstruct OneUse_match { 49221345Sdim SubPattern_t SubPattern; 50252723Sdim 51221345Sdim OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} 52252723Sdim 53221345Sdim template<typename OpTy> 54221345Sdim bool match(OpTy *V) { 55221345Sdim return V->hasOneUse() && SubPattern.match(V); 56221345Sdim } 57221345Sdim}; 58221345Sdim 59221345Sdimtemplate<typename T> 60221345Sdiminline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; } 61252723Sdim 62252723Sdim 63193323Sedtemplate<typename Class> 64218893Sdimstruct class_match { 65193323Sed template<typename ITy> 66193323Sed bool match(ITy *V) { return isa<Class>(V); } 67193323Sed}; 68193323Sed 69193323Sed/// m_Value() - Match an arbitrary value and ignore it. 70218893Sdiminline class_match<Value> m_Value() { return class_match<Value>(); } 71193323Sed/// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it. 72218893Sdiminline class_match<ConstantInt> m_ConstantInt() { 73218893Sdim return class_match<ConstantInt>(); 74218893Sdim} 75218893Sdim/// m_Undef() - Match an arbitrary undef constant. 76218893Sdiminline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); } 77193323Sed 78218893Sdiminline class_match<Constant> m_Constant() { return class_match<Constant>(); } 79252723Sdim 80252723Sdim/// Matching combinators 81252723Sdimtemplate<typename LTy, typename RTy> 82252723Sdimstruct match_combine_or { 83252723Sdim LTy L; 84252723Sdim RTy R; 85252723Sdim 86252723Sdim match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { } 87252723Sdim 88252723Sdim template<typename ITy> 89252723Sdim bool match(ITy *V) { 90252723Sdim if (L.match(V)) 91252723Sdim return true; 92252723Sdim if (R.match(V)) 93252723Sdim return true; 94252723Sdim return false; 95252723Sdim } 96252723Sdim}; 97252723Sdim 98252723Sdimtemplate<typename LTy, typename RTy> 99252723Sdimstruct match_combine_and { 100252723Sdim LTy L; 101252723Sdim RTy R; 102252723Sdim 103252723Sdim match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { } 104252723Sdim 105252723Sdim template<typename ITy> 106252723Sdim bool match(ITy *V) { 107252723Sdim if (L.match(V)) 108252723Sdim if (R.match(V)) 109252723Sdim return true; 110252723Sdim return false; 111252723Sdim } 112252723Sdim}; 113252723Sdim 114252723Sdim/// Combine two pattern matchers matching L || R 115252723Sdimtemplate<typename LTy, typename RTy> 116252723Sdiminline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { 117252723Sdim return match_combine_or<LTy, RTy>(L, R); 118252723Sdim} 119252723Sdim 120252723Sdim/// Combine two pattern matchers matching L && R 121252723Sdimtemplate<typename LTy, typename RTy> 122252723Sdiminline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { 123252723Sdim return match_combine_and<LTy, RTy>(L, R); 124252723Sdim} 125252723Sdim 126218893Sdimstruct match_zero { 127218893Sdim template<typename ITy> 128218893Sdim bool match(ITy *V) { 129218893Sdim if (const Constant *C = dyn_cast<Constant>(V)) 130218893Sdim return C->isNullValue(); 131218893Sdim return false; 132218893Sdim } 133218893Sdim}; 134252723Sdim 135218893Sdim/// m_Zero() - Match an arbitrary zero/null constant. This includes 136218893Sdim/// zero_initializer for vectors and ConstantPointerNull for pointers. 137218893Sdiminline match_zero m_Zero() { return match_zero(); } 138252723Sdim 139252723Sdimstruct match_neg_zero { 140252723Sdim template<typename ITy> 141252723Sdim bool match(ITy *V) { 142252723Sdim if (const Constant *C = dyn_cast<Constant>(V)) 143252723Sdim return C->isNegativeZeroValue(); 144252723Sdim return false; 145252723Sdim } 146252723Sdim}; 147252723Sdim 148252723Sdim/// m_NegZero() - Match an arbitrary zero/null constant. This includes 149252723Sdim/// zero_initializer for vectors and ConstantPointerNull for pointers. For 150252723Sdim/// floating point constants, this will match negative zero but not positive 151252723Sdim/// zero 152252723Sdiminline match_neg_zero m_NegZero() { return match_neg_zero(); } 153252723Sdim 154252723Sdim/// m_AnyZero() - Match an arbitrary zero/null constant. This includes 155252723Sdim/// zero_initializer for vectors and ConstantPointerNull for pointers. For 156252723Sdim/// floating point constants, this will match negative zero and positive zero 157252723Sdiminline match_combine_or<match_zero, match_neg_zero> m_AnyZero() { 158252723Sdim return m_CombineOr(m_Zero(), m_NegZero()); 159252723Sdim} 160252723Sdim 161218893Sdimstruct apint_match { 162218893Sdim const APInt *&Res; 163218893Sdim apint_match(const APInt *&R) : Res(R) {} 164218893Sdim template<typename ITy> 165218893Sdim bool match(ITy *V) { 166218893Sdim if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 167218893Sdim Res = &CI->getValue(); 168218893Sdim return true; 169218893Sdim } 170252723Sdim if (V->getType()->isVectorTy()) 171252723Sdim if (const Constant *C = dyn_cast<Constant>(V)) 172252723Sdim if (ConstantInt *CI = 173252723Sdim dyn_cast_or_null<ConstantInt>(C->getSplatValue())) { 174252723Sdim Res = &CI->getValue(); 175252723Sdim return true; 176252723Sdim } 177218893Sdim return false; 178218893Sdim } 179218893Sdim}; 180252723Sdim 181218893Sdim/// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the 182218893Sdim/// specified pointer to the contained APInt. 183218893Sdiminline apint_match m_APInt(const APInt *&Res) { return Res; } 184218893Sdim 185252723Sdim 186193323Sedtemplate<int64_t Val> 187218893Sdimstruct constantint_match { 188193323Sed template<typename ITy> 189193323Sed bool match(ITy *V) { 190193323Sed if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 191193323Sed const APInt &CIV = CI->getValue(); 192193323Sed if (Val >= 0) 193198090Srdivacky return CIV == static_cast<uint64_t>(Val); 194193323Sed // If Val is negative, and CI is shorter than it, truncate to the right 195193323Sed // number of bits. If it is larger, then we have to sign extend. Just 196193323Sed // compare their negated values. 197193323Sed return -CIV == -Val; 198193323Sed } 199193323Sed return false; 200193323Sed } 201193323Sed}; 202193323Sed 203218893Sdim/// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value. 204193323Sedtemplate<int64_t Val> 205218893Sdiminline constantint_match<Val> m_ConstantInt() { 206218893Sdim return constantint_match<Val>(); 207193323Sed} 208193323Sed 209218893Sdim/// cst_pred_ty - This helper class is used to match scalar and vector constants 210218893Sdim/// that satisfy a specified predicate. 211218893Sdimtemplate<typename Predicate> 212218893Sdimstruct cst_pred_ty : public Predicate { 213193323Sed template<typename ITy> 214193323Sed bool match(ITy *V) { 215218893Sdim if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) 216218893Sdim return this->isValue(CI->getValue()); 217252723Sdim if (V->getType()->isVectorTy()) 218252723Sdim if (const Constant *C = dyn_cast<Constant>(V)) 219252723Sdim if (const ConstantInt *CI = 220252723Sdim dyn_cast_or_null<ConstantInt>(C->getSplatValue())) 221252723Sdim return this->isValue(CI->getValue()); 222193323Sed return false; 223193323Sed } 224193323Sed}; 225252723Sdim 226218893Sdim/// api_pred_ty - This helper class is used to match scalar and vector constants 227218893Sdim/// that satisfy a specified predicate, and bind them to an APInt. 228218893Sdimtemplate<typename Predicate> 229218893Sdimstruct api_pred_ty : public Predicate { 230218893Sdim const APInt *&Res; 231218893Sdim api_pred_ty(const APInt *&R) : Res(R) {} 232198090Srdivacky template<typename ITy> 233198090Srdivacky bool match(ITy *V) { 234218893Sdim if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) 235218893Sdim if (this->isValue(CI->getValue())) { 236218893Sdim Res = &CI->getValue(); 237218893Sdim return true; 238218893Sdim } 239252723Sdim if (V->getType()->isVectorTy()) 240252723Sdim if (const Constant *C = dyn_cast<Constant>(V)) 241252723Sdim if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) 242252723Sdim if (this->isValue(CI->getValue())) { 243252723Sdim Res = &CI->getValue(); 244252723Sdim return true; 245252723Sdim } 246235633Sdim 247198090Srdivacky return false; 248198090Srdivacky } 249198090Srdivacky}; 250252723Sdim 251252723Sdim 252218893Sdimstruct is_one { 253218893Sdim bool isValue(const APInt &C) { return C == 1; } 254218893Sdim}; 255193323Sed 256218893Sdim/// m_One() - Match an integer 1 or a vector with all elements equal to 1. 257218893Sdiminline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); } 258218893Sdiminline api_pred_ty<is_one> m_One(const APInt *&V) { return V; } 259252723Sdim 260218893Sdimstruct is_all_ones { 261218893Sdim bool isValue(const APInt &C) { return C.isAllOnesValue(); } 262218893Sdim}; 263252723Sdim 264218893Sdim/// m_AllOnes() - Match an integer or vector with all bits set to true. 265218893Sdiminline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();} 266218893Sdiminline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; } 267198090Srdivacky 268218893Sdimstruct is_sign_bit { 269218893Sdim bool isValue(const APInt &C) { return C.isSignBit(); } 270218893Sdim}; 271218893Sdim 272218893Sdim/// m_SignBit() - Match an integer or vector with only the sign bit(s) set. 273218893Sdiminline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();} 274218893Sdiminline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; } 275218893Sdim 276218893Sdimstruct is_power2 { 277218893Sdim bool isValue(const APInt &C) { return C.isPowerOf2(); } 278218893Sdim}; 279218893Sdim 280218893Sdim/// m_Power2() - Match an integer or vector power of 2. 281218893Sdiminline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); } 282218893Sdiminline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; } 283218893Sdim 284193323Sedtemplate<typename Class> 285193323Sedstruct bind_ty { 286193323Sed Class *&VR; 287193323Sed bind_ty(Class *&V) : VR(V) {} 288193323Sed 289193323Sed template<typename ITy> 290193323Sed bool match(ITy *V) { 291193323Sed if (Class *CV = dyn_cast<Class>(V)) { 292193323Sed VR = CV; 293193323Sed return true; 294193323Sed } 295193323Sed return false; 296193323Sed } 297193323Sed}; 298193323Sed 299193323Sed/// m_Value - Match a value, capturing it if we match. 300193323Sedinline bind_ty<Value> m_Value(Value *&V) { return V; } 301193323Sed 302193323Sed/// m_ConstantInt - Match a ConstantInt, capturing the value if we match. 303193323Sedinline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } 304193323Sed 305218893Sdim/// m_Constant - Match a Constant, capturing the value if we match. 306218893Sdiminline bind_ty<Constant> m_Constant(Constant *&C) { return C; } 307218893Sdim 308252723Sdim/// m_ConstantFP - Match a ConstantFP, capturing the value if we match. 309252723Sdiminline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } 310252723Sdim 311193323Sed/// specificval_ty - Match a specified Value*. 312193323Sedstruct specificval_ty { 313193323Sed const Value *Val; 314193323Sed specificval_ty(const Value *V) : Val(V) {} 315193323Sed 316193323Sed template<typename ITy> 317193323Sed bool match(ITy *V) { 318193323Sed return V == Val; 319193323Sed } 320193323Sed}; 321193323Sed 322193323Sed/// m_Specific - Match if we have a specific specified value. 323193323Sedinline specificval_ty m_Specific(const Value *V) { return V; } 324193323Sed 325252723Sdim/// Match a specified floating point value or vector of all elements of that 326252723Sdim/// value. 327252723Sdimstruct specific_fpval { 328252723Sdim double Val; 329252723Sdim specific_fpval(double V) : Val(V) {} 330252723Sdim 331252723Sdim template<typename ITy> 332252723Sdim bool match(ITy *V) { 333252723Sdim if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) 334252723Sdim return CFP->isExactlyValue(Val); 335252723Sdim if (V->getType()->isVectorTy()) 336252723Sdim if (const Constant *C = dyn_cast<Constant>(V)) 337252723Sdim if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) 338252723Sdim return CFP->isExactlyValue(Val); 339252723Sdim return false; 340252723Sdim } 341252723Sdim}; 342252723Sdim 343252723Sdim/// Match a specific floating point value or vector with all elements equal to 344252723Sdim/// the value. 345252723Sdiminline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } 346252723Sdim 347252723Sdim/// Match a float 1.0 or vector with all elements equal to 1.0. 348252723Sdiminline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } 349252723Sdim 350221345Sdimstruct bind_const_intval_ty { 351221345Sdim uint64_t &VR; 352221345Sdim bind_const_intval_ty(uint64_t &V) : VR(V) {} 353252723Sdim 354221345Sdim template<typename ITy> 355221345Sdim bool match(ITy *V) { 356221345Sdim if (ConstantInt *CV = dyn_cast<ConstantInt>(V)) 357221345Sdim if (CV->getBitWidth() <= 64) { 358221345Sdim VR = CV->getZExtValue(); 359221345Sdim return true; 360221345Sdim } 361221345Sdim return false; 362221345Sdim } 363221345Sdim}; 364193323Sed 365221345Sdim/// m_ConstantInt - Match a ConstantInt and bind to its value. This does not 366221345Sdim/// match ConstantInts wider than 64-bits. 367221345Sdiminline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } 368252723Sdim 369193323Sed//===----------------------------------------------------------------------===// 370193323Sed// Matchers for specific binary operators. 371193323Sed// 372193323Sed 373218893Sdimtemplate<typename LHS_t, typename RHS_t, unsigned Opcode> 374193323Sedstruct BinaryOp_match { 375193323Sed LHS_t L; 376193323Sed RHS_t R; 377193323Sed 378193323Sed BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 379193323Sed 380193323Sed template<typename OpTy> 381193323Sed bool match(OpTy *V) { 382193323Sed if (V->getValueID() == Value::InstructionVal + Opcode) { 383218893Sdim BinaryOperator *I = cast<BinaryOperator>(V); 384218893Sdim return L.match(I->getOperand(0)) && R.match(I->getOperand(1)); 385193323Sed } 386193323Sed if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 387193323Sed return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) && 388193323Sed R.match(CE->getOperand(1)); 389193323Sed return false; 390193323Sed } 391193323Sed}; 392193323Sed 393193323Sedtemplate<typename LHS, typename RHS> 394218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::Add> 395218893Sdimm_Add(const LHS &L, const RHS &R) { 396193323Sed return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); 397193323Sed} 398193323Sed 399193323Sedtemplate<typename LHS, typename RHS> 400218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::FAdd> 401218893Sdimm_FAdd(const LHS &L, const RHS &R) { 402193574Sed return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); 403193574Sed} 404193574Sed 405193574Sedtemplate<typename LHS, typename RHS> 406218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::Sub> 407218893Sdimm_Sub(const LHS &L, const RHS &R) { 408193323Sed return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); 409193323Sed} 410193323Sed 411193323Sedtemplate<typename LHS, typename RHS> 412218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::FSub> 413218893Sdimm_FSub(const LHS &L, const RHS &R) { 414193574Sed return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); 415193574Sed} 416193574Sed 417193574Sedtemplate<typename LHS, typename RHS> 418218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::Mul> 419218893Sdimm_Mul(const LHS &L, const RHS &R) { 420193323Sed return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); 421193323Sed} 422193323Sed 423193323Sedtemplate<typename LHS, typename RHS> 424218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::FMul> 425218893Sdimm_FMul(const LHS &L, const RHS &R) { 426193574Sed return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); 427193574Sed} 428193574Sed 429193574Sedtemplate<typename LHS, typename RHS> 430218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::UDiv> 431218893Sdimm_UDiv(const LHS &L, const RHS &R) { 432193323Sed return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); 433193323Sed} 434193323Sed 435193323Sedtemplate<typename LHS, typename RHS> 436218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::SDiv> 437218893Sdimm_SDiv(const LHS &L, const RHS &R) { 438193323Sed return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); 439193323Sed} 440193323Sed 441193323Sedtemplate<typename LHS, typename RHS> 442218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::FDiv> 443218893Sdimm_FDiv(const LHS &L, const RHS &R) { 444193323Sed return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); 445193323Sed} 446193323Sed 447193323Sedtemplate<typename LHS, typename RHS> 448218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::URem> 449218893Sdimm_URem(const LHS &L, const RHS &R) { 450193323Sed return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); 451193323Sed} 452193323Sed 453193323Sedtemplate<typename LHS, typename RHS> 454218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::SRem> 455218893Sdimm_SRem(const LHS &L, const RHS &R) { 456193323Sed return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); 457193323Sed} 458193323Sed 459193323Sedtemplate<typename LHS, typename RHS> 460218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::FRem> 461218893Sdimm_FRem(const LHS &L, const RHS &R) { 462193323Sed return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); 463193323Sed} 464193323Sed 465193323Sedtemplate<typename LHS, typename RHS> 466218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::And> 467218893Sdimm_And(const LHS &L, const RHS &R) { 468193323Sed return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); 469193323Sed} 470193323Sed 471193323Sedtemplate<typename LHS, typename RHS> 472218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::Or> 473218893Sdimm_Or(const LHS &L, const RHS &R) { 474193323Sed return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); 475193323Sed} 476193323Sed 477193323Sedtemplate<typename LHS, typename RHS> 478218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::Xor> 479218893Sdimm_Xor(const LHS &L, const RHS &R) { 480193323Sed return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); 481193323Sed} 482193323Sed 483193323Sedtemplate<typename LHS, typename RHS> 484218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::Shl> 485218893Sdimm_Shl(const LHS &L, const RHS &R) { 486193323Sed return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); 487193323Sed} 488193323Sed 489193323Sedtemplate<typename LHS, typename RHS> 490218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::LShr> 491218893Sdimm_LShr(const LHS &L, const RHS &R) { 492193323Sed return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); 493193323Sed} 494193323Sed 495193323Sedtemplate<typename LHS, typename RHS> 496218893Sdiminline BinaryOp_match<LHS, RHS, Instruction::AShr> 497218893Sdimm_AShr(const LHS &L, const RHS &R) { 498193323Sed return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); 499193323Sed} 500193323Sed 501193323Sed//===----------------------------------------------------------------------===// 502218893Sdim// Class that matches two different binary ops. 503193323Sed// 504218893Sdimtemplate<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2> 505218893Sdimstruct BinOp2_match { 506193323Sed LHS_t L; 507193323Sed RHS_t R; 508193323Sed 509218893Sdim BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 510193323Sed 511193323Sed template<typename OpTy> 512193323Sed bool match(OpTy *V) { 513218893Sdim if (V->getValueID() == Value::InstructionVal + Opc1 || 514218893Sdim V->getValueID() == Value::InstructionVal + Opc2) { 515218893Sdim BinaryOperator *I = cast<BinaryOperator>(V); 516218893Sdim return L.match(I->getOperand(0)) && R.match(I->getOperand(1)); 517193323Sed } 518193323Sed if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 519218893Sdim return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) && 520218893Sdim L.match(CE->getOperand(0)) && R.match(CE->getOperand(1)); 521193323Sed return false; 522193323Sed } 523193323Sed}; 524193323Sed 525218893Sdim/// m_Shr - Matches LShr or AShr. 526193323Sedtemplate<typename LHS, typename RHS> 527218893Sdiminline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr> 528218893Sdimm_Shr(const LHS &L, const RHS &R) { 529218893Sdim return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R); 530193323Sed} 531193323Sed 532218893Sdim/// m_LogicalShift - Matches LShr or Shl. 533193323Sedtemplate<typename LHS, typename RHS> 534218893Sdiminline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl> 535218893Sdimm_LogicalShift(const LHS &L, const RHS &R) { 536218893Sdim return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R); 537193323Sed} 538193323Sed 539218893Sdim/// m_IDiv - Matches UDiv and SDiv. 540193323Sedtemplate<typename LHS, typename RHS> 541218893Sdiminline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv> 542218893Sdimm_IDiv(const LHS &L, const RHS &R) { 543218893Sdim return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R); 544193323Sed} 545193323Sed 546193323Sed//===----------------------------------------------------------------------===// 547235633Sdim// Class that matches exact binary ops. 548235633Sdim// 549235633Sdimtemplate<typename SubPattern_t> 550235633Sdimstruct Exact_match { 551235633Sdim SubPattern_t SubPattern; 552235633Sdim 553235633Sdim Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} 554235633Sdim 555235633Sdim template<typename OpTy> 556235633Sdim bool match(OpTy *V) { 557235633Sdim if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V)) 558235633Sdim return PEO->isExact() && SubPattern.match(V); 559235633Sdim return false; 560235633Sdim } 561235633Sdim}; 562235633Sdim 563235633Sdimtemplate<typename T> 564235633Sdiminline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; } 565235633Sdim 566235633Sdim//===----------------------------------------------------------------------===// 567193323Sed// Matchers for CmpInst classes 568193323Sed// 569193323Sed 570193323Sedtemplate<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy> 571193323Sedstruct CmpClass_match { 572193323Sed PredicateTy &Predicate; 573193323Sed LHS_t L; 574193323Sed RHS_t R; 575193323Sed 576218893Sdim CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS) 577193323Sed : Predicate(Pred), L(LHS), R(RHS) {} 578193323Sed 579193323Sed template<typename OpTy> 580193323Sed bool match(OpTy *V) { 581193323Sed if (Class *I = dyn_cast<Class>(V)) 582218893Sdim if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { 583193323Sed Predicate = I->getPredicate(); 584193323Sed return true; 585193323Sed } 586193323Sed return false; 587193323Sed } 588193323Sed}; 589193323Sed 590193323Sedtemplate<typename LHS, typename RHS> 591193323Sedinline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate> 592193323Sedm_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 593193323Sed return CmpClass_match<LHS, RHS, 594193323Sed ICmpInst, ICmpInst::Predicate>(Pred, L, R); 595193323Sed} 596193323Sed 597193323Sedtemplate<typename LHS, typename RHS> 598193323Sedinline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate> 599193323Sedm_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 600193323Sed return CmpClass_match<LHS, RHS, 601193323Sed FCmpInst, FCmpInst::Predicate>(Pred, L, R); 602193323Sed} 603193323Sed 604193323Sed//===----------------------------------------------------------------------===// 605193323Sed// Matchers for SelectInst classes 606193323Sed// 607193323Sed 608193323Sedtemplate<typename Cond_t, typename LHS_t, typename RHS_t> 609193323Sedstruct SelectClass_match { 610193323Sed Cond_t C; 611193323Sed LHS_t L; 612193323Sed RHS_t R; 613193323Sed 614193323Sed SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, 615193323Sed const RHS_t &RHS) 616193323Sed : C(Cond), L(LHS), R(RHS) {} 617193323Sed 618193323Sed template<typename OpTy> 619193323Sed bool match(OpTy *V) { 620193323Sed if (SelectInst *I = dyn_cast<SelectInst>(V)) 621193323Sed return C.match(I->getOperand(0)) && 622193323Sed L.match(I->getOperand(1)) && 623193323Sed R.match(I->getOperand(2)); 624193323Sed return false; 625193323Sed } 626193323Sed}; 627193323Sed 628193323Sedtemplate<typename Cond, typename LHS, typename RHS> 629198090Srdivackyinline SelectClass_match<Cond, LHS, RHS> 630193323Sedm_Select(const Cond &C, const LHS &L, const RHS &R) { 631193323Sed return SelectClass_match<Cond, LHS, RHS>(C, L, R); 632193323Sed} 633193323Sed 634193323Sed/// m_SelectCst - This matches a select of two constants, e.g.: 635203954Srdivacky/// m_SelectCst<-1, 0>(m_Value(V)) 636193323Sedtemplate<int64_t L, int64_t R, typename Cond> 637218893Sdiminline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> > 638193323Sedm_SelectCst(const Cond &C) { 639218893Sdim return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); 640193323Sed} 641193323Sed 642193323Sed 643193323Sed//===----------------------------------------------------------------------===// 644193323Sed// Matchers for CastInst classes 645193323Sed// 646193323Sed 647202375Srdivackytemplate<typename Op_t, unsigned Opcode> 648193323Sedstruct CastClass_match { 649193323Sed Op_t Op; 650193323Sed 651193323Sed CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} 652193323Sed 653193323Sed template<typename OpTy> 654193323Sed bool match(OpTy *V) { 655235633Sdim if (Operator *O = dyn_cast<Operator>(V)) 656235633Sdim return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); 657193323Sed return false; 658193323Sed } 659193323Sed}; 660193323Sed 661212904Sdim/// m_BitCast 662212904Sdimtemplate<typename OpTy> 663212904Sdiminline CastClass_match<OpTy, Instruction::BitCast> 664212904Sdimm_BitCast(const OpTy &Op) { 665212904Sdim return CastClass_match<OpTy, Instruction::BitCast>(Op); 666212904Sdim} 667252723Sdim 668202375Srdivacky/// m_PtrToInt 669202375Srdivackytemplate<typename OpTy> 670202375Srdivackyinline CastClass_match<OpTy, Instruction::PtrToInt> 671202375Srdivackym_PtrToInt(const OpTy &Op) { 672202375Srdivacky return CastClass_match<OpTy, Instruction::PtrToInt>(Op); 673193323Sed} 674193323Sed 675202375Srdivacky/// m_Trunc 676202375Srdivackytemplate<typename OpTy> 677202375Srdivackyinline CastClass_match<OpTy, Instruction::Trunc> 678202375Srdivackym_Trunc(const OpTy &Op) { 679202375Srdivacky return CastClass_match<OpTy, Instruction::Trunc>(Op); 680202375Srdivacky} 681203954Srdivacky 682203954Srdivacky/// m_SExt 683203954Srdivackytemplate<typename OpTy> 684203954Srdivackyinline CastClass_match<OpTy, Instruction::SExt> 685203954Srdivackym_SExt(const OpTy &Op) { 686203954Srdivacky return CastClass_match<OpTy, Instruction::SExt>(Op); 687203954Srdivacky} 688203954Srdivacky 689203954Srdivacky/// m_ZExt 690203954Srdivackytemplate<typename OpTy> 691203954Srdivackyinline CastClass_match<OpTy, Instruction::ZExt> 692203954Srdivackym_ZExt(const OpTy &Op) { 693203954Srdivacky return CastClass_match<OpTy, Instruction::ZExt>(Op); 694203954Srdivacky} 695193323Sed 696252723Sdim/// m_UIToFP 697252723Sdimtemplate<typename OpTy> 698252723Sdiminline CastClass_match<OpTy, Instruction::UIToFP> 699263509Sdimm_UIToFP(const OpTy &Op) { 700252723Sdim return CastClass_match<OpTy, Instruction::UIToFP>(Op); 701252723Sdim} 702252723Sdim 703263509Sdim/// m_SIToFP 704263509Sdimtemplate<typename OpTy> 705263509Sdiminline CastClass_match<OpTy, Instruction::SIToFP> 706263509Sdimm_SIToFP(const OpTy &Op) { 707263509Sdim return CastClass_match<OpTy, Instruction::SIToFP>(Op); 708263509Sdim} 709263509Sdim 710193323Sed//===----------------------------------------------------------------------===// 711193323Sed// Matchers for unary operators 712193323Sed// 713193323Sed 714193323Sedtemplate<typename LHS_t> 715193323Sedstruct not_match { 716193323Sed LHS_t L; 717193323Sed 718193323Sed not_match(const LHS_t &LHS) : L(LHS) {} 719193323Sed 720193323Sed template<typename OpTy> 721193323Sed bool match(OpTy *V) { 722235633Sdim if (Operator *O = dyn_cast<Operator>(V)) 723235633Sdim if (O->getOpcode() == Instruction::Xor) 724235633Sdim return matchIfNot(O->getOperand(0), O->getOperand(1)); 725193323Sed return false; 726193323Sed } 727193323Sedprivate: 728193323Sed bool matchIfNot(Value *LHS, Value *RHS) { 729235633Sdim return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) || 730235633Sdim // FIXME: Remove CV. 731235633Sdim isa<ConstantVector>(RHS)) && 732235633Sdim cast<Constant>(RHS)->isAllOnesValue() && 733235633Sdim L.match(LHS); 734193323Sed } 735193323Sed}; 736193323Sed 737193323Sedtemplate<typename LHS> 738193323Sedinline not_match<LHS> m_Not(const LHS &L) { return L; } 739193323Sed 740193323Sed 741193323Sedtemplate<typename LHS_t> 742193323Sedstruct neg_match { 743193323Sed LHS_t L; 744193323Sed 745193323Sed neg_match(const LHS_t &LHS) : L(LHS) {} 746193323Sed 747193323Sed template<typename OpTy> 748193323Sed bool match(OpTy *V) { 749235633Sdim if (Operator *O = dyn_cast<Operator>(V)) 750235633Sdim if (O->getOpcode() == Instruction::Sub) 751235633Sdim return matchIfNeg(O->getOperand(0), O->getOperand(1)); 752193323Sed return false; 753193323Sed } 754193323Sedprivate: 755193323Sed bool matchIfNeg(Value *LHS, Value *RHS) { 756235633Sdim return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) || 757235633Sdim isa<ConstantAggregateZero>(LHS)) && 758235633Sdim L.match(RHS); 759193323Sed } 760193323Sed}; 761193323Sed 762218893Sdim/// m_Neg - Match an integer negate. 763193323Sedtemplate<typename LHS> 764193323Sedinline neg_match<LHS> m_Neg(const LHS &L) { return L; } 765193323Sed 766193323Sed 767193574Sedtemplate<typename LHS_t> 768193574Sedstruct fneg_match { 769193574Sed LHS_t L; 770193574Sed 771193574Sed fneg_match(const LHS_t &LHS) : L(LHS) {} 772193574Sed 773193574Sed template<typename OpTy> 774193574Sed bool match(OpTy *V) { 775235633Sdim if (Operator *O = dyn_cast<Operator>(V)) 776235633Sdim if (O->getOpcode() == Instruction::FSub) 777235633Sdim return matchIfFNeg(O->getOperand(0), O->getOperand(1)); 778193574Sed return false; 779193574Sed } 780193574Sedprivate: 781193574Sed bool matchIfFNeg(Value *LHS, Value *RHS) { 782218893Sdim if (ConstantFP *C = dyn_cast<ConstantFP>(LHS)) 783218893Sdim return C->isNegativeZeroValue() && L.match(RHS); 784218893Sdim return false; 785193574Sed } 786193574Sed}; 787193574Sed 788218893Sdim/// m_FNeg - Match a floating point negate. 789193574Sedtemplate<typename LHS> 790193574Sedinline fneg_match<LHS> m_FNeg(const LHS &L) { return L; } 791193574Sed 792193574Sed 793193323Sed//===----------------------------------------------------------------------===// 794218893Sdim// Matchers for control flow. 795193323Sed// 796193323Sed 797252723Sdimstruct br_match { 798252723Sdim BasicBlock *&Succ; 799252723Sdim br_match(BasicBlock *&Succ) 800252723Sdim : Succ(Succ) { 801252723Sdim } 802252723Sdim 803252723Sdim template<typename OpTy> 804252723Sdim bool match(OpTy *V) { 805252723Sdim if (BranchInst *BI = dyn_cast<BranchInst>(V)) 806252723Sdim if (BI->isUnconditional()) { 807252723Sdim Succ = BI->getSuccessor(0); 808252723Sdim return true; 809252723Sdim } 810252723Sdim return false; 811252723Sdim } 812252723Sdim}; 813252723Sdim 814252723Sdiminline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } 815252723Sdim 816193323Sedtemplate<typename Cond_t> 817193323Sedstruct brc_match { 818193323Sed Cond_t Cond; 819193323Sed BasicBlock *&T, *&F; 820193323Sed brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f) 821193323Sed : Cond(C), T(t), F(f) { 822193323Sed } 823193323Sed 824193323Sed template<typename OpTy> 825193323Sed bool match(OpTy *V) { 826193323Sed if (BranchInst *BI = dyn_cast<BranchInst>(V)) 827218893Sdim if (BI->isConditional() && Cond.match(BI->getCondition())) { 828218893Sdim T = BI->getSuccessor(0); 829218893Sdim F = BI->getSuccessor(1); 830218893Sdim return true; 831193323Sed } 832193323Sed return false; 833193323Sed } 834193323Sed}; 835193323Sed 836193323Sedtemplate<typename Cond_t> 837193323Sedinline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { 838193323Sed return brc_match<Cond_t>(C, T, F); 839193323Sed} 840193323Sed 841223017Sdim 842223017Sdim//===----------------------------------------------------------------------===// 843223017Sdim// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). 844223017Sdim// 845223017Sdim 846252723Sdimtemplate<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t> 847223017Sdimstruct MaxMin_match { 848223017Sdim LHS_t L; 849223017Sdim RHS_t R; 850223017Sdim 851223017Sdim MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) 852223017Sdim : L(LHS), R(RHS) {} 853223017Sdim 854223017Sdim template<typename OpTy> 855223017Sdim bool match(OpTy *V) { 856223017Sdim // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". 857223017Sdim SelectInst *SI = dyn_cast<SelectInst>(V); 858223017Sdim if (!SI) 859223017Sdim return false; 860252723Sdim CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); 861223017Sdim if (!Cmp) 862223017Sdim return false; 863223017Sdim // At this point we have a select conditioned on a comparison. Check that 864223017Sdim // it is the values returned by the select that are being compared. 865223017Sdim Value *TrueVal = SI->getTrueValue(); 866223017Sdim Value *FalseVal = SI->getFalseValue(); 867223017Sdim Value *LHS = Cmp->getOperand(0); 868223017Sdim Value *RHS = Cmp->getOperand(1); 869223017Sdim if ((TrueVal != LHS || FalseVal != RHS) && 870223017Sdim (TrueVal != RHS || FalseVal != LHS)) 871223017Sdim return false; 872252723Sdim typename CmpInst_t::Predicate Pred = LHS == TrueVal ? 873223017Sdim Cmp->getPredicate() : Cmp->getSwappedPredicate(); 874223017Sdim // Does "(x pred y) ? x : y" represent the desired max/min operation? 875223017Sdim if (!Pred_t::match(Pred)) 876223017Sdim return false; 877223017Sdim // It does! Bind the operands. 878223017Sdim return L.match(LHS) && R.match(RHS); 879223017Sdim } 880223017Sdim}; 881223017Sdim 882223017Sdim/// smax_pred_ty - Helper class for identifying signed max predicates. 883223017Sdimstruct smax_pred_ty { 884223017Sdim static bool match(ICmpInst::Predicate Pred) { 885223017Sdim return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; 886223017Sdim } 887223017Sdim}; 888223017Sdim 889223017Sdim/// smin_pred_ty - Helper class for identifying signed min predicates. 890223017Sdimstruct smin_pred_ty { 891223017Sdim static bool match(ICmpInst::Predicate Pred) { 892223017Sdim return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; 893223017Sdim } 894223017Sdim}; 895223017Sdim 896223017Sdim/// umax_pred_ty - Helper class for identifying unsigned max predicates. 897223017Sdimstruct umax_pred_ty { 898223017Sdim static bool match(ICmpInst::Predicate Pred) { 899223017Sdim return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; 900223017Sdim } 901223017Sdim}; 902223017Sdim 903223017Sdim/// umin_pred_ty - Helper class for identifying unsigned min predicates. 904223017Sdimstruct umin_pred_ty { 905223017Sdim static bool match(ICmpInst::Predicate Pred) { 906223017Sdim return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; 907223017Sdim } 908223017Sdim}; 909223017Sdim 910252723Sdim/// ofmax_pred_ty - Helper class for identifying ordered max predicates. 911252723Sdimstruct ofmax_pred_ty { 912252723Sdim static bool match(FCmpInst::Predicate Pred) { 913252723Sdim return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; 914252723Sdim } 915252723Sdim}; 916252723Sdim 917252723Sdim/// ofmin_pred_ty - Helper class for identifying ordered min predicates. 918252723Sdimstruct ofmin_pred_ty { 919252723Sdim static bool match(FCmpInst::Predicate Pred) { 920252723Sdim return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; 921252723Sdim } 922252723Sdim}; 923252723Sdim 924252723Sdim/// ufmax_pred_ty - Helper class for identifying unordered max predicates. 925252723Sdimstruct ufmax_pred_ty { 926252723Sdim static bool match(FCmpInst::Predicate Pred) { 927252723Sdim return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; 928252723Sdim } 929252723Sdim}; 930252723Sdim 931252723Sdim/// ufmin_pred_ty - Helper class for identifying unordered min predicates. 932252723Sdimstruct ufmin_pred_ty { 933252723Sdim static bool match(FCmpInst::Predicate Pred) { 934252723Sdim return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; 935252723Sdim } 936252723Sdim}; 937252723Sdim 938223017Sdimtemplate<typename LHS, typename RHS> 939252723Sdiminline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> 940223017Sdimm_SMax(const LHS &L, const RHS &R) { 941252723Sdim return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); 942223017Sdim} 943223017Sdim 944223017Sdimtemplate<typename LHS, typename RHS> 945252723Sdiminline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> 946223017Sdimm_SMin(const LHS &L, const RHS &R) { 947252723Sdim return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); 948223017Sdim} 949223017Sdim 950223017Sdimtemplate<typename LHS, typename RHS> 951252723Sdiminline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> 952223017Sdimm_UMax(const LHS &L, const RHS &R) { 953252723Sdim return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); 954223017Sdim} 955223017Sdim 956223017Sdimtemplate<typename LHS, typename RHS> 957252723Sdiminline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> 958223017Sdimm_UMin(const LHS &L, const RHS &R) { 959252723Sdim return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); 960223017Sdim} 961223017Sdim 962252723Sdim/// \brief Match an 'ordered' floating point maximum function. 963252723Sdim/// Floating point has one special value 'NaN'. Therefore, there is no total 964252723Sdim/// order. However, if we can ignore the 'NaN' value (for example, because of a 965252723Sdim/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 966252723Sdim/// semantics. In the presence of 'NaN' we have to preserve the original 967252723Sdim/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. 968252723Sdim/// 969252723Sdim/// max(L, R) iff L and R are not NaN 970252723Sdim/// m_OrdFMax(L, R) = R iff L or R are NaN 971252723Sdimtemplate<typename LHS, typename RHS> 972252723Sdiminline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> 973252723Sdimm_OrdFMax(const LHS &L, const RHS &R) { 974252723Sdim return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); 975252723Sdim} 976252723Sdim 977252723Sdim/// \brief Match an 'ordered' floating point minimum function. 978252723Sdim/// Floating point has one special value 'NaN'. Therefore, there is no total 979252723Sdim/// order. However, if we can ignore the 'NaN' value (for example, because of a 980252723Sdim/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 981252723Sdim/// semantics. In the presence of 'NaN' we have to preserve the original 982252723Sdim/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. 983252723Sdim/// 984252723Sdim/// max(L, R) iff L and R are not NaN 985252723Sdim/// m_OrdFMin(L, R) = R iff L or R are NaN 986252723Sdimtemplate<typename LHS, typename RHS> 987252723Sdiminline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> 988252723Sdimm_OrdFMin(const LHS &L, const RHS &R) { 989252723Sdim return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); 990252723Sdim} 991252723Sdim 992252723Sdim/// \brief Match an 'unordered' floating point maximum function. 993252723Sdim/// Floating point has one special value 'NaN'. Therefore, there is no total 994252723Sdim/// order. However, if we can ignore the 'NaN' value (for example, because of a 995252723Sdim/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 996252723Sdim/// semantics. In the presence of 'NaN' we have to preserve the original 997252723Sdim/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. 998252723Sdim/// 999252723Sdim/// max(L, R) iff L and R are not NaN 1000252723Sdim/// m_UnordFMin(L, R) = L iff L or R are NaN 1001252723Sdimtemplate<typename LHS, typename RHS> 1002252723Sdiminline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> 1003252723Sdimm_UnordFMax(const LHS &L, const RHS &R) { 1004252723Sdim return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); 1005252723Sdim} 1006252723Sdim 1007252723Sdim/// \brief Match an 'unordered' floating point minimum function. 1008252723Sdim/// Floating point has one special value 'NaN'. Therefore, there is no total 1009252723Sdim/// order. However, if we can ignore the 'NaN' value (for example, because of a 1010252723Sdim/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 1011252723Sdim/// semantics. In the presence of 'NaN' we have to preserve the original 1012252723Sdim/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. 1013252723Sdim/// 1014252723Sdim/// max(L, R) iff L and R are not NaN 1015252723Sdim/// m_UnordFMin(L, R) = L iff L or R are NaN 1016252723Sdimtemplate<typename LHS, typename RHS> 1017252723Sdiminline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> 1018252723Sdimm_UnordFMin(const LHS &L, const RHS &R) { 1019252723Sdim return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); 1020252723Sdim} 1021252723Sdim 1022252723Sdimtemplate<typename Opnd_t> 1023252723Sdimstruct Argument_match { 1024252723Sdim unsigned OpI; 1025252723Sdim Opnd_t Val; 1026252723Sdim Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { } 1027252723Sdim 1028252723Sdim template<typename OpTy> 1029252723Sdim bool match(OpTy *V) { 1030252723Sdim CallSite CS(V); 1031252723Sdim return CS.isCall() && Val.match(CS.getArgument(OpI)); 1032252723Sdim } 1033252723Sdim}; 1034252723Sdim 1035252723Sdim/// Match an argument 1036252723Sdimtemplate<unsigned OpI, typename Opnd_t> 1037252723Sdiminline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { 1038252723Sdim return Argument_match<Opnd_t>(OpI, Op); 1039252723Sdim} 1040252723Sdim 1041252723Sdim/// Intrinsic matchers. 1042252723Sdimstruct IntrinsicID_match { 1043252723Sdim unsigned ID; 1044263509Sdim IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) { } 1045252723Sdim 1046252723Sdim template<typename OpTy> 1047252723Sdim bool match(OpTy *V) { 1048252723Sdim IntrinsicInst *II = dyn_cast<IntrinsicInst>(V); 1049252723Sdim return II && II->getIntrinsicID() == ID; 1050252723Sdim } 1051252723Sdim}; 1052252723Sdim 1053252723Sdim/// Intrinsic matches are combinations of ID matchers, and argument 1054252723Sdim/// matchers. Higher arity matcher are defined recursively in terms of and-ing 1055252723Sdim/// them with lower arity matchers. Here's some convenient typedefs for up to 1056252723Sdim/// several arguments, and more can be added as needed 1057252723Sdimtemplate <typename T0 = void, typename T1 = void, typename T2 = void, 1058252723Sdim typename T3 = void, typename T4 = void, typename T5 = void, 1059252723Sdim typename T6 = void, typename T7 = void, typename T8 = void, 1060252723Sdim typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty; 1061252723Sdimtemplate <typename T0> 1062252723Sdimstruct m_Intrinsic_Ty<T0> { 1063252723Sdim typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty; 1064252723Sdim}; 1065252723Sdimtemplate <typename T0, typename T1> 1066252723Sdimstruct m_Intrinsic_Ty<T0, T1> { 1067252723Sdim typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, 1068252723Sdim Argument_match<T1> > Ty; 1069252723Sdim}; 1070252723Sdimtemplate <typename T0, typename T1, typename T2> 1071252723Sdimstruct m_Intrinsic_Ty<T0, T1, T2> { 1072252723Sdim typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, 1073252723Sdim Argument_match<T2> > Ty; 1074252723Sdim}; 1075252723Sdimtemplate <typename T0, typename T1, typename T2, typename T3> 1076252723Sdimstruct m_Intrinsic_Ty<T0, T1, T2, T3> { 1077252723Sdim typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, 1078252723Sdim Argument_match<T3> > Ty; 1079252723Sdim}; 1080252723Sdim 1081252723Sdim/// Match intrinsic calls like this: 1082252723Sdim/// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) 1083263509Sdimtemplate <Intrinsic::ID IntrID> 1084252723Sdiminline IntrinsicID_match 1085252723Sdimm_Intrinsic() { return IntrinsicID_match(IntrID); } 1086252723Sdim 1087263509Sdimtemplate<Intrinsic::ID IntrID, typename T0> 1088252723Sdiminline typename m_Intrinsic_Ty<T0>::Ty 1089252723Sdimm_Intrinsic(const T0 &Op0) { 1090252723Sdim return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); 1091252723Sdim} 1092252723Sdim 1093263509Sdimtemplate<Intrinsic::ID IntrID, typename T0, typename T1> 1094252723Sdiminline typename m_Intrinsic_Ty<T0, T1>::Ty 1095252723Sdimm_Intrinsic(const T0 &Op0, const T1 &Op1) { 1096252723Sdim return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); 1097252723Sdim} 1098252723Sdim 1099263509Sdimtemplate<Intrinsic::ID IntrID, typename T0, typename T1, typename T2> 1100252723Sdiminline typename m_Intrinsic_Ty<T0, T1, T2>::Ty 1101252723Sdimm_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { 1102252723Sdim return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); 1103252723Sdim} 1104252723Sdim 1105263509Sdimtemplate<Intrinsic::ID IntrID, typename T0, typename T1, typename T2, typename T3> 1106252723Sdiminline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty 1107252723Sdimm_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { 1108252723Sdim return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); 1109252723Sdim} 1110252723Sdim 1111252723Sdim// Helper intrinsic matching specializations 1112252723Sdimtemplate<typename Opnd0> 1113252723Sdiminline typename m_Intrinsic_Ty<Opnd0>::Ty 1114252723Sdimm_BSwap(const Opnd0 &Op0) { 1115252723Sdim return m_Intrinsic<Intrinsic::bswap>(Op0); 1116252723Sdim} 1117252723Sdim 1118193323Sed} // end namespace PatternMatch 1119193323Sed} // end namespace llvm 1120193323Sed 1121193323Sed#endif 1122