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/FMF.h"
37#include "llvm/IR/InstrTypes.h"
38#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/Operator.h"
43#include "llvm/IR/Value.h"
46#include <cstdint>
47
48using namespace llvm::PatternMatchHelpers;
49
50namespace llvm {
51namespace PatternMatch {
52
53template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
54 return P.match(V);
55}
56
57/// A match functor that can be used as a UnaryPredicate in functional
58/// algorithms like all_of.
59template <typename Val = const Value, typename Pattern>
60auto match_fn(const Pattern &P) {
62}
63
64template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
65 return P.match(Mask);
66}
67
68template <typename SubPattern_t> struct OneUse_match {
69 SubPattern_t SubPattern;
70
71 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
72
73 template <typename OpTy> bool match(OpTy *V) const {
74 return V->hasOneUse() && SubPattern.match(V);
75 }
76};
77
78template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
79 return SubPattern;
80}
81
82template <typename SubPattern_t, int Flag> struct AllowFmf_match {
83 SubPattern_t SubPattern;
85
86 AllowFmf_match(const SubPattern_t &SP) : SubPattern(SP), FMF(Flag) {}
87
88 template <typename OpTy> bool match(OpTy *V) const {
89 auto *I = dyn_cast<FPMathOperator>(V);
90 return I && ((I->getFastMathFlags() & FMF) == FMF) && SubPattern.match(I);
91 }
92};
93
94template <typename T>
96m_AllowReassoc(const T &SubPattern) {
97 return SubPattern;
98}
99
100template <typename T>
102m_AllowReciprocal(const T &SubPattern) {
103 return SubPattern;
104}
105
106template <typename T>
108m_AllowContract(const T &SubPattern) {
109 return SubPattern;
110}
111
112template <typename T>
114m_ApproxFunc(const T &SubPattern) {
115 return SubPattern;
116}
117
118template <typename T>
120 return SubPattern;
121}
122
123template <typename T>
125 return SubPattern;
126}
127
128template <typename T>
130m_NoSignedZeros(const T &SubPattern) {
131 return SubPattern;
132}
133
134/// Match an arbitrary value and ignore it.
135inline auto m_Value() { return m_Isa<Value>(); }
136
137/// Match an arbitrary unary operation and ignore it.
138inline auto m_UnOp() { return m_Isa<UnaryOperator>(); }
139
140/// Match an arbitrary binary operation and ignore it.
141inline auto m_BinOp() { return m_Isa<BinaryOperator>(); }
142
143/// Matches any compare instruction and ignore it.
144inline auto m_Cmp() { return m_Isa<CmpInst>(); }
145
146/// Matches any intrinsic call and ignore it.
147inline auto m_AnyIntrinsic() { return m_Isa<IntrinsicInst>(); }
148
150private:
151 static bool checkAggregate(const ConstantAggregate *CA);
152
153public:
154 static bool check(const Value *V) {
155 if (isa<UndefValue>(V))
156 return true;
157 if (const auto *CA = dyn_cast<ConstantAggregate>(V))
158 return checkAggregate(CA);
159 return false;
160 }
161 template <typename ITy> bool match(ITy *V) const { return check(V); }
162};
163
164/// Match an arbitrary undef constant. This matches poison as well.
165/// If this is an aggregate and contains a non-aggregate element that is
166/// neither undef nor poison, the aggregate is not matched.
167inline auto m_Undef() { return undef_match(); }
168
169/// Match an arbitrary UndefValue constant.
170inline auto m_UndefValue() { return m_Isa<UndefValue>(); }
171
172/// Match an arbitrary poison constant.
173inline auto m_Poison() { return m_Isa<PoisonValue>(); }
174
175/// Match an arbitrary Constant and ignore it.
176inline auto m_Constant() { return m_Isa<Constant>(); }
177
178/// Match an arbitrary ConstantInt and ignore it.
179inline auto m_ConstantInt() { return m_Isa<ConstantInt>(); }
180
181/// Match an arbitrary ConstantFP and ignore it.
182inline auto m_ConstantFP() { return m_Isa<ConstantFP>(); }
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.
216inline auto m_BasicBlock() { return m_Isa<BasicBlock>(); }
217
218/// Inverting matcher
219template <typename Ty> struct match_unless {
220 Ty M;
221
222 match_unless(const Ty &Matcher) : M(Matcher) {}
223
224 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
225};
226
227/// Match if the inner matcher does *NOT* match.
228template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
229 return match_unless<Ty>(M);
230}
231
232template <typename APTy> struct ap_match {
233 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
235 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
236
237 const APTy *&Res;
239
240 ap_match(const APTy *&Res, bool AllowPoison)
242
243 template <typename ITy> bool match(ITy *V) const {
244 if (auto *CI = dyn_cast<ConstantTy>(V)) {
245 Res = &CI->getValue();
246 return true;
247 }
248 if (V->getType()->isVectorTy())
249 if (const auto *C = dyn_cast<Constant>(V))
250 if (auto *CI =
251 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
252 Res = &CI->getValue();
253 return true;
254 }
255 return false;
256 }
257};
258
259/// Match a ConstantInt or splatted ConstantVector, binding the
260/// specified pointer to the contained APInt.
261inline ap_match<APInt> m_APInt(const APInt *&Res) {
262 // Forbid poison by default to maintain previous behavior.
263 return ap_match<APInt>(Res, /* AllowPoison */ false);
264}
265
266/// Match APInt while allowing poison in splat vector constants.
268 return ap_match<APInt>(Res, /* AllowPoison */ true);
269}
270
271/// Match APInt while forbidding poison in splat vector constants.
273 return ap_match<APInt>(Res, /* AllowPoison */ false);
274}
275
276/// Match a ConstantFP or splatted ConstantVector, binding the
277/// specified pointer to the contained APFloat.
279 // Forbid undefs by default to maintain previous behavior.
280 return ap_match<APFloat>(Res, /* AllowPoison */ false);
281}
282
283/// Match APFloat while allowing poison in splat vector constants.
285 return ap_match<APFloat>(Res, /* AllowPoison */ true);
286}
287
288/// Match APFloat while forbidding poison in splat vector constants.
290 return ap_match<APFloat>(Res, /* AllowPoison */ false);
291}
292
293template <int64_t Val> struct constantint_match {
294 template <typename ITy> bool match(ITy *V) const {
295 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
296 const APInt &CIV = CI->getValue();
297 if (Val >= 0)
298 return CIV == static_cast<uint64_t>(Val);
299 // If Val is negative, and CI is shorter than it, truncate to the right
300 // number of bits. If it is larger, then we have to sign extend. Just
301 // compare their negated values.
302 return -CIV == -Val;
303 }
304 return false;
305 }
306};
307
308/// Match a ConstantInt with a specific value.
309template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
310 return constantint_match<Val>();
311}
312
313/// This helper class is used to match constant scalars, vector splats,
314/// and fixed width vectors that satisfy a specified predicate.
315/// For fixed width vector constants, poison elements are ignored if AllowPoison
316/// is true.
317template <typename Predicate, typename ConstantVal, bool AllowPoison>
318struct cstval_pred_ty : public Predicate {
319private:
320 bool matchVector(const Value *V) const {
321 if (const auto *C = dyn_cast<Constant>(V)) {
322 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
323 return this->isValue(CV->getValue());
324
325 // Number of elements of a scalable vector unknown at compile time
326 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
327 if (!FVTy)
328 return false;
329
330 // Non-splat vector constant: check each element for a match.
331 unsigned NumElts = FVTy->getNumElements();
332 assert(NumElts != 0 && "Constant vector with no elements?");
333 bool HasNonPoisonElements = false;
334 for (unsigned i = 0; i != NumElts; ++i) {
335 Constant *Elt = C->getAggregateElement(i);
336 if (!Elt)
337 return false;
338 if (AllowPoison && isa<PoisonValue>(Elt))
339 continue;
340 auto *CV = dyn_cast<ConstantVal>(Elt);
341 if (!CV || !this->isValue(CV->getValue()))
342 return false;
343 HasNonPoisonElements = true;
344 }
345 return HasNonPoisonElements;
346 }
347 return false;
348 }
349
350public:
351 const Constant **Res = nullptr;
352 template <typename ITy> bool match_impl(ITy *V) const {
353 if (const auto *CV = dyn_cast<ConstantVal>(V))
354 return this->isValue(CV->getValue());
355 if (isa<VectorType>(V->getType()))
356 return matchVector(V);
357 return false;
358 }
359
360 template <typename ITy> bool match(ITy *V) const {
361 if (this->match_impl(V)) {
362 if (Res)
363 *Res = cast<Constant>(V);
364 return true;
365 }
366 return false;
367 }
368};
369
370/// specialization of cstval_pred_ty for ConstantInt
371template <typename Predicate, bool AllowPoison = true>
373
374/// specialization of cstval_pred_ty for ConstantFP
375template <typename Predicate>
377 /*AllowPoison=*/true>;
378
379/// This helper class is used to match scalar and vector constants that
380/// satisfy a specified predicate, and bind them to an APInt.
381template <typename Predicate> struct api_pred_ty : public Predicate {
382 const APInt *&Res;
383
384 api_pred_ty(const APInt *&R) : Res(R) {}
385
386 template <typename ITy> bool match(ITy *V) const {
387 if (const auto *CI = dyn_cast<ConstantInt>(V))
388 if (this->isValue(CI->getValue())) {
389 Res = &CI->getValue();
390 return true;
391 }
392 if (V->getType()->isVectorTy())
393 if (const auto *C = dyn_cast<Constant>(V))
394 if (auto *CI = dyn_cast_or_null<ConstantInt>(
395 C->getSplatValue(/*AllowPoison=*/true)))
396 if (this->isValue(CI->getValue())) {
397 Res = &CI->getValue();
398 return true;
399 }
400
401 return false;
402 }
403};
404
405/// This helper class is used to match scalar and vector constants that
406/// satisfy a specified predicate, and bind them to an APFloat.
407/// Poison is allowed in splat vector constants.
408template <typename Predicate> struct apf_pred_ty : public Predicate {
409 const APFloat *&Res;
410
411 apf_pred_ty(const APFloat *&R) : Res(R) {}
412
413 template <typename ITy> bool match(ITy *V) const {
414 if (const auto *CI = dyn_cast<ConstantFP>(V))
415 if (this->isValue(CI->getValue())) {
416 Res = &CI->getValue();
417 return true;
418 }
419 if (V->getType()->isVectorTy())
420 if (const auto *C = dyn_cast<Constant>(V))
421 if (auto *CI = dyn_cast_or_null<ConstantFP>(
422 C->getSplatValue(/* AllowPoison */ true)))
423 if (this->isValue(CI->getValue())) {
424 Res = &CI->getValue();
425 return true;
426 }
427
428 return false;
429 }
430};
431
432///////////////////////////////////////////////////////////////////////////////
433//
434// Encapsulate constant value queries for use in templated predicate matchers.
435// This allows checking if constants match using compound predicates and works
436// with vector constants, possibly with relaxed constraints. For example, ignore
437// undef values.
438//
439///////////////////////////////////////////////////////////////////////////////
440
441template <typename APTy> struct custom_checkfn {
442 function_ref<bool(const APTy &)> CheckFn;
443 bool isValue(const APTy &C) const { return CheckFn(C); }
444};
445
446/// Match an integer or vector where CheckFn(ele) for each element is true.
447/// For vectors, poison elements are assumed to match.
449m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
450 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
451}
452
454m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
455 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
456}
457
458/// Match a float or vector where CheckFn(ele) for each element is true.
459/// For vectors, poison elements are assumed to match.
461m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
462 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
463}
464
466m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
467 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
468}
469
471 bool isValue(const APInt &C) const { return true; }
472};
473/// Match an integer or vector with any integral constant.
474/// For vectors, this includes constants with undefined elements.
478
480 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
481};
482
486
488 bool isValue(const APInt &C) const { return C.isAllOnes(); }
489};
490/// Match an integer or vector with all bits set.
491/// For vectors, this includes constants with undefined elements.
495
499
500inline auto m_AllOnesOrPoison() { return m_CombineOr(m_AllOnes(), m_Poison()); }
501
503 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
504};
505/// Match an integer or vector with values having all bits except for the high
506/// bit set (0x7f...).
507/// For vectors, this includes constants with undefined elements.
512 return V;
513}
514
516 bool isValue(const APInt &C) const { return C.isNegative(); }
517};
518/// Match an integer or vector of negative values.
519/// For vectors, this includes constants with undefined elements.
523inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
524
526 bool isValue(const APInt &C) const { return C.isNonNegative(); }
527};
528/// Match an integer or vector of non-negative values.
529/// For vectors, this includes constants with undefined elements.
533inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
534
536 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
537};
538/// Match an integer or vector of strictly positive values.
539/// For vectors, this includes constants with undefined elements.
544 return V;
545}
546
548 bool isValue(const APInt &C) const { return C.isNonPositive(); }
549};
550/// Match an integer or vector of non-positive values.
551/// For vectors, this includes constants with undefined elements.
555inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
556
557struct is_one {
558 bool isValue(const APInt &C) const { return C.isOne(); }
559};
560/// Match an integer 1 or a vector with all elements equal to 1.
561/// For vectors, this includes constants with undefined elements.
563
565 bool isValue(const APInt &C) const { return C.isZero(); }
566};
567/// Match an integer 0 or a vector with all elements equal to 0.
568/// For vectors, this includes constants with undefined elements.
572
574 bool isValue(const APInt &C) const { return !C.isZero(); }
575};
576/// Match a non-zero integer or a vector with all non-zero elements.
577/// For vectors, this includes constants with undefined elements.
581
582struct is_zero {
583 template <typename ITy> bool match(ITy *V) const {
584 auto *C = dyn_cast<Constant>(V);
585 // FIXME: this should be able to do something for scalable vectors
586 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
587 }
588};
589/// Match any null constant or a vector with all elements equal to 0.
590/// For vectors, this includes constants with undefined elements.
591inline is_zero m_Zero() { return is_zero(); }
592
593inline auto m_ZeroOrPoison() { return m_CombineOr(m_Zero(), m_Poison()); }
594
595struct is_power2 {
596 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
597};
598/// Match an integer or vector power-of-2.
599/// For vectors, this includes constants with undefined elements.
601inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
602
604 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
605};
606/// Match a integer or vector negated power-of-2.
607/// For vectors, this includes constants with undefined elements.
612 return V;
613}
614
616 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
617};
618/// Match a integer or vector negated power-of-2.
619/// For vectors, this includes constants with undefined elements.
625 return V;
626}
627
629 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
630};
631/// Match an integer or vector of 0 or power-of-2 values.
632/// For vectors, this includes constants with undefined elements.
637 return V;
638}
639
641 bool isValue(const APInt &C) const { return C.isSignMask(); }
642};
643/// Match an integer or vector with only the sign bit(s) set.
644/// For vectors, this includes constants with undefined elements.
648
650 bool isValue(const APInt &C) const { return C.isMask(); }
651};
652/// Match an integer or vector with only the low bit(s) set.
653/// For vectors, this includes constants with undefined elements.
657inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
658
660 bool isValue(const APInt &C) const { return !C || C.isMask(); }
661};
662/// Match an integer or vector with only the low bit(s) set.
663/// For vectors, this includes constants with undefined elements.
668 return V;
669}
670
673 const APInt *Thr;
674 bool isValue(const APInt &C) const {
675 return ICmpInst::compare(C, *Thr, Pred);
676 }
677};
678/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
679/// to Threshold. For vectors, this includes constants with undefined elements.
681m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
683 P.Pred = Predicate;
684 P.Thr = &Threshold;
685 return P;
686}
687
688struct is_nan {
689 bool isValue(const APFloat &C) const { return C.isNaN(); }
690};
691/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
692/// For vectors, this includes constants with undefined elements.
694
695struct is_nonnan {
696 bool isValue(const APFloat &C) const { return !C.isNaN(); }
697};
698/// Match a non-NaN FP constant.
699/// For vectors, this includes constants with undefined elements.
703
704struct is_inf {
705 bool isValue(const APFloat &C) const { return C.isInfinity(); }
706};
707/// Match a positive or negative infinity FP constant.
708/// For vectors, this includes constants with undefined elements.
710
711template <bool IsNegative> struct is_signed_inf {
712 bool isValue(const APFloat &C) const {
713 return C.isInfinity() && IsNegative == C.isNegative();
714 }
715};
716
717/// Match a positive infinity FP constant.
718/// For vectors, this includes constants with undefined elements.
722
723/// Match a negative infinity FP constant.
724/// For vectors, this includes constants with undefined elements.
728
729struct is_noninf {
730 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
731};
732/// Match a non-infinity FP constant, i.e. finite or NaN.
733/// For vectors, this includes constants with undefined elements.
737
738struct is_finite {
739 bool isValue(const APFloat &C) const { return C.isFinite(); }
740};
741/// Match a finite FP constant, i.e. not infinity or NaN.
742/// For vectors, this includes constants with undefined elements.
746inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
747
749 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
750};
751/// Match a finite non-zero FP constant.
752/// For vectors, this includes constants with undefined elements.
757 return V;
758}
759
761 bool isValue(const APFloat &C) const { return C.isZero(); }
762};
763/// Match a floating-point negative zero or positive zero.
764/// For vectors, this includes constants with undefined elements.
768
770 bool isValue(const APFloat &C) const { return C.isPosZero(); }
771};
772/// Match a floating-point positive zero.
773/// For vectors, this includes constants with undefined elements.
777
779 bool isValue(const APFloat &C) const { return C.isNegZero(); }
780};
781/// Match a floating-point negative zero.
782/// For vectors, this includes constants with undefined elements.
786
788 bool isValue(const APFloat &C) const { return C.isNonZero(); }
789};
790/// Match a floating-point non-zero.
791/// For vectors, this includes constants with undefined elements.
795
797 bool isValue(const APFloat &C) const {
798 return !C.isDenormal() && C.isNonZero();
799 }
800};
801
802/// Match a floating-point non-zero that is not a denormal.
803/// For vectors, this includes constants with undefined elements.
807
808///////////////////////////////////////////////////////////////////////////////
809
810/// Match a value, capturing it if we match.
811inline match_bind<Value> m_Value(Value *&V) { return V; }
812inline match_bind<const Value> m_Value(const Value *&V) { return V; }
813
814/// Match against the nested pattern, and capture the value if we match.
815template <typename Pattern> inline auto m_Value(Value *&V, const Pattern &P) {
816 return m_CombineAnd(P, match_bind<Value>(V));
817}
818
819/// Match against the nested pattern, and capture the value if we match.
820template <typename Pattern>
821inline auto m_Value(const Value *&V, const Pattern &P) {
823}
824
825/// Match an instruction, capturing it if we match.
828 return I;
829}
830
831/// Match against the nested pattern, and capture the instruction if we match.
832template <typename Pattern>
833inline auto m_Instruction(Instruction *&I, const Pattern &P) {
835}
836template <typename Pattern>
837inline auto m_Instruction(const Instruction *&I, const Pattern &P) {
839}
840
841/// Match a unary operator, capturing it if we match.
844 return I;
845}
846/// Match a binary operator, capturing it if we match.
849 return I;
850}
851/// Match any intrinsic call, capturing it if we match.
856/// Match a with overflow intrinsic, capturing it if we match.
862 return I;
863}
864
865/// Match an UndefValue, capturing the value if we match.
867
868/// Match a Constant, capturing the value if we match.
870
871/// Match a ConstantInt, capturing the value if we match.
873
874/// Match a ConstantFP, capturing the value if we match.
876
877/// Match a ConstantExpr, capturing the value if we match.
879
880/// Match a basic block value, capturing it if we match.
883 return V;
884}
885
886// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
887// and use m_Unless(m_ConstantExpr).
889 template <typename ITy> static bool isImmConstant(ITy *V) {
890 if (auto *CV = dyn_cast<Constant>(V)) {
891 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
892 return true;
893
894 if (CV->getType()->isVectorTy()) {
895 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
896 if (!isa<ConstantExpr>(Splat) &&
897 !Splat->containsConstantExpression()) {
898 return true;
899 }
900 }
901 }
902 }
903 return false;
904 }
905};
906
908 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
909};
910
911/// Match an arbitrary immediate Constant and ignore it.
913
916
918
919 template <typename ITy> bool match(ITy *V) const {
920 if (isImmConstant(V)) {
921 VR = cast<Constant>(V);
922 return true;
923 }
924 return false;
925 }
926};
927
928/// Match an immediate Constant, capturing the value if we match.
932
933/// Matcher for specified Value*.
935 const Value *Val;
936
937 specificval_ty(const Value *V) : Val(V) {}
938
939 template <typename ITy> bool match(ITy *V) const { return V == Val; }
940};
941
942/// Match if we have a specific specified value.
943inline specificval_ty m_Specific(const Value *V) { return V; }
944
945/// Like m_Specific(), but works if the specific value to match is determined
946/// as part of the same match() expression. For example:
947/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
948/// bind X before the pattern match starts.
949/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
950/// whichever value m_Value(X) populated.
951inline match_deferred<Value> m_Deferred(Value *const &V) { return V; }
953 return V;
954}
955
956/// Match a specified floating point value or vector of all elements of
957/// that value.
959 double Val;
960
961 specific_fpval(double V) : Val(V) {}
962
963 template <typename ITy> bool match(ITy *V) const {
964 if (const auto *CFP = dyn_cast<ConstantFP>(V))
965 return CFP->isExactlyValue(Val);
966 if (V->getType()->isVectorTy())
967 if (const auto *C = dyn_cast<Constant>(V))
968 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
969 return CFP->isExactlyValue(Val);
970 return false;
971 }
972};
973
974/// Match a specific floating point value or vector with all elements
975/// equal to the value.
976inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
977
978/// Match a float 1.0 or vector with all elements equal to 1.0.
979inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
980
983
985
986 template <typename ITy> bool match(ITy *V) const {
987 const APInt *ConstInt;
988 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
989 return false;
990 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
991 if (!ZExtVal)
992 return false;
993 VR = *ZExtVal;
994 return true;
995 }
996};
997
998/// Match a specified integer value or vector of all elements of that
999/// value.
1000template <bool AllowPoison> struct specific_intval {
1001 const APInt &Val;
1002
1003 specific_intval(const APInt &V) : Val(V) {}
1004
1005 template <typename ITy> bool match(ITy *V) const {
1006 const auto *CI = dyn_cast<ConstantInt>(V);
1007 if (!CI && V->getType()->isVectorTy())
1008 if (const auto *C = dyn_cast<Constant>(V))
1009 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1010
1011 return CI && APInt::isSameValue(CI->getValue(), Val);
1012 }
1013};
1014
1015template <bool AllowPoison> struct specific_intval64 {
1017
1019
1020 template <typename ITy> bool match(ITy *V) const {
1021 const auto *CI = dyn_cast<ConstantInt>(V);
1022 if (!CI && V->getType()->isVectorTy())
1023 if (const auto *C = dyn_cast<Constant>(V))
1024 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1025
1026 return CI && CI->getValue() == Val;
1027 }
1028};
1029
1030/// Match a specific integer value or vector with all elements equal to
1031/// the value.
1033 return specific_intval<false>(V);
1034}
1035
1039
1043
1047
1048/// Match a ConstantInt and bind to its value. This does not match
1049/// ConstantInts wider than 64-bits.
1051
1052/// Match a specified basic block value.
1055
1057
1058 template <typename ITy> bool match(ITy *V) const {
1059 const auto *BB = dyn_cast<BasicBlock>(V);
1060 return BB && BB == Val;
1061 }
1062};
1063
1064/// Match a specific basic block value.
1066 return specific_bbval(BB);
1067}
1068
1069/// A commutative-friendly version of m_Specific().
1071 return BB;
1072}
1074m_Deferred(const BasicBlock *const &BB) {
1075 return BB;
1076}
1077
1078//===----------------------------------------------------------------------===//
1079// Matcher for any binary operator.
1080//
1081template <typename LHS_t, typename RHS_t, bool Commutable = false>
1085
1086 // The evaluation order is always stable, regardless of Commutability.
1087 // The LHS is always matched first.
1088 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1089
1090 template <typename OpTy> bool match(OpTy *V) const {
1091 if (auto *I = dyn_cast<BinaryOperator>(V))
1092 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1093 (Commutable && L.match(I->getOperand(1)) &&
1094 R.match(I->getOperand(0)));
1095 return false;
1096 }
1097};
1098
1099template <typename LHS, typename RHS>
1100inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1101 return AnyBinaryOp_match<LHS, RHS>(L, R);
1102}
1103
1104//===----------------------------------------------------------------------===//
1105// Matcher for any unary operator.
1106// TODO fuse unary, binary matcher into n-ary matcher
1107//
1108template <typename OP_t> struct AnyUnaryOp_match {
1109 OP_t X;
1110
1111 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1112
1113 template <typename OpTy> bool match(OpTy *V) const {
1114 if (auto *I = dyn_cast<UnaryOperator>(V))
1115 return X.match(I->getOperand(0));
1116 return false;
1117 }
1118};
1119
1120template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1121 return AnyUnaryOp_match<OP_t>(X);
1122}
1123
1124//===----------------------------------------------------------------------===//
1125// Matchers for specific binary operators.
1126//
1127
1128template <typename LHS_t, typename RHS_t, unsigned Opcode,
1129 bool Commutable = false>
1133
1134 // The evaluation order is always stable, regardless of Commutability.
1135 // The LHS is always matched first.
1136 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1137
1138 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1139 if (V->getValueID() == Value::InstructionVal + Opc) {
1140 auto *I = cast<BinaryOperator>(V);
1141 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1142 (Commutable && L.match(I->getOperand(1)) &&
1143 R.match(I->getOperand(0)));
1144 }
1145 return false;
1146 }
1147
1148 template <typename OpTy> bool match(OpTy *V) const {
1149 return match(Opcode, V);
1150 }
1151};
1152
1153template <typename LHS, typename RHS>
1155 const RHS &R) {
1157}
1158
1159template <typename LHS, typename RHS>
1161 const RHS &R) {
1163}
1164
1165template <typename LHS, typename RHS>
1167 const RHS &R) {
1169}
1170
1171template <typename LHS, typename RHS>
1173 const RHS &R) {
1175}
1176
1177template <typename Op_t> struct FNeg_match {
1178 Op_t X;
1179
1180 FNeg_match(const Op_t &Op) : X(Op) {}
1181 template <typename OpTy> bool match(OpTy *V) const {
1182 auto *FPMO = dyn_cast<FPMathOperator>(V);
1183 if (!FPMO)
1184 return false;
1185
1186 if (FPMO->getOpcode() == Instruction::FNeg)
1187 return X.match(FPMO->getOperand(0));
1188
1189 if (FPMO->getOpcode() == Instruction::FSub) {
1190 if (FPMO->hasNoSignedZeros()) {
1191 // With 'nsz', any zero goes.
1192 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1193 return false;
1194 } else {
1195 // Without 'nsz', we need fsub -0.0, X exactly.
1196 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1197 return false;
1198 }
1199
1200 return X.match(FPMO->getOperand(1));
1201 }
1202
1203 return false;
1204 }
1205};
1206
1207/// Match 'fneg X' as 'fsub -0.0, X'.
1208template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1209 return FNeg_match<OpTy>(X);
1210}
1211
1212/// Match 'fneg X' as 'fsub +-0.0, X'.
1213template <typename RHS>
1214inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1215m_FNegNSZ(const RHS &X) {
1216 return m_FSub(m_AnyZeroFP(), X);
1217}
1218
1219template <typename LHS, typename RHS>
1221 const RHS &R) {
1223}
1224
1225template <typename LHS, typename RHS>
1227 const RHS &R) {
1229}
1230
1231template <typename LHS, typename RHS>
1233 const RHS &R) {
1235}
1236
1237template <typename LHS, typename RHS>
1239 const RHS &R) {
1241}
1242
1243template <typename LHS, typename RHS>
1245 const RHS &R) {
1247}
1248
1249template <typename LHS, typename RHS>
1251 const RHS &R) {
1253}
1254
1255template <typename LHS, typename RHS>
1257 const RHS &R) {
1259}
1260
1261template <typename LHS, typename RHS>
1263 const RHS &R) {
1265}
1266
1267template <typename LHS, typename RHS>
1269 const RHS &R) {
1271}
1272
1273template <typename LHS, typename RHS>
1275 const RHS &R) {
1277}
1278
1279template <typename LHS, typename RHS>
1281 const RHS &R) {
1283}
1284
1285template <typename LHS, typename RHS>
1287 const RHS &R) {
1289}
1290
1291template <typename LHS, typename RHS>
1293 const RHS &R) {
1295}
1296
1297template <typename LHS, typename RHS>
1299 const RHS &R) {
1301}
1302
1303template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1306
1307 ShiftLike_match(const LHS_t &LHS, uint64_t &RHS) : L(LHS), R(RHS) {}
1308
1309 template <typename OpTy> bool match(OpTy *V) const {
1310 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1311 if (Op->getOpcode() == Opcode)
1312 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1313 L.match(Op->getOperand(0));
1314 }
1315 // Interpreted as shiftop V, 0
1316 R = 0;
1317 return L.match(V);
1318 }
1319};
1320
1321/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1322template <typename LHS>
1327
1328/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1329template <typename LHS>
1334
1335/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1336template <typename LHS>
1341
1342template <typename LHS_t, typename RHS_t, unsigned Opcode,
1343 unsigned WrapFlags = 0, bool Commutable = false>
1347
1348 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1349 : L(LHS), R(RHS) {}
1350
1351 template <typename OpTy> bool match(OpTy *V) const {
1352 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1353 if (Op->getOpcode() != Opcode)
1354 return false;
1356 !Op->hasNoUnsignedWrap())
1357 return false;
1358 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1359 !Op->hasNoSignedWrap())
1360 return false;
1361 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1362 (Commutable && L.match(Op->getOperand(1)) &&
1363 R.match(Op->getOperand(0)));
1364 }
1365 return false;
1366 }
1367};
1368
1369template <typename LHS, typename RHS>
1370inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1372m_NSWAdd(const LHS &L, const RHS &R) {
1373 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1375 R);
1376}
1377template <typename LHS, typename RHS>
1378inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1380m_c_NSWAdd(const LHS &L, const RHS &R) {
1381 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1383 true>(L, R);
1384}
1385template <typename LHS, typename RHS>
1386inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1388m_NSWSub(const LHS &L, const RHS &R) {
1389 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1391 R);
1392}
1393template <typename LHS, typename RHS>
1394inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1396m_NSWMul(const LHS &L, const RHS &R) {
1397 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1399 R);
1400}
1401template <typename LHS, typename RHS>
1402inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1404m_NSWShl(const LHS &L, const RHS &R) {
1405 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1407 R);
1408}
1409
1410template <typename LHS, typename RHS>
1411inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1413m_NUWAdd(const LHS &L, const RHS &R) {
1414 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1416 L, R);
1417}
1418
1419template <typename LHS, typename RHS>
1421 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1422m_c_NUWAdd(const LHS &L, const RHS &R) {
1423 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1425 true>(L, R);
1426}
1427
1428template <typename LHS, typename RHS>
1429inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1431m_NUWSub(const LHS &L, const RHS &R) {
1432 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1434 L, R);
1435}
1436template <typename LHS, typename RHS>
1437inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1439m_NUWMul(const LHS &L, const RHS &R) {
1440 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1442 L, R);
1443}
1444template <typename LHS, typename RHS>
1445inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1447m_NUWShl(const LHS &L, const RHS &R) {
1448 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1450 L, R);
1451}
1452
1453template <typename LHS_t, typename RHS_t, bool Commutable = false>
1455 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1456 unsigned Opcode;
1457
1458 SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
1459 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1460
1461 template <typename OpTy> bool match(OpTy *V) const {
1463 }
1464};
1465
1466/// Matches a specific opcode.
1467template <typename LHS, typename RHS>
1468inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1469 const RHS &R) {
1470 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1471}
1472
1473template <typename LHS, typename RHS, bool Commutable = false>
1475 LHS L;
1476 RHS R;
1477
1478 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1479
1480 template <typename OpTy> bool match(OpTy *V) const {
1481 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1482 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1483 if (!PDI->isDisjoint())
1484 return false;
1485 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1486 (Commutable && L.match(PDI->getOperand(1)) &&
1487 R.match(PDI->getOperand(0)));
1488 }
1489 return false;
1490 }
1491};
1492
1493template <typename LHS, typename RHS>
1494inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
1495 return DisjointOr_match<LHS, RHS>(L, R);
1496}
1497
1498template <typename LHS, typename RHS>
1500 const RHS &R) {
1502}
1503
1504/// Match either "add" or "or disjoint".
1505template <typename LHS, typename RHS>
1508m_AddLike(const LHS &L, const RHS &R) {
1509 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1510}
1511
1512/// Match either "add nsw" or "or disjoint"
1513template <typename LHS, typename RHS>
1514inline match_combine_or<
1515 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1518m_NSWAddLike(const LHS &L, const RHS &R) {
1519 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1520}
1521
1522/// Match either "add nuw" or "or disjoint"
1523template <typename LHS, typename RHS>
1524inline match_combine_or<
1525 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1528m_NUWAddLike(const LHS &L, const RHS &R) {
1529 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1530}
1531
1532template <typename LHS, typename RHS>
1534 LHS L;
1535 RHS R;
1536
1537 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1538
1539 template <typename OpTy> bool match(OpTy *V) const {
1540 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1541 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1542 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1543 ; // Pass
1544 else if (Op->getOpcode() != Instruction::Xor)
1545 return false;
1546 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1547 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1548 }
1549 return false;
1550 }
1551};
1552
1553/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1554/// Only commutative matcher as the `sub` will need to swap the L and R.
1555template <typename LHS, typename RHS>
1556inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1557 return XorLike_match<LHS, RHS>(L, R);
1558}
1559
1560//===----------------------------------------------------------------------===//
1561// Class that matches a group of binary opcodes.
1562//
1563template <typename LHS_t, typename RHS_t, typename Predicate,
1564 bool Commutable = false>
1565struct BinOpPred_match : Predicate {
1568
1569 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1570
1571 template <typename OpTy> bool match(OpTy *V) const {
1572 if (auto *I = dyn_cast<Instruction>(V))
1573 return this->isOpType(I->getOpcode()) &&
1574 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1575 (Commutable && L.match(I->getOperand(1)) &&
1576 R.match(I->getOperand(0))));
1577 return false;
1578 }
1579};
1580
1582 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1583};
1584
1586 bool isOpType(unsigned Opcode) const {
1587 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1588 }
1589};
1590
1592 bool isOpType(unsigned Opcode) const {
1593 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1594 }
1595};
1596
1598 bool isOpType(unsigned Opcode) const {
1599 return Instruction::isBitwiseLogicOp(Opcode);
1600 }
1601};
1602
1604 bool isOpType(unsigned Opcode) const {
1605 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1606 }
1607};
1608
1610 bool isOpType(unsigned Opcode) const {
1611 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1612 }
1613};
1614
1615/// Matches shift operations.
1616template <typename LHS, typename RHS>
1618 const RHS &R) {
1620}
1621
1622/// Matches logical shift operations.
1623template <typename LHS, typename RHS>
1625 const RHS &R) {
1627}
1628
1629/// Matches logical shift operations.
1630template <typename LHS, typename RHS>
1632m_LogicalShift(const LHS &L, const RHS &R) {
1634}
1635
1636/// Matches bitwise logic operations.
1637template <typename LHS, typename RHS>
1639m_BitwiseLogic(const LHS &L, const RHS &R) {
1641}
1642
1643/// Matches bitwise logic operations in either order.
1644template <typename LHS, typename RHS>
1646m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1648}
1649
1650/// Matches integer division operations.
1651template <typename LHS, typename RHS>
1653 const RHS &R) {
1655}
1656
1657/// Matches integer remainder operations.
1658template <typename LHS, typename RHS>
1660 const RHS &R) {
1662}
1663
1664//===----------------------------------------------------------------------===//
1665// Class that matches exact binary ops.
1666//
1667template <typename SubPattern_t> struct Exact_match {
1668 SubPattern_t SubPattern;
1669
1670 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1671
1672 template <typename OpTy> bool match(OpTy *V) const {
1673 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1674 return PEO->isExact() && SubPattern.match(V);
1675 return false;
1676 }
1677};
1678
1679template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1680 return SubPattern;
1681}
1682
1683//===----------------------------------------------------------------------===//
1684// Matchers for CmpInst classes
1685//
1686
1687template <typename LHS_t, typename RHS_t, typename Class,
1688 bool Commutable = false>
1693
1694 // The evaluation order is always stable, regardless of Commutability.
1695 // The LHS is always matched first.
1696 CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
1697 : Predicate(&Pred), L(LHS), R(RHS) {}
1698 CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
1699 : Predicate(nullptr), L(LHS), R(RHS) {}
1700
1701 template <typename OpTy> bool match(OpTy *V) const {
1702 if (auto *I = dyn_cast<Class>(V)) {
1703 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1704 if (Predicate)
1706 return true;
1707 }
1708 if (Commutable && L.match(I->getOperand(1)) &&
1709 R.match(I->getOperand(0))) {
1710 if (Predicate)
1712 return true;
1713 }
1714 }
1715 return false;
1716 }
1717};
1718
1719template <typename LHS, typename RHS>
1721 const RHS &R) {
1722 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1723}
1724
1725template <typename LHS, typename RHS>
1727 const LHS &L, const RHS &R) {
1728 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1729}
1730
1731template <typename LHS, typename RHS>
1733 const LHS &L, const RHS &R) {
1734 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1735}
1736
1737template <typename LHS, typename RHS>
1738inline CmpClass_match<LHS, RHS, CmpInst> m_Cmp(const LHS &L, const RHS &R) {
1740}
1741
1742template <typename LHS, typename RHS>
1743inline CmpClass_match<LHS, RHS, ICmpInst> m_ICmp(const LHS &L, const RHS &R) {
1745}
1746
1747template <typename LHS, typename RHS>
1748inline CmpClass_match<LHS, RHS, FCmpInst> m_FCmp(const LHS &L, const RHS &R) {
1750}
1751
1752// Same as CmpClass, but instead of saving Pred as out output variable, match a
1753// specific input pred for equality.
1754template <typename LHS_t, typename RHS_t, typename Class,
1755 bool Commutable = false>
1760
1761 SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
1762 : Predicate(Pred), L(LHS), R(RHS) {}
1763
1764 template <typename OpTy> bool match(OpTy *V) const {
1765 if (auto *I = dyn_cast<Class>(V)) {
1767 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1768 return true;
1769 if constexpr (Commutable) {
1772 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1773 return true;
1774 }
1775 }
1776
1777 return false;
1778 }
1779};
1780
1781template <typename LHS, typename RHS>
1783m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1784 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1785}
1786
1787template <typename LHS, typename RHS>
1789m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1790 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1791}
1792
1793template <typename LHS, typename RHS>
1795m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1797}
1798
1799template <typename LHS, typename RHS>
1801m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1802 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1803}
1804
1805//===----------------------------------------------------------------------===//
1806// Matchers for instructions with a given opcode and number of operands.
1807//
1808
1809/// Matches instructions with Opcode and three operands.
1810template <typename T0, unsigned Opcode> struct OneOps_match {
1812
1813 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1814
1815 template <typename OpTy> bool match(OpTy *V) const {
1816 if (V->getValueID() == Value::InstructionVal + Opcode) {
1817 auto *I = cast<Instruction>(V);
1818 return Op1.match(I->getOperand(0));
1819 }
1820 return false;
1821 }
1822};
1823
1824/// Matches instructions with Opcode and three operands.
1825template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1828
1829 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1830
1831 template <typename OpTy> bool match(OpTy *V) const {
1832 if (V->getValueID() == Value::InstructionVal + Opcode) {
1833 auto *I = cast<Instruction>(V);
1834 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1835 }
1836 return false;
1837 }
1838};
1839
1840/// Matches instructions with Opcode and three operands.
1841template <typename T0, typename T1, typename T2, unsigned Opcode,
1842 bool CommutableOp2Op3 = false>
1847
1848 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1849 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1850
1851 template <typename OpTy> bool match(OpTy *V) const {
1852 if (V->getValueID() == Value::InstructionVal + Opcode) {
1853 auto *I = cast<Instruction>(V);
1854 if (!Op1.match(I->getOperand(0)))
1855 return false;
1856 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1857 return true;
1858 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1859 Op3.match(I->getOperand(1));
1860 }
1861 return false;
1862 }
1863};
1864
1865/// Matches instructions with Opcode and any number of operands
1866template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1867 std::tuple<OperandTypes...> Operands;
1868
1869 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1870
1871 // Operand matching works by recursively calling match_operands, matching the
1872 // operands left to right. The first version is called for each operand but
1873 // the last, for which the second version is called. The second version of
1874 // match_operands is also used to match each individual operand.
1875 template <int Idx, int Last>
1876 std::enable_if_t<Idx != Last, bool>
1880
1881 template <int Idx, int Last>
1882 std::enable_if_t<Idx == Last, bool>
1884 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1885 }
1886
1887 template <typename OpTy> bool match(OpTy *V) const {
1888 if (V->getValueID() == Value::InstructionVal + Opcode) {
1889 auto *I = cast<Instruction>(V);
1890 return I->getNumOperands() == sizeof...(OperandTypes) &&
1891 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1892 }
1893 return false;
1894 }
1895};
1896
1897/// Matches SelectInst.
1898template <typename Cond, typename LHS, typename RHS>
1900m_Select(const Cond &C, const LHS &L, const RHS &R) {
1902}
1903
1904/// This matches a select of two constants, e.g.:
1905/// m_SelectCst<-1, 0>(m_Value(V))
1906template <int64_t L, int64_t R, typename Cond>
1908 Instruction::Select>
1911}
1912
1913/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1914template <typename LHS, typename RHS>
1915inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1916m_c_Select(const LHS &L, const RHS &R) {
1917 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1918 true>(m_Value(), L, R);
1919}
1920
1921/// Matches FreezeInst.
1922template <typename OpTy>
1926
1927/// Matches InsertElementInst.
1928template <typename Val_t, typename Elt_t, typename Idx_t>
1930m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1932 Val, Elt, Idx);
1933}
1934
1935/// Matches ExtractElementInst.
1936template <typename Val_t, typename Idx_t>
1938m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1940}
1941
1942/// Matches shuffle.
1943template <typename T0, typename T1, typename T2> struct Shuffle_match {
1947
1948 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1949 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1950
1951 template <typename OpTy> bool match(OpTy *V) const {
1952 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1953 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1954 Mask.match(I->getShuffleMask());
1955 }
1956 return false;
1957 }
1958};
1959
1960struct m_Mask {
1963 bool match(ArrayRef<int> Mask) const {
1964 MaskRef = Mask;
1965 return true;
1966 }
1967};
1968
1970 bool match(ArrayRef<int> Mask) const {
1971 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1972 }
1973};
1974
1978 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
1979};
1980
1984 bool match(ArrayRef<int> Mask) const {
1985 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
1986 if (First == Mask.end())
1987 return false;
1988 SplatIndex = *First;
1989 return all_of(Mask,
1990 [First](int Elem) { return Elem == *First || Elem == -1; });
1991 }
1992};
1993
1994template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1995 PointerOpTy PointerOp;
1996 OffsetOpTy OffsetOp;
1997
1998 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2000
2001 template <typename OpTy> bool match(OpTy *V) const {
2002 auto *GEP = dyn_cast<GEPOperator>(V);
2003 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2004 PointerOp.match(GEP->getPointerOperand()) &&
2005 OffsetOp.match(GEP->idx_begin()->get());
2006 }
2007};
2008
2009/// Matches ShuffleVectorInst independently of mask value.
2010template <typename V1_t, typename V2_t>
2012m_Shuffle(const V1_t &v1, const V2_t &v2) {
2014}
2015
2016template <typename V1_t, typename V2_t, typename Mask_t>
2018m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2020}
2021
2022/// Matches LoadInst.
2023template <typename OpTy>
2027
2028/// Matches StoreInst.
2029template <typename ValueOpTy, typename PointerOpTy>
2031m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2033 PointerOp);
2034}
2035
2036/// Matches GetElementPtrInst.
2037template <typename... OperandTypes>
2038inline auto m_GEP(const OperandTypes &...Ops) {
2039 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2040}
2041
2042/// Matches GEP with i8 source element type
2043template <typename PointerOpTy, typename OffsetOpTy>
2045m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2047}
2048
2049//===----------------------------------------------------------------------===//
2050// Matchers for CastInst classes
2051//
2052
2053template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2054 Op_t Op;
2055
2056 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2057
2058 template <typename OpTy> bool match(OpTy *V) const {
2059 if (auto *O = dyn_cast<Operator>(V))
2060 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2061 return false;
2062 }
2063};
2064
2065template <typename Op_t, typename Class> struct CastInst_match {
2066 Op_t Op;
2067
2068 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2069
2070 template <typename OpTy> bool match(OpTy *V) const {
2071 if (auto *I = dyn_cast<Class>(V))
2072 return Op.match(I->getOperand(0));
2073 return false;
2074 }
2075};
2076
2077template <typename Op_t> struct PtrToIntSameSize_match {
2079 Op_t Op;
2080
2081 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2082 : DL(DL), Op(OpMatch) {}
2083
2084 template <typename OpTy> bool match(OpTy *V) const {
2085 if (auto *O = dyn_cast<Operator>(V))
2086 return O->getOpcode() == Instruction::PtrToInt &&
2087 DL.getTypeSizeInBits(O->getType()) ==
2088 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2089 Op.match(O->getOperand(0));
2090 return false;
2091 }
2092};
2093
2094template <typename Op_t> struct NNegZExt_match {
2095 Op_t Op;
2096
2097 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2098
2099 template <typename OpTy> bool match(OpTy *V) const {
2100 if (auto *I = dyn_cast<ZExtInst>(V))
2101 return I->hasNonNeg() && Op.match(I->getOperand(0));
2102 return false;
2103 }
2104};
2105
2106template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2107 Op_t Op;
2108
2109 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2110
2111 template <typename OpTy> bool match(OpTy *V) const {
2112 if (auto *I = dyn_cast<TruncInst>(V))
2113 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2114 Op.match(I->getOperand(0));
2115 return false;
2116 }
2117};
2118
2119/// Matches BitCast.
2120template <typename OpTy>
2125
2126template <typename Op_t> struct ElementWiseBitCast_match {
2127 Op_t Op;
2128
2129 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2130
2131 template <typename OpTy> bool match(OpTy *V) const {
2132 auto *I = dyn_cast<BitCastInst>(V);
2133 if (!I)
2134 return false;
2135 Type *SrcType = I->getSrcTy();
2136 Type *DstType = I->getType();
2137 // Make sure the bitcast doesn't change between scalar and vector and
2138 // doesn't change the number of vector elements.
2139 if (SrcType->isVectorTy() != DstType->isVectorTy())
2140 return false;
2141 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2142 SrcVecTy && SrcVecTy->getElementCount() !=
2143 cast<VectorType>(DstType)->getElementCount())
2144 return false;
2145 return Op.match(I->getOperand(0));
2146 }
2147};
2148
2149template <typename OpTy>
2153
2154/// Matches PtrToInt.
2155template <typename OpTy>
2160
2161template <typename OpTy>
2166
2167/// Matches PtrToAddr.
2168template <typename OpTy>
2173
2174/// Matches PtrToInt or PtrToAddr.
2175template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2177}
2178
2179/// Matches IntToPtr.
2180template <typename OpTy>
2185
2186/// Matches any cast or self. Used to ignore casts.
2187template <typename OpTy>
2189m_CastOrSelf(const OpTy &Op) {
2191}
2192
2193/// Matches Trunc.
2194template <typename OpTy>
2198
2199/// Matches trunc nuw.
2200template <typename OpTy>
2205
2206/// Matches trunc nsw.
2207template <typename OpTy>
2212
2213template <typename OpTy>
2215m_TruncOrSelf(const OpTy &Op) {
2216 return m_CombineOr(m_Trunc(Op), Op);
2217}
2218
2219/// Matches SExt.
2220template <typename OpTy>
2224
2225/// Matches ZExt.
2226template <typename OpTy>
2230
2231template <typename OpTy>
2233 return NNegZExt_match<OpTy>(Op);
2234}
2235
2236template <typename OpTy>
2238m_ZExtOrSelf(const OpTy &Op) {
2239 return m_CombineOr(m_ZExt(Op), Op);
2240}
2241
2242template <typename OpTy>
2244m_SExtOrSelf(const OpTy &Op) {
2245 return m_CombineOr(m_SExt(Op), Op);
2246}
2247
2248/// Match either "sext" or "zext nneg".
2249template <typename OpTy>
2251m_SExtLike(const OpTy &Op) {
2252 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2253}
2254
2255template <typename OpTy>
2258m_ZExtOrSExt(const OpTy &Op) {
2259 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2260}
2261
2262template <typename OpTy>
2265 OpTy>
2267 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2268}
2269
2270template <typename OpTy> inline auto m_ZExtOrTruncOrSelf(const OpTy &Op) {
2271 return m_CombineOr(m_ZExt(Op), m_Trunc(Op), Op);
2272}
2273
2274template <typename LHS_t, typename RHS_t> struct ICmpLike_match {
2278
2280 : Pred(P), L(Left), R(Right) {}
2281
2282 template <typename OpTy> bool match(OpTy *V) const {
2283 if (PatternMatch::match(V, m_ICmp(Pred, L, R)))
2284 return true;
2285 Value *A;
2286 // trunc nuw x to i1 is equivalent to icmp ne x, 0
2287 if (V->getType()->isIntOrIntVectorTy(1) &&
2288 PatternMatch::match(V, m_NUWTrunc(m_Value(A))) && L.match(A) &&
2289 R.match(ConstantInt::getNullValue(A->getType()))) {
2291 return true;
2292 }
2293 return false;
2294 }
2295};
2296
2297template <typename LHS, typename RHS>
2299 const RHS &R) {
2300 return ICmpLike_match<LHS, RHS>(Pred, L, R);
2301}
2302
2303template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2304 CondTy Cond;
2307
2308 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2309 : Cond(C), TrueC(TC), FalseC(FC) {}
2310
2311 template <typename OpTy> bool match(OpTy *V) const {
2312 // select(Cond, TrueC, FalseC) — captures both constants directly
2314 return true;
2315
2316 Type *Ty = V->getType();
2317 Value *CondV = nullptr;
2318
2319 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2320 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2321 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2322 TrueC.match(ConstantInt::get(Ty, 1)) &&
2323 FalseC.match(ConstantInt::get(Ty, 0)))
2324 return true;
2325
2326 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2327 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2328 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2329 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2330 FalseC.match(ConstantInt::get(Ty, 0)))
2331 return true;
2332
2333 return false;
2334 }
2335};
2336
2337/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2338/// select i1 Cond, TrueC, FalseC
2339/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2340/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2341///
2342/// The condition is matched against \p Cond, and the true/false constants
2343/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2344/// constants are bound to \p TrueC and \p FalseC via their matchers.
2345template <typename CondTy, typename LTy, typename RTy>
2347m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2348 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2349}
2350
2351template <typename OpTy>
2355
2356template <typename OpTy>
2360
2361template <typename OpTy>
2364m_IToFP(const OpTy &Op) {
2365 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2366}
2367
2368template <typename OpTy>
2372
2373template <typename OpTy>
2377
2378template <typename OpTy>
2381m_FPToI(const OpTy &Op) {
2382 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2383}
2384
2385template <typename OpTy>
2389
2390template <typename OpTy>
2394
2395//===----------------------------------------------------------------------===//
2396// Matchers for control flow.
2397//
2398
2399struct br_match {
2401
2403
2404 template <typename OpTy> bool match(OpTy *V) const {
2405 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2406 Succ = BI->getSuccessor();
2407 return true;
2408 }
2409 return false;
2410 }
2411};
2412
2413inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2414
2415template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2417 Cond_t Cond;
2418 TrueBlock_t T;
2419 FalseBlock_t F;
2420
2421 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2422 : Cond(C), T(t), F(f) {}
2423
2424 template <typename OpTy> bool match(OpTy *V) const {
2425 if (auto *BI = dyn_cast<CondBrInst>(V))
2426 if (Cond.match(BI->getCondition()))
2427 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2428 return false;
2429 }
2430};
2431
2432template <typename Cond_t>
2438
2439template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2441m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2443}
2444
2445//===----------------------------------------------------------------------===//
2446// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2447//
2448
2449template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2450 bool Commutable = false>
2452 using PredType = Pred_t;
2455
2456 // The evaluation order is always stable, regardless of Commutability.
2457 // The LHS is always matched first.
2458 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2459
2460 template <typename OpTy> bool match(OpTy *V) const {
2461 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2462 Intrinsic::ID IID = II->getIntrinsicID();
2463 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2464 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2465 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2466 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2467 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2468 return (L.match(LHS) && R.match(RHS)) ||
2469 (Commutable && L.match(RHS) && R.match(LHS));
2470 }
2471 }
2472 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2473 auto *SI = dyn_cast<SelectInst>(V);
2474 if (!SI)
2475 return false;
2476 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2477 if (!Cmp)
2478 return false;
2479 // At this point we have a select conditioned on a comparison. Check that
2480 // it is the values returned by the select that are being compared.
2481 auto *TrueVal = SI->getTrueValue();
2482 auto *FalseVal = SI->getFalseValue();
2483 auto *LHS = Cmp->getOperand(0);
2484 auto *RHS = Cmp->getOperand(1);
2485 if ((TrueVal != LHS || FalseVal != RHS) &&
2486 (TrueVal != RHS || FalseVal != LHS))
2487 return false;
2488 typename CmpInst_t::Predicate Pred =
2489 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2490 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2491 if (!Pred_t::match(Pred))
2492 return false;
2493 // It does! Bind the operands.
2494 return (L.match(LHS) && R.match(RHS)) ||
2495 (Commutable && L.match(RHS) && R.match(LHS));
2496 }
2497};
2498
2499/// Helper class for identifying signed max predicates.
2501 static bool match(ICmpInst::Predicate Pred) {
2502 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2503 }
2504};
2505
2506/// Helper class for identifying signed min predicates.
2508 static bool match(ICmpInst::Predicate Pred) {
2509 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2510 }
2511};
2512
2513/// Helper class for identifying unsigned max predicates.
2515 static bool match(ICmpInst::Predicate Pred) {
2516 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2517 }
2518};
2519
2520/// Helper class for identifying unsigned min predicates.
2522 static bool match(ICmpInst::Predicate Pred) {
2523 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2524 }
2525};
2526
2527/// Helper class for identifying ordered max predicates.
2529 static bool match(FCmpInst::Predicate Pred) {
2530 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2531 }
2532};
2533
2534/// Helper class for identifying ordered min predicates.
2536 static bool match(FCmpInst::Predicate Pred) {
2537 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2538 }
2539};
2540
2541/// Helper class for identifying unordered max predicates.
2543 static bool match(FCmpInst::Predicate Pred) {
2544 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2545 }
2546};
2547
2548/// Helper class for identifying unordered min predicates.
2550 static bool match(FCmpInst::Predicate Pred) {
2551 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2552 }
2553};
2554
2555template <typename LHS, typename RHS>
2557 const RHS &R) {
2559}
2560
2561template <typename LHS, typename RHS>
2563 const RHS &R) {
2565}
2566
2567template <typename LHS, typename RHS>
2569 const RHS &R) {
2571}
2572
2573template <typename LHS, typename RHS>
2575 const RHS &R) {
2577}
2578
2579template <typename LHS, typename RHS>
2580inline auto m_MaxOrMin(const LHS &L, const RHS &R) {
2581 return m_CombineOr(m_SMax(L, R), m_SMin(L, R), m_UMax(L, R), m_UMin(L, R));
2582}
2583
2584/// Match an 'ordered' floating point maximum function.
2585/// Floating point has one special value 'NaN'. Therefore, there is no total
2586/// order. However, if we can ignore the 'NaN' value (for example, because of a
2587/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2588/// semantics. In the presence of 'NaN' we have to preserve the original
2589/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2590///
2591/// max(L, R) iff L and R are not NaN
2592/// m_OrdFMax(L, R) = R iff L or R are NaN
2593template <typename LHS, typename RHS>
2598
2599/// Match an 'ordered' floating point minimum function.
2600/// Floating point has one special value 'NaN'. Therefore, there is no total
2601/// order. However, if we can ignore the 'NaN' value (for example, because of a
2602/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2603/// semantics. In the presence of 'NaN' we have to preserve the original
2604/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2605///
2606/// min(L, R) iff L and R are not NaN
2607/// m_OrdFMin(L, R) = R iff L or R are NaN
2608template <typename LHS, typename RHS>
2613
2614/// Match an 'unordered' floating point maximum function.
2615/// Floating point has one special value 'NaN'. Therefore, there is no total
2616/// order. However, if we can ignore the 'NaN' value (for example, because of a
2617/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2618/// semantics. In the presence of 'NaN' we have to preserve the original
2619/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2620///
2621/// max(L, R) iff L and R are not NaN
2622/// m_UnordFMax(L, R) = L iff L or R are NaN
2623template <typename LHS, typename RHS>
2625m_UnordFMax(const LHS &L, const RHS &R) {
2627}
2628
2629/// Match an 'unordered' floating point minimum function.
2630/// Floating point has one special value 'NaN'. Therefore, there is no total
2631/// order. However, if we can ignore the 'NaN' value (for example, because of a
2632/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2633/// semantics. In the presence of 'NaN' we have to preserve the original
2634/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2635///
2636/// min(L, R) iff L and R are not NaN
2637/// m_UnordFMin(L, R) = L iff L or R are NaN
2638template <typename LHS, typename RHS>
2640m_UnordFMin(const LHS &L, const RHS &R) {
2642}
2643
2644/// Match an 'ordered' or 'unordered' floating point maximum function.
2645/// Floating point has one special value 'NaN'. Therefore, there is no total
2646/// order. However, if we can ignore the 'NaN' value (for example, because of a
2647/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2648/// semantics.
2649template <typename LHS, typename RHS>
2656
2657/// Match an 'ordered' or 'unordered' floating point minimum function.
2658/// Floating point has one special value 'NaN'. Therefore, there is no total
2659/// order. However, if we can ignore the 'NaN' value (for example, because of a
2660/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2661/// semantics.
2662template <typename LHS, typename RHS>
2669
2670/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2671/// NOTE: we first match the 'Not' (by matching '-1'),
2672/// and only then match the inner matcher!
2673template <typename ValTy>
2674inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2675m_Not(const ValTy &V) {
2676 return m_c_Xor(m_AllOnes(), V);
2677}
2678
2679template <typename ValTy>
2680inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2681 true>
2682m_NotForbidPoison(const ValTy &V) {
2683 return m_c_Xor(m_AllOnesForbidPoison(), V);
2684}
2685
2686//===----------------------------------------------------------------------===//
2687// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2688// Note that S might be matched to other instructions than AddInst.
2689//
2690
2691template <typename LHS_t, typename RHS_t, typename Sum_t>
2695 Sum_t S;
2696
2697 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2698 : L(L), R(R), S(S) {}
2699
2700 template <typename OpTy> bool match(OpTy *V) const {
2701 Value *ICmpLHS, *ICmpRHS;
2702 CmpPredicate Pred;
2703 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2704 return false;
2705
2706 Value *AddLHS, *AddRHS;
2707 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2708
2709 // (a + b) u< a, (a + b) u< b
2710 if (Pred == ICmpInst::ICMP_ULT)
2711 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2712 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2713
2714 // a >u (a + b), b >u (a + b)
2715 if (Pred == ICmpInst::ICMP_UGT)
2716 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2717 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2718
2719 Value *Op1;
2720 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2721 // (~a) <u b
2722 if (Pred == ICmpInst::ICMP_ULT) {
2723 if (XorExpr.match(ICmpLHS))
2724 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2725 }
2726 // b > u (~a)
2727 if (Pred == ICmpInst::ICMP_UGT) {
2728 if (XorExpr.match(ICmpRHS))
2729 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2730 }
2731
2732 // Match special-case for increment-by-1.
2733 if (Pred == ICmpInst::ICMP_EQ) {
2734 // (a + 1) == 0
2735 // (1 + a) == 0
2736 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2737 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2738 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2739 // 0 == (a + 1)
2740 // 0 == (1 + a)
2741 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2742 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2743 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2744 }
2745
2746 return false;
2747 }
2748};
2749
2750/// Match an icmp instruction checking for unsigned overflow on addition.
2751///
2752/// S is matched to the addition whose result is being checked for overflow, and
2753/// L and R are matched to the LHS and RHS of S.
2754template <typename LHS_t, typename RHS_t, typename Sum_t>
2756m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2758}
2759
2760template <typename Opnd_t> struct Argument_match {
2761 unsigned OpI;
2762 Opnd_t Val;
2763
2764 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2765
2766 template <typename OpTy> bool match(OpTy *V) const {
2767 // FIXME: Should likely be switched to use `CallBase`.
2768 if (const auto *CI = dyn_cast<CallInst>(V))
2769 return Val.match(CI->getArgOperand(OpI));
2770 return false;
2771 }
2772};
2773
2774/// Match an argument.
2775template <unsigned OpI, typename Opnd_t>
2776inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2777 return Argument_match<Opnd_t>(OpI, Op);
2778}
2779
2780/// Intrinsic matchers.
2782 unsigned ID;
2783
2785
2786 template <typename OpTy> bool match(OpTy *V) const {
2787 if (const auto *CI = dyn_cast<CallInst>(V))
2788 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2789 return F->getIntrinsicID() == ID;
2790 return false;
2791 }
2792};
2793
2794/// Match intrinsic calls with any of the given IDs.
2795template <Intrinsic::ID... IntrIDs> struct IntrinsicIDs_match {
2796 template <typename OpTy> bool match(OpTy *V) const {
2797 if (const auto *CI = dyn_cast<CallInst>(V))
2798 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand())) {
2799 Intrinsic::ID ID = F->getIntrinsicID();
2800 return ((ID == IntrIDs) || ...);
2801 }
2802 return false;
2803 }
2804};
2805
2806/// Intrinsic matches are combinations of ID matchers, and argument
2807/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2808/// them with lower arity matchers. Here's some convenient typedefs for up to
2809/// several arguments, and more can be added as needed
2810template <typename T0 = void, typename T1 = void, typename T2 = void,
2811 typename T3 = void, typename T4 = void, typename T5 = void,
2812 typename T6 = void, typename T7 = void, typename T8 = void,
2813 typename T9 = void, typename T10 = void>
2815template <typename T0> struct m_Intrinsic_Ty<T0> {
2817};
2818template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2819 using Ty =
2821};
2822template <typename T0, typename T1, typename T2>
2827template <typename T0, typename T1, typename T2, typename T3>
2832
2833template <typename T0, typename T1, typename T2, typename T3, typename T4>
2838
2839template <typename T0, typename T1, typename T2, typename T3, typename T4,
2840 typename T5>
2845
2846/// Match intrinsic calls like this:
2847/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2848template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2849 return IntrinsicID_match(IntrID);
2850}
2851
2852/// Match intrinsic calls with any of the given IDs like this:
2853/// m_AnyIntrinsic<Intrinsic::fptosi_sat, Intrinsic::fptoui_sat>()
2854/// This is more efficient than using nested m_CombineOr with m_Intrinsic
2855/// because it performs the CallInst/Function cast only once.
2856template <Intrinsic::ID... IntrIDs>
2858 return IntrinsicIDs_match<IntrIDs...>();
2859}
2860
2861/// Matches MaskedLoad Intrinsic.
2862template <typename Opnd0, typename Opnd1, typename Opnd2>
2864m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2865 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2866}
2867
2868/// Matches MaskedStore Intrinsic.
2869template <typename Opnd0, typename Opnd1, typename Opnd2>
2871m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2872 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2873}
2874
2875/// Matches MaskedGather Intrinsic.
2876template <typename Opnd0, typename Opnd1, typename Opnd2>
2878m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2879 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2880}
2881
2882template <Intrinsic::ID IntrID, typename T0>
2883inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2885}
2886
2887template <Intrinsic::ID IntrID, typename T0, typename T1>
2888inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2889 const T1 &Op1) {
2891}
2892
2893template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2894inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2895m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2896 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2897}
2898
2899template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2900 typename T3>
2902m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2903 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2904}
2905
2906template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2907 typename T3, typename T4>
2909m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2910 const T4 &Op4) {
2911 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2912 m_Argument<4>(Op4));
2913}
2914
2915template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2916 typename T3, typename T4, typename T5>
2918m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2919 const T4 &Op4, const T5 &Op5) {
2920 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2921 m_Argument<5>(Op5));
2922}
2923
2924// Helper intrinsic matching specializations.
2925template <typename Opnd0>
2926inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2928}
2929
2930template <typename Opnd0>
2931inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2933}
2934template <typename Opnd0>
2935inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Ctpop(const Opnd0 &Op0) {
2937}
2938
2939template <typename Opnd0>
2940inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2941 return m_Intrinsic<Intrinsic::fabs>(Op0);
2942}
2943
2944template <typename Opnd0>
2945inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2947}
2948
2949template <typename Opnd0, typename Opnd1>
2950inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_Ctlz(const Opnd0 &Op0,
2951 const Opnd1 &Op1) {
2952 return m_Intrinsic<Intrinsic::ctlz>(Op0, Op1);
2953}
2954
2955template <typename Opnd0, typename Opnd1>
2956inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_Cttz(const Opnd0 &Op0,
2957 const Opnd1 &Op1) {
2958 return m_Intrinsic<Intrinsic::cttz>(Op0, Op1);
2959}
2960
2961template <typename Opnd0, typename Opnd1>
2962inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2963 const Opnd1 &Op1) {
2964 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2965}
2966
2967template <typename Opnd0, typename Opnd1>
2968inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2969 const Opnd1 &Op1) {
2970 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2971}
2972
2973template <typename Opnd0, typename Opnd1>
2975m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2976 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2977}
2978
2979template <typename Opnd0, typename Opnd1>
2980inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2981 const Opnd1 &Op1) {
2982 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2983}
2984
2985template <typename Opnd0, typename Opnd1>
2986inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2987 const Opnd1 &Op1) {
2988 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2989}
2990
2991template <typename Opnd0, typename Opnd1>
2993m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2994 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2995}
2996
2997template <typename Opnd0, typename Opnd1>
3000m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3001 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
3002}
3003
3004template <typename Opnd0, typename Opnd1>
3007m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3008 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
3009}
3010
3011template <typename Opnd0, typename Opnd1, typename Opnd2>
3013m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3014 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
3015}
3016
3017template <typename Opnd0, typename Opnd1, typename Opnd2>
3019m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3020 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
3021}
3022
3023template <typename Opnd0>
3024inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3025 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3026}
3027
3028template <typename Opnd0, typename Opnd1>
3029inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3030 const Opnd1 &Op1) {
3031 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3032}
3033
3034template <typename Opnd0>
3035inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3037}
3038
3039template <typename Opnd0, typename Opnd1, typename Opnd2>
3041m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3042 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3043}
3044
3045//===----------------------------------------------------------------------===//
3046// Matchers for two-operands operators with the operators in either order
3047//
3048
3049/// Matches a BinaryOperator with LHS and RHS in either order.
3050template <typename LHS, typename RHS>
3051inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
3053}
3054
3055/// Matches an ICmp with a predicate over LHS and RHS in either order.
3056/// Swaps the predicate if operands are commuted.
3057template <typename LHS, typename RHS>
3059m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3061}
3062
3063template <typename LHS, typename RHS>
3065 const RHS &R) {
3067}
3068
3069/// Matches a specific opcode with LHS and RHS in either order.
3070template <typename LHS, typename RHS>
3072m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3073 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3074}
3075
3076/// Matches a Add with LHS and RHS in either order.
3077template <typename LHS, typename RHS>
3082
3083/// Matches a Mul with LHS and RHS in either order.
3084template <typename LHS, typename RHS>
3089
3090/// Matches an And with LHS and RHS in either order.
3091template <typename LHS, typename RHS>
3096
3097/// Matches an Or with LHS and RHS in either order.
3098template <typename LHS, typename RHS>
3100 const RHS &R) {
3102}
3103
3104/// Matches an Xor with LHS and RHS in either order.
3105template <typename LHS, typename RHS>
3110
3111/// Matches a 'Neg' as 'sub 0, V'.
3112template <typename ValTy>
3113inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3114m_Neg(const ValTy &V) {
3115 return m_Sub(m_ZeroInt(), V);
3116}
3117
3118/// Matches a 'Neg' as 'sub nsw 0, V'.
3119template <typename ValTy>
3121 Instruction::Sub,
3123m_NSWNeg(const ValTy &V) {
3124 return m_NSWSub(m_ZeroInt(), V);
3125}
3126
3127/// Matches an SMin with LHS and RHS in either order.
3128template <typename LHS, typename RHS>
3130m_c_SMin(const LHS &L, const RHS &R) {
3132}
3133/// Matches an SMax with LHS and RHS in either order.
3134template <typename LHS, typename RHS>
3136m_c_SMax(const LHS &L, const RHS &R) {
3138}
3139/// Matches a UMin with LHS and RHS in either order.
3140template <typename LHS, typename RHS>
3142m_c_UMin(const LHS &L, const RHS &R) {
3144}
3145/// Matches a UMax with LHS and RHS in either order.
3146template <typename LHS, typename RHS>
3148m_c_UMax(const LHS &L, const RHS &R) {
3150}
3151
3152template <typename LHS, typename RHS>
3153inline auto m_c_MaxOrMin(const LHS &L, const RHS &R) {
3154 return m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R), m_c_UMax(L, R),
3155 m_c_UMin(L, R));
3156}
3157
3158template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3160 LHS L;
3161 RHS R;
3162
3163 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3164
3165 template <typename OpTy> bool match(OpTy *V) const {
3166 const auto *II = dyn_cast<IntrinsicInst>(V);
3167 if (!II || II->getIntrinsicID() != IntrID)
3168 return false;
3169 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3170 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3171 }
3172};
3173
3174template <Intrinsic::ID IntrID, typename T0, typename T1>
3176m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3178}
3179
3180/// Matches FAdd with LHS and RHS in either order.
3181template <typename LHS, typename RHS>
3183m_c_FAdd(const LHS &L, const RHS &R) {
3185}
3186
3187/// Matches FMul with LHS and RHS in either order.
3188template <typename LHS, typename RHS>
3190m_c_FMul(const LHS &L, const RHS &R) {
3192}
3193
3194template <typename Opnd_t> struct Signum_match {
3195 Opnd_t Val;
3196 Signum_match(const Opnd_t &V) : Val(V) {}
3197
3198 template <typename OpTy> bool match(OpTy *V) const {
3199 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3200 if (TypeSize == 0)
3201 return false;
3202
3203 unsigned ShiftWidth = TypeSize - 1;
3204 Value *Op;
3205
3206 // This is the representation of signum we match:
3207 //
3208 // signum(x) == (x >> 63) | (-x >>u 63)
3209 //
3210 // An i1 value is its own signum, so it's correct to match
3211 //
3212 // signum(x) == (x >> 0) | (-x >>u 0)
3213 //
3214 // for i1 values.
3215
3216 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3217 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3218 auto Signum = m_c_Or(LHS, RHS);
3219
3220 return Signum.match(V) && Val.match(Op);
3221 }
3222};
3223
3224/// Matches a signum pattern.
3225///
3226/// signum(x) =
3227/// x > 0 -> 1
3228/// x == 0 -> 0
3229/// x < 0 -> -1
3230template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3231 return Signum_match<Val_t>(V);
3232}
3233
3234template <int Ind, typename Opnd_t> struct ExtractValue_match {
3235 Opnd_t Val;
3236 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3237
3238 template <typename OpTy> bool match(OpTy *V) const {
3239 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3240 // If Ind is -1, don't inspect indices
3241 if (Ind != -1 &&
3242 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3243 return false;
3244 return Val.match(I->getAggregateOperand());
3245 }
3246 return false;
3247 }
3248};
3249
3250/// Match a single index ExtractValue instruction.
3251/// For example m_ExtractValue<1>(...)
3252template <int Ind, typename Val_t>
3256
3257/// Match an ExtractValue instruction with any index.
3258/// For example m_ExtractValue(...)
3259template <typename Val_t>
3260inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3261 return ExtractValue_match<-1, Val_t>(V);
3262}
3263
3264/// Matcher for a single index InsertValue instruction.
3265template <int Ind, typename T0, typename T1> struct InsertValue_match {
3268
3269 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3270
3271 template <typename OpTy> bool match(OpTy *V) const {
3272 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3273 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3274 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3275 }
3276 return false;
3277 }
3278};
3279
3280/// Matches a single index InsertValue instruction.
3281template <int Ind, typename Val_t, typename Elt_t>
3283 const Elt_t &Elt) {
3284 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3285}
3286
3287/// Matches a call to `llvm.vscale()`.
3289
3290template <typename Opnd0, typename Opnd1>
3292m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3294}
3295
3296template <typename Opnd>
3300
3301template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3303 LHS L;
3304 RHS R;
3305
3306 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3307
3308 template <typename T> bool match(T *V) const {
3309 auto *I = dyn_cast<Instruction>(V);
3310 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3311 return false;
3312
3313 if (I->getOpcode() == Opcode) {
3314 auto *Op0 = I->getOperand(0);
3315 auto *Op1 = I->getOperand(1);
3316 return (L.match(Op0) && R.match(Op1)) ||
3317 (Commutable && L.match(Op1) && R.match(Op0));
3318 }
3319
3320 if (auto *Select = dyn_cast<SelectInst>(I)) {
3321 auto *Cond = Select->getCondition();
3322 auto *TVal = Select->getTrueValue();
3323 auto *FVal = Select->getFalseValue();
3324
3325 // Don't match a scalar select of bool vectors.
3326 // Transforms expect a single type for operands if this matches.
3327 if (Cond->getType() != Select->getType())
3328 return false;
3329
3330 if (Opcode == Instruction::And) {
3331 auto *C = dyn_cast<Constant>(FVal);
3332 if (C && C->isNullValue())
3333 return (L.match(Cond) && R.match(TVal)) ||
3334 (Commutable && L.match(TVal) && R.match(Cond));
3335 } else {
3336 assert(Opcode == Instruction::Or);
3337 auto *C = dyn_cast<Constant>(TVal);
3338 if (C && C->isOneValue())
3339 return (L.match(Cond) && R.match(FVal)) ||
3340 (Commutable && L.match(FVal) && R.match(Cond));
3341 }
3342 }
3343
3344 return false;
3345 }
3346};
3347
3348/// Matches L && R either in the form of L & R or L ? R : false.
3349/// Note that the latter form is poison-blocking.
3350template <typename LHS, typename RHS>
3352 const RHS &R) {
3354}
3355
3356/// Matches L && R where L and R are arbitrary values.
3357inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3358
3359/// Matches L && R with LHS and RHS in either order.
3360template <typename LHS, typename RHS>
3362m_c_LogicalAnd(const LHS &L, const RHS &R) {
3364}
3365
3366/// Matches L || R either in the form of L | R or L ? true : R.
3367/// Note that the latter form is poison-blocking.
3368template <typename LHS, typename RHS>
3370 const RHS &R) {
3372}
3373
3374/// Matches L || R where L and R are arbitrary values.
3375inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3376
3377/// Matches L || R with LHS and RHS in either order.
3378template <typename LHS, typename RHS>
3380m_c_LogicalOr(const LHS &L, const RHS &R) {
3382}
3383
3384/// Matches either L && R or L || R,
3385/// either one being in the either binary or logical form.
3386/// Note that the latter form is poison-blocking.
3387template <typename LHS, typename RHS, bool Commutable = false>
3393
3394/// Matches either L && R or L || R where L and R are arbitrary values.
3395inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3396
3397/// Matches either L && R or L || R with LHS and RHS in either order.
3398template <typename LHS, typename RHS>
3399inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3400 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3401}
3402
3403} // end namespace PatternMatch
3404} // end namespace llvm
3405
3406#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
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
static constexpr unsigned long long mask(BlockVerifier::State S)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
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
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1575
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
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:740
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
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:559
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1310
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
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
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
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
A wrapper class for inspecting calls to intrinsic functions.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
'undef' values are things that do not have specified contents.
Definition Constants.h:1625
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
Base class of all SIMD vector types.
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
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.
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).
AllowFmf_match< T, FastMathFlags::NoSignedZeros > m_NoSignedZeros(const T &SubPattern)
auto m_Cmp()
Matches any compare instruction and ignore it.
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)
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)
AllowFmf_match< T, FastMathFlags::NoInfs > m_NoInfs(const T &SubPattern)
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.
match_combine_or< typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty, typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty > m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
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)
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
auto m_Poison()
Match an arbitrary poison constant.
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.
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
match_deferred< 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()...
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.
cstfp_pred_ty< is_signed_inf< true > > m_NegInf()
Match a negative infinity FP constant.
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.
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.
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
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.
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
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.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, UIToFPInst >, CastInst_match< OpTy, SIToFPInst > > m_IToFP(const OpTy &Op)
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)
ICmpLike_match< LHS, RHS > m_ICmpLike(CmpPredicate &Pred, const LHS &L, const RHS &R)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
auto m_Value()
Match an arbitrary value and ignore it.
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).
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)
auto m_UndefValue()
Match an arbitrary UndefValue constant.
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
auto m_Constant()
Match an arbitrary Constant and ignore it.
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.
AllowFmf_match< T, FastMathFlags::NoNaNs > m_NoNaNs(const T &SubPattern)
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
match_combine_or< CastInst_match< OpTy, FPToUIInst >, CastInst_match< OpTy, FPToSIInst > > m_FPToI(const OpTy &Op)
cst_pred_ty< is_non_zero_int > m_NonZeroInt()
Match a non-zero integer or a vector with all non-zero elements.
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)
match_bind< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
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)
AllowFmf_match< T, FastMathFlags::AllowReassoc > m_AllowReassoc(const T &SubPattern)
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)
auto m_AnyIntrinsic()
Matches any intrinsic call and ignore it.
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.
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)
AllowFmf_match< T, FastMathFlags::ApproxFunc > m_ApproxFunc(const T &SubPattern)
cstfp_pred_ty< is_signed_inf< false > > m_PosInf()
Match a positive infinity FP constant.
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_ZExtOrTruncOrSelf(const OpTy &Op)
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.
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
auto m_MaxOrMin(const LHS &L, const RHS &R)
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)
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.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
auto m_c_MaxOrMin(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".
m_Intrinsic_Ty< Opnd0 >::Ty m_Ctpop(const Opnd0 &Op0)
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.
auto m_UnOp()
Match an arbitrary unary operation and ignore it.
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.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Cttz(const Opnd0 &Op0, const Opnd1 &Op1)
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.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
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)
AllowFmf_match< T, FastMathFlags::AllowContract > m_AllowContract(const T &SubPattern)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Ctlz(const Opnd0 &Op0, const Opnd1 &Op1)
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.
auto m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
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'.
match_combine_or< typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty, typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty > m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
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)
AllowFmf_match< T, FastMathFlags::AllowReciprocal > m_AllowReciprocal(const T &SubPattern)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
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.
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.
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
This is an optimization pass for GlobalISel generic memory operations.
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:1738
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:1771
Matcher to bind the captured value.
Matcher for a specific value, but stores a reference to the value, not the value itself.
AllowFmf_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)
ICmpLike_match(CmpPredicate &P, const LHS_t &Left, const RHS_t &Right)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
Match intrinsic calls with any of the given IDs.
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)
SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
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,...
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
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 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 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 APFloat &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
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.
Matcher for 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)