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
806template <typename Class> struct bind_ty {
807 Class *&VR;
808
809 bind_ty(Class *&V) : VR(V) {}
810
811 template <typename ITy> bool match(ITy *V) const {
812 if (auto *CV = dyn_cast<Class>(V)) {
813 VR = CV;
814 return true;
815 }
816 return false;
817 }
818};
819
820/// Check whether the value has the given Class and matches the nested
821/// pattern. Capture it into the provided variable if successful.
822template <typename Class, typename MatchTy> struct bind_and_match_ty {
823 Class *&VR;
824 MatchTy Match;
825
826 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
827
828 template <typename ITy> bool match(ITy *V) const {
829 auto *CV = dyn_cast<Class>(V);
830 if (CV && Match.match(V)) {
831 VR = CV;
832 return true;
833 }
834 return false;
835 }
836};
837
838/// Match a value, capturing it if we match.
839inline bind_ty<Value> m_Value(Value *&V) { return V; }
840inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
841
842/// Match against the nested pattern, and capture the value if we match.
843template <typename MatchTy>
845 const MatchTy &Match) {
846 return {V, Match};
847}
848
849/// Match against the nested pattern, and capture the value if we match.
850template <typename MatchTy>
852 const MatchTy &Match) {
853 return {V, Match};
854}
855
856/// Match an instruction, capturing it if we match.
859 return I;
860}
861
862/// Match against the nested pattern, and capture the instruction if we match.
863template <typename MatchTy>
865m_Instruction(Instruction *&I, const MatchTy &Match) {
866 return {I, Match};
867}
868template <typename MatchTy>
870m_Instruction(const Instruction *&I, const MatchTy &Match) {
871 return {I, Match};
872}
873
874/// Match a unary operator, capturing it if we match.
877 return I;
878}
879/// Match a binary operator, capturing it if we match.
882 return I;
883}
884/// Match any intrinsic call, capturing it if we match.
889/// Match a with overflow intrinsic, capturing it if we match.
895 return I;
896}
897
898/// Match an UndefValue, capturing the value if we match.
900
901/// Match a Constant, capturing the value if we match.
903
904/// Match a ConstantInt, capturing the value if we match.
906
907/// Match a ConstantFP, capturing the value if we match.
909
910/// Match a ConstantExpr, capturing the value if we match.
912
913/// Match a basic block value, capturing it if we match.
916 return V;
917}
918
919// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
920// and use m_Unless(m_ConstantExpr).
922 template <typename ITy> static bool isImmConstant(ITy *V) {
923 if (auto *CV = dyn_cast<Constant>(V)) {
924 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
925 return true;
926
927 if (CV->getType()->isVectorTy()) {
928 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
929 if (!isa<ConstantExpr>(Splat) &&
930 !Splat->containsConstantExpression()) {
931 return true;
932 }
933 }
934 }
935 }
936 return false;
937 }
938};
939
941 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
942};
943
944/// Match an arbitrary immediate Constant and ignore it.
946
949
951
952 template <typename ITy> bool match(ITy *V) const {
953 if (isImmConstant(V)) {
954 VR = cast<Constant>(V);
955 return true;
956 }
957 return false;
958 }
959};
960
961/// Match an immediate Constant, capturing the value if we match.
965
966/// Match a specified Value*.
968 const Value *Val;
969
970 specificval_ty(const Value *V) : Val(V) {}
971
972 template <typename ITy> bool match(ITy *V) const { return V == Val; }
973};
974
975/// Match if we have a specific specified value.
976inline specificval_ty m_Specific(const Value *V) { return V; }
977
978/// Stores a reference to the Value *, not the Value * itself,
979/// thus can be used in commutative matchers.
980template <typename Class> struct deferredval_ty {
981 Class *const &Val;
982
983 deferredval_ty(Class *const &V) : Val(V) {}
984
985 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
986};
987
988/// Like m_Specific(), but works if the specific value to match is determined
989/// as part of the same match() expression. For example:
990/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
991/// bind X before the pattern match starts.
992/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
993/// whichever value m_Value(X) populated.
994inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
996 return V;
997}
998
999/// Match a specified floating point value or vector of all elements of
1000/// that value.
1002 double Val;
1003
1004 specific_fpval(double V) : Val(V) {}
1005
1006 template <typename ITy> bool match(ITy *V) const {
1007 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1008 return CFP->isExactlyValue(Val);
1009 if (V->getType()->isVectorTy())
1010 if (const auto *C = dyn_cast<Constant>(V))
1011 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1012 return CFP->isExactlyValue(Val);
1013 return false;
1014 }
1015};
1016
1017/// Match a specific floating point value or vector with all elements
1018/// equal to the value.
1019inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1020
1021/// Match a float 1.0 or vector with all elements equal to 1.0.
1022inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1023
1026
1028
1029 template <typename ITy> bool match(ITy *V) const {
1030 const APInt *ConstInt;
1031 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1032 return false;
1033 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
1034 if (!ZExtVal)
1035 return false;
1036 VR = *ZExtVal;
1037 return true;
1038 }
1039};
1040
1041/// Match a specified integer value or vector of all elements of that
1042/// value.
1043template <bool AllowPoison> struct specific_intval {
1044 const APInt &Val;
1045
1046 specific_intval(const APInt &V) : Val(V) {}
1047
1048 template <typename ITy> bool match(ITy *V) const {
1049 const auto *CI = dyn_cast<ConstantInt>(V);
1050 if (!CI && V->getType()->isVectorTy())
1051 if (const auto *C = dyn_cast<Constant>(V))
1052 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1053
1054 return CI && APInt::isSameValue(CI->getValue(), Val);
1055 }
1056};
1057
1058template <bool AllowPoison> struct specific_intval64 {
1060
1062
1063 template <typename ITy> bool match(ITy *V) const {
1064 const auto *CI = dyn_cast<ConstantInt>(V);
1065 if (!CI && V->getType()->isVectorTy())
1066 if (const auto *C = dyn_cast<Constant>(V))
1067 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1068
1069 return CI && CI->getValue() == Val;
1070 }
1071};
1072
1073/// Match a specific integer value or vector with all elements equal to
1074/// the value.
1076 return specific_intval<false>(V);
1077}
1078
1082
1086
1090
1091/// Match a ConstantInt and bind to its value. This does not match
1092/// ConstantInts wider than 64-bits.
1094
1095/// Match a specified basic block value.
1098
1100
1101 template <typename ITy> bool match(ITy *V) const {
1102 const auto *BB = dyn_cast<BasicBlock>(V);
1103 return BB && BB == Val;
1104 }
1105};
1106
1107/// Match a specific basic block value.
1109 return specific_bbval(BB);
1110}
1111
1112/// A commutative-friendly version of m_Specific().
1114 return BB;
1115}
1117m_Deferred(const BasicBlock *const &BB) {
1118 return BB;
1119}
1120
1121//===----------------------------------------------------------------------===//
1122// Matcher for any binary operator.
1123//
1124template <typename LHS_t, typename RHS_t, bool Commutable = false>
1128
1129 // The evaluation order is always stable, regardless of Commutability.
1130 // The LHS is always matched first.
1131 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1132
1133 template <typename OpTy> bool match(OpTy *V) const {
1134 if (auto *I = dyn_cast<BinaryOperator>(V))
1135 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1136 (Commutable && L.match(I->getOperand(1)) &&
1137 R.match(I->getOperand(0)));
1138 return false;
1139 }
1140};
1141
1142template <typename LHS, typename RHS>
1143inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1144 return AnyBinaryOp_match<LHS, RHS>(L, R);
1145}
1146
1147//===----------------------------------------------------------------------===//
1148// Matcher for any unary operator.
1149// TODO fuse unary, binary matcher into n-ary matcher
1150//
1151template <typename OP_t> struct AnyUnaryOp_match {
1152 OP_t X;
1153
1154 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1155
1156 template <typename OpTy> bool match(OpTy *V) const {
1157 if (auto *I = dyn_cast<UnaryOperator>(V))
1158 return X.match(I->getOperand(0));
1159 return false;
1160 }
1161};
1162
1163template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1164 return AnyUnaryOp_match<OP_t>(X);
1165}
1166
1167//===----------------------------------------------------------------------===//
1168// Matchers for specific binary operators.
1169//
1170
1171template <typename LHS_t, typename RHS_t, unsigned Opcode,
1172 bool Commutable = false>
1176
1177 // The evaluation order is always stable, regardless of Commutability.
1178 // The LHS is always matched first.
1179 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1180
1181 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1182 if (V->getValueID() == Value::InstructionVal + Opc) {
1183 auto *I = cast<BinaryOperator>(V);
1184 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1185 (Commutable && L.match(I->getOperand(1)) &&
1186 R.match(I->getOperand(0)));
1187 }
1188 return false;
1189 }
1190
1191 template <typename OpTy> bool match(OpTy *V) const {
1192 return match(Opcode, V);
1193 }
1194};
1195
1196template <typename LHS, typename RHS>
1198 const RHS &R) {
1200}
1201
1202template <typename LHS, typename RHS>
1204 const RHS &R) {
1206}
1207
1208template <typename LHS, typename RHS>
1210 const RHS &R) {
1212}
1213
1214template <typename LHS, typename RHS>
1216 const RHS &R) {
1218}
1219
1220template <typename Op_t> struct FNeg_match {
1221 Op_t X;
1222
1223 FNeg_match(const Op_t &Op) : X(Op) {}
1224 template <typename OpTy> bool match(OpTy *V) const {
1225 auto *FPMO = dyn_cast<FPMathOperator>(V);
1226 if (!FPMO)
1227 return false;
1228
1229 if (FPMO->getOpcode() == Instruction::FNeg)
1230 return X.match(FPMO->getOperand(0));
1231
1232 if (FPMO->getOpcode() == Instruction::FSub) {
1233 if (FPMO->hasNoSignedZeros()) {
1234 // With 'nsz', any zero goes.
1235 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1236 return false;
1237 } else {
1238 // Without 'nsz', we need fsub -0.0, X exactly.
1239 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1240 return false;
1241 }
1242
1243 return X.match(FPMO->getOperand(1));
1244 }
1245
1246 return false;
1247 }
1248};
1249
1250/// Match 'fneg X' as 'fsub -0.0, X'.
1251template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1252 return FNeg_match<OpTy>(X);
1253}
1254
1255/// Match 'fneg X' as 'fsub +-0.0, X'.
1256template <typename RHS>
1257inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1258m_FNegNSZ(const RHS &X) {
1259 return m_FSub(m_AnyZeroFP(), X);
1260}
1261
1262template <typename LHS, typename RHS>
1264 const RHS &R) {
1266}
1267
1268template <typename LHS, typename RHS>
1270 const RHS &R) {
1272}
1273
1274template <typename LHS, typename RHS>
1276 const RHS &R) {
1278}
1279
1280template <typename LHS, typename RHS>
1282 const RHS &R) {
1284}
1285
1286template <typename LHS, typename RHS>
1288 const RHS &R) {
1290}
1291
1292template <typename LHS, typename RHS>
1294 const RHS &R) {
1296}
1297
1298template <typename LHS, typename RHS>
1300 const RHS &R) {
1302}
1303
1304template <typename LHS, typename RHS>
1306 const RHS &R) {
1308}
1309
1310template <typename LHS, typename RHS>
1312 const RHS &R) {
1314}
1315
1316template <typename LHS, typename RHS>
1318 const RHS &R) {
1320}
1321
1322template <typename LHS, typename RHS>
1324 const RHS &R) {
1326}
1327
1328template <typename LHS, typename RHS>
1330 const RHS &R) {
1332}
1333
1334template <typename LHS, typename RHS>
1336 const RHS &R) {
1338}
1339
1340template <typename LHS, typename RHS>
1342 const RHS &R) {
1344}
1345
1346template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1349
1350 ShiftLike_match(const LHS_t &LHS, uint64_t &RHS) : L(LHS), R(RHS) {}
1351
1352 template <typename OpTy> bool match(OpTy *V) const {
1353 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1354 if (Op->getOpcode() == Opcode)
1355 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1356 L.match(Op->getOperand(0));
1357 }
1358 // Interpreted as shiftop V, 0
1359 R = 0;
1360 return L.match(V);
1361 }
1362};
1363
1364/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1365template <typename LHS>
1370
1371/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1372template <typename LHS>
1377
1378/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1379template <typename LHS>
1384
1385template <typename LHS_t, typename RHS_t, unsigned Opcode,
1386 unsigned WrapFlags = 0, bool Commutable = false>
1390
1391 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1392 : L(LHS), R(RHS) {}
1393
1394 template <typename OpTy> bool match(OpTy *V) const {
1395 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1396 if (Op->getOpcode() != Opcode)
1397 return false;
1399 !Op->hasNoUnsignedWrap())
1400 return false;
1401 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1402 !Op->hasNoSignedWrap())
1403 return false;
1404 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1405 (Commutable && L.match(Op->getOperand(1)) &&
1406 R.match(Op->getOperand(0)));
1407 }
1408 return false;
1409 }
1410};
1411
1412template <typename LHS, typename RHS>
1413inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1415m_NSWAdd(const LHS &L, const RHS &R) {
1416 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1418 R);
1419}
1420template <typename LHS, typename RHS>
1421inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1423m_c_NSWAdd(const LHS &L, const RHS &R) {
1424 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1426 true>(L, R);
1427}
1428template <typename LHS, typename RHS>
1429inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1431m_NSWSub(const LHS &L, const RHS &R) {
1432 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1434 R);
1435}
1436template <typename LHS, typename RHS>
1437inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1439m_NSWMul(const LHS &L, const RHS &R) {
1440 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1442 R);
1443}
1444template <typename LHS, typename RHS>
1445inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1447m_NSWShl(const LHS &L, const RHS &R) {
1448 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1450 R);
1451}
1452
1453template <typename LHS, typename RHS>
1454inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1456m_NUWAdd(const LHS &L, const RHS &R) {
1457 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1459 L, R);
1460}
1461
1462template <typename LHS, typename RHS>
1464 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1465m_c_NUWAdd(const LHS &L, const RHS &R) {
1466 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1468 true>(L, R);
1469}
1470
1471template <typename LHS, typename RHS>
1472inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1474m_NUWSub(const LHS &L, const RHS &R) {
1475 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1477 L, R);
1478}
1479template <typename LHS, typename RHS>
1480inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1482m_NUWMul(const LHS &L, const RHS &R) {
1483 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1485 L, R);
1486}
1487template <typename LHS, typename RHS>
1488inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1490m_NUWShl(const LHS &L, const RHS &R) {
1491 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1493 L, R);
1494}
1495
1496template <typename LHS_t, typename RHS_t, bool Commutable = false>
1498 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1499 unsigned Opcode;
1500
1501 SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
1502 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1503
1504 template <typename OpTy> bool match(OpTy *V) const {
1506 }
1507};
1508
1509/// Matches a specific opcode.
1510template <typename LHS, typename RHS>
1511inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1512 const RHS &R) {
1513 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1514}
1515
1516template <typename LHS, typename RHS, bool Commutable = false>
1518 LHS L;
1519 RHS R;
1520
1521 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1522
1523 template <typename OpTy> bool match(OpTy *V) const {
1524 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1525 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1526 if (!PDI->isDisjoint())
1527 return false;
1528 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1529 (Commutable && L.match(PDI->getOperand(1)) &&
1530 R.match(PDI->getOperand(0)));
1531 }
1532 return false;
1533 }
1534};
1535
1536template <typename LHS, typename RHS>
1537inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
1538 return DisjointOr_match<LHS, RHS>(L, R);
1539}
1540
1541template <typename LHS, typename RHS>
1543 const RHS &R) {
1545}
1546
1547/// Match either "add" or "or disjoint".
1548template <typename LHS, typename RHS>
1551m_AddLike(const LHS &L, const RHS &R) {
1552 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1553}
1554
1555/// Match either "add nsw" or "or disjoint"
1556template <typename LHS, typename RHS>
1557inline match_combine_or<
1558 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1561m_NSWAddLike(const LHS &L, const RHS &R) {
1562 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1563}
1564
1565/// Match either "add nuw" or "or disjoint"
1566template <typename LHS, typename RHS>
1567inline match_combine_or<
1568 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1571m_NUWAddLike(const LHS &L, const RHS &R) {
1572 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1573}
1574
1575template <typename LHS, typename RHS>
1577 LHS L;
1578 RHS R;
1579
1580 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1581
1582 template <typename OpTy> bool match(OpTy *V) const {
1583 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1584 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1585 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1586 ; // Pass
1587 else if (Op->getOpcode() != Instruction::Xor)
1588 return false;
1589 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1590 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1591 }
1592 return false;
1593 }
1594};
1595
1596/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1597/// Only commutative matcher as the `sub` will need to swap the L and R.
1598template <typename LHS, typename RHS>
1599inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1600 return XorLike_match<LHS, RHS>(L, R);
1601}
1602
1603//===----------------------------------------------------------------------===//
1604// Class that matches a group of binary opcodes.
1605//
1606template <typename LHS_t, typename RHS_t, typename Predicate,
1607 bool Commutable = false>
1608struct BinOpPred_match : Predicate {
1611
1612 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1613
1614 template <typename OpTy> bool match(OpTy *V) const {
1615 if (auto *I = dyn_cast<Instruction>(V))
1616 return this->isOpType(I->getOpcode()) &&
1617 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1618 (Commutable && L.match(I->getOperand(1)) &&
1619 R.match(I->getOperand(0))));
1620 return false;
1621 }
1622};
1623
1625 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1626};
1627
1629 bool isOpType(unsigned Opcode) const {
1630 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1631 }
1632};
1633
1635 bool isOpType(unsigned Opcode) const {
1636 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1637 }
1638};
1639
1641 bool isOpType(unsigned Opcode) const {
1642 return Instruction::isBitwiseLogicOp(Opcode);
1643 }
1644};
1645
1647 bool isOpType(unsigned Opcode) const {
1648 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1649 }
1650};
1651
1653 bool isOpType(unsigned Opcode) const {
1654 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1655 }
1656};
1657
1658/// Matches shift operations.
1659template <typename LHS, typename RHS>
1661 const RHS &R) {
1663}
1664
1665/// Matches logical shift operations.
1666template <typename LHS, typename RHS>
1668 const RHS &R) {
1670}
1671
1672/// Matches logical shift operations.
1673template <typename LHS, typename RHS>
1675m_LogicalShift(const LHS &L, const RHS &R) {
1677}
1678
1679/// Matches bitwise logic operations.
1680template <typename LHS, typename RHS>
1682m_BitwiseLogic(const LHS &L, const RHS &R) {
1684}
1685
1686/// Matches bitwise logic operations in either order.
1687template <typename LHS, typename RHS>
1689m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1691}
1692
1693/// Matches integer division operations.
1694template <typename LHS, typename RHS>
1696 const RHS &R) {
1698}
1699
1700/// Matches integer remainder operations.
1701template <typename LHS, typename RHS>
1703 const RHS &R) {
1705}
1706
1707//===----------------------------------------------------------------------===//
1708// Class that matches exact binary ops.
1709//
1710template <typename SubPattern_t> struct Exact_match {
1711 SubPattern_t SubPattern;
1712
1713 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1714
1715 template <typename OpTy> bool match(OpTy *V) const {
1716 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1717 return PEO->isExact() && SubPattern.match(V);
1718 return false;
1719 }
1720};
1721
1722template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1723 return SubPattern;
1724}
1725
1726//===----------------------------------------------------------------------===//
1727// Matchers for CmpInst classes
1728//
1729
1730template <typename LHS_t, typename RHS_t, typename Class,
1731 bool Commutable = false>
1736
1737 // The evaluation order is always stable, regardless of Commutability.
1738 // The LHS is always matched first.
1739 CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
1740 : Predicate(&Pred), L(LHS), R(RHS) {}
1741 CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
1742 : Predicate(nullptr), L(LHS), R(RHS) {}
1743
1744 template <typename OpTy> bool match(OpTy *V) const {
1745 if (auto *I = dyn_cast<Class>(V)) {
1746 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1747 if (Predicate)
1749 return true;
1750 }
1751 if (Commutable && L.match(I->getOperand(1)) &&
1752 R.match(I->getOperand(0))) {
1753 if (Predicate)
1755 return true;
1756 }
1757 }
1758 return false;
1759 }
1760};
1761
1762template <typename LHS, typename RHS>
1764 const RHS &R) {
1765 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1766}
1767
1768template <typename LHS, typename RHS>
1770 const LHS &L, const RHS &R) {
1771 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1772}
1773
1774template <typename LHS, typename RHS>
1776 const LHS &L, const RHS &R) {
1777 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1778}
1779
1780template <typename LHS, typename RHS>
1781inline CmpClass_match<LHS, RHS, CmpInst> m_Cmp(const LHS &L, const RHS &R) {
1783}
1784
1785template <typename LHS, typename RHS>
1786inline CmpClass_match<LHS, RHS, ICmpInst> m_ICmp(const LHS &L, const RHS &R) {
1788}
1789
1790template <typename LHS, typename RHS>
1791inline CmpClass_match<LHS, RHS, FCmpInst> m_FCmp(const LHS &L, const RHS &R) {
1793}
1794
1795// Same as CmpClass, but instead of saving Pred as out output variable, match a
1796// specific input pred for equality.
1797template <typename LHS_t, typename RHS_t, typename Class,
1798 bool Commutable = false>
1803
1804 SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
1805 : Predicate(Pred), L(LHS), R(RHS) {}
1806
1807 template <typename OpTy> bool match(OpTy *V) const {
1808 if (auto *I = dyn_cast<Class>(V)) {
1810 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1811 return true;
1812 if constexpr (Commutable) {
1815 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1816 return true;
1817 }
1818 }
1819
1820 return false;
1821 }
1822};
1823
1824template <typename LHS, typename RHS>
1826m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1827 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1828}
1829
1830template <typename LHS, typename RHS>
1832m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1833 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1834}
1835
1836template <typename LHS, typename RHS>
1838m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1840}
1841
1842template <typename LHS, typename RHS>
1844m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1845 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1846}
1847
1848//===----------------------------------------------------------------------===//
1849// Matchers for instructions with a given opcode and number of operands.
1850//
1851
1852/// Matches instructions with Opcode and three operands.
1853template <typename T0, unsigned Opcode> struct OneOps_match {
1855
1856 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1857
1858 template <typename OpTy> bool match(OpTy *V) const {
1859 if (V->getValueID() == Value::InstructionVal + Opcode) {
1860 auto *I = cast<Instruction>(V);
1861 return Op1.match(I->getOperand(0));
1862 }
1863 return false;
1864 }
1865};
1866
1867/// Matches instructions with Opcode and three operands.
1868template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1871
1872 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1873
1874 template <typename OpTy> bool match(OpTy *V) const {
1875 if (V->getValueID() == Value::InstructionVal + Opcode) {
1876 auto *I = cast<Instruction>(V);
1877 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1878 }
1879 return false;
1880 }
1881};
1882
1883/// Matches instructions with Opcode and three operands.
1884template <typename T0, typename T1, typename T2, unsigned Opcode,
1885 bool CommutableOp2Op3 = false>
1890
1891 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1892 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1893
1894 template <typename OpTy> bool match(OpTy *V) const {
1895 if (V->getValueID() == Value::InstructionVal + Opcode) {
1896 auto *I = cast<Instruction>(V);
1897 if (!Op1.match(I->getOperand(0)))
1898 return false;
1899 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1900 return true;
1901 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1902 Op3.match(I->getOperand(1));
1903 }
1904 return false;
1905 }
1906};
1907
1908/// Matches instructions with Opcode and any number of operands
1909template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1910 std::tuple<OperandTypes...> Operands;
1911
1912 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1913
1914 // Operand matching works by recursively calling match_operands, matching the
1915 // operands left to right. The first version is called for each operand but
1916 // the last, for which the second version is called. The second version of
1917 // match_operands is also used to match each individual operand.
1918 template <int Idx, int Last>
1919 std::enable_if_t<Idx != Last, bool>
1923
1924 template <int Idx, int Last>
1925 std::enable_if_t<Idx == Last, bool>
1927 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1928 }
1929
1930 template <typename OpTy> bool match(OpTy *V) const {
1931 if (V->getValueID() == Value::InstructionVal + Opcode) {
1932 auto *I = cast<Instruction>(V);
1933 return I->getNumOperands() == sizeof...(OperandTypes) &&
1934 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1935 }
1936 return false;
1937 }
1938};
1939
1940/// Matches SelectInst.
1941template <typename Cond, typename LHS, typename RHS>
1943m_Select(const Cond &C, const LHS &L, const RHS &R) {
1945}
1946
1947/// This matches a select of two constants, e.g.:
1948/// m_SelectCst<-1, 0>(m_Value(V))
1949template <int64_t L, int64_t R, typename Cond>
1951 Instruction::Select>
1954}
1955
1956/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1957template <typename LHS, typename RHS>
1958inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1959m_c_Select(const LHS &L, const RHS &R) {
1960 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1961 true>(m_Value(), L, R);
1962}
1963
1964/// Matches FreezeInst.
1965template <typename OpTy>
1969
1970/// Matches InsertElementInst.
1971template <typename Val_t, typename Elt_t, typename Idx_t>
1973m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1975 Val, Elt, Idx);
1976}
1977
1978/// Matches ExtractElementInst.
1979template <typename Val_t, typename Idx_t>
1981m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1983}
1984
1985/// Matches shuffle.
1986template <typename T0, typename T1, typename T2> struct Shuffle_match {
1990
1991 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1992 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1993
1994 template <typename OpTy> bool match(OpTy *V) const {
1995 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1996 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1997 Mask.match(I->getShuffleMask());
1998 }
1999 return false;
2000 }
2001};
2002
2003struct m_Mask {
2006 bool match(ArrayRef<int> Mask) const {
2007 MaskRef = Mask;
2008 return true;
2009 }
2010};
2011
2013 bool match(ArrayRef<int> Mask) const {
2014 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2015 }
2016};
2017
2021 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2022};
2023
2027 bool match(ArrayRef<int> Mask) const {
2028 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2029 if (First == Mask.end())
2030 return false;
2031 SplatIndex = *First;
2032 return all_of(Mask,
2033 [First](int Elem) { return Elem == *First || Elem == -1; });
2034 }
2035};
2036
2037template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2038 PointerOpTy PointerOp;
2039 OffsetOpTy OffsetOp;
2040
2041 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2043
2044 template <typename OpTy> bool match(OpTy *V) const {
2045 auto *GEP = dyn_cast<GEPOperator>(V);
2046 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2047 PointerOp.match(GEP->getPointerOperand()) &&
2048 OffsetOp.match(GEP->idx_begin()->get());
2049 }
2050};
2051
2052/// Matches ShuffleVectorInst independently of mask value.
2053template <typename V1_t, typename V2_t>
2055m_Shuffle(const V1_t &v1, const V2_t &v2) {
2057}
2058
2059template <typename V1_t, typename V2_t, typename Mask_t>
2061m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2063}
2064
2065/// Matches LoadInst.
2066template <typename OpTy>
2070
2071/// Matches StoreInst.
2072template <typename ValueOpTy, typename PointerOpTy>
2074m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2076 PointerOp);
2077}
2078
2079/// Matches GetElementPtrInst.
2080template <typename... OperandTypes>
2081inline auto m_GEP(const OperandTypes &...Ops) {
2082 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2083}
2084
2085/// Matches GEP with i8 source element type
2086template <typename PointerOpTy, typename OffsetOpTy>
2088m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2090}
2091
2092//===----------------------------------------------------------------------===//
2093// Matchers for CastInst classes
2094//
2095
2096template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2097 Op_t Op;
2098
2099 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2100
2101 template <typename OpTy> bool match(OpTy *V) const {
2102 if (auto *O = dyn_cast<Operator>(V))
2103 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2104 return false;
2105 }
2106};
2107
2108template <typename Op_t, typename Class> struct CastInst_match {
2109 Op_t Op;
2110
2111 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2112
2113 template <typename OpTy> bool match(OpTy *V) const {
2114 if (auto *I = dyn_cast<Class>(V))
2115 return Op.match(I->getOperand(0));
2116 return false;
2117 }
2118};
2119
2120template <typename Op_t> struct PtrToIntSameSize_match {
2122 Op_t Op;
2123
2124 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2125 : DL(DL), Op(OpMatch) {}
2126
2127 template <typename OpTy> bool match(OpTy *V) const {
2128 if (auto *O = dyn_cast<Operator>(V))
2129 return O->getOpcode() == Instruction::PtrToInt &&
2130 DL.getTypeSizeInBits(O->getType()) ==
2131 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2132 Op.match(O->getOperand(0));
2133 return false;
2134 }
2135};
2136
2137template <typename Op_t> struct NNegZExt_match {
2138 Op_t Op;
2139
2140 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2141
2142 template <typename OpTy> bool match(OpTy *V) const {
2143 if (auto *I = dyn_cast<ZExtInst>(V))
2144 return I->hasNonNeg() && Op.match(I->getOperand(0));
2145 return false;
2146 }
2147};
2148
2149template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2150 Op_t Op;
2151
2152 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2153
2154 template <typename OpTy> bool match(OpTy *V) const {
2155 if (auto *I = dyn_cast<TruncInst>(V))
2156 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2157 Op.match(I->getOperand(0));
2158 return false;
2159 }
2160};
2161
2162/// Matches BitCast.
2163template <typename OpTy>
2168
2169template <typename Op_t> struct ElementWiseBitCast_match {
2170 Op_t Op;
2171
2172 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2173
2174 template <typename OpTy> bool match(OpTy *V) const {
2175 auto *I = dyn_cast<BitCastInst>(V);
2176 if (!I)
2177 return false;
2178 Type *SrcType = I->getSrcTy();
2179 Type *DstType = I->getType();
2180 // Make sure the bitcast doesn't change between scalar and vector and
2181 // doesn't change the number of vector elements.
2182 if (SrcType->isVectorTy() != DstType->isVectorTy())
2183 return false;
2184 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2185 SrcVecTy && SrcVecTy->getElementCount() !=
2186 cast<VectorType>(DstType)->getElementCount())
2187 return false;
2188 return Op.match(I->getOperand(0));
2189 }
2190};
2191
2192template <typename OpTy>
2196
2197/// Matches PtrToInt.
2198template <typename OpTy>
2203
2204template <typename OpTy>
2209
2210/// Matches PtrToAddr.
2211template <typename OpTy>
2216
2217/// Matches PtrToInt or PtrToAddr.
2218template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2220}
2221
2222/// Matches IntToPtr.
2223template <typename OpTy>
2228
2229/// Matches any cast or self. Used to ignore casts.
2230template <typename OpTy>
2232m_CastOrSelf(const OpTy &Op) {
2234}
2235
2236/// Matches Trunc.
2237template <typename OpTy>
2241
2242/// Matches trunc nuw.
2243template <typename OpTy>
2248
2249/// Matches trunc nsw.
2250template <typename OpTy>
2255
2256template <typename OpTy>
2258m_TruncOrSelf(const OpTy &Op) {
2259 return m_CombineOr(m_Trunc(Op), Op);
2260}
2261
2262/// Matches SExt.
2263template <typename OpTy>
2267
2268/// Matches ZExt.
2269template <typename OpTy>
2273
2274template <typename OpTy>
2276 return NNegZExt_match<OpTy>(Op);
2277}
2278
2279template <typename OpTy>
2281m_ZExtOrSelf(const OpTy &Op) {
2282 return m_CombineOr(m_ZExt(Op), Op);
2283}
2284
2285template <typename OpTy>
2287m_SExtOrSelf(const OpTy &Op) {
2288 return m_CombineOr(m_SExt(Op), Op);
2289}
2290
2291/// Match either "sext" or "zext nneg".
2292template <typename OpTy>
2294m_SExtLike(const OpTy &Op) {
2295 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2296}
2297
2298template <typename OpTy>
2301m_ZExtOrSExt(const OpTy &Op) {
2302 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2303}
2304
2305template <typename OpTy>
2308 OpTy>
2310 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2311}
2312
2313template <typename OpTy> inline auto m_ZExtOrTruncOrSelf(const OpTy &Op) {
2314 return m_CombineOr(m_ZExt(Op), m_Trunc(Op), Op);
2315}
2316
2317template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2318 CondTy Cond;
2321
2322 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2323 : Cond(C), TrueC(TC), FalseC(FC) {}
2324
2325 template <typename OpTy> bool match(OpTy *V) const {
2326 // select(Cond, TrueC, FalseC) — captures both constants directly
2328 return true;
2329
2330 Type *Ty = V->getType();
2331 Value *CondV = nullptr;
2332
2333 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2334 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2335 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2336 TrueC.match(ConstantInt::get(Ty, 1)) &&
2337 FalseC.match(ConstantInt::get(Ty, 0)))
2338 return true;
2339
2340 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2341 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2342 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2343 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2344 FalseC.match(ConstantInt::get(Ty, 0)))
2345 return true;
2346
2347 return false;
2348 }
2349};
2350
2351/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2352/// select i1 Cond, TrueC, FalseC
2353/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2354/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2355///
2356/// The condition is matched against \p Cond, and the true/false constants
2357/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2358/// constants are bound to \p TrueC and \p FalseC via their matchers.
2359template <typename CondTy, typename LTy, typename RTy>
2361m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2362 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2363}
2364
2365template <typename OpTy>
2369
2370template <typename OpTy>
2374
2375template <typename OpTy>
2378m_IToFP(const OpTy &Op) {
2379 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2380}
2381
2382template <typename OpTy>
2386
2387template <typename OpTy>
2391
2392template <typename OpTy>
2395m_FPToI(const OpTy &Op) {
2396 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2397}
2398
2399template <typename OpTy>
2403
2404template <typename OpTy>
2408
2409//===----------------------------------------------------------------------===//
2410// Matchers for control flow.
2411//
2412
2413struct br_match {
2415
2417
2418 template <typename OpTy> bool match(OpTy *V) const {
2419 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2420 Succ = BI->getSuccessor();
2421 return true;
2422 }
2423 return false;
2424 }
2425};
2426
2427inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2428
2429template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2431 Cond_t Cond;
2432 TrueBlock_t T;
2433 FalseBlock_t F;
2434
2435 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2436 : Cond(C), T(t), F(f) {}
2437
2438 template <typename OpTy> bool match(OpTy *V) const {
2439 if (auto *BI = dyn_cast<CondBrInst>(V))
2440 if (Cond.match(BI->getCondition()))
2441 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2442 return false;
2443 }
2444};
2445
2446template <typename Cond_t>
2452
2453template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2455m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2457}
2458
2459//===----------------------------------------------------------------------===//
2460// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2461//
2462
2463template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2464 bool Commutable = false>
2466 using PredType = Pred_t;
2469
2470 // The evaluation order is always stable, regardless of Commutability.
2471 // The LHS is always matched first.
2472 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2473
2474 template <typename OpTy> bool match(OpTy *V) const {
2475 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2476 Intrinsic::ID IID = II->getIntrinsicID();
2477 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2478 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2479 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2480 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2481 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2482 return (L.match(LHS) && R.match(RHS)) ||
2483 (Commutable && L.match(RHS) && R.match(LHS));
2484 }
2485 }
2486 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2487 auto *SI = dyn_cast<SelectInst>(V);
2488 if (!SI)
2489 return false;
2490 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2491 if (!Cmp)
2492 return false;
2493 // At this point we have a select conditioned on a comparison. Check that
2494 // it is the values returned by the select that are being compared.
2495 auto *TrueVal = SI->getTrueValue();
2496 auto *FalseVal = SI->getFalseValue();
2497 auto *LHS = Cmp->getOperand(0);
2498 auto *RHS = Cmp->getOperand(1);
2499 if ((TrueVal != LHS || FalseVal != RHS) &&
2500 (TrueVal != RHS || FalseVal != LHS))
2501 return false;
2502 typename CmpInst_t::Predicate Pred =
2503 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2504 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2505 if (!Pred_t::match(Pred))
2506 return false;
2507 // It does! Bind the operands.
2508 return (L.match(LHS) && R.match(RHS)) ||
2509 (Commutable && L.match(RHS) && R.match(LHS));
2510 }
2511};
2512
2513/// Helper class for identifying signed max predicates.
2515 static bool match(ICmpInst::Predicate Pred) {
2516 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2517 }
2518};
2519
2520/// Helper class for identifying signed min predicates.
2522 static bool match(ICmpInst::Predicate Pred) {
2523 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2524 }
2525};
2526
2527/// Helper class for identifying unsigned max predicates.
2529 static bool match(ICmpInst::Predicate Pred) {
2530 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2531 }
2532};
2533
2534/// Helper class for identifying unsigned min predicates.
2536 static bool match(ICmpInst::Predicate Pred) {
2537 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2538 }
2539};
2540
2541/// Helper class for identifying ordered max predicates.
2543 static bool match(FCmpInst::Predicate Pred) {
2544 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2545 }
2546};
2547
2548/// Helper class for identifying ordered min predicates.
2550 static bool match(FCmpInst::Predicate Pred) {
2551 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2552 }
2553};
2554
2555/// Helper class for identifying unordered max predicates.
2557 static bool match(FCmpInst::Predicate Pred) {
2558 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2559 }
2560};
2561
2562/// Helper class for identifying unordered min predicates.
2564 static bool match(FCmpInst::Predicate Pred) {
2565 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2566 }
2567};
2568
2569template <typename LHS, typename RHS>
2571 const RHS &R) {
2573}
2574
2575template <typename LHS, typename RHS>
2577 const RHS &R) {
2579}
2580
2581template <typename LHS, typename RHS>
2583 const RHS &R) {
2585}
2586
2587template <typename LHS, typename RHS>
2589 const RHS &R) {
2591}
2592
2593template <typename LHS, typename RHS>
2594inline auto m_MaxOrMin(const LHS &L, const RHS &R) {
2595 return m_CombineOr(m_SMax(L, R), m_SMin(L, R), m_UMax(L, R), m_UMin(L, R));
2596}
2597
2598/// Match an 'ordered' floating point maximum function.
2599/// Floating point has one special value 'NaN'. Therefore, there is no total
2600/// order. However, if we can ignore the 'NaN' value (for example, because of a
2601/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2602/// semantics. In the presence of 'NaN' we have to preserve the original
2603/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2604///
2605/// max(L, R) iff L and R are not NaN
2606/// m_OrdFMax(L, R) = R iff L or R are NaN
2607template <typename LHS, typename RHS>
2612
2613/// Match an 'ordered' floating point minimum function.
2614/// Floating point has one special value 'NaN'. Therefore, there is no total
2615/// order. However, if we can ignore the 'NaN' value (for example, because of a
2616/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2617/// semantics. In the presence of 'NaN' we have to preserve the original
2618/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2619///
2620/// min(L, R) iff L and R are not NaN
2621/// m_OrdFMin(L, R) = R iff L or R are NaN
2622template <typename LHS, typename RHS>
2627
2628/// Match an 'unordered' floating point maximum function.
2629/// Floating point has one special value 'NaN'. Therefore, there is no total
2630/// order. However, if we can ignore the 'NaN' value (for example, because of a
2631/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2632/// semantics. In the presence of 'NaN' we have to preserve the original
2633/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2634///
2635/// max(L, R) iff L and R are not NaN
2636/// m_UnordFMax(L, R) = L iff L or R are NaN
2637template <typename LHS, typename RHS>
2639m_UnordFMax(const LHS &L, const RHS &R) {
2641}
2642
2643/// Match an 'unordered' floating point minimum function.
2644/// Floating point has one special value 'NaN'. Therefore, there is no total
2645/// order. However, if we can ignore the 'NaN' value (for example, because of a
2646/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2647/// semantics. In the presence of 'NaN' we have to preserve the original
2648/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2649///
2650/// min(L, R) iff L and R are not NaN
2651/// m_UnordFMin(L, R) = L iff L or R are NaN
2652template <typename LHS, typename RHS>
2654m_UnordFMin(const LHS &L, const RHS &R) {
2656}
2657
2658/// Match an 'ordered' or 'unordered' floating point maximum function.
2659/// Floating point has one special value 'NaN'. Therefore, there is no total
2660/// order. However, if we can ignore the 'NaN' value (for example, because of a
2661/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2662/// semantics.
2663template <typename LHS, typename RHS>
2670
2671/// Match an 'ordered' or 'unordered' floating point minimum function.
2672/// Floating point has one special value 'NaN'. Therefore, there is no total
2673/// order. However, if we can ignore the 'NaN' value (for example, because of a
2674/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2675/// semantics.
2676template <typename LHS, typename RHS>
2683
2684/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2685/// NOTE: we first match the 'Not' (by matching '-1'),
2686/// and only then match the inner matcher!
2687template <typename ValTy>
2688inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2689m_Not(const ValTy &V) {
2690 return m_c_Xor(m_AllOnes(), V);
2691}
2692
2693template <typename ValTy>
2694inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2695 true>
2696m_NotForbidPoison(const ValTy &V) {
2697 return m_c_Xor(m_AllOnesForbidPoison(), V);
2698}
2699
2700//===----------------------------------------------------------------------===//
2701// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2702// Note that S might be matched to other instructions than AddInst.
2703//
2704
2705template <typename LHS_t, typename RHS_t, typename Sum_t>
2709 Sum_t S;
2710
2711 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2712 : L(L), R(R), S(S) {}
2713
2714 template <typename OpTy> bool match(OpTy *V) const {
2715 Value *ICmpLHS, *ICmpRHS;
2716 CmpPredicate Pred;
2717 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2718 return false;
2719
2720 Value *AddLHS, *AddRHS;
2721 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2722
2723 // (a + b) u< a, (a + b) u< b
2724 if (Pred == ICmpInst::ICMP_ULT)
2725 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2726 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2727
2728 // a >u (a + b), b >u (a + b)
2729 if (Pred == ICmpInst::ICMP_UGT)
2730 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2731 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2732
2733 Value *Op1;
2734 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2735 // (~a) <u b
2736 if (Pred == ICmpInst::ICMP_ULT) {
2737 if (XorExpr.match(ICmpLHS))
2738 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2739 }
2740 // b > u (~a)
2741 if (Pred == ICmpInst::ICMP_UGT) {
2742 if (XorExpr.match(ICmpRHS))
2743 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2744 }
2745
2746 // Match special-case for increment-by-1.
2747 if (Pred == ICmpInst::ICMP_EQ) {
2748 // (a + 1) == 0
2749 // (1 + a) == 0
2750 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2751 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2752 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2753 // 0 == (a + 1)
2754 // 0 == (1 + a)
2755 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2756 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2757 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2758 }
2759
2760 return false;
2761 }
2762};
2763
2764/// Match an icmp instruction checking for unsigned overflow on addition.
2765///
2766/// S is matched to the addition whose result is being checked for overflow, and
2767/// L and R are matched to the LHS and RHS of S.
2768template <typename LHS_t, typename RHS_t, typename Sum_t>
2770m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2772}
2773
2774template <typename Opnd_t> struct Argument_match {
2775 unsigned OpI;
2776 Opnd_t Val;
2777
2778 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2779
2780 template <typename OpTy> bool match(OpTy *V) const {
2781 // FIXME: Should likely be switched to use `CallBase`.
2782 if (const auto *CI = dyn_cast<CallInst>(V))
2783 return Val.match(CI->getArgOperand(OpI));
2784 return false;
2785 }
2786};
2787
2788/// Match an argument.
2789template <unsigned OpI, typename Opnd_t>
2790inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2791 return Argument_match<Opnd_t>(OpI, Op);
2792}
2793
2794/// Intrinsic matchers.
2796 unsigned ID;
2797
2799
2800 template <typename OpTy> bool match(OpTy *V) const {
2801 if (const auto *CI = dyn_cast<CallInst>(V))
2802 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2803 return F->getIntrinsicID() == ID;
2804 return false;
2805 }
2806};
2807
2808/// Intrinsic matches are combinations of ID matchers, and argument
2809/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2810/// them with lower arity matchers. Here's some convenient typedefs for up to
2811/// several arguments, and more can be added as needed
2812template <typename T0 = void, typename T1 = void, typename T2 = void,
2813 typename T3 = void, typename T4 = void, typename T5 = void,
2814 typename T6 = void, typename T7 = void, typename T8 = void,
2815 typename T9 = void, typename T10 = void>
2817template <typename T0> struct m_Intrinsic_Ty<T0> {
2819};
2820template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2821 using Ty =
2823};
2824template <typename T0, typename T1, typename T2>
2829template <typename T0, typename T1, typename T2, typename T3>
2834
2835template <typename T0, typename T1, typename T2, typename T3, typename T4>
2840
2841template <typename T0, typename T1, typename T2, typename T3, typename T4,
2842 typename T5>
2847
2848/// Match intrinsic calls like this:
2849/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2850template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2851 return IntrinsicID_match(IntrID);
2852}
2853
2854/// Matches MaskedLoad Intrinsic.
2855template <typename Opnd0, typename Opnd1, typename Opnd2>
2857m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2858 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2859}
2860
2861/// Matches MaskedStore Intrinsic.
2862template <typename Opnd0, typename Opnd1, typename Opnd2>
2864m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2865 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2866}
2867
2868/// Matches MaskedGather Intrinsic.
2869template <typename Opnd0, typename Opnd1, typename Opnd2>
2871m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2872 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2873}
2874
2875template <Intrinsic::ID IntrID, typename T0>
2876inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2878}
2879
2880template <Intrinsic::ID IntrID, typename T0, typename T1>
2881inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2882 const T1 &Op1) {
2884}
2885
2886template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2887inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2888m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2889 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2890}
2891
2892template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2893 typename T3>
2895m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2896 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2897}
2898
2899template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2900 typename T3, typename T4>
2902m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2903 const T4 &Op4) {
2904 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2905 m_Argument<4>(Op4));
2906}
2907
2908template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2909 typename T3, typename T4, typename T5>
2911m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2912 const T4 &Op4, const T5 &Op5) {
2913 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2914 m_Argument<5>(Op5));
2915}
2916
2917// Helper intrinsic matching specializations.
2918template <typename Opnd0>
2919inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2921}
2922
2923template <typename Opnd0>
2924inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2926}
2927
2928template <typename Opnd0>
2929inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2930 return m_Intrinsic<Intrinsic::fabs>(Op0);
2931}
2932
2933template <typename Opnd0>
2934inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2936}
2937
2938template <typename Opnd0, typename Opnd1>
2939inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2940 const Opnd1 &Op1) {
2941 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2942}
2943
2944template <typename Opnd0, typename Opnd1>
2945inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2946 const Opnd1 &Op1) {
2947 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2948}
2949
2950template <typename Opnd0, typename Opnd1>
2952m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2953 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2954}
2955
2956template <typename Opnd0, typename Opnd1>
2957inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2958 const Opnd1 &Op1) {
2959 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2960}
2961
2962template <typename Opnd0, typename Opnd1>
2963inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2964 const Opnd1 &Op1) {
2965 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2966}
2967
2968template <typename Opnd0, typename Opnd1>
2970m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2971 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2972}
2973
2974template <typename Opnd0, typename Opnd1>
2977m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2978 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
2979}
2980
2981template <typename Opnd0, typename Opnd1>
2984m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2985 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
2986}
2987
2988template <typename Opnd0, typename Opnd1, typename Opnd2>
2990m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2991 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2992}
2993
2994template <typename Opnd0, typename Opnd1, typename Opnd2>
2996m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2997 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2998}
2999
3000template <typename Opnd0>
3001inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3002 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3003}
3004
3005template <typename Opnd0, typename Opnd1>
3006inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3007 const Opnd1 &Op1) {
3008 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3009}
3010
3011template <typename Opnd0>
3012inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3014}
3015
3016template <typename Opnd0, typename Opnd1, typename Opnd2>
3018m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3019 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3020}
3021
3022//===----------------------------------------------------------------------===//
3023// Matchers for two-operands operators with the operators in either order
3024//
3025
3026/// Matches a BinaryOperator with LHS and RHS in either order.
3027template <typename LHS, typename RHS>
3028inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
3030}
3031
3032/// Matches an ICmp with a predicate over LHS and RHS in either order.
3033/// Swaps the predicate if operands are commuted.
3034template <typename LHS, typename RHS>
3036m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3038}
3039
3040template <typename LHS, typename RHS>
3042 const RHS &R) {
3044}
3045
3046/// Matches a specific opcode with LHS and RHS in either order.
3047template <typename LHS, typename RHS>
3049m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3050 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3051}
3052
3053/// Matches a Add with LHS and RHS in either order.
3054template <typename LHS, typename RHS>
3059
3060/// Matches a Mul with LHS and RHS in either order.
3061template <typename LHS, typename RHS>
3066
3067/// Matches an And with LHS and RHS in either order.
3068template <typename LHS, typename RHS>
3073
3074/// Matches an Or with LHS and RHS in either order.
3075template <typename LHS, typename RHS>
3077 const RHS &R) {
3079}
3080
3081/// Matches an Xor with LHS and RHS in either order.
3082template <typename LHS, typename RHS>
3087
3088/// Matches a 'Neg' as 'sub 0, V'.
3089template <typename ValTy>
3090inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3091m_Neg(const ValTy &V) {
3092 return m_Sub(m_ZeroInt(), V);
3093}
3094
3095/// Matches a 'Neg' as 'sub nsw 0, V'.
3096template <typename ValTy>
3098 Instruction::Sub,
3100m_NSWNeg(const ValTy &V) {
3101 return m_NSWSub(m_ZeroInt(), V);
3102}
3103
3104/// Matches an SMin with LHS and RHS in either order.
3105template <typename LHS, typename RHS>
3107m_c_SMin(const LHS &L, const RHS &R) {
3109}
3110/// Matches an SMax with LHS and RHS in either order.
3111template <typename LHS, typename RHS>
3113m_c_SMax(const LHS &L, const RHS &R) {
3115}
3116/// Matches a UMin with LHS and RHS in either order.
3117template <typename LHS, typename RHS>
3119m_c_UMin(const LHS &L, const RHS &R) {
3121}
3122/// Matches a UMax with LHS and RHS in either order.
3123template <typename LHS, typename RHS>
3125m_c_UMax(const LHS &L, const RHS &R) {
3127}
3128
3129template <typename LHS, typename RHS>
3130inline auto m_c_MaxOrMin(const LHS &L, const RHS &R) {
3131 return m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R), m_c_UMax(L, R),
3132 m_c_UMin(L, R));
3133}
3134
3135template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3137 LHS L;
3138 RHS R;
3139
3140 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3141
3142 template <typename OpTy> bool match(OpTy *V) const {
3143 const auto *II = dyn_cast<IntrinsicInst>(V);
3144 if (!II || II->getIntrinsicID() != IntrID)
3145 return false;
3146 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3147 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3148 }
3149};
3150
3151template <Intrinsic::ID IntrID, typename T0, typename T1>
3153m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3155}
3156
3157/// Matches FAdd with LHS and RHS in either order.
3158template <typename LHS, typename RHS>
3160m_c_FAdd(const LHS &L, const RHS &R) {
3162}
3163
3164/// Matches FMul with LHS and RHS in either order.
3165template <typename LHS, typename RHS>
3167m_c_FMul(const LHS &L, const RHS &R) {
3169}
3170
3171template <typename Opnd_t> struct Signum_match {
3172 Opnd_t Val;
3173 Signum_match(const Opnd_t &V) : Val(V) {}
3174
3175 template <typename OpTy> bool match(OpTy *V) const {
3176 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3177 if (TypeSize == 0)
3178 return false;
3179
3180 unsigned ShiftWidth = TypeSize - 1;
3181 Value *Op;
3182
3183 // This is the representation of signum we match:
3184 //
3185 // signum(x) == (x >> 63) | (-x >>u 63)
3186 //
3187 // An i1 value is its own signum, so it's correct to match
3188 //
3189 // signum(x) == (x >> 0) | (-x >>u 0)
3190 //
3191 // for i1 values.
3192
3193 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3194 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3195 auto Signum = m_c_Or(LHS, RHS);
3196
3197 return Signum.match(V) && Val.match(Op);
3198 }
3199};
3200
3201/// Matches a signum pattern.
3202///
3203/// signum(x) =
3204/// x > 0 -> 1
3205/// x == 0 -> 0
3206/// x < 0 -> -1
3207template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3208 return Signum_match<Val_t>(V);
3209}
3210
3211template <int Ind, typename Opnd_t> struct ExtractValue_match {
3212 Opnd_t Val;
3213 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3214
3215 template <typename OpTy> bool match(OpTy *V) const {
3216 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3217 // If Ind is -1, don't inspect indices
3218 if (Ind != -1 &&
3219 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3220 return false;
3221 return Val.match(I->getAggregateOperand());
3222 }
3223 return false;
3224 }
3225};
3226
3227/// Match a single index ExtractValue instruction.
3228/// For example m_ExtractValue<1>(...)
3229template <int Ind, typename Val_t>
3233
3234/// Match an ExtractValue instruction with any index.
3235/// For example m_ExtractValue(...)
3236template <typename Val_t>
3237inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3238 return ExtractValue_match<-1, Val_t>(V);
3239}
3240
3241/// Matcher for a single index InsertValue instruction.
3242template <int Ind, typename T0, typename T1> struct InsertValue_match {
3245
3246 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3247
3248 template <typename OpTy> bool match(OpTy *V) const {
3249 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3250 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3251 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3252 }
3253 return false;
3254 }
3255};
3256
3257/// Matches a single index InsertValue instruction.
3258template <int Ind, typename Val_t, typename Elt_t>
3260 const Elt_t &Elt) {
3261 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3262}
3263
3264/// Matches a call to `llvm.vscale()`.
3266
3267template <typename Opnd0, typename Opnd1>
3269m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3271}
3272
3273template <typename Opnd>
3277
3278template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3280 LHS L;
3281 RHS R;
3282
3283 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3284
3285 template <typename T> bool match(T *V) const {
3286 auto *I = dyn_cast<Instruction>(V);
3287 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3288 return false;
3289
3290 if (I->getOpcode() == Opcode) {
3291 auto *Op0 = I->getOperand(0);
3292 auto *Op1 = I->getOperand(1);
3293 return (L.match(Op0) && R.match(Op1)) ||
3294 (Commutable && L.match(Op1) && R.match(Op0));
3295 }
3296
3297 if (auto *Select = dyn_cast<SelectInst>(I)) {
3298 auto *Cond = Select->getCondition();
3299 auto *TVal = Select->getTrueValue();
3300 auto *FVal = Select->getFalseValue();
3301
3302 // Don't match a scalar select of bool vectors.
3303 // Transforms expect a single type for operands if this matches.
3304 if (Cond->getType() != Select->getType())
3305 return false;
3306
3307 if (Opcode == Instruction::And) {
3308 auto *C = dyn_cast<Constant>(FVal);
3309 if (C && C->isNullValue())
3310 return (L.match(Cond) && R.match(TVal)) ||
3311 (Commutable && L.match(TVal) && R.match(Cond));
3312 } else {
3313 assert(Opcode == Instruction::Or);
3314 auto *C = dyn_cast<Constant>(TVal);
3315 if (C && C->isOneValue())
3316 return (L.match(Cond) && R.match(FVal)) ||
3317 (Commutable && L.match(FVal) && R.match(Cond));
3318 }
3319 }
3320
3321 return false;
3322 }
3323};
3324
3325/// Matches L && R either in the form of L & R or L ? R : false.
3326/// Note that the latter form is poison-blocking.
3327template <typename LHS, typename RHS>
3329 const RHS &R) {
3331}
3332
3333/// Matches L && R where L and R are arbitrary values.
3334inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3335
3336/// Matches L && R with LHS and RHS in either order.
3337template <typename LHS, typename RHS>
3339m_c_LogicalAnd(const LHS &L, const RHS &R) {
3341}
3342
3343/// Matches L || R either in the form of L | R or L ? true : R.
3344/// Note that the latter form is poison-blocking.
3345template <typename LHS, typename RHS>
3347 const RHS &R) {
3349}
3350
3351/// Matches L || R where L and R are arbitrary values.
3352inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3353
3354/// Matches L || R with LHS and RHS in either order.
3355template <typename LHS, typename RHS>
3357m_c_LogicalOr(const LHS &L, const RHS &R) {
3359}
3360
3361/// Matches either L && R or L || R,
3362/// either one being in the either binary or logical form.
3363/// Note that the latter form is poison-blocking.
3364template <typename LHS, typename RHS, bool Commutable = false>
3370
3371/// Matches either L && R or L || R where L and R are arbitrary values.
3372inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3373
3374/// Matches either L && R or L || R with LHS and RHS in either order.
3375template <typename LHS, typename RHS>
3376inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3377 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3378}
3379
3380} // end namespace PatternMatch
3381} // end namespace llvm
3382
3383#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)
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:1567
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_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)
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.
@ 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.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
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)
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).
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
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)
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)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
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)
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
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)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
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,...
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const 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.
Match a specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)