LLVM  7.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 match_zero {
136  template <typename ITy> bool match(ITy *V) {
137  if (const auto *C = dyn_cast<Constant>(V))
138  return C->isNullValue();
139  return false;
140  }
141 };
142 
143 /// Match an arbitrary zero/null constant. This includes
144 /// zero_initializer for vectors and ConstantPointerNull for pointers.
145 inline match_zero m_Zero() { return match_zero(); }
146 
147 struct apint_match {
148  const APInt *&Res;
149 
150  apint_match(const APInt *&R) : Res(R) {}
151 
152  template <typename ITy> bool match(ITy *V) {
153  if (auto *CI = dyn_cast<ConstantInt>(V)) {
154  Res = &CI->getValue();
155  return true;
156  }
157  if (V->getType()->isVectorTy())
158  if (const auto *C = dyn_cast<Constant>(V))
159  if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
160  Res = &CI->getValue();
161  return true;
162  }
163  return false;
164  }
165 };
166 // Either constexpr if or renaming ConstantFP::getValueAPF to
167 // ConstantFP::getValue is needed to do it via single template
168 // function for both apint/apfloat.
170  const APFloat *&Res;
171  apfloat_match(const APFloat *&R) : Res(R) {}
172  template <typename ITy> bool match(ITy *V) {
173  if (auto *CI = dyn_cast<ConstantFP>(V)) {
174  Res = &CI->getValueAPF();
175  return true;
176  }
177  if (V->getType()->isVectorTy())
178  if (const auto *C = dyn_cast<Constant>(V))
179  if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) {
180  Res = &CI->getValueAPF();
181  return true;
182  }
183  return false;
184  }
185 };
186 
187 /// Match a ConstantInt or splatted ConstantVector, binding the
188 /// specified pointer to the contained APInt.
189 inline apint_match m_APInt(const APInt *&Res) { return Res; }
190 
191 /// Match a ConstantFP or splatted ConstantVector, binding the
192 /// specified pointer to the contained APFloat.
193 inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
194 
195 template <int64_t Val> struct constantint_match {
196  template <typename ITy> bool match(ITy *V) {
197  if (const auto *CI = dyn_cast<ConstantInt>(V)) {
198  const APInt &CIV = CI->getValue();
199  if (Val >= 0)
200  return CIV == static_cast<uint64_t>(Val);
201  // If Val is negative, and CI is shorter than it, truncate to the right
202  // number of bits. If it is larger, then we have to sign extend. Just
203  // compare their negated values.
204  return -CIV == -Val;
205  }
206  return false;
207  }
208 };
209 
210 /// Match a ConstantInt with a specific value.
211 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
212  return constantint_match<Val>();
213 }
214 
215 /// This helper class is used to match scalar and vector integer constants that
216 /// satisfy a specified predicate.
217 /// For vector constants, undefined elements are ignored.
218 template <typename Predicate> struct cst_pred_ty : public Predicate {
219  template <typename ITy> bool match(ITy *V) {
220  if (const auto *CI = dyn_cast<ConstantInt>(V))
221  return this->isValue(CI->getValue());
222  if (V->getType()->isVectorTy()) {
223  if (const auto *C = dyn_cast<Constant>(V)) {
224  if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
225  return this->isValue(CI->getValue());
226 
227  // Non-splat vector constant: check each element for a match.
228  unsigned NumElts = V->getType()->getVectorNumElements();
229  assert(NumElts != 0 && "Constant vector with no elements?");
230  for (unsigned i = 0; i != NumElts; ++i) {
231  Constant *Elt = C->getAggregateElement(i);
232  if (!Elt)
233  return false;
234  if (isa<UndefValue>(Elt))
235  continue;
236  auto *CI = dyn_cast<ConstantInt>(Elt);
237  if (!CI || !this->isValue(CI->getValue()))
238  return false;
239  }
240  return true;
241  }
242  }
243  return false;
244  }
245 };
246 
247 /// This helper class is used to match scalar and vector constants that
248 /// satisfy a specified predicate, and bind them to an APInt.
249 template <typename Predicate> struct api_pred_ty : public Predicate {
250  const APInt *&Res;
251 
252  api_pred_ty(const APInt *&R) : Res(R) {}
253 
254  template <typename ITy> bool match(ITy *V) {
255  if (const auto *CI = dyn_cast<ConstantInt>(V))
256  if (this->isValue(CI->getValue())) {
257  Res = &CI->getValue();
258  return true;
259  }
260  if (V->getType()->isVectorTy())
261  if (const auto *C = dyn_cast<Constant>(V))
262  if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
263  if (this->isValue(CI->getValue())) {
264  Res = &CI->getValue();
265  return true;
266  }
267 
268  return false;
269  }
270 };
271 
272 /// This helper class is used to match scalar and vector floating-point
273 /// constants that satisfy a specified predicate.
274 /// For vector constants, undefined elements are ignored.
275 template <typename Predicate> struct cstfp_pred_ty : public Predicate {
276  template <typename ITy> bool match(ITy *V) {
277  if (const auto *CF = dyn_cast<ConstantFP>(V))
278  return this->isValue(CF->getValueAPF());
279  if (V->getType()->isVectorTy()) {
280  if (const auto *C = dyn_cast<Constant>(V)) {
281  if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
282  return this->isValue(CF->getValueAPF());
283 
284  // Non-splat vector constant: check each element for a match.
285  unsigned NumElts = V->getType()->getVectorNumElements();
286  assert(NumElts != 0 && "Constant vector with no elements?");
287  for (unsigned i = 0; i != NumElts; ++i) {
288  Constant *Elt = C->getAggregateElement(i);
289  if (!Elt)
290  return false;
291  if (isa<UndefValue>(Elt))
292  continue;
293  auto *CF = dyn_cast<ConstantFP>(Elt);
294  if (!CF || !this->isValue(CF->getValueAPF()))
295  return false;
296  }
297  return true;
298  }
299  }
300  return false;
301  }
302 };
303 
304 ///////////////////////////////////////////////////////////////////////////////
305 //
306 // Encapsulate constant value queries for use in templated predicate matchers.
307 // This allows checking if constants match using compound predicates and works
308 // with vector constants, possibly with relaxed constraints. For example, ignore
309 // undef values.
310 //
311 ///////////////////////////////////////////////////////////////////////////////
312 
313 struct is_all_ones {
314  bool isValue(const APInt &C) { return C.isAllOnesValue(); }
315 };
316 /// Match an integer or vector with all bits set.
317 /// For vectors, this includes constants with undefined elements.
319  return cst_pred_ty<is_all_ones>();
320 }
321 
323  bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
324 };
325 /// Match an integer or vector with values having all bits except for the high
326 /// bit set (0x7f...).
327 /// For vectors, this includes constants with undefined elements.
330 }
332  return V;
333 }
334 
335 struct is_negative {
336  bool isValue(const APInt &C) { return C.isNegative(); }
337 };
338 /// Match an integer or vector of negative values.
339 /// For vectors, this includes constants with undefined elements.
341  return cst_pred_ty<is_negative>();
342 }
344  return V;
345 }
346 
348  bool isValue(const APInt &C) { return C.isNonNegative(); }
349 };
350 /// Match an integer or vector of nonnegative values.
351 /// For vectors, this includes constants with undefined elements.
354 }
356  return V;
357 }
358 
359 struct is_one {
360  bool isValue(const APInt &C) { return C.isOneValue(); }
361 };
362 /// Match an integer 1 or a vector with all elements equal to 1.
363 /// For vectors, this includes constants with undefined elements.
365  return cst_pred_ty<is_one>();
366 }
367 
368 struct is_power2 {
369  bool isValue(const APInt &C) { return C.isPowerOf2(); }
370 };
371 /// Match an integer or vector power-of-2.
372 /// For vectors, this includes constants with undefined elements.
374  return cst_pred_ty<is_power2>();
375 }
377  return V;
378 }
379 
381  bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
382 };
383 /// Match an integer or vector of 0 or power-of-2 values.
384 /// For vectors, this includes constants with undefined elements.
387 }
389  return V;
390 }
391 
392 struct is_sign_mask {
393  bool isValue(const APInt &C) { return C.isSignMask(); }
394 };
395 /// Match an integer or vector with only the sign bit(s) set.
396 /// For vectors, this includes constants with undefined elements.
398  return cst_pred_ty<is_sign_mask>();
399 }
400 
401 struct is_nan {
402  bool isValue(const APFloat &C) { return C.isNaN(); }
403 };
404 /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
405 /// For vectors, this includes constants with undefined elements.
407  return cstfp_pred_ty<is_nan>();
408 }
409 
411  bool isValue(const APFloat &C) { return C.isZero(); }
412 };
413 /// Match a floating-point negative zero or positive zero.
414 /// For vectors, this includes constants with undefined elements.
417 }
418 
420  bool isValue(const APFloat &C) { return C.isPosZero(); }
421 };
422 /// Match a floating-point positive zero.
423 /// For vectors, this includes constants with undefined elements.
426 }
427 
429  bool isValue(const APFloat &C) { return C.isNegZero(); }
430 };
431 /// Match a floating-point negative zero.
432 /// For vectors, this includes constants with undefined elements.
435 }
436 
437 ///////////////////////////////////////////////////////////////////////////////
438 
439 template <typename Class> struct bind_ty {
440  Class *&VR;
441 
442  bind_ty(Class *&V) : VR(V) {}
443 
444  template <typename ITy> bool match(ITy *V) {
445  if (auto *CV = dyn_cast<Class>(V)) {
446  VR = CV;
447  return true;
448  }
449  return false;
450  }
451 };
452 
453 /// Match a value, capturing it if we match.
454 inline bind_ty<Value> m_Value(Value *&V) { return V; }
455 inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
456 
457 /// Match an instruction, capturing it if we match.
459 /// Match a binary operator, capturing it if we match.
461 
462 /// Match a ConstantInt, capturing the value if we match.
463 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
464 
465 /// Match a Constant, capturing the value if we match.
466 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
467 
468 /// Match a ConstantFP, capturing the value if we match.
470 
471 /// Match a specified Value*.
473  const Value *Val;
474 
475  specificval_ty(const Value *V) : Val(V) {}
476 
477  template <typename ITy> bool match(ITy *V) { return V == Val; }
478 };
479 
480 /// Match if we have a specific specified value.
481 inline specificval_ty m_Specific(const Value *V) { return V; }
482 
483 /// Match a specified floating point value or vector of all elements of
484 /// that value.
486  double Val;
487 
488  specific_fpval(double V) : Val(V) {}
489 
490  template <typename ITy> bool match(ITy *V) {
491  if (const auto *CFP = dyn_cast<ConstantFP>(V))
492  return CFP->isExactlyValue(Val);
493  if (V->getType()->isVectorTy())
494  if (const auto *C = dyn_cast<Constant>(V))
495  if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
496  return CFP->isExactlyValue(Val);
497  return false;
498  }
499 };
500 
501 /// Match a specific floating point value or vector with all elements
502 /// equal to the value.
503 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
504 
505 /// Match a float 1.0 or vector with all elements equal to 1.0.
506 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
507 
509  uint64_t &VR;
510 
511  bind_const_intval_ty(uint64_t &V) : VR(V) {}
512 
513  template <typename ITy> bool match(ITy *V) {
514  if (const auto *CV = dyn_cast<ConstantInt>(V))
515  if (CV->getValue().ule(UINT64_MAX)) {
516  VR = CV->getZExtValue();
517  return true;
518  }
519  return false;
520  }
521 };
522 
523 /// Match a specified integer value or vector of all elements of that
524 // value.
526  uint64_t Val;
527 
528  specific_intval(uint64_t V) : Val(V) {}
529 
530  template <typename ITy> bool match(ITy *V) {
531  const auto *CI = dyn_cast<ConstantInt>(V);
532  if (!CI && V->getType()->isVectorTy())
533  if (const auto *C = dyn_cast<Constant>(V))
534  CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
535 
536  return CI && CI->getValue() == Val;
537  }
538 };
539 
540 /// Match a specific integer value or vector with all elements equal to
541 /// the value.
542 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
543 
544 /// Match a ConstantInt and bind to its value. This does not match
545 /// ConstantInts wider than 64-bits.
546 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
547 
548 //===----------------------------------------------------------------------===//
549 // Matcher for any binary operator.
550 //
551 template <typename LHS_t, typename RHS_t, bool Commutable = false>
553  LHS_t L;
554  RHS_t R;
555 
556  AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
557 
558  template <typename OpTy> bool match(OpTy *V) {
559  if (auto *I = dyn_cast<BinaryOperator>(V))
560  return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
561  (Commutable && R.match(I->getOperand(0)) &&
562  L.match(I->getOperand(1)));
563  return false;
564  }
565 };
566 
567 template <typename LHS, typename RHS>
568 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
569  return AnyBinaryOp_match<LHS, RHS>(L, R);
570 }
571 
572 //===----------------------------------------------------------------------===//
573 // Matchers for specific binary operators.
574 //
575 
576 template <typename LHS_t, typename RHS_t, unsigned Opcode,
577  bool Commutable = false>
579  LHS_t L;
580  RHS_t R;
581 
582  BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
583 
584  template <typename OpTy> bool match(OpTy *V) {
585  if (V->getValueID() == Value::InstructionVal + Opcode) {
586  auto *I = cast<BinaryOperator>(V);
587  return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
588  (Commutable && R.match(I->getOperand(0)) &&
589  L.match(I->getOperand(1)));
590  }
591  if (auto *CE = dyn_cast<ConstantExpr>(V))
592  return CE->getOpcode() == Opcode &&
593  ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
594  (Commutable && R.match(CE->getOperand(0)) &&
595  L.match(CE->getOperand(1))));
596  return false;
597  }
598 };
599 
600 template <typename LHS, typename RHS>
602  const RHS &R) {
604 }
605 
606 template <typename LHS, typename RHS>
608  const RHS &R) {
610 }
611 
612 template <typename LHS, typename RHS>
614  const RHS &R) {
616 }
617 
618 template <typename LHS, typename RHS>
620  const RHS &R) {
622 }
623 
624 /// Match 'fneg X' as 'fsub -0.0, X'.
625 template <typename RHS>
626 inline BinaryOp_match<cstfp_pred_ty<is_neg_zero_fp>, RHS, Instruction::FSub>
627 m_FNeg(const RHS &X) {
628  return m_FSub(m_NegZeroFP(), X);
629 }
630 
631 template <typename LHS, typename RHS>
633  const RHS &R) {
635 }
636 
637 template <typename LHS, typename RHS>
639  const RHS &R) {
641 }
642 
643 template <typename LHS, typename RHS>
645  const RHS &R) {
647 }
648 
649 template <typename LHS, typename RHS>
651  const RHS &R) {
653 }
654 
655 template <typename LHS, typename RHS>
657  const RHS &R) {
659 }
660 
661 template <typename LHS, typename RHS>
663  const RHS &R) {
665 }
666 
667 template <typename LHS, typename RHS>
669  const RHS &R) {
671 }
672 
673 template <typename LHS, typename RHS>
675  const RHS &R) {
677 }
678 
679 template <typename LHS, typename RHS>
681  const RHS &R) {
683 }
684 
685 template <typename LHS, typename RHS>
687  const RHS &R) {
689 }
690 
691 template <typename LHS, typename RHS>
693  const RHS &R) {
695 }
696 
697 template <typename LHS, typename RHS>
699  const RHS &R) {
701 }
702 
703 template <typename LHS, typename RHS>
705  const RHS &R) {
707 }
708 
709 template <typename LHS, typename RHS>
711  const RHS &R) {
713 }
714 
715 template <typename LHS_t, typename RHS_t, unsigned Opcode,
716  unsigned WrapFlags = 0>
718  LHS_t L;
719  RHS_t R;
720 
721  OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
722  : L(LHS), R(RHS) {}
723 
724  template <typename OpTy> bool match(OpTy *V) {
725  if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
726  if (Op->getOpcode() != Opcode)
727  return false;
729  !Op->hasNoUnsignedWrap())
730  return false;
731  if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
732  !Op->hasNoSignedWrap())
733  return false;
734  return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
735  }
736  return false;
737  }
738 };
739 
740 template <typename LHS, typename RHS>
743 m_NSWAdd(const LHS &L, const RHS &R) {
746  L, R);
747 }
748 template <typename LHS, typename RHS>
749 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
751 m_NSWSub(const LHS &L, const RHS &R) {
752  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
754  L, R);
755 }
756 template <typename LHS, typename RHS>
757 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
759 m_NSWMul(const LHS &L, const RHS &R) {
760  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
762  L, R);
763 }
764 template <typename LHS, typename RHS>
765 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
767 m_NSWShl(const LHS &L, const RHS &R) {
768  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
770  L, R);
771 }
772 
773 template <typename LHS, typename RHS>
776 m_NUWAdd(const LHS &L, const RHS &R) {
779  L, R);
780 }
781 template <typename LHS, typename RHS>
782 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
784 m_NUWSub(const LHS &L, const RHS &R) {
785  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
787  L, R);
788 }
789 template <typename LHS, typename RHS>
790 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
792 m_NUWMul(const LHS &L, const RHS &R) {
793  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
795  L, R);
796 }
797 template <typename LHS, typename RHS>
798 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
800 m_NUWShl(const LHS &L, const RHS &R) {
801  return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
803  L, R);
804 }
805 
806 //===----------------------------------------------------------------------===//
807 // Class that matches a group of binary opcodes.
808 //
809 template <typename LHS_t, typename RHS_t, typename Predicate>
811  LHS_t L;
812  RHS_t R;
813 
814  BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
815 
816  template <typename OpTy> bool match(OpTy *V) {
817  if (auto *I = dyn_cast<Instruction>(V))
818  return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
819  R.match(I->getOperand(1));
820  if (auto *CE = dyn_cast<ConstantExpr>(V))
821  return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
822  R.match(CE->getOperand(1));
823  return false;
824  }
825 };
826 
827 struct is_shift_op {
828  bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
829 };
830 
832  bool isOpType(unsigned Opcode) {
833  return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
834  }
835 };
836 
838  bool isOpType(unsigned Opcode) {
839  return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
840  }
841 };
842 
844  bool isOpType(unsigned Opcode) {
845  return Instruction::isBitwiseLogicOp(Opcode);
846  }
847 };
848 
849 struct is_idiv_op {
850  bool isOpType(unsigned Opcode) {
851  return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
852  }
853 };
854 
855 /// Matches shift operations.
856 template <typename LHS, typename RHS>
858  const RHS &R) {
860 }
861 
862 /// Matches logical shift operations.
863 template <typename LHS, typename RHS>
865  const RHS &R) {
867 }
868 
869 /// Matches logical shift operations.
870 template <typename LHS, typename RHS>
872 m_LogicalShift(const LHS &L, const RHS &R) {
874 }
875 
876 /// Matches bitwise logic operations.
877 template <typename LHS, typename RHS>
879 m_BitwiseLogic(const LHS &L, const RHS &R) {
881 }
882 
883 /// Matches integer division operations.
884 template <typename LHS, typename RHS>
886  const RHS &R) {
888 }
889 
890 //===----------------------------------------------------------------------===//
891 // Class that matches exact binary ops.
892 //
893 template <typename SubPattern_t> struct Exact_match {
894  SubPattern_t SubPattern;
895 
896  Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
897 
898  template <typename OpTy> bool match(OpTy *V) {
899  if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
900  return PEO->isExact() && SubPattern.match(V);
901  return false;
902  }
903 };
904 
905 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
906  return SubPattern;
907 }
908 
909 //===----------------------------------------------------------------------===//
910 // Matchers for CmpInst classes
911 //
912 
913 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
914  bool Commutable = false>
916  PredicateTy &Predicate;
917  LHS_t L;
918  RHS_t R;
919 
920  CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
921  : Predicate(Pred), L(LHS), R(RHS) {}
922 
923  template <typename OpTy> bool match(OpTy *V) {
924  if (auto *I = dyn_cast<Class>(V))
925  if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
926  (Commutable && R.match(I->getOperand(0)) &&
927  L.match(I->getOperand(1)))) {
928  Predicate = I->getPredicate();
929  return true;
930  }
931  return false;
932  }
933 };
934 
935 template <typename LHS, typename RHS>
937 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
939 }
940 
941 template <typename LHS, typename RHS>
943 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
945 }
946 
947 template <typename LHS, typename RHS>
949 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
951 }
952 
953 //===----------------------------------------------------------------------===//
954 // Matchers for SelectInst classes
955 //
956 
957 template <typename Cond_t, typename LHS_t, typename RHS_t>
959  Cond_t C;
960  LHS_t L;
961  RHS_t R;
962 
963  SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
964  : C(Cond), L(LHS), R(RHS) {}
965 
966  template <typename OpTy> bool match(OpTy *V) {
967  if (auto *I = dyn_cast<SelectInst>(V))
968  return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
969  R.match(I->getOperand(2));
970  return false;
971  }
972 };
973 
974 template <typename Cond, typename LHS, typename RHS>
975 inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
976  const RHS &R) {
977  return SelectClass_match<Cond, LHS, RHS>(C, L, R);
978 }
979 
980 /// This matches a select of two constants, e.g.:
981 /// m_SelectCst<-1, 0>(m_Value(V))
982 template <int64_t L, int64_t R, typename Cond>
984 m_SelectCst(const Cond &C) {
985  return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
986 }
987 
988 //===----------------------------------------------------------------------===//
989 // Matchers for InsertElementInst classes
990 //
991 
992 template <typename Val_t, typename Elt_t, typename Idx_t>
994  Val_t V;
995  Elt_t E;
996  Idx_t I;
997 
998  InsertElementClass_match(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
999  : V(Val), E(Elt), I(Idx) {}
1000 
1001  template <typename OpTy> bool match(OpTy *VV) {
1002  if (auto *II = dyn_cast<InsertElementInst>(VV))
1003  return V.match(II->getOperand(0)) && E.match(II->getOperand(1)) &&
1004  I.match(II->getOperand(2));
1005  return false;
1006  }
1007 };
1008 
1009 template <typename Val_t, typename Elt_t, typename Idx_t>
1011 m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1012  return InsertElementClass_match<Val_t, Elt_t, Idx_t>(Val, Elt, Idx);
1013 }
1014 
1015 //===----------------------------------------------------------------------===//
1016 // Matchers for ExtractElementInst classes
1017 //
1018 
1019 template <typename Val_t, typename Idx_t> struct ExtractElementClass_match {
1020  Val_t V;
1021  Idx_t I;
1022 
1023  ExtractElementClass_match(const Val_t &Val, const Idx_t &Idx)
1024  : V(Val), I(Idx) {}
1025 
1026  template <typename OpTy> bool match(OpTy *VV) {
1027  if (auto *II = dyn_cast<ExtractElementInst>(VV))
1028  return V.match(II->getOperand(0)) && I.match(II->getOperand(1));
1029  return false;
1030  }
1031 };
1032 
1033 template <typename Val_t, typename Idx_t>
1035 m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
1036  return ExtractElementClass_match<Val_t, Idx_t>(Val, Idx);
1037 }
1038 
1039 //===----------------------------------------------------------------------===//
1040 // Matchers for ShuffleVectorInst classes
1041 //
1042 
1043 template <typename V1_t, typename V2_t, typename Mask_t>
1045  V1_t V1;
1046  V2_t V2;
1047  Mask_t M;
1048 
1049  ShuffleVectorClass_match(const V1_t &v1, const V2_t &v2, const Mask_t &m)
1050  : V1(v1), V2(v2), M(m) {}
1051 
1052  template <typename OpTy> bool match(OpTy *V) {
1053  if (auto *SI = dyn_cast<ShuffleVectorInst>(V))
1054  return V1.match(SI->getOperand(0)) && V2.match(SI->getOperand(1)) &&
1055  M.match(SI->getOperand(2));
1056  return false;
1057  }
1058 };
1059 
1060 template <typename V1_t, typename V2_t, typename Mask_t>
1062 m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
1064 }
1065 
1066 //===----------------------------------------------------------------------===//
1067 // Matchers for CastInst classes
1068 //
1069 
1070 template <typename Op_t, unsigned Opcode> struct CastClass_match {
1071  Op_t Op;
1072 
1073  CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1074 
1075  template <typename OpTy> bool match(OpTy *V) {
1076  if (auto *O = dyn_cast<Operator>(V))
1077  return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1078  return false;
1079  }
1080 };
1081 
1082 /// Matches BitCast.
1083 template <typename OpTy>
1086 }
1087 
1088 /// Matches PtrToInt.
1089 template <typename OpTy>
1092 }
1093 
1094 /// Matches Trunc.
1095 template <typename OpTy>
1098 }
1099 
1100 /// Matches SExt.
1101 template <typename OpTy>
1104 }
1105 
1106 /// Matches ZExt.
1107 template <typename OpTy>
1110 }
1111 
1112 template <typename OpTy>
1115 m_ZExtOrSExt(const OpTy &Op) {
1116  return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1117 }
1118 
1119 /// Matches UIToFP.
1120 template <typename OpTy>
1123 }
1124 
1125 /// Matches SIToFP.
1126 template <typename OpTy>
1129 }
1130 
1131 /// Matches FPTrunc
1132 template <typename OpTy>
1135 }
1136 
1137 /// Matches FPExt
1138 template <typename OpTy>
1141 }
1142 
1143 //===----------------------------------------------------------------------===//
1144 // Matcher for LoadInst classes
1145 //
1146 
1147 template <typename Op_t> struct LoadClass_match {
1148  Op_t Op;
1149 
1150  LoadClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1151 
1152  template <typename OpTy> bool match(OpTy *V) {
1153  if (auto *LI = dyn_cast<LoadInst>(V))
1154  return Op.match(LI->getPointerOperand());
1155  return false;
1156  }
1157 };
1158 
1159 /// Matches LoadInst.
1160 template <typename OpTy> inline LoadClass_match<OpTy> m_Load(const OpTy &Op) {
1161  return LoadClass_match<OpTy>(Op);
1162 }
1163 
1164 //===----------------------------------------------------------------------===//
1165 // Matchers for unary operators
1166 //
1167 
1168 template <typename LHS_t> struct neg_match {
1169  LHS_t L;
1170 
1171  neg_match(const LHS_t &LHS) : L(LHS) {}
1172 
1173  template <typename OpTy> bool match(OpTy *V) {
1174  if (auto *O = dyn_cast<Operator>(V))
1175  if (O->getOpcode() == Instruction::Sub)
1176  return matchIfNeg(O->getOperand(0), O->getOperand(1));
1177  return false;
1178  }
1179 
1180 private:
1181  bool matchIfNeg(Value *LHS, Value *RHS) {
1182  return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
1183  isa<ConstantAggregateZero>(LHS)) &&
1184  L.match(RHS);
1185  }
1186 };
1187 
1188 /// Match an integer negate.
1189 template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
1190 
1191 //===----------------------------------------------------------------------===//
1192 // Matchers for control flow.
1193 //
1194 
1195 struct br_match {
1197 
1198  br_match(BasicBlock *&Succ) : Succ(Succ) {}
1199 
1200  template <typename OpTy> bool match(OpTy *V) {
1201  if (auto *BI = dyn_cast<BranchInst>(V))
1202  if (BI->isUnconditional()) {
1203  Succ = BI->getSuccessor(0);
1204  return true;
1205  }
1206  return false;
1207  }
1208 };
1209 
1210 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1211 
1212 template <typename Cond_t> struct brc_match {
1213  Cond_t Cond;
1215 
1216  brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
1217  : Cond(C), T(t), F(f) {}
1218 
1219  template <typename OpTy> bool match(OpTy *V) {
1220  if (auto *BI = dyn_cast<BranchInst>(V))
1221  if (BI->isConditional() && Cond.match(BI->getCondition())) {
1222  T = BI->getSuccessor(0);
1223  F = BI->getSuccessor(1);
1224  return true;
1225  }
1226  return false;
1227  }
1228 };
1229 
1230 template <typename Cond_t>
1231 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1232  return brc_match<Cond_t>(C, T, F);
1233 }
1234 
1235 //===----------------------------------------------------------------------===//
1236 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1237 //
1238 
1239 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1240  bool Commutable = false>
1242  LHS_t L;
1243  RHS_t R;
1244 
1245  MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1246 
1247  template <typename OpTy> bool match(OpTy *V) {
1248  // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1249  auto *SI = dyn_cast<SelectInst>(V);
1250  if (!SI)
1251  return false;
1252  auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1253  if (!Cmp)
1254  return false;
1255  // At this point we have a select conditioned on a comparison. Check that
1256  // it is the values returned by the select that are being compared.
1257  Value *TrueVal = SI->getTrueValue();
1258  Value *FalseVal = SI->getFalseValue();
1259  Value *LHS = Cmp->getOperand(0);
1260  Value *RHS = Cmp->getOperand(1);
1261  if ((TrueVal != LHS || FalseVal != RHS) &&
1262  (TrueVal != RHS || FalseVal != LHS))
1263  return false;
1264  typename CmpInst_t::Predicate Pred =
1265  LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1266  // Does "(x pred y) ? x : y" represent the desired max/min operation?
1267  if (!Pred_t::match(Pred))
1268  return false;
1269  // It does! Bind the operands.
1270  return (L.match(LHS) && R.match(RHS)) ||
1271  (Commutable && R.match(LHS) && L.match(RHS));
1272  }
1273 };
1274 
1275 /// Helper class for identifying signed max predicates.
1277  static bool match(ICmpInst::Predicate Pred) {
1278  return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1279  }
1280 };
1281 
1282 /// Helper class for identifying signed min predicates.
1284  static bool match(ICmpInst::Predicate Pred) {
1285  return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1286  }
1287 };
1288 
1289 /// Helper class for identifying unsigned max predicates.
1291  static bool match(ICmpInst::Predicate Pred) {
1292  return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1293  }
1294 };
1295 
1296 /// Helper class for identifying unsigned min predicates.
1298  static bool match(ICmpInst::Predicate Pred) {
1299  return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1300  }
1301 };
1302 
1303 /// Helper class for identifying ordered max predicates.
1305  static bool match(FCmpInst::Predicate Pred) {
1306  return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1307  }
1308 };
1309 
1310 /// Helper class for identifying ordered min predicates.
1312  static bool match(FCmpInst::Predicate Pred) {
1313  return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1314  }
1315 };
1316 
1317 /// Helper class for identifying unordered max predicates.
1319  static bool match(FCmpInst::Predicate Pred) {
1320  return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1321  }
1322 };
1323 
1324 /// Helper class for identifying unordered min predicates.
1326  static bool match(FCmpInst::Predicate Pred) {
1327  return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1328  }
1329 };
1330 
1331 template <typename LHS, typename RHS>
1333  const RHS &R) {
1335 }
1336 
1337 template <typename LHS, typename RHS>
1339  const RHS &R) {
1341 }
1342 
1343 template <typename LHS, typename RHS>
1345  const RHS &R) {
1347 }
1348 
1349 template <typename LHS, typename RHS>
1351  const RHS &R) {
1353 }
1354 
1355 /// Match an 'ordered' floating point maximum function.
1356 /// Floating point has one special value 'NaN'. Therefore, there is no total
1357 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1358 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1359 /// semantics. In the presence of 'NaN' we have to preserve the original
1360 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1361 ///
1362 /// max(L, R) iff L and R are not NaN
1363 /// m_OrdFMax(L, R) = R iff L or R are NaN
1364 template <typename LHS, typename RHS>
1366  const RHS &R) {
1368 }
1369 
1370 /// Match an 'ordered' floating point minimum function.
1371 /// Floating point has one special value 'NaN'. Therefore, there is no total
1372 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1373 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1374 /// semantics. In the presence of 'NaN' we have to preserve the original
1375 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1376 ///
1377 /// min(L, R) iff L and R are not NaN
1378 /// m_OrdFMin(L, R) = R iff L or R are NaN
1379 template <typename LHS, typename RHS>
1381  const RHS &R) {
1383 }
1384 
1385 /// Match an 'unordered' floating point maximum function.
1386 /// Floating point has one special value 'NaN'. Therefore, there is no total
1387 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1388 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1389 /// semantics. In the presence of 'NaN' we have to preserve the original
1390 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1391 ///
1392 /// max(L, R) iff L and R are not NaN
1393 /// m_UnordFMax(L, R) = L iff L or R are NaN
1394 template <typename LHS, typename RHS>
1396 m_UnordFMax(const LHS &L, const RHS &R) {
1398 }
1399 
1400 /// Match an 'unordered' floating point minimum function.
1401 /// Floating point has one special value 'NaN'. Therefore, there is no total
1402 /// order. However, if we can ignore the 'NaN' value (for example, because of a
1403 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1404 /// semantics. In the presence of 'NaN' we have to preserve the original
1405 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1406 ///
1407 /// min(L, R) iff L and R are not NaN
1408 /// m_UnordFMin(L, R) = L iff L or R are NaN
1409 template <typename LHS, typename RHS>
1411 m_UnordFMin(const LHS &L, const RHS &R) {
1413 }
1414 
1415 //===----------------------------------------------------------------------===//
1416 // Matchers for overflow check patterns: e.g. (a + b) u< a
1417 //
1418 
1419 template <typename LHS_t, typename RHS_t, typename Sum_t>
1421  LHS_t L;
1422  RHS_t R;
1423  Sum_t S;
1424 
1425  UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1426  : L(L), R(R), S(S) {}
1427 
1428  template <typename OpTy> bool match(OpTy *V) {
1429  Value *ICmpLHS, *ICmpRHS;
1430  ICmpInst::Predicate Pred;
1431  if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1432  return false;
1433 
1434  Value *AddLHS, *AddRHS;
1435  auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1436 
1437  // (a + b) u< a, (a + b) u< b
1438  if (Pred == ICmpInst::ICMP_ULT)
1439  if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1440  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1441 
1442  // a >u (a + b), b >u (a + b)
1443  if (Pred == ICmpInst::ICMP_UGT)
1444  if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1445  return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1446 
1447  return false;
1448  }
1449 };
1450 
1451 /// Match an icmp instruction checking for unsigned overflow on addition.
1452 ///
1453 /// S is matched to the addition whose result is being checked for overflow, and
1454 /// L and R are matched to the LHS and RHS of S.
1455 template <typename LHS_t, typename RHS_t, typename Sum_t>
1457 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1459 }
1460 
1461 template <typename Opnd_t> struct Argument_match {
1462  unsigned OpI;
1463  Opnd_t Val;
1464 
1465  Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1466 
1467  template <typename OpTy> bool match(OpTy *V) {
1468  CallSite CS(V);
1469  return CS.isCall() && Val.match(CS.getArgument(OpI));
1470  }
1471 };
1472 
1473 /// Match an argument.
1474 template <unsigned OpI, typename Opnd_t>
1475 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1476  return Argument_match<Opnd_t>(OpI, Op);
1477 }
1478 
1479 /// Intrinsic matchers.
1481  unsigned ID;
1482 
1483  IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1484 
1485  template <typename OpTy> bool match(OpTy *V) {
1486  if (const auto *CI = dyn_cast<CallInst>(V))
1487  if (const auto *F = CI->getCalledFunction())
1488  return F->getIntrinsicID() == ID;
1489  return false;
1490  }
1491 };
1492 
1493 /// Intrinsic matches are combinations of ID matchers, and argument
1494 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
1495 /// them with lower arity matchers. Here's some convenient typedefs for up to
1496 /// several arguments, and more can be added as needed
1497 template <typename T0 = void, typename T1 = void, typename T2 = void,
1498  typename T3 = void, typename T4 = void, typename T5 = void,
1499  typename T6 = void, typename T7 = void, typename T8 = void,
1500  typename T9 = void, typename T10 = void>
1502 template <typename T0> struct m_Intrinsic_Ty<T0> {
1504 };
1505 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1506  using Ty =
1508 };
1509 template <typename T0, typename T1, typename T2>
1510 struct m_Intrinsic_Ty<T0, T1, T2> {
1511  using Ty =
1514 };
1515 template <typename T0, typename T1, typename T2, typename T3>
1516 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1517  using Ty =
1520 };
1521 
1522 /// Match intrinsic calls like this:
1523 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1524 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1525  return IntrinsicID_match(IntrID);
1526 }
1527 
1528 template <Intrinsic::ID IntrID, typename T0>
1529 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1530  return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1531 }
1532 
1533 template <Intrinsic::ID IntrID, typename T0, typename T1>
1534 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1535  const T1 &Op1) {
1536  return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1537 }
1538 
1539 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1540 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1541 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1542  return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1543 }
1544 
1545 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1546  typename T3>
1547 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1548 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1549  return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1550 }
1551 
1552 // Helper intrinsic matching specializations.
1553 template <typename Opnd0>
1554 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1555  return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1556 }
1557 
1558 template <typename Opnd0>
1559 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1560  return m_Intrinsic<Intrinsic::bswap>(Op0);
1561 }
1562 
1563 template <typename Opnd0, typename Opnd1>
1564 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1565  const Opnd1 &Op1) {
1566  return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1567 }
1568 
1569 template <typename Opnd0, typename Opnd1>
1570 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1571  const Opnd1 &Op1) {
1572  return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1573 }
1574 
1575 template <typename Opnd_t> struct Signum_match {
1576  Opnd_t Val;
1577  Signum_match(const Opnd_t &V) : Val(V) {}
1578 
1579  template <typename OpTy> bool match(OpTy *V) {
1580  unsigned TypeSize = V->getType()->getScalarSizeInBits();
1581  if (TypeSize == 0)
1582  return false;
1583 
1584  unsigned ShiftWidth = TypeSize - 1;
1585  Value *OpL = nullptr, *OpR = nullptr;
1586 
1587  // This is the representation of signum we match:
1588  //
1589  // signum(x) == (x >> 63) | (-x >>u 63)
1590  //
1591  // An i1 value is its own signum, so it's correct to match
1592  //
1593  // signum(x) == (x >> 0) | (-x >>u 0)
1594  //
1595  // for i1 values.
1596 
1597  auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
1598  auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
1599  auto Signum = m_Or(LHS, RHS);
1600 
1601  return Signum.match(V) && OpL == OpR && Val.match(OpL);
1602  }
1603 };
1604 
1605 /// Matches a signum pattern.
1606 ///
1607 /// signum(x) =
1608 /// x > 0 -> 1
1609 /// x == 0 -> 0
1610 /// x < 0 -> -1
1611 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1612  return Signum_match<Val_t>(V);
1613 }
1614 
1615 //===----------------------------------------------------------------------===//
1616 // Matchers for two-operands operators with the operators in either order
1617 //
1618 
1619 /// Matches a BinaryOperator with LHS and RHS in either order.
1620 template <typename LHS, typename RHS>
1621 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
1622  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
1623 }
1624 
1625 /// Matches an ICmp with a predicate over LHS and RHS in either order.
1626 /// Does not swap the predicate.
1627 template <typename LHS, typename RHS>
1629 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1631  R);
1632 }
1633 
1634 /// Matches a Add with LHS and RHS in either order.
1635 template <typename LHS, typename RHS>
1637  const RHS &R) {
1639 }
1640 
1641 /// Matches a Mul with LHS and RHS in either order.
1642 template <typename LHS, typename RHS>
1644  const RHS &R) {
1646 }
1647 
1648 /// Matches an And with LHS and RHS in either order.
1649 template <typename LHS, typename RHS>
1651  const RHS &R) {
1653 }
1654 
1655 /// Matches an Or with LHS and RHS in either order.
1656 template <typename LHS, typename RHS>
1658  const RHS &R) {
1660 }
1661 
1662 /// Matches an Xor with LHS and RHS in either order.
1663 template <typename LHS, typename RHS>
1665  const RHS &R) {
1667 }
1668 
1669 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1670 template <typename ValTy>
1671 inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1672 m_Not(const ValTy &V) {
1673  return m_c_Xor(V, m_AllOnes());
1674 }
1675 
1676 /// Matches an SMin with LHS and RHS in either order.
1677 template <typename LHS, typename RHS>
1679 m_c_SMin(const LHS &L, const RHS &R) {
1681 }
1682 /// Matches an SMax with LHS and RHS in either order.
1683 template <typename LHS, typename RHS>
1685 m_c_SMax(const LHS &L, const RHS &R) {
1687 }
1688 /// Matches a UMin with LHS and RHS in either order.
1689 template <typename LHS, typename RHS>
1691 m_c_UMin(const LHS &L, const RHS &R) {
1693 }
1694 /// Matches a UMax with LHS and RHS in either order.
1695 template <typename LHS, typename RHS>
1697 m_c_UMax(const LHS &L, const RHS &R) {
1699 }
1700 
1701 /// Matches FAdd with LHS and RHS in either order.
1702 template <typename LHS, typename RHS>
1704 m_c_FAdd(const LHS &L, const RHS &R) {
1706 }
1707 
1708 /// Matches FMul with LHS and RHS in either order.
1709 template <typename LHS, typename RHS>
1711 m_c_FMul(const LHS &L, const RHS &R) {
1713 }
1714 
1715 } // end namespace PatternMatch
1716 } // end namespace llvm
1717 
1718 #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:680
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:864
bool isValue(const APFloat &C)
Definition: PatternMatch.h:420
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:627
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:751
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of nonnegative values.
Definition: PatternMatch.h:352
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:466
bool isZero() const
Definition: APFloat.h:1143
Match a specified integer value or vector of all elements of that.
Definition: PatternMatch.h:525
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:80
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:613
br_match(BasicBlock *&Succ)
InsertElementClass_match(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Definition: PatternMatch.h:998
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:607
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:485
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMax(const Opnd0 &Op0, const Opnd1 &Op1)
bool isValue(const APInt &C)
Definition: PatternMatch.h:393
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:656
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:668
match_zero m_Zero()
Match an arbitrary zero/null constant.
Definition: PatternMatch.h:145
Exact_match(const SubPattern_t &SP)
Definition: PatternMatch.h:896
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:632
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:275
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:397
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:911
unsigned less than
Definition: InstrTypes.h:910
bool isValue(const APInt &C)
Definition: PatternMatch.h:336
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:710
static bool match(ICmpInst::Predicate Pred)
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:891
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)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:619
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.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
Definition: PatternMatch.h:872
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
Definition: PatternMatch.h:503
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:949
Helper class for identifying signed min predicates.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:692
This class represents the LLVM &#39;select&#39; instruction.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:362
Exact_match< T > m_Exact(const T &SubPattern)
Definition: PatternMatch.h:905
bool isValue(const APFloat &C)
Definition: PatternMatch.h:411
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bool isValue(const APFloat &C)
Definition: PatternMatch.h:402
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:328
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:892
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:601
bind_ty< ConstantFP > m_ConstantFP(ConstantFP *&C)
Match a ConstantFP, capturing the value if we match.
Definition: PatternMatch.h:469
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Definition: Instruction.h:166
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:776
#define UINT64_MAX
Definition: DataTypes.h:83
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
Definition: PatternMatch.h:406
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:193
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
LoadClass_match(const Op_t &OpMatch)
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:424
IntrinsicID_match(Intrinsic::ID IntrID)
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
Definition: PatternMatch.h:975
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:373
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:792
ShuffleVectorClass_match< V1_t, V2_t, Mask_t > m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m)
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:357
#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:704
Helper class for identifying signed max predicates.
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:389
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:189
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:650
This helper class is used to match scalar and vector integer constants that satisfy a specified predi...
Definition: PatternMatch.h:218
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:800
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:674
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
Definition: PatternMatch.h:385
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:686
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 isOneValue() const
Determine if this is a value of 1.
Definition: APInt.h:404
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:318
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:481
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:698
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:885
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.
ShuffleVectorClass_match(const V1_t &v1, const V2_t &v2, const Mask_t &m)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
Definition: PatternMatch.h:879
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:556
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:75
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:885
Helper class for identifying unordered min predicates.
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:900
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:582
signed greater than
Definition: InstrTypes.h:912
LoadClass_match< OpTy > m_Load(const OpTy &Op)
Matches LoadInst.
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
neg_match< LHS > m_Neg(const LHS &L)
Match an integer negate.
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:889
ExtractElementClass_match(const Val_t &Val, const Idx_t &Idx)
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:340
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:420
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:369
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:186
Match a specified Value*.
Definition: PatternMatch.h:472
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:899
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:662
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:644
signed less than
Definition: InstrTypes.h:914
bool isOpType(unsigned Opcode)
Definition: PatternMatch.h:850
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.
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:638
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:743
signed less or equal
Definition: InstrTypes.h:915
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:433
bool isPowerOf2() const
Check if this APInt&#39;s value is a power of two greater than zero.
Definition: APInt.h:457
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:857
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:784
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:506
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:546
bool isPosZero() const
Definition: APFloat.h:1158
InsertElementClass_match< Val_t, Elt_t, Idx_t > m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
unsigned greater or equal
Definition: InstrTypes.h:909
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:920
#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:767
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
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:897
SelectClass_match< Cond, constantint_match< L >, constantint_match< R > > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.
Definition: PatternMatch.h:984
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:721
bool isOpType(unsigned Opcode)
Definition: PatternMatch.h:828
static bool match(FCmpInst::Predicate Pred)
apfloat_match(const APFloat *&R)
Definition: PatternMatch.h:171
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:314
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:898
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:759
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:364
static bool match(ICmpInst::Predicate Pred)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:814
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:908
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:542
ExtractElementClass_match< Val_t, Idx_t > m_ExtractElement(const Val_t &Val, const Idx_t &Idx)
bool isValue(const APInt &C)
Definition: PatternMatch.h:360
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:415
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
Definition: PatternMatch.h:963
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:890
bool isValue(const APFloat &C)
Definition: PatternMatch.h:429
This helper class is used to match scalar and vector constants that satisfy a specified predicate...
Definition: PatternMatch.h:249
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:458
#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;.
signed greater or equal
Definition: InstrTypes.h:913
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:943