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