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