• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides a simple and efficient mechanism for performing general
10 // tree-based pattern matches on the LLVM IR. The power of these routines is
11 // that it allows you to write concise patterns that are expressive and easy to
12 // understand. The other major advantage of this is that it allows you to
13 // trivially capture/bind elements in the pattern to variables. For example,
14 // you can do something like this:
15 //
16 //  Value *Exp = ...
17 //  Value *X, *Y;  ConstantInt *C1, *C2;      // (X & C1) | (Y & C2)
18 //  if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19 //                      m_And(m_Value(Y), m_ConstantInt(C2))))) {
20 //    ... Pattern is matched and variables are bound ...
21 //  }
22 //
23 // This is primarily useful to things like the instruction combiner, but can
24 // also be useful for static analysis tools or code generators.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #ifndef LLVM_IR_PATTERNMATCH_H
29 #define LLVM_IR_PATTERNMATCH_H
30 
31 #include "llvm/ADT/APFloat.h"
32 #include "llvm/ADT/APInt.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/InstrTypes.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/Instructions.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/IR/Operator.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/Support/Casting.h"
43 #include <cstdint>
44 
45 namespace llvm {
46 namespace PatternMatch {
47 
match(Val * V,const Pattern & P)48 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
49   return const_cast<Pattern &>(P).match(V);
50 }
51 
52 template <typename SubPattern_t> struct OneUse_match {
53   SubPattern_t SubPattern;
54 
OneUse_matchOneUse_match55   OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
56 
matchOneUse_match57   template <typename OpTy> bool match(OpTy *V) {
58     return V->hasOneUse() && SubPattern.match(V);
59   }
60 };
61 
m_OneUse(const T & SubPattern)62 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
63   return SubPattern;
64 }
65 
66 template <typename Class> struct class_match {
matchclass_match67   template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
68 };
69 
70 /// Match an arbitrary value and ignore it.
m_Value()71 inline class_match<Value> m_Value() { return class_match<Value>(); }
72 
73 /// Match an arbitrary binary operation and ignore it.
m_BinOp()74 inline class_match<BinaryOperator> m_BinOp() {
75   return class_match<BinaryOperator>();
76 }
77 
78 /// Matches any compare instruction and ignore it.
m_Cmp()79 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
80 
81 /// Match an arbitrary ConstantInt and ignore it.
m_ConstantInt()82 inline class_match<ConstantInt> m_ConstantInt() {
83   return class_match<ConstantInt>();
84 }
85 
86 /// Match an arbitrary undef constant.
m_Undef()87 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
88 
89 /// Match an arbitrary Constant and ignore it.
m_Constant()90 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
91 
92 /// Match an arbitrary basic block value and ignore it.
m_BasicBlock()93 inline class_match<BasicBlock> m_BasicBlock() {
94   return class_match<BasicBlock>();
95 }
96 
97 /// Inverting matcher
98 template <typename Ty> struct match_unless {
99   Ty M;
100 
match_unlessmatch_unless101   match_unless(const Ty &Matcher) : M(Matcher) {}
102 
matchmatch_unless103   template <typename ITy> bool match(ITy *V) { return !M.match(V); }
104 };
105 
106 /// Match if the inner matcher does *NOT* match.
m_Unless(const Ty & M)107 template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
108   return match_unless<Ty>(M);
109 }
110 
111 /// Matching combinators
112 template <typename LTy, typename RTy> struct match_combine_or {
113   LTy L;
114   RTy R;
115 
match_combine_ormatch_combine_or116   match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
117 
matchmatch_combine_or118   template <typename ITy> bool match(ITy *V) {
119     if (L.match(V))
120       return true;
121     if (R.match(V))
122       return true;
123     return false;
124   }
125 };
126 
127 template <typename LTy, typename RTy> struct match_combine_and {
128   LTy L;
129   RTy R;
130 
match_combine_andmatch_combine_and131   match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
132 
matchmatch_combine_and133   template <typename ITy> bool match(ITy *V) {
134     if (L.match(V))
135       if (R.match(V))
136         return true;
137     return false;
138   }
139 };
140 
141 /// Combine two pattern matchers matching L || R
142 template <typename LTy, typename RTy>
m_CombineOr(const LTy & L,const RTy & R)143 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
144   return match_combine_or<LTy, RTy>(L, R);
145 }
146 
147 /// Combine two pattern matchers matching L && R
148 template <typename LTy, typename RTy>
m_CombineAnd(const LTy & L,const RTy & R)149 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
150   return match_combine_and<LTy, RTy>(L, R);
151 }
152 
153 struct apint_match {
154   const APInt *&Res;
155 
apint_matchapint_match156   apint_match(const APInt *&R) : Res(R) {}
157 
matchapint_match158   template <typename ITy> bool match(ITy *V) {
159     if (auto *CI = dyn_cast<ConstantInt>(V)) {
160       Res = &CI->getValue();
161       return true;
162     }
163     if (V->getType()->isVectorTy())
164       if (const auto *C = dyn_cast<Constant>(V))
165         if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
166           Res = &CI->getValue();
167           return true;
168         }
169     return false;
170   }
171 };
172 // Either constexpr if or renaming ConstantFP::getValueAPF to
173 // ConstantFP::getValue is needed to do it via single template
174 // function for both apint/apfloat.
175 struct apfloat_match {
176   const APFloat *&Res;
apfloat_matchapfloat_match177   apfloat_match(const APFloat *&R) : Res(R) {}
matchapfloat_match178   template <typename ITy> bool match(ITy *V) {
179     if (auto *CI = dyn_cast<ConstantFP>(V)) {
180       Res = &CI->getValueAPF();
181       return true;
182     }
183     if (V->getType()->isVectorTy())
184       if (const auto *C = dyn_cast<Constant>(V))
185         if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) {
186           Res = &CI->getValueAPF();
187           return true;
188         }
189     return false;
190   }
191 };
192 
193 /// Match a ConstantInt or splatted ConstantVector, binding the
194 /// specified pointer to the contained APInt.
m_APInt(const APInt * & Res)195 inline apint_match m_APInt(const APInt *&Res) { return Res; }
196 
197 /// Match a ConstantFP or splatted ConstantVector, binding the
198 /// specified pointer to the contained APFloat.
m_APFloat(const APFloat * & Res)199 inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
200 
201 template <int64_t Val> struct constantint_match {
matchconstantint_match202   template <typename ITy> bool match(ITy *V) {
203     if (const auto *CI = dyn_cast<ConstantInt>(V)) {
204       const APInt &CIV = CI->getValue();
205       if (Val >= 0)
206         return CIV == static_cast<uint64_t>(Val);
207       // If Val is negative, and CI is shorter than it, truncate to the right
208       // number of bits.  If it is larger, then we have to sign extend.  Just
209       // compare their negated values.
210       return -CIV == -Val;
211     }
212     return false;
213   }
214 };
215 
216 /// Match a ConstantInt with a specific value.
m_ConstantInt()217 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
218   return constantint_match<Val>();
219 }
220 
221 /// This helper class is used to match scalar and vector integer constants that
222 /// satisfy a specified predicate.
223 /// For vector constants, undefined elements are ignored.
224 template <typename Predicate> struct cst_pred_ty : public Predicate {
matchcst_pred_ty225   template <typename ITy> bool match(ITy *V) {
226     if (const auto *CI = dyn_cast<ConstantInt>(V))
227       return this->isValue(CI->getValue());
228     if (V->getType()->isVectorTy()) {
229       if (const auto *C = dyn_cast<Constant>(V)) {
230         if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
231           return this->isValue(CI->getValue());
232 
233         // Non-splat vector constant: check each element for a match.
234         unsigned NumElts = V->getType()->getVectorNumElements();
235         assert(NumElts != 0 && "Constant vector with no elements?");
236         bool HasNonUndefElements = false;
237         for (unsigned i = 0; i != NumElts; ++i) {
238           Constant *Elt = C->getAggregateElement(i);
239           if (!Elt)
240             return false;
241           if (isa<UndefValue>(Elt))
242             continue;
243           auto *CI = dyn_cast<ConstantInt>(Elt);
244           if (!CI || !this->isValue(CI->getValue()))
245             return false;
246           HasNonUndefElements = true;
247         }
248         return HasNonUndefElements;
249       }
250     }
251     return false;
252   }
253 };
254 
255 /// This helper class is used to match scalar and vector constants that
256 /// satisfy a specified predicate, and bind them to an APInt.
257 template <typename Predicate> struct api_pred_ty : public Predicate {
258   const APInt *&Res;
259 
api_pred_tyapi_pred_ty260   api_pred_ty(const APInt *&R) : Res(R) {}
261 
matchapi_pred_ty262   template <typename ITy> bool match(ITy *V) {
263     if (const auto *CI = dyn_cast<ConstantInt>(V))
264       if (this->isValue(CI->getValue())) {
265         Res = &CI->getValue();
266         return true;
267       }
268     if (V->getType()->isVectorTy())
269       if (const auto *C = dyn_cast<Constant>(V))
270         if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
271           if (this->isValue(CI->getValue())) {
272             Res = &CI->getValue();
273             return true;
274           }
275 
276     return false;
277   }
278 };
279 
280 /// This helper class is used to match scalar and vector floating-point
281 /// constants that satisfy a specified predicate.
282 /// For vector constants, undefined elements are ignored.
283 template <typename Predicate> struct cstfp_pred_ty : public Predicate {
matchcstfp_pred_ty284   template <typename ITy> bool match(ITy *V) {
285     if (const auto *CF = dyn_cast<ConstantFP>(V))
286       return this->isValue(CF->getValueAPF());
287     if (V->getType()->isVectorTy()) {
288       if (const auto *C = dyn_cast<Constant>(V)) {
289         if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
290           return this->isValue(CF->getValueAPF());
291 
292         // Non-splat vector constant: check each element for a match.
293         unsigned NumElts = V->getType()->getVectorNumElements();
294         assert(NumElts != 0 && "Constant vector with no elements?");
295         bool HasNonUndefElements = false;
296         for (unsigned i = 0; i != NumElts; ++i) {
297           Constant *Elt = C->getAggregateElement(i);
298           if (!Elt)
299             return false;
300           if (isa<UndefValue>(Elt))
301             continue;
302           auto *CF = dyn_cast<ConstantFP>(Elt);
303           if (!CF || !this->isValue(CF->getValueAPF()))
304             return false;
305           HasNonUndefElements = true;
306         }
307         return HasNonUndefElements;
308       }
309     }
310     return false;
311   }
312 };
313 
314 ///////////////////////////////////////////////////////////////////////////////
315 //
316 // Encapsulate constant value queries for use in templated predicate matchers.
317 // This allows checking if constants match using compound predicates and works
318 // with vector constants, possibly with relaxed constraints. For example, ignore
319 // undef values.
320 //
321 ///////////////////////////////////////////////////////////////////////////////
322 
323 struct is_any_apint {
isValueis_any_apint324   bool isValue(const APInt &C) { return true; }
325 };
326 /// Match an integer or vector with any integral constant.
327 /// For vectors, this includes constants with undefined elements.
m_AnyIntegralConstant()328 inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
329   return cst_pred_ty<is_any_apint>();
330 }
331 
332 struct is_all_ones {
isValueis_all_ones333   bool isValue(const APInt &C) { return C.isAllOnesValue(); }
334 };
335 /// Match an integer or vector with all bits set.
336 /// For vectors, this includes constants with undefined elements.
m_AllOnes()337 inline cst_pred_ty<is_all_ones> m_AllOnes() {
338   return cst_pred_ty<is_all_ones>();
339 }
340 
341 struct is_maxsignedvalue {
isValueis_maxsignedvalue342   bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
343 };
344 /// Match an integer or vector with values having all bits except for the high
345 /// bit set (0x7f...).
346 /// For vectors, this includes constants with undefined elements.
m_MaxSignedValue()347 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
348   return cst_pred_ty<is_maxsignedvalue>();
349 }
m_MaxSignedValue(const APInt * & V)350 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
351   return V;
352 }
353 
354 struct is_negative {
isValueis_negative355   bool isValue(const APInt &C) { return C.isNegative(); }
356 };
357 /// Match an integer or vector of negative values.
358 /// For vectors, this includes constants with undefined elements.
m_Negative()359 inline cst_pred_ty<is_negative> m_Negative() {
360   return cst_pred_ty<is_negative>();
361 }
m_Negative(const APInt * & V)362 inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
363   return V;
364 }
365 
366 struct is_nonnegative {
isValueis_nonnegative367   bool isValue(const APInt &C) { return C.isNonNegative(); }
368 };
369 /// Match an integer or vector of non-negative values.
370 /// For vectors, this includes constants with undefined elements.
m_NonNegative()371 inline cst_pred_ty<is_nonnegative> m_NonNegative() {
372   return cst_pred_ty<is_nonnegative>();
373 }
m_NonNegative(const APInt * & V)374 inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
375   return V;
376 }
377 
378 struct is_strictlypositive {
isValueis_strictlypositive379   bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
380 };
381 /// Match an integer or vector of strictly positive values.
382 /// For vectors, this includes constants with undefined elements.
m_StrictlyPositive()383 inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
384   return cst_pred_ty<is_strictlypositive>();
385 }
m_StrictlyPositive(const APInt * & V)386 inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
387   return V;
388 }
389 
390 struct is_nonpositive {
isValueis_nonpositive391   bool isValue(const APInt &C) { return C.isNonPositive(); }
392 };
393 /// Match an integer or vector of non-positive values.
394 /// For vectors, this includes constants with undefined elements.
m_NonPositive()395 inline cst_pred_ty<is_nonpositive> m_NonPositive() {
396   return cst_pred_ty<is_nonpositive>();
397 }
m_NonPositive(const APInt * & V)398 inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
399 
400 struct is_one {
isValueis_one401   bool isValue(const APInt &C) { return C.isOneValue(); }
402 };
403 /// Match an integer 1 or a vector with all elements equal to 1.
404 /// For vectors, this includes constants with undefined elements.
m_One()405 inline cst_pred_ty<is_one> m_One() {
406   return cst_pred_ty<is_one>();
407 }
408 
409 struct is_zero_int {
isValueis_zero_int410   bool isValue(const APInt &C) { return C.isNullValue(); }
411 };
412 /// Match an integer 0 or a vector with all elements equal to 0.
413 /// For vectors, this includes constants with undefined elements.
m_ZeroInt()414 inline cst_pred_ty<is_zero_int> m_ZeroInt() {
415   return cst_pred_ty<is_zero_int>();
416 }
417 
418 struct is_zero {
matchis_zero419   template <typename ITy> bool match(ITy *V) {
420     auto *C = dyn_cast<Constant>(V);
421     return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
422   }
423 };
424 /// Match any null constant or a vector with all elements equal to 0.
425 /// For vectors, this includes constants with undefined elements.
m_Zero()426 inline is_zero m_Zero() {
427   return is_zero();
428 }
429 
430 struct is_power2 {
isValueis_power2431   bool isValue(const APInt &C) { return C.isPowerOf2(); }
432 };
433 /// Match an integer or vector power-of-2.
434 /// For vectors, this includes constants with undefined elements.
m_Power2()435 inline cst_pred_ty<is_power2> m_Power2() {
436   return cst_pred_ty<is_power2>();
437 }
m_Power2(const APInt * & V)438 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
439   return V;
440 }
441 
442 struct is_negated_power2 {
isValueis_negated_power2443   bool isValue(const APInt &C) { return (-C).isPowerOf2(); }
444 };
445 /// Match a integer or vector negated power-of-2.
446 /// For vectors, this includes constants with undefined elements.
m_NegatedPower2()447 inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
448   return cst_pred_ty<is_negated_power2>();
449 }
m_NegatedPower2(const APInt * & V)450 inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
451   return V;
452 }
453 
454 struct is_power2_or_zero {
isValueis_power2_or_zero455   bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
456 };
457 /// Match an integer or vector of 0 or power-of-2 values.
458 /// For vectors, this includes constants with undefined elements.
m_Power2OrZero()459 inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
460   return cst_pred_ty<is_power2_or_zero>();
461 }
m_Power2OrZero(const APInt * & V)462 inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
463   return V;
464 }
465 
466 struct is_sign_mask {
isValueis_sign_mask467   bool isValue(const APInt &C) { return C.isSignMask(); }
468 };
469 /// Match an integer or vector with only the sign bit(s) set.
470 /// For vectors, this includes constants with undefined elements.
m_SignMask()471 inline cst_pred_ty<is_sign_mask> m_SignMask() {
472   return cst_pred_ty<is_sign_mask>();
473 }
474 
475 struct is_lowbit_mask {
isValueis_lowbit_mask476   bool isValue(const APInt &C) { return C.isMask(); }
477 };
478 /// Match an integer or vector with only the low bit(s) set.
479 /// For vectors, this includes constants with undefined elements.
m_LowBitMask()480 inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
481   return cst_pred_ty<is_lowbit_mask>();
482 }
483 
484 struct icmp_pred_with_threshold {
485   ICmpInst::Predicate Pred;
486   const APInt *Thr;
isValueicmp_pred_with_threshold487   bool isValue(const APInt &C) {
488     switch (Pred) {
489     case ICmpInst::Predicate::ICMP_EQ:
490       return C.eq(*Thr);
491     case ICmpInst::Predicate::ICMP_NE:
492       return C.ne(*Thr);
493     case ICmpInst::Predicate::ICMP_UGT:
494       return C.ugt(*Thr);
495     case ICmpInst::Predicate::ICMP_UGE:
496       return C.uge(*Thr);
497     case ICmpInst::Predicate::ICMP_ULT:
498       return C.ult(*Thr);
499     case ICmpInst::Predicate::ICMP_ULE:
500       return C.ule(*Thr);
501     case ICmpInst::Predicate::ICMP_SGT:
502       return C.sgt(*Thr);
503     case ICmpInst::Predicate::ICMP_SGE:
504       return C.sge(*Thr);
505     case ICmpInst::Predicate::ICMP_SLT:
506       return C.slt(*Thr);
507     case ICmpInst::Predicate::ICMP_SLE:
508       return C.sle(*Thr);
509     default:
510       llvm_unreachable("Unhandled ICmp predicate");
511     }
512   }
513 };
514 /// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
515 /// to Threshold. For vectors, this includes constants with undefined elements.
516 inline cst_pred_ty<icmp_pred_with_threshold>
m_SpecificInt_ICMP(ICmpInst::Predicate Predicate,const APInt & Threshold)517 m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
518   cst_pred_ty<icmp_pred_with_threshold> P;
519   P.Pred = Predicate;
520   P.Thr = &Threshold;
521   return P;
522 }
523 
524 struct is_nan {
isValueis_nan525   bool isValue(const APFloat &C) { return C.isNaN(); }
526 };
527 /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
528 /// For vectors, this includes constants with undefined elements.
m_NaN()529 inline cstfp_pred_ty<is_nan> m_NaN() {
530   return cstfp_pred_ty<is_nan>();
531 }
532 
533 struct is_any_zero_fp {
isValueis_any_zero_fp534   bool isValue(const APFloat &C) { return C.isZero(); }
535 };
536 /// Match a floating-point negative zero or positive zero.
537 /// For vectors, this includes constants with undefined elements.
m_AnyZeroFP()538 inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
539   return cstfp_pred_ty<is_any_zero_fp>();
540 }
541 
542 struct is_pos_zero_fp {
isValueis_pos_zero_fp543   bool isValue(const APFloat &C) { return C.isPosZero(); }
544 };
545 /// Match a floating-point positive zero.
546 /// For vectors, this includes constants with undefined elements.
m_PosZeroFP()547 inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
548   return cstfp_pred_ty<is_pos_zero_fp>();
549 }
550 
551 struct is_neg_zero_fp {
isValueis_neg_zero_fp552   bool isValue(const APFloat &C) { return C.isNegZero(); }
553 };
554 /// Match a floating-point negative zero.
555 /// For vectors, this includes constants with undefined elements.
m_NegZeroFP()556 inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
557   return cstfp_pred_ty<is_neg_zero_fp>();
558 }
559 
560 ///////////////////////////////////////////////////////////////////////////////
561 
562 template <typename Class> struct bind_ty {
563   Class *&VR;
564 
bind_tybind_ty565   bind_ty(Class *&V) : VR(V) {}
566 
matchbind_ty567   template <typename ITy> bool match(ITy *V) {
568     if (auto *CV = dyn_cast<Class>(V)) {
569       VR = CV;
570       return true;
571     }
572     return false;
573   }
574 };
575 
576 /// Match a value, capturing it if we match.
m_Value(Value * & V)577 inline bind_ty<Value> m_Value(Value *&V) { return V; }
m_Value(const Value * & V)578 inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
579 
580 /// Match an instruction, capturing it if we match.
m_Instruction(Instruction * & I)581 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
582 /// Match a binary operator, capturing it if we match.
m_BinOp(BinaryOperator * & I)583 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
584 /// Match a with overflow intrinsic, capturing it if we match.
m_WithOverflowInst(WithOverflowInst * & I)585 inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { return I; }
586 
587 /// Match a ConstantInt, capturing the value if we match.
m_ConstantInt(ConstantInt * & CI)588 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
589 
590 /// Match a Constant, capturing the value if we match.
m_Constant(Constant * & C)591 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
592 
593 /// Match a ConstantFP, capturing the value if we match.
m_ConstantFP(ConstantFP * & C)594 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
595 
596 /// Match a basic block value, capturing it if we match.
m_BasicBlock(BasicBlock * & V)597 inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; }
m_BasicBlock(const BasicBlock * & V)598 inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
599   return V;
600 }
601 
602 /// Match a specified Value*.
603 struct specificval_ty {
604   const Value *Val;
605 
specificval_tyspecificval_ty606   specificval_ty(const Value *V) : Val(V) {}
607 
matchspecificval_ty608   template <typename ITy> bool match(ITy *V) { return V == Val; }
609 };
610 
611 /// Match if we have a specific specified value.
m_Specific(const Value * V)612 inline specificval_ty m_Specific(const Value *V) { return V; }
613 
614 /// Stores a reference to the Value *, not the Value * itself,
615 /// thus can be used in commutative matchers.
616 template <typename Class> struct deferredval_ty {
617   Class *const &Val;
618 
deferredval_tydeferredval_ty619   deferredval_ty(Class *const &V) : Val(V) {}
620 
matchdeferredval_ty621   template <typename ITy> bool match(ITy *const V) { return V == Val; }
622 };
623 
624 /// A commutative-friendly version of m_Specific().
m_Deferred(Value * const & V)625 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
m_Deferred(const Value * const & V)626 inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
627   return V;
628 }
629 
630 /// Match a specified floating point value or vector of all elements of
631 /// that value.
632 struct specific_fpval {
633   double Val;
634 
specific_fpvalspecific_fpval635   specific_fpval(double V) : Val(V) {}
636 
matchspecific_fpval637   template <typename ITy> bool match(ITy *V) {
638     if (const auto *CFP = dyn_cast<ConstantFP>(V))
639       return CFP->isExactlyValue(Val);
640     if (V->getType()->isVectorTy())
641       if (const auto *C = dyn_cast<Constant>(V))
642         if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
643           return CFP->isExactlyValue(Val);
644     return false;
645   }
646 };
647 
648 /// Match a specific floating point value or vector with all elements
649 /// equal to the value.
m_SpecificFP(double V)650 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
651 
652 /// Match a float 1.0 or vector with all elements equal to 1.0.
m_FPOne()653 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
654 
655 struct bind_const_intval_ty {
656   uint64_t &VR;
657 
bind_const_intval_tybind_const_intval_ty658   bind_const_intval_ty(uint64_t &V) : VR(V) {}
659 
matchbind_const_intval_ty660   template <typename ITy> bool match(ITy *V) {
661     if (const auto *CV = dyn_cast<ConstantInt>(V))
662       if (CV->getValue().ule(UINT64_MAX)) {
663         VR = CV->getZExtValue();
664         return true;
665       }
666     return false;
667   }
668 };
669 
670 /// Match a specified integer value or vector of all elements of that
671 /// value.
672 struct specific_intval {
673   APInt Val;
674 
specific_intvalspecific_intval675   specific_intval(APInt V) : Val(std::move(V)) {}
676 
matchspecific_intval677   template <typename ITy> bool match(ITy *V) {
678     const auto *CI = dyn_cast<ConstantInt>(V);
679     if (!CI && V->getType()->isVectorTy())
680       if (const auto *C = dyn_cast<Constant>(V))
681         CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
682 
683     return CI && APInt::isSameValue(CI->getValue(), Val);
684   }
685 };
686 
687 /// Match a specific integer value or vector with all elements equal to
688 /// the value.
m_SpecificInt(APInt V)689 inline specific_intval m_SpecificInt(APInt V) {
690   return specific_intval(std::move(V));
691 }
692 
m_SpecificInt(uint64_t V)693 inline specific_intval m_SpecificInt(uint64_t V) {
694   return m_SpecificInt(APInt(64, V));
695 }
696 
697 /// Match a ConstantInt and bind to its value.  This does not match
698 /// ConstantInts wider than 64-bits.
m_ConstantInt(uint64_t & V)699 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
700 
701 /// Match a specified basic block value.
702 struct specific_bbval {
703   BasicBlock *Val;
704 
specific_bbvalspecific_bbval705   specific_bbval(BasicBlock *Val) : Val(Val) {}
706 
matchspecific_bbval707   template <typename ITy> bool match(ITy *V) {
708     const auto *BB = dyn_cast<BasicBlock>(V);
709     return BB && BB == Val;
710   }
711 };
712 
713 /// Match a specific basic block value.
m_SpecificBB(BasicBlock * BB)714 inline specific_bbval m_SpecificBB(BasicBlock *BB) {
715   return specific_bbval(BB);
716 }
717 
718 /// A commutative-friendly version of m_Specific().
m_Deferred(BasicBlock * const & BB)719 inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) {
720   return BB;
721 }
722 inline deferredval_ty<const BasicBlock>
m_Deferred(const BasicBlock * const & BB)723 m_Deferred(const BasicBlock *const &BB) {
724   return BB;
725 }
726 
727 //===----------------------------------------------------------------------===//
728 // Matcher for any binary operator.
729 //
730 template <typename LHS_t, typename RHS_t, bool Commutable = false>
731 struct AnyBinaryOp_match {
732   LHS_t L;
733   RHS_t R;
734 
735   // The evaluation order is always stable, regardless of Commutability.
736   // The LHS is always matched first.
AnyBinaryOp_matchAnyBinaryOp_match737   AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
738 
matchAnyBinaryOp_match739   template <typename OpTy> bool match(OpTy *V) {
740     if (auto *I = dyn_cast<BinaryOperator>(V))
741       return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
742              (Commutable && L.match(I->getOperand(1)) &&
743               R.match(I->getOperand(0)));
744     return false;
745   }
746 };
747 
748 template <typename LHS, typename RHS>
m_BinOp(const LHS & L,const RHS & R)749 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
750   return AnyBinaryOp_match<LHS, RHS>(L, R);
751 }
752 
753 //===----------------------------------------------------------------------===//
754 // Matchers for specific binary operators.
755 //
756 
757 template <typename LHS_t, typename RHS_t, unsigned Opcode,
758           bool Commutable = false>
759 struct BinaryOp_match {
760   LHS_t L;
761   RHS_t R;
762 
763   // The evaluation order is always stable, regardless of Commutability.
764   // The LHS is always matched first.
BinaryOp_matchBinaryOp_match765   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
766 
matchBinaryOp_match767   template <typename OpTy> bool match(OpTy *V) {
768     if (V->getValueID() == Value::InstructionVal + Opcode) {
769       auto *I = cast<BinaryOperator>(V);
770       return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
771              (Commutable && L.match(I->getOperand(1)) &&
772               R.match(I->getOperand(0)));
773     }
774     if (auto *CE = dyn_cast<ConstantExpr>(V))
775       return CE->getOpcode() == Opcode &&
776              ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
777               (Commutable && L.match(CE->getOperand(1)) &&
778                R.match(CE->getOperand(0))));
779     return false;
780   }
781 };
782 
783 template <typename LHS, typename RHS>
m_Add(const LHS & L,const RHS & R)784 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
785                                                         const RHS &R) {
786   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
787 }
788 
789 template <typename LHS, typename RHS>
m_FAdd(const LHS & L,const RHS & R)790 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
791                                                           const RHS &R) {
792   return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
793 }
794 
795 template <typename LHS, typename RHS>
m_Sub(const LHS & L,const RHS & R)796 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
797                                                         const RHS &R) {
798   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
799 }
800 
801 template <typename LHS, typename RHS>
m_FSub(const LHS & L,const RHS & R)802 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
803                                                           const RHS &R) {
804   return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
805 }
806 
807 template <typename Op_t> struct FNeg_match {
808   Op_t X;
809 
FNeg_matchFNeg_match810   FNeg_match(const Op_t &Op) : X(Op) {}
matchFNeg_match811   template <typename OpTy> bool match(OpTy *V) {
812     auto *FPMO = dyn_cast<FPMathOperator>(V);
813     if (!FPMO) return false;
814 
815     if (FPMO->getOpcode() == Instruction::FNeg)
816       return X.match(FPMO->getOperand(0));
817 
818     if (FPMO->getOpcode() == Instruction::FSub) {
819       if (FPMO->hasNoSignedZeros()) {
820         // With 'nsz', any zero goes.
821         if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
822           return false;
823       } else {
824         // Without 'nsz', we need fsub -0.0, X exactly.
825         if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
826           return false;
827       }
828 
829       return X.match(FPMO->getOperand(1));
830     }
831 
832     return false;
833   }
834 };
835 
836 /// Match 'fneg X' as 'fsub -0.0, X'.
837 template <typename OpTy>
838 inline FNeg_match<OpTy>
m_FNeg(const OpTy & X)839 m_FNeg(const OpTy &X) {
840   return FNeg_match<OpTy>(X);
841 }
842 
843 /// Match 'fneg X' as 'fsub +-0.0, X'.
844 template <typename RHS>
845 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
m_FNegNSZ(const RHS & X)846 m_FNegNSZ(const RHS &X) {
847   return m_FSub(m_AnyZeroFP(), X);
848 }
849 
850 template <typename LHS, typename RHS>
m_Mul(const LHS & L,const RHS & R)851 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
852                                                         const RHS &R) {
853   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
854 }
855 
856 template <typename LHS, typename RHS>
m_FMul(const LHS & L,const RHS & R)857 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
858                                                           const RHS &R) {
859   return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
860 }
861 
862 template <typename LHS, typename RHS>
m_UDiv(const LHS & L,const RHS & R)863 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
864                                                           const RHS &R) {
865   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
866 }
867 
868 template <typename LHS, typename RHS>
m_SDiv(const LHS & L,const RHS & R)869 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
870                                                           const RHS &R) {
871   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
872 }
873 
874 template <typename LHS, typename RHS>
m_FDiv(const LHS & L,const RHS & R)875 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
876                                                           const RHS &R) {
877   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
878 }
879 
880 template <typename LHS, typename RHS>
m_URem(const LHS & L,const RHS & R)881 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
882                                                           const RHS &R) {
883   return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
884 }
885 
886 template <typename LHS, typename RHS>
m_SRem(const LHS & L,const RHS & R)887 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
888                                                           const RHS &R) {
889   return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
890 }
891 
892 template <typename LHS, typename RHS>
m_FRem(const LHS & L,const RHS & R)893 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
894                                                           const RHS &R) {
895   return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
896 }
897 
898 template <typename LHS, typename RHS>
m_And(const LHS & L,const RHS & R)899 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
900                                                         const RHS &R) {
901   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
902 }
903 
904 template <typename LHS, typename RHS>
m_Or(const LHS & L,const RHS & R)905 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
906                                                       const RHS &R) {
907   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
908 }
909 
910 template <typename LHS, typename RHS>
m_Xor(const LHS & L,const RHS & R)911 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
912                                                         const RHS &R) {
913   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
914 }
915 
916 template <typename LHS, typename RHS>
m_Shl(const LHS & L,const RHS & R)917 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
918                                                         const RHS &R) {
919   return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
920 }
921 
922 template <typename LHS, typename RHS>
m_LShr(const LHS & L,const RHS & R)923 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
924                                                           const RHS &R) {
925   return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
926 }
927 
928 template <typename LHS, typename RHS>
m_AShr(const LHS & L,const RHS & R)929 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
930                                                           const RHS &R) {
931   return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
932 }
933 
934 template <typename LHS_t, typename RHS_t, unsigned Opcode,
935           unsigned WrapFlags = 0>
936 struct OverflowingBinaryOp_match {
937   LHS_t L;
938   RHS_t R;
939 
OverflowingBinaryOp_matchOverflowingBinaryOp_match940   OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
941       : L(LHS), R(RHS) {}
942 
matchOverflowingBinaryOp_match943   template <typename OpTy> bool match(OpTy *V) {
944     if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
945       if (Op->getOpcode() != Opcode)
946         return false;
947       if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
948           !Op->hasNoUnsignedWrap())
949         return false;
950       if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
951           !Op->hasNoSignedWrap())
952         return false;
953       return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
954     }
955     return false;
956   }
957 };
958 
959 template <typename LHS, typename RHS>
960 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
961                                  OverflowingBinaryOperator::NoSignedWrap>
m_NSWAdd(const LHS & L,const RHS & R)962 m_NSWAdd(const LHS &L, const RHS &R) {
963   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
964                                    OverflowingBinaryOperator::NoSignedWrap>(
965       L, R);
966 }
967 template <typename LHS, typename RHS>
968 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
969                                  OverflowingBinaryOperator::NoSignedWrap>
m_NSWSub(const LHS & L,const RHS & R)970 m_NSWSub(const LHS &L, const RHS &R) {
971   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
972                                    OverflowingBinaryOperator::NoSignedWrap>(
973       L, R);
974 }
975 template <typename LHS, typename RHS>
976 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
977                                  OverflowingBinaryOperator::NoSignedWrap>
m_NSWMul(const LHS & L,const RHS & R)978 m_NSWMul(const LHS &L, const RHS &R) {
979   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
980                                    OverflowingBinaryOperator::NoSignedWrap>(
981       L, R);
982 }
983 template <typename LHS, typename RHS>
984 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
985                                  OverflowingBinaryOperator::NoSignedWrap>
m_NSWShl(const LHS & L,const RHS & R)986 m_NSWShl(const LHS &L, const RHS &R) {
987   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
988                                    OverflowingBinaryOperator::NoSignedWrap>(
989       L, R);
990 }
991 
992 template <typename LHS, typename RHS>
993 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
994                                  OverflowingBinaryOperator::NoUnsignedWrap>
m_NUWAdd(const LHS & L,const RHS & R)995 m_NUWAdd(const LHS &L, const RHS &R) {
996   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
997                                    OverflowingBinaryOperator::NoUnsignedWrap>(
998       L, R);
999 }
1000 template <typename LHS, typename RHS>
1001 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1002                                  OverflowingBinaryOperator::NoUnsignedWrap>
m_NUWSub(const LHS & L,const RHS & R)1003 m_NUWSub(const LHS &L, const RHS &R) {
1004   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1005                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1006       L, R);
1007 }
1008 template <typename LHS, typename RHS>
1009 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1010                                  OverflowingBinaryOperator::NoUnsignedWrap>
m_NUWMul(const LHS & L,const RHS & R)1011 m_NUWMul(const LHS &L, const RHS &R) {
1012   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1013                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1014       L, R);
1015 }
1016 template <typename LHS, typename RHS>
1017 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1018                                  OverflowingBinaryOperator::NoUnsignedWrap>
m_NUWShl(const LHS & L,const RHS & R)1019 m_NUWShl(const LHS &L, const RHS &R) {
1020   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1021                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1022       L, R);
1023 }
1024 
1025 //===----------------------------------------------------------------------===//
1026 // Class that matches a group of binary opcodes.
1027 //
1028 template <typename LHS_t, typename RHS_t, typename Predicate>
1029 struct BinOpPred_match : Predicate {
1030   LHS_t L;
1031   RHS_t R;
1032 
BinOpPred_matchBinOpPred_match1033   BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1034 
matchBinOpPred_match1035   template <typename OpTy> bool match(OpTy *V) {
1036     if (auto *I = dyn_cast<Instruction>(V))
1037       return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
1038              R.match(I->getOperand(1));
1039     if (auto *CE = dyn_cast<ConstantExpr>(V))
1040       return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
1041              R.match(CE->getOperand(1));
1042     return false;
1043   }
1044 };
1045 
1046 struct is_shift_op {
isOpTypeis_shift_op1047   bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
1048 };
1049 
1050 struct is_right_shift_op {
isOpTypeis_right_shift_op1051   bool isOpType(unsigned Opcode) {
1052     return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1053   }
1054 };
1055 
1056 struct is_logical_shift_op {
isOpTypeis_logical_shift_op1057   bool isOpType(unsigned Opcode) {
1058     return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1059   }
1060 };
1061 
1062 struct is_bitwiselogic_op {
isOpTypeis_bitwiselogic_op1063   bool isOpType(unsigned Opcode) {
1064     return Instruction::isBitwiseLogicOp(Opcode);
1065   }
1066 };
1067 
1068 struct is_idiv_op {
isOpTypeis_idiv_op1069   bool isOpType(unsigned Opcode) {
1070     return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1071   }
1072 };
1073 
1074 struct is_irem_op {
isOpTypeis_irem_op1075   bool isOpType(unsigned Opcode) {
1076     return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1077   }
1078 };
1079 
1080 /// Matches shift operations.
1081 template <typename LHS, typename RHS>
m_Shift(const LHS & L,const RHS & R)1082 inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
1083                                                       const RHS &R) {
1084   return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
1085 }
1086 
1087 /// Matches logical shift operations.
1088 template <typename LHS, typename RHS>
m_Shr(const LHS & L,const RHS & R)1089 inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
1090                                                           const RHS &R) {
1091   return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
1092 }
1093 
1094 /// Matches logical shift operations.
1095 template <typename LHS, typename RHS>
1096 inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
m_LogicalShift(const LHS & L,const RHS & R)1097 m_LogicalShift(const LHS &L, const RHS &R) {
1098   return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
1099 }
1100 
1101 /// Matches bitwise logic operations.
1102 template <typename LHS, typename RHS>
1103 inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
m_BitwiseLogic(const LHS & L,const RHS & R)1104 m_BitwiseLogic(const LHS &L, const RHS &R) {
1105   return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
1106 }
1107 
1108 /// Matches integer division operations.
1109 template <typename LHS, typename RHS>
m_IDiv(const LHS & L,const RHS & R)1110 inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
1111                                                     const RHS &R) {
1112   return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
1113 }
1114 
1115 /// Matches integer remainder operations.
1116 template <typename LHS, typename RHS>
m_IRem(const LHS & L,const RHS & R)1117 inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L,
1118                                                     const RHS &R) {
1119   return BinOpPred_match<LHS, RHS, is_irem_op>(L, R);
1120 }
1121 
1122 //===----------------------------------------------------------------------===//
1123 // Class that matches exact binary ops.
1124 //
1125 template <typename SubPattern_t> struct Exact_match {
1126   SubPattern_t SubPattern;
1127 
Exact_matchExact_match1128   Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1129 
matchExact_match1130   template <typename OpTy> bool match(OpTy *V) {
1131     if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1132       return PEO->isExact() && SubPattern.match(V);
1133     return false;
1134   }
1135 };
1136 
m_Exact(const T & SubPattern)1137 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1138   return SubPattern;
1139 }
1140 
1141 //===----------------------------------------------------------------------===//
1142 // Matchers for CmpInst classes
1143 //
1144 
1145 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1146           bool Commutable = false>
1147 struct CmpClass_match {
1148   PredicateTy &Predicate;
1149   LHS_t L;
1150   RHS_t R;
1151 
1152   // The evaluation order is always stable, regardless of Commutability.
1153   // The LHS is always matched first.
CmpClass_matchCmpClass_match1154   CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1155       : Predicate(Pred), L(LHS), R(RHS) {}
1156 
matchCmpClass_match1157   template <typename OpTy> bool match(OpTy *V) {
1158     if (auto *I = dyn_cast<Class>(V))
1159       if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1160           (Commutable && L.match(I->getOperand(1)) &&
1161            R.match(I->getOperand(0)))) {
1162         Predicate = I->getPredicate();
1163         return true;
1164       }
1165     return false;
1166   }
1167 };
1168 
1169 template <typename LHS, typename RHS>
1170 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
m_Cmp(CmpInst::Predicate & Pred,const LHS & L,const RHS & R)1171 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1172   return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
1173 }
1174 
1175 template <typename LHS, typename RHS>
1176 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
m_ICmp(ICmpInst::Predicate & Pred,const LHS & L,const RHS & R)1177 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1178   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
1179 }
1180 
1181 template <typename LHS, typename RHS>
1182 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
m_FCmp(FCmpInst::Predicate & Pred,const LHS & L,const RHS & R)1183 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1184   return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
1185 }
1186 
1187 //===----------------------------------------------------------------------===//
1188 // Matchers for instructions with a given opcode and number of operands.
1189 //
1190 
1191 /// Matches instructions with Opcode and three operands.
1192 template <typename T0, unsigned Opcode> struct OneOps_match {
1193   T0 Op1;
1194 
OneOps_matchOneOps_match1195   OneOps_match(const T0 &Op1) : Op1(Op1) {}
1196 
matchOneOps_match1197   template <typename OpTy> bool match(OpTy *V) {
1198     if (V->getValueID() == Value::InstructionVal + Opcode) {
1199       auto *I = cast<Instruction>(V);
1200       return Op1.match(I->getOperand(0));
1201     }
1202     return false;
1203   }
1204 };
1205 
1206 /// Matches instructions with Opcode and three operands.
1207 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1208   T0 Op1;
1209   T1 Op2;
1210 
TwoOps_matchTwoOps_match1211   TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1212 
matchTwoOps_match1213   template <typename OpTy> bool match(OpTy *V) {
1214     if (V->getValueID() == Value::InstructionVal + Opcode) {
1215       auto *I = cast<Instruction>(V);
1216       return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1217     }
1218     return false;
1219   }
1220 };
1221 
1222 /// Matches instructions with Opcode and three operands.
1223 template <typename T0, typename T1, typename T2, unsigned Opcode>
1224 struct ThreeOps_match {
1225   T0 Op1;
1226   T1 Op2;
1227   T2 Op3;
1228 
ThreeOps_matchThreeOps_match1229   ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1230       : Op1(Op1), Op2(Op2), Op3(Op3) {}
1231 
matchThreeOps_match1232   template <typename OpTy> bool match(OpTy *V) {
1233     if (V->getValueID() == Value::InstructionVal + Opcode) {
1234       auto *I = cast<Instruction>(V);
1235       return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1236              Op3.match(I->getOperand(2));
1237     }
1238     return false;
1239   }
1240 };
1241 
1242 /// Matches SelectInst.
1243 template <typename Cond, typename LHS, typename RHS>
1244 inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
m_Select(const Cond & C,const LHS & L,const RHS & R)1245 m_Select(const Cond &C, const LHS &L, const RHS &R) {
1246   return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
1247 }
1248 
1249 /// This matches a select of two constants, e.g.:
1250 /// m_SelectCst<-1, 0>(m_Value(V))
1251 template <int64_t L, int64_t R, typename Cond>
1252 inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1253                       Instruction::Select>
m_SelectCst(const Cond & C)1254 m_SelectCst(const Cond &C) {
1255   return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1256 }
1257 
1258 /// Matches FreezeInst.
1259 template <typename OpTy>
m_Freeze(const OpTy & Op)1260 inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
1261   return OneOps_match<OpTy, Instruction::Freeze>(Op);
1262 }
1263 
1264 /// Matches InsertElementInst.
1265 template <typename Val_t, typename Elt_t, typename Idx_t>
1266 inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
m_InsertElement(const Val_t & Val,const Elt_t & Elt,const Idx_t & Idx)1267 m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1268   return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1269       Val, Elt, Idx);
1270 }
1271 
1272 /// Matches ExtractElementInst.
1273 template <typename Val_t, typename Idx_t>
1274 inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
m_ExtractElement(const Val_t & Val,const Idx_t & Idx)1275 m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
1276   return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
1277 }
1278 
1279 /// Matches ShuffleVectorInst.
1280 template <typename V1_t, typename V2_t, typename Mask_t>
1281 inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>
m_ShuffleVector(const V1_t & v1,const V2_t & v2,const Mask_t & m)1282 m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
1283   return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2,
1284                                                                         m);
1285 }
1286 
1287 /// Matches LoadInst.
1288 template <typename OpTy>
m_Load(const OpTy & Op)1289 inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1290   return OneOps_match<OpTy, Instruction::Load>(Op);
1291 }
1292 
1293 /// Matches StoreInst.
1294 template <typename ValueOpTy, typename PointerOpTy>
1295 inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
m_Store(const ValueOpTy & ValueOp,const PointerOpTy & PointerOp)1296 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1297   return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1298                                                                   PointerOp);
1299 }
1300 
1301 //===----------------------------------------------------------------------===//
1302 // Matchers for CastInst classes
1303 //
1304 
1305 template <typename Op_t, unsigned Opcode> struct CastClass_match {
1306   Op_t Op;
1307 
CastClass_matchCastClass_match1308   CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1309 
matchCastClass_match1310   template <typename OpTy> bool match(OpTy *V) {
1311     if (auto *O = dyn_cast<Operator>(V))
1312       return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1313     return false;
1314   }
1315 };
1316 
1317 /// Matches BitCast.
1318 template <typename OpTy>
m_BitCast(const OpTy & Op)1319 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
1320   return CastClass_match<OpTy, Instruction::BitCast>(Op);
1321 }
1322 
1323 /// Matches PtrToInt.
1324 template <typename OpTy>
m_PtrToInt(const OpTy & Op)1325 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
1326   return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
1327 }
1328 
1329 /// Matches Trunc.
1330 template <typename OpTy>
m_Trunc(const OpTy & Op)1331 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1332   return CastClass_match<OpTy, Instruction::Trunc>(Op);
1333 }
1334 
1335 template <typename OpTy>
1336 inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy>
m_TruncOrSelf(const OpTy & Op)1337 m_TruncOrSelf(const OpTy &Op) {
1338   return m_CombineOr(m_Trunc(Op), Op);
1339 }
1340 
1341 /// Matches SExt.
1342 template <typename OpTy>
m_SExt(const OpTy & Op)1343 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1344   return CastClass_match<OpTy, Instruction::SExt>(Op);
1345 }
1346 
1347 /// Matches ZExt.
1348 template <typename OpTy>
m_ZExt(const OpTy & Op)1349 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1350   return CastClass_match<OpTy, Instruction::ZExt>(Op);
1351 }
1352 
1353 template <typename OpTy>
1354 inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy>
m_ZExtOrSelf(const OpTy & Op)1355 m_ZExtOrSelf(const OpTy &Op) {
1356   return m_CombineOr(m_ZExt(Op), Op);
1357 }
1358 
1359 template <typename OpTy>
1360 inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy>
m_SExtOrSelf(const OpTy & Op)1361 m_SExtOrSelf(const OpTy &Op) {
1362   return m_CombineOr(m_SExt(Op), Op);
1363 }
1364 
1365 template <typename OpTy>
1366 inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1367                         CastClass_match<OpTy, Instruction::SExt>>
m_ZExtOrSExt(const OpTy & Op)1368 m_ZExtOrSExt(const OpTy &Op) {
1369   return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1370 }
1371 
1372 template <typename OpTy>
1373 inline match_combine_or<
1374     match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1375                      CastClass_match<OpTy, Instruction::SExt>>,
1376     OpTy>
m_ZExtOrSExtOrSelf(const OpTy & Op)1377 m_ZExtOrSExtOrSelf(const OpTy &Op) {
1378   return m_CombineOr(m_ZExtOrSExt(Op), Op);
1379 }
1380 
1381 /// Matches UIToFP.
1382 template <typename OpTy>
m_UIToFP(const OpTy & Op)1383 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1384   return CastClass_match<OpTy, Instruction::UIToFP>(Op);
1385 }
1386 
1387 /// Matches SIToFP.
1388 template <typename OpTy>
m_SIToFP(const OpTy & Op)1389 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1390   return CastClass_match<OpTy, Instruction::SIToFP>(Op);
1391 }
1392 
1393 /// Matches FPTrunc
1394 template <typename OpTy>
m_FPTrunc(const OpTy & Op)1395 inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
1396   return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
1397 }
1398 
1399 /// Matches FPExt
1400 template <typename OpTy>
m_FPExt(const OpTy & Op)1401 inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1402   return CastClass_match<OpTy, Instruction::FPExt>(Op);
1403 }
1404 
1405 //===----------------------------------------------------------------------===//
1406 // Matchers for control flow.
1407 //
1408 
1409 struct br_match {
1410   BasicBlock *&Succ;
1411 
br_matchbr_match1412   br_match(BasicBlock *&Succ) : Succ(Succ) {}
1413 
matchbr_match1414   template <typename OpTy> bool match(OpTy *V) {
1415     if (auto *BI = dyn_cast<BranchInst>(V))
1416       if (BI->isUnconditional()) {
1417         Succ = BI->getSuccessor(0);
1418         return true;
1419       }
1420     return false;
1421   }
1422 };
1423 
m_UnconditionalBr(BasicBlock * & Succ)1424 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1425 
1426 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1427 struct brc_match {
1428   Cond_t Cond;
1429   TrueBlock_t T;
1430   FalseBlock_t F;
1431 
brc_matchbrc_match1432   brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
1433       : Cond(C), T(t), F(f) {}
1434 
matchbrc_match1435   template <typename OpTy> bool match(OpTy *V) {
1436     if (auto *BI = dyn_cast<BranchInst>(V))
1437       if (BI->isConditional() && Cond.match(BI->getCondition()))
1438         return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
1439     return false;
1440   }
1441 };
1442 
1443 template <typename Cond_t>
1444 inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>
m_Br(const Cond_t & C,BasicBlock * & T,BasicBlock * & F)1445 m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1446   return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>(
1447       C, m_BasicBlock(T), m_BasicBlock(F));
1448 }
1449 
1450 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1451 inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t>
m_Br(const Cond_t & C,const TrueBlock_t & T,const FalseBlock_t & F)1452 m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
1453   return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F);
1454 }
1455 
1456 //===----------------------------------------------------------------------===//
1457 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1458 //
1459 
1460 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1461           bool Commutable = false>
1462 struct MaxMin_match {
1463   LHS_t L;
1464   RHS_t R;
1465 
1466   // The evaluation order is always stable, regardless of Commutability.
1467   // The LHS is always matched first.
MaxMin_matchMaxMin_match1468   MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1469 
matchMaxMin_match1470   template <typename OpTy> bool match(OpTy *V) {
1471     // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1472     auto *SI = dyn_cast<SelectInst>(V);
1473     if (!SI)
1474       return false;
1475     auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1476     if (!Cmp)
1477       return false;
1478     // At this point we have a select conditioned on a comparison.  Check that
1479     // it is the values returned by the select that are being compared.
1480     Value *TrueVal = SI->getTrueValue();
1481     Value *FalseVal = SI->getFalseValue();
1482     Value *LHS = Cmp->getOperand(0);
1483     Value *RHS = Cmp->getOperand(1);
1484     if ((TrueVal != LHS || FalseVal != RHS) &&
1485         (TrueVal != RHS || FalseVal != LHS))
1486       return false;
1487     typename CmpInst_t::Predicate Pred =
1488         LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1489     // Does "(x pred y) ? x : y" represent the desired max/min operation?
1490     if (!Pred_t::match(Pred))
1491       return false;
1492     // It does!  Bind the operands.
1493     return (L.match(LHS) && R.match(RHS)) ||
1494            (Commutable && L.match(RHS) && R.match(LHS));
1495   }
1496 };
1497 
1498 /// Helper class for identifying signed max predicates.
1499 struct smax_pred_ty {
matchsmax_pred_ty1500   static bool match(ICmpInst::Predicate Pred) {
1501     return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1502   }
1503 };
1504 
1505 /// Helper class for identifying signed min predicates.
1506 struct smin_pred_ty {
matchsmin_pred_ty1507   static bool match(ICmpInst::Predicate Pred) {
1508     return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1509   }
1510 };
1511 
1512 /// Helper class for identifying unsigned max predicates.
1513 struct umax_pred_ty {
matchumax_pred_ty1514   static bool match(ICmpInst::Predicate Pred) {
1515     return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1516   }
1517 };
1518 
1519 /// Helper class for identifying unsigned min predicates.
1520 struct umin_pred_ty {
matchumin_pred_ty1521   static bool match(ICmpInst::Predicate Pred) {
1522     return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1523   }
1524 };
1525 
1526 /// Helper class for identifying ordered max predicates.
1527 struct ofmax_pred_ty {
matchofmax_pred_ty1528   static bool match(FCmpInst::Predicate Pred) {
1529     return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1530   }
1531 };
1532 
1533 /// Helper class for identifying ordered min predicates.
1534 struct ofmin_pred_ty {
matchofmin_pred_ty1535   static bool match(FCmpInst::Predicate Pred) {
1536     return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1537   }
1538 };
1539 
1540 /// Helper class for identifying unordered max predicates.
1541 struct ufmax_pred_ty {
matchufmax_pred_ty1542   static bool match(FCmpInst::Predicate Pred) {
1543     return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1544   }
1545 };
1546 
1547 /// Helper class for identifying unordered min predicates.
1548 struct ufmin_pred_ty {
matchufmin_pred_ty1549   static bool match(FCmpInst::Predicate Pred) {
1550     return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1551   }
1552 };
1553 
1554 template <typename LHS, typename RHS>
m_SMax(const LHS & L,const RHS & R)1555 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1556                                                              const RHS &R) {
1557   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1558 }
1559 
1560 template <typename LHS, typename RHS>
m_SMin(const LHS & L,const RHS & R)1561 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1562                                                              const RHS &R) {
1563   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1564 }
1565 
1566 template <typename LHS, typename RHS>
m_UMax(const LHS & L,const RHS & R)1567 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1568                                                              const RHS &R) {
1569   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1570 }
1571 
1572 template <typename LHS, typename RHS>
m_UMin(const LHS & L,const RHS & R)1573 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1574                                                              const RHS &R) {
1575   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1576 }
1577 
1578 /// Match an 'ordered' floating point maximum function.
1579 /// Floating point has one special value 'NaN'. Therefore, there is no total
1580 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1581 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1582 /// semantics. In the presence of 'NaN' we have to preserve the original
1583 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1584 ///
1585 ///                         max(L, R)  iff L and R are not NaN
1586 ///  m_OrdFMax(L, R) =      R          iff L or R are NaN
1587 template <typename LHS, typename RHS>
m_OrdFMax(const LHS & L,const RHS & R)1588 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1589                                                                  const RHS &R) {
1590   return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1591 }
1592 
1593 /// Match an 'ordered' floating point minimum function.
1594 /// Floating point has one special value 'NaN'. Therefore, there is no total
1595 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1596 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1597 /// semantics. In the presence of 'NaN' we have to preserve the original
1598 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1599 ///
1600 ///                         min(L, R)  iff L and R are not NaN
1601 ///  m_OrdFMin(L, R) =      R          iff L or R are NaN
1602 template <typename LHS, typename RHS>
m_OrdFMin(const LHS & L,const RHS & R)1603 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1604                                                                  const RHS &R) {
1605   return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1606 }
1607 
1608 /// Match an 'unordered' floating point maximum function.
1609 /// Floating point has one special value 'NaN'. Therefore, there is no total
1610 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1611 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1612 /// semantics. In the presence of 'NaN' we have to preserve the original
1613 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1614 ///
1615 ///                         max(L, R)  iff L and R are not NaN
1616 ///  m_UnordFMax(L, R) =    L          iff L or R are NaN
1617 template <typename LHS, typename RHS>
1618 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
m_UnordFMax(const LHS & L,const RHS & R)1619 m_UnordFMax(const LHS &L, const RHS &R) {
1620   return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1621 }
1622 
1623 /// Match an 'unordered' floating point minimum function.
1624 /// Floating point has one special value 'NaN'. Therefore, there is no total
1625 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1626 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1627 /// semantics. In the presence of 'NaN' we have to preserve the original
1628 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1629 ///
1630 ///                          min(L, R)  iff L and R are not NaN
1631 ///  m_UnordFMin(L, R) =     L          iff L or R are NaN
1632 template <typename LHS, typename RHS>
1633 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
m_UnordFMin(const LHS & L,const RHS & R)1634 m_UnordFMin(const LHS &L, const RHS &R) {
1635   return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1636 }
1637 
1638 //===----------------------------------------------------------------------===//
1639 // Matchers for overflow check patterns: e.g. (a + b) u< a
1640 //
1641 
1642 template <typename LHS_t, typename RHS_t, typename Sum_t>
1643 struct UAddWithOverflow_match {
1644   LHS_t L;
1645   RHS_t R;
1646   Sum_t S;
1647 
UAddWithOverflow_matchUAddWithOverflow_match1648   UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1649       : L(L), R(R), S(S) {}
1650 
matchUAddWithOverflow_match1651   template <typename OpTy> bool match(OpTy *V) {
1652     Value *ICmpLHS, *ICmpRHS;
1653     ICmpInst::Predicate Pred;
1654     if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1655       return false;
1656 
1657     Value *AddLHS, *AddRHS;
1658     auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1659 
1660     // (a + b) u< a, (a + b) u< b
1661     if (Pred == ICmpInst::ICMP_ULT)
1662       if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1663         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1664 
1665     // a >u (a + b), b >u (a + b)
1666     if (Pred == ICmpInst::ICMP_UGT)
1667       if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1668         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1669 
1670     // Match special-case for increment-by-1.
1671     if (Pred == ICmpInst::ICMP_EQ) {
1672       // (a + 1) == 0
1673       // (1 + a) == 0
1674       if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
1675           (m_One().match(AddLHS) || m_One().match(AddRHS)))
1676         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1677       // 0 == (a + 1)
1678       // 0 == (1 + a)
1679       if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
1680           (m_One().match(AddLHS) || m_One().match(AddRHS)))
1681         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1682     }
1683 
1684     return false;
1685   }
1686 };
1687 
1688 /// Match an icmp instruction checking for unsigned overflow on addition.
1689 ///
1690 /// S is matched to the addition whose result is being checked for overflow, and
1691 /// L and R are matched to the LHS and RHS of S.
1692 template <typename LHS_t, typename RHS_t, typename Sum_t>
1693 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
m_UAddWithOverflow(const LHS_t & L,const RHS_t & R,const Sum_t & S)1694 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1695   return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
1696 }
1697 
1698 template <typename Opnd_t> struct Argument_match {
1699   unsigned OpI;
1700   Opnd_t Val;
1701 
Argument_matchArgument_match1702   Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1703 
matchArgument_match1704   template <typename OpTy> bool match(OpTy *V) {
1705     // FIXME: Should likely be switched to use `CallBase`.
1706     if (const auto *CI = dyn_cast<CallInst>(V))
1707       return Val.match(CI->getArgOperand(OpI));
1708     return false;
1709   }
1710 };
1711 
1712 /// Match an argument.
1713 template <unsigned OpI, typename Opnd_t>
m_Argument(const Opnd_t & Op)1714 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1715   return Argument_match<Opnd_t>(OpI, Op);
1716 }
1717 
1718 /// Intrinsic matchers.
1719 struct IntrinsicID_match {
1720   unsigned ID;
1721 
IntrinsicID_matchIntrinsicID_match1722   IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1723 
matchIntrinsicID_match1724   template <typename OpTy> bool match(OpTy *V) {
1725     if (const auto *CI = dyn_cast<CallInst>(V))
1726       if (const auto *F = CI->getCalledFunction())
1727         return F->getIntrinsicID() == ID;
1728     return false;
1729   }
1730 };
1731 
1732 /// Intrinsic matches are combinations of ID matchers, and argument
1733 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1734 /// them with lower arity matchers. Here's some convenient typedefs for up to
1735 /// several arguments, and more can be added as needed
1736 template <typename T0 = void, typename T1 = void, typename T2 = void,
1737           typename T3 = void, typename T4 = void, typename T5 = void,
1738           typename T6 = void, typename T7 = void, typename T8 = void,
1739           typename T9 = void, typename T10 = void>
1740 struct m_Intrinsic_Ty;
1741 template <typename T0> struct m_Intrinsic_Ty<T0> {
1742   using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
1743 };
1744 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1745   using Ty =
1746       match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
1747 };
1748 template <typename T0, typename T1, typename T2>
1749 struct m_Intrinsic_Ty<T0, T1, T2> {
1750   using Ty =
1751       match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1752                         Argument_match<T2>>;
1753 };
1754 template <typename T0, typename T1, typename T2, typename T3>
1755 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1756   using Ty =
1757       match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1758                         Argument_match<T3>>;
1759 };
1760 
1761 template <typename T0, typename T1, typename T2, typename T3, typename T4>
1762 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
1763   using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty,
1764                                Argument_match<T4>>;
1765 };
1766 
1767 /// Match intrinsic calls like this:
1768 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1769 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1770   return IntrinsicID_match(IntrID);
1771 }
1772 
1773 template <Intrinsic::ID IntrID, typename T0>
1774 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1775   return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1776 }
1777 
1778 template <Intrinsic::ID IntrID, typename T0, typename T1>
1779 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1780                                                        const T1 &Op1) {
1781   return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1782 }
1783 
1784 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1785 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1786 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1787   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1788 }
1789 
1790 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1791           typename T3>
1792 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1793 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1794   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1795 }
1796 
1797 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1798           typename T3, typename T4>
1799 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty
1800 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
1801             const T4 &Op4) {
1802   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
1803                       m_Argument<4>(Op4));
1804 }
1805 
1806 // Helper intrinsic matching specializations.
1807 template <typename Opnd0>
1808 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1809   return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1810 }
1811 
1812 template <typename Opnd0>
1813 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1814   return m_Intrinsic<Intrinsic::bswap>(Op0);
1815 }
1816 
1817 template <typename Opnd0>
1818 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
1819   return m_Intrinsic<Intrinsic::fabs>(Op0);
1820 }
1821 
1822 template <typename Opnd0>
1823 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
1824   return m_Intrinsic<Intrinsic::canonicalize>(Op0);
1825 }
1826 
1827 template <typename Opnd0, typename Opnd1>
1828 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1829                                                         const Opnd1 &Op1) {
1830   return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1831 }
1832 
1833 template <typename Opnd0, typename Opnd1>
1834 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1835                                                         const Opnd1 &Op1) {
1836   return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1837 }
1838 
1839 //===----------------------------------------------------------------------===//
1840 // Matchers for two-operands operators with the operators in either order
1841 //
1842 
1843 /// Matches a BinaryOperator with LHS and RHS in either order.
1844 template <typename LHS, typename RHS>
1845 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
1846   return AnyBinaryOp_match<LHS, RHS, true>(L, R);
1847 }
1848 
1849 /// Matches an ICmp with a predicate over LHS and RHS in either order.
1850 /// Does not swap the predicate.
1851 template <typename LHS, typename RHS>
1852 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
1853 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1854   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
1855                                                                        R);
1856 }
1857 
1858 /// Matches a Add with LHS and RHS in either order.
1859 template <typename LHS, typename RHS>
1860 inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
1861                                                                 const RHS &R) {
1862   return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
1863 }
1864 
1865 /// Matches a Mul with LHS and RHS in either order.
1866 template <typename LHS, typename RHS>
1867 inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
1868                                                                 const RHS &R) {
1869   return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
1870 }
1871 
1872 /// Matches an And with LHS and RHS in either order.
1873 template <typename LHS, typename RHS>
1874 inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
1875                                                                 const RHS &R) {
1876   return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
1877 }
1878 
1879 /// Matches an Or with LHS and RHS in either order.
1880 template <typename LHS, typename RHS>
1881 inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
1882                                                               const RHS &R) {
1883   return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
1884 }
1885 
1886 /// Matches an Xor with LHS and RHS in either order.
1887 template <typename LHS, typename RHS>
1888 inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
1889                                                                 const RHS &R) {
1890   return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
1891 }
1892 
1893 /// Matches a 'Neg' as 'sub 0, V'.
1894 template <typename ValTy>
1895 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
1896 m_Neg(const ValTy &V) {
1897   return m_Sub(m_ZeroInt(), V);
1898 }
1899 
1900 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1901 template <typename ValTy>
1902 inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1903 m_Not(const ValTy &V) {
1904   return m_c_Xor(V, m_AllOnes());
1905 }
1906 
1907 /// Matches an SMin with LHS and RHS in either order.
1908 template <typename LHS, typename RHS>
1909 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
1910 m_c_SMin(const LHS &L, const RHS &R) {
1911   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
1912 }
1913 /// Matches an SMax with LHS and RHS in either order.
1914 template <typename LHS, typename RHS>
1915 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
1916 m_c_SMax(const LHS &L, const RHS &R) {
1917   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
1918 }
1919 /// Matches a UMin with LHS and RHS in either order.
1920 template <typename LHS, typename RHS>
1921 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
1922 m_c_UMin(const LHS &L, const RHS &R) {
1923   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
1924 }
1925 /// Matches a UMax with LHS and RHS in either order.
1926 template <typename LHS, typename RHS>
1927 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
1928 m_c_UMax(const LHS &L, const RHS &R) {
1929   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
1930 }
1931 
1932 /// Matches FAdd with LHS and RHS in either order.
1933 template <typename LHS, typename RHS>
1934 inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
1935 m_c_FAdd(const LHS &L, const RHS &R) {
1936   return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
1937 }
1938 
1939 /// Matches FMul with LHS and RHS in either order.
1940 template <typename LHS, typename RHS>
1941 inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
1942 m_c_FMul(const LHS &L, const RHS &R) {
1943   return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
1944 }
1945 
1946 template <typename Opnd_t> struct Signum_match {
1947   Opnd_t Val;
1948   Signum_match(const Opnd_t &V) : Val(V) {}
1949 
1950   template <typename OpTy> bool match(OpTy *V) {
1951     unsigned TypeSize = V->getType()->getScalarSizeInBits();
1952     if (TypeSize == 0)
1953       return false;
1954 
1955     unsigned ShiftWidth = TypeSize - 1;
1956     Value *OpL = nullptr, *OpR = nullptr;
1957 
1958     // This is the representation of signum we match:
1959     //
1960     //  signum(x) == (x >> 63) | (-x >>u 63)
1961     //
1962     // An i1 value is its own signum, so it's correct to match
1963     //
1964     //  signum(x) == (x >> 0)  | (-x >>u 0)
1965     //
1966     // for i1 values.
1967 
1968     auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
1969     auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
1970     auto Signum = m_Or(LHS, RHS);
1971 
1972     return Signum.match(V) && OpL == OpR && Val.match(OpL);
1973   }
1974 };
1975 
1976 /// Matches a signum pattern.
1977 ///
1978 /// signum(x) =
1979 ///      x >  0  ->  1
1980 ///      x == 0  ->  0
1981 ///      x <  0  -> -1
1982 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1983   return Signum_match<Val_t>(V);
1984 }
1985 
1986 template <int Ind, typename Opnd_t> struct ExtractValue_match {
1987   Opnd_t Val;
1988   ExtractValue_match(const Opnd_t &V) : Val(V) {}
1989 
1990   template <typename OpTy> bool match(OpTy *V) {
1991     if (auto *I = dyn_cast<ExtractValueInst>(V))
1992       return I->getNumIndices() == 1 && I->getIndices()[0] == Ind &&
1993              Val.match(I->getAggregateOperand());
1994     return false;
1995   }
1996 };
1997 
1998 /// Match a single index ExtractValue instruction.
1999 /// For example m_ExtractValue<1>(...)
2000 template <int Ind, typename Val_t>
2001 inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) {
2002   return ExtractValue_match<Ind, Val_t>(V);
2003 }
2004 
2005 } // end namespace PatternMatch
2006 } // end namespace llvm
2007 
2008 #endif // LLVM_IR_PATTERNMATCH_H
2009