LLVM  9.0.0svn
PatternMatch.h
Go to the documentation of this file.
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/Intrinsics.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/Value.h"
41 #include "llvm/Support/Casting.h"
42 #include <cstdint>
43 
44 namespace llvm {
45 namespace PatternMatch {
46 
47 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
48  return const_cast<Pattern &>(P).match(V);
49 }
50 
51 template <typename SubPattern_t> struct OneUse_match {
52  SubPattern_t SubPattern;
53 
54  OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
55 
56  template <typename OpTy> bool match(OpTy *V) {
57  return V->hasOneUse() && SubPattern.match(V);
58  }
59 };
60 
61 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
62  return SubPattern;
63 }
64 
65 template <typename Class> struct class_match {
66  template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
67 };
68 
69 /// Match an arbitrary value and ignore it.
71 
72 /// Match an arbitrary binary operation and ignore it.
75 }
76 
77 /// Matches any compare instruction and ignore it.
79 
80 /// Match an arbitrary ConstantInt and ignore it.
82  return class_match<ConstantInt>();
83 }
84 
85 /// Match an arbitrary undef constant.
87 
88 /// Match an arbitrary Constant and ignore it.
90 
91 /// Matching combinators
92 template <typename LTy, typename RTy> struct match_combine_or {
93  LTy L;
94  RTy R;
95 
96  match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
97 
98  template <typename ITy> bool match(ITy *V) {
99  if (L.match(V))
100  return true;
101  if (R.match(V))
102  return true;
103  return false;
104  }
105 };
106 
107 template <typename LTy, typename RTy> struct match_combine_and {
108  LTy L;
109  RTy R;
110 
111  match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
112 
113  template <typename ITy> bool match(ITy *V) {
114  if (L.match(V))
115  if (R.match(V))
116  return true;
117  return false;
118  }
119 };
120 
121 /// Combine two pattern matchers matching L || R
122 template <typename LTy, typename RTy>
123 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
124  return match_combine_or<LTy, RTy>(L, R);
125 }
126 
127 /// Combine two pattern matchers matching L && R
128 template <typename LTy, typename RTy>
129 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
130  return match_combine_and<LTy, RTy>(L, R);
131 }
132 
133 struct apint_match {
134  const APInt *&Res;
135 
136  apint_match(const APInt *&R) : Res(R) {}
137 
138  template <typename ITy> bool match(ITy *V) {
139  if (auto *CI = dyn_cast<ConstantInt>(V)) {
140  Res = &CI->getValue();
141  return true;
142  }
143  if (V->getType()->isVectorTy())
144  if (const auto *C = dyn_cast<Constant>(V))
145  if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
146  Res = &CI->getValue();
147  return true;
148  }
149  return false;
150  }
151 };
152 // Either constexpr if or renaming ConstantFP::getValueAPF to
153 // ConstantFP::getValue is needed to do it via single template
154 // function for both apint/apfloat.
156  const APFloat *&Res;
157  apfloat_match(const APFloat *&R) : Res(R) {}
158  template <typename ITy> bool match(ITy *V) {
159  if (auto *CI = dyn_cast<ConstantFP>(V)) {
160  Res = &CI->getValueAPF();
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<ConstantFP>(C->getSplatValue())) {
166  Res = &CI->getValueAPF();
167  return true;
168  }
169  return false;
170  }
171 };
172 
173 /// Match a ConstantInt or splatted ConstantVector, binding the
174 /// specified pointer to the contained APInt.
175 inline apint_match m_APInt(const APInt *&Res) { return Res; }
176 
177 /// Match a ConstantFP or splatted ConstantVector, binding the
178 /// specified pointer to the contained APFloat.
179 inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
180 
181 template <int64_t Val> struct constantint_match {
182  template <typename ITy> bool match(ITy *V) {
183  if (const auto *CI = dyn_cast<ConstantInt>(V)) {
184  const APInt &CIV = CI->getValue();
185  if (Val >= 0)
186  return CIV == static_cast<uint64_t>(Val);
187  // If Val is negative, and CI is shorter than it, truncate to the right
188  // number of bits. If it is larger, then we have to sign extend. Just
189  // compare their negated values.
190  return -CIV == -Val;
191  }
192  return false;
193  }
194 };
195 
196 /// Match a ConstantInt with a specific value.
197 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
198  return constantint_match<Val>();
199 }
200 
201 /// This helper class is used to match scalar and vector integer constants that
202 /// satisfy a specified predicate.
203 /// For vector constants, undefined elements are ignored.
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 
213  // Non-splat vector constant: check each element for a match.
214  unsigned NumElts = V->getType()->getVectorNumElements();
215  assert(NumElts != 0 && "Constant vector with no elements?");
216  bool HasNonUndefElements = false;
217  for (unsigned i = 0; i != NumElts; ++i) {
218  Constant *Elt = C->getAggregateElement(i);
219  if (!Elt)
220  return false;
221  if (isa<UndefValue>(Elt))
222  continue;
223  auto *CI = dyn_cast<ConstantInt>(Elt);
224  if (!CI || !this->isValue(CI->getValue()))
225  return false;
226  HasNonUndefElements = true;
227  }
228  return HasNonUndefElements;
229  }
230  }
231  return false;
232  }
233 };
234 
235 /// This helper class is used to match scalar and vector constants that
236 /// satisfy a specified predicate, and bind them to an APInt.
237 template <typename Predicate> struct api_pred_ty : public Predicate {
238  const APInt *&Res;
239 
240  api_pred_ty(const APInt *&R) : Res(R) {}
241 
242  template <typename ITy> bool match(ITy *V) {
243  if (const auto *CI = dyn_cast<ConstantInt>(V))
244  if (this->isValue(CI->getValue())) {
245  Res = &CI->getValue();
246  return true;
247  }
248  if (V->getType()->isVectorTy())
249  if (const auto *C = dyn_cast<Constant>(V))
250  if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
251  if (this->isValue(CI->getValue())) {
252  Res = &CI->getValue();
253  return true;
254  }
255 
256  return false;
257  }
258 };
259 
260 /// This helper class is used to match scalar and vector floating-point
261 /// constants that satisfy a specified predicate.
262 /// For vector constants, undefined elements are ignored.
263 template <typename Predicate> struct cstfp_pred_ty : public Predicate {
264  template <typename ITy> bool match(ITy *V) {
265  if (const auto *CF = dyn_cast<ConstantFP>(V))
266  return this->isValue(CF->getValueAPF());
267  if (V->getType()->isVectorTy()) {
268  if (const auto *C = dyn_cast<Constant>(V)) {
269  if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
270  return this->isValue(CF->getValueAPF());
271 
272  // Non-splat vector constant: check each element for a match.
273  unsigned NumElts = V->getType()->getVectorNumElements();
274  assert(NumElts != 0 && "Constant vector with no elements?");
275  bool HasNonUndefElements = false;
276  for (unsigned i = 0; i != NumElts; ++i) {
277  Constant *Elt = C->getAggregateElement(i);
278  if (!Elt)
279  return false;
280  if (isa<UndefValue>(Elt))
281  continue;
282  auto *CF = dyn_cast<ConstantFP>(Elt);
283  if (!CF || !this->isValue(CF->getValueAPF()))
284  return false;
285  HasNonUndefElements = true;
286  }
287  return HasNonUndefElements;
288  }
289  }
290  return false;
291  }
292 };
293 
294 ///////////////////////////////////////////////////////////////////////////////
295 //
296 // Encapsulate constant value queries for use in templated predicate matchers.
297 // This allows checking if constants match using compound predicates and works
298 // with vector constants, possibly with relaxed constraints. For example, ignore
299 // undef values.
300 //
301 ///////////////////////////////////////////////////////////////////////////////
302 
303 struct is_all_ones {
304  bool isValue(const APInt &C) { return C.isAllOnesValue(); }
305 };
306 /// Match an integer or vector with all bits set.
307 /// For vectors, this includes constants with undefined elements.
309  return cst_pred_ty<is_all_ones>();
310 }
311 
313  bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
314 };
315 /// Match an integer or vector with values having all bits except for the high
316 /// bit set (0x7f...).
317 /// For vectors, this includes constants with undefined elements.
320 }
322  return V;
323 }
324 
325 struct is_negative {
326  bool isValue(const APInt &C) { return C.isNegative(); }
327 };
328 /// Match an integer or vector of negative values.
329 /// For vectors, this includes constants with undefined elements.
331  return cst_pred_ty<is_negative>();
332 }
334  return V;
335 }
336 
338  bool isValue(const APInt &C) { return C.isNonNegative(); }
339 };
340 /// Match an integer or vector of nonnegative values.
341 /// For vectors, this includes constants with undefined elements.
344 }
346  return V;
347 }
348 
349 struct is_one {
350  bool isValue(const APInt &C) { return C.isOneValue(); }
351 };
352 /// Match an integer 1 or a vector with all elements equal to 1.
353 /// For vectors, this includes constants with undefined elements.
355  return cst_pred_ty<is_one>();
356 }
357 
358 struct is_zero_int {
359  bool isValue(const APInt &C) { return C.isNullValue(); }
360 };
361 /// Match an integer 0 or a vector with all elements equal to 0.
362 /// For vectors, this includes constants with undefined elements.
364  return cst_pred_ty<is_zero_int>();
365 }
366 
367 struct is_zero {
368  template <typename ITy> bool match(ITy *V) {
369  auto *C = dyn_cast<Constant>(V);
370  return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
371  }
372 };
373 /// Match any null constant or a vector with all elements equal to 0.
374 /// For vectors, this includes constants with undefined elements.
375 inline is_zero m_Zero() {
376  return is_zero();
377 }
378 
379 struct is_power2 {
380  bool isValue(const APInt &C) { return C.isPowerOf2(); }
381 };
382 /// Match an integer or vector power-of-2.
383 /// For vectors, this includes constants with undefined elements.
385  return cst_pred_ty<is_power2>();
386 }
388  return V;
389 }
390 
392  bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
393 };
394 /// Match an integer or vector of 0 or power-of-2 values.
395 /// For vectors, this includes constants with undefined elements.
398 }
400  return V;
401 }
402 
403 struct is_sign_mask {
404  bool isValue(const APInt &C) { return C.isSignMask(); }
405 };
406 /// Match an integer or vector with only the sign bit(s) set.
407 /// For vectors, this includes constants with undefined elements.
409  return cst_pred_ty<is_sign_mask>();
410 }
411 
413  bool isValue(const APInt &C) { return C.isMask(); }
414 };
415 /// Match an integer or vector with only the low bit(s) set.
416 /// For vectors, this includes constants with undefined elements.
419 }
420 
421 struct is_nan {
422  bool isValue(const APFloat &C) { return C.isNaN(); }
423 };
424 /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
425 /// For vectors, this includes constants with undefined elements.
427  return cstfp_pred_ty<is_nan>();
428 }
429 
431  bool isValue(const APFloat &C) { return C.isZero(); }
432 };
433 /// Match a floating-point negative zero or positive zero.
434 /// For vectors, this includes constants with undefined elements.
437 }
438 
440  bool isValue(const APFloat &C) { return C.isPosZero(); }
441 };
442 /// Match a floating-point positive zero.
443 /// For vectors, this includes constants with undefined elements.
446 }
447 
449  bool isValue(const APFloat &C) { return C.isNegZero(); }
450 };
451 /// Match a floating-point negative zero.
452 /// For vectors, this includes constants with undefined elements.
455 }
456 
457 ///////////////////////////////////////////////////////////////////////////////
458 
459 template <typename Class> struct bind_ty {
460  Class *&VR;
461 
462  bind_ty(Class *&V) : VR(V) {}
463 
464  template <typename ITy> bool match(ITy *V) {
465  if (auto *CV = dyn_cast<Class>(V)) {
466  VR = CV;
467  return true;
468  }
469  return false;
470  }
471 };
472 
473 /// Match a value, capturing it if we match.
474 inline bind_ty<Value> m_Value(Value *&V) { return V; }
475 inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
476 
477 /// Match an instruction, capturing it if we match.
479 /// Match a binary operator, capturing it if we match.
481 
482 /// Match a ConstantInt, capturing the value if we match.
483 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
484 
485 /// Match a Constant, capturing the value if we match.
486 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
487 
488 /// Match a ConstantFP, capturing the value if we match.
490 
491 /// Match a specified Value*.
493  const Value *Val;
494 
495  specificval_ty(const Value *V) : Val(V) {}
496 
497  template <typename ITy> bool match(ITy *V) { return V == Val; }
498 };
499 
500 /// Match if we have a specific specified value.
501 inline specificval_ty m_Specific(const Value *V) { return V; }
502 
503 /// Stores a reference to the Value *, not the Value * itself,
504 /// thus can be used in commutative matchers.
505 template <typename Class> struct deferredval_ty {
506  Class *const &Val;
507 
508  deferredval_ty(Class *const &V) : Val(V) {}
509 
510  template <typename ITy> bool match(ITy *const V) { return V == Val; }
511 };
512 
513 /// A commutative-friendly version of m_Specific().
514 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
516  return V;
517 }
518 
519 /// Match a specified floating point value or vector of all elements of
520 /// that value.
522  double Val;
523 
524  specific_fpval(double V) : Val(V) {}
525 
526  template <typename ITy> bool match(ITy *V) {
527  if (const auto *CFP = dyn_cast<ConstantFP>(V))
528  return CFP->isExactlyValue(Val);
529  if (V->getType()->isVectorTy())
530  if (const auto *C = dyn_cast<Constant>(V))
531  if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
532  return CFP->isExactlyValue(Val);
533  return false;
534  }
535 };
536 
537 /// Match a specific floating point value or vector with all elements
538 /// equal to the value.
539 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
540 
541 /// Match a float 1.0 or vector with all elements equal to 1.0.
542 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
543 
545  uint64_t &VR;
546 
547  bind_const_intval_ty(uint64_t &V) : VR(V) {}
548 
549  template <typename ITy> bool match(ITy *V) {
550  if (const auto *CV = dyn_cast<ConstantInt>(V))
551  if (CV->getValue().ule(UINT64_MAX)) {
552  VR = CV->getZExtValue();
553  return true;
554  }
555  return false;
556  }
557 };
558 
559 /// Match a specified integer value or vector of all elements of that
560 // value.
562  uint64_t Val;
563 
564  specific_intval(uint64_t V) : Val(V) {}
565 
566  template <typename ITy> bool match(ITy *V) {
567  const auto *CI = dyn_cast<ConstantInt>(V);
568  if (!CI && V->getType()->isVectorTy())
569  if (const auto *C = dyn_cast<Constant>(V))
570  CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
571 
572  return CI && CI->getValue() == Val;
573  }
574 };
575 
576 /// Match a specific integer value or vector with all elements equal to
577 /// the value.
578 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
579 
580 /// Match a ConstantInt and bind to its value. This does not match
581 /// ConstantInts wider than 64-bits.
582 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
583 
584 //===----------------------------------------------------------------------===//
585 // Matcher for any binary operator.
586 //
587 template <typename LHS_t, typename RHS_t, bool Commutable = false>
589  LHS_t L;
590  RHS_t R;
591 
592  // The evaluation order is always stable, regardless of Commutability.
593  // The LHS is always matched first.
594  AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
595 
596  template <typename OpTy> bool match(OpTy *V) {
597  if (auto *I = dyn_cast<BinaryOperator>(V))
598  return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
599  (Commutable && L.match(I->getOperand(1)) &&
600  R.match(I->getOperand(0)));
601  return false;
602  }
603 };
604 
605 template <typename LHS, typename RHS>
606 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
607  return AnyBinaryOp_match<LHS, RHS>(L, R);
608 }
609 
610 //===----------------------------------------------------------------------===//
611 // Matchers for specific binary operators.
612 //
613 
614 template <typename LHS_t, typename RHS_t, unsigned Opcode,
615  bool Commutable = false>
617  LHS_t L;
618  RHS_t R;
619 
620  // The evaluation order is always stable, regardless of Commutability.
621  // The LHS is always matched first.
622  BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
623 
624  template <typename OpTy> bool match(OpTy *V) {
625  if (V->getValueID() == Value::InstructionVal + Opcode) {
626  auto *I = cast<BinaryOperator>(V);
627  return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
628  (Commutable && L.match(I->getOperand(1)) &&
629  R.match(I->getOperand(0)));
630  }
631  if (auto *CE = dyn_cast<ConstantExpr>(V))
632  return CE->getOpcode() == Opcode &&
633  ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
634  (Commutable && L.match(CE->getOperand(1)) &&
635  R.match(CE->getOperand(0))));
636  return false;
637  }
638 };
639 
640 template <typename LHS, typename RHS>
642  const RHS &R) {
644 }
645 
646 template <typename LHS, typename RHS>
648  const RHS &R) {
650 }
651 
652 template <typename LHS, typename RHS>
654  const RHS &R) {
656 }
657 
658 template <typename LHS, typename RHS>
660  const RHS &R) {
662 }
663 
664 template <typename Op_t> struct FNeg_match {
665  Op_t X;
666 
667  FNeg_match(const Op_t &Op) : X(Op) {}
668  template <typename OpTy> bool match(OpTy *V) {
669  auto *FPMO = dyn_cast<FPMathOperator>(V);
670  if (!FPMO) return false;
671 
672  if (FPMO->getOpcode() == Instruction::FNeg)
673  return X.match(FPMO->getOperand(0));
674 
675  if (FPMO->getOpcode() == Instruction::FSub) {
676  if (FPMO->hasNoSignedZeros()) {
677  // With 'nsz', any zero goes.
678  if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
679  return false;
680  } else {
681  // Without 'nsz', we need fsub -0.0, X exactly.
682  if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
683  return false;
684  }
685 
686  return X.match(FPMO->getOperand(1));
687  }
688 
689  return false;
690  }
691 };
692 
693 /// Match 'fneg X' as 'fsub -0.0, X'.
694 template <typename OpTy>
695 inline FNeg_match<OpTy>
696 m_FNeg(const OpTy &X) {
697  return FNeg_match<OpTy>(X);
698 }
699 
700 /// Match 'fneg X' as 'fsub +-0.0, X'.
701 template <typename RHS>
702 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
703 m_FNegNSZ(const RHS &X) {
704  return m_FSub(m_AnyZeroFP(), X);
705 }
706 
707 template <typename LHS, typename RHS>
709  const RHS &R) {
711 }
712 
713 template <typename LHS, typename RHS>
715  const RHS &R) {
717 }
718 
719 template <typename LHS, typename RHS>
721  const RHS &R) {
723 }
724 
725 template <typename LHS, typename RHS>
727  const RHS &R) {
729 }
730 
731 template <typename LHS, typename RHS>
733  const RHS &R) {
735 }
736 
737 template <typename LHS, typename RHS>
739  const RHS &R) {
741 }
742 
743 template <typename LHS, typename RHS>
745  const RHS &R) {
747 }
748 
749 template <typename LHS, typename RHS>
751  const RHS &R) {
753 }
754 
755 template <typename LHS, typename RHS>
757  const RHS &R) {
759 }
760 
761 template <typename LHS, typename RHS>
763  const RHS &R) {
765 }
766 
767 template <typename LHS, typename RHS>
769  const RHS &R) {
771 }
772 
773 template <typename LHS, typename RHS>
775  const RHS &R) {
777 }
778 
779 template <typename LHS, typename RHS>
781  const RHS &R) {
783 }
784 
785 template <typename LHS, typename RHS>
787  const RHS &R) {
789 }
790 
791 template <typename LHS_t, typename RHS_t, unsigned Opcode,
792  unsigned WrapFlags = 0>
794  LHS_t L;
795  RHS_t R;
796 
797  OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
798  : L(LHS), R(RHS) {}
799 
800  template <typename OpTy> bool match(OpTy *V) {
801  if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
802  if (Op->getOpcode() != Opcode)
803  return false;
805  !Op->hasNoUnsignedWrap())
806  return false;
807  if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
808  !Op->hasNoSignedWrap())
809  return false;
810  return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
811  }
812  return false;
813  }
814 };
815 
816 template <typename LHS, typename RHS>
819 m_NSWAdd(const LHS &L, const RHS &R) {
822  L, R);
823 }
824 template <typename LHS, typename RHS>
825 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
827 m_NSWSub(const LHS &L, const RHS &R) {
828  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
830  L, R);
831 }
832 template <typename LHS, typename RHS>
833 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
835 m_NSWMul(const LHS &L, const RHS &R) {
836  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
838  L, R);
839 }
840 template <typename LHS, typename RHS>
841 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
843 m_NSWShl(const LHS &L, const RHS &R) {
844  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
846  L, R);
847 }
848 
849 template <typename LHS, typename RHS>
852 m_NUWAdd(const LHS &L, const RHS &R) {
855  L, R);
856 }
857 template <typename LHS, typename RHS>
858 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
860 m_NUWSub(const LHS &L, const RHS &R) {
861  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
863  L, R);
864 }
865 template <typename LHS, typename RHS>
866 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
868 m_NUWMul(const LHS &L, const RHS &R) {
869  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
871  L, R);
872 }
873 template <typename LHS, typename RHS>
874 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
876 m_NUWShl(const LHS &L, const RHS &R) {
877  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
879  L, R);
880 }
881 
882 //===----------------------------------------------------------------------===//
883 // Class that matches a group of binary opcodes.
884 //
885 template <typename LHS_t, typename RHS_t, typename Predicate>
887  LHS_t L;
888  RHS_t R;
889 
890  BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
891 
892  template <typename OpTy> bool match(OpTy *V) {
893  if (auto *I = dyn_cast<Instruction>(V))
894  return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
895  R.match(I->getOperand(1));
896  if (auto *CE = dyn_cast<ConstantExpr>(V))
897  return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
898  R.match(CE->getOperand(1));
899  return false;
900  }
901 };
902 
903 struct is_shift_op {
904  bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
905 };
906 
908  bool isOpType(unsigned Opcode) {
909  return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
910  }
911 };
912 
914  bool isOpType(unsigned Opcode) {
915  return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
916  }
917 };
918 
920  bool isOpType(unsigned Opcode) {
921  return Instruction::isBitwiseLogicOp(Opcode);
922  }
923 };
924 
925 struct is_idiv_op {
926  bool isOpType(unsigned Opcode) {
927  return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
928  }
929 };
930 
931 /// Matches shift operations.
932 template <typename LHS, typename RHS>
934  const RHS &R) {
936 }
937 
938 /// Matches logical shift operations.
939 template <typename LHS, typename RHS>
941  const RHS &R) {
943 }
944 
945 /// Matches logical shift operations.
946 template <typename LHS, typename RHS>
948 m_LogicalShift(const LHS &L, const RHS &R) {
950 }
951 
952 /// Matches bitwise logic operations.
953 template <typename LHS, typename RHS>
955 m_BitwiseLogic(const LHS &L, const RHS &R) {
957 }
958 
959 /// Matches integer division operations.
960 template <typename LHS, typename RHS>
962  const RHS &R) {
964 }
965 
966 //===----------------------------------------------------------------------===//
967 // Class that matches exact binary ops.
968 //
969 template <typename SubPattern_t> struct Exact_match {
970  SubPattern_t SubPattern;
971 
972  Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
973 
974  template <typename OpTy> bool match(OpTy *V) {
975  if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
976  return PEO->isExact() && SubPattern.match(V);
977  return false;
978  }
979 };
980 
981 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
982  return SubPattern;
983 }
984 
985 //===----------------------------------------------------------------------===//
986 // Matchers for CmpInst classes
987 //
988 
989 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
990  bool Commutable = false>
992  PredicateTy &Predicate;
993  LHS_t L;
994  RHS_t R;
995 
996  // The evaluation order is always stable, regardless of Commutability.
997  // The LHS is always matched first.
998  CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
999  : Predicate(Pred), L(LHS), R(RHS) {}
1000 
1001  template <typename OpTy> bool match(OpTy *V) {
1002  if (auto *I = dyn_cast<Class>(V))
1003  if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1004  (Commutable && L.match(I->getOperand(1)) &&
1005  R.match(I->getOperand(0)))) {
1006  Predicate = I->getPredicate();
1007  return true;
1008  }
1009  return false;
1010  }
1011 };
1012 
1013 template <typename LHS, typename RHS>
1015 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1017 }
1018 
1019 template <typename LHS, typename RHS>
1021 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1023 }
1024 
1025 template <typename LHS, typename RHS>
1027 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1029 }
1030 
1031 //===----------------------------------------------------------------------===//
1032 // Matchers for instructions with a given opcode and number of operands.
1033 //
1034 
1035 /// Matches instructions with Opcode and three operands.
1036 template <typename T0, unsigned Opcode> struct OneOps_match {
1037  T0 Op1;
1038 
1039  OneOps_match(const T0 &Op1) : Op1(Op1) {}
1040 
1041  template <typename OpTy> bool match(OpTy *V) {
1042  if (V->getValueID() == Value::InstructionVal + Opcode) {
1043  auto *I = cast<Instruction>(V);
1044  return Op1.match(I->getOperand(0));
1045  }
1046  return false;
1047  }
1048 };
1049 
1050 /// Matches instructions with Opcode and three operands.
1051 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1052  T0 Op1;
1054 
1055  TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1056 
1057  template <typename OpTy> bool match(OpTy *V) {
1058  if (V->getValueID() == Value::InstructionVal + Opcode) {
1059  auto *I = cast<Instruction>(V);
1060  return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1061  }
1062  return false;
1063  }
1064 };
1065 
1066 /// Matches instructions with Opcode and three operands.
1067 template <typename T0, typename T1, typename T2, unsigned Opcode>
1069  T0 Op1;
1071  T2 Op3;
1072 
1073  ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1074  : Op1(Op1), Op2(Op2), Op3(Op3) {}
1075 
1076  template <typename OpTy> bool match(OpTy *V) {
1077  if (V->getValueID() == Value::InstructionVal + Opcode) {
1078  auto *I = cast<Instruction>(V);
1079  return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1080  Op3.match(I->getOperand(2));
1081  }
1082  return false;
1083  }
1084 };
1085 
1086 /// Matches SelectInst.
1087 template <typename Cond, typename LHS, typename RHS>
1089 m_Select(const Cond &C, const LHS &L, const RHS &R) {
1091 }
1092 
1093 /// This matches a select of two constants, e.g.:
1094 /// m_SelectCst<-1, 0>(m_Value(V))
1095 template <int64_t L, int64_t R, typename Cond>
1098 m_SelectCst(const Cond &C) {
1099  return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1100 }
1101 
1102 /// Matches InsertElementInst.
1103 template <typename Val_t, typename Elt_t, typename Idx_t>
1105 m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1107  Val, Elt, Idx);
1108 }
1109 
1110 /// Matches ExtractElementInst.
1111 template <typename Val_t, typename Idx_t>
1113 m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
1115 }
1116 
1117 /// Matches ShuffleVectorInst.
1118 template <typename V1_t, typename V2_t, typename Mask_t>
1120 m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
1122  m);
1123 }
1124 
1125 /// Matches LoadInst.
1126 template <typename OpTy>
1129 }
1130 
1131 /// Matches StoreInst.
1132 template <typename ValueOpTy, typename PointerOpTy>
1134 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1136  PointerOp);
1137 }
1138 
1139 //===----------------------------------------------------------------------===//
1140 // Matchers for CastInst classes
1141 //
1142 
1143 template <typename Op_t, unsigned Opcode> struct CastClass_match {
1144  Op_t Op;
1145 
1146  CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1147 
1148  template <typename OpTy> bool match(OpTy *V) {
1149  if (auto *O = dyn_cast<Operator>(V))
1150  return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1151  return false;
1152  }
1153 };
1154 
1155 /// Matches BitCast.
1156 template <typename OpTy>
1159 }
1160 
1161 /// Matches PtrToInt.
1162 template <typename OpTy>
1165 }
1166 
1167 /// Matches Trunc.
1168 template <typename OpTy>
1171 }
1172 
1173 /// Matches SExt.
1174 template <typename OpTy>
1177 }
1178 
1179 /// Matches ZExt.
1180 template <typename OpTy>
1183 }
1184 
1185 template <typename OpTy>
1188 m_ZExtOrSExt(const OpTy &Op) {
1189  return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1190 }
1191 
1192 /// Matches UIToFP.
1193 template <typename OpTy>
1196 }
1197 
1198 /// Matches SIToFP.
1199 template <typename OpTy>
1202 }
1203 
1204 /// Matches FPTrunc
1205 template <typename OpTy>
1208 }
1209 
1210 /// Matches FPExt
1211 template <typename OpTy>
1214 }
1215 
1216 //===----------------------------------------------------------------------===//
1217 // Matchers for control flow.
1218 //
1219 
1220 struct br_match {
1222 
1223  br_match(BasicBlock *&Succ) : Succ(Succ) {}
1224 
1225  template <typename OpTy> bool match(OpTy *V) {
1226  if (auto *BI = dyn_cast<BranchInst>(V))
1227  if (BI->isUnconditional()) {
1228  Succ = BI->getSuccessor(0);
1229  return true;
1230  }
1231  return false;
1232  }
1233 };
1234 
1235 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1236 
1237 template <typename Cond_t> struct brc_match {
1238  Cond_t Cond;
1240 
1241  brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
1242  : Cond(C), T(t), F(f) {}
1243 
1244  template <typename OpTy> bool match(OpTy *V) {
1245  if (auto *BI = dyn_cast<BranchInst>(V))
1246  if (BI->isConditional() && Cond.match(BI->getCondition())) {
1247  T = BI->getSuccessor(0);
1248  F = BI->getSuccessor(1);
1249  return true;
1250  }
1251  return false;
1252  }
1253 };
1254 
1255 template <typename Cond_t>
1256 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1257  return brc_match<Cond_t>(C, T, F);
1258 }
1259 
1260 //===----------------------------------------------------------------------===//
1261 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1262 //
1263 
1264 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1265  bool Commutable = false>
1267  LHS_t L;
1268  RHS_t R;
1269 
1270  // The evaluation order is always stable, regardless of Commutability.
1271  // The LHS is always matched first.
1272  MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1273 
1274  template <typename OpTy> bool match(OpTy *V) {
1275  // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1276  auto *SI = dyn_cast<SelectInst>(V);
1277  if (!SI)
1278  return false;
1279  auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1280  if (!Cmp)
1281  return false;
1282  // At this point we have a select conditioned on a comparison. Check that
1283  // it is the values returned by the select that are being compared.
1284  Value *TrueVal = SI->getTrueValue();
1285  Value *FalseVal = SI->getFalseValue();
1286  Value *LHS = Cmp->getOperand(0);
1287  Value *RHS = Cmp->getOperand(1);
1288  if ((TrueVal != LHS || FalseVal != RHS) &&
1289  (TrueVal != RHS || FalseVal != LHS))
1290  return false;
1291  typename CmpInst_t::Predicate Pred =
1292  LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1293  // Does "(x pred y) ? x : y" represent the desired max/min operation?
1294  if (!Pred_t::match(Pred))
1295  return false;
1296  // It does! Bind the operands.
1297  return (L.match(LHS) && R.match(RHS)) ||
1298  (Commutable && L.match(RHS) && R.match(LHS));
1299  }
1300 };
1301 
1302 /// Helper class for identifying signed max predicates.
1304  static bool match(ICmpInst::Predicate Pred) {
1305  return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1306  }
1307 };
1308 
1309 /// Helper class for identifying signed min predicates.
1311  static bool match(ICmpInst::Predicate Pred) {
1312  return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1313  }
1314 };
1315 
1316 /// Helper class for identifying unsigned max predicates.
1318  static bool match(ICmpInst::Predicate Pred) {
1319  return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1320  }
1321 };
1322 
1323 /// Helper class for identifying unsigned min predicates.
1325  static bool match(ICmpInst::Predicate Pred) {
1326  return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1327  }
1328 };
1329 
1330 /// Helper class for identifying ordered max predicates.
1332  static bool match(FCmpInst::Predicate Pred) {
1333  return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1334  }
1335 };
1336 
1337 /// Helper class for identifying ordered min predicates.
1339  static bool match(FCmpInst::Predicate Pred) {
1340  return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1341  }
1342 };
1343 
1344 /// Helper class for identifying unordered max predicates.
1346  static bool match(FCmpInst::Predicate Pred) {
1347  return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1348  }
1349 };
1350 
1351 /// Helper class for identifying unordered min predicates.
1353  static bool match(FCmpInst::Predicate Pred) {
1354  return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1355  }
1356 };
1357 
1358 template <typename LHS, typename RHS>
1360  const RHS &R) {
1362 }
1363 
1364 template <typename LHS, typename RHS>
1366  const RHS &R) {
1368 }
1369 
1370 template <typename LHS, typename RHS>
1372  const RHS &R) {
1374 }
1375 
1376 template <typename LHS, typename RHS>
1378  const RHS &R) {
1380 }
1381 
1382 /// Match an 'ordered' floating point maximum function.
1383 /// Floating point has one special value 'NaN'. Therefore, there is no total
1384 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1385 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1386 /// semantics. In the presence of 'NaN' we have to preserve the original
1387 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1388 ///
1389 /// max(L, R) iff L and R are not NaN
1390 /// m_OrdFMax(L, R) = R iff L or R are NaN
1391 template <typename LHS, typename RHS>
1393  const RHS &R) {
1395 }
1396 
1397 /// Match an 'ordered' floating point minimum function.
1398 /// Floating point has one special value 'NaN'. Therefore, there is no total
1399 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1400 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1401 /// semantics. In the presence of 'NaN' we have to preserve the original
1402 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1403 ///
1404 /// min(L, R) iff L and R are not NaN
1405 /// m_OrdFMin(L, R) = R iff L or R are NaN
1406 template <typename LHS, typename RHS>
1408  const RHS &R) {
1410 }
1411 
1412 /// Match an 'unordered' floating point maximum function.
1413 /// Floating point has one special value 'NaN'. Therefore, there is no total
1414 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1415 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1416 /// semantics. In the presence of 'NaN' we have to preserve the original
1417 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1418 ///
1419 /// max(L, R) iff L and R are not NaN
1420 /// m_UnordFMax(L, R) = L iff L or R are NaN
1421 template <typename LHS, typename RHS>
1423 m_UnordFMax(const LHS &L, const RHS &R) {
1425 }
1426 
1427 /// Match an 'unordered' floating point minimum function.
1428 /// Floating point has one special value 'NaN'. Therefore, there is no total
1429 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1430 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1431 /// semantics. In the presence of 'NaN' we have to preserve the original
1432 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1433 ///
1434 /// min(L, R) iff L and R are not NaN
1435 /// m_UnordFMin(L, R) = L iff L or R are NaN
1436 template <typename LHS, typename RHS>
1438 m_UnordFMin(const LHS &L, const RHS &R) {
1440 }
1441 
1442 //===----------------------------------------------------------------------===//
1443 // Matchers for overflow check patterns: e.g. (a + b) u< a
1444 //
1445 
1446 template <typename LHS_t, typename RHS_t, typename Sum_t>
1448  LHS_t L;
1449  RHS_t R;
1450  Sum_t S;
1451 
1452  UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1453  : L(L), R(R), S(S) {}
1454 
1455  template <typename OpTy> bool match(OpTy *V) {
1456  Value *ICmpLHS, *ICmpRHS;
1457  ICmpInst::Predicate Pred;
1458  if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1459  return false;
1460 
1461  Value *AddLHS, *AddRHS;
1462  auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1463 
1464  // (a + b) u< a, (a + b) u< b
1465  if (Pred == ICmpInst::ICMP_ULT)
1466  if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1467  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1468 
1469  // a >u (a + b), b >u (a + b)
1470  if (Pred == ICmpInst::ICMP_UGT)
1471  if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1472  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1473 
1474  // Match special-case for increment-by-1.
1475  if (Pred == ICmpInst::ICMP_EQ) {
1476  // (a + 1) == 0
1477  // (1 + a) == 0
1478  if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
1479  (m_One().match(AddLHS) || m_One().match(AddRHS)))
1480  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1481  // 0 == (a + 1)
1482  // 0 == (1 + a)
1483  if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
1484  (m_One().match(AddLHS) || m_One().match(AddRHS)))
1485  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1486  }
1487 
1488  return false;
1489  }
1490 };
1491 
1492 /// Match an icmp instruction checking for unsigned overflow on addition.
1493 ///
1494 /// S is matched to the addition whose result is being checked for overflow, and
1495 /// L and R are matched to the LHS and RHS of S.
1496 template <typename LHS_t, typename RHS_t, typename Sum_t>
1498 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1500 }
1501 
1502 template <typename Opnd_t> struct Argument_match {
1503  unsigned OpI;
1504  Opnd_t Val;
1505 
1506  Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1507 
1508  template <typename OpTy> bool match(OpTy *V) {
1509  // FIXME: Should likely be switched to use `CallBase`.
1510  if (const auto *CI = dyn_cast<CallInst>(V))
1511  return Val.match(CI->getArgOperand(OpI));
1512  return false;
1513  }
1514 };
1515 
1516 /// Match an argument.
1517 template <unsigned OpI, typename Opnd_t>
1518 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1519  return Argument_match<Opnd_t>(OpI, Op);
1520 }
1521 
1522 /// Intrinsic matchers.
1524  unsigned ID;
1525 
1526  IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1527 
1528  template <typename OpTy> bool match(OpTy *V) {
1529  if (const auto *CI = dyn_cast<CallInst>(V))
1530  if (const auto *F = CI->getCalledFunction())
1531  return F->getIntrinsicID() == ID;
1532  return false;
1533  }
1534 };
1535 
1536 /// Intrinsic matches are combinations of ID matchers, and argument
1537 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1538 /// them with lower arity matchers. Here's some convenient typedefs for up to
1539 /// several arguments, and more can be added as needed
1540 template <typename T0 = void, typename T1 = void, typename T2 = void,
1541  typename T3 = void, typename T4 = void, typename T5 = void,
1542  typename T6 = void, typename T7 = void, typename T8 = void,
1543  typename T9 = void, typename T10 = void>
1545 template <typename T0> struct m_Intrinsic_Ty<T0> {
1547 };
1548 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1549  using Ty =
1551 };
1552 template <typename T0, typename T1, typename T2>
1553 struct m_Intrinsic_Ty<T0, T1, T2> {
1554  using Ty =
1557 };
1558 template <typename T0, typename T1, typename T2, typename T3>
1559 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1560  using Ty =
1563 };
1564 
1565 /// Match intrinsic calls like this:
1566 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1567 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1568  return IntrinsicID_match(IntrID);
1569 }
1570 
1571 template <Intrinsic::ID IntrID, typename T0>
1572 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1573  return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1574 }
1575 
1576 template <Intrinsic::ID IntrID, typename T0, typename T1>
1577 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1578  const T1 &Op1) {
1579  return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1580 }
1581 
1582 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1583 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1584 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1585  return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1586 }
1587 
1588 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1589  typename T3>
1590 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1591 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1592  return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1593 }
1594 
1595 // Helper intrinsic matching specializations.
1596 template <typename Opnd0>
1597 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1598  return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1599 }
1600 
1601 template <typename Opnd0>
1602 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1603  return m_Intrinsic<Intrinsic::bswap>(Op0);
1604 }
1605 
1606 template <typename Opnd0>
1607 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
1608  return m_Intrinsic<Intrinsic::fabs>(Op0);
1609 }
1610 
1611 template <typename Opnd0>
1612 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
1613  return m_Intrinsic<Intrinsic::canonicalize>(Op0);
1614 }
1615 
1616 template <typename Opnd0, typename Opnd1>
1617 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1618  const Opnd1 &Op1) {
1619  return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1620 }
1621 
1622 template <typename Opnd0, typename Opnd1>
1623 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1624  const Opnd1 &Op1) {
1625  return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1626 }
1627 
1628 //===----------------------------------------------------------------------===//
1629 // Matchers for two-operands operators with the operators in either order
1630 //
1631 
1632 /// Matches a BinaryOperator with LHS and RHS in either order.
1633 template <typename LHS, typename RHS>
1634 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
1635  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
1636 }
1637 
1638 /// Matches an ICmp with a predicate over LHS and RHS in either order.
1639 /// Does not swap the predicate.
1640 template <typename LHS, typename RHS>
1642 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1644  R);
1645 }
1646 
1647 /// Matches a Add with LHS and RHS in either order.
1648 template <typename LHS, typename RHS>
1650  const RHS &R) {
1652 }
1653 
1654 /// Matches a Mul with LHS and RHS in either order.
1655 template <typename LHS, typename RHS>
1657  const RHS &R) {
1659 }
1660 
1661 /// Matches an And with LHS and RHS in either order.
1662 template <typename LHS, typename RHS>
1664  const RHS &R) {
1666 }
1667 
1668 /// Matches an Or with LHS and RHS in either order.
1669 template <typename LHS, typename RHS>
1671  const RHS &R) {
1673 }
1674 
1675 /// Matches an Xor with LHS and RHS in either order.
1676 template <typename LHS, typename RHS>
1678  const RHS &R) {
1680 }
1681 
1682 /// Matches a 'Neg' as 'sub 0, V'.
1683 template <typename ValTy>
1684 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
1685 m_Neg(const ValTy &V) {
1686  return m_Sub(m_ZeroInt(), V);
1687 }
1688 
1689 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1690 template <typename ValTy>
1691 inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1692 m_Not(const ValTy &V) {
1693  return m_c_Xor(V, m_AllOnes());
1694 }
1695 
1696 /// Matches an SMin with LHS and RHS in either order.
1697 template <typename LHS, typename RHS>
1699 m_c_SMin(const LHS &L, const RHS &R) {
1701 }
1702 /// Matches an SMax with LHS and RHS in either order.
1703 template <typename LHS, typename RHS>
1705 m_c_SMax(const LHS &L, const RHS &R) {
1707 }
1708 /// Matches a UMin with LHS and RHS in either order.
1709 template <typename LHS, typename RHS>
1711 m_c_UMin(const LHS &L, const RHS &R) {
1713 }
1714 /// Matches a UMax with LHS and RHS in either order.
1715 template <typename LHS, typename RHS>
1717 m_c_UMax(const LHS &L, const RHS &R) {
1719 }
1720 
1721 /// Matches FAdd with LHS and RHS in either order.
1722 template <typename LHS, typename RHS>
1724 m_c_FAdd(const LHS &L, const RHS &R) {
1726 }
1727 
1728 /// Matches FMul with LHS and RHS in either order.
1729 template <typename LHS, typename RHS>
1731 m_c_FMul(const LHS &L, const RHS &R) {
1733 }
1734 
1735 template <typename Opnd_t> struct Signum_match {
1736  Opnd_t Val;
1737  Signum_match(const Opnd_t &V) : Val(V) {}
1738 
1739  template <typename OpTy> bool match(OpTy *V) {
1740  unsigned TypeSize = V->getType()->getScalarSizeInBits();
1741  if (TypeSize == 0)
1742  return false;
1743 
1744  unsigned ShiftWidth = TypeSize - 1;
1745  Value *OpL = nullptr, *OpR = nullptr;
1746 
1747  // This is the representation of signum we match:
1748  //
1749  // signum(x) == (x >> 63) | (-x >>u 63)
1750  //
1751  // An i1 value is its own signum, so it's correct to match
1752  //
1753  // signum(x) == (x >> 0) | (-x >>u 0)
1754  //
1755  // for i1 values.
1756 
1757  auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
1758  auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
1759  auto Signum = m_Or(LHS, RHS);
1760 
1761  return Signum.match(V) && OpL == OpR && Val.match(OpL);
1762  }
1763 };
1764 
1765 /// Matches a signum pattern.
1766 ///
1767 /// signum(x) =
1768 /// x > 0 -> 1
1769 /// x == 0 -> 0
1770 /// x < 0 -> -1
1771 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1772  return Signum_match<Val_t>(V);
1773 }
1774 
1775 } // end namespace PatternMatch
1776 } // end namespace llvm
1777 
1778 #endif // LLVM_IR_PATTERNMATCH_H
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:756
uint64_t CallInst * C
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
Definition: PatternMatch.h:940
bool isValue(const APFloat &C)
Definition: PatternMatch.h:440
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:827
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of nonnegative values.
Definition: PatternMatch.h:342
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMin(const Opnd0 &Op0, const Opnd1 &Op1)
static bool match(FCmpInst::Predicate Pred)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:86
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool isSignMask() const
Check if the APInt&#39;s value is returned by getSignMask.
Definition: APInt.h:472
bool isZero() const
Definition: APFloat.h:1142
Match a specified integer value or vector of all elements of that.
Definition: PatternMatch.h:561
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:78
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:653
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:375
br_match(BasicBlock *&Succ)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:647
Matches instructions with Opcode and three operands.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
Match a specified floating point value or vector of all elements of that value.
Definition: PatternMatch.h:521
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMax(const Opnd0 &Op0, const Opnd1 &Op1)
bool isValue(const APInt &C)
Definition: PatternMatch.h:404
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:732
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:744
bool isValue(const APInt &C)
Definition: PatternMatch.h:359
Exact_match(const SubPattern_t &SP)
Definition: PatternMatch.h:972
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match &#39;fneg X&#39; as &#39;fsub +-0.0, X&#39;.
Definition: PatternMatch.h:703
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:708
br_match m_UnconditionalBr(BasicBlock *&Succ)
This helper class is used to match scalar and vector floating-point constants that satisfy a specifie...
Definition: PatternMatch.h:263
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:408
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:89
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
unsigned less or equal
Definition: InstrTypes.h:735
unsigned less than
Definition: InstrTypes.h:734
bool isValue(const APInt &C)
Definition: PatternMatch.h:326
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:786
static bool match(ICmpInst::Predicate Pred)
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:715
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
match_combine_or(const LTy &Left, const RTy &Right)
Definition: PatternMatch.h:96
F(f)
ThreeOps_match< V1_t, V2_t, Mask_t, Instruction::ShuffleVector > m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m)
Matches ShuffleVectorInst.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:659
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an &#39;unordered&#39; floating point maximum function.
Argument_match(unsigned OpIdx, const Opnd_t &V)
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:363
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
Definition: PatternMatch.h:948
Matches instructions with Opcode and three operands.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
Definition: PatternMatch.h:539
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:47
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Helper class for identifying signed min predicates.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:768
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElement(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
This class represents the LLVM &#39;select&#39; instruction.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:368
Exact_match< T > m_Exact(const T &SubPattern)
Definition: PatternMatch.h:981
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:417
bool isValue(const APFloat &C)
Definition: PatternMatch.h:431
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bool isValue(const APFloat &C)
Definition: PatternMatch.h:422
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f...
Definition: PatternMatch.h:318
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:716
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an &#39;unordered&#39; floating point minimum function.
This file implements a class to represent arbitrary precision integral constant values and operations...
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:641
bind_ty< ConstantFP > m_ConstantFP(ConstantFP *&C)
Match a ConstantFP, capturing the value if we match.
Definition: PatternMatch.h:489
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Definition: Instruction.h:180
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:852
#define UINT64_MAX
Definition: DataTypes.h:83
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
Definition: PatternMatch.h:426
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:179
CastClass_match< OpTy, Instruction::FPExt > m_FPExt(const OpTy &Op)
Matches FPExt.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:123
CastClass_match(const Op_t &OpMatch)
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
#define T
CastClass_match< OpTy, Instruction::FPTrunc > m_FPTrunc(const OpTy &Op)
Matches FPTrunc.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an &#39;ordered&#39; floating point minimum function.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:81
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:137
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:444
IntrinsicID_match(Intrinsic::ID IntrID)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:384
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:868
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
match_combine_and(const LTy &Left, const RTy &Right)
Definition: PatternMatch.h:111
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:61
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:363
#define P(N)
bool isNegZero() const
Definition: APFloat.h:1158
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:780
Helper class for identifying signed max predicates.
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:395
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
Definition: PatternMatch.h:175
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:726
This helper class is used to match scalar and vector integer constants that satisfy a specified predi...
Definition: PatternMatch.h:204
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:876
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:750
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
Definition: PatternMatch.h:396
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:762
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
bool isNaN() const
Definition: APFloat.h:1144
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:263
bool isMask(unsigned numBits) const
Definition: APInt.h:494
bool isOneValue() const
Determine if this is a value of 1.
Definition: APInt.h:410
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:308
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:501
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:774
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:709
match_combine_or< CastClass_match< OpTy, Instruction::ZExt >, CastClass_match< OpTy, Instruction::SExt > > m_ZExtOrSExt(const OpTy &Op)
Helper class for identifying unsigned min predicates.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
Definition: PatternMatch.h:955
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:594
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:73
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
Definition: PatternMatch.h:961
Helper class for identifying unordered min predicates.
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:724
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:622
deferredval_ty< Value > m_Deferred(Value *const &V)
A commutative-friendly version of m_Specific().
Definition: PatternMatch.h:514
signed greater than
Definition: InstrTypes.h:736
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
OneUse_match(const SubPattern_t &SP)
Definition: PatternMatch.h:54
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:713
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:330
Intrinsic matches are combinations of ID matchers, and argument matchers.
static bool match(ICmpInst::Predicate Pred)
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:426
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
static bool match(ICmpInst::Predicate Pred)
bool isValue(const APInt &C)
Definition: PatternMatch.h:380
Match a specified Value*.
Definition: PatternMatch.h:492
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:723
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:245
brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:738
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:26
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:720
signed less than
Definition: InstrTypes.h:738
bool isOpType(unsigned Opcode)
Definition: PatternMatch.h:926
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers...
Definition: PatternMatch.h:505
CastClass_match< OpTy, Instruction::UIToFP > m_UIToFP(const OpTy &Op)
Matches UIToFP.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:714
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:819
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a &#39;Neg&#39; as &#39;sub 0, V&#39;.
signed less or equal
Definition: InstrTypes.h:739
Class for arbitrary precision integers.
Definition: APInt.h:69
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:453
bool isPowerOf2() const
Check if this APInt&#39;s value is a power of two greater than zero.
Definition: APInt.h:463
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
Definition: PatternMatch.h:933
Helper class for identifying unsigned max predicates.
Helper class for identifying ordered max predicates.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:860
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:542
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match &#39;fneg X&#39; as &#39;fsub -0.0, X&#39;.
Definition: PatternMatch.h:696
bool isPosZero() const
Definition: APFloat.h:1157
unsigned greater or equal
Definition: InstrTypes.h:733
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:998
#define I(x, y, z)
Definition: MD5.cpp:58
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:843
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
ThreeOps_match< Cond, constantint_match< L >, constantint_match< R >, Instruction::Select > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
CastClass_match< OpTy, Instruction::SIToFP > m_SIToFP(const OpTy &Op)
Matches SIToFP.
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:721
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:797
Matches instructions with Opcode and three operands.
bool isOpType(unsigned Opcode)
Definition: PatternMatch.h:904
static bool match(FCmpInst::Predicate Pred)
apfloat_match(const APFloat *&R)
Definition: PatternMatch.h:157
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an &#39;ordered&#39; floating point maximum function.
Helper class for identifying unordered max predicates.
LLVM Value Representation.
Definition: Value.h:72
bool isValue(const APInt &C)
Definition: PatternMatch.h:304
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:722
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:835
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:354
static bool match(ICmpInst::Predicate Pred)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:890
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:129
static bool match(FCmpInst::Predicate Pred)
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
unsigned greater than
Definition: InstrTypes.h:732
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:578
bool isValue(const APInt &C)
Definition: PatternMatch.h:350
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:435
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:714
bool isValue(const APFloat &C)
Definition: PatternMatch.h:449
This helper class is used to match scalar and vector constants that satisfy a specified predicate...
Definition: PatternMatch.h:237
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:478
#define T1
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a &#39;Not&#39; as &#39;xor V, -1&#39; or &#39;xor -1, V&#39;.
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:405
signed greater or equal
Definition: InstrTypes.h:737
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)