LLVM 23.0.0git
PatternMatch.h
Go to the documentation of this file.
1//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides a simple and efficient mechanism for performing general
10// tree-based pattern matches on the LLVM IR. The power of these routines is
11// that it allows you to write concise patterns that are expressive and easy to
12// understand. The other major advantage of this is that it allows you to
13// trivially capture/bind elements in the pattern to variables. For example,
14// you can do something like this:
15//
16// Value *Exp = ...
17// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
18// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19// m_And(m_Value(Y), m_ConstantInt(C2))))) {
20// ... Pattern is matched and variables are bound ...
21// }
22//
23// This is primarily useful to things like the instruction combiner, but can
24// also be useful for static analysis tools or code generators.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_IR_PATTERNMATCH_H
29#define LLVM_IR_PATTERNMATCH_H
30
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/InstrTypes.h"
37#include "llvm/IR/Instruction.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Operator.h"
42#include "llvm/IR/Value.h"
44#include <cstdint>
45
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return P.match(V);
51}
52
53/// A match functor that can be used as a UnaryPredicate in functional
54/// algorithms like all_of.
55template <typename Val = const Value, typename Pattern>
56auto match_fn(const Pattern &P) {
58}
59
60template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
61 return P.match(Mask);
62}
63
64template <typename SubPattern_t> struct OneUse_match {
65 SubPattern_t SubPattern;
66
67 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
68
69 template <typename OpTy> bool match(OpTy *V) const {
70 return V->hasOneUse() && SubPattern.match(V);
71 }
72};
73
74template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
75 return SubPattern;
76}
77
78template <typename SubPattern_t> struct AllowReassoc_match {
79 SubPattern_t SubPattern;
80
81 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
82
83 template <typename OpTy> bool match(OpTy *V) const {
84 auto *I = dyn_cast<FPMathOperator>(V);
85 return I && I->hasAllowReassoc() && SubPattern.match(I);
86 }
87};
88
89template <typename T>
90inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
91 return SubPattern;
92}
93
94template <typename Class> struct class_match {
95 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
96};
97
98/// Match an arbitrary value and ignore it.
100
101/// Match an arbitrary unary operation and ignore it.
105
106/// Match an arbitrary binary operation and ignore it.
110
111/// Matches any compare instruction and ignore it.
113
115 static bool check(const Value *V) {
116 if (isa<UndefValue>(V))
117 return true;
118
119 const auto *CA = dyn_cast<ConstantAggregate>(V);
120 if (!CA)
121 return false;
122
125
126 // Either UndefValue, PoisonValue, or an aggregate that only contains
127 // these is accepted by matcher.
128 // CheckValue returns false if CA cannot satisfy this constraint.
129 auto CheckValue = [&](const ConstantAggregate *CA) {
130 for (const Value *Op : CA->operand_values()) {
131 if (isa<UndefValue>(Op))
132 continue;
133
134 const auto *CA = dyn_cast<ConstantAggregate>(Op);
135 if (!CA)
136 return false;
137 if (Seen.insert(CA).second)
138 Worklist.emplace_back(CA);
139 }
140
141 return true;
142 };
143
144 if (!CheckValue(CA))
145 return false;
146
147 while (!Worklist.empty()) {
148 if (!CheckValue(Worklist.pop_back_val()))
149 return false;
150 }
151 return true;
152 }
153 template <typename ITy> bool match(ITy *V) const { return check(V); }
154};
155
156/// Match an arbitrary undef constant. This matches poison as well.
157/// If this is an aggregate and contains a non-aggregate element that is
158/// neither undef nor poison, the aggregate is not matched.
159inline auto m_Undef() { return undef_match(); }
160
161/// Match an arbitrary UndefValue constant.
165
166/// Match an arbitrary poison constant.
170
171/// Match an arbitrary Constant and ignore it.
173
174/// Match an arbitrary ConstantInt and ignore it.
178
179/// Match an arbitrary ConstantFP and ignore it.
183
185 template <typename ITy> bool match(ITy *V) const {
186 auto *C = dyn_cast<Constant>(V);
187 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
188 }
189};
190
191/// Match a constant expression or a constant that contains a constant
192/// expression.
194
195template <typename SubPattern_t> struct Splat_match {
196 SubPattern_t SubPattern;
197 Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
198
199 template <typename OpTy> bool match(OpTy *V) const {
200 if (auto *C = dyn_cast<Constant>(V)) {
201 auto *Splat = C->getSplatValue();
202 return Splat ? SubPattern.match(Splat) : false;
203 }
204 // TODO: Extend to other cases (e.g. shufflevectors).
205 return false;
206 }
207};
208
209/// Match a constant splat. TODO: Extend this to non-constant splats.
210template <typename T>
211inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
212 return SubPattern;
213}
214
215/// Match an arbitrary basic block value and ignore it.
219
220/// Inverting matcher
221template <typename Ty> struct match_unless {
222 Ty M;
223
224 match_unless(const Ty &Matcher) : M(Matcher) {}
225
226 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
227};
228
229/// Match if the inner matcher does *NOT* match.
230template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
231 return match_unless<Ty>(M);
232}
233
234/// Matching combinators
235template <typename LTy, typename RTy> struct match_combine_or {
236 LTy L;
237 RTy R;
238
239 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
240
241 template <typename ITy> bool match(ITy *V) const {
242 if (L.match(V))
243 return true;
244 if (R.match(V))
245 return true;
246 return false;
247 }
248};
249
250template <typename LTy, typename RTy> struct match_combine_and {
251 LTy L;
252 RTy R;
253
254 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
255
256 template <typename ITy> bool match(ITy *V) const {
257 if (L.match(V))
258 if (R.match(V))
259 return true;
260 return false;
261 }
262};
263
264/// Combine two pattern matchers matching L || R
265template <typename LTy, typename RTy>
266inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
267 return match_combine_or<LTy, RTy>(L, R);
268}
269
270/// Combine two pattern matchers matching L && R
271template <typename LTy, typename RTy>
272inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
273 return match_combine_and<LTy, RTy>(L, R);
274}
275
276template <typename APTy> struct ap_match {
277 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
279 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
280
281 const APTy *&Res;
283
284 ap_match(const APTy *&Res, bool AllowPoison)
286
287 template <typename ITy> bool match(ITy *V) const {
288 if (auto *CI = dyn_cast<ConstantTy>(V)) {
289 Res = &CI->getValue();
290 return true;
291 }
292 if (V->getType()->isVectorTy())
293 if (const auto *C = dyn_cast<Constant>(V))
294 if (auto *CI =
295 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
296 Res = &CI->getValue();
297 return true;
298 }
299 return false;
300 }
301};
302
303/// Match a ConstantInt or splatted ConstantVector, binding the
304/// specified pointer to the contained APInt.
305inline ap_match<APInt> m_APInt(const APInt *&Res) {
306 // Forbid poison by default to maintain previous behavior.
307 return ap_match<APInt>(Res, /* AllowPoison */ false);
308}
309
310/// Match APInt while allowing poison in splat vector constants.
312 return ap_match<APInt>(Res, /* AllowPoison */ true);
313}
314
315/// Match APInt while forbidding poison in splat vector constants.
317 return ap_match<APInt>(Res, /* AllowPoison */ false);
318}
319
320/// Match a ConstantFP or splatted ConstantVector, binding the
321/// specified pointer to the contained APFloat.
323 // Forbid undefs by default to maintain previous behavior.
324 return ap_match<APFloat>(Res, /* AllowPoison */ false);
325}
326
327/// Match APFloat while allowing poison in splat vector constants.
329 return ap_match<APFloat>(Res, /* AllowPoison */ true);
330}
331
332/// Match APFloat while forbidding poison in splat vector constants.
334 return ap_match<APFloat>(Res, /* AllowPoison */ false);
335}
336
337template <int64_t Val> struct constantint_match {
338 template <typename ITy> bool match(ITy *V) const {
339 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
340 const APInt &CIV = CI->getValue();
341 if (Val >= 0)
342 return CIV == static_cast<uint64_t>(Val);
343 // If Val is negative, and CI is shorter than it, truncate to the right
344 // number of bits. If it is larger, then we have to sign extend. Just
345 // compare their negated values.
346 return -CIV == -Val;
347 }
348 return false;
349 }
350};
351
352/// Match a ConstantInt with a specific value.
353template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
354 return constantint_match<Val>();
355}
356
357/// This helper class is used to match constant scalars, vector splats,
358/// and fixed width vectors that satisfy a specified predicate.
359/// For fixed width vector constants, poison elements are ignored if AllowPoison
360/// is true.
361template <typename Predicate, typename ConstantVal, bool AllowPoison>
362struct cstval_pred_ty : public Predicate {
363 const Constant **Res = nullptr;
364 template <typename ITy> bool match_impl(ITy *V) const {
365 if (const auto *CV = dyn_cast<ConstantVal>(V))
366 return this->isValue(CV->getValue());
367 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
368 if (const auto *C = dyn_cast<Constant>(V)) {
369 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
370 return this->isValue(CV->getValue());
371
372 // Number of elements of a scalable vector unknown at compile time
373 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
374 if (!FVTy)
375 return false;
376
377 // Non-splat vector constant: check each element for a match.
378 unsigned NumElts = FVTy->getNumElements();
379 assert(NumElts != 0 && "Constant vector with no elements?");
380 bool HasNonPoisonElements = false;
381 for (unsigned i = 0; i != NumElts; ++i) {
382 Constant *Elt = C->getAggregateElement(i);
383 if (!Elt)
384 return false;
385 if (AllowPoison && isa<PoisonValue>(Elt))
386 continue;
387 auto *CV = dyn_cast<ConstantVal>(Elt);
388 if (!CV || !this->isValue(CV->getValue()))
389 return false;
390 HasNonPoisonElements = true;
391 }
392 return HasNonPoisonElements;
393 }
394 }
395 return false;
396 }
397
398 template <typename ITy> bool match(ITy *V) const {
399 if (this->match_impl(V)) {
400 if (Res)
401 *Res = cast<Constant>(V);
402 return true;
403 }
404 return false;
405 }
406};
407
408/// specialization of cstval_pred_ty for ConstantInt
409template <typename Predicate, bool AllowPoison = true>
411
412/// specialization of cstval_pred_ty for ConstantFP
413template <typename Predicate>
415 /*AllowPoison=*/true>;
416
417/// This helper class is used to match scalar and vector constants that
418/// satisfy a specified predicate, and bind them to an APInt.
419template <typename Predicate> struct api_pred_ty : public Predicate {
420 const APInt *&Res;
421
422 api_pred_ty(const APInt *&R) : Res(R) {}
423
424 template <typename ITy> bool match(ITy *V) const {
425 if (const auto *CI = dyn_cast<ConstantInt>(V))
426 if (this->isValue(CI->getValue())) {
427 Res = &CI->getValue();
428 return true;
429 }
430 if (V->getType()->isVectorTy())
431 if (const auto *C = dyn_cast<Constant>(V))
432 if (auto *CI = dyn_cast_or_null<ConstantInt>(
433 C->getSplatValue(/*AllowPoison=*/true)))
434 if (this->isValue(CI->getValue())) {
435 Res = &CI->getValue();
436 return true;
437 }
438
439 return false;
440 }
441};
442
443/// This helper class is used to match scalar and vector constants that
444/// satisfy a specified predicate, and bind them to an APFloat.
445/// Poison is allowed in splat vector constants.
446template <typename Predicate> struct apf_pred_ty : public Predicate {
447 const APFloat *&Res;
448
449 apf_pred_ty(const APFloat *&R) : Res(R) {}
450
451 template <typename ITy> bool match(ITy *V) const {
452 if (const auto *CI = dyn_cast<ConstantFP>(V))
453 if (this->isValue(CI->getValue())) {
454 Res = &CI->getValue();
455 return true;
456 }
457 if (V->getType()->isVectorTy())
458 if (const auto *C = dyn_cast<Constant>(V))
459 if (auto *CI = dyn_cast_or_null<ConstantFP>(
460 C->getSplatValue(/* AllowPoison */ true)))
461 if (this->isValue(CI->getValue())) {
462 Res = &CI->getValue();
463 return true;
464 }
465
466 return false;
467 }
468};
469
470///////////////////////////////////////////////////////////////////////////////
471//
472// Encapsulate constant value queries for use in templated predicate matchers.
473// This allows checking if constants match using compound predicates and works
474// with vector constants, possibly with relaxed constraints. For example, ignore
475// undef values.
476//
477///////////////////////////////////////////////////////////////////////////////
478
479template <typename APTy> struct custom_checkfn {
480 function_ref<bool(const APTy &)> CheckFn;
481 bool isValue(const APTy &C) const { return CheckFn(C); }
482};
483
484/// Match an integer or vector where CheckFn(ele) for each element is true.
485/// For vectors, poison elements are assumed to match.
487m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
488 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
489}
490
492m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
493 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
494}
495
496/// Match a float or vector where CheckFn(ele) for each element is true.
497/// For vectors, poison elements are assumed to match.
499m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
500 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
501}
502
504m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
505 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
506}
507
509 bool isValue(const APInt &C) const { return true; }
510};
511/// Match an integer or vector with any integral constant.
512/// For vectors, this includes constants with undefined elements.
516
518 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
519};
520
524
526 bool isValue(const APInt &C) const { return C.isAllOnes(); }
527};
528/// Match an integer or vector with all bits set.
529/// For vectors, this includes constants with undefined elements.
533
537
539 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
540};
541/// Match an integer or vector with values having all bits except for the high
542/// bit set (0x7f...).
543/// For vectors, this includes constants with undefined elements.
548 return V;
549}
550
552 bool isValue(const APInt &C) const { return C.isNegative(); }
553};
554/// Match an integer or vector of negative values.
555/// For vectors, this includes constants with undefined elements.
559inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
560
562 bool isValue(const APInt &C) const { return C.isNonNegative(); }
563};
564/// Match an integer or vector of non-negative values.
565/// For vectors, this includes constants with undefined elements.
569inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
570
572 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
573};
574/// Match an integer or vector of strictly positive values.
575/// For vectors, this includes constants with undefined elements.
580 return V;
581}
582
584 bool isValue(const APInt &C) const { return C.isNonPositive(); }
585};
586/// Match an integer or vector of non-positive values.
587/// For vectors, this includes constants with undefined elements.
591inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
592
593struct is_one {
594 bool isValue(const APInt &C) const { return C.isOne(); }
595};
596/// Match an integer 1 or a vector with all elements equal to 1.
597/// For vectors, this includes constants with undefined elements.
599
601 bool isValue(const APInt &C) const { return C.isZero(); }
602};
603/// Match an integer 0 or a vector with all elements equal to 0.
604/// For vectors, this includes constants with undefined elements.
608
609struct is_zero {
610 template <typename ITy> bool match(ITy *V) const {
611 auto *C = dyn_cast<Constant>(V);
612 // FIXME: this should be able to do something for scalable vectors
613 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
614 }
615};
616/// Match any null constant or a vector with all elements equal to 0.
617/// For vectors, this includes constants with undefined elements.
618inline is_zero m_Zero() { return is_zero(); }
619
620struct is_power2 {
621 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
622};
623/// Match an integer or vector power-of-2.
624/// For vectors, this includes constants with undefined elements.
626inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
627
629 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
630};
631/// Match a integer or vector negated power-of-2.
632/// For vectors, this includes constants with undefined elements.
637 return V;
638}
639
641 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
642};
643/// Match a integer or vector negated power-of-2.
644/// For vectors, this includes constants with undefined elements.
650 return V;
651}
652
654 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
655};
656/// Match an integer or vector of 0 or power-of-2 values.
657/// For vectors, this includes constants with undefined elements.
662 return V;
663}
664
666 bool isValue(const APInt &C) const { return C.isSignMask(); }
667};
668/// Match an integer or vector with only the sign bit(s) set.
669/// For vectors, this includes constants with undefined elements.
673
675 bool isValue(const APInt &C) const { return C.isMask(); }
676};
677/// Match an integer or vector with only the low bit(s) set.
678/// For vectors, this includes constants with undefined elements.
682inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
683
685 bool isValue(const APInt &C) const { return !C || C.isMask(); }
686};
687/// Match an integer or vector with only the low bit(s) set.
688/// For vectors, this includes constants with undefined elements.
693 return V;
694}
695
698 const APInt *Thr;
699 bool isValue(const APInt &C) const {
700 return ICmpInst::compare(C, *Thr, Pred);
701 }
702};
703/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
704/// to Threshold. For vectors, this includes constants with undefined elements.
708 P.Pred = Predicate;
709 P.Thr = &Threshold;
710 return P;
711}
712
713struct is_nan {
714 bool isValue(const APFloat &C) const { return C.isNaN(); }
715};
716/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
717/// For vectors, this includes constants with undefined elements.
719
720struct is_nonnan {
721 bool isValue(const APFloat &C) const { return !C.isNaN(); }
722};
723/// Match a non-NaN FP constant.
724/// For vectors, this includes constants with undefined elements.
728
729struct is_inf {
730 bool isValue(const APFloat &C) const { return C.isInfinity(); }
731};
732/// Match a positive or negative infinity FP constant.
733/// For vectors, this includes constants with undefined elements.
735
736struct is_noninf {
737 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
738};
739/// Match a non-infinity FP constant, i.e. finite or NaN.
740/// For vectors, this includes constants with undefined elements.
744
745struct is_finite {
746 bool isValue(const APFloat &C) const { return C.isFinite(); }
747};
748/// Match a finite FP constant, i.e. not infinity or NaN.
749/// For vectors, this includes constants with undefined elements.
753inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
754
756 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
757};
758/// Match a finite non-zero FP constant.
759/// For vectors, this includes constants with undefined elements.
764 return V;
765}
766
768 bool isValue(const APFloat &C) const { return C.isZero(); }
769};
770/// Match a floating-point negative zero or positive zero.
771/// For vectors, this includes constants with undefined elements.
775
777 bool isValue(const APFloat &C) const { return C.isPosZero(); }
778};
779/// Match a floating-point positive zero.
780/// For vectors, this includes constants with undefined elements.
784
786 bool isValue(const APFloat &C) const { return C.isNegZero(); }
787};
788/// Match a floating-point negative zero.
789/// For vectors, this includes constants with undefined elements.
793
795 bool isValue(const APFloat &C) const { return C.isNonZero(); }
796};
797/// Match a floating-point non-zero.
798/// For vectors, this includes constants with undefined elements.
802
804 bool isValue(const APFloat &C) const {
805 return !C.isDenormal() && C.isNonZero();
806 }
807};
808
809/// Match a floating-point non-zero that is not a denormal.
810/// For vectors, this includes constants with undefined elements.
814
815///////////////////////////////////////////////////////////////////////////////
816
817template <typename Class> struct bind_ty {
818 Class *&VR;
819
820 bind_ty(Class *&V) : VR(V) {}
821
822 template <typename ITy> bool match(ITy *V) const {
823 if (auto *CV = dyn_cast<Class>(V)) {
824 VR = CV;
825 return true;
826 }
827 return false;
828 }
829};
830
831/// Check whether the value has the given Class and matches the nested
832/// pattern. Capture it into the provided variable if successful.
833template <typename Class, typename MatchTy> struct bind_and_match_ty {
834 Class *&VR;
835 MatchTy Match;
836
837 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
838
839 template <typename ITy> bool match(ITy *V) const {
840 auto *CV = dyn_cast<Class>(V);
841 if (CV && Match.match(V)) {
842 VR = CV;
843 return true;
844 }
845 return false;
846 }
847};
848
849/// Match a value, capturing it if we match.
850inline bind_ty<Value> m_Value(Value *&V) { return V; }
851inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
852
853/// Match against the nested pattern, and capture the value if we match.
854template <typename MatchTy>
856 const MatchTy &Match) {
857 return {V, Match};
858}
859
860/// Match against the nested pattern, and capture the value if we match.
861template <typename MatchTy>
863 const MatchTy &Match) {
864 return {V, Match};
865}
866
867/// Match an instruction, capturing it if we match.
870 return I;
871}
872
873/// Match against the nested pattern, and capture the instruction if we match.
874template <typename MatchTy>
876m_Instruction(Instruction *&I, const MatchTy &Match) {
877 return {I, Match};
878}
879template <typename MatchTy>
881m_Instruction(const Instruction *&I, const MatchTy &Match) {
882 return {I, Match};
883}
884
885/// Match a unary operator, capturing it if we match.
888 return I;
889}
890/// Match a binary operator, capturing it if we match.
893 return I;
894}
895/// Match a with overflow intrinsic, capturing it if we match.
901 return I;
902}
903
904/// Match an UndefValue, capturing the value if we match.
906
907/// Match a Constant, capturing the value if we match.
909
910/// Match a ConstantInt, capturing the value if we match.
912
913/// Match a ConstantFP, capturing the value if we match.
915
916/// Match a ConstantExpr, capturing the value if we match.
918
919/// Match a basic block value, capturing it if we match.
922 return V;
923}
924
925// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
926// and use m_Unless(m_ConstantExpr).
928 template <typename ITy> static bool isImmConstant(ITy *V) {
929 if (auto *CV = dyn_cast<Constant>(V)) {
930 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
931 return true;
932
933 if (CV->getType()->isVectorTy()) {
934 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
935 if (!isa<ConstantExpr>(Splat) &&
936 !Splat->containsConstantExpression()) {
937 return true;
938 }
939 }
940 }
941 }
942 return false;
943 }
944};
945
947 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
948};
949
950/// Match an arbitrary immediate Constant and ignore it.
952
955
957
958 template <typename ITy> bool match(ITy *V) const {
959 if (isImmConstant(V)) {
960 VR = cast<Constant>(V);
961 return true;
962 }
963 return false;
964 }
965};
966
967/// Match an immediate Constant, capturing the value if we match.
971
972/// Match a specified Value*.
974 const Value *Val;
975
976 specificval_ty(const Value *V) : Val(V) {}
977
978 template <typename ITy> bool match(ITy *V) const { return V == Val; }
979};
980
981/// Match if we have a specific specified value.
982inline specificval_ty m_Specific(const Value *V) { return V; }
983
984/// Stores a reference to the Value *, not the Value * itself,
985/// thus can be used in commutative matchers.
986template <typename Class> struct deferredval_ty {
987 Class *const &Val;
988
989 deferredval_ty(Class *const &V) : Val(V) {}
990
991 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
992};
993
994/// Like m_Specific(), but works if the specific value to match is determined
995/// as part of the same match() expression. For example:
996/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
997/// bind X before the pattern match starts.
998/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
999/// whichever value m_Value(X) populated.
1000inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
1002 return V;
1003}
1004
1005/// Match a specified floating point value or vector of all elements of
1006/// that value.
1008 double Val;
1009
1010 specific_fpval(double V) : Val(V) {}
1011
1012 template <typename ITy> bool match(ITy *V) const {
1013 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1014 return CFP->isExactlyValue(Val);
1015 if (V->getType()->isVectorTy())
1016 if (const auto *C = dyn_cast<Constant>(V))
1017 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1018 return CFP->isExactlyValue(Val);
1019 return false;
1020 }
1021};
1022
1023/// Match a specific floating point value or vector with all elements
1024/// equal to the value.
1025inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1026
1027/// Match a float 1.0 or vector with all elements equal to 1.0.
1028inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1029
1032
1034
1035 template <typename ITy> bool match(ITy *V) const {
1036 const APInt *ConstInt;
1037 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1038 return false;
1039 if (ConstInt->getActiveBits() > 64)
1040 return false;
1041 VR = ConstInt->getZExtValue();
1042 return true;
1043 }
1044};
1045
1046/// Match a specified integer value or vector of all elements of that
1047/// value.
1048template <bool AllowPoison> struct specific_intval {
1049 const APInt &Val;
1050
1051 specific_intval(const APInt &V) : Val(V) {}
1052
1053 template <typename ITy> bool match(ITy *V) const {
1054 const auto *CI = dyn_cast<ConstantInt>(V);
1055 if (!CI && V->getType()->isVectorTy())
1056 if (const auto *C = dyn_cast<Constant>(V))
1057 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1058
1059 return CI && APInt::isSameValue(CI->getValue(), Val);
1060 }
1061};
1062
1063template <bool AllowPoison> struct specific_intval64 {
1065
1067
1068 template <typename ITy> bool match(ITy *V) const {
1069 const auto *CI = dyn_cast<ConstantInt>(V);
1070 if (!CI && V->getType()->isVectorTy())
1071 if (const auto *C = dyn_cast<Constant>(V))
1072 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1073
1074 return CI && CI->getValue() == Val;
1075 }
1076};
1077
1078/// Match a specific integer value or vector with all elements equal to
1079/// the value.
1081 return specific_intval<false>(V);
1082}
1083
1087
1091
1095
1096/// Match a ConstantInt and bind to its value. This does not match
1097/// ConstantInts wider than 64-bits.
1099
1100/// Match a specified basic block value.
1103
1105
1106 template <typename ITy> bool match(ITy *V) const {
1107 const auto *BB = dyn_cast<BasicBlock>(V);
1108 return BB && BB == Val;
1109 }
1110};
1111
1112/// Match a specific basic block value.
1114 return specific_bbval(BB);
1115}
1116
1117/// A commutative-friendly version of m_Specific().
1119 return BB;
1120}
1122m_Deferred(const BasicBlock *const &BB) {
1123 return BB;
1124}
1125
1126//===----------------------------------------------------------------------===//
1127// Matcher for any binary operator.
1128//
1129template <typename LHS_t, typename RHS_t, bool Commutable = false>
1133
1134 // The evaluation order is always stable, regardless of Commutability.
1135 // The LHS is always matched first.
1136 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1137
1138 template <typename OpTy> bool match(OpTy *V) const {
1139 if (auto *I = dyn_cast<BinaryOperator>(V))
1140 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1141 (Commutable && L.match(I->getOperand(1)) &&
1142 R.match(I->getOperand(0)));
1143 return false;
1144 }
1145};
1146
1147template <typename LHS, typename RHS>
1148inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1149 return AnyBinaryOp_match<LHS, RHS>(L, R);
1150}
1151
1152//===----------------------------------------------------------------------===//
1153// Matcher for any unary operator.
1154// TODO fuse unary, binary matcher into n-ary matcher
1155//
1156template <typename OP_t> struct AnyUnaryOp_match {
1157 OP_t X;
1158
1159 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1160
1161 template <typename OpTy> bool match(OpTy *V) const {
1162 if (auto *I = dyn_cast<UnaryOperator>(V))
1163 return X.match(I->getOperand(0));
1164 return false;
1165 }
1166};
1167
1168template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1169 return AnyUnaryOp_match<OP_t>(X);
1170}
1171
1172//===----------------------------------------------------------------------===//
1173// Matchers for specific binary operators.
1174//
1175
1176template <typename LHS_t, typename RHS_t, unsigned Opcode,
1177 bool Commutable = false>
1181
1182 // The evaluation order is always stable, regardless of Commutability.
1183 // The LHS is always matched first.
1184 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1185
1186 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1187 if (V->getValueID() == Value::InstructionVal + Opc) {
1188 auto *I = cast<BinaryOperator>(V);
1189 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1190 (Commutable && L.match(I->getOperand(1)) &&
1191 R.match(I->getOperand(0)));
1192 }
1193 return false;
1194 }
1195
1196 template <typename OpTy> bool match(OpTy *V) const {
1197 return match(Opcode, V);
1198 }
1199};
1200
1201template <typename LHS, typename RHS>
1206
1207template <typename LHS, typename RHS>
1212
1213template <typename LHS, typename RHS>
1218
1219template <typename LHS, typename RHS>
1224
1225template <typename Op_t> struct FNeg_match {
1226 Op_t X;
1227
1228 FNeg_match(const Op_t &Op) : X(Op) {}
1229 template <typename OpTy> bool match(OpTy *V) const {
1230 auto *FPMO = dyn_cast<FPMathOperator>(V);
1231 if (!FPMO)
1232 return false;
1233
1234 if (FPMO->getOpcode() == Instruction::FNeg)
1235 return X.match(FPMO->getOperand(0));
1236
1237 if (FPMO->getOpcode() == Instruction::FSub) {
1238 if (FPMO->hasNoSignedZeros()) {
1239 // With 'nsz', any zero goes.
1240 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1241 return false;
1242 } else {
1243 // Without 'nsz', we need fsub -0.0, X exactly.
1244 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1245 return false;
1246 }
1247
1248 return X.match(FPMO->getOperand(1));
1249 }
1250
1251 return false;
1252 }
1253};
1254
1255/// Match 'fneg X' as 'fsub -0.0, X'.
1256template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1257 return FNeg_match<OpTy>(X);
1258}
1259
1260/// Match 'fneg X' as 'fsub +-0.0, X'.
1261template <typename RHS>
1262inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1263m_FNegNSZ(const RHS &X) {
1264 return m_FSub(m_AnyZeroFP(), X);
1265}
1266
1267template <typename LHS, typename RHS>
1272
1273template <typename LHS, typename RHS>
1278
1279template <typename LHS, typename RHS>
1284
1285template <typename LHS, typename RHS>
1290
1291template <typename LHS, typename RHS>
1296
1297template <typename LHS, typename RHS>
1302
1303template <typename LHS, typename RHS>
1308
1309template <typename LHS, typename RHS>
1314
1315template <typename LHS, typename RHS>
1320
1321template <typename LHS, typename RHS>
1326
1327template <typename LHS, typename RHS>
1332
1333template <typename LHS, typename RHS>
1338
1339template <typename LHS, typename RHS>
1344
1345template <typename LHS, typename RHS>
1350
1351template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1354
1356
1357 template <typename OpTy> bool match(OpTy *V) const {
1358 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1359 if (Op->getOpcode() == Opcode)
1360 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1361 L.match(Op->getOperand(0));
1362 }
1363 // Interpreted as shiftop V, 0
1364 R = 0;
1365 return L.match(V);
1366 }
1367};
1368
1369/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1370template <typename LHS>
1375
1376/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1377template <typename LHS>
1382
1383/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1384template <typename LHS>
1389
1390template <typename LHS_t, typename RHS_t, unsigned Opcode,
1391 unsigned WrapFlags = 0, bool Commutable = false>
1395
1397 : L(LHS), R(RHS) {}
1398
1399 template <typename OpTy> bool match(OpTy *V) const {
1400 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1401 if (Op->getOpcode() != Opcode)
1402 return false;
1404 !Op->hasNoUnsignedWrap())
1405 return false;
1406 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1407 !Op->hasNoSignedWrap())
1408 return false;
1409 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1410 (Commutable && L.match(Op->getOperand(1)) &&
1411 R.match(Op->getOperand(0)));
1412 }
1413 return false;
1414 }
1415};
1416
1417template <typename LHS, typename RHS>
1418inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1420m_NSWAdd(const LHS &L, const RHS &R) {
1421 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1423 R);
1424}
1425template <typename LHS, typename RHS>
1426inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1428m_c_NSWAdd(const LHS &L, const RHS &R) {
1429 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1431 true>(L, R);
1432}
1433template <typename LHS, typename RHS>
1434inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1436m_NSWSub(const LHS &L, const RHS &R) {
1437 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1439 R);
1440}
1441template <typename LHS, typename RHS>
1442inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1444m_NSWMul(const LHS &L, const RHS &R) {
1445 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1447 R);
1448}
1449template <typename LHS, typename RHS>
1450inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1452m_NSWShl(const LHS &L, const RHS &R) {
1453 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1455 R);
1456}
1457
1458template <typename LHS, typename RHS>
1459inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1461m_NUWAdd(const LHS &L, const RHS &R) {
1462 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1464 L, R);
1465}
1466
1467template <typename LHS, typename RHS>
1469 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1470m_c_NUWAdd(const LHS &L, const RHS &R) {
1471 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1473 true>(L, R);
1474}
1475
1476template <typename LHS, typename RHS>
1477inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1479m_NUWSub(const LHS &L, const RHS &R) {
1480 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1482 L, R);
1483}
1484template <typename LHS, typename RHS>
1485inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1487m_NUWMul(const LHS &L, const RHS &R) {
1488 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1490 L, R);
1491}
1492template <typename LHS, typename RHS>
1493inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1495m_NUWShl(const LHS &L, const RHS &R) {
1496 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1498 L, R);
1499}
1500
1501template <typename LHS_t, typename RHS_t, bool Commutable = false>
1503 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1504 unsigned Opcode;
1505
1507 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1508
1509 template <typename OpTy> bool match(OpTy *V) const {
1511 }
1512};
1513
1514/// Matches a specific opcode.
1515template <typename LHS, typename RHS>
1516inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1517 const RHS &R) {
1518 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1519}
1520
1521template <typename LHS, typename RHS, bool Commutable = false>
1525
1526 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1527
1528 template <typename OpTy> bool match(OpTy *V) const {
1529 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1530 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1531 if (!PDI->isDisjoint())
1532 return false;
1533 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1534 (Commutable && L.match(PDI->getOperand(1)) &&
1535 R.match(PDI->getOperand(0)));
1536 }
1537 return false;
1538 }
1539};
1540
1541template <typename LHS, typename RHS>
1543 return DisjointOr_match<LHS, RHS>(L, R);
1544}
1545
1546template <typename LHS, typename RHS>
1548 const RHS &R) {
1550}
1551
1552/// Match either "add" or "or disjoint".
1553template <typename LHS, typename RHS>
1556m_AddLike(const LHS &L, const RHS &R) {
1557 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1558}
1559
1560/// Match either "add nsw" or "or disjoint"
1561template <typename LHS, typename RHS>
1562inline match_combine_or<
1563 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1566m_NSWAddLike(const LHS &L, const RHS &R) {
1567 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1568}
1569
1570/// Match either "add nuw" or "or disjoint"
1571template <typename LHS, typename RHS>
1572inline match_combine_or<
1573 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1576m_NUWAddLike(const LHS &L, const RHS &R) {
1577 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1578}
1579
1580template <typename LHS, typename RHS>
1584
1585 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1586
1587 template <typename OpTy> bool match(OpTy *V) const {
1588 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1589 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1590 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1591 ; // Pass
1592 else if (Op->getOpcode() != Instruction::Xor)
1593 return false;
1594 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1595 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1596 }
1597 return false;
1598 }
1599};
1600
1601/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1602/// Only commutative matcher as the `sub` will need to swap the L and R.
1603template <typename LHS, typename RHS>
1604inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1605 return XorLike_match<LHS, RHS>(L, R);
1606}
1607
1608//===----------------------------------------------------------------------===//
1609// Class that matches a group of binary opcodes.
1610//
1611template <typename LHS_t, typename RHS_t, typename Predicate,
1612 bool Commutable = false>
1613struct BinOpPred_match : Predicate {
1616
1617 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1618
1619 template <typename OpTy> bool match(OpTy *V) const {
1620 if (auto *I = dyn_cast<Instruction>(V))
1621 return this->isOpType(I->getOpcode()) &&
1622 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1623 (Commutable && L.match(I->getOperand(1)) &&
1624 R.match(I->getOperand(0))));
1625 return false;
1626 }
1627};
1628
1630 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1631};
1632
1634 bool isOpType(unsigned Opcode) const {
1635 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1636 }
1637};
1638
1640 bool isOpType(unsigned Opcode) const {
1641 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1642 }
1643};
1644
1646 bool isOpType(unsigned Opcode) const {
1647 return Instruction::isBitwiseLogicOp(Opcode);
1648 }
1649};
1650
1652 bool isOpType(unsigned Opcode) const {
1653 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1654 }
1655};
1656
1658 bool isOpType(unsigned Opcode) const {
1659 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1660 }
1661};
1662
1663/// Matches shift operations.
1664template <typename LHS, typename RHS>
1666 const RHS &R) {
1668}
1669
1670/// Matches logical shift operations.
1671template <typename LHS, typename RHS>
1676
1677/// Matches logical shift operations.
1678template <typename LHS, typename RHS>
1680m_LogicalShift(const LHS &L, const RHS &R) {
1682}
1683
1684/// Matches bitwise logic operations.
1685template <typename LHS, typename RHS>
1687m_BitwiseLogic(const LHS &L, const RHS &R) {
1689}
1690
1691/// Matches bitwise logic operations in either order.
1692template <typename LHS, typename RHS>
1697
1698/// Matches integer division operations.
1699template <typename LHS, typename RHS>
1701 const RHS &R) {
1703}
1704
1705/// Matches integer remainder operations.
1706template <typename LHS, typename RHS>
1708 const RHS &R) {
1710}
1711
1712//===----------------------------------------------------------------------===//
1713// Class that matches exact binary ops.
1714//
1715template <typename SubPattern_t> struct Exact_match {
1716 SubPattern_t SubPattern;
1717
1718 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1719
1720 template <typename OpTy> bool match(OpTy *V) const {
1721 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1722 return PEO->isExact() && SubPattern.match(V);
1723 return false;
1724 }
1725};
1726
1727template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1728 return SubPattern;
1729}
1730
1731//===----------------------------------------------------------------------===//
1732// Matchers for CmpInst classes
1733//
1734
1735template <typename LHS_t, typename RHS_t, typename Class,
1736 bool Commutable = false>
1741
1742 // The evaluation order is always stable, regardless of Commutability.
1743 // The LHS is always matched first.
1745 : Predicate(&Pred), L(LHS), R(RHS) {}
1747 : Predicate(nullptr), L(LHS), R(RHS) {}
1748
1749 template <typename OpTy> bool match(OpTy *V) const {
1750 if (auto *I = dyn_cast<Class>(V)) {
1751 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1752 if (Predicate)
1754 return true;
1755 }
1756 if (Commutable && L.match(I->getOperand(1)) &&
1757 R.match(I->getOperand(0))) {
1758 if (Predicate)
1760 return true;
1761 }
1762 }
1763 return false;
1764 }
1765};
1766
1767template <typename LHS, typename RHS>
1769 const RHS &R) {
1770 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1771}
1772
1773template <typename LHS, typename RHS>
1775 const LHS &L, const RHS &R) {
1776 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1777}
1778
1779template <typename LHS, typename RHS>
1781 const LHS &L, const RHS &R) {
1782 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1783}
1784
1785template <typename LHS, typename RHS>
1788}
1789
1790template <typename LHS, typename RHS>
1793}
1794
1795template <typename LHS, typename RHS>
1798}
1799
1800// Same as CmpClass, but instead of saving Pred as out output variable, match a
1801// specific input pred for equality.
1802template <typename LHS_t, typename RHS_t, typename Class,
1803 bool Commutable = false>
1808
1810 : Predicate(Pred), L(LHS), R(RHS) {}
1811
1812 template <typename OpTy> bool match(OpTy *V) const {
1813 if (auto *I = dyn_cast<Class>(V)) {
1815 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1816 return true;
1817 if constexpr (Commutable) {
1820 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1821 return true;
1822 }
1823 }
1824
1825 return false;
1826 }
1827};
1828
1829template <typename LHS, typename RHS>
1831m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1832 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1833}
1834
1835template <typename LHS, typename RHS>
1837m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1838 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1839}
1840
1841template <typename LHS, typename RHS>
1843m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1845}
1846
1847template <typename LHS, typename RHS>
1849m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1850 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1851}
1852
1853//===----------------------------------------------------------------------===//
1854// Matchers for instructions with a given opcode and number of operands.
1855//
1856
1857/// Matches instructions with Opcode and three operands.
1858template <typename T0, unsigned Opcode> struct OneOps_match {
1860
1861 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1862
1863 template <typename OpTy> bool match(OpTy *V) const {
1864 if (V->getValueID() == Value::InstructionVal + Opcode) {
1865 auto *I = cast<Instruction>(V);
1866 return Op1.match(I->getOperand(0));
1867 }
1868 return false;
1869 }
1870};
1871
1872/// Matches instructions with Opcode and three operands.
1873template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1876
1877 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1878
1879 template <typename OpTy> bool match(OpTy *V) const {
1880 if (V->getValueID() == Value::InstructionVal + Opcode) {
1881 auto *I = cast<Instruction>(V);
1882 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1883 }
1884 return false;
1885 }
1886};
1887
1888/// Matches instructions with Opcode and three operands.
1889template <typename T0, typename T1, typename T2, unsigned Opcode,
1890 bool CommutableOp2Op3 = false>
1895
1896 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1897 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1898
1899 template <typename OpTy> bool match(OpTy *V) const {
1900 if (V->getValueID() == Value::InstructionVal + Opcode) {
1901 auto *I = cast<Instruction>(V);
1902 if (!Op1.match(I->getOperand(0)))
1903 return false;
1904 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1905 return true;
1906 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1907 Op3.match(I->getOperand(1));
1908 }
1909 return false;
1910 }
1911};
1912
1913/// Matches instructions with Opcode and any number of operands
1914template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1915 std::tuple<OperandTypes...> Operands;
1916
1917 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1918
1919 // Operand matching works by recursively calling match_operands, matching the
1920 // operands left to right. The first version is called for each operand but
1921 // the last, for which the second version is called. The second version of
1922 // match_operands is also used to match each individual operand.
1923 template <int Idx, int Last>
1924 std::enable_if_t<Idx != Last, bool>
1928
1929 template <int Idx, int Last>
1930 std::enable_if_t<Idx == Last, bool>
1932 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1933 }
1934
1935 template <typename OpTy> bool match(OpTy *V) const {
1936 if (V->getValueID() == Value::InstructionVal + Opcode) {
1937 auto *I = cast<Instruction>(V);
1938 return I->getNumOperands() == sizeof...(OperandTypes) &&
1939 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1940 }
1941 return false;
1942 }
1943};
1944
1945/// Matches SelectInst.
1946template <typename Cond, typename LHS, typename RHS>
1948m_Select(const Cond &C, const LHS &L, const RHS &R) {
1950}
1951
1952/// This matches a select of two constants, e.g.:
1953/// m_SelectCst<-1, 0>(m_Value(V))
1954template <int64_t L, int64_t R, typename Cond>
1956 Instruction::Select>
1959}
1960
1961/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1962template <typename LHS, typename RHS>
1963inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1964m_c_Select(const LHS &L, const RHS &R) {
1965 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1966 true>(m_Value(), L, R);
1967}
1968
1969/// Matches FreezeInst.
1970template <typename OpTy>
1974
1975/// Matches InsertElementInst.
1976template <typename Val_t, typename Elt_t, typename Idx_t>
1978m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1980 Val, Elt, Idx);
1981}
1982
1983/// Matches ExtractElementInst.
1984template <typename Val_t, typename Idx_t>
1986m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1988}
1989
1990/// Matches shuffle.
1991template <typename T0, typename T1, typename T2> struct Shuffle_match {
1995
1996 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1997 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1998
1999 template <typename OpTy> bool match(OpTy *V) const {
2000 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
2001 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
2002 Mask.match(I->getShuffleMask());
2003 }
2004 return false;
2005 }
2006};
2007
2008struct m_Mask {
2011 bool match(ArrayRef<int> Mask) const {
2012 MaskRef = Mask;
2013 return true;
2014 }
2015};
2016
2018 bool match(ArrayRef<int> Mask) const {
2019 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2020 }
2021};
2022
2026 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2027};
2028
2032 bool match(ArrayRef<int> Mask) const {
2033 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2034 if (First == Mask.end())
2035 return false;
2036 SplatIndex = *First;
2037 return all_of(Mask,
2038 [First](int Elem) { return Elem == *First || Elem == -1; });
2039 }
2040};
2041
2042template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2043 PointerOpTy PointerOp;
2044 OffsetOpTy OffsetOp;
2045
2046 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2048
2049 template <typename OpTy> bool match(OpTy *V) const {
2050 auto *GEP = dyn_cast<GEPOperator>(V);
2051 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2052 PointerOp.match(GEP->getPointerOperand()) &&
2053 OffsetOp.match(GEP->idx_begin()->get());
2054 }
2055};
2056
2057/// Matches ShuffleVectorInst independently of mask value.
2058template <typename V1_t, typename V2_t>
2060m_Shuffle(const V1_t &v1, const V2_t &v2) {
2062}
2063
2064template <typename V1_t, typename V2_t, typename Mask_t>
2066m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2068}
2069
2070/// Matches LoadInst.
2071template <typename OpTy>
2075
2076/// Matches StoreInst.
2077template <typename ValueOpTy, typename PointerOpTy>
2079m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2081 PointerOp);
2082}
2083
2084/// Matches GetElementPtrInst.
2085template <typename... OperandTypes>
2086inline auto m_GEP(const OperandTypes &...Ops) {
2087 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2088}
2089
2090/// Matches GEP with i8 source element type
2091template <typename PointerOpTy, typename OffsetOpTy>
2093m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2095}
2096
2097//===----------------------------------------------------------------------===//
2098// Matchers for CastInst classes
2099//
2100
2101template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2102 Op_t Op;
2103
2104 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2105
2106 template <typename OpTy> bool match(OpTy *V) const {
2107 if (auto *O = dyn_cast<Operator>(V))
2108 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2109 return false;
2110 }
2111};
2112
2113template <typename Op_t, typename Class> struct CastInst_match {
2114 Op_t Op;
2115
2116 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2117
2118 template <typename OpTy> bool match(OpTy *V) const {
2119 if (auto *I = dyn_cast<Class>(V))
2120 return Op.match(I->getOperand(0));
2121 return false;
2122 }
2123};
2124
2125template <typename Op_t> struct PtrToIntSameSize_match {
2127 Op_t Op;
2128
2129 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2130 : DL(DL), Op(OpMatch) {}
2131
2132 template <typename OpTy> bool match(OpTy *V) const {
2133 if (auto *O = dyn_cast<Operator>(V))
2134 return O->getOpcode() == Instruction::PtrToInt &&
2135 DL.getTypeSizeInBits(O->getType()) ==
2136 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2137 Op.match(O->getOperand(0));
2138 return false;
2139 }
2140};
2141
2142template <typename Op_t> struct NNegZExt_match {
2143 Op_t Op;
2144
2145 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2146
2147 template <typename OpTy> bool match(OpTy *V) const {
2148 if (auto *I = dyn_cast<ZExtInst>(V))
2149 return I->hasNonNeg() && Op.match(I->getOperand(0));
2150 return false;
2151 }
2152};
2153
2154template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2155 Op_t Op;
2156
2157 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2158
2159 template <typename OpTy> bool match(OpTy *V) const {
2160 if (auto *I = dyn_cast<TruncInst>(V))
2161 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2162 Op.match(I->getOperand(0));
2163 return false;
2164 }
2165};
2166
2167/// Matches BitCast.
2168template <typename OpTy>
2173
2174template <typename Op_t> struct ElementWiseBitCast_match {
2175 Op_t Op;
2176
2177 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2178
2179 template <typename OpTy> bool match(OpTy *V) const {
2180 auto *I = dyn_cast<BitCastInst>(V);
2181 if (!I)
2182 return false;
2183 Type *SrcType = I->getSrcTy();
2184 Type *DstType = I->getType();
2185 // Make sure the bitcast doesn't change between scalar and vector and
2186 // doesn't change the number of vector elements.
2187 if (SrcType->isVectorTy() != DstType->isVectorTy())
2188 return false;
2189 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2190 SrcVecTy && SrcVecTy->getElementCount() !=
2191 cast<VectorType>(DstType)->getElementCount())
2192 return false;
2193 return Op.match(I->getOperand(0));
2194 }
2195};
2196
2197template <typename OpTy>
2201
2202/// Matches PtrToInt.
2203template <typename OpTy>
2208
2209template <typename OpTy>
2214
2215/// Matches PtrToAddr.
2216template <typename OpTy>
2221
2222/// Matches PtrToInt or PtrToAddr.
2223template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2225}
2226
2227/// Matches IntToPtr.
2228template <typename OpTy>
2233
2234/// Matches any cast or self. Used to ignore casts.
2235template <typename OpTy>
2237m_CastOrSelf(const OpTy &Op) {
2239}
2240
2241/// Matches Trunc.
2242template <typename OpTy>
2246
2247/// Matches trunc nuw.
2248template <typename OpTy>
2253
2254/// Matches trunc nsw.
2255template <typename OpTy>
2260
2261template <typename OpTy>
2263m_TruncOrSelf(const OpTy &Op) {
2264 return m_CombineOr(m_Trunc(Op), Op);
2265}
2266
2267/// Matches SExt.
2268template <typename OpTy>
2272
2273/// Matches ZExt.
2274template <typename OpTy>
2278
2279template <typename OpTy>
2281 return NNegZExt_match<OpTy>(Op);
2282}
2283
2284template <typename OpTy>
2286m_ZExtOrSelf(const OpTy &Op) {
2287 return m_CombineOr(m_ZExt(Op), Op);
2288}
2289
2290template <typename OpTy>
2292m_SExtOrSelf(const OpTy &Op) {
2293 return m_CombineOr(m_SExt(Op), Op);
2294}
2295
2296/// Match either "sext" or "zext nneg".
2297template <typename OpTy>
2299m_SExtLike(const OpTy &Op) {
2300 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2301}
2302
2303template <typename OpTy>
2306m_ZExtOrSExt(const OpTy &Op) {
2307 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2308}
2309
2310template <typename OpTy>
2313 OpTy>
2315 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2316}
2317
2318template <typename OpTy>
2321 OpTy>
2324}
2325
2326template <typename OpTy>
2330
2331template <typename OpTy>
2335
2336template <typename OpTy>
2340
2341template <typename OpTy>
2345
2346template <typename OpTy>
2350
2351template <typename OpTy>
2355
2356//===----------------------------------------------------------------------===//
2357// Matchers for control flow.
2358//
2359
2360struct br_match {
2362
2364
2365 template <typename OpTy> bool match(OpTy *V) const {
2366 if (auto *BI = dyn_cast<BranchInst>(V))
2367 if (BI->isUnconditional()) {
2368 Succ = BI->getSuccessor(0);
2369 return true;
2370 }
2371 return false;
2372 }
2373};
2374
2375inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2376
2377template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2379 Cond_t Cond;
2380 TrueBlock_t T;
2381 FalseBlock_t F;
2382
2383 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2384 : Cond(C), T(t), F(f) {}
2385
2386 template <typename OpTy> bool match(OpTy *V) const {
2387 if (auto *BI = dyn_cast<BranchInst>(V))
2388 if (BI->isConditional() && Cond.match(BI->getCondition()))
2389 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2390 return false;
2391 }
2392};
2393
2394template <typename Cond_t>
2400
2401template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2403m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2405}
2406
2407//===----------------------------------------------------------------------===//
2408// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2409//
2410
2411template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2412 bool Commutable = false>
2414 using PredType = Pred_t;
2417
2418 // The evaluation order is always stable, regardless of Commutability.
2419 // The LHS is always matched first.
2420 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2421
2422 template <typename OpTy> bool match(OpTy *V) const {
2423 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2424 Intrinsic::ID IID = II->getIntrinsicID();
2425 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2426 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2427 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2428 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2429 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2430 return (L.match(LHS) && R.match(RHS)) ||
2431 (Commutable && L.match(RHS) && R.match(LHS));
2432 }
2433 }
2434 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2435 auto *SI = dyn_cast<SelectInst>(V);
2436 if (!SI)
2437 return false;
2438 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2439 if (!Cmp)
2440 return false;
2441 // At this point we have a select conditioned on a comparison. Check that
2442 // it is the values returned by the select that are being compared.
2443 auto *TrueVal = SI->getTrueValue();
2444 auto *FalseVal = SI->getFalseValue();
2445 auto *LHS = Cmp->getOperand(0);
2446 auto *RHS = Cmp->getOperand(1);
2447 if ((TrueVal != LHS || FalseVal != RHS) &&
2448 (TrueVal != RHS || FalseVal != LHS))
2449 return false;
2450 typename CmpInst_t::Predicate Pred =
2451 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2452 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2453 if (!Pred_t::match(Pred))
2454 return false;
2455 // It does! Bind the operands.
2456 return (L.match(LHS) && R.match(RHS)) ||
2457 (Commutable && L.match(RHS) && R.match(LHS));
2458 }
2459};
2460
2461/// Helper class for identifying signed max predicates.
2463 static bool match(ICmpInst::Predicate Pred) {
2464 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2465 }
2466};
2467
2468/// Helper class for identifying signed min predicates.
2470 static bool match(ICmpInst::Predicate Pred) {
2471 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2472 }
2473};
2474
2475/// Helper class for identifying unsigned max predicates.
2477 static bool match(ICmpInst::Predicate Pred) {
2478 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2479 }
2480};
2481
2482/// Helper class for identifying unsigned min predicates.
2484 static bool match(ICmpInst::Predicate Pred) {
2485 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2486 }
2487};
2488
2489/// Helper class for identifying ordered max predicates.
2491 static bool match(FCmpInst::Predicate Pred) {
2492 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2493 }
2494};
2495
2496/// Helper class for identifying ordered min predicates.
2498 static bool match(FCmpInst::Predicate Pred) {
2499 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2500 }
2501};
2502
2503/// Helper class for identifying unordered max predicates.
2505 static bool match(FCmpInst::Predicate Pred) {
2506 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2507 }
2508};
2509
2510/// Helper class for identifying unordered min predicates.
2512 static bool match(FCmpInst::Predicate Pred) {
2513 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2514 }
2515};
2516
2517template <typename LHS, typename RHS>
2522
2523template <typename LHS, typename RHS>
2528
2529template <typename LHS, typename RHS>
2534
2535template <typename LHS, typename RHS>
2540
2541template <typename LHS, typename RHS>
2542inline match_combine_or<
2547m_MaxOrMin(const LHS &L, const RHS &R) {
2548 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2549 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2550}
2551
2552/// Match an 'ordered' floating point maximum function.
2553/// Floating point has one special value 'NaN'. Therefore, there is no total
2554/// order. However, if we can ignore the 'NaN' value (for example, because of a
2555/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2556/// semantics. In the presence of 'NaN' we have to preserve the original
2557/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2558///
2559/// max(L, R) iff L and R are not NaN
2560/// m_OrdFMax(L, R) = R iff L or R are NaN
2561template <typename LHS, typename RHS>
2566
2567/// Match an 'ordered' floating point minimum function.
2568/// Floating point has one special value 'NaN'. Therefore, there is no total
2569/// order. However, if we can ignore the 'NaN' value (for example, because of a
2570/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2571/// semantics. In the presence of 'NaN' we have to preserve the original
2572/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2573///
2574/// min(L, R) iff L and R are not NaN
2575/// m_OrdFMin(L, R) = R iff L or R are NaN
2576template <typename LHS, typename RHS>
2581
2582/// Match an 'unordered' floating point maximum function.
2583/// Floating point has one special value 'NaN'. Therefore, there is no total
2584/// order. However, if we can ignore the 'NaN' value (for example, because of a
2585/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2586/// semantics. In the presence of 'NaN' we have to preserve the original
2587/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2588///
2589/// max(L, R) iff L and R are not NaN
2590/// m_UnordFMax(L, R) = L iff L or R are NaN
2591template <typename LHS, typename RHS>
2593m_UnordFMax(const LHS &L, const RHS &R) {
2595}
2596
2597/// Match an 'unordered' floating point minimum function.
2598/// Floating point has one special value 'NaN'. Therefore, there is no total
2599/// order. However, if we can ignore the 'NaN' value (for example, because of a
2600/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2601/// semantics. In the presence of 'NaN' we have to preserve the original
2602/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2603///
2604/// min(L, R) iff L and R are not NaN
2605/// m_UnordFMin(L, R) = L iff L or R are NaN
2606template <typename LHS, typename RHS>
2608m_UnordFMin(const LHS &L, const RHS &R) {
2610}
2611
2612/// Match an 'ordered' or 'unordered' floating point maximum function.
2613/// Floating point has one special value 'NaN'. Therefore, there is no total
2614/// order. However, if we can ignore the 'NaN' value (for example, because of a
2615/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2616/// semantics.
2617template <typename LHS, typename RHS>
2624
2625/// Match an 'ordered' or 'unordered' floating point minimum function.
2626/// Floating point has one special value 'NaN'. Therefore, there is no total
2627/// order. However, if we can ignore the 'NaN' value (for example, because of a
2628/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2629/// semantics.
2630template <typename LHS, typename RHS>
2637
2638/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2639/// NOTE: we first match the 'Not' (by matching '-1'),
2640/// and only then match the inner matcher!
2641template <typename ValTy>
2642inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2643m_Not(const ValTy &V) {
2644 return m_c_Xor(m_AllOnes(), V);
2645}
2646
2647template <typename ValTy>
2648inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2649 true>
2650m_NotForbidPoison(const ValTy &V) {
2651 return m_c_Xor(m_AllOnesForbidPoison(), V);
2652}
2653
2654//===----------------------------------------------------------------------===//
2655// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2656// Note that S might be matched to other instructions than AddInst.
2657//
2658
2659template <typename LHS_t, typename RHS_t, typename Sum_t>
2663 Sum_t S;
2664
2665 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2666 : L(L), R(R), S(S) {}
2667
2668 template <typename OpTy> bool match(OpTy *V) const {
2669 Value *ICmpLHS, *ICmpRHS;
2670 CmpPredicate Pred;
2671 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2672 return false;
2673
2674 Value *AddLHS, *AddRHS;
2675 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2676
2677 // (a + b) u< a, (a + b) u< b
2678 if (Pred == ICmpInst::ICMP_ULT)
2679 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2680 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2681
2682 // a >u (a + b), b >u (a + b)
2683 if (Pred == ICmpInst::ICMP_UGT)
2684 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2685 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2686
2687 Value *Op1;
2688 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2689 // (~a) <u b
2690 if (Pred == ICmpInst::ICMP_ULT) {
2691 if (XorExpr.match(ICmpLHS))
2692 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2693 }
2694 // b > u (~a)
2695 if (Pred == ICmpInst::ICMP_UGT) {
2696 if (XorExpr.match(ICmpRHS))
2697 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2698 }
2699
2700 // Match special-case for increment-by-1.
2701 if (Pred == ICmpInst::ICMP_EQ) {
2702 // (a + 1) == 0
2703 // (1 + a) == 0
2704 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2705 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2706 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2707 // 0 == (a + 1)
2708 // 0 == (1 + a)
2709 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2710 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2711 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2712 }
2713
2714 return false;
2715 }
2716};
2717
2718/// Match an icmp instruction checking for unsigned overflow on addition.
2719///
2720/// S is matched to the addition whose result is being checked for overflow, and
2721/// L and R are matched to the LHS and RHS of S.
2722template <typename LHS_t, typename RHS_t, typename Sum_t>
2724m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2726}
2727
2728template <typename Opnd_t> struct Argument_match {
2729 unsigned OpI;
2730 Opnd_t Val;
2731
2732 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2733
2734 template <typename OpTy> bool match(OpTy *V) const {
2735 // FIXME: Should likely be switched to use `CallBase`.
2736 if (const auto *CI = dyn_cast<CallInst>(V))
2737 return Val.match(CI->getArgOperand(OpI));
2738 return false;
2739 }
2740};
2741
2742/// Match an argument.
2743template <unsigned OpI, typename Opnd_t>
2744inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2745 return Argument_match<Opnd_t>(OpI, Op);
2746}
2747
2748/// Intrinsic matchers.
2750 unsigned ID;
2751
2753
2754 template <typename OpTy> bool match(OpTy *V) const {
2755 if (const auto *CI = dyn_cast<CallInst>(V))
2756 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2757 return F->getIntrinsicID() == ID;
2758 return false;
2759 }
2760};
2761
2762/// Intrinsic matches are combinations of ID matchers, and argument
2763/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2764/// them with lower arity matchers. Here's some convenient typedefs for up to
2765/// several arguments, and more can be added as needed
2766template <typename T0 = void, typename T1 = void, typename T2 = void,
2767 typename T3 = void, typename T4 = void, typename T5 = void,
2768 typename T6 = void, typename T7 = void, typename T8 = void,
2769 typename T9 = void, typename T10 = void>
2771template <typename T0> struct m_Intrinsic_Ty<T0> {
2773};
2774template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2775 using Ty =
2777};
2778template <typename T0, typename T1, typename T2>
2783template <typename T0, typename T1, typename T2, typename T3>
2788
2789template <typename T0, typename T1, typename T2, typename T3, typename T4>
2794
2795template <typename T0, typename T1, typename T2, typename T3, typename T4,
2796 typename T5>
2801
2802/// Match intrinsic calls like this:
2803/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2804template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2805 return IntrinsicID_match(IntrID);
2806}
2807
2808/// Matches MaskedLoad Intrinsic.
2809template <typename Opnd0, typename Opnd1, typename Opnd2>
2811m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2812 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2813}
2814
2815/// Matches MaskedStore Intrinsic.
2816template <typename Opnd0, typename Opnd1, typename Opnd2>
2818m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2819 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2820}
2821
2822/// Matches MaskedGather Intrinsic.
2823template <typename Opnd0, typename Opnd1, typename Opnd2>
2825m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2826 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2827}
2828
2829template <Intrinsic::ID IntrID, typename T0>
2830inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2832}
2833
2834template <Intrinsic::ID IntrID, typename T0, typename T1>
2835inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2836 const T1 &Op1) {
2838}
2839
2840template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2841inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2842m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2843 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2844}
2845
2846template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2847 typename T3>
2849m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2850 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2851}
2852
2853template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2854 typename T3, typename T4>
2856m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2857 const T4 &Op4) {
2858 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2859 m_Argument<4>(Op4));
2860}
2861
2862template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2863 typename T3, typename T4, typename T5>
2865m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2866 const T4 &Op4, const T5 &Op5) {
2867 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2868 m_Argument<5>(Op5));
2869}
2870
2871// Helper intrinsic matching specializations.
2872template <typename Opnd0>
2873inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2875}
2876
2877template <typename Opnd0>
2878inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2880}
2881
2882template <typename Opnd0>
2883inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2884 return m_Intrinsic<Intrinsic::fabs>(Op0);
2885}
2886
2887template <typename Opnd0>
2888inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2890}
2891
2892template <typename Opnd0, typename Opnd1>
2893inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2894 const Opnd1 &Op1) {
2895 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2896}
2897
2898template <typename Opnd0, typename Opnd1>
2899inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2900 const Opnd1 &Op1) {
2901 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2902}
2903
2904template <typename Opnd0, typename Opnd1>
2906m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2907 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2908}
2909
2910template <typename Opnd0, typename Opnd1>
2911inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2912 const Opnd1 &Op1) {
2913 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2914}
2915
2916template <typename Opnd0, typename Opnd1>
2917inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2918 const Opnd1 &Op1) {
2919 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2920}
2921
2922template <typename Opnd0, typename Opnd1>
2924m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2925 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2926}
2927
2928template <typename Opnd0, typename Opnd1, typename Opnd2>
2930m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2931 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2932}
2933
2934template <typename Opnd0, typename Opnd1, typename Opnd2>
2936m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2937 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2938}
2939
2940template <typename Opnd0>
2941inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2942 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2943}
2944
2945template <typename Opnd0, typename Opnd1>
2946inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2947 const Opnd1 &Op1) {
2948 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2949}
2950
2951template <typename Opnd0>
2952inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2954}
2955
2956template <typename Opnd0, typename Opnd1, typename Opnd2>
2958m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2959 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
2960}
2961
2962//===----------------------------------------------------------------------===//
2963// Matchers for two-operands operators with the operators in either order
2964//
2965
2966/// Matches a BinaryOperator with LHS and RHS in either order.
2967template <typename LHS, typename RHS>
2970}
2971
2972/// Matches an ICmp with a predicate over LHS and RHS in either order.
2973/// Swaps the predicate if operands are commuted.
2974template <typename LHS, typename RHS>
2976m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2978}
2979
2980template <typename LHS, typename RHS>
2985
2986/// Matches a specific opcode with LHS and RHS in either order.
2987template <typename LHS, typename RHS>
2989m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2990 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2991}
2992
2993/// Matches a Add with LHS and RHS in either order.
2994template <typename LHS, typename RHS>
2999
3000/// Matches a Mul with LHS and RHS in either order.
3001template <typename LHS, typename RHS>
3006
3007/// Matches an And with LHS and RHS in either order.
3008template <typename LHS, typename RHS>
3013
3014/// Matches an Or with LHS and RHS in either order.
3015template <typename LHS, typename RHS>
3020
3021/// Matches an Xor with LHS and RHS in either order.
3022template <typename LHS, typename RHS>
3027
3028/// Matches a 'Neg' as 'sub 0, V'.
3029template <typename ValTy>
3030inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3031m_Neg(const ValTy &V) {
3032 return m_Sub(m_ZeroInt(), V);
3033}
3034
3035/// Matches a 'Neg' as 'sub nsw 0, V'.
3036template <typename ValTy>
3038 Instruction::Sub,
3040m_NSWNeg(const ValTy &V) {
3041 return m_NSWSub(m_ZeroInt(), V);
3042}
3043
3044/// Matches an SMin with LHS and RHS in either order.
3045template <typename LHS, typename RHS>
3047m_c_SMin(const LHS &L, const RHS &R) {
3049}
3050/// Matches an SMax with LHS and RHS in either order.
3051template <typename LHS, typename RHS>
3053m_c_SMax(const LHS &L, const RHS &R) {
3055}
3056/// Matches a UMin with LHS and RHS in either order.
3057template <typename LHS, typename RHS>
3059m_c_UMin(const LHS &L, const RHS &R) {
3061}
3062/// Matches a UMax with LHS and RHS in either order.
3063template <typename LHS, typename RHS>
3065m_c_UMax(const LHS &L, const RHS &R) {
3067}
3068
3069template <typename LHS, typename RHS>
3070inline match_combine_or<
3075m_c_MaxOrMin(const LHS &L, const RHS &R) {
3076 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3077 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3078}
3079
3080template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3084
3085 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3086
3087 template <typename OpTy> bool match(OpTy *V) const {
3088 const auto *II = dyn_cast<IntrinsicInst>(V);
3089 if (!II || II->getIntrinsicID() != IntrID)
3090 return false;
3091 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3092 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3093 }
3094};
3095
3096template <Intrinsic::ID IntrID, typename T0, typename T1>
3098m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3100}
3101
3102/// Matches FAdd with LHS and RHS in either order.
3103template <typename LHS, typename RHS>
3105m_c_FAdd(const LHS &L, const RHS &R) {
3107}
3108
3109/// Matches FMul with LHS and RHS in either order.
3110template <typename LHS, typename RHS>
3112m_c_FMul(const LHS &L, const RHS &R) {
3114}
3115
3116template <typename Opnd_t> struct Signum_match {
3117 Opnd_t Val;
3118 Signum_match(const Opnd_t &V) : Val(V) {}
3119
3120 template <typename OpTy> bool match(OpTy *V) const {
3121 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3122 if (TypeSize == 0)
3123 return false;
3124
3125 unsigned ShiftWidth = TypeSize - 1;
3126 Value *Op;
3127
3128 // This is the representation of signum we match:
3129 //
3130 // signum(x) == (x >> 63) | (-x >>u 63)
3131 //
3132 // An i1 value is its own signum, so it's correct to match
3133 //
3134 // signum(x) == (x >> 0) | (-x >>u 0)
3135 //
3136 // for i1 values.
3137
3138 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3139 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3140 auto Signum = m_c_Or(LHS, RHS);
3141
3142 return Signum.match(V) && Val.match(Op);
3143 }
3144};
3145
3146/// Matches a signum pattern.
3147///
3148/// signum(x) =
3149/// x > 0 -> 1
3150/// x == 0 -> 0
3151/// x < 0 -> -1
3152template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3153 return Signum_match<Val_t>(V);
3154}
3155
3156template <int Ind, typename Opnd_t> struct ExtractValue_match {
3157 Opnd_t Val;
3158 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3159
3160 template <typename OpTy> bool match(OpTy *V) const {
3161 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3162 // If Ind is -1, don't inspect indices
3163 if (Ind != -1 &&
3164 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3165 return false;
3166 return Val.match(I->getAggregateOperand());
3167 }
3168 return false;
3169 }
3170};
3171
3172/// Match a single index ExtractValue instruction.
3173/// For example m_ExtractValue<1>(...)
3174template <int Ind, typename Val_t>
3178
3179/// Match an ExtractValue instruction with any index.
3180/// For example m_ExtractValue(...)
3181template <typename Val_t>
3182inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3183 return ExtractValue_match<-1, Val_t>(V);
3184}
3185
3186/// Matcher for a single index InsertValue instruction.
3187template <int Ind, typename T0, typename T1> struct InsertValue_match {
3190
3191 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3192
3193 template <typename OpTy> bool match(OpTy *V) const {
3194 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3195 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3196 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3197 }
3198 return false;
3199 }
3200};
3201
3202/// Matches a single index InsertValue instruction.
3203template <int Ind, typename Val_t, typename Elt_t>
3205 const Elt_t &Elt) {
3206 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3207}
3208
3209/// Matches a call to `llvm.vscale()`.
3211
3212template <typename Opnd0, typename Opnd1>
3214m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3216}
3217
3218template <typename Opnd>
3222
3223template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3227
3228 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3229
3230 template <typename T> bool match(T *V) const {
3231 auto *I = dyn_cast<Instruction>(V);
3232 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3233 return false;
3234
3235 if (I->getOpcode() == Opcode) {
3236 auto *Op0 = I->getOperand(0);
3237 auto *Op1 = I->getOperand(1);
3238 return (L.match(Op0) && R.match(Op1)) ||
3239 (Commutable && L.match(Op1) && R.match(Op0));
3240 }
3241
3242 if (auto *Select = dyn_cast<SelectInst>(I)) {
3243 auto *Cond = Select->getCondition();
3244 auto *TVal = Select->getTrueValue();
3245 auto *FVal = Select->getFalseValue();
3246
3247 // Don't match a scalar select of bool vectors.
3248 // Transforms expect a single type for operands if this matches.
3249 if (Cond->getType() != Select->getType())
3250 return false;
3251
3252 if (Opcode == Instruction::And) {
3253 auto *C = dyn_cast<Constant>(FVal);
3254 if (C && C->isNullValue())
3255 return (L.match(Cond) && R.match(TVal)) ||
3256 (Commutable && L.match(TVal) && R.match(Cond));
3257 } else {
3258 assert(Opcode == Instruction::Or);
3259 auto *C = dyn_cast<Constant>(TVal);
3260 if (C && C->isOneValue())
3261 return (L.match(Cond) && R.match(FVal)) ||
3262 (Commutable && L.match(FVal) && R.match(Cond));
3263 }
3264 }
3265
3266 return false;
3267 }
3268};
3269
3270/// Matches L && R either in the form of L & R or L ? R : false.
3271/// Note that the latter form is poison-blocking.
3272template <typename LHS, typename RHS>
3277
3278/// Matches L && R where L and R are arbitrary values.
3279inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3280
3281/// Matches L && R with LHS and RHS in either order.
3282template <typename LHS, typename RHS>
3284m_c_LogicalAnd(const LHS &L, const RHS &R) {
3286}
3287
3288/// Matches L || R either in the form of L | R or L ? true : R.
3289/// Note that the latter form is poison-blocking.
3290template <typename LHS, typename RHS>
3295
3296/// Matches L || R where L and R are arbitrary values.
3297inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3298
3299/// Matches L || R with LHS and RHS in either order.
3300template <typename LHS, typename RHS>
3302m_c_LogicalOr(const LHS &L, const RHS &R) {
3304}
3305
3306/// Matches either L && R or L || R,
3307/// either one being in the either binary or logical form.
3308/// Note that the latter form is poison-blocking.
3309template <typename LHS, typename RHS, bool Commutable = false>
3315
3316/// Matches either L && R or L || R where L and R are arbitrary values.
3317inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3318
3319/// Matches either L && R or L || R with LHS and RHS in either order.
3320template <typename LHS, typename RHS>
3321inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3322 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3323}
3324
3325} // end namespace PatternMatch
3326} // end namespace llvm
3327
3328#endif // LLVM_IR_PATTERNMATCH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static constexpr unsigned long long mask(BlockVerifier::State S)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define T1
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1549
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1521
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h:413
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1130
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
bool isShift() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
'undef' values are things that do not have specified contents.
Definition Constants.h:1430
LLVM Value Representation.
Definition Value.h:75
Base class of all SIMD vector types.
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
ShiftLike_match< LHS, Instruction::LShr > m_LShrOrSelf(const LHS &L, uint64_t &R)
Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
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.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, true > m_c_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, CastInst >, OpTy > m_CastOrSelf(const OpTy &Op)
Matches any cast or self. Used to ignore casts.
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
CommutativeBinaryIntrinsic_match< IntrID, T0, T1 > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
AllowReassoc_match< T > m_AllowReassoc(const T &SubPattern)
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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.
ap_match< APFloat > m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_c_XorLike(const LHS &L, const RHS &R)
Match either (xor L, R), (xor R, L) or (sub nuw R, L) iff R.isMask() Only commutative matcher as the ...
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
ap_match< APFloat > m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R, either one being in the either binary or logical form.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
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)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > >, OpTy > m_ZExtOrSExtOrSelf(const OpTy &Op)
OneUse_match< T > m_OneUse(const T &SubPattern)
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
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.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
Splat_match< T > m_ConstantSplat(const T &SubPattern)
Match a constant splat. TODO: Extend this to non-constant splats.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
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.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
ap_match< APInt > m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
class_match< ConstantFP > m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_not_denormal_fp > m_NonZeroNotDenormalFP()
Match a floating-point non-zero that is not a denormal.
cst_pred_ty< is_all_ones, false > m_AllOnesForbidPoison()
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
class_match< UndefValue > m_UndefValue()
Match an arbitrary UndefValue constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
ShiftLike_match< LHS, Instruction::AShr > m_AShrOrSelf(const LHS &L, uint64_t &R)
Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
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.
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.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedGather Intrinsic.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
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.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
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.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
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.
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
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.: m_SelectCst<-1, 0>(m_Value(V))
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
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.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
SpecificCmpClass_match< LHS, RHS, ICmpInst, true > m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
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.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, TruncInst > >, OpTy > m_ZExtOrTruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
m_Intrinsic_Ty< Opnd >::Ty m_Deinterleave2(const Opnd &Op)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr auto bind_back(FnT &&Fn, BindArgsT &&...BindArgs)
C++23 bind_back.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1770
AllowReassoc_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3, T4 >::Ty, Argument_match< T5 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3 >::Ty, Argument_match< T4 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1 >::Ty, Argument_match< T2 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0 >::Ty, Argument_match< T1 > > Ty
match_combine_and< IntrinsicID_match, Argument_match< T0 > > Ty
Intrinsic matches are combinations of ID matchers, and argument matchers.
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
match_combine_and(const LTy &Left, const RTy &Right)
match_combine_or(const LTy &Left, const RTy &Right)
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying signed max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying signed min predicates.
static bool match(ICmpInst::Predicate Pred)
Match a specified basic block value.
Match a specified floating point value or vector of all elements of that value.
Match a specified integer value or vector of all elements of that value.
Match a specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)