LLVM 24.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#include <utility>
48
49namespace llvm {
50namespace PatternMatch {
51
52using namespace llvm::PatternMatchHelpers;
53
54template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
55 return P.match(V);
56}
57
58/// A match functor that can be used as a UnaryPredicate in functional
59/// algorithms like all_of.
60template <typename Val = const Value, typename Pattern>
61auto match_fn(const Pattern &P) {
63}
64
65template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
66 return P.match(Mask);
67}
68
69template <typename SubPattern_t> struct OneUse_match {
70 SubPattern_t SubPattern;
71
72 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
73
74 template <typename OpTy> bool match(OpTy *V) const {
75 return V->hasOneUse() && SubPattern.match(V);
76 }
77};
78
79template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
80 return SubPattern;
81}
82
83template <typename SubPattern_t, int Flag> struct AllowFmf_match {
84 SubPattern_t SubPattern;
86
87 AllowFmf_match(const SubPattern_t &SP) : SubPattern(SP), FMF(Flag) {}
88
89 template <typename OpTy> bool match(OpTy *V) const {
90 auto *I = dyn_cast<FPMathOperator>(V);
91 return I && ((I->getFastMathFlags() & FMF) == FMF) && SubPattern.match(I);
92 }
93};
94
95template <typename T>
97m_AllowReassoc(const T &SubPattern) {
98 return SubPattern;
99}
100
101template <typename T>
103m_AllowReciprocal(const T &SubPattern) {
104 return SubPattern;
105}
106
107template <typename T>
109m_AllowContract(const T &SubPattern) {
110 return SubPattern;
111}
112
113template <typename T>
115m_ApproxFunc(const T &SubPattern) {
116 return SubPattern;
117}
118
119template <typename T>
121 return SubPattern;
122}
123
124template <typename T>
126 return SubPattern;
127}
128
129template <typename T>
131m_NoSignedZeros(const T &SubPattern) {
132 return SubPattern;
133}
134
135/// Match an arbitrary value and ignore it.
136inline auto m_Value() { return m_Isa<Value>(); }
137
138/// Match an arbitrary unary operation and ignore it.
139inline auto m_UnOp() { return m_Isa<UnaryOperator>(); }
140
141/// Match an arbitrary binary operation and ignore it.
142inline auto m_BinOp() { return m_Isa<BinaryOperator>(); }
143
144/// Matches any compare instruction and ignore it.
145inline auto m_Cmp() { return m_Isa<CmpInst>(); }
146
147/// Matches any intrinsic call and ignore it.
148inline auto m_AnyIntrinsic() { return m_Isa<IntrinsicInst>(); }
149
151private:
152 LLVM_ABI static bool checkAggregate(const ConstantAggregate *CA);
153
154public:
155 static bool check(const Value *V) {
156 if (isa<UndefValue>(V))
157 return true;
158 if (const auto *CA = dyn_cast<ConstantAggregate>(V))
159 return checkAggregate(CA);
160 return false;
161 }
162 template <typename ITy> bool match(ITy *V) const { return check(V); }
163};
164
165/// Match an arbitrary undef constant. This matches poison as well.
166/// If this is an aggregate and contains a non-aggregate element that is
167/// neither undef nor poison, the aggregate is not matched.
168inline auto m_Undef() { return undef_match(); }
169
170/// Match an arbitrary UndefValue constant.
171inline auto m_UndefValue() { return m_Isa<UndefValue>(); }
172
173/// Match an arbitrary poison constant.
174inline auto m_Poison() { return m_Isa<PoisonValue>(); }
175
176/// Match an arbitrary Constant and ignore it.
177inline auto m_Constant() { return m_Isa<Constant>(); }
178
179/// Match an arbitrary ConstantInt and ignore it.
180inline auto m_ConstantInt() { return m_Isa<ConstantInt>(); }
181
182/// Match an arbitrary ConstantFP and ignore it.
183inline auto m_ConstantFP() { return m_Isa<ConstantFP>(); }
184
185template <typename SPTy> struct ContainsMatchingVectorElement_match {
188
189 template <typename ITy> bool match(ITy *V) const {
190 auto *C = dyn_cast<Constant>(V);
191 return C && C->containsMatchingVectorElement(
192 [&](Constant *E) { return SubPattern.match(E); });
193 }
194};
195
196/// Match a vector constant where at least one of its elements matches the
197/// subpattern. Scalable vector constants are not matched. Any bindings in the
198/// subpattern will be bound to the first match.
199template <typename SPTy>
201m_ContainsMatchingVectorElement(const SPTy &SubPattern) {
202 return SubPattern;
203}
204
205/// Match a constant expression or a constant that contains a constant
206/// expression.
211
212template <typename SubPattern_t> struct Splat_match {
213 SubPattern_t SubPattern;
214 Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
215
216 template <typename OpTy> bool match(OpTy *V) const {
217 if (auto *C = dyn_cast<Constant>(V)) {
218 auto *Splat = C->getSplatValue();
219 return Splat ? SubPattern.match(Splat) : false;
220 }
221 // TODO: Extend to other cases (e.g. shufflevectors).
222 return false;
223 }
224};
225
226/// Match a constant splat. TODO: Extend this to non-constant splats.
227template <typename T>
228inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
229 return SubPattern;
230}
231
232/// Match an arbitrary basic block value and ignore it.
233inline auto m_BasicBlock() { return m_Isa<BasicBlock>(); }
234
235template <typename APTy> struct ap_match {
236 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
238 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
239
240 const APTy *&Res;
242
243 ap_match(const APTy *&Res, bool AllowPoison)
245
246 template <typename ITy> bool match(ITy *V) const {
247 if (auto *CI = dyn_cast<ConstantTy>(V)) {
248 Res = &CI->getValue();
249 return true;
250 }
251 if (V->getType()->isVectorTy())
252 if (const auto *C = dyn_cast<Constant>(V))
253 if (auto *CI =
254 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
255 Res = &CI->getValue();
256 return true;
257 }
258 return false;
259 }
260};
261
262/// Match a ConstantInt or splatted ConstantVector, binding the
263/// specified pointer to the contained APInt.
264inline ap_match<APInt> m_APInt(const APInt *&Res) {
265 // Forbid poison by default to maintain previous behavior.
266 return ap_match<APInt>(Res, /* AllowPoison */ false);
267}
268
269/// Match APInt while allowing poison in splat vector constants.
271 return ap_match<APInt>(Res, /* AllowPoison */ true);
272}
273
274/// Match APInt while forbidding poison in splat vector constants.
276 return ap_match<APInt>(Res, /* AllowPoison */ false);
277}
278
279/// Match a ConstantFP or splatted ConstantVector, binding the
280/// specified pointer to the contained APFloat.
282 // Forbid undefs by default to maintain previous behavior.
283 return ap_match<APFloat>(Res, /* AllowPoison */ false);
284}
285
286/// Match APFloat while allowing poison in splat vector constants.
288 return ap_match<APFloat>(Res, /* AllowPoison */ true);
289}
290
291/// Match APFloat while forbidding poison in splat vector constants.
293 return ap_match<APFloat>(Res, /* AllowPoison */ false);
294}
295
296template <int64_t Val> struct constantint_match {
297 template <typename ITy> bool match(ITy *V) const {
298 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
299 const APInt &CIV = CI->getValue();
300 if (Val >= 0)
301 return CIV == static_cast<uint64_t>(Val);
302 // If Val is negative, and CI is shorter than it, truncate to the right
303 // number of bits. If it is larger, then we have to sign extend. Just
304 // compare their negated values.
305 return -CIV == -Val;
306 }
307 return false;
308 }
309};
310
311/// Match a ConstantInt with a specific value.
312template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
313 return constantint_match<Val>();
314}
315
316/// This helper class is used to match constant scalars, vector splats,
317/// and fixed width vectors that satisfy a specified predicate.
318/// For fixed width vector constants, poison elements are ignored if AllowPoison
319/// is true.
320template <typename Predicate, typename ConstantVal, bool AllowPoison>
321struct cstval_pred_ty : public Predicate {
322private:
323 bool matchVector(const Value *V) const {
324 if (const auto *C = dyn_cast<Constant>(V)) {
325 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
326 return this->isValue(CV->getValue());
327
328 // Number of elements of a scalable vector unknown at compile time
329 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
330 if (!FVTy)
331 return false;
332
333 // Non-splat vector constant: check each element for a match.
334 unsigned NumElts = FVTy->getNumElements();
335 assert(NumElts != 0 && "Constant vector with no elements?");
336 bool HasNonPoisonElements = false;
337 for (unsigned i = 0; i != NumElts; ++i) {
338 Constant *Elt = C->getAggregateElement(i);
339 if (!Elt)
340 return false;
341 if (AllowPoison && isa<PoisonValue>(Elt))
342 continue;
343 auto *CV = dyn_cast<ConstantVal>(Elt);
344 if (!CV || !this->isValue(CV->getValue()))
345 return false;
346 HasNonPoisonElements = true;
347 }
348 return HasNonPoisonElements;
349 }
350 return false;
351 }
352
353public:
354 const Constant **Res = nullptr;
355 template <typename ITy> bool match_impl(ITy *V) const {
356 if (const auto *CV = dyn_cast<ConstantVal>(V))
357 return this->isValue(CV->getValue());
358 if (isa<VectorType>(V->getType()))
359 return matchVector(V);
360 return false;
361 }
362
363 template <typename ITy> bool match(ITy *V) const {
364 if (this->match_impl(V)) {
365 if (Res)
366 *Res = cast<Constant>(V);
367 return true;
368 }
369 return false;
370 }
371};
372
373/// specialization of cstval_pred_ty for ConstantInt
374template <typename Predicate, bool AllowPoison = true>
376
377/// specialization of cstval_pred_ty for ConstantFP
378template <typename Predicate>
380 /*AllowPoison=*/true>;
381
382/// This helper class is used to match scalar and vector constants that
383/// satisfy a specified predicate, and bind them to an APInt.
384template <typename Predicate> struct api_pred_ty : public Predicate {
385 const APInt *&Res;
386
387 api_pred_ty(const APInt *&R) : Res(R) {}
388
389 template <typename ITy> bool match(ITy *V) const {
390 if (const auto *CI = dyn_cast<ConstantInt>(V))
391 if (this->isValue(CI->getValue())) {
392 Res = &CI->getValue();
393 return true;
394 }
395 if (V->getType()->isVectorTy())
396 if (const auto *C = dyn_cast<Constant>(V))
397 if (auto *CI = dyn_cast_or_null<ConstantInt>(
398 C->getSplatValue(/*AllowPoison=*/true)))
399 if (this->isValue(CI->getValue())) {
400 Res = &CI->getValue();
401 return true;
402 }
403
404 return false;
405 }
406};
407
408/// This helper class is used to match scalar and vector constants that
409/// satisfy a specified predicate, and bind them to an APFloat.
410/// Poison is allowed in splat vector constants.
411template <typename Predicate> struct apf_pred_ty : public Predicate {
412 const APFloat *&Res;
413
414 apf_pred_ty(const APFloat *&R) : Res(R) {}
415
416 template <typename ITy> bool match(ITy *V) const {
417 if (const auto *CI = dyn_cast<ConstantFP>(V))
418 if (this->isValue(CI->getValue())) {
419 Res = &CI->getValue();
420 return true;
421 }
422 if (V->getType()->isVectorTy())
423 if (const auto *C = dyn_cast<Constant>(V))
424 if (auto *CI = dyn_cast_or_null<ConstantFP>(
425 C->getSplatValue(/* AllowPoison */ true)))
426 if (this->isValue(CI->getValue())) {
427 Res = &CI->getValue();
428 return true;
429 }
430
431 return false;
432 }
433};
434
435///////////////////////////////////////////////////////////////////////////////
436//
437// Encapsulate constant value queries for use in templated predicate matchers.
438// This allows checking if constants match using compound predicates and works
439// with vector constants, possibly with relaxed constraints. For example, ignore
440// undef values.
441//
442///////////////////////////////////////////////////////////////////////////////
443
444template <typename APTy> struct custom_checkfn {
445 function_ref<bool(const APTy &)> CheckFn;
446 bool isValue(const APTy &C) const { return CheckFn(C); }
447};
448
449/// Match an integer or vector where CheckFn(ele) for each element is true.
450/// For vectors, poison elements are assumed to match.
452m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
453 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
454}
455
457m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
458 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
459}
460
461/// Match a float or vector where CheckFn(ele) for each element is true.
462/// For vectors, poison elements are assumed to match.
464m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
465 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
466}
467
469m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
470 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
471}
472
474 bool isValue(const APInt &C) const { return true; }
475};
476/// Match an integer or vector with any integral constant.
477/// For vectors, this includes constants with undefined elements.
481
483 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
484};
485
489
491 bool isValue(const APInt &C) const { return C.isAllOnes(); }
492};
493/// Match an integer or vector with all bits set.
494/// For vectors, this includes constants with undefined elements.
498
502
503inline auto m_AllOnesOrPoison() { return m_CombineOr(m_AllOnes(), m_Poison()); }
504
506 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
507};
508/// Match an integer or vector with values having all bits except for the high
509/// bit set (0x7f...).
510/// For vectors, this includes constants with undefined elements.
515 return V;
516}
517
519 bool isValue(const APInt &C) const { return C.isNegative(); }
520};
521/// Match an integer or vector of negative values.
522/// For vectors, this includes constants with undefined elements.
526inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
527
529 bool isValue(const APInt &C) const { return C.isNonNegative(); }
530};
531/// Match an integer or vector of non-negative values.
532/// For vectors, this includes constants with undefined elements.
536inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
537
539 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
540};
541/// Match an integer or vector of strictly positive values.
542/// For vectors, this includes constants with undefined elements.
547 return V;
548}
549
551 bool isValue(const APInt &C) const { return C.isNonPositive(); }
552};
553/// Match an integer or vector of non-positive values.
554/// For vectors, this includes constants with undefined elements.
558inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
559
560struct is_one {
561 bool isValue(const APInt &C) const { return C.isOne(); }
562};
563/// Match an integer 1 or a vector with all elements equal to 1.
564/// For vectors, this includes constants with undefined elements.
566
568 bool isValue(const APInt &C) const { return C.isZero(); }
569};
570/// Match an integer 0 or a vector with all elements equal to 0.
571/// For vectors, this includes constants with undefined elements.
575
577 bool isValue(const APInt &C) const { return !C.isZero(); }
578};
579/// Match a non-zero integer or a vector with all non-zero elements.
580/// For vectors, this includes constants with undefined elements.
584
585struct is_zero {
586 template <typename ITy> bool match(ITy *V) const {
587 auto *C = dyn_cast<Constant>(V);
588 // FIXME: this should be able to do something for scalable vectors
589 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
590 }
591};
592/// Match any null constant or a vector with all elements equal to 0.
593/// For vectors, this includes constants with undefined elements.
594inline is_zero m_Zero() { return is_zero(); }
595
596inline auto m_ZeroOrPoison() { return m_CombineOr(m_Zero(), m_Poison()); }
597
598struct is_power2 {
599 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
600};
601/// Match an integer or vector power-of-2.
602/// For vectors, this includes constants with undefined elements.
604inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
605
607 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
608};
609/// Match a integer or vector negated power-of-2.
610/// For vectors, this includes constants with undefined elements.
615 return V;
616}
617
619 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
620};
621/// Match a integer or vector negated power-of-2.
622/// For vectors, this includes constants with undefined elements.
628 return V;
629}
630
632 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
633};
634/// Match an integer or vector of 0 or power-of-2 values.
635/// For vectors, this includes constants with undefined elements.
640 return V;
641}
642
644 bool isValue(const APInt &C) const { return C.isSignMask(); }
645};
646/// Match an integer or vector with only the sign bit(s) set.
647/// For vectors, this includes constants with undefined elements.
651
653 bool isValue(const APInt &C) const { return C.isMask(); }
654};
655/// Match an integer or vector with only the low bit(s) set.
656/// For vectors, this includes constants with undefined elements.
660inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
661
663 bool isValue(const APInt &C) const { return !C || C.isMask(); }
664};
665/// Match an integer or vector with only the low bit(s) set.
666/// For vectors, this includes constants with undefined elements.
671 return V;
672}
673
676 const APInt *Thr;
677 bool isValue(const APInt &C) const {
678 return ICmpInst::compare(C, *Thr, Pred);
679 }
680};
681/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
682/// to Threshold. For vectors, this includes constants with undefined elements.
686 P.Pred = Predicate;
687 P.Thr = &Threshold;
688 return P;
689}
690
691struct is_nan {
692 bool isValue(const APFloat &C) const { return C.isNaN(); }
693};
694/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
695/// For vectors, this includes constants with undefined elements.
697
698struct is_nonnan {
699 bool isValue(const APFloat &C) const { return !C.isNaN(); }
700};
701/// Match a non-NaN FP constant.
702/// For vectors, this includes constants with undefined elements.
706
707struct is_inf {
708 bool isValue(const APFloat &C) const { return C.isInfinity(); }
709};
710/// Match a positive or negative infinity FP constant.
711/// For vectors, this includes constants with undefined elements.
713
714template <bool IsNegative> struct is_signed_inf {
715 bool isValue(const APFloat &C) const {
716 return C.isInfinity() && IsNegative == C.isNegative();
717 }
718};
719
720/// Match a positive infinity FP constant.
721/// For vectors, this includes constants with undefined elements.
725
726/// Match a negative infinity FP constant.
727/// For vectors, this includes constants with undefined elements.
731
732struct is_noninf {
733 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
734};
735/// Match a non-infinity FP constant, i.e. finite or NaN.
736/// For vectors, this includes constants with undefined elements.
740
741struct is_finite {
742 bool isValue(const APFloat &C) const { return C.isFinite(); }
743};
744/// Match a finite FP constant, i.e. not infinity or NaN.
745/// For vectors, this includes constants with undefined elements.
749inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
750
752 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
753};
754/// Match a finite non-zero FP constant.
755/// For vectors, this includes constants with undefined elements.
760 return V;
761}
762
764 bool isValue(const APFloat &C) const { return C.isZero(); }
765};
766/// Match a floating-point negative zero or positive zero.
767/// For vectors, this includes constants with undefined elements.
771
773 bool isValue(const APFloat &C) const { return C.isPosZero(); }
774};
775/// Match a floating-point positive zero.
776/// For vectors, this includes constants with undefined elements.
780
782 bool isValue(const APFloat &C) const { return C.isNegZero(); }
783};
784/// Match a floating-point negative zero.
785/// For vectors, this includes constants with undefined elements.
789
791 bool isValue(const APFloat &C) const { return C.isNonZero(); }
792};
793/// Match a floating-point non-zero.
794/// For vectors, this includes constants with undefined elements.
798
800 bool isValue(const APFloat &C) const {
801 return !C.isDenormal() && C.isNonZero();
802 }
803};
804
805/// Match a floating-point non-zero that is not a denormal.
806/// For vectors, this includes constants with undefined elements.
810
811///////////////////////////////////////////////////////////////////////////////
812
813/// Match a value, capturing it if we match.
814inline match_bind<Value> m_Value(Value *&V) { return V; }
815inline match_bind<const Value> m_Value(const Value *&V) { return V; }
816
817/// Match against the nested pattern, and capture the value if we match.
818template <typename Pattern> inline auto m_Value(Value *&V, const Pattern &P) {
819 return m_CombineAnd(P, match_bind<Value>(V));
820}
821
822/// Match against the nested pattern, and capture the value if we match.
823template <typename Pattern>
824inline auto m_Value(const Value *&V, const Pattern &P) {
826}
827
828/// Match an instruction, capturing it if we match.
831 return I;
832}
833
834/// Match against the nested pattern, and capture the instruction if we match.
835template <typename Pattern>
836inline auto m_Instruction(Instruction *&I, const Pattern &P) {
838}
839template <typename Pattern>
840inline auto m_Instruction(const Instruction *&I, const Pattern &P) {
842}
843
844/// Match a unary operator, capturing it if we match.
847 return I;
848}
849/// Match a binary operator, capturing it if we match.
852 return I;
853}
854/// Match any intrinsic call, capturing it if we match.
859/// Match a with overflow intrinsic, capturing it if we match.
865 return I;
866}
867
868/// Match a PHI node, capturing it if we match.
869inline match_bind<PHINode> m_Phi(PHINode *&PN) { return PN; }
870
871/// Match an UndefValue, capturing the value if we match.
873
874/// Match a Constant, capturing the value if we match.
876
877/// Match a ConstantInt, capturing the value if we match.
879
880/// Match a ConstantFP, capturing the value if we match.
882
883/// Match a ConstantExpr, capturing the value if we match.
885
886/// Match a basic block value, capturing it if we match.
889 return V;
890}
891
892// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
893// and use m_Unless(m_ConstantExpr).
895 template <typename ITy> static bool isImmConstant(ITy *V) {
896 if (auto *CV = dyn_cast<Constant>(V)) {
897 if (!match(CV, m_ConstantExpr()))
898 return true;
899
900 if (CV->getType()->isVectorTy()) {
901 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
902 if (!match(Splat, m_ConstantExpr())) {
903 return true;
904 }
905 }
906 }
907 }
908 return false;
909 }
910};
911
913 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
914};
915
916/// Match an arbitrary immediate Constant and ignore it.
918
921
923
924 template <typename ITy> bool match(ITy *V) const {
925 if (isImmConstant(V)) {
926 VR = cast<Constant>(V);
927 return true;
928 }
929 return false;
930 }
931};
932
933/// Match an immediate Constant, capturing the value if we match.
937
938/// Matcher for specified Value*.
940 const Value *Val;
941
942 specificval_ty(const Value *V) : Val(V) {}
943
944 template <typename ITy> bool match(ITy *V) const { return V == Val; }
945};
946
947/// Match if we have a specific specified value.
948inline specificval_ty m_Specific(const Value *V) { return V; }
949
950/// Like m_Specific(), but works if the specific value to match is determined
951/// as part of the same match() expression. For example:
952/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
953/// bind X before the pattern match starts.
954/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
955/// whichever value m_Value(X) populated.
956inline match_deferred<Value> m_Deferred(Value *const &V) { return V; }
958 return V;
959}
960
961/// Match a specified floating point value or vector of all elements of
962/// that value.
964 double Val;
965
966 specific_fpval(double V) : Val(V) {}
967
968 template <typename ITy> bool match(ITy *V) const {
969 if (const auto *CFP = dyn_cast<ConstantFP>(V))
970 return CFP->isExactlyValue(Val);
971 if (V->getType()->isVectorTy())
972 if (const auto *C = dyn_cast<Constant>(V))
973 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
974 return CFP->isExactlyValue(Val);
975 return false;
976 }
977};
978
979/// Match a specific floating point value or vector with all elements
980/// equal to the value.
981inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
982
983/// Match a float 1.0 or vector with all elements equal to 1.0.
984inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
985
988
990
991 template <typename ITy> bool match(ITy *V) const {
992 const APInt *ConstInt;
993 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
994 return false;
995 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
996 if (!ZExtVal)
997 return false;
998 VR = *ZExtVal;
999 return true;
1000 }
1001};
1002
1003/// Match a specified integer value or vector of all elements of that
1004/// value.
1005template <bool AllowPoison> struct specific_intval {
1006 const APInt &Val;
1007
1008 specific_intval(const APInt &V) : Val(V) {}
1009
1010 template <typename ITy> bool match(ITy *V) const {
1011 const auto *CI = dyn_cast<ConstantInt>(V);
1012 if (!CI && V->getType()->isVectorTy())
1013 if (const auto *C = dyn_cast<Constant>(V))
1014 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1015
1016 return CI && APInt::isSameValue(CI->getValue(), Val);
1017 }
1018};
1019
1020template <bool AllowPoison> struct specific_intval64 {
1022
1024
1025 template <typename ITy> bool match(ITy *V) const {
1026 const auto *CI = dyn_cast<ConstantInt>(V);
1027 if (!CI && V->getType()->isVectorTy())
1028 if (const auto *C = dyn_cast<Constant>(V))
1029 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1030
1031 return CI && CI->getValue() == Val;
1032 }
1033};
1034
1035/// Match a specific integer value or vector with all elements equal to
1036/// the value.
1038 return specific_intval<false>(V);
1039}
1040
1044
1048
1052
1053/// Match a ConstantInt and bind to its value. This does not match
1054/// ConstantInts wider than 64-bits.
1056
1057/// Match a specified basic block value.
1060
1062
1063 template <typename ITy> bool match(ITy *V) const {
1064 const auto *BB = dyn_cast<BasicBlock>(V);
1065 return BB && BB == Val;
1066 }
1067};
1068
1069/// Match a specific basic block value.
1071 return specific_bbval(BB);
1072}
1073
1074/// A commutative-friendly version of m_Specific().
1076 return BB;
1077}
1079m_Deferred(const BasicBlock *const &BB) {
1080 return BB;
1081}
1082
1083template <typename Pattern> struct SpecificType_match {
1086
1088
1089 template <typename ITy> bool match(ITy *V) const {
1090 return V->getType() == RefTy && P.match(V);
1091 }
1092};
1093
1094// Explicit deduction guide.
1095template <typename Pattern>
1098
1099/// Match a value of a specific type.
1100template <typename Pattern>
1101inline auto m_SpecificType(Type *RefTy, const Pattern &P) {
1102 return SpecificType_match<Pattern>(RefTy, P);
1103}
1104inline auto m_SpecificType(Type *RefTy) {
1105 return m_SpecificType(RefTy, m_Value());
1106}
1107
1108/// Match a value of a specific type, capturing it if we match.
1109inline auto m_SpecificType(Type *RefTy, Value *&V) {
1110 return m_SpecificType(RefTy, m_Value(V));
1111}
1112inline auto m_SpecificType(Type *RefTy, const Value *&V) {
1113 return m_SpecificType(RefTy, m_Value(V));
1114}
1115
1116//===----------------------------------------------------------------------===//
1117// Matcher for any binary operator.
1118//
1119template <typename LHS_t, typename RHS_t, bool Commutable = false>
1123
1124 // The evaluation order is always stable, regardless of Commutability.
1125 // The LHS is always matched first.
1126 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1127
1128 template <typename OpTy> bool match(OpTy *V) const {
1129 if (auto *I = dyn_cast<BinaryOperator>(V))
1130 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1131 (Commutable && L.match(I->getOperand(1)) &&
1132 R.match(I->getOperand(0)));
1133 return false;
1134 }
1135};
1136
1137template <typename LHS, typename RHS>
1138inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1139 return AnyBinaryOp_match<LHS, RHS>(L, R);
1140}
1141
1142//===----------------------------------------------------------------------===//
1143// Matcher for any unary operator.
1144// TODO fuse unary, binary matcher into n-ary matcher
1145//
1146template <typename OP_t> struct AnyUnaryOp_match {
1147 OP_t X;
1148
1149 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1150
1151 template <typename OpTy> bool match(OpTy *V) const {
1152 if (auto *I = dyn_cast<UnaryOperator>(V))
1153 return X.match(I->getOperand(0));
1154 return false;
1155 }
1156};
1157
1158template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1159 return AnyUnaryOp_match<OP_t>(X);
1160}
1161
1162//===----------------------------------------------------------------------===//
1163// Matchers for specific binary operators.
1164//
1165
1166template <typename LHS_t, typename RHS_t, unsigned Opcode,
1167 bool Commutable = false>
1171
1172 // The evaluation order is always stable, regardless of Commutability.
1173 // The LHS is always matched first.
1174 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1175
1176 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1177 if (V->getValueID() == Value::InstructionVal + Opc) {
1178 auto *I = cast<BinaryOperator>(V);
1179 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1180 (Commutable && L.match(I->getOperand(1)) &&
1181 R.match(I->getOperand(0)));
1182 }
1183 return false;
1184 }
1185
1186 template <typename OpTy> bool match(OpTy *V) const {
1187 return match(Opcode, V);
1188 }
1189};
1190
1191template <typename LHS, typename RHS>
1196
1197template <typename LHS, typename RHS>
1202
1203template <typename LHS, typename RHS>
1208
1209template <typename LHS, typename RHS>
1214
1215template <typename Op_t> struct FNeg_match {
1216 Op_t X;
1217
1218 FNeg_match(const Op_t &Op) : X(Op) {}
1219 template <typename OpTy> bool match(OpTy *V) const {
1220 auto *FPMO = dyn_cast<FPMathOperator>(V);
1221 if (!FPMO)
1222 return false;
1223
1224 if (FPMO->getOpcode() == Instruction::FNeg)
1225 return X.match(FPMO->getOperand(0));
1226
1227 if (FPMO->getOpcode() == Instruction::FSub) {
1228 if (FPMO->hasNoSignedZeros()) {
1229 // With 'nsz', any zero goes.
1230 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1231 return false;
1232 } else {
1233 // Without 'nsz', we need fsub -0.0, X exactly.
1234 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1235 return false;
1236 }
1237
1238 return X.match(FPMO->getOperand(1));
1239 }
1240
1241 return false;
1242 }
1243};
1244
1245/// Match 'fneg X' as 'fsub -0.0, X'.
1246template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1247 return FNeg_match<OpTy>(X);
1248}
1249
1250/// Match 'fneg X' as 'fsub +-0.0, X'.
1251template <typename RHS>
1252inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1253m_FNegNSZ(const RHS &X) {
1254 return m_FSub(m_AnyZeroFP(), X);
1255}
1256
1257template <typename LHS, typename RHS>
1262
1263template <typename LHS, typename RHS>
1268
1269template <typename LHS, typename RHS>
1274
1275template <typename LHS, typename RHS>
1280
1281template <typename LHS, typename RHS>
1286
1287template <typename LHS, typename RHS>
1292
1293template <typename LHS, typename RHS>
1298
1299template <typename LHS, typename RHS>
1304
1305template <typename LHS, typename RHS>
1310
1311template <typename LHS, typename RHS>
1316
1317template <typename LHS, typename RHS>
1322
1323template <typename LHS, typename RHS>
1328
1329template <typename LHS, typename RHS>
1334
1335template <typename LHS, typename RHS>
1340
1341template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1344
1346
1347 template <typename OpTy> bool match(OpTy *V) const {
1348 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1349 if (Op->getOpcode() == Opcode)
1350 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1351 L.match(Op->getOperand(0));
1352 }
1353 // Interpreted as shiftop V, 0
1354 R = 0;
1355 return L.match(V);
1356 }
1357};
1358
1359/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1360template <typename LHS>
1365
1366/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1367template <typename LHS>
1372
1373/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1374template <typename LHS>
1379
1380template <typename LHS_t, typename RHS_t, unsigned Opcode,
1381 unsigned WrapFlags = 0, bool Commutable = false>
1385
1387 : L(LHS), R(RHS) {}
1388
1389 template <typename OpTy> bool match(OpTy *V) const {
1390 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1391 if (Op->getOpcode() != Opcode)
1392 return false;
1394 !Op->hasNoUnsignedWrap())
1395 return false;
1396 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1397 !Op->hasNoSignedWrap())
1398 return false;
1399 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1400 (Commutable && L.match(Op->getOperand(1)) &&
1401 R.match(Op->getOperand(0)));
1402 }
1403 return false;
1404 }
1405};
1406
1407template <typename LHS, typename RHS>
1408inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1410m_NSWAdd(const LHS &L, const RHS &R) {
1411 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1413 R);
1414}
1415template <typename LHS, typename RHS>
1416inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1418m_c_NSWAdd(const LHS &L, const RHS &R) {
1419 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1421 true>(L, R);
1422}
1423template <typename LHS, typename RHS>
1424inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1426m_NSWSub(const LHS &L, const RHS &R) {
1427 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1429 R);
1430}
1431template <typename LHS, typename RHS>
1432inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1434m_NSWMul(const LHS &L, const RHS &R) {
1435 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1437 R);
1438}
1439template <typename LHS, typename RHS>
1440inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1442m_NSWShl(const LHS &L, const RHS &R) {
1443 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1445 R);
1446}
1447
1448template <typename LHS, typename RHS>
1449inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1451m_NUWAdd(const LHS &L, const RHS &R) {
1452 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1454 L, R);
1455}
1456
1457template <typename LHS, typename RHS>
1459 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1460m_c_NUWAdd(const LHS &L, const RHS &R) {
1461 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1463 true>(L, R);
1464}
1465
1466template <typename LHS, typename RHS>
1467inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1469m_NUWSub(const LHS &L, const RHS &R) {
1470 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1472 L, R);
1473}
1474template <typename LHS, typename RHS>
1475inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1477m_NUWMul(const LHS &L, const RHS &R) {
1478 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1480 L, R);
1481}
1482template <typename LHS, typename RHS>
1483inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1485m_NUWShl(const LHS &L, const RHS &R) {
1486 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1488 L, R);
1489}
1490
1491template <typename LHS_t, typename RHS_t, bool Commutable = false>
1493 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1494 unsigned Opcode;
1495
1497 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1498
1499 template <typename OpTy> bool match(OpTy *V) const {
1501 }
1502};
1503
1504/// Matches a specific opcode.
1505template <typename LHS, typename RHS>
1506inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1507 const RHS &R) {
1508 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1509}
1510
1511template <typename LHS, typename RHS, bool Commutable = false>
1515
1516 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1517
1518 template <typename OpTy> bool match(OpTy *V) const {
1519 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1520 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1521 if (!PDI->isDisjoint())
1522 return false;
1523 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1524 (Commutable && L.match(PDI->getOperand(1)) &&
1525 R.match(PDI->getOperand(0)));
1526 }
1527 return false;
1528 }
1529};
1530
1531template <typename LHS, typename RHS>
1533 return DisjointOr_match<LHS, RHS>(L, R);
1534}
1535
1536template <typename LHS, typename RHS>
1538 const RHS &R) {
1540}
1541
1542/// Match either "add" or "or disjoint".
1543template <typename LHS, typename RHS>
1546m_AddLike(const LHS &L, const RHS &R) {
1547 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1548}
1549
1550/// Match either "add nsw" or "or disjoint"
1551template <typename LHS, typename RHS>
1552inline match_combine_or<
1553 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1556m_NSWAddLike(const LHS &L, const RHS &R) {
1557 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1558}
1559
1560/// Match either "add nuw" or "or disjoint"
1561template <typename LHS, typename RHS>
1562inline match_combine_or<
1563 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1566m_NUWAddLike(const LHS &L, const RHS &R) {
1567 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1568}
1569
1570template <typename LHS, typename RHS>
1574
1575 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1576
1577 template <typename OpTy> bool match(OpTy *V) const {
1578 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1579 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1580 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1581 ; // Pass
1582 else if (Op->getOpcode() != Instruction::Xor)
1583 return false;
1584 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1585 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1586 }
1587 return false;
1588 }
1589};
1590
1591/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1592/// Only commutative matcher as the `sub` will need to swap the L and R.
1593template <typename LHS, typename RHS>
1594inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1595 return XorLike_match<LHS, RHS>(L, R);
1596}
1597
1598//===----------------------------------------------------------------------===//
1599// Class that matches a group of binary opcodes.
1600//
1601template <typename LHS_t, typename RHS_t, typename Predicate,
1602 bool Commutable = false>
1603struct BinOpPred_match : Predicate {
1606
1607 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1608
1609 template <typename OpTy> bool match(OpTy *V) const {
1610 if (auto *I = dyn_cast<Instruction>(V))
1611 return this->isOpType(I->getOpcode()) &&
1612 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1613 (Commutable && L.match(I->getOperand(1)) &&
1614 R.match(I->getOperand(0))));
1615 return false;
1616 }
1617};
1618
1620 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1621};
1622
1624 bool isOpType(unsigned Opcode) const {
1625 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1626 }
1627};
1628
1630 bool isOpType(unsigned Opcode) const {
1631 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1632 }
1633};
1634
1636 bool isOpType(unsigned Opcode) const {
1637 return Instruction::isBitwiseLogicOp(Opcode);
1638 }
1639};
1640
1642 bool isOpType(unsigned Opcode) const {
1643 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1644 }
1645};
1646
1648 bool isOpType(unsigned Opcode) const {
1649 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1650 }
1651};
1652
1653/// Matches shift operations.
1654template <typename LHS, typename RHS>
1656 const RHS &R) {
1658}
1659
1660/// Matches logical shift operations.
1661template <typename LHS, typename RHS>
1666
1667/// Matches logical shift operations.
1668template <typename LHS, typename RHS>
1670m_LogicalShift(const LHS &L, const RHS &R) {
1672}
1673
1674/// Matches bitwise logic operations.
1675template <typename LHS, typename RHS>
1677m_BitwiseLogic(const LHS &L, const RHS &R) {
1679}
1680
1681/// Matches bitwise logic operations in either order.
1682template <typename LHS, typename RHS>
1687
1688/// Matches integer division operations.
1689template <typename LHS, typename RHS>
1691 const RHS &R) {
1693}
1694
1695/// Matches integer remainder operations.
1696template <typename LHS, typename RHS>
1698 const RHS &R) {
1700}
1701
1702//===----------------------------------------------------------------------===//
1703// Class that matches exact binary ops.
1704//
1705template <typename SubPattern_t> struct Exact_match {
1706 SubPattern_t SubPattern;
1707
1708 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1709
1710 template <typename OpTy> bool match(OpTy *V) const {
1711 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1712 return PEO->isExact() && SubPattern.match(V);
1713 return false;
1714 }
1715};
1716
1717template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1718 return SubPattern;
1719}
1720
1721//===----------------------------------------------------------------------===//
1722// Matchers for CmpInst classes
1723//
1724
1725template <typename LHS_t, typename RHS_t, typename Class,
1726 bool Commutable = false>
1731
1732 // The evaluation order is always stable, regardless of Commutability.
1733 // The LHS is always matched first.
1735 : Predicate(&Pred), L(LHS), R(RHS) {}
1737 : Predicate(nullptr), L(LHS), R(RHS) {}
1738
1739 template <typename OpTy> bool match(OpTy *V) const {
1740 if (auto *I = dyn_cast<Class>(V)) {
1741 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1742 if (Predicate)
1744 return true;
1745 }
1746 if (Commutable && L.match(I->getOperand(1)) &&
1747 R.match(I->getOperand(0))) {
1748 if (Predicate)
1750 return true;
1751 }
1752 }
1753 return false;
1754 }
1755};
1756
1757template <typename LHS, typename RHS>
1759 const RHS &R) {
1760 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1761}
1762
1763template <typename LHS, typename RHS>
1765 const LHS &L, const RHS &R) {
1766 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1767}
1768
1769template <typename LHS, typename RHS>
1771 const LHS &L, const RHS &R) {
1772 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1773}
1774
1775template <typename LHS, typename RHS>
1778}
1779
1780template <typename LHS, typename RHS>
1783}
1784
1785template <typename LHS, typename RHS>
1788}
1789
1790// Same as CmpClass, but instead of saving Pred as out output variable, match a
1791// specific input pred for equality.
1792template <typename LHS_t, typename RHS_t, typename Class,
1793 bool Commutable = false>
1798
1800 : Predicate(Pred), L(LHS), R(RHS) {}
1801
1802 template <typename OpTy> bool match(OpTy *V) const {
1803 if (auto *I = dyn_cast<Class>(V)) {
1805 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1806 return true;
1807 if constexpr (Commutable) {
1810 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1811 return true;
1812 }
1813 }
1814
1815 return false;
1816 }
1817};
1818
1819template <typename LHS, typename RHS>
1821m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1822 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1823}
1824
1825template <typename LHS, typename RHS>
1827m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1828 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1829}
1830
1831template <typename LHS, typename RHS>
1833m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1835}
1836
1837template <typename LHS, typename RHS>
1839m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1840 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1841}
1842
1843//===----------------------------------------------------------------------===//
1844// Matchers for instructions with a given opcode and number of operands.
1845//
1846
1847/// Matches instructions with Opcode and three operands.
1848template <typename T0, unsigned Opcode> struct OneOps_match {
1850
1851 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1852
1853 template <typename OpTy> bool match(OpTy *V) const {
1854 if (V->getValueID() == Value::InstructionVal + Opcode) {
1855 auto *I = cast<Instruction>(V);
1856 return Op1.match(I->getOperand(0));
1857 }
1858 return false;
1859 }
1860};
1861
1862/// Matches instructions with Opcode and three operands.
1863template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1866
1867 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1868
1869 template <typename OpTy> bool match(OpTy *V) const {
1870 if (V->getValueID() == Value::InstructionVal + Opcode) {
1871 auto *I = cast<Instruction>(V);
1872 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1873 }
1874 return false;
1875 }
1876};
1877
1878/// Matches instructions with Opcode and three operands.
1879template <typename T0, typename T1, typename T2, unsigned Opcode,
1880 bool CommutableOp2Op3 = false>
1885
1886 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1887 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1888
1889 template <typename OpTy> bool match(OpTy *V) const {
1890 if (V->getValueID() == Value::InstructionVal + Opcode) {
1891 auto *I = cast<Instruction>(V);
1892 if (!Op1.match(I->getOperand(0)))
1893 return false;
1894 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1895 return true;
1896 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1897 Op3.match(I->getOperand(1));
1898 }
1899 return false;
1900 }
1901};
1902
1903/// Matches instructions with Opcode and any number of operands
1904template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1905 std::tuple<OperandTypes...> Operands;
1906
1907 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1908
1909 // Operand matching works by recursively calling match_operands, matching the
1910 // operands left to right. The first version is called for each operand but
1911 // the last, for which the second version is called. The second version of
1912 // match_operands is also used to match each individual operand.
1913 template <int Idx, int Last>
1914 std::enable_if_t<Idx != Last, bool>
1918
1919 template <int Idx, int Last>
1920 std::enable_if_t<Idx == Last, bool>
1922 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1923 }
1924
1925 template <typename OpTy> bool match(OpTy *V) const {
1926 if (V->getValueID() == Value::InstructionVal + Opcode) {
1927 auto *I = cast<Instruction>(V);
1928 return I->getNumOperands() == sizeof...(OperandTypes) &&
1929 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1930 }
1931 return false;
1932 }
1933};
1934
1935/// Matches SelectInst.
1936template <typename Cond, typename LHS, typename RHS>
1938m_Select(const Cond &C, const LHS &L, const RHS &R) {
1940}
1941
1942/// This matches a select of two constants, e.g.:
1943/// m_SelectCst<-1, 0>(m_Value(V))
1944template <int64_t L, int64_t R, typename Cond>
1946 Instruction::Select>
1949}
1950
1951/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1952template <typename LHS, typename RHS>
1953inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1954m_c_Select(const LHS &L, const RHS &R) {
1955 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1956 true>(m_Value(), L, R);
1957}
1958
1959/// Matches FreezeInst.
1960template <typename OpTy>
1964
1965/// Matches InsertElementInst.
1966template <typename Val_t, typename Elt_t, typename Idx_t>
1968m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1970 Val, Elt, Idx);
1971}
1972
1973/// Matches ExtractElementInst.
1974template <typename Val_t, typename Idx_t>
1976m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1978}
1979
1980/// Matches shuffle.
1981template <typename T0, typename T1, typename T2> struct Shuffle_match {
1985
1986 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1987 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1988
1989 template <typename OpTy> bool match(OpTy *V) const {
1990 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1991 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1992 Mask.match(I->getShuffleMask());
1993 }
1994 return false;
1995 }
1996};
1997
1998struct m_Mask {
2001 bool match(ArrayRef<int> Mask) const {
2002 MaskRef = Mask;
2003 return true;
2004 }
2005};
2006
2008 bool match(ArrayRef<int> Mask) const {
2009 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2010 }
2011};
2012
2016 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2017};
2018
2020 bool match(ArrayRef<int> Mask) const { return all_equal(Mask); }
2021};
2022
2026 bool match(ArrayRef<int> Mask) const {
2027 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2028 if (First == Mask.end())
2029 return false;
2030 SplatIndex = *First;
2031 return all_of(Mask,
2032 [First](int Elem) { return Elem == *First || Elem == -1; });
2033 }
2034};
2035
2036template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2037 PointerOpTy PointerOp;
2038 OffsetOpTy OffsetOp;
2039
2040 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2042
2043 template <typename OpTy> bool match(OpTy *V) const {
2044 auto *GEP = dyn_cast<GEPOperator>(V);
2045 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2046 PointerOp.match(GEP->getPointerOperand()) &&
2047 OffsetOp.match(GEP->idx_begin()->get());
2048 }
2049};
2050
2051/// Matches ShuffleVectorInst independently of mask value.
2052template <typename V1_t, typename V2_t>
2054m_Shuffle(const V1_t &v1, const V2_t &v2) {
2056}
2057
2058template <typename V1_t, typename V2_t, typename Mask_t>
2060m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2062}
2063
2064/// Matches LoadInst.
2065template <typename OpTy>
2069
2070/// Matches a simple (non-volatile, non-atomic) LoadInst.
2071template <typename OpTy> struct LoadSimple_match {
2073
2075
2076 template <typename ITy> bool match(ITy *V) const {
2077 return Base.match(V) && cast<LoadInst>(V)->isSimple();
2078 }
2079};
2080
2081template <typename OpTy>
2085
2086/// Matches StoreInst.
2087template <typename ValueOpTy, typename PointerOpTy>
2089m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2091 PointerOp);
2092}
2093
2094/// Matches GetElementPtrInst.
2095template <typename... OperandTypes>
2096inline auto m_GEP(const OperandTypes &...Ops) {
2097 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2098}
2099
2100/// Matches GEP with i8 source element type
2101template <typename PointerOpTy, typename OffsetOpTy>
2103m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2105}
2106
2107//===----------------------------------------------------------------------===//
2108// Matchers for CastInst classes
2109//
2110
2111template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2112 Op_t Op;
2113
2114 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2115
2116 template <typename OpTy> bool match(OpTy *V) const {
2117 if (auto *O = dyn_cast<Operator>(V))
2118 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2119 return false;
2120 }
2121};
2122
2123template <typename Op_t, typename Class> struct CastInst_match {
2124 Op_t Op;
2125
2126 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2127
2128 template <typename OpTy> bool match(OpTy *V) const {
2129 if (auto *I = dyn_cast<Class>(V))
2130 return Op.match(I->getOperand(0));
2131 return false;
2132 }
2133};
2134
2135template <typename Op_t> struct PtrToIntSameSize_match {
2137 Op_t Op;
2138
2139 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2140 : DL(DL), Op(OpMatch) {}
2141
2142 template <typename OpTy> bool match(OpTy *V) const {
2143 if (auto *O = dyn_cast<Operator>(V))
2144 return O->getOpcode() == Instruction::PtrToInt &&
2145 DL.getTypeSizeInBits(O->getType()) ==
2146 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2147 Op.match(O->getOperand(0));
2148 return false;
2149 }
2150};
2151
2152template <typename Op_t> struct NNegZExt_match {
2153 Op_t Op;
2154
2155 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2156
2157 template <typename OpTy> bool match(OpTy *V) const {
2158 if (auto *I = dyn_cast<ZExtInst>(V))
2159 return I->hasNonNeg() && Op.match(I->getOperand(0));
2160 return false;
2161 }
2162};
2163
2164template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2165 Op_t Op;
2166
2167 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2168
2169 template <typename OpTy> bool match(OpTy *V) const {
2170 if (auto *I = dyn_cast<TruncInst>(V))
2171 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2172 Op.match(I->getOperand(0));
2173 return false;
2174 }
2175};
2176
2177/// Matches BitCast.
2178template <typename OpTy>
2183
2184template <typename Op_t> struct ElementWiseBitCast_match {
2185 Op_t Op;
2186
2187 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2188
2189 template <typename OpTy> bool match(OpTy *V) const {
2190 auto *I = dyn_cast<BitCastInst>(V);
2191 if (!I)
2192 return false;
2193 Type *SrcType = I->getSrcTy();
2194 Type *DstType = I->getType();
2195 // Make sure the bitcast doesn't change between scalar and vector and
2196 // doesn't change the number of vector elements.
2197 if (SrcType->isVectorTy() != DstType->isVectorTy())
2198 return false;
2199 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2200 SrcVecTy && SrcVecTy->getElementCount() !=
2201 cast<VectorType>(DstType)->getElementCount())
2202 return false;
2203 return Op.match(I->getOperand(0));
2204 }
2205};
2206
2207template <typename OpTy>
2211
2212/// Matches PtrToInt.
2213template <typename OpTy>
2218
2219template <typename OpTy>
2224
2225/// Matches PtrToAddr.
2226template <typename OpTy>
2231
2232/// Matches PtrToInt or PtrToAddr.
2233template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2235}
2236
2237/// Matches IntToPtr.
2238template <typename OpTy>
2243
2244/// Matches any cast or self. Used to ignore casts.
2245template <typename OpTy>
2250
2251/// Matches Trunc.
2252template <typename OpTy>
2256
2257/// Matches trunc nuw.
2258template <typename OpTy>
2263
2264/// Matches trunc nsw.
2265template <typename OpTy>
2270
2271template <typename OpTy>
2274 return m_CombineOr(m_Trunc(Op), Op);
2275}
2276
2277/// Matches SExt.
2278template <typename OpTy>
2282
2283/// Matches ZExt.
2284template <typename OpTy>
2288
2289template <typename OpTy>
2291 return NNegZExt_match<OpTy>(Op);
2292}
2293
2294template <typename OpTy>
2297 return m_CombineOr(m_ZExt(Op), Op);
2298}
2299
2300template <typename OpTy>
2303 return m_CombineOr(m_SExt(Op), Op);
2304}
2305
2306/// Match either "sext" or "zext nneg".
2307template <typename OpTy>
2310 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2311}
2312
2313template <typename OpTy>
2317 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2318}
2319
2320template <typename OpTy>
2323 OpTy>
2325 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2326}
2327
2328template <typename OpTy> inline auto m_ZExtOrTruncOrSelf(const OpTy &Op) {
2329 return m_CombineOr(m_ZExt(Op), m_Trunc(Op), Op);
2330}
2331
2332template <typename LHS_t, typename RHS_t> struct ICmpLike_match {
2336
2338 : Pred(P), L(Left), R(Right) {}
2339
2340 template <typename OpTy> bool match(OpTy *V) const {
2341 if (PatternMatch::match(V, m_ICmp(Pred, L, R)))
2342 return true;
2343 Value *A;
2344 // trunc nuw x to i1 is equivalent to icmp ne x, 0
2345 if (V->getType()->isIntOrIntVectorTy(1) &&
2346 PatternMatch::match(V, m_NUWTrunc(m_Value(A))) && L.match(A) &&
2347 R.match(ConstantInt::getNullValue(A->getType()))) {
2349 return true;
2350 }
2351 return false;
2352 }
2353};
2354
2355template <typename LHS, typename RHS>
2357 const RHS &R) {
2358 return ICmpLike_match<LHS, RHS>(Pred, L, R);
2359}
2360
2361template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2362 CondTy Cond;
2365
2366 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2367 : Cond(C), TrueC(TC), FalseC(FC) {}
2368
2369 template <typename OpTy> bool match(OpTy *V) const {
2370 // select(Cond, TrueC, FalseC) — captures both constants directly
2372 return true;
2373
2374 Type *Ty = V->getType();
2375 Value *CondV = nullptr;
2376
2377 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2378 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2379 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2380 TrueC.match(ConstantInt::get(Ty, 1)) &&
2381 FalseC.match(ConstantInt::get(Ty, 0)))
2382 return true;
2383
2384 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2385 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2386 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2387 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2388 FalseC.match(ConstantInt::get(Ty, 0)))
2389 return true;
2390
2391 return false;
2392 }
2393};
2394
2395/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2396/// select i1 Cond, TrueC, FalseC
2397/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2398/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2399///
2400/// The condition is matched against \p Cond, and the true/false constants
2401/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2402/// constants are bound to \p TrueC and \p FalseC via their matchers.
2403template <typename CondTy, typename LTy, typename RTy>
2405m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2406 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2407}
2408
2409template <typename OpTy>
2413
2414template <typename OpTy>
2418
2419template <typename OpTy>
2422m_IToFP(const OpTy &Op) {
2423 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2424}
2425
2426template <typename OpTy>
2430
2431template <typename OpTy>
2435
2436template <typename OpTy>
2439m_FPToI(const OpTy &Op) {
2440 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2441}
2442
2443template <typename OpTy>
2447
2448template <typename OpTy>
2452
2453//===----------------------------------------------------------------------===//
2454// Matchers for control flow.
2455//
2456
2457struct br_match {
2459
2461
2462 template <typename OpTy> bool match(OpTy *V) const {
2463 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2464 Succ = BI->getSuccessor();
2465 return true;
2466 }
2467 return false;
2468 }
2469};
2470
2471inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2472
2473template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2475 Cond_t Cond;
2476 TrueBlock_t T;
2477 FalseBlock_t F;
2478
2479 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2480 : Cond(C), T(t), F(f) {}
2481
2482 template <typename OpTy> bool match(OpTy *V) const {
2483 if (auto *BI = dyn_cast<CondBrInst>(V))
2484 if (Cond.match(BI->getCondition()))
2485 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2486 return false;
2487 }
2488};
2489
2490template <typename Cond_t>
2496
2497template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2499m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2501}
2502
2503//===----------------------------------------------------------------------===//
2504// Matchers for fmax/fmin idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2505//
2506
2507template <typename LHS_t, typename RHS_t, typename Pred_t>
2509 using PredType = Pred_t;
2512
2513 // The evaluation order is always stable, regardless of Commutability.
2514 // The LHS is always matched first.
2515 FMaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2516
2517 template <typename OpTy> bool match(OpTy *V) const {
2518 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2519 auto *SI = dyn_cast<SelectInst>(V);
2520 if (!SI)
2521 return false;
2522 auto *Cmp = dyn_cast<FCmpInst>(SI->getCondition());
2523 if (!Cmp)
2524 return false;
2525 // At this point we have a select conditioned on a comparison. Check that
2526 // it is the values returned by the select that are being compared.
2527 auto *TrueVal = SI->getTrueValue();
2528 auto *FalseVal = SI->getFalseValue();
2529 auto *LHS = Cmp->getOperand(0);
2530 auto *RHS = Cmp->getOperand(1);
2531 if ((TrueVal != LHS || FalseVal != RHS) &&
2532 (TrueVal != RHS || FalseVal != LHS))
2533 return false;
2534 FCmpInst::Predicate Pred =
2535 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2536 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2537 if (!Pred_t::match(Pred))
2538 return false;
2539 // It does! Bind the operands.
2540 return L.match(LHS) && R.match(RHS);
2541 }
2542};
2543
2544/// Helper class for identifying ordered max predicates.
2546 static bool match(FCmpInst::Predicate Pred) {
2547 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2548 }
2549};
2550
2551/// Helper class for identifying ordered min predicates.
2553 static bool match(FCmpInst::Predicate Pred) {
2554 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2555 }
2556};
2557
2558/// Helper class for identifying unordered max predicates.
2560 static bool match(FCmpInst::Predicate Pred) {
2561 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2562 }
2563};
2564
2565/// Helper class for identifying unordered min predicates.
2567 static bool match(FCmpInst::Predicate Pred) {
2568 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2569 }
2570};
2571
2572/// Match an 'ordered' floating point maximum function.
2573/// Floating point has one special value 'NaN'. Therefore, there is no total
2574/// order. However, if we can ignore the 'NaN' value (for example, because of a
2575/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2576/// semantics. In the presence of 'NaN' we have to preserve the original
2577/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2578///
2579/// max(L, R) iff L and R are not NaN
2580/// m_OrdFMax(L, R) = R iff L or R are NaN
2581template <typename LHS, typename RHS>
2583 const RHS &R) {
2585}
2586
2587/// Match an 'ordered' floating point minimum function.
2588/// Floating point has one special value 'NaN'. Therefore, there is no total
2589/// order. However, if we can ignore the 'NaN' value (for example, because of a
2590/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2591/// semantics. In the presence of 'NaN' we have to preserve the original
2592/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2593///
2594/// min(L, R) iff L and R are not NaN
2595/// m_OrdFMin(L, R) = R iff L or R are NaN
2596template <typename LHS, typename RHS>
2598 const RHS &R) {
2600}
2601
2602/// Match an 'unordered' floating point maximum function.
2603/// Floating point has one special value 'NaN'. Therefore, there is no total
2604/// order. However, if we can ignore the 'NaN' value (for example, because of a
2605/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2606/// semantics. In the presence of 'NaN' we have to preserve the original
2607/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2608///
2609/// max(L, R) iff L and R are not NaN
2610/// m_UnordFMax(L, R) = L iff L or R are NaN
2611template <typename LHS, typename RHS>
2616
2617/// Match an 'unordered' floating point minimum function.
2618/// Floating point has one special value 'NaN'. Therefore, there is no total
2619/// order. However, if we can ignore the 'NaN' value (for example, because of a
2620/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2621/// semantics. In the presence of 'NaN' we have to preserve the original
2622/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2623///
2624/// min(L, R) iff L and R are not NaN
2625/// m_UnordFMin(L, R) = L iff L or R are NaN
2626template <typename LHS, typename RHS>
2631
2632/// Match an 'ordered' or 'unordered' floating point maximum function.
2633/// Floating point has one special value 'NaN'. Therefore, there is no total
2634/// order. However, if we can ignore the 'NaN' value (for example, because of a
2635/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2636/// semantics.
2637template <typename LHS, typename RHS>
2644
2645/// Match an 'ordered' or 'unordered' floating point minimum function.
2646/// Floating point has one special value 'NaN'. Therefore, there is no total
2647/// order. However, if we can ignore the 'NaN' value (for example, because of a
2648/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2649/// semantics.
2650template <typename LHS, typename RHS>
2657
2658/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2659/// NOTE: we first match the 'Not' (by matching '-1'),
2660/// and only then match the inner matcher!
2661template <typename ValTy>
2662inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2663m_Not(const ValTy &V) {
2664 return m_c_Xor(m_AllOnes(), V);
2665}
2666
2667template <typename ValTy>
2668inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2669 true>
2670m_NotForbidPoison(const ValTy &V) {
2671 return m_c_Xor(m_AllOnesForbidPoison(), V);
2672}
2673
2674//===----------------------------------------------------------------------===//
2675// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2676// Note that S might be matched to other instructions than AddInst.
2677//
2678
2679template <typename LHS_t, typename RHS_t, typename Sum_t>
2683 Sum_t S;
2684
2685 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2686 : L(L), R(R), S(S) {}
2687
2688 template <typename OpTy> bool match(OpTy *V) const {
2689 Value *ICmpLHS, *ICmpRHS;
2690 CmpPredicate Pred;
2691 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2692 return false;
2693
2694 Value *AddLHS, *AddRHS;
2695 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2696
2697 // (a + b) u< a, (a + b) u< b
2698 if (Pred == ICmpInst::ICMP_ULT)
2699 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2700 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2701
2702 // a >u (a + b), b >u (a + b)
2703 if (Pred == ICmpInst::ICMP_UGT)
2704 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2705 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2706
2707 Value *Op1;
2708 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2709 // (~a) <u b
2710 if (Pred == ICmpInst::ICMP_ULT) {
2711 if (XorExpr.match(ICmpLHS))
2712 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2713 }
2714 // b > u (~a)
2715 if (Pred == ICmpInst::ICMP_UGT) {
2716 if (XorExpr.match(ICmpRHS))
2717 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2718 }
2719
2720 // Match special-case for increment-by-1.
2721 if (Pred == ICmpInst::ICMP_EQ) {
2722 // (a + 1) == 0
2723 // (1 + a) == 0
2724 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2725 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2726 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2727 // 0 == (a + 1)
2728 // 0 == (1 + a)
2729 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2730 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2731 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2732 }
2733
2734 return false;
2735 }
2736};
2737
2738/// Match an icmp instruction checking for unsigned overflow on addition.
2739///
2740/// S is matched to the addition whose result is being checked for overflow, and
2741/// L and R are matched to the LHS and RHS of S.
2742template <typename LHS_t, typename RHS_t, typename Sum_t>
2744m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2746}
2747
2748template <typename Opnd_t> struct Argument_match {
2749 unsigned OpI;
2750 Opnd_t Val;
2751
2752 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2753
2754 template <typename OpTy> bool match(OpTy *V) const {
2755 // FIXME: Should likely be switched to use `CallBase`.
2756 if (const auto *CI = dyn_cast<CallInst>(V))
2757 return Val.match(CI->getArgOperand(OpI));
2758 return false;
2759 }
2760};
2761
2762/// Match an argument.
2763template <unsigned OpI, typename Opnd_t>
2764inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2765 return Argument_match<Opnd_t>(OpI, Op);
2766}
2767
2768/// Intrinsic matchers.
2770 unsigned ID;
2771
2773
2774 template <typename OpTy> bool match(OpTy *V) const {
2775 if (const auto *CI = dyn_cast<CallInst>(V))
2776 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2777 return F->getIntrinsicID() == ID;
2778 return false;
2779 }
2780};
2781
2782/// Match intrinsic calls with any of the given IDs.
2783template <Intrinsic::ID... IntrIDs> struct IntrinsicIDs_match {
2784 template <typename OpTy> bool match(OpTy *V) const {
2785 if (const auto *CI = dyn_cast<CallInst>(V))
2786 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand())) {
2787 Intrinsic::ID ID = F->getIntrinsicID();
2788 return ((ID == IntrIDs) || ...);
2789 }
2790 return false;
2791 }
2792};
2793
2795 template <Intrinsic::ID IntrID, typename... Ts, size_t... Is>
2796 static auto impl(std::index_sequence<Is...>, const Ts &...Ops) {
2797 return m_CombineAnd(IntrinsicID_match(IntrID), m_Argument<Is>(Ops)...);
2798 }
2799};
2800
2801/// Match intrinsic calls like this:
2802/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2803template <Intrinsic::ID IntrID, typename... Ts>
2804inline auto m_Intrinsic(const Ts &...Ops) {
2806 std::make_index_sequence<sizeof...(Ts)>{}, Ops...);
2807}
2808
2809/// Match intrinsic calls with any of the given IDs like this:
2810/// m_AnyIntrinsic<Intrinsic::fptosi_sat, Intrinsic::fptoui_sat>()
2811/// This is more efficient than using nested m_CombineOr with m_Intrinsic
2812/// because it performs the CallInst/Function cast only once.
2813template <Intrinsic::ID... IntrIDs>
2815 return IntrinsicIDs_match<IntrIDs...>();
2816}
2817
2818/// Matches MaskedLoad Intrinsic.
2819template <typename Opnd0, typename Opnd1, typename Opnd2>
2820inline auto m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2821 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2822}
2823
2824/// Matches MaskedStore Intrinsic.
2825template <typename Opnd0, typename Opnd1, typename Opnd2>
2826inline auto m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1,
2827 const Opnd2 &Op2) {
2828 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2829}
2830
2831/// Matches MaskedGather Intrinsic.
2832template <typename Opnd0, typename Opnd1, typename Opnd2>
2833inline auto m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1,
2834 const Opnd2 &Op2) {
2835 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2836}
2837
2838// Helper intrinsic matching specializations.
2839template <typename Opnd0> inline auto m_BitReverse(const Opnd0 &Op0) {
2841}
2842
2843template <typename Opnd0> inline auto m_BSwap(const Opnd0 &Op0) {
2845}
2846template <typename Opnd0> inline auto m_Ctpop(const Opnd0 &Op0) {
2848}
2849
2850template <typename Opnd0> inline auto m_FAbs(const Opnd0 &Op0) {
2851 return m_Intrinsic<Intrinsic::fabs>(Op0);
2852}
2853
2854template <typename Opnd0> inline auto m_FCanonicalize(const Opnd0 &Op0) {
2856}
2857
2858template <typename Opnd0, typename Opnd1>
2859inline auto m_Ctlz(const Opnd0 &Op0, const Opnd1 &Op1) {
2860 return m_Intrinsic<Intrinsic::ctlz>(Op0, Op1);
2861}
2862
2863template <typename Opnd0, typename Opnd1>
2864inline auto m_Cttz(const Opnd0 &Op0, const Opnd1 &Op1) {
2865 return m_Intrinsic<Intrinsic::cttz>(Op0, Op1);
2866}
2867
2868template <typename Opnd0, typename Opnd1>
2869inline auto m_SMax(const Opnd0 &Op0, const Opnd1 &Op1) {
2870 return m_Intrinsic<Intrinsic::smax>(Op0, Op1);
2871}
2872
2873template <typename Opnd0, typename Opnd1>
2874inline auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1) {
2875 return m_Intrinsic<Intrinsic::smin>(Op0, Op1);
2876}
2877
2878template <typename Opnd0, typename Opnd1>
2879inline auto m_UMax(const Opnd0 &Op0, const Opnd1 &Op1) {
2880 return m_Intrinsic<Intrinsic::umax>(Op0, Op1);
2881}
2882
2883template <typename Opnd0, typename Opnd1>
2884inline auto m_UMin(const Opnd0 &Op0, const Opnd1 &Op1) {
2885 return m_Intrinsic<Intrinsic::umin>(Op0, Op1);
2886}
2887
2888template <typename Opnd0, typename Opnd1>
2889inline auto m_MaxOrMin(const Opnd0 &Op0, const Opnd1 &Op1) {
2890 return m_CombineOr(m_SMax(Op0, Op1), m_SMin(Op0, Op1), m_UMax(Op0, Op1),
2891 m_UMin(Op0, Op1));
2892}
2893
2894template <typename Opnd0, typename Opnd1>
2895inline auto m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2896 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2897}
2898
2899template <typename Opnd0, typename Opnd1>
2900inline auto m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1) {
2901 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2902}
2903
2904template <typename Opnd0, typename Opnd1>
2905inline auto m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2906 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2907}
2908
2909template <typename Opnd0, typename Opnd1>
2910inline auto m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2911 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2912}
2913
2914template <typename Opnd0, typename Opnd1>
2915inline auto m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1) {
2916 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2917}
2918
2919template <typename Opnd0, typename Opnd1>
2920inline auto m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2921 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2922}
2923
2924template <typename Opnd0, typename Opnd1>
2925inline auto m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2926 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
2927}
2928
2929template <typename Opnd0, typename Opnd1>
2930inline auto m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2931 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
2932}
2933
2934template <typename Opnd0, typename Opnd1, typename Opnd2>
2935inline auto m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2936 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2937}
2938
2939template <typename Opnd0, typename Opnd1, typename Opnd2>
2940inline auto m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2941 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2942}
2943
2944template <typename Opnd0> inline auto m_Sqrt(const Opnd0 &Op0) {
2945 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2946}
2947
2948template <typename Opnd0, typename Opnd1>
2949inline auto m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1) {
2950 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2951}
2952
2953template <typename Opnd0> inline auto m_VecReverse(const Opnd0 &Op0) {
2955}
2956
2957template <typename Opnd0, typename Opnd1, typename Opnd2>
2958inline auto m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1,
2959 const Opnd2 &Op2) {
2960 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
2961}
2962
2963//===----------------------------------------------------------------------===//
2964// Matchers for two-operands operators with the operators in either order
2965//
2966
2967/// Matches a BinaryOperator with LHS and RHS in either order.
2968template <typename LHS, typename RHS>
2971}
2972
2973/// Matches an ICmp with a predicate over LHS and RHS in either order.
2974/// Swaps the predicate if operands are commuted.
2975template <typename LHS, typename RHS>
2977m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2979}
2980
2981template <typename LHS, typename RHS>
2986
2987/// Matches a specific opcode with LHS and RHS in either order.
2988template <typename LHS, typename RHS>
2990m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2991 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2992}
2993
2994/// Matches a Add with LHS and RHS in either order.
2995template <typename LHS, typename RHS>
3000
3001/// Matches a Mul with LHS and RHS in either order.
3002template <typename LHS, typename RHS>
3007
3008/// Matches an And with LHS and RHS in either order.
3009template <typename LHS, typename RHS>
3014
3015/// Matches an Or with LHS and RHS in either order.
3016template <typename LHS, typename RHS>
3021
3022/// Matches an Xor with LHS and RHS in either order.
3023template <typename LHS, typename RHS>
3028
3029/// Matches a 'Neg' as 'sub 0, V'.
3030template <typename ValTy>
3031inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3032m_Neg(const ValTy &V) {
3033 return m_Sub(m_ZeroInt(), V);
3034}
3035
3036/// Matches a 'Neg' as 'sub nsw 0, V'.
3037template <typename ValTy>
3039 Instruction::Sub,
3041m_NSWNeg(const ValTy &V) {
3042 return m_NSWSub(m_ZeroInt(), V);
3043}
3044
3045template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3049
3050 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3051
3052 template <typename OpTy> bool match(OpTy *V) const {
3053 const auto *II = dyn_cast<IntrinsicInst>(V);
3054 if (!II || II->getIntrinsicID() != IntrID)
3055 return false;
3056 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3057 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3058 }
3059};
3060
3061template <Intrinsic::ID IntrID, typename T0, typename T1>
3063m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3065}
3066
3067/// Matches an SMin with LHS and RHS in either order.
3068template <typename LHS, typename RHS>
3069inline auto m_c_SMin(const LHS &L, const RHS &R) {
3070 return m_c_Intrinsic<Intrinsic::smin>(L, R);
3071}
3072/// Matches an SMax with LHS and RHS in either order.
3073template <typename LHS, typename RHS>
3074inline auto m_c_SMax(const LHS &L, const RHS &R) {
3075 return m_c_Intrinsic<Intrinsic::smax>(L, R);
3076}
3077/// Matches a UMin with LHS and RHS in either order.
3078template <typename LHS, typename RHS>
3079inline auto m_c_UMin(const LHS &L, const RHS &R) {
3080 return m_c_Intrinsic<Intrinsic::umin>(L, R);
3081}
3082/// Matches a UMax with LHS and RHS in either order.
3083template <typename LHS, typename RHS>
3084inline auto m_c_UMax(const LHS &L, const RHS &R) {
3085 return m_c_Intrinsic<Intrinsic::umax>(L, R);
3086}
3087
3088template <typename LHS, typename RHS>
3089inline auto m_c_MaxOrMin(const LHS &L, const RHS &R) {
3090 return m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R), m_c_UMax(L, R),
3091 m_c_UMin(L, R));
3092}
3093
3094/// Matches FAdd with LHS and RHS in either order.
3095template <typename LHS, typename RHS>
3097m_c_FAdd(const LHS &L, const RHS &R) {
3099}
3100
3101/// Matches FMul with LHS and RHS in either order.
3102template <typename LHS, typename RHS>
3104m_c_FMul(const LHS &L, const RHS &R) {
3106}
3107
3108template <typename Opnd_t> struct Signum_match {
3109 Opnd_t Val;
3110 Signum_match(const Opnd_t &V) : Val(V) {}
3111
3112 template <typename OpTy> bool match(OpTy *V) const {
3113 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3114 if (TypeSize == 0)
3115 return false;
3116
3117 unsigned ShiftWidth = TypeSize - 1;
3118 Value *Op;
3119
3120 // This is the representation of signum we match:
3121 //
3122 // signum(x) == (x >> 63) | (-x >>u 63)
3123 //
3124 // An i1 value is its own signum, so it's correct to match
3125 //
3126 // signum(x) == (x >> 0) | (-x >>u 0)
3127 //
3128 // for i1 values.
3129
3130 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3131 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3132 auto Signum = m_c_Or(LHS, RHS);
3133
3134 return Signum.match(V) && Val.match(Op);
3135 }
3136};
3137
3138/// Matches a signum pattern.
3139///
3140/// signum(x) =
3141/// x > 0 -> 1
3142/// x == 0 -> 0
3143/// x < 0 -> -1
3144template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3145 return Signum_match<Val_t>(V);
3146}
3147
3148template <int Ind, typename Opnd_t> struct ExtractValue_match {
3149 Opnd_t Val;
3150 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3151
3152 template <typename OpTy> bool match(OpTy *V) const {
3153 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3154 // If Ind is -1, don't inspect indices
3155 if (Ind != -1 &&
3156 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3157 return false;
3158 return Val.match(I->getAggregateOperand());
3159 }
3160 return false;
3161 }
3162};
3163
3164/// Match a single index ExtractValue instruction.
3165/// For example m_ExtractValue<1>(...)
3166template <int Ind, typename Val_t>
3170
3171/// Match an ExtractValue instruction with any index.
3172/// For example m_ExtractValue(...)
3173template <typename Val_t>
3174inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3175 return ExtractValue_match<-1, Val_t>(V);
3176}
3177
3178/// Matcher for a single index InsertValue instruction.
3179template <int Ind, typename T0, typename T1> struct InsertValue_match {
3182
3183 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3184
3185 template <typename OpTy> bool match(OpTy *V) const {
3186 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3187 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3188 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3189 }
3190 return false;
3191 }
3192};
3193
3194/// Matches a single index InsertValue instruction.
3195template <int Ind, typename Val_t, typename Elt_t>
3197 const Elt_t &Elt) {
3198 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3199}
3200
3201/// Matches a call to `llvm.vscale()`.
3202inline auto m_VScale() { return m_Intrinsic<Intrinsic::vscale>(); }
3203
3204template <typename Opnd0, typename Opnd1>
3205inline auto m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3207}
3208
3209template <typename Opnd> inline auto m_Deinterleave2(const Opnd &Op) {
3211}
3212
3213template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3217
3218 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3219
3220 template <typename T> bool match(T *V) const {
3221 auto *I = dyn_cast<Instruction>(V);
3222 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3223 return false;
3224
3225 if (I->getOpcode() == Opcode) {
3226 auto *Op0 = I->getOperand(0);
3227 auto *Op1 = I->getOperand(1);
3228 return (L.match(Op0) && R.match(Op1)) ||
3229 (Commutable && L.match(Op1) && R.match(Op0));
3230 }
3231
3232 if (auto *Select = dyn_cast<SelectInst>(I)) {
3233 auto *Cond = Select->getCondition();
3234 auto *TVal = Select->getTrueValue();
3235 auto *FVal = Select->getFalseValue();
3236
3237 // Don't match a scalar select of bool vectors.
3238 // Transforms expect a single type for operands if this matches.
3239 if (Cond->getType() != Select->getType())
3240 return false;
3241
3242 if (Opcode == Instruction::And) {
3243 auto *C = dyn_cast<Constant>(FVal);
3244 if (C && C->isNullValue())
3245 return (L.match(Cond) && R.match(TVal)) ||
3246 (Commutable && L.match(TVal) && R.match(Cond));
3247 } else {
3248 assert(Opcode == Instruction::Or);
3249 auto *C = dyn_cast<Constant>(TVal);
3250 if (C && C->isOneValue())
3251 return (L.match(Cond) && R.match(FVal)) ||
3252 (Commutable && L.match(FVal) && R.match(Cond));
3253 }
3254 }
3255
3256 return false;
3257 }
3258};
3259
3260/// Matches L && R either in the form of L & R or L ? R : false.
3261/// Note that the latter form is poison-blocking.
3262template <typename LHS, typename RHS>
3267
3268/// Matches L && R where L and R are arbitrary values.
3269inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3270
3271/// Matches L && R with LHS and RHS in either order.
3272template <typename LHS, typename RHS>
3274m_c_LogicalAnd(const LHS &L, const RHS &R) {
3276}
3277
3278/// Matches L || R either in the form of L | R or L ? true : R.
3279/// Note that the latter form is poison-blocking.
3280template <typename LHS, typename RHS>
3285
3286/// Matches L || R where L and R are arbitrary values.
3287inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3288
3289/// Matches L || R with LHS and RHS in either order.
3290template <typename LHS, typename RHS>
3292m_c_LogicalOr(const LHS &L, const RHS &R) {
3294}
3295
3296/// Matches either L && R or L || R,
3297/// either one being in the either binary or logical form.
3298/// Note that the latter form is poison-blocking.
3299template <typename LHS, typename RHS, bool Commutable = false>
3305
3306/// Matches either L && R or L || R where L and R are arbitrary values.
3307inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3308
3309/// Matches either L && R or L || R with LHS and RHS in either order.
3310template <typename LHS, typename RHS>
3311inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3312 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3313}
3314
3315} // end namespace PatternMatch
3316} // end namespace llvm
3317
3318#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:856
static constexpr unsigned long long mask(BlockVerifier::State S)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define T1
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1577
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h:565
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1316
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
bool isShift() const
A wrapper class for inspecting calls to intrinsic functions.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
'undef' values are things that do not have specified contents.
Definition Constants.h:1631
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
Base class of all SIMD vector types.
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
match_bind< PHINode > m_Phi(PHINode *&PN)
Match a PHI node, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
auto m_BSwap(const Opnd0 &Op0)
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)
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)
auto m_BitReverse(const Opnd0 &Op0)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
auto m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
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.
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
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.
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.
auto m_Sqrt(const Opnd0 &Op0)
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
auto m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
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)
LoadSimple_match< OpTy > m_LoadSimple(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
auto m_UMin(const Opnd0 &Op0, const Opnd1 &Op1)
match_deferred< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
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)
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'.
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.
FMaxMin_match< LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
auto m_SMax(const Opnd0 &Op0, const Opnd1 &Op1)
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.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
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.
auto m_UMax(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
FMaxMin_match< LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
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.
auto m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_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.
auto m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
ICmpLike_match< LHS, RHS > m_ICmpLike(CmpPredicate &Pred, const LHS &L, const RHS &R)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
auto m_Value()
Match an arbitrary value and ignore it.
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
auto m_Ctpop(const Opnd0 &Op0)
auto m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_SpecificType(Type *RefTy, const Pattern &P)
Match a value of a specific type.
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.
ContainsMatchingVectorElement_match< SPTy > m_ContainsMatchingVectorElement(const SPTy &SubPattern)
Match a vector constant where at least one of its elements matches the subpattern.
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)
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.
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.
auto m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedGather Intrinsic.
auto m_VScale()
Matches a call to llvm.vscale().
FMaxMin_match< LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
match_combine_or< CastInst_match< OpTy, FPToUIInst >, CastInst_match< OpTy, FPToSIInst > > m_FPToI(const OpTy &Op)
cst_pred_ty< is_non_zero_int > m_NonZeroInt()
Match a non-zero integer or a vector with all non-zero elements.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
match_bind< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
AllowFmf_match< T, FastMathFlags::AllowReassoc > m_AllowReassoc(const T &SubPattern)
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
auto m_Ctlz(const Opnd0 &Op0, const Opnd1 &Op1)
FMaxMin_match< LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
auto m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
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()
match_combine_or< FMaxMin_match< LHS, RHS, ofmin_pred_ty >, FMaxMin_match< LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
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)
auto m_FCanonicalize(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
auto m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
AllowFmf_match< T, FastMathFlags::ApproxFunc > m_ApproxFunc(const T &SubPattern)
auto m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
SpecificType_match(const Type *, const Pattern &) -> SpecificType_match< Pattern >
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.
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)
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".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
auto m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
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)
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
auto m_Deinterleave2(const Opnd &Op)
auto m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
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.
auto m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
auto m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
auto m_FAbs(const Opnd0 &Op0)
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.
match_combine_or< FMaxMin_match< LHS, RHS, ofmax_pred_ty >, FMaxMin_match< LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
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)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_MaxOrMin(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
auto m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
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))
auto m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
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.
auto m_FMaxNum(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'.
auto m_VecReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
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.
auto m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
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)
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.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
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)
auto m_Cttz(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
auto m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
auto m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
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
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2166
Matcher to bind the captured value.
Matcher for a specific value, but stores a reference to the value, not the value itself.
AllowFmf_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
FMaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
ICmpLike_match(CmpPredicate &P, const LHS_t &Left, const RHS_t &Right)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
Match intrinsic calls with any of the given IDs.
static auto impl(std::index_sequence< Is... >, const Ts &...Ops)
Matches a simple (non-volatile, non-atomic) LoadInst.
OneOps_match< OpTy, Instruction::Load > Base
LogicalOp_match(const LHS &L, const RHS &R)
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)
SpecificType_match(Type *RefTy, const Pattern &P)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Match a specified basic block value.
Match a specified floating point value or vector of all elements of that value.
Match a specified integer value or vector of all elements of that value.
Matcher for specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
static bool check(const Value *V)