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