LLVM  8.0.0svn
PatternMatch.h
Go to the documentation of this file.
1 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides a simple and efficient mechanism for performing general
11 // tree-based pattern matches on the LLVM IR. The power of these routines is
12 // that it allows you to write concise patterns that are expressive and easy to
13 // understand. The other major advantage of this is that it allows you to
14 // trivially capture/bind elements in the pattern to variables. For example,
15 // you can do something like this:
16 //
17 // Value *Exp = ...
18 // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
19 // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
20 // m_And(m_Value(Y), m_ConstantInt(C2))))) {
21 // ... Pattern is matched and variables are bound ...
22 // }
23 //
24 // This is primarily useful to things like the instruction combiner, but can
25 // also be useful for static analysis tools or code generators.
26 //
27 //===----------------------------------------------------------------------===//
28 
29 #ifndef LLVM_IR_PATTERNMATCH_H
30 #define LLVM_IR_PATTERNMATCH_H
31 
32 #include "llvm/ADT/APFloat.h"
33 #include "llvm/ADT/APInt.h"
34 #include "llvm/IR/CallSite.h"
35 #include "llvm/IR/Constant.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/InstrTypes.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/Intrinsics.h"
41 #include "llvm/IR/Operator.h"
42 #include "llvm/IR/Value.h"
43 #include "llvm/Support/Casting.h"
44 #include <cstdint>
45 
46 namespace llvm {
47 namespace PatternMatch {
48 
49 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50  return const_cast<Pattern &>(P).match(V);
51 }
52 
53 template <typename SubPattern_t> struct OneUse_match {
54  SubPattern_t SubPattern;
55 
56  OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
57 
58  template <typename OpTy> bool match(OpTy *V) {
59  return V->hasOneUse() && SubPattern.match(V);
60  }
61 };
62 
63 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
64  return SubPattern;
65 }
66 
67 template <typename Class> struct class_match {
68  template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
69 };
70 
71 /// Match an arbitrary value and ignore it.
73 
74 /// Match an arbitrary binary operation and ignore it.
77 }
78 
79 /// Matches any compare instruction and ignore it.
81 
82 /// Match an arbitrary ConstantInt and ignore it.
84  return class_match<ConstantInt>();
85 }
86 
87 /// Match an arbitrary undef constant.
89 
90 /// Match an arbitrary Constant and ignore it.
92 
93 /// Matching combinators
94 template <typename LTy, typename RTy> struct match_combine_or {
95  LTy L;
96  RTy R;
97 
98  match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
99 
100  template <typename ITy> bool match(ITy *V) {
101  if (L.match(V))
102  return true;
103  if (R.match(V))
104  return true;
105  return false;
106  }
107 };
108 
109 template <typename LTy, typename RTy> struct match_combine_and {
110  LTy L;
111  RTy R;
112 
113  match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
114 
115  template <typename ITy> bool match(ITy *V) {
116  if (L.match(V))
117  if (R.match(V))
118  return true;
119  return false;
120  }
121 };
122 
123 /// Combine two pattern matchers matching L || R
124 template <typename LTy, typename RTy>
125 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
126  return match_combine_or<LTy, RTy>(L, R);
127 }
128 
129 /// Combine two pattern matchers matching L && R
130 template <typename LTy, typename RTy>
131 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
132  return match_combine_and<LTy, RTy>(L, R);
133 }
134 
135 struct apint_match {
136  const APInt *&Res;
137 
138  apint_match(const APInt *&R) : Res(R) {}
139 
140  template <typename ITy> bool match(ITy *V) {
141  if (auto *CI = dyn_cast<ConstantInt>(V)) {
142  Res = &CI->getValue();
143  return true;
144  }
145  if (V->getType()->isVectorTy())
146  if (const auto *C = dyn_cast<Constant>(V))
147  if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
148  Res = &CI->getValue();
149  return true;
150  }
151  return false;
152  }
153 };
154 // Either constexpr if or renaming ConstantFP::getValueAPF to
155 // ConstantFP::getValue is needed to do it via single template
156 // function for both apint/apfloat.
158  const APFloat *&Res;
159  apfloat_match(const APFloat *&R) : Res(R) {}
160  template <typename ITy> bool match(ITy *V) {
161  if (auto *CI = dyn_cast<ConstantFP>(V)) {
162  Res = &CI->getValueAPF();
163  return true;
164  }
165  if (V->getType()->isVectorTy())
166  if (const auto *C = dyn_cast<Constant>(V))
167  if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) {
168  Res = &CI->getValueAPF();
169  return true;
170  }
171  return false;
172  }
173 };
174 
175 /// Match a ConstantInt or splatted ConstantVector, binding the
176 /// specified pointer to the contained APInt.
177 inline apint_match m_APInt(const APInt *&Res) { return Res; }
178 
179 /// Match a ConstantFP or splatted ConstantVector, binding the
180 /// specified pointer to the contained APFloat.
181 inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
182 
183 template <int64_t Val> struct constantint_match {
184  template <typename ITy> bool match(ITy *V) {
185  if (const auto *CI = dyn_cast<ConstantInt>(V)) {
186  const APInt &CIV = CI->getValue();
187  if (Val >= 0)
188  return CIV == static_cast<uint64_t>(Val);
189  // If Val is negative, and CI is shorter than it, truncate to the right
190  // number of bits. If it is larger, then we have to sign extend. Just
191  // compare their negated values.
192  return -CIV == -Val;
193  }
194  return false;
195  }
196 };
197 
198 /// Match a ConstantInt with a specific value.
199 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
200  return constantint_match<Val>();
201 }
202 
203 /// This helper class is used to match scalar and vector integer constants that
204 /// satisfy a specified predicate.
205 /// For vector constants, undefined elements are ignored.
206 template <typename Predicate> struct cst_pred_ty : public Predicate {
207  template <typename ITy> bool match(ITy *V) {
208  if (const auto *CI = dyn_cast<ConstantInt>(V))
209  return this->isValue(CI->getValue());
210  if (V->getType()->isVectorTy()) {
211  if (const auto *C = dyn_cast<Constant>(V)) {
212  if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
213  return this->isValue(CI->getValue());
214 
215  // Non-splat vector constant: check each element for a match.
216  unsigned NumElts = V->getType()->getVectorNumElements();
217  assert(NumElts != 0 && "Constant vector with no elements?");
218  for (unsigned i = 0; i != NumElts; ++i) {
219  Constant *Elt = C->getAggregateElement(i);
220  if (!Elt)
221  return false;
222  if (isa<UndefValue>(Elt))
223  continue;
224  auto *CI = dyn_cast<ConstantInt>(Elt);
225  if (!CI || !this->isValue(CI->getValue()))
226  return false;
227  }
228  return true;
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  for (unsigned i = 0; i != NumElts; ++i) {
276  Constant *Elt = C->getAggregateElement(i);
277  if (!Elt)
278  return false;
279  if (isa<UndefValue>(Elt))
280  continue;
281  auto *CF = dyn_cast<ConstantFP>(Elt);
282  if (!CF || !this->isValue(CF->getValueAPF()))
283  return false;
284  }
285  return true;
286  }
287  }
288  return false;
289  }
290 };
291 
292 ///////////////////////////////////////////////////////////////////////////////
293 //
294 // Encapsulate constant value queries for use in templated predicate matchers.
295 // This allows checking if constants match using compound predicates and works
296 // with vector constants, possibly with relaxed constraints. For example, ignore
297 // undef values.
298 //
299 ///////////////////////////////////////////////////////////////////////////////
300 
301 struct is_all_ones {
302  bool isValue(const APInt &C) { return C.isAllOnesValue(); }
303 };
304 /// Match an integer or vector with all bits set.
305 /// For vectors, this includes constants with undefined elements.
307  return cst_pred_ty<is_all_ones>();
308 }
309 
311  bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
312 };
313 /// Match an integer or vector with values having all bits except for the high
314 /// bit set (0x7f...).
315 /// For vectors, this includes constants with undefined elements.
318 }
320  return V;
321 }
322 
323 struct is_negative {
324  bool isValue(const APInt &C) { return C.isNegative(); }
325 };
326 /// Match an integer or vector of negative values.
327 /// For vectors, this includes constants with undefined elements.
329  return cst_pred_ty<is_negative>();
330 }
332  return V;
333 }
334 
336  bool isValue(const APInt &C) { return C.isNonNegative(); }
337 };
338 /// Match an integer or vector of nonnegative values.
339 /// For vectors, this includes constants with undefined elements.
342 }
344  return V;
345 }
346 
347 struct is_one {
348  bool isValue(const APInt &C) { return C.isOneValue(); }
349 };
350 /// Match an integer 1 or a vector with all elements equal to 1.
351 /// For vectors, this includes constants with undefined elements.
353  return cst_pred_ty<is_one>();
354 }
355 
356 struct is_zero_int {
357  bool isValue(const APInt &C) { return C.isNullValue(); }
358 };
359 /// Match an integer 0 or a vector with all elements equal to 0.
360 /// For vectors, this includes constants with undefined elements.
362  return cst_pred_ty<is_zero_int>();
363 }
364 
365 struct is_zero {
366  template <typename ITy> bool match(ITy *V) {
367  auto *C = dyn_cast<Constant>(V);
368  return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
369  }
370 };
371 /// Match any null constant or a vector with all elements equal to 0.
372 /// For vectors, this includes constants with undefined elements.
373 inline is_zero m_Zero() {
374  return is_zero();
375 }
376 
377 struct is_power2 {
378  bool isValue(const APInt &C) { return C.isPowerOf2(); }
379 };
380 /// Match an integer or vector power-of-2.
381 /// For vectors, this includes constants with undefined elements.
383  return cst_pred_ty<is_power2>();
384 }
386  return V;
387 }
388 
390  bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
391 };
392 /// Match an integer or vector of 0 or power-of-2 values.
393 /// For vectors, this includes constants with undefined elements.
396 }
398  return V;
399 }
400 
401 struct is_sign_mask {
402  bool isValue(const APInt &C) { return C.isSignMask(); }
403 };
404 /// Match an integer or vector with only the sign bit(s) set.
405 /// For vectors, this includes constants with undefined elements.
407  return cst_pred_ty<is_sign_mask>();
408 }
409 
411  bool isValue(const APInt &C) { return C.isMask(); }
412 };
413 /// Match an integer or vector with only the low bit(s) set.
414 /// For vectors, this includes constants with undefined elements.
417 }
418 
419 struct is_nan {
420  bool isValue(const APFloat &C) { return C.isNaN(); }
421 };
422 /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
423 /// For vectors, this includes constants with undefined elements.
425  return cstfp_pred_ty<is_nan>();
426 }
427 
429  bool isValue(const APFloat &C) { return C.isZero(); }
430 };
431 /// Match a floating-point negative zero or positive zero.
432 /// For vectors, this includes constants with undefined elements.
435 }
436 
438  bool isValue(const APFloat &C) { return C.isPosZero(); }
439 };
440 /// Match a floating-point positive zero.
441 /// For vectors, this includes constants with undefined elements.
444 }
445 
447  bool isValue(const APFloat &C) { return C.isNegZero(); }
448 };
449 /// Match a floating-point negative zero.
450 /// For vectors, this includes constants with undefined elements.
453 }
454 
455 ///////////////////////////////////////////////////////////////////////////////
456 
457 template <typename Class> struct bind_ty {
458  Class *&VR;
459 
460  bind_ty(Class *&V) : VR(V) {}
461 
462  template <typename ITy> bool match(ITy *V) {
463  if (auto *CV = dyn_cast<Class>(V)) {
464  VR = CV;
465  return true;
466  }
467  return false;
468  }
469 };
470 
471 /// Match a value, capturing it if we match.
472 inline bind_ty<Value> m_Value(Value *&V) { return V; }
473 inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
474 
475 /// Match an instruction, capturing it if we match.
477 /// Match a binary operator, capturing it if we match.
479 
480 /// Match a ConstantInt, capturing the value if we match.
481 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
482 
483 /// Match a Constant, capturing the value if we match.
484 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
485 
486 /// Match a ConstantFP, capturing the value if we match.
488 
489 /// Match a specified Value*.
491  const Value *Val;
492 
493  specificval_ty(const Value *V) : Val(V) {}
494 
495  template <typename ITy> bool match(ITy *V) { return V == Val; }
496 };
497 
498 /// Match if we have a specific specified value.
499 inline specificval_ty m_Specific(const Value *V) { return V; }
500 
501 /// Stores a reference to the Value *, not the Value * itself,
502 /// thus can be used in commutative matchers.
503 template <typename Class> struct deferredval_ty {
504  Class *const &Val;
505 
506  deferredval_ty(Class *const &V) : Val(V) {}
507 
508  template <typename ITy> bool match(ITy *const V) { return V == Val; }
509 };
510 
511 /// A commutative-friendly version of m_Specific().
512 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
514  return V;
515 }
516 
517 /// Match a specified floating point value or vector of all elements of
518 /// that value.
520  double Val;
521 
522  specific_fpval(double V) : Val(V) {}
523 
524  template <typename ITy> bool match(ITy *V) {
525  if (const auto *CFP = dyn_cast<ConstantFP>(V))
526  return CFP->isExactlyValue(Val);
527  if (V->getType()->isVectorTy())
528  if (const auto *C = dyn_cast<Constant>(V))
529  if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
530  return CFP->isExactlyValue(Val);
531  return false;
532  }
533 };
534 
535 /// Match a specific floating point value or vector with all elements
536 /// equal to the value.
537 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
538 
539 /// Match a float 1.0 or vector with all elements equal to 1.0.
540 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
541 
543  uint64_t &VR;
544 
545  bind_const_intval_ty(uint64_t &V) : VR(V) {}
546 
547  template <typename ITy> bool match(ITy *V) {
548  if (const auto *CV = dyn_cast<ConstantInt>(V))
549  if (CV->getValue().ule(UINT64_MAX)) {
550  VR = CV->getZExtValue();
551  return true;
552  }
553  return false;
554  }
555 };
556 
557 /// Match a specified integer value or vector of all elements of that
558 // value.
560  uint64_t Val;
561 
562  specific_intval(uint64_t V) : Val(V) {}
563 
564  template <typename ITy> bool match(ITy *V) {
565  const auto *CI = dyn_cast<ConstantInt>(V);
566  if (!CI && V->getType()->isVectorTy())
567  if (const auto *C = dyn_cast<Constant>(V))
568  CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
569 
570  return CI && CI->getValue() == Val;
571  }
572 };
573 
574 /// Match a specific integer value or vector with all elements equal to
575 /// the value.
576 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
577 
578 /// Match a ConstantInt and bind to its value. This does not match
579 /// ConstantInts wider than 64-bits.
580 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
581 
582 //===----------------------------------------------------------------------===//
583 // Matcher for any binary operator.
584 //
585 template <typename LHS_t, typename RHS_t, bool Commutable = false>
587  LHS_t L;
588  RHS_t R;
589 
590  // The evaluation order is always stable, regardless of Commutability.
591  // The LHS is always matched first.
592  AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
593 
594  template <typename OpTy> bool match(OpTy *V) {
595  if (auto *I = dyn_cast<BinaryOperator>(V))
596  return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
597  (Commutable && L.match(I->getOperand(1)) &&
598  R.match(I->getOperand(0)));
599  return false;
600  }
601 };
602 
603 template <typename LHS, typename RHS>
604 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
605  return AnyBinaryOp_match<LHS, RHS>(L, R);
606 }
607 
608 //===----------------------------------------------------------------------===//
609 // Matchers for specific binary operators.
610 //
611 
612 template <typename LHS_t, typename RHS_t, unsigned Opcode,
613  bool Commutable = false>
615  LHS_t L;
616  RHS_t R;
617 
618  // The evaluation order is always stable, regardless of Commutability.
619  // The LHS is always matched first.
620  BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
621 
622  template <typename OpTy> bool match(OpTy *V) {
623  if (V->getValueID() == Value::InstructionVal + Opcode) {
624  auto *I = cast<BinaryOperator>(V);
625  return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
626  (Commutable && L.match(I->getOperand(1)) &&
627  R.match(I->getOperand(0)));
628  }
629  if (auto *CE = dyn_cast<ConstantExpr>(V))
630  return CE->getOpcode() == Opcode &&
631  ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
632  (Commutable && L.match(CE->getOperand(1)) &&
633  R.match(CE->getOperand(0))));
634  return false;
635  }
636 };
637 
638 template <typename LHS, typename RHS>
640  const RHS &R) {
642 }
643 
644 template <typename LHS, typename RHS>
646  const RHS &R) {
648 }
649 
650 template <typename LHS, typename RHS>
652  const RHS &R) {
654 }
655 
656 template <typename LHS, typename RHS>
658  const RHS &R) {
660 }
661 
662 /// Match 'fneg X' as 'fsub -0.0, X'.
663 template <typename RHS>
664 inline BinaryOp_match<cstfp_pred_ty<is_neg_zero_fp>, RHS, Instruction::FSub>
665 m_FNeg(const RHS &X) {
666  return m_FSub(m_NegZeroFP(), X);
667 }
668 
669 /// Match 'fneg X' as 'fsub +-0.0, X'.
670 template <typename RHS>
671 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
672 m_FNegNSZ(const RHS &X) {
673  return m_FSub(m_AnyZeroFP(), X);
674 }
675 
676 template <typename LHS, typename RHS>
678  const RHS &R) {
680 }
681 
682 template <typename LHS, typename RHS>
684  const RHS &R) {
686 }
687 
688 template <typename LHS, typename RHS>
690  const RHS &R) {
692 }
693 
694 template <typename LHS, typename RHS>
696  const RHS &R) {
698 }
699 
700 template <typename LHS, typename RHS>
702  const RHS &R) {
704 }
705 
706 template <typename LHS, typename RHS>
708  const RHS &R) {
710 }
711 
712 template <typename LHS, typename RHS>
714  const RHS &R) {
716 }
717 
718 template <typename LHS, typename RHS>
720  const RHS &R) {
722 }
723 
724 template <typename LHS, typename RHS>
726  const RHS &R) {
728 }
729 
730 template <typename LHS, typename RHS>
732  const RHS &R) {
734 }
735 
736 template <typename LHS, typename RHS>
738  const RHS &R) {
740 }
741 
742 template <typename LHS, typename RHS>
744  const RHS &R) {
746 }
747 
748 template <typename LHS, typename RHS>
750  const RHS &R) {
752 }
753 
754 template <typename LHS, typename RHS>
756  const RHS &R) {
758 }
759 
760 template <typename LHS_t, typename RHS_t, unsigned Opcode,
761  unsigned WrapFlags = 0>
763  LHS_t L;
764  RHS_t R;
765 
766  OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
767  : L(LHS), R(RHS) {}
768 
769  template <typename OpTy> bool match(OpTy *V) {
770  if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
771  if (Op->getOpcode() != Opcode)
772  return false;
774  !Op->hasNoUnsignedWrap())
775  return false;
776  if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
777  !Op->hasNoSignedWrap())
778  return false;
779  return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
780  }
781  return false;
782  }
783 };
784 
785 template <typename LHS, typename RHS>
788 m_NSWAdd(const LHS &L, const RHS &R) {
791  L, R);
792 }
793 template <typename LHS, typename RHS>
794 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
796 m_NSWSub(const LHS &L, const RHS &R) {
797  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
799  L, R);
800 }
801 template <typename LHS, typename RHS>
802 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
804 m_NSWMul(const LHS &L, const RHS &R) {
805  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
807  L, R);
808 }
809 template <typename LHS, typename RHS>
810 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
812 m_NSWShl(const LHS &L, const RHS &R) {
813  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
815  L, R);
816 }
817 
818 template <typename LHS, typename RHS>
821 m_NUWAdd(const LHS &L, const RHS &R) {
824  L, R);
825 }
826 template <typename LHS, typename RHS>
827 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
829 m_NUWSub(const LHS &L, const RHS &R) {
830  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
832  L, R);
833 }
834 template <typename LHS, typename RHS>
835 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
837 m_NUWMul(const LHS &L, const RHS &R) {
838  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
840  L, R);
841 }
842 template <typename LHS, typename RHS>
843 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
845 m_NUWShl(const LHS &L, const RHS &R) {
846  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
848  L, R);
849 }
850 
851 //===----------------------------------------------------------------------===//
852 // Class that matches a group of binary opcodes.
853 //
854 template <typename LHS_t, typename RHS_t, typename Predicate>
856  LHS_t L;
857  RHS_t R;
858 
859  BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
860 
861  template <typename OpTy> bool match(OpTy *V) {
862  if (auto *I = dyn_cast<Instruction>(V))
863  return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
864  R.match(I->getOperand(1));
865  if (auto *CE = dyn_cast<ConstantExpr>(V))
866  return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
867  R.match(CE->getOperand(1));
868  return false;
869  }
870 };
871 
872 struct is_shift_op {
873  bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
874 };
875 
877  bool isOpType(unsigned Opcode) {
878  return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
879  }
880 };
881 
883  bool isOpType(unsigned Opcode) {
884  return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
885  }
886 };
887 
889  bool isOpType(unsigned Opcode) {
890  return Instruction::isBitwiseLogicOp(Opcode);
891  }
892 };
893 
894 struct is_idiv_op {
895  bool isOpType(unsigned Opcode) {
896  return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
897  }
898 };
899 
900 /// Matches shift operations.
901 template <typename LHS, typename RHS>
903  const RHS &R) {
905 }
906 
907 /// Matches logical shift operations.
908 template <typename LHS, typename RHS>
910  const RHS &R) {
912 }
913 
914 /// Matches logical shift operations.
915 template <typename LHS, typename RHS>
917 m_LogicalShift(const LHS &L, const RHS &R) {
919 }
920 
921 /// Matches bitwise logic operations.
922 template <typename LHS, typename RHS>
924 m_BitwiseLogic(const LHS &L, const RHS &R) {
926 }
927 
928 /// Matches integer division operations.
929 template <typename LHS, typename RHS>
931  const RHS &R) {
933 }
934 
935 //===----------------------------------------------------------------------===//
936 // Class that matches exact binary ops.
937 //
938 template <typename SubPattern_t> struct Exact_match {
939  SubPattern_t SubPattern;
940 
941  Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
942 
943  template <typename OpTy> bool match(OpTy *V) {
944  if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
945  return PEO->isExact() && SubPattern.match(V);
946  return false;
947  }
948 };
949 
950 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
951  return SubPattern;
952 }
953 
954 //===----------------------------------------------------------------------===//
955 // Matchers for CmpInst classes
956 //
957 
958 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
959  bool Commutable = false>
961  PredicateTy &Predicate;
962  LHS_t L;
963  RHS_t R;
964 
965  // The evaluation order is always stable, regardless of Commutability.
966  // The LHS is always matched first.
967  CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
968  : Predicate(Pred), L(LHS), R(RHS) {}
969 
970  template <typename OpTy> bool match(OpTy *V) {
971  if (auto *I = dyn_cast<Class>(V))
972  if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
973  (Commutable && L.match(I->getOperand(1)) &&
974  R.match(I->getOperand(0)))) {
975  Predicate = I->getPredicate();
976  return true;
977  }
978  return false;
979  }
980 };
981 
982 template <typename LHS, typename RHS>
984 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
986 }
987 
988 template <typename LHS, typename RHS>
990 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
992 }
993 
994 template <typename LHS, typename RHS>
996 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
998 }
999 
1000 //===----------------------------------------------------------------------===//
1001 // Matchers for instructions with a given opcode and number of operands.
1002 //
1003 
1004 /// Matches instructions with Opcode and three operands.
1005 template <typename T0, unsigned Opcode> struct OneOps_match {
1006  T0 Op1;
1007 
1008  OneOps_match(const T0 &Op1) : Op1(Op1) {}
1009 
1010  template <typename OpTy> bool match(OpTy *V) {
1011  if (V->getValueID() == Value::InstructionVal + Opcode) {
1012  auto *I = cast<Instruction>(V);
1013  return Op1.match(I->getOperand(0));
1014  }
1015  return false;
1016  }
1017 };
1018 
1019 /// Matches instructions with Opcode and three operands.
1020 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1021  T0 Op1;
1023 
1024  TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1025 
1026  template <typename OpTy> bool match(OpTy *V) {
1027  if (V->getValueID() == Value::InstructionVal + Opcode) {
1028  auto *I = cast<Instruction>(V);
1029  return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1030  }
1031  return false;
1032  }
1033 };
1034 
1035 /// Matches instructions with Opcode and three operands.
1036 template <typename T0, typename T1, typename T2, unsigned Opcode>
1038  T0 Op1;
1040  T2 Op3;
1041 
1042  ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1043  : Op1(Op1), Op2(Op2), Op3(Op3) {}
1044 
1045  template <typename OpTy> bool match(OpTy *V) {
1046  if (V->getValueID() == Value::InstructionVal + Opcode) {
1047  auto *I = cast<Instruction>(V);
1048  return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1049  Op3.match(I->getOperand(2));
1050  }
1051  return false;
1052  }
1053 };
1054 
1055 /// Matches SelectInst.
1056 template <typename Cond, typename LHS, typename RHS>
1058 m_Select(const Cond &C, const LHS &L, const RHS &R) {
1060 }
1061 
1062 /// This matches a select of two constants, e.g.:
1063 /// m_SelectCst<-1, 0>(m_Value(V))
1064 template <int64_t L, int64_t R, typename Cond>
1067 m_SelectCst(const Cond &C) {
1068  return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1069 }
1070 
1071 /// Matches InsertElementInst.
1072 template <typename Val_t, typename Elt_t, typename Idx_t>
1074 m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1076  Val, Elt, Idx);
1077 }
1078 
1079 /// Matches ExtractElementInst.
1080 template <typename Val_t, typename Idx_t>
1082 m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
1084 }
1085 
1086 /// Matches ShuffleVectorInst.
1087 template <typename V1_t, typename V2_t, typename Mask_t>
1089 m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
1091  m);
1092 }
1093 
1094 /// Matches LoadInst.
1095 template <typename OpTy>
1098 }
1099 
1100 /// Matches StoreInst.
1101 template <typename ValueOpTy, typename PointerOpTy>
1103 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1105  PointerOp);
1106 }
1107 
1108 //===----------------------------------------------------------------------===//
1109 // Matchers for CastInst classes
1110 //
1111 
1112 template <typename Op_t, unsigned Opcode> struct CastClass_match {
1113  Op_t Op;
1114 
1115  CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1116 
1117  template <typename OpTy> bool match(OpTy *V) {
1118  if (auto *O = dyn_cast<Operator>(V))
1119  return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1120  return false;
1121  }
1122 };
1123 
1124 /// Matches BitCast.
1125 template <typename OpTy>
1128 }
1129 
1130 /// Matches PtrToInt.
1131 template <typename OpTy>
1134 }
1135 
1136 /// Matches Trunc.
1137 template <typename OpTy>
1140 }
1141 
1142 /// Matches SExt.
1143 template <typename OpTy>
1146 }
1147 
1148 /// Matches ZExt.
1149 template <typename OpTy>
1152 }
1153 
1154 template <typename OpTy>
1157 m_ZExtOrSExt(const OpTy &Op) {
1158  return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1159 }
1160 
1161 /// Matches UIToFP.
1162 template <typename OpTy>
1165 }
1166 
1167 /// Matches SIToFP.
1168 template <typename OpTy>
1171 }
1172 
1173 /// Matches FPTrunc
1174 template <typename OpTy>
1177 }
1178 
1179 /// Matches FPExt
1180 template <typename OpTy>
1183 }
1184 
1185 //===----------------------------------------------------------------------===//
1186 // Matchers for control flow.
1187 //
1188 
1189 struct br_match {
1191 
1192  br_match(BasicBlock *&Succ) : Succ(Succ) {}
1193 
1194  template <typename OpTy> bool match(OpTy *V) {
1195  if (auto *BI = dyn_cast<BranchInst>(V))
1196  if (BI->isUnconditional()) {
1197  Succ = BI->getSuccessor(0);
1198  return true;
1199  }
1200  return false;
1201  }
1202 };
1203 
1204 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1205 
1206 template <typename Cond_t> struct brc_match {
1207  Cond_t Cond;
1209 
1210  brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
1211  : Cond(C), T(t), F(f) {}
1212 
1213  template <typename OpTy> bool match(OpTy *V) {
1214  if (auto *BI = dyn_cast<BranchInst>(V))
1215  if (BI->isConditional() && Cond.match(BI->getCondition())) {
1216  T = BI->getSuccessor(0);
1217  F = BI->getSuccessor(1);
1218  return true;
1219  }
1220  return false;
1221  }
1222 };
1223 
1224 template <typename Cond_t>
1225 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1226  return brc_match<Cond_t>(C, T, F);
1227 }
1228 
1229 //===----------------------------------------------------------------------===//
1230 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1231 //
1232 
1233 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1234  bool Commutable = false>
1236  LHS_t L;
1237  RHS_t R;
1238 
1239  // The evaluation order is always stable, regardless of Commutability.
1240  // The LHS is always matched first.
1241  MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1242 
1243  template <typename OpTy> bool match(OpTy *V) {
1244  // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1245  auto *SI = dyn_cast<SelectInst>(V);
1246  if (!SI)
1247  return false;
1248  auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1249  if (!Cmp)
1250  return false;
1251  // At this point we have a select conditioned on a comparison. Check that
1252  // it is the values returned by the select that are being compared.
1253  Value *TrueVal = SI->getTrueValue();
1254  Value *FalseVal = SI->getFalseValue();
1255  Value *LHS = Cmp->getOperand(0);
1256  Value *RHS = Cmp->getOperand(1);
1257  if ((TrueVal != LHS || FalseVal != RHS) &&
1258  (TrueVal != RHS || FalseVal != LHS))
1259  return false;
1260  typename CmpInst_t::Predicate Pred =
1261  LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1262  // Does "(x pred y) ? x : y" represent the desired max/min operation?
1263  if (!Pred_t::match(Pred))
1264  return false;
1265  // It does! Bind the operands.
1266  return (L.match(LHS) && R.match(RHS)) ||
1267  (Commutable && L.match(RHS) && R.match(LHS));
1268  }
1269 };
1270 
1271 /// Helper class for identifying signed max predicates.
1273  static bool match(ICmpInst::Predicate Pred) {
1274  return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1275  }
1276 };
1277 
1278 /// Helper class for identifying signed min predicates.
1280  static bool match(ICmpInst::Predicate Pred) {
1281  return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1282  }
1283 };
1284 
1285 /// Helper class for identifying unsigned max predicates.
1287  static bool match(ICmpInst::Predicate Pred) {
1288  return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1289  }
1290 };
1291 
1292 /// Helper class for identifying unsigned min predicates.
1294  static bool match(ICmpInst::Predicate Pred) {
1295  return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1296  }
1297 };
1298 
1299 /// Helper class for identifying ordered max predicates.
1301  static bool match(FCmpInst::Predicate Pred) {
1302  return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1303  }
1304 };
1305 
1306 /// Helper class for identifying ordered min predicates.
1308  static bool match(FCmpInst::Predicate Pred) {
1309  return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1310  }
1311 };
1312 
1313 /// Helper class for identifying unordered max predicates.
1315  static bool match(FCmpInst::Predicate Pred) {
1316  return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1317  }
1318 };
1319 
1320 /// Helper class for identifying unordered min predicates.
1322  static bool match(FCmpInst::Predicate Pred) {
1323  return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1324  }
1325 };
1326 
1327 template <typename LHS, typename RHS>
1329  const RHS &R) {
1331 }
1332 
1333 template <typename LHS, typename RHS>
1335  const RHS &R) {
1337 }
1338 
1339 template <typename LHS, typename RHS>
1341  const RHS &R) {
1343 }
1344 
1345 template <typename LHS, typename RHS>
1347  const RHS &R) {
1349 }
1350 
1351 /// Match an 'ordered' floating point maximum function.
1352 /// Floating point has one special value 'NaN'. Therefore, there is no total
1353 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1354 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1355 /// semantics. In the presence of 'NaN' we have to preserve the original
1356 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1357 ///
1358 /// max(L, R) iff L and R are not NaN
1359 /// m_OrdFMax(L, R) = R iff L or R are NaN
1360 template <typename LHS, typename RHS>
1362  const RHS &R) {
1364 }
1365 
1366 /// Match an 'ordered' floating point minimum function.
1367 /// Floating point has one special value 'NaN'. Therefore, there is no total
1368 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1369 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1370 /// semantics. In the presence of 'NaN' we have to preserve the original
1371 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1372 ///
1373 /// min(L, R) iff L and R are not NaN
1374 /// m_OrdFMin(L, R) = R iff L or R are NaN
1375 template <typename LHS, typename RHS>
1377  const RHS &R) {
1379 }
1380 
1381 /// Match an 'unordered' floating point maximum function.
1382 /// Floating point has one special value 'NaN'. Therefore, there is no total
1383 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1384 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1385 /// semantics. In the presence of 'NaN' we have to preserve the original
1386 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1387 ///
1388 /// max(L, R) iff L and R are not NaN
1389 /// m_UnordFMax(L, R) = L iff L or R are NaN
1390 template <typename LHS, typename RHS>
1392 m_UnordFMax(const LHS &L, const RHS &R) {
1394 }
1395 
1396 /// Match an 'unordered' floating point minimum function.
1397 /// Floating point has one special value 'NaN'. Therefore, there is no total
1398 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1399 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1400 /// semantics. In the presence of 'NaN' we have to preserve the original
1401 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1402 ///
1403 /// min(L, R) iff L and R are not NaN
1404 /// m_UnordFMin(L, R) = L iff L or R are NaN
1405 template <typename LHS, typename RHS>
1407 m_UnordFMin(const LHS &L, const RHS &R) {
1409 }
1410 
1411 //===----------------------------------------------------------------------===//
1412 // Matchers for overflow check patterns: e.g. (a + b) u< a
1413 //
1414 
1415 template <typename LHS_t, typename RHS_t, typename Sum_t>
1417  LHS_t L;
1418  RHS_t R;
1419  Sum_t S;
1420 
1421  UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1422  : L(L), R(R), S(S) {}
1423 
1424  template <typename OpTy> bool match(OpTy *V) {
1425  Value *ICmpLHS, *ICmpRHS;
1426  ICmpInst::Predicate Pred;
1427  if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1428  return false;
1429 
1430  Value *AddLHS, *AddRHS;
1431  auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1432 
1433  // (a + b) u< a, (a + b) u< b
1434  if (Pred == ICmpInst::ICMP_ULT)
1435  if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1436  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1437 
1438  // a >u (a + b), b >u (a + b)
1439  if (Pred == ICmpInst::ICMP_UGT)
1440  if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1441  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1442 
1443  return false;
1444  }
1445 };
1446 
1447 /// Match an icmp instruction checking for unsigned overflow on addition.
1448 ///
1449 /// S is matched to the addition whose result is being checked for overflow, and
1450 /// L and R are matched to the LHS and RHS of S.
1451 template <typename LHS_t, typename RHS_t, typename Sum_t>
1453 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1455 }
1456 
1457 template <typename Opnd_t> struct Argument_match {
1458  unsigned OpI;
1459  Opnd_t Val;
1460 
1461  Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1462 
1463  template <typename OpTy> bool match(OpTy *V) {
1464  CallSite CS(V);
1465  return CS.isCall() && Val.match(CS.getArgument(OpI));
1466  }
1467 };
1468 
1469 /// Match an argument.
1470 template <unsigned OpI, typename Opnd_t>
1471 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1472  return Argument_match<Opnd_t>(OpI, Op);
1473 }
1474 
1475 /// Intrinsic matchers.
1477  unsigned ID;
1478 
1479  IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1480 
1481  template <typename OpTy> bool match(OpTy *V) {
1482  if (const auto *CI = dyn_cast<CallInst>(V))
1483  if (const auto *F = CI->getCalledFunction())
1484  return F->getIntrinsicID() == ID;
1485  return false;
1486  }
1487 };
1488 
1489 /// Intrinsic matches are combinations of ID matchers, and argument
1490 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1491 /// them with lower arity matchers. Here's some convenient typedefs for up to
1492 /// several arguments, and more can be added as needed
1493 template <typename T0 = void, typename T1 = void, typename T2 = void,
1494  typename T3 = void, typename T4 = void, typename T5 = void,
1495  typename T6 = void, typename T7 = void, typename T8 = void,
1496  typename T9 = void, typename T10 = void>
1498 template <typename T0> struct m_Intrinsic_Ty<T0> {
1500 };
1501 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1502  using Ty =
1504 };
1505 template <typename T0, typename T1, typename T2>
1506 struct m_Intrinsic_Ty<T0, T1, T2> {
1507  using Ty =
1510 };
1511 template <typename T0, typename T1, typename T2, typename T3>
1512 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1513  using Ty =
1516 };
1517 
1518 /// Match intrinsic calls like this:
1519 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1520 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1521  return IntrinsicID_match(IntrID);
1522 }
1523 
1524 template <Intrinsic::ID IntrID, typename T0>
1525 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1526  return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1527 }
1528 
1529 template <Intrinsic::ID IntrID, typename T0, typename T1>
1530 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1531  const T1 &Op1) {
1532  return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1533 }
1534 
1535 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1536 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1537 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1538  return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1539 }
1540 
1541 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1542  typename T3>
1543 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1544 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1545  return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1546 }
1547 
1548 // Helper intrinsic matching specializations.
1549 template <typename Opnd0>
1550 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1551  return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1552 }
1553 
1554 template <typename Opnd0>
1555 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1556  return m_Intrinsic<Intrinsic::bswap>(Op0);
1557 }
1558 
1559 template <typename Opnd0>
1560 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
1561  return m_Intrinsic<Intrinsic::fabs>(Op0);
1562 }
1563 
1564 template <typename Opnd0>
1565 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
1566  return m_Intrinsic<Intrinsic::canonicalize>(Op0);
1567 }
1568 
1569 template <typename Opnd0, typename Opnd1>
1570 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1571  const Opnd1 &Op1) {
1572  return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1573 }
1574 
1575 template <typename Opnd0, typename Opnd1>
1576 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1577  const Opnd1 &Op1) {
1578  return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1579 }
1580 
1581 //===----------------------------------------------------------------------===//
1582 // Matchers for two-operands operators with the operators in either order
1583 //
1584 
1585 /// Matches a BinaryOperator with LHS and RHS in either order.
1586 template <typename LHS, typename RHS>
1587 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
1588  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
1589 }
1590 
1591 /// Matches an ICmp with a predicate over LHS and RHS in either order.
1592 /// Does not swap the predicate.
1593 template <typename LHS, typename RHS>
1595 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1597  R);
1598 }
1599 
1600 /// Matches a Add with LHS and RHS in either order.
1601 template <typename LHS, typename RHS>
1603  const RHS &R) {
1605 }
1606 
1607 /// Matches a Mul with LHS and RHS in either order.
1608 template <typename LHS, typename RHS>
1610  const RHS &R) {
1612 }
1613 
1614 /// Matches an And with LHS and RHS in either order.
1615 template <typename LHS, typename RHS>
1617  const RHS &R) {
1619 }
1620 
1621 /// Matches an Or with LHS and RHS in either order.
1622 template <typename LHS, typename RHS>
1624  const RHS &R) {
1626 }
1627 
1628 /// Matches an Xor with LHS and RHS in either order.
1629 template <typename LHS, typename RHS>
1631  const RHS &R) {
1633 }
1634 
1635 /// Matches a 'Neg' as 'sub 0, V'.
1636 template <typename ValTy>
1637 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
1638 m_Neg(const ValTy &V) {
1639  return m_Sub(m_ZeroInt(), V);
1640 }
1641 
1642 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1643 template <typename ValTy>
1644 inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1645 m_Not(const ValTy &V) {
1646  return m_c_Xor(V, m_AllOnes());
1647 }
1648 
1649 /// Matches an SMin with LHS and RHS in either order.
1650 template <typename LHS, typename RHS>
1652 m_c_SMin(const LHS &L, const RHS &R) {
1654 }
1655 /// Matches an SMax with LHS and RHS in either order.
1656 template <typename LHS, typename RHS>
1658 m_c_SMax(const LHS &L, const RHS &R) {
1660 }
1661 /// Matches a UMin with LHS and RHS in either order.
1662 template <typename LHS, typename RHS>
1664 m_c_UMin(const LHS &L, const RHS &R) {
1666 }
1667 /// Matches a UMax with LHS and RHS in either order.
1668 template <typename LHS, typename RHS>
1670 m_c_UMax(const LHS &L, const RHS &R) {
1672 }
1673 
1674 /// Matches FAdd with LHS and RHS in either order.
1675 template <typename LHS, typename RHS>
1677 m_c_FAdd(const LHS &L, const RHS &R) {
1679 }
1680 
1681 /// Matches FMul with LHS and RHS in either order.
1682 template <typename LHS, typename RHS>
1684 m_c_FMul(const LHS &L, const RHS &R) {
1686 }
1687 
1688 template <typename Opnd_t> struct Signum_match {
1689  Opnd_t Val;
1690  Signum_match(const Opnd_t &V) : Val(V) {}
1691 
1692  template <typename OpTy> bool match(OpTy *V) {
1693  unsigned TypeSize = V->getType()->getScalarSizeInBits();
1694  if (TypeSize == 0)
1695  return false;
1696 
1697  unsigned ShiftWidth = TypeSize - 1;
1698  Value *OpL = nullptr, *OpR = nullptr;
1699 
1700  // This is the representation of signum we match:
1701  //
1702  // signum(x) == (x >> 63) | (-x >>u 63)
1703  //
1704  // An i1 value is its own signum, so it's correct to match
1705  //
1706  // signum(x) == (x >> 0) | (-x >>u 0)
1707  //
1708  // for i1 values.
1709 
1710  auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
1711  auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
1712  auto Signum = m_Or(LHS, RHS);
1713 
1714  return Signum.match(V) && OpL == OpR && Val.match(OpL);
1715  }
1716 };
1717 
1718 /// Matches a signum pattern.
1719 ///
1720 /// signum(x) =
1721 /// x > 0 -> 1
1722 /// x == 0 -> 0
1723 /// x < 0 -> -1
1724 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1725  return Signum_match<Val_t>(V);
1726 }
1727 
1728 } // end namespace PatternMatch
1729 } // end namespace llvm
1730 
1731 #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:725
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:909
bool isValue(const APFloat &C)
Definition: PatternMatch.h:438
BinaryOp_match< cstfp_pred_ty< is_neg_zero_fp >, RHS, Instruction::FSub > m_FNeg(const RHS &X)
Match &#39;fneg X&#39; as &#39;fsub -0.0, X&#39;.
Definition: PatternMatch.h:665
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:796
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of nonnegative values.
Definition: PatternMatch.h:340
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:72
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:88
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:473
bool isZero() const
Definition: APFloat.h:1143
Match a specified integer value or vector of all elements of that.
Definition: PatternMatch.h:559
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:80
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:651
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:373
br_match(BasicBlock *&Succ)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:645
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:519
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMax(const Opnd0 &Op0, const Opnd1 &Op1)
bool isValue(const APInt &C)
Definition: PatternMatch.h:402
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:701
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:713
bool isValue(const APInt &C)
Definition: PatternMatch.h:357
Exact_match(const SubPattern_t &SP)
Definition: PatternMatch.h:941
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:672
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:677
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:406
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:91
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
unsigned less or equal
Definition: InstrTypes.h:711
unsigned less than
Definition: InstrTypes.h:710
bool isValue(const APInt &C)
Definition: PatternMatch.h:324
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:755
static bool match(ICmpInst::Predicate Pred)
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:691
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:98
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:657
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:361
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
Definition: PatternMatch.h:917
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:537
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:996
Helper class for identifying signed min predicates.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:737
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:369
Exact_match< T > m_Exact(const T &SubPattern)
Definition: PatternMatch.h:950
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:415
bool isValue(const APFloat &C)
Definition: PatternMatch.h:429
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bool isValue(const APFloat &C)
Definition: PatternMatch.h:420
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:316
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:692
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:639
bind_ty< ConstantFP > m_ConstantFP(ConstantFP *&C)
Match a ConstantFP, capturing the value if we match.
Definition: PatternMatch.h:487
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Definition: Instruction.h:174
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:821
#define UINT64_MAX
Definition: DataTypes.h:83
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
Definition: PatternMatch.h:424
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:181
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:125
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:83
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
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:442
IntrinsicID_match(Intrinsic::ID IntrID)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:382
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:837
bool isCall() const
Return true if a CallInst is enclosed.
Definition: CallSite.h:87
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:113
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:63
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:364
#define P(N)
bool isNegZero() const
Definition: APFloat.h:1159
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:749
Helper class for identifying signed max predicates.
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:396
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:177
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:695
This helper class is used to match scalar and vector integer constants that satisfy a specified predi...
Definition: PatternMatch.h:206
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:845
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:719
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
Definition: PatternMatch.h:394
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:731
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
bool isNaN() const
Definition: APFloat.h:1145
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
This is an important base class in LLVM.
Definition: Constant.h:42
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:264
bool isMask(unsigned numBits) const
Definition: APInt.h:495
bool isOneValue() const
Determine if this is a value of 1.
Definition: APInt.h:411
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:306
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:499
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:743
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:685
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:924
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:592
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:75
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:930
Helper class for identifying unordered min predicates.
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:700
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:620
deferredval_ty< Value > m_Deferred(Value *const &V)
A commutative-friendly version of m_Specific().
Definition: PatternMatch.h:512
signed greater than
Definition: InstrTypes.h:712
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:56
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:689
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:328
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:427
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
static bool match(ICmpInst::Predicate Pred)
bool isValue(const APInt &C)
Definition: PatternMatch.h:378
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:186
Match a specified Value*.
Definition: PatternMatch.h:490
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:699
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:707
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:27
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:689
signed less than
Definition: InstrTypes.h:714
bool isOpType(unsigned Opcode)
Definition: PatternMatch.h:895
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:503
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:683
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:788
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:715
Class for arbitrary precision integers.
Definition: APInt.h:70
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:451
bool isPowerOf2() const
Check if this APInt&#39;s value is a power of two greater than zero.
Definition: APInt.h:464
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:902
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:829
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:540
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
bool isPosZero() const
Definition: APFloat.h:1158
unsigned greater or equal
Definition: InstrTypes.h:709
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:967
#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:812
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:323
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:697
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:766
Matches instructions with Opcode and three operands.
bool isOpType(unsigned Opcode)
Definition: PatternMatch.h:873
static bool match(FCmpInst::Predicate Pred)
apfloat_match(const APFloat *&R)
Definition: PatternMatch.h:159
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:73
bool isValue(const APInt &C)
Definition: PatternMatch.h:302
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:698
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:804
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:352
static bool match(ICmpInst::Predicate Pred)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:859
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:131
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:708
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:576
bool isValue(const APInt &C)
Definition: PatternMatch.h:348
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:433
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:690
bool isValue(const APFloat &C)
Definition: PatternMatch.h:447
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:476
#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:406
signed greater or equal
Definition: InstrTypes.h:713
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)
Definition: PatternMatch.h:990