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
501 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
502};
503/// Match an integer or vector with values having all bits except for the high
504/// bit set (0x7f...).
505/// For vectors, this includes constants with undefined elements.
510 return V;
511}
512
514 bool isValue(const APInt &C) const { return C.isNegative(); }
515};
516/// Match an integer or vector of negative values.
517/// For vectors, this includes constants with undefined elements.
521inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
522
524 bool isValue(const APInt &C) const { return C.isNonNegative(); }
525};
526/// Match an integer or vector of non-negative values.
527/// For vectors, this includes constants with undefined elements.
531inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
532
534 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
535};
536/// Match an integer or vector of strictly positive values.
537/// For vectors, this includes constants with undefined elements.
542 return V;
543}
544
546 bool isValue(const APInt &C) const { return C.isNonPositive(); }
547};
548/// Match an integer or vector of non-positive values.
549/// For vectors, this includes constants with undefined elements.
553inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
554
555struct is_one {
556 bool isValue(const APInt &C) const { return C.isOne(); }
557};
558/// Match an integer 1 or a vector with all elements equal to 1.
559/// For vectors, this includes constants with undefined elements.
561
563 bool isValue(const APInt &C) const { return C.isZero(); }
564};
565/// Match an integer 0 or a vector with all elements equal to 0.
566/// For vectors, this includes constants with undefined elements.
570
572 bool isValue(const APInt &C) const { return !C.isZero(); }
573};
574/// Match a non-zero integer or a vector with all non-zero elements.
575/// For vectors, this includes constants with undefined elements.
579
580struct is_zero {
581 template <typename ITy> bool match(ITy *V) const {
582 auto *C = dyn_cast<Constant>(V);
583 // FIXME: this should be able to do something for scalable vectors
584 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
585 }
586};
587/// Match any null constant or a vector with all elements equal to 0.
588/// For vectors, this includes constants with undefined elements.
589inline is_zero m_Zero() { return is_zero(); }
590
591struct is_power2 {
592 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
593};
594/// Match an integer or vector power-of-2.
595/// For vectors, this includes constants with undefined elements.
597inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
598
600 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
601};
602/// Match a integer or vector negated power-of-2.
603/// For vectors, this includes constants with undefined elements.
608 return V;
609}
610
612 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
613};
614/// Match a integer or vector negated power-of-2.
615/// For vectors, this includes constants with undefined elements.
621 return V;
622}
623
625 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
626};
627/// Match an integer or vector of 0 or power-of-2 values.
628/// For vectors, this includes constants with undefined elements.
633 return V;
634}
635
637 bool isValue(const APInt &C) const { return C.isSignMask(); }
638};
639/// Match an integer or vector with only the sign bit(s) set.
640/// For vectors, this includes constants with undefined elements.
644
646 bool isValue(const APInt &C) const { return C.isMask(); }
647};
648/// Match an integer or vector with only the low bit(s) set.
649/// For vectors, this includes constants with undefined elements.
653inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
654
656 bool isValue(const APInt &C) const { return !C || C.isMask(); }
657};
658/// Match an integer or vector with only the low bit(s) set.
659/// For vectors, this includes constants with undefined elements.
664 return V;
665}
666
669 const APInt *Thr;
670 bool isValue(const APInt &C) const {
671 return ICmpInst::compare(C, *Thr, Pred);
672 }
673};
674/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
675/// to Threshold. For vectors, this includes constants with undefined elements.
677m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
679 P.Pred = Predicate;
680 P.Thr = &Threshold;
681 return P;
682}
683
684struct is_nan {
685 bool isValue(const APFloat &C) const { return C.isNaN(); }
686};
687/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
688/// For vectors, this includes constants with undefined elements.
690
691struct is_nonnan {
692 bool isValue(const APFloat &C) const { return !C.isNaN(); }
693};
694/// Match a non-NaN FP constant.
695/// For vectors, this includes constants with undefined elements.
699
700struct is_inf {
701 bool isValue(const APFloat &C) const { return C.isInfinity(); }
702};
703/// Match a positive or negative infinity FP constant.
704/// For vectors, this includes constants with undefined elements.
706
707template <bool IsNegative> struct is_signed_inf {
708 bool isValue(const APFloat &C) const {
709 return C.isInfinity() && IsNegative == C.isNegative();
710 }
711};
712
713/// Match a positive infinity FP constant.
714/// For vectors, this includes constants with undefined elements.
718
719/// Match a negative infinity FP constant.
720/// For vectors, this includes constants with undefined elements.
724
725struct is_noninf {
726 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
727};
728/// Match a non-infinity FP constant, i.e. finite or NaN.
729/// For vectors, this includes constants with undefined elements.
733
734struct is_finite {
735 bool isValue(const APFloat &C) const { return C.isFinite(); }
736};
737/// Match a finite FP constant, i.e. not infinity or NaN.
738/// For vectors, this includes constants with undefined elements.
742inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
743
745 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
746};
747/// Match a finite non-zero FP constant.
748/// For vectors, this includes constants with undefined elements.
753 return V;
754}
755
757 bool isValue(const APFloat &C) const { return C.isZero(); }
758};
759/// Match a floating-point negative zero or positive zero.
760/// For vectors, this includes constants with undefined elements.
764
766 bool isValue(const APFloat &C) const { return C.isPosZero(); }
767};
768/// Match a floating-point positive zero.
769/// For vectors, this includes constants with undefined elements.
773
775 bool isValue(const APFloat &C) const { return C.isNegZero(); }
776};
777/// Match a floating-point negative zero.
778/// For vectors, this includes constants with undefined elements.
782
784 bool isValue(const APFloat &C) const { return C.isNonZero(); }
785};
786/// Match a floating-point non-zero.
787/// For vectors, this includes constants with undefined elements.
791
793 bool isValue(const APFloat &C) const {
794 return !C.isDenormal() && C.isNonZero();
795 }
796};
797
798/// Match a floating-point non-zero that is not a denormal.
799/// For vectors, this includes constants with undefined elements.
803
804///////////////////////////////////////////////////////////////////////////////
805
806/// Match a value, capturing it if we match.
807inline match_bind<Value> m_Value(Value *&V) { return V; }
808inline match_bind<const Value> m_Value(const Value *&V) { return V; }
809
810/// Match against the nested pattern, and capture the value if we match.
811template <typename Pattern> inline auto m_Value(Value *&V, const Pattern &P) {
812 return m_CombineAnd(P, match_bind<Value>(V));
813}
814
815/// Match against the nested pattern, and capture the value if we match.
816template <typename Pattern>
817inline auto m_Value(const Value *&V, const Pattern &P) {
819}
820
821/// Match an instruction, capturing it if we match.
824 return I;
825}
826
827/// Match against the nested pattern, and capture the instruction if we match.
828template <typename Pattern>
829inline auto m_Instruction(Instruction *&I, const Pattern &P) {
831}
832template <typename Pattern>
833inline auto m_Instruction(const Instruction *&I, const Pattern &P) {
835}
836
837/// Match a unary operator, capturing it if we match.
840 return I;
841}
842/// Match a binary operator, capturing it if we match.
845 return I;
846}
847/// Match any intrinsic call, capturing it if we match.
852/// Match a with overflow intrinsic, capturing it if we match.
858 return I;
859}
860
861/// Match an UndefValue, capturing the value if we match.
863
864/// Match a Constant, capturing the value if we match.
866
867/// Match a ConstantInt, capturing the value if we match.
869
870/// Match a ConstantFP, capturing the value if we match.
872
873/// Match a ConstantExpr, capturing the value if we match.
875
876/// Match a basic block value, capturing it if we match.
879 return V;
880}
881
882// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
883// and use m_Unless(m_ConstantExpr).
885 template <typename ITy> static bool isImmConstant(ITy *V) {
886 if (auto *CV = dyn_cast<Constant>(V)) {
887 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
888 return true;
889
890 if (CV->getType()->isVectorTy()) {
891 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
892 if (!isa<ConstantExpr>(Splat) &&
893 !Splat->containsConstantExpression()) {
894 return true;
895 }
896 }
897 }
898 }
899 return false;
900 }
901};
902
904 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
905};
906
907/// Match an arbitrary immediate Constant and ignore it.
909
912
914
915 template <typename ITy> bool match(ITy *V) const {
916 if (isImmConstant(V)) {
917 VR = cast<Constant>(V);
918 return true;
919 }
920 return false;
921 }
922};
923
924/// Match an immediate Constant, capturing the value if we match.
928
929/// Matcher for specified Value*.
931 const Value *Val;
932
933 specificval_ty(const Value *V) : Val(V) {}
934
935 template <typename ITy> bool match(ITy *V) const { return V == Val; }
936};
937
938/// Match if we have a specific specified value.
939inline specificval_ty m_Specific(const Value *V) { return V; }
940
941/// Like m_Specific(), but works if the specific value to match is determined
942/// as part of the same match() expression. For example:
943/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
944/// bind X before the pattern match starts.
945/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
946/// whichever value m_Value(X) populated.
947inline match_deferred<Value> m_Deferred(Value *const &V) { return V; }
949 return V;
950}
951
952/// Match a specified floating point value or vector of all elements of
953/// that value.
955 double Val;
956
957 specific_fpval(double V) : Val(V) {}
958
959 template <typename ITy> bool match(ITy *V) const {
960 if (const auto *CFP = dyn_cast<ConstantFP>(V))
961 return CFP->isExactlyValue(Val);
962 if (V->getType()->isVectorTy())
963 if (const auto *C = dyn_cast<Constant>(V))
964 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
965 return CFP->isExactlyValue(Val);
966 return false;
967 }
968};
969
970/// Match a specific floating point value or vector with all elements
971/// equal to the value.
972inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
973
974/// Match a float 1.0 or vector with all elements equal to 1.0.
975inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
976
979
981
982 template <typename ITy> bool match(ITy *V) const {
983 const APInt *ConstInt;
984 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
985 return false;
986 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
987 if (!ZExtVal)
988 return false;
989 VR = *ZExtVal;
990 return true;
991 }
992};
993
994/// Match a specified integer value or vector of all elements of that
995/// value.
996template <bool AllowPoison> struct specific_intval {
997 const APInt &Val;
998
999 specific_intval(const APInt &V) : Val(V) {}
1000
1001 template <typename ITy> bool match(ITy *V) const {
1002 const auto *CI = dyn_cast<ConstantInt>(V);
1003 if (!CI && V->getType()->isVectorTy())
1004 if (const auto *C = dyn_cast<Constant>(V))
1005 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1006
1007 return CI && APInt::isSameValue(CI->getValue(), Val);
1008 }
1009};
1010
1011template <bool AllowPoison> struct specific_intval64 {
1013
1015
1016 template <typename ITy> bool match(ITy *V) const {
1017 const auto *CI = dyn_cast<ConstantInt>(V);
1018 if (!CI && V->getType()->isVectorTy())
1019 if (const auto *C = dyn_cast<Constant>(V))
1020 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1021
1022 return CI && CI->getValue() == Val;
1023 }
1024};
1025
1026/// Match a specific integer value or vector with all elements equal to
1027/// the value.
1029 return specific_intval<false>(V);
1030}
1031
1035
1039
1043
1044/// Match a ConstantInt and bind to its value. This does not match
1045/// ConstantInts wider than 64-bits.
1047
1048/// Match a specified basic block value.
1051
1053
1054 template <typename ITy> bool match(ITy *V) const {
1055 const auto *BB = dyn_cast<BasicBlock>(V);
1056 return BB && BB == Val;
1057 }
1058};
1059
1060/// Match a specific basic block value.
1062 return specific_bbval(BB);
1063}
1064
1065/// A commutative-friendly version of m_Specific().
1067 return BB;
1068}
1070m_Deferred(const BasicBlock *const &BB) {
1071 return BB;
1072}
1073
1074//===----------------------------------------------------------------------===//
1075// Matcher for any binary operator.
1076//
1077template <typename LHS_t, typename RHS_t, bool Commutable = false>
1081
1082 // The evaluation order is always stable, regardless of Commutability.
1083 // The LHS is always matched first.
1084 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1085
1086 template <typename OpTy> bool match(OpTy *V) const {
1087 if (auto *I = dyn_cast<BinaryOperator>(V))
1088 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1089 (Commutable && L.match(I->getOperand(1)) &&
1090 R.match(I->getOperand(0)));
1091 return false;
1092 }
1093};
1094
1095template <typename LHS, typename RHS>
1096inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1097 return AnyBinaryOp_match<LHS, RHS>(L, R);
1098}
1099
1100//===----------------------------------------------------------------------===//
1101// Matcher for any unary operator.
1102// TODO fuse unary, binary matcher into n-ary matcher
1103//
1104template <typename OP_t> struct AnyUnaryOp_match {
1105 OP_t X;
1106
1107 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1108
1109 template <typename OpTy> bool match(OpTy *V) const {
1110 if (auto *I = dyn_cast<UnaryOperator>(V))
1111 return X.match(I->getOperand(0));
1112 return false;
1113 }
1114};
1115
1116template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1117 return AnyUnaryOp_match<OP_t>(X);
1118}
1119
1120//===----------------------------------------------------------------------===//
1121// Matchers for specific binary operators.
1122//
1123
1124template <typename LHS_t, typename RHS_t, unsigned Opcode,
1125 bool Commutable = false>
1129
1130 // The evaluation order is always stable, regardless of Commutability.
1131 // The LHS is always matched first.
1132 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1133
1134 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1135 if (V->getValueID() == Value::InstructionVal + Opc) {
1136 auto *I = cast<BinaryOperator>(V);
1137 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1138 (Commutable && L.match(I->getOperand(1)) &&
1139 R.match(I->getOperand(0)));
1140 }
1141 return false;
1142 }
1143
1144 template <typename OpTy> bool match(OpTy *V) const {
1145 return match(Opcode, V);
1146 }
1147};
1148
1149template <typename LHS, typename RHS>
1151 const RHS &R) {
1153}
1154
1155template <typename LHS, typename RHS>
1157 const RHS &R) {
1159}
1160
1161template <typename LHS, typename RHS>
1163 const RHS &R) {
1165}
1166
1167template <typename LHS, typename RHS>
1169 const RHS &R) {
1171}
1172
1173template <typename Op_t> struct FNeg_match {
1174 Op_t X;
1175
1176 FNeg_match(const Op_t &Op) : X(Op) {}
1177 template <typename OpTy> bool match(OpTy *V) const {
1178 auto *FPMO = dyn_cast<FPMathOperator>(V);
1179 if (!FPMO)
1180 return false;
1181
1182 if (FPMO->getOpcode() == Instruction::FNeg)
1183 return X.match(FPMO->getOperand(0));
1184
1185 if (FPMO->getOpcode() == Instruction::FSub) {
1186 if (FPMO->hasNoSignedZeros()) {
1187 // With 'nsz', any zero goes.
1188 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1189 return false;
1190 } else {
1191 // Without 'nsz', we need fsub -0.0, X exactly.
1192 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1193 return false;
1194 }
1195
1196 return X.match(FPMO->getOperand(1));
1197 }
1198
1199 return false;
1200 }
1201};
1202
1203/// Match 'fneg X' as 'fsub -0.0, X'.
1204template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1205 return FNeg_match<OpTy>(X);
1206}
1207
1208/// Match 'fneg X' as 'fsub +-0.0, X'.
1209template <typename RHS>
1210inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1211m_FNegNSZ(const RHS &X) {
1212 return m_FSub(m_AnyZeroFP(), X);
1213}
1214
1215template <typename LHS, typename RHS>
1217 const RHS &R) {
1219}
1220
1221template <typename LHS, typename RHS>
1223 const RHS &R) {
1225}
1226
1227template <typename LHS, typename RHS>
1229 const RHS &R) {
1231}
1232
1233template <typename LHS, typename RHS>
1235 const RHS &R) {
1237}
1238
1239template <typename LHS, typename RHS>
1241 const RHS &R) {
1243}
1244
1245template <typename LHS, typename RHS>
1247 const RHS &R) {
1249}
1250
1251template <typename LHS, typename RHS>
1253 const RHS &R) {
1255}
1256
1257template <typename LHS, typename RHS>
1259 const RHS &R) {
1261}
1262
1263template <typename LHS, typename RHS>
1265 const RHS &R) {
1267}
1268
1269template <typename LHS, typename RHS>
1271 const RHS &R) {
1273}
1274
1275template <typename LHS, typename RHS>
1277 const RHS &R) {
1279}
1280
1281template <typename LHS, typename RHS>
1283 const RHS &R) {
1285}
1286
1287template <typename LHS, typename RHS>
1289 const RHS &R) {
1291}
1292
1293template <typename LHS, typename RHS>
1295 const RHS &R) {
1297}
1298
1299template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1302
1303 ShiftLike_match(const LHS_t &LHS, uint64_t &RHS) : L(LHS), R(RHS) {}
1304
1305 template <typename OpTy> bool match(OpTy *V) const {
1306 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1307 if (Op->getOpcode() == Opcode)
1308 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1309 L.match(Op->getOperand(0));
1310 }
1311 // Interpreted as shiftop V, 0
1312 R = 0;
1313 return L.match(V);
1314 }
1315};
1316
1317/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1318template <typename LHS>
1323
1324/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1325template <typename LHS>
1330
1331/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1332template <typename LHS>
1337
1338template <typename LHS_t, typename RHS_t, unsigned Opcode,
1339 unsigned WrapFlags = 0, bool Commutable = false>
1343
1344 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1345 : L(LHS), R(RHS) {}
1346
1347 template <typename OpTy> bool match(OpTy *V) const {
1348 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1349 if (Op->getOpcode() != Opcode)
1350 return false;
1352 !Op->hasNoUnsignedWrap())
1353 return false;
1354 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1355 !Op->hasNoSignedWrap())
1356 return false;
1357 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1358 (Commutable && L.match(Op->getOperand(1)) &&
1359 R.match(Op->getOperand(0)));
1360 }
1361 return false;
1362 }
1363};
1364
1365template <typename LHS, typename RHS>
1366inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1368m_NSWAdd(const LHS &L, const RHS &R) {
1369 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1371 R);
1372}
1373template <typename LHS, typename RHS>
1374inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1376m_c_NSWAdd(const LHS &L, const RHS &R) {
1377 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1379 true>(L, R);
1380}
1381template <typename LHS, typename RHS>
1382inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1384m_NSWSub(const LHS &L, const RHS &R) {
1385 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1387 R);
1388}
1389template <typename LHS, typename RHS>
1390inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1392m_NSWMul(const LHS &L, const RHS &R) {
1393 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1395 R);
1396}
1397template <typename LHS, typename RHS>
1398inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1400m_NSWShl(const LHS &L, const RHS &R) {
1401 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1403 R);
1404}
1405
1406template <typename LHS, typename RHS>
1407inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1409m_NUWAdd(const LHS &L, const RHS &R) {
1410 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1412 L, R);
1413}
1414
1415template <typename LHS, typename RHS>
1417 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1418m_c_NUWAdd(const LHS &L, const RHS &R) {
1419 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1421 true>(L, R);
1422}
1423
1424template <typename LHS, typename RHS>
1425inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1427m_NUWSub(const LHS &L, const RHS &R) {
1428 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1430 L, R);
1431}
1432template <typename LHS, typename RHS>
1433inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1435m_NUWMul(const LHS &L, const RHS &R) {
1436 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1438 L, R);
1439}
1440template <typename LHS, typename RHS>
1441inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1443m_NUWShl(const LHS &L, const RHS &R) {
1444 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1446 L, R);
1447}
1448
1449template <typename LHS_t, typename RHS_t, bool Commutable = false>
1451 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1452 unsigned Opcode;
1453
1454 SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
1455 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1456
1457 template <typename OpTy> bool match(OpTy *V) const {
1459 }
1460};
1461
1462/// Matches a specific opcode.
1463template <typename LHS, typename RHS>
1464inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1465 const RHS &R) {
1466 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1467}
1468
1469template <typename LHS, typename RHS, bool Commutable = false>
1471 LHS L;
1472 RHS R;
1473
1474 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1475
1476 template <typename OpTy> bool match(OpTy *V) const {
1477 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1478 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1479 if (!PDI->isDisjoint())
1480 return false;
1481 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1482 (Commutable && L.match(PDI->getOperand(1)) &&
1483 R.match(PDI->getOperand(0)));
1484 }
1485 return false;
1486 }
1487};
1488
1489template <typename LHS, typename RHS>
1490inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
1491 return DisjointOr_match<LHS, RHS>(L, R);
1492}
1493
1494template <typename LHS, typename RHS>
1496 const RHS &R) {
1498}
1499
1500/// Match either "add" or "or disjoint".
1501template <typename LHS, typename RHS>
1504m_AddLike(const LHS &L, const RHS &R) {
1505 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1506}
1507
1508/// Match either "add nsw" or "or disjoint"
1509template <typename LHS, typename RHS>
1510inline match_combine_or<
1511 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1514m_NSWAddLike(const LHS &L, const RHS &R) {
1515 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1516}
1517
1518/// Match either "add nuw" or "or disjoint"
1519template <typename LHS, typename RHS>
1520inline match_combine_or<
1521 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1524m_NUWAddLike(const LHS &L, const RHS &R) {
1525 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1526}
1527
1528template <typename LHS, typename RHS>
1530 LHS L;
1531 RHS R;
1532
1533 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1534
1535 template <typename OpTy> bool match(OpTy *V) const {
1536 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1537 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1538 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1539 ; // Pass
1540 else if (Op->getOpcode() != Instruction::Xor)
1541 return false;
1542 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1543 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1544 }
1545 return false;
1546 }
1547};
1548
1549/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1550/// Only commutative matcher as the `sub` will need to swap the L and R.
1551template <typename LHS, typename RHS>
1552inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1553 return XorLike_match<LHS, RHS>(L, R);
1554}
1555
1556//===----------------------------------------------------------------------===//
1557// Class that matches a group of binary opcodes.
1558//
1559template <typename LHS_t, typename RHS_t, typename Predicate,
1560 bool Commutable = false>
1561struct BinOpPred_match : Predicate {
1564
1565 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1566
1567 template <typename OpTy> bool match(OpTy *V) const {
1568 if (auto *I = dyn_cast<Instruction>(V))
1569 return this->isOpType(I->getOpcode()) &&
1570 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1571 (Commutable && L.match(I->getOperand(1)) &&
1572 R.match(I->getOperand(0))));
1573 return false;
1574 }
1575};
1576
1578 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1579};
1580
1582 bool isOpType(unsigned Opcode) const {
1583 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1584 }
1585};
1586
1588 bool isOpType(unsigned Opcode) const {
1589 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1590 }
1591};
1592
1594 bool isOpType(unsigned Opcode) const {
1595 return Instruction::isBitwiseLogicOp(Opcode);
1596 }
1597};
1598
1600 bool isOpType(unsigned Opcode) const {
1601 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1602 }
1603};
1604
1606 bool isOpType(unsigned Opcode) const {
1607 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1608 }
1609};
1610
1611/// Matches shift operations.
1612template <typename LHS, typename RHS>
1614 const RHS &R) {
1616}
1617
1618/// Matches logical shift operations.
1619template <typename LHS, typename RHS>
1621 const RHS &R) {
1623}
1624
1625/// Matches logical shift operations.
1626template <typename LHS, typename RHS>
1628m_LogicalShift(const LHS &L, const RHS &R) {
1630}
1631
1632/// Matches bitwise logic operations.
1633template <typename LHS, typename RHS>
1635m_BitwiseLogic(const LHS &L, const RHS &R) {
1637}
1638
1639/// Matches bitwise logic operations in either order.
1640template <typename LHS, typename RHS>
1642m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1644}
1645
1646/// Matches integer division operations.
1647template <typename LHS, typename RHS>
1649 const RHS &R) {
1651}
1652
1653/// Matches integer remainder operations.
1654template <typename LHS, typename RHS>
1656 const RHS &R) {
1658}
1659
1660//===----------------------------------------------------------------------===//
1661// Class that matches exact binary ops.
1662//
1663template <typename SubPattern_t> struct Exact_match {
1664 SubPattern_t SubPattern;
1665
1666 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1667
1668 template <typename OpTy> bool match(OpTy *V) const {
1669 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1670 return PEO->isExact() && SubPattern.match(V);
1671 return false;
1672 }
1673};
1674
1675template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1676 return SubPattern;
1677}
1678
1679//===----------------------------------------------------------------------===//
1680// Matchers for CmpInst classes
1681//
1682
1683template <typename LHS_t, typename RHS_t, typename Class,
1684 bool Commutable = false>
1689
1690 // The evaluation order is always stable, regardless of Commutability.
1691 // The LHS is always matched first.
1692 CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
1693 : Predicate(&Pred), L(LHS), R(RHS) {}
1694 CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
1695 : Predicate(nullptr), L(LHS), R(RHS) {}
1696
1697 template <typename OpTy> bool match(OpTy *V) const {
1698 if (auto *I = dyn_cast<Class>(V)) {
1699 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1700 if (Predicate)
1702 return true;
1703 }
1704 if (Commutable && L.match(I->getOperand(1)) &&
1705 R.match(I->getOperand(0))) {
1706 if (Predicate)
1708 return true;
1709 }
1710 }
1711 return false;
1712 }
1713};
1714
1715template <typename LHS, typename RHS>
1717 const RHS &R) {
1718 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1719}
1720
1721template <typename LHS, typename RHS>
1723 const LHS &L, const RHS &R) {
1724 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1725}
1726
1727template <typename LHS, typename RHS>
1729 const LHS &L, const RHS &R) {
1730 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1731}
1732
1733template <typename LHS, typename RHS>
1734inline CmpClass_match<LHS, RHS, CmpInst> m_Cmp(const LHS &L, const RHS &R) {
1736}
1737
1738template <typename LHS, typename RHS>
1739inline CmpClass_match<LHS, RHS, ICmpInst> m_ICmp(const LHS &L, const RHS &R) {
1741}
1742
1743template <typename LHS, typename RHS>
1744inline CmpClass_match<LHS, RHS, FCmpInst> m_FCmp(const LHS &L, const RHS &R) {
1746}
1747
1748// Same as CmpClass, but instead of saving Pred as out output variable, match a
1749// specific input pred for equality.
1750template <typename LHS_t, typename RHS_t, typename Class,
1751 bool Commutable = false>
1756
1757 SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
1758 : Predicate(Pred), L(LHS), R(RHS) {}
1759
1760 template <typename OpTy> bool match(OpTy *V) const {
1761 if (auto *I = dyn_cast<Class>(V)) {
1763 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1764 return true;
1765 if constexpr (Commutable) {
1768 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1769 return true;
1770 }
1771 }
1772
1773 return false;
1774 }
1775};
1776
1777template <typename LHS, typename RHS>
1779m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1780 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1781}
1782
1783template <typename LHS, typename RHS>
1785m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1786 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1787}
1788
1789template <typename LHS, typename RHS>
1791m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1793}
1794
1795template <typename LHS, typename RHS>
1797m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1798 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1799}
1800
1801//===----------------------------------------------------------------------===//
1802// Matchers for instructions with a given opcode and number of operands.
1803//
1804
1805/// Matches instructions with Opcode and three operands.
1806template <typename T0, unsigned Opcode> struct OneOps_match {
1808
1809 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1810
1811 template <typename OpTy> bool match(OpTy *V) const {
1812 if (V->getValueID() == Value::InstructionVal + Opcode) {
1813 auto *I = cast<Instruction>(V);
1814 return Op1.match(I->getOperand(0));
1815 }
1816 return false;
1817 }
1818};
1819
1820/// Matches instructions with Opcode and three operands.
1821template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1824
1825 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1826
1827 template <typename OpTy> bool match(OpTy *V) const {
1828 if (V->getValueID() == Value::InstructionVal + Opcode) {
1829 auto *I = cast<Instruction>(V);
1830 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1831 }
1832 return false;
1833 }
1834};
1835
1836/// Matches instructions with Opcode and three operands.
1837template <typename T0, typename T1, typename T2, unsigned Opcode,
1838 bool CommutableOp2Op3 = false>
1843
1844 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1845 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1846
1847 template <typename OpTy> bool match(OpTy *V) const {
1848 if (V->getValueID() == Value::InstructionVal + Opcode) {
1849 auto *I = cast<Instruction>(V);
1850 if (!Op1.match(I->getOperand(0)))
1851 return false;
1852 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1853 return true;
1854 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1855 Op3.match(I->getOperand(1));
1856 }
1857 return false;
1858 }
1859};
1860
1861/// Matches instructions with Opcode and any number of operands
1862template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1863 std::tuple<OperandTypes...> Operands;
1864
1865 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1866
1867 // Operand matching works by recursively calling match_operands, matching the
1868 // operands left to right. The first version is called for each operand but
1869 // the last, for which the second version is called. The second version of
1870 // match_operands is also used to match each individual operand.
1871 template <int Idx, int Last>
1872 std::enable_if_t<Idx != Last, bool>
1876
1877 template <int Idx, int Last>
1878 std::enable_if_t<Idx == Last, bool>
1880 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1881 }
1882
1883 template <typename OpTy> bool match(OpTy *V) const {
1884 if (V->getValueID() == Value::InstructionVal + Opcode) {
1885 auto *I = cast<Instruction>(V);
1886 return I->getNumOperands() == sizeof...(OperandTypes) &&
1887 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1888 }
1889 return false;
1890 }
1891};
1892
1893/// Matches SelectInst.
1894template <typename Cond, typename LHS, typename RHS>
1896m_Select(const Cond &C, const LHS &L, const RHS &R) {
1898}
1899
1900/// This matches a select of two constants, e.g.:
1901/// m_SelectCst<-1, 0>(m_Value(V))
1902template <int64_t L, int64_t R, typename Cond>
1904 Instruction::Select>
1907}
1908
1909/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1910template <typename LHS, typename RHS>
1911inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1912m_c_Select(const LHS &L, const RHS &R) {
1913 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1914 true>(m_Value(), L, R);
1915}
1916
1917/// Matches FreezeInst.
1918template <typename OpTy>
1922
1923/// Matches InsertElementInst.
1924template <typename Val_t, typename Elt_t, typename Idx_t>
1926m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1928 Val, Elt, Idx);
1929}
1930
1931/// Matches ExtractElementInst.
1932template <typename Val_t, typename Idx_t>
1934m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1936}
1937
1938/// Matches shuffle.
1939template <typename T0, typename T1, typename T2> struct Shuffle_match {
1943
1944 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1945 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1946
1947 template <typename OpTy> bool match(OpTy *V) const {
1948 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1949 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1950 Mask.match(I->getShuffleMask());
1951 }
1952 return false;
1953 }
1954};
1955
1956struct m_Mask {
1959 bool match(ArrayRef<int> Mask) const {
1960 MaskRef = Mask;
1961 return true;
1962 }
1963};
1964
1966 bool match(ArrayRef<int> Mask) const {
1967 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1968 }
1969};
1970
1974 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
1975};
1976
1980 bool match(ArrayRef<int> Mask) const {
1981 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
1982 if (First == Mask.end())
1983 return false;
1984 SplatIndex = *First;
1985 return all_of(Mask,
1986 [First](int Elem) { return Elem == *First || Elem == -1; });
1987 }
1988};
1989
1990template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1991 PointerOpTy PointerOp;
1992 OffsetOpTy OffsetOp;
1993
1994 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1996
1997 template <typename OpTy> bool match(OpTy *V) const {
1998 auto *GEP = dyn_cast<GEPOperator>(V);
1999 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2000 PointerOp.match(GEP->getPointerOperand()) &&
2001 OffsetOp.match(GEP->idx_begin()->get());
2002 }
2003};
2004
2005/// Matches ShuffleVectorInst independently of mask value.
2006template <typename V1_t, typename V2_t>
2008m_Shuffle(const V1_t &v1, const V2_t &v2) {
2010}
2011
2012template <typename V1_t, typename V2_t, typename Mask_t>
2014m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2016}
2017
2018/// Matches LoadInst.
2019template <typename OpTy>
2023
2024/// Matches StoreInst.
2025template <typename ValueOpTy, typename PointerOpTy>
2027m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2029 PointerOp);
2030}
2031
2032/// Matches GetElementPtrInst.
2033template <typename... OperandTypes>
2034inline auto m_GEP(const OperandTypes &...Ops) {
2035 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2036}
2037
2038/// Matches GEP with i8 source element type
2039template <typename PointerOpTy, typename OffsetOpTy>
2041m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2043}
2044
2045//===----------------------------------------------------------------------===//
2046// Matchers for CastInst classes
2047//
2048
2049template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2050 Op_t Op;
2051
2052 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2053
2054 template <typename OpTy> bool match(OpTy *V) const {
2055 if (auto *O = dyn_cast<Operator>(V))
2056 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2057 return false;
2058 }
2059};
2060
2061template <typename Op_t, typename Class> struct CastInst_match {
2062 Op_t Op;
2063
2064 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2065
2066 template <typename OpTy> bool match(OpTy *V) const {
2067 if (auto *I = dyn_cast<Class>(V))
2068 return Op.match(I->getOperand(0));
2069 return false;
2070 }
2071};
2072
2073template <typename Op_t> struct PtrToIntSameSize_match {
2075 Op_t Op;
2076
2077 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2078 : DL(DL), Op(OpMatch) {}
2079
2080 template <typename OpTy> bool match(OpTy *V) const {
2081 if (auto *O = dyn_cast<Operator>(V))
2082 return O->getOpcode() == Instruction::PtrToInt &&
2083 DL.getTypeSizeInBits(O->getType()) ==
2084 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2085 Op.match(O->getOperand(0));
2086 return false;
2087 }
2088};
2089
2090template <typename Op_t> struct NNegZExt_match {
2091 Op_t Op;
2092
2093 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2094
2095 template <typename OpTy> bool match(OpTy *V) const {
2096 if (auto *I = dyn_cast<ZExtInst>(V))
2097 return I->hasNonNeg() && Op.match(I->getOperand(0));
2098 return false;
2099 }
2100};
2101
2102template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2103 Op_t Op;
2104
2105 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2106
2107 template <typename OpTy> bool match(OpTy *V) const {
2108 if (auto *I = dyn_cast<TruncInst>(V))
2109 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2110 Op.match(I->getOperand(0));
2111 return false;
2112 }
2113};
2114
2115/// Matches BitCast.
2116template <typename OpTy>
2121
2122template <typename Op_t> struct ElementWiseBitCast_match {
2123 Op_t Op;
2124
2125 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2126
2127 template <typename OpTy> bool match(OpTy *V) const {
2128 auto *I = dyn_cast<BitCastInst>(V);
2129 if (!I)
2130 return false;
2131 Type *SrcType = I->getSrcTy();
2132 Type *DstType = I->getType();
2133 // Make sure the bitcast doesn't change between scalar and vector and
2134 // doesn't change the number of vector elements.
2135 if (SrcType->isVectorTy() != DstType->isVectorTy())
2136 return false;
2137 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2138 SrcVecTy && SrcVecTy->getElementCount() !=
2139 cast<VectorType>(DstType)->getElementCount())
2140 return false;
2141 return Op.match(I->getOperand(0));
2142 }
2143};
2144
2145template <typename OpTy>
2149
2150/// Matches PtrToInt.
2151template <typename OpTy>
2156
2157template <typename OpTy>
2162
2163/// Matches PtrToAddr.
2164template <typename OpTy>
2169
2170/// Matches PtrToInt or PtrToAddr.
2171template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2173}
2174
2175/// Matches IntToPtr.
2176template <typename OpTy>
2181
2182/// Matches any cast or self. Used to ignore casts.
2183template <typename OpTy>
2185m_CastOrSelf(const OpTy &Op) {
2187}
2188
2189/// Matches Trunc.
2190template <typename OpTy>
2194
2195/// Matches trunc nuw.
2196template <typename OpTy>
2201
2202/// Matches trunc nsw.
2203template <typename OpTy>
2208
2209template <typename OpTy>
2211m_TruncOrSelf(const OpTy &Op) {
2212 return m_CombineOr(m_Trunc(Op), Op);
2213}
2214
2215/// Matches SExt.
2216template <typename OpTy>
2220
2221/// Matches ZExt.
2222template <typename OpTy>
2226
2227template <typename OpTy>
2229 return NNegZExt_match<OpTy>(Op);
2230}
2231
2232template <typename OpTy>
2234m_ZExtOrSelf(const OpTy &Op) {
2235 return m_CombineOr(m_ZExt(Op), Op);
2236}
2237
2238template <typename OpTy>
2240m_SExtOrSelf(const OpTy &Op) {
2241 return m_CombineOr(m_SExt(Op), Op);
2242}
2243
2244/// Match either "sext" or "zext nneg".
2245template <typename OpTy>
2247m_SExtLike(const OpTy &Op) {
2248 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2249}
2250
2251template <typename OpTy>
2254m_ZExtOrSExt(const OpTy &Op) {
2255 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2256}
2257
2258template <typename OpTy>
2261 OpTy>
2263 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2264}
2265
2266template <typename OpTy> inline auto m_ZExtOrTruncOrSelf(const OpTy &Op) {
2267 return m_CombineOr(m_ZExt(Op), m_Trunc(Op), Op);
2268}
2269
2270template <typename LHS_t, typename RHS_t> struct ICmpLike_match {
2274
2276 : Pred(P), L(Left), R(Right) {}
2277
2278 template <typename OpTy> bool match(OpTy *V) const {
2279 if (PatternMatch::match(V, m_ICmp(Pred, L, R)))
2280 return true;
2281 Value *A;
2282 // trunc nuw x to i1 is equivalent to icmp ne x, 0
2283 if (V->getType()->isIntOrIntVectorTy(1) &&
2284 PatternMatch::match(V, m_NUWTrunc(m_Value(A))) && L.match(A) &&
2285 R.match(ConstantInt::getNullValue(A->getType()))) {
2287 return true;
2288 }
2289 return false;
2290 }
2291};
2292
2293template <typename LHS, typename RHS>
2295 const RHS &R) {
2296 return ICmpLike_match<LHS, RHS>(Pred, L, R);
2297}
2298
2299template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2300 CondTy Cond;
2303
2304 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2305 : Cond(C), TrueC(TC), FalseC(FC) {}
2306
2307 template <typename OpTy> bool match(OpTy *V) const {
2308 // select(Cond, TrueC, FalseC) — captures both constants directly
2310 return true;
2311
2312 Type *Ty = V->getType();
2313 Value *CondV = nullptr;
2314
2315 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2316 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2317 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2318 TrueC.match(ConstantInt::get(Ty, 1)) &&
2319 FalseC.match(ConstantInt::get(Ty, 0)))
2320 return true;
2321
2322 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2323 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2324 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2325 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2326 FalseC.match(ConstantInt::get(Ty, 0)))
2327 return true;
2328
2329 return false;
2330 }
2331};
2332
2333/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2334/// select i1 Cond, TrueC, FalseC
2335/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2336/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2337///
2338/// The condition is matched against \p Cond, and the true/false constants
2339/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2340/// constants are bound to \p TrueC and \p FalseC via their matchers.
2341template <typename CondTy, typename LTy, typename RTy>
2343m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2344 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2345}
2346
2347template <typename OpTy>
2351
2352template <typename OpTy>
2356
2357template <typename OpTy>
2360m_IToFP(const OpTy &Op) {
2361 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2362}
2363
2364template <typename OpTy>
2368
2369template <typename OpTy>
2373
2374template <typename OpTy>
2377m_FPToI(const OpTy &Op) {
2378 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2379}
2380
2381template <typename OpTy>
2385
2386template <typename OpTy>
2390
2391//===----------------------------------------------------------------------===//
2392// Matchers for control flow.
2393//
2394
2395struct br_match {
2397
2399
2400 template <typename OpTy> bool match(OpTy *V) const {
2401 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2402 Succ = BI->getSuccessor();
2403 return true;
2404 }
2405 return false;
2406 }
2407};
2408
2409inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2410
2411template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2413 Cond_t Cond;
2414 TrueBlock_t T;
2415 FalseBlock_t F;
2416
2417 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2418 : Cond(C), T(t), F(f) {}
2419
2420 template <typename OpTy> bool match(OpTy *V) const {
2421 if (auto *BI = dyn_cast<CondBrInst>(V))
2422 if (Cond.match(BI->getCondition()))
2423 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2424 return false;
2425 }
2426};
2427
2428template <typename Cond_t>
2434
2435template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2437m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2439}
2440
2441//===----------------------------------------------------------------------===//
2442// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2443//
2444
2445template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2446 bool Commutable = false>
2448 using PredType = Pred_t;
2451
2452 // The evaluation order is always stable, regardless of Commutability.
2453 // The LHS is always matched first.
2454 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2455
2456 template <typename OpTy> bool match(OpTy *V) const {
2457 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2458 Intrinsic::ID IID = II->getIntrinsicID();
2459 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2460 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2461 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2462 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2463 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2464 return (L.match(LHS) && R.match(RHS)) ||
2465 (Commutable && L.match(RHS) && R.match(LHS));
2466 }
2467 }
2468 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2469 auto *SI = dyn_cast<SelectInst>(V);
2470 if (!SI)
2471 return false;
2472 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2473 if (!Cmp)
2474 return false;
2475 // At this point we have a select conditioned on a comparison. Check that
2476 // it is the values returned by the select that are being compared.
2477 auto *TrueVal = SI->getTrueValue();
2478 auto *FalseVal = SI->getFalseValue();
2479 auto *LHS = Cmp->getOperand(0);
2480 auto *RHS = Cmp->getOperand(1);
2481 if ((TrueVal != LHS || FalseVal != RHS) &&
2482 (TrueVal != RHS || FalseVal != LHS))
2483 return false;
2484 typename CmpInst_t::Predicate Pred =
2485 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2486 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2487 if (!Pred_t::match(Pred))
2488 return false;
2489 // It does! Bind the operands.
2490 return (L.match(LHS) && R.match(RHS)) ||
2491 (Commutable && L.match(RHS) && R.match(LHS));
2492 }
2493};
2494
2495/// Helper class for identifying signed max predicates.
2497 static bool match(ICmpInst::Predicate Pred) {
2498 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2499 }
2500};
2501
2502/// Helper class for identifying signed min predicates.
2504 static bool match(ICmpInst::Predicate Pred) {
2505 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2506 }
2507};
2508
2509/// Helper class for identifying unsigned max predicates.
2511 static bool match(ICmpInst::Predicate Pred) {
2512 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2513 }
2514};
2515
2516/// Helper class for identifying unsigned min predicates.
2518 static bool match(ICmpInst::Predicate Pred) {
2519 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2520 }
2521};
2522
2523/// Helper class for identifying ordered max predicates.
2525 static bool match(FCmpInst::Predicate Pred) {
2526 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2527 }
2528};
2529
2530/// Helper class for identifying ordered min predicates.
2532 static bool match(FCmpInst::Predicate Pred) {
2533 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2534 }
2535};
2536
2537/// Helper class for identifying unordered max predicates.
2539 static bool match(FCmpInst::Predicate Pred) {
2540 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2541 }
2542};
2543
2544/// Helper class for identifying unordered min predicates.
2546 static bool match(FCmpInst::Predicate Pred) {
2547 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2548 }
2549};
2550
2551template <typename LHS, typename RHS>
2553 const RHS &R) {
2555}
2556
2557template <typename LHS, typename RHS>
2559 const RHS &R) {
2561}
2562
2563template <typename LHS, typename RHS>
2565 const RHS &R) {
2567}
2568
2569template <typename LHS, typename RHS>
2571 const RHS &R) {
2573}
2574
2575template <typename LHS, typename RHS>
2576inline auto m_MaxOrMin(const LHS &L, const RHS &R) {
2577 return m_CombineOr(m_SMax(L, R), m_SMin(L, R), m_UMax(L, R), m_UMin(L, R));
2578}
2579
2580/// Match an 'ordered' floating point maximum function.
2581/// Floating point has one special value 'NaN'. Therefore, there is no total
2582/// order. However, if we can ignore the 'NaN' value (for example, because of a
2583/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2584/// semantics. In the presence of 'NaN' we have to preserve the original
2585/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2586///
2587/// max(L, R) iff L and R are not NaN
2588/// m_OrdFMax(L, R) = R iff L or R are NaN
2589template <typename LHS, typename RHS>
2594
2595/// Match an 'ordered' floating point minimum function.
2596/// Floating point has one special value 'NaN'. Therefore, there is no total
2597/// order. However, if we can ignore the 'NaN' value (for example, because of a
2598/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2599/// semantics. In the presence of 'NaN' we have to preserve the original
2600/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2601///
2602/// min(L, R) iff L and R are not NaN
2603/// m_OrdFMin(L, R) = R iff L or R are NaN
2604template <typename LHS, typename RHS>
2609
2610/// Match an 'unordered' floating point maximum function.
2611/// Floating point has one special value 'NaN'. Therefore, there is no total
2612/// order. However, if we can ignore the 'NaN' value (for example, because of a
2613/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2614/// semantics. In the presence of 'NaN' we have to preserve the original
2615/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2616///
2617/// max(L, R) iff L and R are not NaN
2618/// m_UnordFMax(L, R) = L iff L or R are NaN
2619template <typename LHS, typename RHS>
2621m_UnordFMax(const LHS &L, const RHS &R) {
2623}
2624
2625/// Match an 'unordered' floating point minimum function.
2626/// Floating point has one special value 'NaN'. Therefore, there is no total
2627/// order. However, if we can ignore the 'NaN' value (for example, because of a
2628/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2629/// semantics. In the presence of 'NaN' we have to preserve the original
2630/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2631///
2632/// min(L, R) iff L and R are not NaN
2633/// m_UnordFMin(L, R) = L iff L or R are NaN
2634template <typename LHS, typename RHS>
2636m_UnordFMin(const LHS &L, const RHS &R) {
2638}
2639
2640/// Match an 'ordered' or 'unordered' floating point maximum function.
2641/// Floating point has one special value 'NaN'. Therefore, there is no total
2642/// order. However, if we can ignore the 'NaN' value (for example, because of a
2643/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2644/// semantics.
2645template <typename LHS, typename RHS>
2652
2653/// Match an 'ordered' or 'unordered' floating point minimum function.
2654/// Floating point has one special value 'NaN'. Therefore, there is no total
2655/// order. However, if we can ignore the 'NaN' value (for example, because of a
2656/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2657/// semantics.
2658template <typename LHS, typename RHS>
2665
2666/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2667/// NOTE: we first match the 'Not' (by matching '-1'),
2668/// and only then match the inner matcher!
2669template <typename ValTy>
2670inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2671m_Not(const ValTy &V) {
2672 return m_c_Xor(m_AllOnes(), V);
2673}
2674
2675template <typename ValTy>
2676inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2677 true>
2678m_NotForbidPoison(const ValTy &V) {
2679 return m_c_Xor(m_AllOnesForbidPoison(), V);
2680}
2681
2682//===----------------------------------------------------------------------===//
2683// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2684// Note that S might be matched to other instructions than AddInst.
2685//
2686
2687template <typename LHS_t, typename RHS_t, typename Sum_t>
2691 Sum_t S;
2692
2693 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2694 : L(L), R(R), S(S) {}
2695
2696 template <typename OpTy> bool match(OpTy *V) const {
2697 Value *ICmpLHS, *ICmpRHS;
2698 CmpPredicate Pred;
2699 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2700 return false;
2701
2702 Value *AddLHS, *AddRHS;
2703 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2704
2705 // (a + b) u< a, (a + b) u< b
2706 if (Pred == ICmpInst::ICMP_ULT)
2707 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2708 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2709
2710 // a >u (a + b), b >u (a + b)
2711 if (Pred == ICmpInst::ICMP_UGT)
2712 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2713 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2714
2715 Value *Op1;
2716 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2717 // (~a) <u b
2718 if (Pred == ICmpInst::ICMP_ULT) {
2719 if (XorExpr.match(ICmpLHS))
2720 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2721 }
2722 // b > u (~a)
2723 if (Pred == ICmpInst::ICMP_UGT) {
2724 if (XorExpr.match(ICmpRHS))
2725 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2726 }
2727
2728 // Match special-case for increment-by-1.
2729 if (Pred == ICmpInst::ICMP_EQ) {
2730 // (a + 1) == 0
2731 // (1 + a) == 0
2732 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2733 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2734 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2735 // 0 == (a + 1)
2736 // 0 == (1 + a)
2737 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2738 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2739 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2740 }
2741
2742 return false;
2743 }
2744};
2745
2746/// Match an icmp instruction checking for unsigned overflow on addition.
2747///
2748/// S is matched to the addition whose result is being checked for overflow, and
2749/// L and R are matched to the LHS and RHS of S.
2750template <typename LHS_t, typename RHS_t, typename Sum_t>
2752m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2754}
2755
2756template <typename Opnd_t> struct Argument_match {
2757 unsigned OpI;
2758 Opnd_t Val;
2759
2760 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2761
2762 template <typename OpTy> bool match(OpTy *V) const {
2763 // FIXME: Should likely be switched to use `CallBase`.
2764 if (const auto *CI = dyn_cast<CallInst>(V))
2765 return Val.match(CI->getArgOperand(OpI));
2766 return false;
2767 }
2768};
2769
2770/// Match an argument.
2771template <unsigned OpI, typename Opnd_t>
2772inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2773 return Argument_match<Opnd_t>(OpI, Op);
2774}
2775
2776/// Intrinsic matchers.
2778 unsigned ID;
2779
2781
2782 template <typename OpTy> bool match(OpTy *V) const {
2783 if (const auto *CI = dyn_cast<CallInst>(V))
2784 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2785 return F->getIntrinsicID() == ID;
2786 return false;
2787 }
2788};
2789
2790/// Match intrinsic calls with any of the given IDs.
2791template <Intrinsic::ID... IntrIDs> struct IntrinsicIDs_match {
2792 template <typename OpTy> bool match(OpTy *V) const {
2793 if (const auto *CI = dyn_cast<CallInst>(V))
2794 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand())) {
2795 Intrinsic::ID ID = F->getIntrinsicID();
2796 return ((ID == IntrIDs) || ...);
2797 }
2798 return false;
2799 }
2800};
2801
2802/// Intrinsic matches are combinations of ID matchers, and argument
2803/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2804/// them with lower arity matchers. Here's some convenient typedefs for up to
2805/// several arguments, and more can be added as needed
2806template <typename T0 = void, typename T1 = void, typename T2 = void,
2807 typename T3 = void, typename T4 = void, typename T5 = void,
2808 typename T6 = void, typename T7 = void, typename T8 = void,
2809 typename T9 = void, typename T10 = void>
2811template <typename T0> struct m_Intrinsic_Ty<T0> {
2813};
2814template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2815 using Ty =
2817};
2818template <typename T0, typename T1, typename T2>
2823template <typename T0, typename T1, typename T2, typename T3>
2828
2829template <typename T0, typename T1, typename T2, typename T3, typename T4>
2834
2835template <typename T0, typename T1, typename T2, typename T3, typename T4,
2836 typename T5>
2841
2842/// Match intrinsic calls like this:
2843/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2844template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2845 return IntrinsicID_match(IntrID);
2846}
2847
2848/// Match intrinsic calls with any of the given IDs like this:
2849/// m_AnyIntrinsic<Intrinsic::fptosi_sat, Intrinsic::fptoui_sat>()
2850/// This is more efficient than using nested m_CombineOr with m_Intrinsic
2851/// because it performs the CallInst/Function cast only once.
2852template <Intrinsic::ID... IntrIDs>
2854 return IntrinsicIDs_match<IntrIDs...>();
2855}
2856
2857/// Matches MaskedLoad Intrinsic.
2858template <typename Opnd0, typename Opnd1, typename Opnd2>
2860m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2861 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2862}
2863
2864/// Matches MaskedStore Intrinsic.
2865template <typename Opnd0, typename Opnd1, typename Opnd2>
2867m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2868 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2869}
2870
2871/// Matches MaskedGather Intrinsic.
2872template <typename Opnd0, typename Opnd1, typename Opnd2>
2874m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2875 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2876}
2877
2878template <Intrinsic::ID IntrID, typename T0>
2879inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2881}
2882
2883template <Intrinsic::ID IntrID, typename T0, typename T1>
2884inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2885 const T1 &Op1) {
2887}
2888
2889template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2890inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2891m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2892 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2893}
2894
2895template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2896 typename T3>
2898m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2899 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2900}
2901
2902template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2903 typename T3, typename T4>
2905m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2906 const T4 &Op4) {
2907 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2908 m_Argument<4>(Op4));
2909}
2910
2911template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2912 typename T3, typename T4, typename T5>
2914m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2915 const T4 &Op4, const T5 &Op5) {
2916 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2917 m_Argument<5>(Op5));
2918}
2919
2920// Helper intrinsic matching specializations.
2921template <typename Opnd0>
2922inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2924}
2925
2926template <typename Opnd0>
2927inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2929}
2930
2931template <typename Opnd0>
2932inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2933 return m_Intrinsic<Intrinsic::fabs>(Op0);
2934}
2935
2936template <typename Opnd0>
2937inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2939}
2940
2941template <typename Opnd0, typename Opnd1>
2942inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2943 const Opnd1 &Op1) {
2944 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2945}
2946
2947template <typename Opnd0, typename Opnd1>
2948inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2949 const Opnd1 &Op1) {
2950 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2951}
2952
2953template <typename Opnd0, typename Opnd1>
2955m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2956 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2957}
2958
2959template <typename Opnd0, typename Opnd1>
2960inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2961 const Opnd1 &Op1) {
2962 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2963}
2964
2965template <typename Opnd0, typename Opnd1>
2966inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2967 const Opnd1 &Op1) {
2968 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2969}
2970
2971template <typename Opnd0, typename Opnd1>
2973m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2974 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2975}
2976
2977template <typename Opnd0, typename Opnd1>
2980m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2981 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
2982}
2983
2984template <typename Opnd0, typename Opnd1>
2987m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2988 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
2989}
2990
2991template <typename Opnd0, typename Opnd1, typename Opnd2>
2993m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2994 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2995}
2996
2997template <typename Opnd0, typename Opnd1, typename Opnd2>
2999m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3000 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
3001}
3002
3003template <typename Opnd0>
3004inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3005 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3006}
3007
3008template <typename Opnd0, typename Opnd1>
3009inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3010 const Opnd1 &Op1) {
3011 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3012}
3013
3014template <typename Opnd0>
3015inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3017}
3018
3019template <typename Opnd0, typename Opnd1, typename Opnd2>
3021m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3022 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3023}
3024
3025//===----------------------------------------------------------------------===//
3026// Matchers for two-operands operators with the operators in either order
3027//
3028
3029/// Matches a BinaryOperator with LHS and RHS in either order.
3030template <typename LHS, typename RHS>
3031inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
3033}
3034
3035/// Matches an ICmp with a predicate over LHS and RHS in either order.
3036/// Swaps the predicate if operands are commuted.
3037template <typename LHS, typename RHS>
3039m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3041}
3042
3043template <typename LHS, typename RHS>
3045 const RHS &R) {
3047}
3048
3049/// Matches a specific opcode with LHS and RHS in either order.
3050template <typename LHS, typename RHS>
3052m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3053 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3054}
3055
3056/// Matches a Add with LHS and RHS in either order.
3057template <typename LHS, typename RHS>
3062
3063/// Matches a Mul with LHS and RHS in either order.
3064template <typename LHS, typename RHS>
3069
3070/// Matches an And with LHS and RHS in either order.
3071template <typename LHS, typename RHS>
3076
3077/// Matches an Or with LHS and RHS in either order.
3078template <typename LHS, typename RHS>
3080 const RHS &R) {
3082}
3083
3084/// Matches an Xor with LHS and RHS in either order.
3085template <typename LHS, typename RHS>
3090
3091/// Matches a 'Neg' as 'sub 0, V'.
3092template <typename ValTy>
3093inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3094m_Neg(const ValTy &V) {
3095 return m_Sub(m_ZeroInt(), V);
3096}
3097
3098/// Matches a 'Neg' as 'sub nsw 0, V'.
3099template <typename ValTy>
3101 Instruction::Sub,
3103m_NSWNeg(const ValTy &V) {
3104 return m_NSWSub(m_ZeroInt(), V);
3105}
3106
3107/// Matches an SMin with LHS and RHS in either order.
3108template <typename LHS, typename RHS>
3110m_c_SMin(const LHS &L, const RHS &R) {
3112}
3113/// Matches an SMax with LHS and RHS in either order.
3114template <typename LHS, typename RHS>
3116m_c_SMax(const LHS &L, const RHS &R) {
3118}
3119/// Matches a UMin with LHS and RHS in either order.
3120template <typename LHS, typename RHS>
3122m_c_UMin(const LHS &L, const RHS &R) {
3124}
3125/// Matches a UMax with LHS and RHS in either order.
3126template <typename LHS, typename RHS>
3128m_c_UMax(const LHS &L, const RHS &R) {
3130}
3131
3132template <typename LHS, typename RHS>
3133inline auto m_c_MaxOrMin(const LHS &L, const RHS &R) {
3134 return m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R), m_c_UMax(L, R),
3135 m_c_UMin(L, R));
3136}
3137
3138template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3140 LHS L;
3141 RHS R;
3142
3143 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3144
3145 template <typename OpTy> bool match(OpTy *V) const {
3146 const auto *II = dyn_cast<IntrinsicInst>(V);
3147 if (!II || II->getIntrinsicID() != IntrID)
3148 return false;
3149 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3150 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3151 }
3152};
3153
3154template <Intrinsic::ID IntrID, typename T0, typename T1>
3156m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3158}
3159
3160/// Matches FAdd with LHS and RHS in either order.
3161template <typename LHS, typename RHS>
3163m_c_FAdd(const LHS &L, const RHS &R) {
3165}
3166
3167/// Matches FMul with LHS and RHS in either order.
3168template <typename LHS, typename RHS>
3170m_c_FMul(const LHS &L, const RHS &R) {
3172}
3173
3174template <typename Opnd_t> struct Signum_match {
3175 Opnd_t Val;
3176 Signum_match(const Opnd_t &V) : Val(V) {}
3177
3178 template <typename OpTy> bool match(OpTy *V) const {
3179 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3180 if (TypeSize == 0)
3181 return false;
3182
3183 unsigned ShiftWidth = TypeSize - 1;
3184 Value *Op;
3185
3186 // This is the representation of signum we match:
3187 //
3188 // signum(x) == (x >> 63) | (-x >>u 63)
3189 //
3190 // An i1 value is its own signum, so it's correct to match
3191 //
3192 // signum(x) == (x >> 0) | (-x >>u 0)
3193 //
3194 // for i1 values.
3195
3196 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3197 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3198 auto Signum = m_c_Or(LHS, RHS);
3199
3200 return Signum.match(V) && Val.match(Op);
3201 }
3202};
3203
3204/// Matches a signum pattern.
3205///
3206/// signum(x) =
3207/// x > 0 -> 1
3208/// x == 0 -> 0
3209/// x < 0 -> -1
3210template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3211 return Signum_match<Val_t>(V);
3212}
3213
3214template <int Ind, typename Opnd_t> struct ExtractValue_match {
3215 Opnd_t Val;
3216 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3217
3218 template <typename OpTy> bool match(OpTy *V) const {
3219 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3220 // If Ind is -1, don't inspect indices
3221 if (Ind != -1 &&
3222 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3223 return false;
3224 return Val.match(I->getAggregateOperand());
3225 }
3226 return false;
3227 }
3228};
3229
3230/// Match a single index ExtractValue instruction.
3231/// For example m_ExtractValue<1>(...)
3232template <int Ind, typename Val_t>
3236
3237/// Match an ExtractValue instruction with any index.
3238/// For example m_ExtractValue(...)
3239template <typename Val_t>
3240inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3241 return ExtractValue_match<-1, Val_t>(V);
3242}
3243
3244/// Matcher for a single index InsertValue instruction.
3245template <int Ind, typename T0, typename T1> struct InsertValue_match {
3248
3249 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3250
3251 template <typename OpTy> bool match(OpTy *V) const {
3252 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3253 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3254 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3255 }
3256 return false;
3257 }
3258};
3259
3260/// Matches a single index InsertValue instruction.
3261template <int Ind, typename Val_t, typename Elt_t>
3263 const Elt_t &Elt) {
3264 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3265}
3266
3267/// Matches a call to `llvm.vscale()`.
3269
3270template <typename Opnd0, typename Opnd1>
3272m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3274}
3275
3276template <typename Opnd>
3280
3281template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3283 LHS L;
3284 RHS R;
3285
3286 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3287
3288 template <typename T> bool match(T *V) const {
3289 auto *I = dyn_cast<Instruction>(V);
3290 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3291 return false;
3292
3293 if (I->getOpcode() == Opcode) {
3294 auto *Op0 = I->getOperand(0);
3295 auto *Op1 = I->getOperand(1);
3296 return (L.match(Op0) && R.match(Op1)) ||
3297 (Commutable && L.match(Op1) && R.match(Op0));
3298 }
3299
3300 if (auto *Select = dyn_cast<SelectInst>(I)) {
3301 auto *Cond = Select->getCondition();
3302 auto *TVal = Select->getTrueValue();
3303 auto *FVal = Select->getFalseValue();
3304
3305 // Don't match a scalar select of bool vectors.
3306 // Transforms expect a single type for operands if this matches.
3307 if (Cond->getType() != Select->getType())
3308 return false;
3309
3310 if (Opcode == Instruction::And) {
3311 auto *C = dyn_cast<Constant>(FVal);
3312 if (C && C->isNullValue())
3313 return (L.match(Cond) && R.match(TVal)) ||
3314 (Commutable && L.match(TVal) && R.match(Cond));
3315 } else {
3316 assert(Opcode == Instruction::Or);
3317 auto *C = dyn_cast<Constant>(TVal);
3318 if (C && C->isOneValue())
3319 return (L.match(Cond) && R.match(FVal)) ||
3320 (Commutable && L.match(FVal) && R.match(Cond));
3321 }
3322 }
3323
3324 return false;
3325 }
3326};
3327
3328/// Matches L && R either in the form of L & R or L ? R : false.
3329/// Note that the latter form is poison-blocking.
3330template <typename LHS, typename RHS>
3332 const RHS &R) {
3334}
3335
3336/// Matches L && R where L and R are arbitrary values.
3337inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3338
3339/// Matches L && R with LHS and RHS in either order.
3340template <typename LHS, typename RHS>
3342m_c_LogicalAnd(const LHS &L, const RHS &R) {
3344}
3345
3346/// Matches L || R either in the form of L | R or L ? true : R.
3347/// Note that the latter form is poison-blocking.
3348template <typename LHS, typename RHS>
3350 const RHS &R) {
3352}
3353
3354/// Matches L || R where L and R are arbitrary values.
3355inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3356
3357/// Matches L || R with LHS and RHS in either order.
3358template <typename LHS, typename RHS>
3360m_c_LogicalOr(const LHS &L, const RHS &R) {
3362}
3363
3364/// Matches either L && R or L || R,
3365/// either one being in the either binary or logical form.
3366/// Note that the latter form is poison-blocking.
3367template <typename LHS, typename RHS, bool Commutable = false>
3373
3374/// Matches either L && R or L || R where L and R are arbitrary values.
3375inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3376
3377/// Matches either L && R or L || R with LHS and RHS in either order.
3378template <typename LHS, typename RHS>
3379inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3380 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3381}
3382
3383} // end namespace PatternMatch
3384} // end namespace llvm
3385
3386#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:851
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
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h:551
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1291
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:1606
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".
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.
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)
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:1739
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:1772
Matcher to bind the captured value.
Matcher for a specific value, but stores a reference to the value, not the value itself.
AllowFmf_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
ICmpLike_match(CmpPredicate &P, const LHS_t &Left, const RHS_t &Right)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
Match intrinsic calls with any of the given IDs.
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3, T4 >::Ty, Argument_match< T5 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3 >::Ty, Argument_match< T4 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1 >::Ty, Argument_match< T2 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0 >::Ty, Argument_match< T1 > > Ty
match_combine_and< IntrinsicID_match, Argument_match< T0 > > Ty
Intrinsic matches are combinations of ID matchers, and argument matchers.
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying signed max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying signed min predicates.
static bool match(ICmpInst::Predicate Pred)
Match a specified basic block value.
Match a specified floating point value or vector of all elements of that value.
Match a specified integer value or vector of all elements of that value.
Matcher for specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)