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
1982 bool match(ArrayRef<int> Mask) const { return all_equal(Mask); }
1983};
1984
1988 bool match(ArrayRef<int> Mask) const {
1989 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
1990 if (First == Mask.end())
1991 return false;
1992 SplatIndex = *First;
1993 return all_of(Mask,
1994 [First](int Elem) { return Elem == *First || Elem == -1; });
1995 }
1996};
1997
1998template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1999 PointerOpTy PointerOp;
2000 OffsetOpTy OffsetOp;
2001
2002 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2004
2005 template <typename OpTy> bool match(OpTy *V) const {
2006 auto *GEP = dyn_cast<GEPOperator>(V);
2007 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2008 PointerOp.match(GEP->getPointerOperand()) &&
2009 OffsetOp.match(GEP->idx_begin()->get());
2010 }
2011};
2012
2013/// Matches ShuffleVectorInst independently of mask value.
2014template <typename V1_t, typename V2_t>
2016m_Shuffle(const V1_t &v1, const V2_t &v2) {
2018}
2019
2020template <typename V1_t, typename V2_t, typename Mask_t>
2022m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2024}
2025
2026/// Matches LoadInst.
2027template <typename OpTy>
2031
2032/// Matches StoreInst.
2033template <typename ValueOpTy, typename PointerOpTy>
2035m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2037 PointerOp);
2038}
2039
2040/// Matches GetElementPtrInst.
2041template <typename... OperandTypes>
2042inline auto m_GEP(const OperandTypes &...Ops) {
2043 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2044}
2045
2046/// Matches GEP with i8 source element type
2047template <typename PointerOpTy, typename OffsetOpTy>
2049m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2051}
2052
2053//===----------------------------------------------------------------------===//
2054// Matchers for CastInst classes
2055//
2056
2057template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2058 Op_t Op;
2059
2060 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2061
2062 template <typename OpTy> bool match(OpTy *V) const {
2063 if (auto *O = dyn_cast<Operator>(V))
2064 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2065 return false;
2066 }
2067};
2068
2069template <typename Op_t, typename Class> struct CastInst_match {
2070 Op_t Op;
2071
2072 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2073
2074 template <typename OpTy> bool match(OpTy *V) const {
2075 if (auto *I = dyn_cast<Class>(V))
2076 return Op.match(I->getOperand(0));
2077 return false;
2078 }
2079};
2080
2081template <typename Op_t> struct PtrToIntSameSize_match {
2083 Op_t Op;
2084
2085 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2086 : DL(DL), Op(OpMatch) {}
2087
2088 template <typename OpTy> bool match(OpTy *V) const {
2089 if (auto *O = dyn_cast<Operator>(V))
2090 return O->getOpcode() == Instruction::PtrToInt &&
2091 DL.getTypeSizeInBits(O->getType()) ==
2092 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2093 Op.match(O->getOperand(0));
2094 return false;
2095 }
2096};
2097
2098template <typename Op_t> struct NNegZExt_match {
2099 Op_t Op;
2100
2101 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2102
2103 template <typename OpTy> bool match(OpTy *V) const {
2104 if (auto *I = dyn_cast<ZExtInst>(V))
2105 return I->hasNonNeg() && Op.match(I->getOperand(0));
2106 return false;
2107 }
2108};
2109
2110template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2111 Op_t Op;
2112
2113 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2114
2115 template <typename OpTy> bool match(OpTy *V) const {
2116 if (auto *I = dyn_cast<TruncInst>(V))
2117 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2118 Op.match(I->getOperand(0));
2119 return false;
2120 }
2121};
2122
2123/// Matches BitCast.
2124template <typename OpTy>
2129
2130template <typename Op_t> struct ElementWiseBitCast_match {
2131 Op_t Op;
2132
2133 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2134
2135 template <typename OpTy> bool match(OpTy *V) const {
2136 auto *I = dyn_cast<BitCastInst>(V);
2137 if (!I)
2138 return false;
2139 Type *SrcType = I->getSrcTy();
2140 Type *DstType = I->getType();
2141 // Make sure the bitcast doesn't change between scalar and vector and
2142 // doesn't change the number of vector elements.
2143 if (SrcType->isVectorTy() != DstType->isVectorTy())
2144 return false;
2145 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2146 SrcVecTy && SrcVecTy->getElementCount() !=
2147 cast<VectorType>(DstType)->getElementCount())
2148 return false;
2149 return Op.match(I->getOperand(0));
2150 }
2151};
2152
2153template <typename OpTy>
2157
2158/// Matches PtrToInt.
2159template <typename OpTy>
2164
2165template <typename OpTy>
2170
2171/// Matches PtrToAddr.
2172template <typename OpTy>
2177
2178/// Matches PtrToInt or PtrToAddr.
2179template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2181}
2182
2183/// Matches IntToPtr.
2184template <typename OpTy>
2189
2190/// Matches any cast or self. Used to ignore casts.
2191template <typename OpTy>
2193m_CastOrSelf(const OpTy &Op) {
2195}
2196
2197/// Matches Trunc.
2198template <typename OpTy>
2202
2203/// Matches trunc nuw.
2204template <typename OpTy>
2209
2210/// Matches trunc nsw.
2211template <typename OpTy>
2216
2217template <typename OpTy>
2219m_TruncOrSelf(const OpTy &Op) {
2220 return m_CombineOr(m_Trunc(Op), Op);
2221}
2222
2223/// Matches SExt.
2224template <typename OpTy>
2228
2229/// Matches ZExt.
2230template <typename OpTy>
2234
2235template <typename OpTy>
2237 return NNegZExt_match<OpTy>(Op);
2238}
2239
2240template <typename OpTy>
2242m_ZExtOrSelf(const OpTy &Op) {
2243 return m_CombineOr(m_ZExt(Op), Op);
2244}
2245
2246template <typename OpTy>
2248m_SExtOrSelf(const OpTy &Op) {
2249 return m_CombineOr(m_SExt(Op), Op);
2250}
2251
2252/// Match either "sext" or "zext nneg".
2253template <typename OpTy>
2255m_SExtLike(const OpTy &Op) {
2256 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2257}
2258
2259template <typename OpTy>
2262m_ZExtOrSExt(const OpTy &Op) {
2263 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2264}
2265
2266template <typename OpTy>
2269 OpTy>
2271 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2272}
2273
2274template <typename OpTy> inline auto m_ZExtOrTruncOrSelf(const OpTy &Op) {
2275 return m_CombineOr(m_ZExt(Op), m_Trunc(Op), Op);
2276}
2277
2278template <typename LHS_t, typename RHS_t> struct ICmpLike_match {
2282
2284 : Pred(P), L(Left), R(Right) {}
2285
2286 template <typename OpTy> bool match(OpTy *V) const {
2287 if (PatternMatch::match(V, m_ICmp(Pred, L, R)))
2288 return true;
2289 Value *A;
2290 // trunc nuw x to i1 is equivalent to icmp ne x, 0
2291 if (V->getType()->isIntOrIntVectorTy(1) &&
2292 PatternMatch::match(V, m_NUWTrunc(m_Value(A))) && L.match(A) &&
2293 R.match(ConstantInt::getNullValue(A->getType()))) {
2295 return true;
2296 }
2297 return false;
2298 }
2299};
2300
2301template <typename LHS, typename RHS>
2303 const RHS &R) {
2304 return ICmpLike_match<LHS, RHS>(Pred, L, R);
2305}
2306
2307template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2308 CondTy Cond;
2311
2312 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2313 : Cond(C), TrueC(TC), FalseC(FC) {}
2314
2315 template <typename OpTy> bool match(OpTy *V) const {
2316 // select(Cond, TrueC, FalseC) — captures both constants directly
2318 return true;
2319
2320 Type *Ty = V->getType();
2321 Value *CondV = nullptr;
2322
2323 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2324 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2325 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2326 TrueC.match(ConstantInt::get(Ty, 1)) &&
2327 FalseC.match(ConstantInt::get(Ty, 0)))
2328 return true;
2329
2330 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2331 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2332 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2333 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2334 FalseC.match(ConstantInt::get(Ty, 0)))
2335 return true;
2336
2337 return false;
2338 }
2339};
2340
2341/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2342/// select i1 Cond, TrueC, FalseC
2343/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2344/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2345///
2346/// The condition is matched against \p Cond, and the true/false constants
2347/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2348/// constants are bound to \p TrueC and \p FalseC via their matchers.
2349template <typename CondTy, typename LTy, typename RTy>
2351m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2352 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2353}
2354
2355template <typename OpTy>
2359
2360template <typename OpTy>
2364
2365template <typename OpTy>
2368m_IToFP(const OpTy &Op) {
2369 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2370}
2371
2372template <typename OpTy>
2376
2377template <typename OpTy>
2381
2382template <typename OpTy>
2385m_FPToI(const OpTy &Op) {
2386 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2387}
2388
2389template <typename OpTy>
2393
2394template <typename OpTy>
2398
2399//===----------------------------------------------------------------------===//
2400// Matchers for control flow.
2401//
2402
2403struct br_match {
2405
2407
2408 template <typename OpTy> bool match(OpTy *V) const {
2409 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2410 Succ = BI->getSuccessor();
2411 return true;
2412 }
2413 return false;
2414 }
2415};
2416
2417inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2418
2419template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2421 Cond_t Cond;
2422 TrueBlock_t T;
2423 FalseBlock_t F;
2424
2425 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2426 : Cond(C), T(t), F(f) {}
2427
2428 template <typename OpTy> bool match(OpTy *V) const {
2429 if (auto *BI = dyn_cast<CondBrInst>(V))
2430 if (Cond.match(BI->getCondition()))
2431 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2432 return false;
2433 }
2434};
2435
2436template <typename Cond_t>
2442
2443template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2445m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2447}
2448
2449//===----------------------------------------------------------------------===//
2450// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2451//
2452
2453template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2454 bool Commutable = false>
2456 using PredType = Pred_t;
2459
2460 // The evaluation order is always stable, regardless of Commutability.
2461 // The LHS is always matched first.
2462 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2463
2464 template <typename OpTy> bool match(OpTy *V) const {
2465 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2466 Intrinsic::ID IID = II->getIntrinsicID();
2467 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2468 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2469 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2470 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2471 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2472 return (L.match(LHS) && R.match(RHS)) ||
2473 (Commutable && L.match(RHS) && R.match(LHS));
2474 }
2475 }
2476 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2477 auto *SI = dyn_cast<SelectInst>(V);
2478 if (!SI)
2479 return false;
2480 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2481 if (!Cmp)
2482 return false;
2483 // At this point we have a select conditioned on a comparison. Check that
2484 // it is the values returned by the select that are being compared.
2485 auto *TrueVal = SI->getTrueValue();
2486 auto *FalseVal = SI->getFalseValue();
2487 auto *LHS = Cmp->getOperand(0);
2488 auto *RHS = Cmp->getOperand(1);
2489 if ((TrueVal != LHS || FalseVal != RHS) &&
2490 (TrueVal != RHS || FalseVal != LHS))
2491 return false;
2492 typename CmpInst_t::Predicate Pred =
2493 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2494 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2495 if (!Pred_t::match(Pred))
2496 return false;
2497 // It does! Bind the operands.
2498 return (L.match(LHS) && R.match(RHS)) ||
2499 (Commutable && L.match(RHS) && R.match(LHS));
2500 }
2501};
2502
2503/// Helper class for identifying signed max predicates.
2505 static bool match(ICmpInst::Predicate Pred) {
2506 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2507 }
2508};
2509
2510/// Helper class for identifying signed min predicates.
2512 static bool match(ICmpInst::Predicate Pred) {
2513 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2514 }
2515};
2516
2517/// Helper class for identifying unsigned max predicates.
2519 static bool match(ICmpInst::Predicate Pred) {
2520 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2521 }
2522};
2523
2524/// Helper class for identifying unsigned min predicates.
2526 static bool match(ICmpInst::Predicate Pred) {
2527 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2528 }
2529};
2530
2531/// Helper class for identifying ordered max predicates.
2533 static bool match(FCmpInst::Predicate Pred) {
2534 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2535 }
2536};
2537
2538/// Helper class for identifying ordered min predicates.
2540 static bool match(FCmpInst::Predicate Pred) {
2541 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2542 }
2543};
2544
2545/// Helper class for identifying unordered max predicates.
2547 static bool match(FCmpInst::Predicate Pred) {
2548 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2549 }
2550};
2551
2552/// Helper class for identifying unordered min predicates.
2554 static bool match(FCmpInst::Predicate Pred) {
2555 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2556 }
2557};
2558
2559template <typename LHS, typename RHS>
2561 const RHS &R) {
2563}
2564
2565template <typename LHS, typename RHS>
2567 const RHS &R) {
2569}
2570
2571template <typename LHS, typename RHS>
2573 const RHS &R) {
2575}
2576
2577template <typename LHS, typename RHS>
2579 const RHS &R) {
2581}
2582
2583template <typename LHS, typename RHS>
2584inline auto m_MaxOrMin(const LHS &L, const RHS &R) {
2585 return m_CombineOr(m_SMax(L, R), m_SMin(L, R), m_UMax(L, R), m_UMin(L, R));
2586}
2587
2588/// Match an 'ordered' floating point maximum function.
2589/// Floating point has one special value 'NaN'. Therefore, there is no total
2590/// order. However, if we can ignore the 'NaN' value (for example, because of a
2591/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2592/// semantics. In the presence of 'NaN' we have to preserve the original
2593/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2594///
2595/// max(L, R) iff L and R are not NaN
2596/// m_OrdFMax(L, R) = R iff L or R are NaN
2597template <typename LHS, typename RHS>
2602
2603/// Match an 'ordered' floating point minimum function.
2604/// Floating point has one special value 'NaN'. Therefore, there is no total
2605/// order. However, if we can ignore the 'NaN' value (for example, because of a
2606/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2607/// semantics. In the presence of 'NaN' we have to preserve the original
2608/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2609///
2610/// min(L, R) iff L and R are not NaN
2611/// m_OrdFMin(L, R) = R iff L or R are NaN
2612template <typename LHS, typename RHS>
2617
2618/// Match an 'unordered' floating point maximum function.
2619/// Floating point has one special value 'NaN'. Therefore, there is no total
2620/// order. However, if we can ignore the 'NaN' value (for example, because of a
2621/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2622/// semantics. In the presence of 'NaN' we have to preserve the original
2623/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2624///
2625/// max(L, R) iff L and R are not NaN
2626/// m_UnordFMax(L, R) = L iff L or R are NaN
2627template <typename LHS, typename RHS>
2629m_UnordFMax(const LHS &L, const RHS &R) {
2631}
2632
2633/// Match an 'unordered' floating point minimum function.
2634/// Floating point has one special value 'NaN'. Therefore, there is no total
2635/// order. However, if we can ignore the 'NaN' value (for example, because of a
2636/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2637/// semantics. In the presence of 'NaN' we have to preserve the original
2638/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2639///
2640/// min(L, R) iff L and R are not NaN
2641/// m_UnordFMin(L, R) = L iff L or R are NaN
2642template <typename LHS, typename RHS>
2644m_UnordFMin(const LHS &L, const RHS &R) {
2646}
2647
2648/// Match an 'ordered' or 'unordered' floating point maximum function.
2649/// Floating point has one special value 'NaN'. Therefore, there is no total
2650/// order. However, if we can ignore the 'NaN' value (for example, because of a
2651/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2652/// semantics.
2653template <typename LHS, typename RHS>
2660
2661/// Match an 'ordered' or 'unordered' floating point minimum function.
2662/// Floating point has one special value 'NaN'. Therefore, there is no total
2663/// order. However, if we can ignore the 'NaN' value (for example, because of a
2664/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2665/// semantics.
2666template <typename LHS, typename RHS>
2673
2674/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2675/// NOTE: we first match the 'Not' (by matching '-1'),
2676/// and only then match the inner matcher!
2677template <typename ValTy>
2678inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2679m_Not(const ValTy &V) {
2680 return m_c_Xor(m_AllOnes(), V);
2681}
2682
2683template <typename ValTy>
2684inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2685 true>
2686m_NotForbidPoison(const ValTy &V) {
2687 return m_c_Xor(m_AllOnesForbidPoison(), V);
2688}
2689
2690//===----------------------------------------------------------------------===//
2691// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2692// Note that S might be matched to other instructions than AddInst.
2693//
2694
2695template <typename LHS_t, typename RHS_t, typename Sum_t>
2699 Sum_t S;
2700
2701 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2702 : L(L), R(R), S(S) {}
2703
2704 template <typename OpTy> bool match(OpTy *V) const {
2705 Value *ICmpLHS, *ICmpRHS;
2706 CmpPredicate Pred;
2707 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2708 return false;
2709
2710 Value *AddLHS, *AddRHS;
2711 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2712
2713 // (a + b) u< a, (a + b) u< b
2714 if (Pred == ICmpInst::ICMP_ULT)
2715 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2716 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2717
2718 // a >u (a + b), b >u (a + b)
2719 if (Pred == ICmpInst::ICMP_UGT)
2720 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2721 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2722
2723 Value *Op1;
2724 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2725 // (~a) <u b
2726 if (Pred == ICmpInst::ICMP_ULT) {
2727 if (XorExpr.match(ICmpLHS))
2728 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2729 }
2730 // b > u (~a)
2731 if (Pred == ICmpInst::ICMP_UGT) {
2732 if (XorExpr.match(ICmpRHS))
2733 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2734 }
2735
2736 // Match special-case for increment-by-1.
2737 if (Pred == ICmpInst::ICMP_EQ) {
2738 // (a + 1) == 0
2739 // (1 + a) == 0
2740 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2741 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2742 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2743 // 0 == (a + 1)
2744 // 0 == (1 + a)
2745 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2746 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2747 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2748 }
2749
2750 return false;
2751 }
2752};
2753
2754/// Match an icmp instruction checking for unsigned overflow on addition.
2755///
2756/// S is matched to the addition whose result is being checked for overflow, and
2757/// L and R are matched to the LHS and RHS of S.
2758template <typename LHS_t, typename RHS_t, typename Sum_t>
2760m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2762}
2763
2764template <typename Opnd_t> struct Argument_match {
2765 unsigned OpI;
2766 Opnd_t Val;
2767
2768 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2769
2770 template <typename OpTy> bool match(OpTy *V) const {
2771 // FIXME: Should likely be switched to use `CallBase`.
2772 if (const auto *CI = dyn_cast<CallInst>(V))
2773 return Val.match(CI->getArgOperand(OpI));
2774 return false;
2775 }
2776};
2777
2778/// Match an argument.
2779template <unsigned OpI, typename Opnd_t>
2780inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2781 return Argument_match<Opnd_t>(OpI, Op);
2782}
2783
2784/// Intrinsic matchers.
2786 unsigned ID;
2787
2789
2790 template <typename OpTy> bool match(OpTy *V) const {
2791 if (const auto *CI = dyn_cast<CallInst>(V))
2792 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2793 return F->getIntrinsicID() == ID;
2794 return false;
2795 }
2796};
2797
2798/// Match intrinsic calls with any of the given IDs.
2799template <Intrinsic::ID... IntrIDs> struct IntrinsicIDs_match {
2800 template <typename OpTy> bool match(OpTy *V) const {
2801 if (const auto *CI = dyn_cast<CallInst>(V))
2802 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand())) {
2803 Intrinsic::ID ID = F->getIntrinsicID();
2804 return ((ID == IntrIDs) || ...);
2805 }
2806 return false;
2807 }
2808};
2809
2810/// Intrinsic matches are combinations of ID matchers, and argument
2811/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2812/// them with lower arity matchers. Here's some convenient typedefs for up to
2813/// several arguments, and more can be added as needed
2814template <typename T0 = void, typename T1 = void, typename T2 = void,
2815 typename T3 = void, typename T4 = void, typename T5 = void,
2816 typename T6 = void, typename T7 = void, typename T8 = void,
2817 typename T9 = void, typename T10 = void>
2819template <typename T0> struct m_Intrinsic_Ty<T0> {
2821};
2822template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2823 using Ty =
2825};
2826template <typename T0, typename T1, typename T2>
2831template <typename T0, typename T1, typename T2, typename T3>
2836
2837template <typename T0, typename T1, typename T2, typename T3, typename T4>
2842
2843template <typename T0, typename T1, typename T2, typename T3, typename T4,
2844 typename T5>
2849
2850/// Match intrinsic calls like this:
2851/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2852template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2853 return IntrinsicID_match(IntrID);
2854}
2855
2856/// Match intrinsic calls with any of the given IDs like this:
2857/// m_AnyIntrinsic<Intrinsic::fptosi_sat, Intrinsic::fptoui_sat>()
2858/// This is more efficient than using nested m_CombineOr with m_Intrinsic
2859/// because it performs the CallInst/Function cast only once.
2860template <Intrinsic::ID... IntrIDs>
2862 return IntrinsicIDs_match<IntrIDs...>();
2863}
2864
2865/// Matches MaskedLoad Intrinsic.
2866template <typename Opnd0, typename Opnd1, typename Opnd2>
2868m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2869 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2870}
2871
2872/// Matches MaskedStore Intrinsic.
2873template <typename Opnd0, typename Opnd1, typename Opnd2>
2875m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2876 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2877}
2878
2879/// Matches MaskedGather Intrinsic.
2880template <typename Opnd0, typename Opnd1, typename Opnd2>
2882m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2883 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2884}
2885
2886template <Intrinsic::ID IntrID, typename T0>
2887inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2889}
2890
2891template <Intrinsic::ID IntrID, typename T0, typename T1>
2892inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2893 const T1 &Op1) {
2895}
2896
2897template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2898inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2899m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2900 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2901}
2902
2903template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2904 typename T3>
2906m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2907 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2908}
2909
2910template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2911 typename T3, typename T4>
2913m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2914 const T4 &Op4) {
2915 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2916 m_Argument<4>(Op4));
2917}
2918
2919template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2920 typename T3, typename T4, typename T5>
2922m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2923 const T4 &Op4, const T5 &Op5) {
2924 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2925 m_Argument<5>(Op5));
2926}
2927
2928// Helper intrinsic matching specializations.
2929template <typename Opnd0>
2930inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2932}
2933
2934template <typename Opnd0>
2935inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2937}
2938template <typename Opnd0>
2939inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Ctpop(const Opnd0 &Op0) {
2941}
2942
2943template <typename Opnd0>
2944inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2945 return m_Intrinsic<Intrinsic::fabs>(Op0);
2946}
2947
2948template <typename Opnd0>
2949inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2951}
2952
2953template <typename Opnd0, typename Opnd1>
2954inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_Ctlz(const Opnd0 &Op0,
2955 const Opnd1 &Op1) {
2956 return m_Intrinsic<Intrinsic::ctlz>(Op0, Op1);
2957}
2958
2959template <typename Opnd0, typename Opnd1>
2960inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_Cttz(const Opnd0 &Op0,
2961 const Opnd1 &Op1) {
2962 return m_Intrinsic<Intrinsic::cttz>(Op0, Op1);
2963}
2964
2965template <typename Opnd0, typename Opnd1>
2966inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2967 const Opnd1 &Op1) {
2968 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2969}
2970
2971template <typename Opnd0, typename Opnd1>
2972inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2973 const Opnd1 &Op1) {
2974 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2975}
2976
2977template <typename Opnd0, typename Opnd1>
2979m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2980 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2981}
2982
2983template <typename Opnd0, typename Opnd1>
2984inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2985 const Opnd1 &Op1) {
2986 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2987}
2988
2989template <typename Opnd0, typename Opnd1>
2990inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2991 const Opnd1 &Op1) {
2992 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2993}
2994
2995template <typename Opnd0, typename Opnd1>
2997m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2998 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2999}
3000
3001template <typename Opnd0, typename Opnd1>
3004m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3005 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
3006}
3007
3008template <typename Opnd0, typename Opnd1>
3011m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3012 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
3013}
3014
3015template <typename Opnd0, typename Opnd1, typename Opnd2>
3017m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3018 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
3019}
3020
3021template <typename Opnd0, typename Opnd1, typename Opnd2>
3023m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3024 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
3025}
3026
3027template <typename Opnd0>
3028inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3029 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3030}
3031
3032template <typename Opnd0, typename Opnd1>
3033inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3034 const Opnd1 &Op1) {
3035 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3036}
3037
3038template <typename Opnd0>
3039inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3041}
3042
3043template <typename Opnd0, typename Opnd1, typename Opnd2>
3045m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3046 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3047}
3048
3049//===----------------------------------------------------------------------===//
3050// Matchers for two-operands operators with the operators in either order
3051//
3052
3053/// Matches a BinaryOperator with LHS and RHS in either order.
3054template <typename LHS, typename RHS>
3055inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
3057}
3058
3059/// Matches an ICmp with a predicate over LHS and RHS in either order.
3060/// Swaps the predicate if operands are commuted.
3061template <typename LHS, typename RHS>
3063m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3065}
3066
3067template <typename LHS, typename RHS>
3069 const RHS &R) {
3071}
3072
3073/// Matches a specific opcode with LHS and RHS in either order.
3074template <typename LHS, typename RHS>
3076m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3077 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3078}
3079
3080/// Matches a Add with LHS and RHS in either order.
3081template <typename LHS, typename RHS>
3086
3087/// Matches a Mul with LHS and RHS in either order.
3088template <typename LHS, typename RHS>
3093
3094/// Matches an And with LHS and RHS in either order.
3095template <typename LHS, typename RHS>
3100
3101/// Matches an Or with LHS and RHS in either order.
3102template <typename LHS, typename RHS>
3104 const RHS &R) {
3106}
3107
3108/// Matches an Xor with LHS and RHS in either order.
3109template <typename LHS, typename RHS>
3114
3115/// Matches a 'Neg' as 'sub 0, V'.
3116template <typename ValTy>
3117inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3118m_Neg(const ValTy &V) {
3119 return m_Sub(m_ZeroInt(), V);
3120}
3121
3122/// Matches a 'Neg' as 'sub nsw 0, V'.
3123template <typename ValTy>
3125 Instruction::Sub,
3127m_NSWNeg(const ValTy &V) {
3128 return m_NSWSub(m_ZeroInt(), V);
3129}
3130
3131/// Matches an SMin with LHS and RHS in either order.
3132template <typename LHS, typename RHS>
3134m_c_SMin(const LHS &L, const RHS &R) {
3136}
3137/// Matches an SMax with LHS and RHS in either order.
3138template <typename LHS, typename RHS>
3140m_c_SMax(const LHS &L, const RHS &R) {
3142}
3143/// Matches a UMin with LHS and RHS in either order.
3144template <typename LHS, typename RHS>
3146m_c_UMin(const LHS &L, const RHS &R) {
3148}
3149/// Matches a UMax with LHS and RHS in either order.
3150template <typename LHS, typename RHS>
3152m_c_UMax(const LHS &L, const RHS &R) {
3154}
3155
3156template <typename LHS, typename RHS>
3157inline auto m_c_MaxOrMin(const LHS &L, const RHS &R) {
3158 return m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R), m_c_UMax(L, R),
3159 m_c_UMin(L, R));
3160}
3161
3162template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3164 LHS L;
3165 RHS R;
3166
3167 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3168
3169 template <typename OpTy> bool match(OpTy *V) const {
3170 const auto *II = dyn_cast<IntrinsicInst>(V);
3171 if (!II || II->getIntrinsicID() != IntrID)
3172 return false;
3173 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3174 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3175 }
3176};
3177
3178template <Intrinsic::ID IntrID, typename T0, typename T1>
3180m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3182}
3183
3184/// Matches FAdd with LHS and RHS in either order.
3185template <typename LHS, typename RHS>
3187m_c_FAdd(const LHS &L, const RHS &R) {
3189}
3190
3191/// Matches FMul with LHS and RHS in either order.
3192template <typename LHS, typename RHS>
3194m_c_FMul(const LHS &L, const RHS &R) {
3196}
3197
3198template <typename Opnd_t> struct Signum_match {
3199 Opnd_t Val;
3200 Signum_match(const Opnd_t &V) : Val(V) {}
3201
3202 template <typename OpTy> bool match(OpTy *V) const {
3203 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3204 if (TypeSize == 0)
3205 return false;
3206
3207 unsigned ShiftWidth = TypeSize - 1;
3208 Value *Op;
3209
3210 // This is the representation of signum we match:
3211 //
3212 // signum(x) == (x >> 63) | (-x >>u 63)
3213 //
3214 // An i1 value is its own signum, so it's correct to match
3215 //
3216 // signum(x) == (x >> 0) | (-x >>u 0)
3217 //
3218 // for i1 values.
3219
3220 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3221 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3222 auto Signum = m_c_Or(LHS, RHS);
3223
3224 return Signum.match(V) && Val.match(Op);
3225 }
3226};
3227
3228/// Matches a signum pattern.
3229///
3230/// signum(x) =
3231/// x > 0 -> 1
3232/// x == 0 -> 0
3233/// x < 0 -> -1
3234template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3235 return Signum_match<Val_t>(V);
3236}
3237
3238template <int Ind, typename Opnd_t> struct ExtractValue_match {
3239 Opnd_t Val;
3240 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3241
3242 template <typename OpTy> bool match(OpTy *V) const {
3243 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3244 // If Ind is -1, don't inspect indices
3245 if (Ind != -1 &&
3246 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3247 return false;
3248 return Val.match(I->getAggregateOperand());
3249 }
3250 return false;
3251 }
3252};
3253
3254/// Match a single index ExtractValue instruction.
3255/// For example m_ExtractValue<1>(...)
3256template <int Ind, typename Val_t>
3260
3261/// Match an ExtractValue instruction with any index.
3262/// For example m_ExtractValue(...)
3263template <typename Val_t>
3264inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3265 return ExtractValue_match<-1, Val_t>(V);
3266}
3267
3268/// Matcher for a single index InsertValue instruction.
3269template <int Ind, typename T0, typename T1> struct InsertValue_match {
3272
3273 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3274
3275 template <typename OpTy> bool match(OpTy *V) const {
3276 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3277 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3278 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3279 }
3280 return false;
3281 }
3282};
3283
3284/// Matches a single index InsertValue instruction.
3285template <int Ind, typename Val_t, typename Elt_t>
3287 const Elt_t &Elt) {
3288 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3289}
3290
3291/// Matches a call to `llvm.vscale()`.
3293
3294template <typename Opnd0, typename Opnd1>
3296m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3298}
3299
3300template <typename Opnd>
3304
3305template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3307 LHS L;
3308 RHS R;
3309
3310 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3311
3312 template <typename T> bool match(T *V) const {
3313 auto *I = dyn_cast<Instruction>(V);
3314 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3315 return false;
3316
3317 if (I->getOpcode() == Opcode) {
3318 auto *Op0 = I->getOperand(0);
3319 auto *Op1 = I->getOperand(1);
3320 return (L.match(Op0) && R.match(Op1)) ||
3321 (Commutable && L.match(Op1) && R.match(Op0));
3322 }
3323
3324 if (auto *Select = dyn_cast<SelectInst>(I)) {
3325 auto *Cond = Select->getCondition();
3326 auto *TVal = Select->getTrueValue();
3327 auto *FVal = Select->getFalseValue();
3328
3329 // Don't match a scalar select of bool vectors.
3330 // Transforms expect a single type for operands if this matches.
3331 if (Cond->getType() != Select->getType())
3332 return false;
3333
3334 if (Opcode == Instruction::And) {
3335 auto *C = dyn_cast<Constant>(FVal);
3336 if (C && C->isNullValue())
3337 return (L.match(Cond) && R.match(TVal)) ||
3338 (Commutable && L.match(TVal) && R.match(Cond));
3339 } else {
3340 assert(Opcode == Instruction::Or);
3341 auto *C = dyn_cast<Constant>(TVal);
3342 if (C && C->isOneValue())
3343 return (L.match(Cond) && R.match(FVal)) ||
3344 (Commutable && L.match(FVal) && R.match(Cond));
3345 }
3346 }
3347
3348 return false;
3349 }
3350};
3351
3352/// Matches L && R either in the form of L & R or L ? R : false.
3353/// Note that the latter form is poison-blocking.
3354template <typename LHS, typename RHS>
3356 const RHS &R) {
3358}
3359
3360/// Matches L && R where L and R are arbitrary values.
3361inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3362
3363/// Matches L && R with LHS and RHS in either order.
3364template <typename LHS, typename RHS>
3366m_c_LogicalAnd(const LHS &L, const RHS &R) {
3368}
3369
3370/// Matches L || R either in the form of L | R or L ? true : R.
3371/// Note that the latter form is poison-blocking.
3372template <typename LHS, typename RHS>
3374 const RHS &R) {
3376}
3377
3378/// Matches L || R where L and R are arbitrary values.
3379inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3380
3381/// Matches L || R with LHS and RHS in either order.
3382template <typename LHS, typename RHS>
3384m_c_LogicalOr(const LHS &L, const RHS &R) {
3386}
3387
3388/// Matches either L && R or L || R,
3389/// either one being in the either binary or logical form.
3390/// Note that the latter form is poison-blocking.
3391template <typename LHS, typename RHS, bool Commutable = false>
3397
3398/// Matches either L && R or L || R where L and R are arbitrary values.
3399inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3400
3401/// Matches either L && R or L || R with LHS and RHS in either order.
3402template <typename LHS, typename RHS>
3403inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3404 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3405}
3406
3407} // end namespace PatternMatch
3408} // end namespace llvm
3409
3410#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
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2165
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
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)