Add more pattern matchers for compares, instructions, and BinaryOperators. NFC.
[oota-llvm.git] / include / llvm / IR / PatternMatch.h
1 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides a simple and efficient mechanism for performing general
11 // tree-based pattern matches on the LLVM IR.  The power of these routines is
12 // that it allows you to write concise patterns that are expressive and easy to
13 // understand.  The other major advantage of this is that it allows you to
14 // trivially capture/bind elements in the pattern to variables.  For example,
15 // you can do something like this:
16 //
17 //  Value *Exp = ...
18 //  Value *X, *Y;  ConstantInt *C1, *C2;      // (X & C1) | (Y & C2)
19 //  if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
20 //                      m_And(m_Value(Y), m_ConstantInt(C2))))) {
21 //    ... Pattern is matched and variables are bound ...
22 //  }
23 //
24 // This is primarily useful to things like the instruction combiner, but can
25 // also be useful for static analysis tools or code generators.
26 //
27 //===----------------------------------------------------------------------===//
28
29 #ifndef LLVM_IR_PATTERNMATCH_H
30 #define LLVM_IR_PATTERNMATCH_H
31
32 #include "llvm/IR/CallSite.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/Operator.h"
37
38 namespace llvm {
39 namespace PatternMatch {
40
41 template<typename Val, typename Pattern>
42 bool match(Val *V, const Pattern &P) {
43   return const_cast<Pattern&>(P).match(V);
44 }
45
46
47 template<typename SubPattern_t>
48 struct OneUse_match {
49   SubPattern_t SubPattern;
50
51   OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
52
53   template<typename OpTy>
54   bool match(OpTy *V) {
55     return V->hasOneUse() && SubPattern.match(V);
56   }
57 };
58
59 template<typename T>
60 inline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; }
61
62
63 template<typename Class>
64 struct class_match {
65   template<typename ITy>
66   bool match(ITy *V) { return isa<Class>(V); }
67 };
68
69 /// m_Value() - Match an arbitrary value and ignore it.
70 inline class_match<Value> m_Value() { return class_match<Value>(); }
71 /// m_Instruction() - Match an arbitrary instruction and ignore it.
72 inline class_match<Instruction> m_Instruction() {
73   return class_match<Instruction>();
74 }
75 /// m_BinOp() - Match an arbitrary binary operation and ignore it.
76 inline class_match<BinaryOperator> m_BinOp() {
77   return class_match<BinaryOperator>();
78 }
79 /// m_Cmp() - Matches any compare instruction and ignore it.
80 inline class_match<CmpInst> m_Cmp() {
81   return class_match<CmpInst>();
82 }
83 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
84 inline class_match<ConstantInt> m_ConstantInt() {
85   return class_match<ConstantInt>();
86 }
87 /// m_Undef() - Match an arbitrary undef constant.
88 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
89
90 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
91
92 /// Matching combinators
93 template<typename LTy, typename RTy>
94 struct match_combine_or {
95   LTy L;
96   RTy R;
97
98   match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
99
100   template<typename ITy>
101   bool match(ITy *V) {
102     if (L.match(V))
103       return true;
104     if (R.match(V))
105       return true;
106     return false;
107   }
108 };
109
110 template<typename LTy, typename RTy>
111 struct match_combine_and {
112   LTy L;
113   RTy R;
114
115   match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
116
117   template<typename ITy>
118   bool match(ITy *V) {
119     if (L.match(V))
120       if (R.match(V))
121         return true;
122     return false;
123   }
124 };
125
126 /// Combine two pattern matchers matching L || R
127 template<typename LTy, typename RTy>
128 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
129   return match_combine_or<LTy, RTy>(L, R);
130 }
131
132 /// Combine two pattern matchers matching L && R
133 template<typename LTy, typename RTy>
134 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
135   return match_combine_and<LTy, RTy>(L, R);
136 }
137
138 struct match_zero {
139   template<typename ITy>
140   bool match(ITy *V) {
141     if (const Constant *C = dyn_cast<Constant>(V))
142       return C->isNullValue();
143     return false;
144   }
145 };
146
147 /// m_Zero() - Match an arbitrary zero/null constant.  This includes
148 /// zero_initializer for vectors and ConstantPointerNull for pointers.
149 inline match_zero m_Zero() { return match_zero(); }
150
151 struct match_neg_zero {
152   template<typename ITy>
153   bool match(ITy *V) {
154     if (const Constant *C = dyn_cast<Constant>(V))
155       return C->isNegativeZeroValue();
156     return false;
157   }
158 };
159
160 /// m_NegZero() - Match an arbitrary zero/null constant.  This includes
161 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
162 /// floating point constants, this will match negative zero but not positive
163 /// zero
164 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
165
166 /// m_AnyZero() - Match an arbitrary zero/null constant.  This includes
167 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
168 /// floating point constants, this will match negative zero and positive zero
169 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
170   return m_CombineOr(m_Zero(), m_NegZero());
171 }
172
173 struct apint_match {
174   const APInt *&Res;
175   apint_match(const APInt *&R) : Res(R) {}
176   template<typename ITy>
177   bool match(ITy *V) {
178     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
179       Res = &CI->getValue();
180       return true;
181     }
182     if (V->getType()->isVectorTy())
183       if (const Constant *C = dyn_cast<Constant>(V))
184         if (ConstantInt *CI =
185             dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
186           Res = &CI->getValue();
187           return true;
188         }
189     return false;
190   }
191 };
192
193 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
194 /// specified pointer to the contained APInt.
195 inline apint_match m_APInt(const APInt *&Res) { return Res; }
196
197
198 template<int64_t Val>
199 struct constantint_match {
200   template<typename ITy>
201   bool match(ITy *V) {
202     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
203       const APInt &CIV = CI->getValue();
204       if (Val >= 0)
205         return CIV == static_cast<uint64_t>(Val);
206       // If Val is negative, and CI is shorter than it, truncate to the right
207       // number of bits.  If it is larger, then we have to sign extend.  Just
208       // compare their negated values.
209       return -CIV == -Val;
210     }
211     return false;
212   }
213 };
214
215 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
216 template<int64_t Val>
217 inline constantint_match<Val> m_ConstantInt() {
218   return constantint_match<Val>();
219 }
220
221 /// cst_pred_ty - This helper class is used to match scalar and vector constants
222 /// that satisfy a specified predicate.
223 template<typename Predicate>
224 struct cst_pred_ty : public Predicate {
225   template<typename ITy>
226   bool match(ITy *V) {
227     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
228       return this->isValue(CI->getValue());
229     if (V->getType()->isVectorTy())
230       if (const Constant *C = dyn_cast<Constant>(V))
231         if (const ConstantInt *CI =
232             dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
233           return this->isValue(CI->getValue());
234     return false;
235   }
236 };
237
238 /// api_pred_ty - This helper class is used to match scalar and vector constants
239 /// that satisfy a specified predicate, and bind them to an APInt.
240 template<typename Predicate>
241 struct api_pred_ty : public Predicate {
242   const APInt *&Res;
243   api_pred_ty(const APInt *&R) : Res(R) {}
244   template<typename ITy>
245   bool match(ITy *V) {
246     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
247       if (this->isValue(CI->getValue())) {
248         Res = &CI->getValue();
249         return true;
250       }
251     if (V->getType()->isVectorTy())
252       if (const Constant *C = dyn_cast<Constant>(V))
253         if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
254           if (this->isValue(CI->getValue())) {
255             Res = &CI->getValue();
256             return true;
257           }
258
259     return false;
260   }
261 };
262
263
264 struct is_one {
265   bool isValue(const APInt &C) { return C == 1; }
266 };
267
268 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
269 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
270 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
271
272 struct is_all_ones {
273   bool isValue(const APInt &C) { return C.isAllOnesValue(); }
274 };
275
276 /// m_AllOnes() - Match an integer or vector with all bits set to true.
277 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
278 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
279
280 struct is_sign_bit {
281   bool isValue(const APInt &C) { return C.isSignBit(); }
282 };
283
284 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
285 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
286 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
287
288 struct is_power2 {
289   bool isValue(const APInt &C) { return C.isPowerOf2(); }
290 };
291
292 /// m_Power2() - Match an integer or vector power of 2.
293 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
294 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
295
296 template<typename Class>
297 struct bind_ty {
298   Class *&VR;
299   bind_ty(Class *&V) : VR(V) {}
300
301   template<typename ITy>
302   bool match(ITy *V) {
303     if (Class *CV = dyn_cast<Class>(V)) {
304       VR = CV;
305       return true;
306     }
307     return false;
308   }
309 };
310
311 /// m_Value - Match a value, capturing it if we match.
312 inline bind_ty<Value> m_Value(Value *&V) { return V; }
313
314 /// m_Instruction - Match a instruction, capturing it if we match.
315 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
316
317 /// m_BinOp - Match a instruction, capturing it if we match.
318 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
319
320 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
321 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
322
323 /// m_Constant - Match a Constant, capturing the value if we match.
324 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
325
326 /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
327 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
328
329 /// specificval_ty - Match a specified Value*.
330 struct specificval_ty {
331   const Value *Val;
332   specificval_ty(const Value *V) : Val(V) {}
333
334   template<typename ITy>
335   bool match(ITy *V) {
336     return V == Val;
337   }
338 };
339
340 /// m_Specific - Match if we have a specific specified value.
341 inline specificval_ty m_Specific(const Value *V) { return V; }
342
343 /// Match a specified floating point value or vector of all elements of that
344 /// value.
345 struct specific_fpval {
346   double Val;
347   specific_fpval(double V) : Val(V) {}
348
349   template<typename ITy>
350   bool match(ITy *V) {
351     if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
352       return CFP->isExactlyValue(Val);
353     if (V->getType()->isVectorTy())
354       if (const Constant *C = dyn_cast<Constant>(V))
355         if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
356           return CFP->isExactlyValue(Val);
357     return false;
358   }
359 };
360
361 /// Match a specific floating point value or vector with all elements equal to
362 /// the value.
363 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
364
365 /// Match a float 1.0 or vector with all elements equal to 1.0.
366 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
367
368 struct bind_const_intval_ty {
369   uint64_t &VR;
370   bind_const_intval_ty(uint64_t &V) : VR(V) {}
371
372   template<typename ITy>
373   bool match(ITy *V) {
374     if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
375       if (CV->getBitWidth() <= 64) {
376         VR = CV->getZExtValue();
377         return true;
378       }
379     return false;
380   }
381 };
382
383 /// Match a specified integer value or vector of all elements of that value.
384 struct specific_intval {
385   uint64_t Val;
386   specific_intval(uint64_t V) : Val(V) {}
387
388   template<typename ITy>
389   bool match(ITy *V) {
390     ConstantInt *CI = dyn_cast<ConstantInt>(V);
391     if (!CI && V->getType()->isVectorTy())
392       if (const auto *C = dyn_cast<Constant>(V))
393         CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
394
395     if (CI && CI->getBitWidth() <= 64)
396       return CI->getZExtValue() == Val;
397
398     return false;
399   }
400 };
401
402 /// Match a specific integer value or vector with all elements equal to the
403 /// value.
404 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
405
406 /// m_ConstantInt - Match a ConstantInt and bind to its value.  This does not
407 /// match ConstantInts wider than 64-bits.
408 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
409
410 //===----------------------------------------------------------------------===//
411 // Matcher for any binary operator.
412 //
413 template<typename LHS_t, typename RHS_t>
414 struct AnyBinaryOp_match {
415   LHS_t L;
416   RHS_t R;
417
418   AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
419
420   template<typename OpTy>
421   bool match(OpTy *V) {
422     if (auto *I = dyn_cast<BinaryOperator>(V))
423       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
424     return false;
425   }
426 };
427
428 template<typename LHS, typename RHS>
429 inline AnyBinaryOp_match<LHS, RHS>
430 m_BinOp(const LHS &L, const RHS &R) {
431   return AnyBinaryOp_match<LHS, RHS>(L, R);
432 }
433
434 //===----------------------------------------------------------------------===//
435 // Matchers for specific binary operators.
436 //
437
438 template<typename LHS_t, typename RHS_t, unsigned Opcode>
439 struct BinaryOp_match {
440   LHS_t L;
441   RHS_t R;
442
443   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
444
445   template<typename OpTy>
446   bool match(OpTy *V) {
447     if (V->getValueID() == Value::InstructionVal + Opcode) {
448       BinaryOperator *I = cast<BinaryOperator>(V);
449       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
450     }
451     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
452       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
453              R.match(CE->getOperand(1));
454     return false;
455   }
456 };
457
458 template<typename LHS, typename RHS>
459 inline BinaryOp_match<LHS, RHS, Instruction::Add>
460 m_Add(const LHS &L, const RHS &R) {
461   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
462 }
463
464 template<typename LHS, typename RHS>
465 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
466 m_FAdd(const LHS &L, const RHS &R) {
467   return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
468 }
469
470 template<typename LHS, typename RHS>
471 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
472 m_Sub(const LHS &L, const RHS &R) {
473   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
474 }
475
476 template<typename LHS, typename RHS>
477 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
478 m_FSub(const LHS &L, const RHS &R) {
479   return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
480 }
481
482 template<typename LHS, typename RHS>
483 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
484 m_Mul(const LHS &L, const RHS &R) {
485   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
486 }
487
488 template<typename LHS, typename RHS>
489 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
490 m_FMul(const LHS &L, const RHS &R) {
491   return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
492 }
493
494 template<typename LHS, typename RHS>
495 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
496 m_UDiv(const LHS &L, const RHS &R) {
497   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
498 }
499
500 template<typename LHS, typename RHS>
501 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
502 m_SDiv(const LHS &L, const RHS &R) {
503   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
504 }
505
506 template<typename LHS, typename RHS>
507 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
508 m_FDiv(const LHS &L, const RHS &R) {
509   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
510 }
511
512 template<typename LHS, typename RHS>
513 inline BinaryOp_match<LHS, RHS, Instruction::URem>
514 m_URem(const LHS &L, const RHS &R) {
515   return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
516 }
517
518 template<typename LHS, typename RHS>
519 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
520 m_SRem(const LHS &L, const RHS &R) {
521   return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
522 }
523
524 template<typename LHS, typename RHS>
525 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
526 m_FRem(const LHS &L, const RHS &R) {
527   return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
528 }
529
530 template<typename LHS, typename RHS>
531 inline BinaryOp_match<LHS, RHS, Instruction::And>
532 m_And(const LHS &L, const RHS &R) {
533   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
534 }
535
536 template<typename LHS, typename RHS>
537 inline BinaryOp_match<LHS, RHS, Instruction::Or>
538 m_Or(const LHS &L, const RHS &R) {
539   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
540 }
541
542 template<typename LHS, typename RHS>
543 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
544 m_Xor(const LHS &L, const RHS &R) {
545   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
546 }
547
548 template<typename LHS, typename RHS>
549 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
550 m_Shl(const LHS &L, const RHS &R) {
551   return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
552 }
553
554 template<typename LHS, typename RHS>
555 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
556 m_LShr(const LHS &L, const RHS &R) {
557   return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
558 }
559
560 template<typename LHS, typename RHS>
561 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
562 m_AShr(const LHS &L, const RHS &R) {
563   return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
564 }
565
566 template<typename LHS_t, typename RHS_t, unsigned Opcode, unsigned WrapFlags = 0>
567 struct OverflowingBinaryOp_match {
568   LHS_t L;
569   RHS_t R;
570
571   OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
572
573   template<typename OpTy>
574   bool match(OpTy *V) {
575     if (OverflowingBinaryOperator *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
576       if (Op->getOpcode() != Opcode)
577         return false;
578       if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
579           !Op->hasNoUnsignedWrap())
580         return false;
581       if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
582           !Op->hasNoSignedWrap())
583         return false;
584       return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
585     }
586     return false;
587   }
588 };
589
590 template <typename LHS, typename RHS>
591 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
592                                  OverflowingBinaryOperator::NoSignedWrap>
593 m_NSWAdd(const LHS &L, const RHS &R) {
594   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
595                                    OverflowingBinaryOperator::NoSignedWrap>(
596       L, R);
597 }
598 template <typename LHS, typename RHS>
599 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
600                                  OverflowingBinaryOperator::NoSignedWrap>
601 m_NSWSub(const LHS &L, const RHS &R) {
602   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
603                                    OverflowingBinaryOperator::NoSignedWrap>(
604       L, R);
605 }
606 template <typename LHS, typename RHS>
607 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
608                                  OverflowingBinaryOperator::NoSignedWrap>
609 m_NSWMul(const LHS &L, const RHS &R) {
610   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
611                                    OverflowingBinaryOperator::NoSignedWrap>(
612       L, R);
613 }
614 template <typename LHS, typename RHS>
615 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
616                                  OverflowingBinaryOperator::NoSignedWrap>
617 m_NSWShl(const LHS &L, const RHS &R) {
618   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
619                                    OverflowingBinaryOperator::NoSignedWrap>(
620       L, R);
621 }
622
623 template <typename LHS, typename RHS>
624 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
625                                  OverflowingBinaryOperator::NoUnsignedWrap>
626 m_NUWAdd(const LHS &L, const RHS &R) {
627   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
628                                    OverflowingBinaryOperator::NoUnsignedWrap>(
629       L, R);
630 }
631 template <typename LHS, typename RHS>
632 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
633                                  OverflowingBinaryOperator::NoUnsignedWrap>
634 m_NUWSub(const LHS &L, const RHS &R) {
635   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
636                                    OverflowingBinaryOperator::NoUnsignedWrap>(
637       L, R);
638 }
639 template <typename LHS, typename RHS>
640 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
641                                  OverflowingBinaryOperator::NoUnsignedWrap>
642 m_NUWMul(const LHS &L, const RHS &R) {
643   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
644                                    OverflowingBinaryOperator::NoUnsignedWrap>(
645       L, R);
646 }
647 template <typename LHS, typename RHS>
648 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
649                                  OverflowingBinaryOperator::NoUnsignedWrap>
650 m_NUWShl(const LHS &L, const RHS &R) {
651   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
652                                    OverflowingBinaryOperator::NoUnsignedWrap>(
653       L, R);
654 }
655
656 //===----------------------------------------------------------------------===//
657 // Class that matches two different binary ops.
658 //
659 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
660 struct BinOp2_match {
661   LHS_t L;
662   RHS_t R;
663
664   BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
665
666   template<typename OpTy>
667   bool match(OpTy *V) {
668     if (V->getValueID() == Value::InstructionVal + Opc1 ||
669         V->getValueID() == Value::InstructionVal + Opc2) {
670       BinaryOperator *I = cast<BinaryOperator>(V);
671       return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
672     }
673     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
674       return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
675              L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
676     return false;
677   }
678 };
679
680 /// m_Shr - Matches LShr or AShr.
681 template<typename LHS, typename RHS>
682 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
683 m_Shr(const LHS &L, const RHS &R) {
684   return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
685 }
686
687 /// m_LogicalShift - Matches LShr or Shl.
688 template<typename LHS, typename RHS>
689 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
690 m_LogicalShift(const LHS &L, const RHS &R) {
691   return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
692 }
693
694 /// m_IDiv - Matches UDiv and SDiv.
695 template<typename LHS, typename RHS>
696 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
697 m_IDiv(const LHS &L, const RHS &R) {
698   return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
699 }
700
701 //===----------------------------------------------------------------------===//
702 // Class that matches exact binary ops.
703 //
704 template<typename SubPattern_t>
705 struct Exact_match {
706   SubPattern_t SubPattern;
707
708   Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
709
710   template<typename OpTy>
711   bool match(OpTy *V) {
712     if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
713       return PEO->isExact() && SubPattern.match(V);
714     return false;
715   }
716 };
717
718 template<typename T>
719 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
720
721 //===----------------------------------------------------------------------===//
722 // Matchers for CmpInst classes
723 //
724
725 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
726 struct CmpClass_match {
727   PredicateTy &Predicate;
728   LHS_t L;
729   RHS_t R;
730
731   CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
732     : Predicate(Pred), L(LHS), R(RHS) {}
733
734   template<typename OpTy>
735   bool match(OpTy *V) {
736     if (Class *I = dyn_cast<Class>(V))
737       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
738         Predicate = I->getPredicate();
739         return true;
740       }
741     return false;
742   }
743 };
744
745 template<typename LHS, typename RHS>
746 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
747 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
748   return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
749 }
750
751 template<typename LHS, typename RHS>
752 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
753 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
754   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
755 }
756
757 template<typename LHS, typename RHS>
758 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
759 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
760   return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
761 }
762
763 //===----------------------------------------------------------------------===//
764 // Matchers for SelectInst classes
765 //
766
767 template<typename Cond_t, typename LHS_t, typename RHS_t>
768 struct SelectClass_match {
769   Cond_t C;
770   LHS_t L;
771   RHS_t R;
772
773   SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
774                     const RHS_t &RHS)
775     : C(Cond), L(LHS), R(RHS) {}
776
777   template<typename OpTy>
778   bool match(OpTy *V) {
779     if (SelectInst *I = dyn_cast<SelectInst>(V))
780       return C.match(I->getOperand(0)) &&
781              L.match(I->getOperand(1)) &&
782              R.match(I->getOperand(2));
783     return false;
784   }
785 };
786
787 template<typename Cond, typename LHS, typename RHS>
788 inline SelectClass_match<Cond, LHS, RHS>
789 m_Select(const Cond &C, const LHS &L, const RHS &R) {
790   return SelectClass_match<Cond, LHS, RHS>(C, L, R);
791 }
792
793 /// m_SelectCst - This matches a select of two constants, e.g.:
794 ///    m_SelectCst<-1, 0>(m_Value(V))
795 template<int64_t L, int64_t R, typename Cond>
796 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
797 m_SelectCst(const Cond &C) {
798   return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
799 }
800
801
802 //===----------------------------------------------------------------------===//
803 // Matchers for CastInst classes
804 //
805
806 template<typename Op_t, unsigned Opcode>
807 struct CastClass_match {
808   Op_t Op;
809
810   CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
811
812   template<typename OpTy>
813   bool match(OpTy *V) {
814     if (Operator *O = dyn_cast<Operator>(V))
815       return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
816     return false;
817   }
818 };
819
820 /// m_BitCast
821 template<typename OpTy>
822 inline CastClass_match<OpTy, Instruction::BitCast>
823 m_BitCast(const OpTy &Op) {
824   return CastClass_match<OpTy, Instruction::BitCast>(Op);
825 }
826
827 /// m_PtrToInt
828 template<typename OpTy>
829 inline CastClass_match<OpTy, Instruction::PtrToInt>
830 m_PtrToInt(const OpTy &Op) {
831   return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
832 }
833
834 /// m_Trunc
835 template<typename OpTy>
836 inline CastClass_match<OpTy, Instruction::Trunc>
837 m_Trunc(const OpTy &Op) {
838   return CastClass_match<OpTy, Instruction::Trunc>(Op);
839 }
840
841 /// m_SExt
842 template<typename OpTy>
843 inline CastClass_match<OpTy, Instruction::SExt>
844 m_SExt(const OpTy &Op) {
845   return CastClass_match<OpTy, Instruction::SExt>(Op);
846 }
847
848 /// m_ZExt
849 template<typename OpTy>
850 inline CastClass_match<OpTy, Instruction::ZExt>
851 m_ZExt(const OpTy &Op) {
852   return CastClass_match<OpTy, Instruction::ZExt>(Op);
853 }
854
855 /// m_UIToFP
856 template<typename OpTy>
857 inline CastClass_match<OpTy, Instruction::UIToFP>
858 m_UIToFP(const OpTy &Op) {
859   return CastClass_match<OpTy, Instruction::UIToFP>(Op);
860 }
861
862 /// m_SIToFP
863 template<typename OpTy>
864 inline CastClass_match<OpTy, Instruction::SIToFP>
865 m_SIToFP(const OpTy &Op) {
866   return CastClass_match<OpTy, Instruction::SIToFP>(Op);
867 }
868
869 //===----------------------------------------------------------------------===//
870 // Matchers for unary operators
871 //
872
873 template<typename LHS_t>
874 struct not_match {
875   LHS_t L;
876
877   not_match(const LHS_t &LHS) : L(LHS) {}
878
879   template<typename OpTy>
880   bool match(OpTy *V) {
881     if (Operator *O = dyn_cast<Operator>(V))
882       if (O->getOpcode() == Instruction::Xor)
883         return matchIfNot(O->getOperand(0), O->getOperand(1));
884     return false;
885   }
886 private:
887   bool matchIfNot(Value *LHS, Value *RHS) {
888     return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
889             // FIXME: Remove CV.
890             isa<ConstantVector>(RHS)) &&
891            cast<Constant>(RHS)->isAllOnesValue() &&
892            L.match(LHS);
893   }
894 };
895
896 template<typename LHS>
897 inline not_match<LHS> m_Not(const LHS &L) { return L; }
898
899
900 template<typename LHS_t>
901 struct neg_match {
902   LHS_t L;
903
904   neg_match(const LHS_t &LHS) : L(LHS) {}
905
906   template<typename OpTy>
907   bool match(OpTy *V) {
908     if (Operator *O = dyn_cast<Operator>(V))
909       if (O->getOpcode() == Instruction::Sub)
910         return matchIfNeg(O->getOperand(0), O->getOperand(1));
911     return false;
912   }
913 private:
914   bool matchIfNeg(Value *LHS, Value *RHS) {
915     return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
916             isa<ConstantAggregateZero>(LHS)) &&
917            L.match(RHS);
918   }
919 };
920
921 /// m_Neg - Match an integer negate.
922 template<typename LHS>
923 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
924
925
926 template<typename LHS_t>
927 struct fneg_match {
928   LHS_t L;
929
930   fneg_match(const LHS_t &LHS) : L(LHS) {}
931
932   template<typename OpTy>
933   bool match(OpTy *V) {
934     if (Operator *O = dyn_cast<Operator>(V))
935       if (O->getOpcode() == Instruction::FSub)
936         return matchIfFNeg(O->getOperand(0), O->getOperand(1));
937     return false;
938   }
939 private:
940   bool matchIfFNeg(Value *LHS, Value *RHS) {
941     if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
942       return C->isNegativeZeroValue() && L.match(RHS);
943     return false;
944   }
945 };
946
947 /// m_FNeg - Match a floating point negate.
948 template<typename LHS>
949 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
950
951
952 //===----------------------------------------------------------------------===//
953 // Matchers for control flow.
954 //
955
956 struct br_match {
957   BasicBlock *&Succ;
958   br_match(BasicBlock *&Succ)
959     : Succ(Succ) {
960   }
961
962   template<typename OpTy>
963   bool match(OpTy *V) {
964     if (BranchInst *BI = dyn_cast<BranchInst>(V))
965       if (BI->isUnconditional()) {
966         Succ = BI->getSuccessor(0);
967         return true;
968       }
969     return false;
970   }
971 };
972
973 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
974
975 template<typename Cond_t>
976 struct brc_match {
977   Cond_t Cond;
978   BasicBlock *&T, *&F;
979   brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
980     : Cond(C), T(t), F(f) {
981   }
982
983   template<typename OpTy>
984   bool match(OpTy *V) {
985     if (BranchInst *BI = dyn_cast<BranchInst>(V))
986       if (BI->isConditional() && Cond.match(BI->getCondition())) {
987         T = BI->getSuccessor(0);
988         F = BI->getSuccessor(1);
989         return true;
990       }
991     return false;
992   }
993 };
994
995 template<typename Cond_t>
996 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
997   return brc_match<Cond_t>(C, T, F);
998 }
999
1000
1001 //===----------------------------------------------------------------------===//
1002 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1003 //
1004
1005 template<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
1006 struct MaxMin_match {
1007   LHS_t L;
1008   RHS_t R;
1009
1010   MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
1011     : L(LHS), R(RHS) {}
1012
1013   template<typename OpTy>
1014   bool match(OpTy *V) {
1015     // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1016     SelectInst *SI = dyn_cast<SelectInst>(V);
1017     if (!SI)
1018       return false;
1019     CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1020     if (!Cmp)
1021       return false;
1022     // At this point we have a select conditioned on a comparison.  Check that
1023     // it is the values returned by the select that are being compared.
1024     Value *TrueVal = SI->getTrueValue();
1025     Value *FalseVal = SI->getFalseValue();
1026     Value *LHS = Cmp->getOperand(0);
1027     Value *RHS = Cmp->getOperand(1);
1028     if ((TrueVal != LHS || FalseVal != RHS) &&
1029         (TrueVal != RHS || FalseVal != LHS))
1030       return false;
1031     typename CmpInst_t::Predicate Pred = LHS == TrueVal ?
1032       Cmp->getPredicate() : Cmp->getSwappedPredicate();
1033     // Does "(x pred y) ? x : y" represent the desired max/min operation?
1034     if (!Pred_t::match(Pred))
1035       return false;
1036     // It does!  Bind the operands.
1037     return L.match(LHS) && R.match(RHS);
1038   }
1039 };
1040
1041 /// smax_pred_ty - Helper class for identifying signed max predicates.
1042 struct smax_pred_ty {
1043   static bool match(ICmpInst::Predicate Pred) {
1044     return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1045   }
1046 };
1047
1048 /// smin_pred_ty - Helper class for identifying signed min predicates.
1049 struct smin_pred_ty {
1050   static bool match(ICmpInst::Predicate Pred) {
1051     return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1052   }
1053 };
1054
1055 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
1056 struct umax_pred_ty {
1057   static bool match(ICmpInst::Predicate Pred) {
1058     return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1059   }
1060 };
1061
1062 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
1063 struct umin_pred_ty {
1064   static bool match(ICmpInst::Predicate Pred) {
1065     return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1066   }
1067 };
1068
1069 /// ofmax_pred_ty - Helper class for identifying ordered max predicates.
1070 struct ofmax_pred_ty {
1071   static bool match(FCmpInst::Predicate Pred) {
1072     return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1073   }
1074 };
1075
1076 /// ofmin_pred_ty - Helper class for identifying ordered min predicates.
1077 struct ofmin_pred_ty {
1078   static bool match(FCmpInst::Predicate Pred) {
1079     return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1080   }
1081 };
1082
1083 /// ufmax_pred_ty - Helper class for identifying unordered max predicates.
1084 struct ufmax_pred_ty {
1085   static bool match(FCmpInst::Predicate Pred) {
1086     return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1087   }
1088 };
1089
1090 /// ufmin_pred_ty - Helper class for identifying unordered min predicates.
1091 struct ufmin_pred_ty {
1092   static bool match(FCmpInst::Predicate Pred) {
1093     return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1094   }
1095 };
1096
1097 template<typename LHS, typename RHS>
1098 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>
1099 m_SMax(const LHS &L, const RHS &R) {
1100   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1101 }
1102
1103 template<typename LHS, typename RHS>
1104 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>
1105 m_SMin(const LHS &L, const RHS &R) {
1106   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1107 }
1108
1109 template<typename LHS, typename RHS>
1110 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>
1111 m_UMax(const LHS &L, const RHS &R) {
1112   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1113 }
1114
1115 template<typename LHS, typename RHS>
1116 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>
1117 m_UMin(const LHS &L, const RHS &R) {
1118   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1119 }
1120
1121 /// \brief Match an 'ordered' floating point maximum function.
1122 /// Floating point has one special value 'NaN'. Therefore, there is no total
1123 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1124 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1125 /// semantics. In the presence of 'NaN' we have to preserve the original
1126 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1127 ///
1128 ///                         max(L, R)  iff L and R are not NaN
1129 ///  m_OrdFMax(L, R) =      R          iff L or R are NaN
1130 template<typename LHS, typename RHS>
1131 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>
1132 m_OrdFMax(const LHS &L, const RHS &R) {
1133   return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1134 }
1135
1136 /// \brief Match an 'ordered' floating point minimum function.
1137 /// Floating point has one special value 'NaN'. Therefore, there is no total
1138 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1139 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1140 /// semantics. In the presence of 'NaN' we have to preserve the original
1141 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1142 ///
1143 ///                         max(L, R)  iff L and R are not NaN
1144 ///  m_OrdFMin(L, R) =      R          iff L or R are NaN
1145 template<typename LHS, typename RHS>
1146 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>
1147 m_OrdFMin(const LHS &L, const RHS &R) {
1148   return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1149 }
1150
1151 /// \brief Match an 'unordered' floating point maximum function.
1152 /// Floating point has one special value 'NaN'. Therefore, there is no total
1153 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1154 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1155 /// semantics. In the presence of 'NaN' we have to preserve the original
1156 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1157 ///
1158 ///                         max(L, R)  iff L and R are not NaN
1159 ///  m_UnordFMin(L, R) =    L          iff L or R are NaN
1160 template<typename LHS, typename RHS>
1161 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1162 m_UnordFMax(const LHS &L, const RHS &R) {
1163   return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1164 }
1165
1166 /// \brief Match an 'unordered' floating point minimum function.
1167 /// Floating point has one special value 'NaN'. Therefore, there is no total
1168 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1169 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1170 /// semantics. In the presence of 'NaN' we have to preserve the original
1171 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1172 ///
1173 ///                          max(L, R)  iff L and R are not NaN
1174 ///  m_UnordFMin(L, R) =     L          iff L or R are NaN
1175 template<typename LHS, typename RHS>
1176 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1177 m_UnordFMin(const LHS &L, const RHS &R) {
1178   return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1179 }
1180
1181 template<typename Opnd_t>
1182 struct Argument_match {
1183   unsigned OpI;
1184   Opnd_t Val;
1185   Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
1186
1187   template<typename OpTy>
1188   bool match(OpTy *V) {
1189     CallSite CS(V);
1190     return CS.isCall() && Val.match(CS.getArgument(OpI));
1191   }
1192 };
1193
1194 /// Match an argument
1195 template<unsigned OpI, typename Opnd_t>
1196 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1197   return Argument_match<Opnd_t>(OpI, Op);
1198 }
1199
1200 /// Intrinsic matchers.
1201 struct IntrinsicID_match {
1202   unsigned ID;
1203   IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) { }
1204
1205   template<typename OpTy>
1206   bool match(OpTy *V) {
1207     if (const CallInst *CI = dyn_cast<CallInst>(V))
1208       if (const Function *F = CI->getCalledFunction())
1209         return F->getIntrinsicID() == ID;
1210     return false;
1211   }
1212 };
1213
1214 /// Intrinsic matches are combinations of ID matchers, and argument
1215 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1216 /// them with lower arity matchers. Here's some convenient typedefs for up to
1217 /// several arguments, and more can be added as needed
1218 template <typename T0 = void, typename T1 = void, typename T2 = void,
1219           typename T3 = void, typename T4 = void, typename T5 = void,
1220           typename T6 = void, typename T7 = void, typename T8 = void,
1221           typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
1222 template <typename T0>
1223 struct m_Intrinsic_Ty<T0> {
1224   typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
1225 };
1226 template <typename T0, typename T1>
1227 struct m_Intrinsic_Ty<T0, T1> {
1228   typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
1229                             Argument_match<T1> > Ty;
1230 };
1231 template <typename T0, typename T1, typename T2>
1232 struct m_Intrinsic_Ty<T0, T1, T2> {
1233   typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1234                             Argument_match<T2> > Ty;
1235 };
1236 template <typename T0, typename T1, typename T2, typename T3>
1237 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1238   typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1239                             Argument_match<T3> > Ty;
1240 };
1241
1242 /// Match intrinsic calls like this:
1243 ///   m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1244 template <Intrinsic::ID IntrID>
1245 inline IntrinsicID_match
1246 m_Intrinsic() { return IntrinsicID_match(IntrID); }
1247
1248 template<Intrinsic::ID IntrID, typename T0>
1249 inline typename m_Intrinsic_Ty<T0>::Ty
1250 m_Intrinsic(const T0 &Op0) {
1251   return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1252 }
1253
1254 template<Intrinsic::ID IntrID, typename T0, typename T1>
1255 inline typename m_Intrinsic_Ty<T0, T1>::Ty
1256 m_Intrinsic(const T0 &Op0, const T1 &Op1) {
1257   return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1258 }
1259
1260 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1261 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1262 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1263   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1264 }
1265
1266 template<Intrinsic::ID IntrID, typename T0, typename T1, typename T2, typename T3>
1267 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1268 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1269   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1270 }
1271
1272 // Helper intrinsic matching specializations
1273 template<typename Opnd0>
1274 inline typename m_Intrinsic_Ty<Opnd0>::Ty
1275 m_BSwap(const Opnd0 &Op0) {
1276   return m_Intrinsic<Intrinsic::bswap>(Op0);
1277 }
1278
1279 template<typename Opnd0, typename Opnd1>
1280 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1281 m_FMin(const Opnd0 &Op0, const Opnd1 &Op1) {
1282   return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1283 }
1284
1285 template<typename Opnd0, typename Opnd1>
1286 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty
1287 m_FMax(const Opnd0 &Op0, const Opnd1 &Op1) {
1288   return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1289 }
1290
1291 } // end namespace PatternMatch
1292 } // end namespace llvm
1293
1294 #endif