LLVM 23.0.0git
PatternMatch.h
Go to the documentation of this file.
1//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides a simple and efficient mechanism for performing general
10// tree-based pattern matches on the LLVM IR. The power of these routines is
11// that it allows you to write concise patterns that are expressive and easy to
12// understand. The other major advantage of this is that it allows you to
13// trivially capture/bind elements in the pattern to variables. For example,
14// you can do something like this:
15//
16// Value *Exp = ...
17// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
18// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19// m_And(m_Value(Y), m_ConstantInt(C2))))) {
20// ... Pattern is matched and variables are bound ...
21// }
22//
23// This is primarily useful to things like the instruction combiner, but can
24// also be useful for static analysis tools or code generators.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_IR_PATTERNMATCH_H
29#define LLVM_IR_PATTERNMATCH_H
30
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/FMF.h"
37#include "llvm/IR/InstrTypes.h"
38#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/Operator.h"
43#include "llvm/IR/Value.h"
46#include <cstdint>
47
48using namespace llvm::PatternMatchHelpers;
49
50namespace llvm {
51namespace PatternMatch {
52
53template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
54 return P.match(V);
55}
56
57/// A match functor that can be used as a UnaryPredicate in functional
58/// algorithms like all_of.
59template <typename Val = const Value, typename Pattern>
60auto match_fn(const Pattern &P) {
62}
63
64template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
65 return P.match(Mask);
66}
67
68template <typename SubPattern_t> struct OneUse_match {
69 SubPattern_t SubPattern;
70
71 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
72
73 template <typename OpTy> bool match(OpTy *V) const {
74 return V->hasOneUse() && SubPattern.match(V);
75 }
76};
77
78template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
79 return SubPattern;
80}
81
82template <typename SubPattern_t, int Flag> struct AllowFmf_match {
83 SubPattern_t SubPattern;
85
86 AllowFmf_match(const SubPattern_t &SP) : SubPattern(SP), FMF(Flag) {}
87
88 template <typename OpTy> bool match(OpTy *V) const {
89 auto *I = dyn_cast<FPMathOperator>(V);
90 return I && ((I->getFastMathFlags() & FMF) == FMF) && SubPattern.match(I);
91 }
92};
93
94template <typename T>
96m_AllowReassoc(const T &SubPattern) {
97 return SubPattern;
98}
99
100template <typename T>
102m_AllowReciprocal(const T &SubPattern) {
103 return SubPattern;
104}
105
106template <typename T>
108m_AllowContract(const T &SubPattern) {
109 return SubPattern;
110}
111
112template <typename T>
114m_ApproxFunc(const T &SubPattern) {
115 return SubPattern;
116}
117
118template <typename T>
120 return SubPattern;
121}
122
123template <typename T>
125 return SubPattern;
126}
127
128template <typename T>
130m_NoSignedZeros(const T &SubPattern) {
131 return SubPattern;
132}
133
134/// Match an arbitrary value and ignore it.
135inline auto m_Value() { return m_Isa<Value>(); }
136
137/// Match an arbitrary unary operation and ignore it.
138inline auto m_UnOp() { return m_Isa<UnaryOperator>(); }
139
140/// Match an arbitrary binary operation and ignore it.
141inline auto m_BinOp() { return m_Isa<BinaryOperator>(); }
142
143/// Matches any compare instruction and ignore it.
144inline auto m_Cmp() { return m_Isa<CmpInst>(); }
145
146/// Matches any intrinsic call and ignore it.
147inline auto m_AnyIntrinsic() { return m_Isa<IntrinsicInst>(); }
148
150private:
151 LLVM_ABI static bool checkAggregate(const ConstantAggregate *CA);
152
153public:
154 static bool check(const Value *V) {
155 if (isa<UndefValue>(V))
156 return true;
157 if (const auto *CA = dyn_cast<ConstantAggregate>(V))
158 return checkAggregate(CA);
159 return false;
160 }
161 template <typename ITy> bool match(ITy *V) const { return check(V); }
162};
163
164/// Match an arbitrary undef constant. This matches poison as well.
165/// If this is an aggregate and contains a non-aggregate element that is
166/// neither undef nor poison, the aggregate is not matched.
167inline auto m_Undef() { return undef_match(); }
168
169/// Match an arbitrary UndefValue constant.
170inline auto m_UndefValue() { return m_Isa<UndefValue>(); }
171
172/// Match an arbitrary poison constant.
173inline auto m_Poison() { return m_Isa<PoisonValue>(); }
174
175/// Match an arbitrary Constant and ignore it.
176inline auto m_Constant() { return m_Isa<Constant>(); }
177
178/// Match an arbitrary ConstantInt and ignore it.
179inline auto m_ConstantInt() { return m_Isa<ConstantInt>(); }
180
181/// Match an arbitrary ConstantFP and ignore it.
182inline auto m_ConstantFP() { return m_Isa<ConstantFP>(); }
183
184template <typename SPTy> struct ContainsMatchingVectorElement_match {
187
188 template <typename ITy> bool match(ITy *V) const {
189 auto *C = dyn_cast<Constant>(V);
190 return C && C->containsMatchingVectorElement(
191 [&](Constant *E) { return SubPattern.match(E); });
192 }
193};
194
195/// Match a vector constant where at least one of its elements matches the
196/// subpattern. Scalable vector constants are not matched. Any bindings in the
197/// subpattern will be bound to the first match.
198template <typename SPTy>
200m_ContainsMatchingVectorElement(const SPTy &SubPattern) {
201 return SubPattern;
202}
203
204/// Match a constant expression or a constant that contains a constant
205/// expression.
210
211template <typename SubPattern_t> struct Splat_match {
212 SubPattern_t SubPattern;
213 Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
214
215 template <typename OpTy> bool match(OpTy *V) const {
216 if (auto *C = dyn_cast<Constant>(V)) {
217 auto *Splat = C->getSplatValue();
218 return Splat ? SubPattern.match(Splat) : false;
219 }
220 // TODO: Extend to other cases (e.g. shufflevectors).
221 return false;
222 }
223};
224
225/// Match a constant splat. TODO: Extend this to non-constant splats.
226template <typename T>
227inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
228 return SubPattern;
229}
230
231/// Match an arbitrary basic block value and ignore it.
232inline auto m_BasicBlock() { return m_Isa<BasicBlock>(); }
233
234/// Inverting matcher
235template <typename Ty> struct match_unless {
236 Ty M;
237
238 match_unless(const Ty &Matcher) : M(Matcher) {}
239
240 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
241};
242
243/// Match if the inner matcher does *NOT* match.
244template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
245 return match_unless<Ty>(M);
246}
247
248template <typename APTy> struct ap_match {
249 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
251 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
252
253 const APTy *&Res;
255
256 ap_match(const APTy *&Res, bool AllowPoison)
258
259 template <typename ITy> bool match(ITy *V) const {
260 if (auto *CI = dyn_cast<ConstantTy>(V)) {
261 Res = &CI->getValue();
262 return true;
263 }
264 if (V->getType()->isVectorTy())
265 if (const auto *C = dyn_cast<Constant>(V))
266 if (auto *CI =
267 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
268 Res = &CI->getValue();
269 return true;
270 }
271 return false;
272 }
273};
274
275/// Match a ConstantInt or splatted ConstantVector, binding the
276/// specified pointer to the contained APInt.
277inline ap_match<APInt> m_APInt(const APInt *&Res) {
278 // Forbid poison by default to maintain previous behavior.
279 return ap_match<APInt>(Res, /* AllowPoison */ false);
280}
281
282/// Match APInt while allowing poison in splat vector constants.
284 return ap_match<APInt>(Res, /* AllowPoison */ true);
285}
286
287/// Match APInt while forbidding poison in splat vector constants.
289 return ap_match<APInt>(Res, /* AllowPoison */ false);
290}
291
292/// Match a ConstantFP or splatted ConstantVector, binding the
293/// specified pointer to the contained APFloat.
295 // Forbid undefs by default to maintain previous behavior.
296 return ap_match<APFloat>(Res, /* AllowPoison */ false);
297}
298
299/// Match APFloat while allowing poison in splat vector constants.
301 return ap_match<APFloat>(Res, /* AllowPoison */ true);
302}
303
304/// Match APFloat while forbidding poison in splat vector constants.
306 return ap_match<APFloat>(Res, /* AllowPoison */ false);
307}
308
309template <int64_t Val> struct constantint_match {
310 template <typename ITy> bool match(ITy *V) const {
311 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
312 const APInt &CIV = CI->getValue();
313 if (Val >= 0)
314 return CIV == static_cast<uint64_t>(Val);
315 // If Val is negative, and CI is shorter than it, truncate to the right
316 // number of bits. If it is larger, then we have to sign extend. Just
317 // compare their negated values.
318 return -CIV == -Val;
319 }
320 return false;
321 }
322};
323
324/// Match a ConstantInt with a specific value.
325template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
326 return constantint_match<Val>();
327}
328
329/// This helper class is used to match constant scalars, vector splats,
330/// and fixed width vectors that satisfy a specified predicate.
331/// For fixed width vector constants, poison elements are ignored if AllowPoison
332/// is true.
333template <typename Predicate, typename ConstantVal, bool AllowPoison>
334struct cstval_pred_ty : public Predicate {
335private:
336 bool matchVector(const Value *V) const {
337 if (const auto *C = dyn_cast<Constant>(V)) {
338 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
339 return this->isValue(CV->getValue());
340
341 // Number of elements of a scalable vector unknown at compile time
342 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
343 if (!FVTy)
344 return false;
345
346 // Non-splat vector constant: check each element for a match.
347 unsigned NumElts = FVTy->getNumElements();
348 assert(NumElts != 0 && "Constant vector with no elements?");
349 bool HasNonPoisonElements = false;
350 for (unsigned i = 0; i != NumElts; ++i) {
351 Constant *Elt = C->getAggregateElement(i);
352 if (!Elt)
353 return false;
354 if (AllowPoison && isa<PoisonValue>(Elt))
355 continue;
356 auto *CV = dyn_cast<ConstantVal>(Elt);
357 if (!CV || !this->isValue(CV->getValue()))
358 return false;
359 HasNonPoisonElements = true;
360 }
361 return HasNonPoisonElements;
362 }
363 return false;
364 }
365
366public:
367 const Constant **Res = nullptr;
368 template <typename ITy> bool match_impl(ITy *V) const {
369 if (const auto *CV = dyn_cast<ConstantVal>(V))
370 return this->isValue(CV->getValue());
371 if (isa<VectorType>(V->getType()))
372 return matchVector(V);
373 return false;
374 }
375
376 template <typename ITy> bool match(ITy *V) const {
377 if (this->match_impl(V)) {
378 if (Res)
379 *Res = cast<Constant>(V);
380 return true;
381 }
382 return false;
383 }
384};
385
386/// specialization of cstval_pred_ty for ConstantInt
387template <typename Predicate, bool AllowPoison = true>
389
390/// specialization of cstval_pred_ty for ConstantFP
391template <typename Predicate>
393 /*AllowPoison=*/true>;
394
395/// This helper class is used to match scalar and vector constants that
396/// satisfy a specified predicate, and bind them to an APInt.
397template <typename Predicate> struct api_pred_ty : public Predicate {
398 const APInt *&Res;
399
400 api_pred_ty(const APInt *&R) : Res(R) {}
401
402 template <typename ITy> bool match(ITy *V) const {
403 if (const auto *CI = dyn_cast<ConstantInt>(V))
404 if (this->isValue(CI->getValue())) {
405 Res = &CI->getValue();
406 return true;
407 }
408 if (V->getType()->isVectorTy())
409 if (const auto *C = dyn_cast<Constant>(V))
410 if (auto *CI = dyn_cast_or_null<ConstantInt>(
411 C->getSplatValue(/*AllowPoison=*/true)))
412 if (this->isValue(CI->getValue())) {
413 Res = &CI->getValue();
414 return true;
415 }
416
417 return false;
418 }
419};
420
421/// This helper class is used to match scalar and vector constants that
422/// satisfy a specified predicate, and bind them to an APFloat.
423/// Poison is allowed in splat vector constants.
424template <typename Predicate> struct apf_pred_ty : public Predicate {
425 const APFloat *&Res;
426
427 apf_pred_ty(const APFloat *&R) : Res(R) {}
428
429 template <typename ITy> bool match(ITy *V) const {
430 if (const auto *CI = dyn_cast<ConstantFP>(V))
431 if (this->isValue(CI->getValue())) {
432 Res = &CI->getValue();
433 return true;
434 }
435 if (V->getType()->isVectorTy())
436 if (const auto *C = dyn_cast<Constant>(V))
437 if (auto *CI = dyn_cast_or_null<ConstantFP>(
438 C->getSplatValue(/* AllowPoison */ true)))
439 if (this->isValue(CI->getValue())) {
440 Res = &CI->getValue();
441 return true;
442 }
443
444 return false;
445 }
446};
447
448///////////////////////////////////////////////////////////////////////////////
449//
450// Encapsulate constant value queries for use in templated predicate matchers.
451// This allows checking if constants match using compound predicates and works
452// with vector constants, possibly with relaxed constraints. For example, ignore
453// undef values.
454//
455///////////////////////////////////////////////////////////////////////////////
456
457template <typename APTy> struct custom_checkfn {
458 function_ref<bool(const APTy &)> CheckFn;
459 bool isValue(const APTy &C) const { return CheckFn(C); }
460};
461
462/// Match an integer or vector where CheckFn(ele) for each element is true.
463/// For vectors, poison elements are assumed to match.
465m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
466 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
467}
468
470m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
471 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
472}
473
474/// Match a float or vector where CheckFn(ele) for each element is true.
475/// For vectors, poison elements are assumed to match.
477m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
478 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
479}
480
482m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
483 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
484}
485
487 bool isValue(const APInt &C) const { return true; }
488};
489/// Match an integer or vector with any integral constant.
490/// For vectors, this includes constants with undefined elements.
494
496 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
497};
498
502
504 bool isValue(const APInt &C) const { return C.isAllOnes(); }
505};
506/// Match an integer or vector with all bits set.
507/// For vectors, this includes constants with undefined elements.
511
515
516inline auto m_AllOnesOrPoison() { return m_CombineOr(m_AllOnes(), m_Poison()); }
517
519 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
520};
521/// Match an integer or vector with values having all bits except for the high
522/// bit set (0x7f...).
523/// For vectors, this includes constants with undefined elements.
528 return V;
529}
530
532 bool isValue(const APInt &C) const { return C.isNegative(); }
533};
534/// Match an integer or vector of negative values.
535/// For vectors, this includes constants with undefined elements.
539inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
540
542 bool isValue(const APInt &C) const { return C.isNonNegative(); }
543};
544/// Match an integer or vector of non-negative values.
545/// For vectors, this includes constants with undefined elements.
549inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
550
552 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
553};
554/// Match an integer or vector of strictly positive values.
555/// For vectors, this includes constants with undefined elements.
560 return V;
561}
562
564 bool isValue(const APInt &C) const { return C.isNonPositive(); }
565};
566/// Match an integer or vector of non-positive values.
567/// For vectors, this includes constants with undefined elements.
571inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
572
573struct is_one {
574 bool isValue(const APInt &C) const { return C.isOne(); }
575};
576/// Match an integer 1 or a vector with all elements equal to 1.
577/// For vectors, this includes constants with undefined elements.
579
581 bool isValue(const APInt &C) const { return C.isZero(); }
582};
583/// Match an integer 0 or a vector with all elements equal to 0.
584/// For vectors, this includes constants with undefined elements.
588
590 bool isValue(const APInt &C) const { return !C.isZero(); }
591};
592/// Match a non-zero integer or a vector with all non-zero elements.
593/// For vectors, this includes constants with undefined elements.
597
598struct is_zero {
599 template <typename ITy> bool match(ITy *V) const {
600 auto *C = dyn_cast<Constant>(V);
601 // FIXME: this should be able to do something for scalable vectors
602 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
603 }
604};
605/// Match any null constant or a vector with all elements equal to 0.
606/// For vectors, this includes constants with undefined elements.
607inline is_zero m_Zero() { return is_zero(); }
608
609inline auto m_ZeroOrPoison() { return m_CombineOr(m_Zero(), m_Poison()); }
610
611struct is_power2 {
612 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
613};
614/// Match an integer or vector power-of-2.
615/// For vectors, this includes constants with undefined elements.
617inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
618
620 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
621};
622/// Match a integer or vector negated power-of-2.
623/// For vectors, this includes constants with undefined elements.
628 return V;
629}
630
632 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
633};
634/// Match a integer or vector negated power-of-2.
635/// For vectors, this includes constants with undefined elements.
641 return V;
642}
643
645 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
646};
647/// Match an integer or vector of 0 or power-of-2 values.
648/// For vectors, this includes constants with undefined elements.
653 return V;
654}
655
657 bool isValue(const APInt &C) const { return C.isSignMask(); }
658};
659/// Match an integer or vector with only the sign bit(s) set.
660/// For vectors, this includes constants with undefined elements.
664
666 bool isValue(const APInt &C) const { return C.isMask(); }
667};
668/// Match an integer or vector with only the low bit(s) set.
669/// For vectors, this includes constants with undefined elements.
673inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
674
676 bool isValue(const APInt &C) const { return !C || C.isMask(); }
677};
678/// Match an integer or vector with only the low bit(s) set.
679/// For vectors, this includes constants with undefined elements.
684 return V;
685}
686
689 const APInt *Thr;
690 bool isValue(const APInt &C) const {
691 return ICmpInst::compare(C, *Thr, Pred);
692 }
693};
694/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
695/// to Threshold. For vectors, this includes constants with undefined elements.
697m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
699 P.Pred = Predicate;
700 P.Thr = &Threshold;
701 return P;
702}
703
704struct is_nan {
705 bool isValue(const APFloat &C) const { return C.isNaN(); }
706};
707/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
708/// For vectors, this includes constants with undefined elements.
710
711struct is_nonnan {
712 bool isValue(const APFloat &C) const { return !C.isNaN(); }
713};
714/// Match a non-NaN FP constant.
715/// For vectors, this includes constants with undefined elements.
719
720struct is_inf {
721 bool isValue(const APFloat &C) const { return C.isInfinity(); }
722};
723/// Match a positive or negative infinity FP constant.
724/// For vectors, this includes constants with undefined elements.
726
727template <bool IsNegative> struct is_signed_inf {
728 bool isValue(const APFloat &C) const {
729 return C.isInfinity() && IsNegative == C.isNegative();
730 }
731};
732
733/// Match a positive infinity FP constant.
734/// For vectors, this includes constants with undefined elements.
738
739/// Match a negative infinity FP constant.
740/// For vectors, this includes constants with undefined elements.
744
745struct is_noninf {
746 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
747};
748/// Match a non-infinity FP constant, i.e. finite or NaN.
749/// For vectors, this includes constants with undefined elements.
753
754struct is_finite {
755 bool isValue(const APFloat &C) const { return C.isFinite(); }
756};
757/// Match a finite FP constant, i.e. not infinity or NaN.
758/// For vectors, this includes constants with undefined elements.
762inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
763
765 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
766};
767/// Match a finite non-zero FP constant.
768/// For vectors, this includes constants with undefined elements.
773 return V;
774}
775
777 bool isValue(const APFloat &C) const { return C.isZero(); }
778};
779/// Match a floating-point negative zero or positive zero.
780/// For vectors, this includes constants with undefined elements.
784
786 bool isValue(const APFloat &C) const { return C.isPosZero(); }
787};
788/// Match a floating-point positive zero.
789/// For vectors, this includes constants with undefined elements.
793
795 bool isValue(const APFloat &C) const { return C.isNegZero(); }
796};
797/// Match a floating-point negative zero.
798/// For vectors, this includes constants with undefined elements.
802
804 bool isValue(const APFloat &C) const { return C.isNonZero(); }
805};
806/// Match a floating-point non-zero.
807/// For vectors, this includes constants with undefined elements.
811
813 bool isValue(const APFloat &C) const {
814 return !C.isDenormal() && C.isNonZero();
815 }
816};
817
818/// Match a floating-point non-zero that is not a denormal.
819/// For vectors, this includes constants with undefined elements.
823
824///////////////////////////////////////////////////////////////////////////////
825
826/// Match a value, capturing it if we match.
827inline match_bind<Value> m_Value(Value *&V) { return V; }
828inline match_bind<const Value> m_Value(const Value *&V) { return V; }
829
830/// Match against the nested pattern, and capture the value if we match.
831template <typename Pattern> inline auto m_Value(Value *&V, const Pattern &P) {
832 return m_CombineAnd(P, match_bind<Value>(V));
833}
834
835/// Match against the nested pattern, and capture the value if we match.
836template <typename Pattern>
837inline auto m_Value(const Value *&V, const Pattern &P) {
839}
840
841/// Match an instruction, capturing it if we match.
844 return I;
845}
846
847/// Match against the nested pattern, and capture the instruction if we match.
848template <typename Pattern>
849inline auto m_Instruction(Instruction *&I, const Pattern &P) {
851}
852template <typename Pattern>
853inline auto m_Instruction(const Instruction *&I, const Pattern &P) {
855}
856
857/// Match a unary operator, capturing it if we match.
860 return I;
861}
862/// Match a binary operator, capturing it if we match.
865 return I;
866}
867/// Match any intrinsic call, capturing it if we match.
872/// Match a with overflow intrinsic, capturing it if we match.
878 return I;
879}
880
881/// Match an UndefValue, capturing the value if we match.
883
884/// Match a Constant, capturing the value if we match.
886
887/// Match a ConstantInt, capturing the value if we match.
889
890/// Match a ConstantFP, capturing the value if we match.
892
893/// Match a ConstantExpr, capturing the value if we match.
895
896/// Match a basic block value, capturing it if we match.
899 return V;
900}
901
902// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
903// and use m_Unless(m_ConstantExpr).
905 template <typename ITy> static bool isImmConstant(ITy *V) {
906 if (auto *CV = dyn_cast<Constant>(V)) {
907 if (!match(CV, m_ConstantExpr()))
908 return true;
909
910 if (CV->getType()->isVectorTy()) {
911 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
912 if (!match(Splat, m_ConstantExpr())) {
913 return true;
914 }
915 }
916 }
917 }
918 return false;
919 }
920};
921
923 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
924};
925
926/// Match an arbitrary immediate Constant and ignore it.
928
931
933
934 template <typename ITy> bool match(ITy *V) const {
935 if (isImmConstant(V)) {
936 VR = cast<Constant>(V);
937 return true;
938 }
939 return false;
940 }
941};
942
943/// Match an immediate Constant, capturing the value if we match.
947
948/// Matcher for specified Value*.
950 const Value *Val;
951
952 specificval_ty(const Value *V) : Val(V) {}
953
954 template <typename ITy> bool match(ITy *V) const { return V == Val; }
955};
956
957/// Match if we have a specific specified value.
958inline specificval_ty m_Specific(const Value *V) { return V; }
959
960/// Like m_Specific(), but works if the specific value to match is determined
961/// as part of the same match() expression. For example:
962/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
963/// bind X before the pattern match starts.
964/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
965/// whichever value m_Value(X) populated.
966inline match_deferred<Value> m_Deferred(Value *const &V) { return V; }
968 return V;
969}
970
971/// Match a specified floating point value or vector of all elements of
972/// that value.
974 double Val;
975
976 specific_fpval(double V) : Val(V) {}
977
978 template <typename ITy> bool match(ITy *V) const {
979 if (const auto *CFP = dyn_cast<ConstantFP>(V))
980 return CFP->isExactlyValue(Val);
981 if (V->getType()->isVectorTy())
982 if (const auto *C = dyn_cast<Constant>(V))
983 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
984 return CFP->isExactlyValue(Val);
985 return false;
986 }
987};
988
989/// Match a specific floating point value or vector with all elements
990/// equal to the value.
991inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
992
993/// Match a float 1.0 or vector with all elements equal to 1.0.
994inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
995
998
1000
1001 template <typename ITy> bool match(ITy *V) const {
1002 const APInt *ConstInt;
1003 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1004 return false;
1005 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
1006 if (!ZExtVal)
1007 return false;
1008 VR = *ZExtVal;
1009 return true;
1010 }
1011};
1012
1013/// Match a specified integer value or vector of all elements of that
1014/// value.
1015template <bool AllowPoison> struct specific_intval {
1016 const APInt &Val;
1017
1018 specific_intval(const APInt &V) : Val(V) {}
1019
1020 template <typename ITy> bool match(ITy *V) const {
1021 const auto *CI = dyn_cast<ConstantInt>(V);
1022 if (!CI && V->getType()->isVectorTy())
1023 if (const auto *C = dyn_cast<Constant>(V))
1024 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1025
1026 return CI && APInt::isSameValue(CI->getValue(), Val);
1027 }
1028};
1029
1030template <bool AllowPoison> struct specific_intval64 {
1032
1034
1035 template <typename ITy> bool match(ITy *V) const {
1036 const auto *CI = dyn_cast<ConstantInt>(V);
1037 if (!CI && V->getType()->isVectorTy())
1038 if (const auto *C = dyn_cast<Constant>(V))
1039 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1040
1041 return CI && CI->getValue() == Val;
1042 }
1043};
1044
1045/// Match a specific integer value or vector with all elements equal to
1046/// the value.
1048 return specific_intval<false>(V);
1049}
1050
1054
1058
1062
1063/// Match a ConstantInt and bind to its value. This does not match
1064/// ConstantInts wider than 64-bits.
1066
1067/// Match a specified basic block value.
1070
1072
1073 template <typename ITy> bool match(ITy *V) const {
1074 const auto *BB = dyn_cast<BasicBlock>(V);
1075 return BB && BB == Val;
1076 }
1077};
1078
1079/// Match a specific basic block value.
1081 return specific_bbval(BB);
1082}
1083
1084/// A commutative-friendly version of m_Specific().
1086 return BB;
1087}
1089m_Deferred(const BasicBlock *const &BB) {
1090 return BB;
1091}
1092
1093//===----------------------------------------------------------------------===//
1094// Matcher for any binary operator.
1095//
1096template <typename LHS_t, typename RHS_t, bool Commutable = false>
1100
1101 // The evaluation order is always stable, regardless of Commutability.
1102 // The LHS is always matched first.
1103 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1104
1105 template <typename OpTy> bool match(OpTy *V) const {
1106 if (auto *I = dyn_cast<BinaryOperator>(V))
1107 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1108 (Commutable && L.match(I->getOperand(1)) &&
1109 R.match(I->getOperand(0)));
1110 return false;
1111 }
1112};
1113
1114template <typename LHS, typename RHS>
1115inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1116 return AnyBinaryOp_match<LHS, RHS>(L, R);
1117}
1118
1119//===----------------------------------------------------------------------===//
1120// Matcher for any unary operator.
1121// TODO fuse unary, binary matcher into n-ary matcher
1122//
1123template <typename OP_t> struct AnyUnaryOp_match {
1124 OP_t X;
1125
1126 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1127
1128 template <typename OpTy> bool match(OpTy *V) const {
1129 if (auto *I = dyn_cast<UnaryOperator>(V))
1130 return X.match(I->getOperand(0));
1131 return false;
1132 }
1133};
1134
1135template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1136 return AnyUnaryOp_match<OP_t>(X);
1137}
1138
1139//===----------------------------------------------------------------------===//
1140// Matchers for specific binary operators.
1141//
1142
1143template <typename LHS_t, typename RHS_t, unsigned Opcode,
1144 bool Commutable = false>
1148
1149 // The evaluation order is always stable, regardless of Commutability.
1150 // The LHS is always matched first.
1151 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1152
1153 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1154 if (V->getValueID() == Value::InstructionVal + Opc) {
1155 auto *I = cast<BinaryOperator>(V);
1156 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1157 (Commutable && L.match(I->getOperand(1)) &&
1158 R.match(I->getOperand(0)));
1159 }
1160 return false;
1161 }
1162
1163 template <typename OpTy> bool match(OpTy *V) const {
1164 return match(Opcode, V);
1165 }
1166};
1167
1168template <typename LHS, typename RHS>
1170 const RHS &R) {
1172}
1173
1174template <typename LHS, typename RHS>
1176 const RHS &R) {
1178}
1179
1180template <typename LHS, typename RHS>
1182 const RHS &R) {
1184}
1185
1186template <typename LHS, typename RHS>
1188 const RHS &R) {
1190}
1191
1192template <typename Op_t> struct FNeg_match {
1193 Op_t X;
1194
1195 FNeg_match(const Op_t &Op) : X(Op) {}
1196 template <typename OpTy> bool match(OpTy *V) const {
1197 auto *FPMO = dyn_cast<FPMathOperator>(V);
1198 if (!FPMO)
1199 return false;
1200
1201 if (FPMO->getOpcode() == Instruction::FNeg)
1202 return X.match(FPMO->getOperand(0));
1203
1204 if (FPMO->getOpcode() == Instruction::FSub) {
1205 if (FPMO->hasNoSignedZeros()) {
1206 // With 'nsz', any zero goes.
1207 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1208 return false;
1209 } else {
1210 // Without 'nsz', we need fsub -0.0, X exactly.
1211 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1212 return false;
1213 }
1214
1215 return X.match(FPMO->getOperand(1));
1216 }
1217
1218 return false;
1219 }
1220};
1221
1222/// Match 'fneg X' as 'fsub -0.0, X'.
1223template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1224 return FNeg_match<OpTy>(X);
1225}
1226
1227/// Match 'fneg X' as 'fsub +-0.0, X'.
1228template <typename RHS>
1229inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1230m_FNegNSZ(const RHS &X) {
1231 return m_FSub(m_AnyZeroFP(), X);
1232}
1233
1234template <typename LHS, typename RHS>
1236 const RHS &R) {
1238}
1239
1240template <typename LHS, typename RHS>
1242 const RHS &R) {
1244}
1245
1246template <typename LHS, typename RHS>
1248 const RHS &R) {
1250}
1251
1252template <typename LHS, typename RHS>
1254 const RHS &R) {
1256}
1257
1258template <typename LHS, typename RHS>
1260 const RHS &R) {
1262}
1263
1264template <typename LHS, typename RHS>
1266 const RHS &R) {
1268}
1269
1270template <typename LHS, typename RHS>
1272 const RHS &R) {
1274}
1275
1276template <typename LHS, typename RHS>
1278 const RHS &R) {
1280}
1281
1282template <typename LHS, typename RHS>
1284 const RHS &R) {
1286}
1287
1288template <typename LHS, typename RHS>
1290 const RHS &R) {
1292}
1293
1294template <typename LHS, typename RHS>
1296 const RHS &R) {
1298}
1299
1300template <typename LHS, typename RHS>
1302 const RHS &R) {
1304}
1305
1306template <typename LHS, typename RHS>
1308 const RHS &R) {
1310}
1311
1312template <typename LHS, typename RHS>
1314 const RHS &R) {
1316}
1317
1318template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1321
1322 ShiftLike_match(const LHS_t &LHS, uint64_t &RHS) : L(LHS), R(RHS) {}
1323
1324 template <typename OpTy> bool match(OpTy *V) const {
1325 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1326 if (Op->getOpcode() == Opcode)
1327 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1328 L.match(Op->getOperand(0));
1329 }
1330 // Interpreted as shiftop V, 0
1331 R = 0;
1332 return L.match(V);
1333 }
1334};
1335
1336/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1337template <typename LHS>
1342
1343/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1344template <typename LHS>
1349
1350/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1351template <typename LHS>
1356
1357template <typename LHS_t, typename RHS_t, unsigned Opcode,
1358 unsigned WrapFlags = 0, bool Commutable = false>
1362
1363 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1364 : L(LHS), R(RHS) {}
1365
1366 template <typename OpTy> bool match(OpTy *V) const {
1367 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1368 if (Op->getOpcode() != Opcode)
1369 return false;
1371 !Op->hasNoUnsignedWrap())
1372 return false;
1373 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1374 !Op->hasNoSignedWrap())
1375 return false;
1376 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1377 (Commutable && L.match(Op->getOperand(1)) &&
1378 R.match(Op->getOperand(0)));
1379 }
1380 return false;
1381 }
1382};
1383
1384template <typename LHS, typename RHS>
1385inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1387m_NSWAdd(const LHS &L, const RHS &R) {
1388 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1390 R);
1391}
1392template <typename LHS, typename RHS>
1393inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1395m_c_NSWAdd(const LHS &L, const RHS &R) {
1396 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1398 true>(L, R);
1399}
1400template <typename LHS, typename RHS>
1401inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1403m_NSWSub(const LHS &L, const RHS &R) {
1404 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1406 R);
1407}
1408template <typename LHS, typename RHS>
1409inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1411m_NSWMul(const LHS &L, const RHS &R) {
1412 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1414 R);
1415}
1416template <typename LHS, typename RHS>
1417inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1419m_NSWShl(const LHS &L, const RHS &R) {
1420 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1422 R);
1423}
1424
1425template <typename LHS, typename RHS>
1426inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1428m_NUWAdd(const LHS &L, const RHS &R) {
1429 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1431 L, R);
1432}
1433
1434template <typename LHS, typename RHS>
1436 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1437m_c_NUWAdd(const LHS &L, const RHS &R) {
1438 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1440 true>(L, R);
1441}
1442
1443template <typename LHS, typename RHS>
1444inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1446m_NUWSub(const LHS &L, const RHS &R) {
1447 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1449 L, R);
1450}
1451template <typename LHS, typename RHS>
1452inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1454m_NUWMul(const LHS &L, const RHS &R) {
1455 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1457 L, R);
1458}
1459template <typename LHS, typename RHS>
1460inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1462m_NUWShl(const LHS &L, const RHS &R) {
1463 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1465 L, R);
1466}
1467
1468template <typename LHS_t, typename RHS_t, bool Commutable = false>
1470 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1471 unsigned Opcode;
1472
1473 SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
1474 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1475
1476 template <typename OpTy> bool match(OpTy *V) const {
1478 }
1479};
1480
1481/// Matches a specific opcode.
1482template <typename LHS, typename RHS>
1483inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1484 const RHS &R) {
1485 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1486}
1487
1488template <typename LHS, typename RHS, bool Commutable = false>
1490 LHS L;
1491 RHS R;
1492
1493 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1494
1495 template <typename OpTy> bool match(OpTy *V) const {
1496 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1497 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1498 if (!PDI->isDisjoint())
1499 return false;
1500 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1501 (Commutable && L.match(PDI->getOperand(1)) &&
1502 R.match(PDI->getOperand(0)));
1503 }
1504 return false;
1505 }
1506};
1507
1508template <typename LHS, typename RHS>
1509inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
1510 return DisjointOr_match<LHS, RHS>(L, R);
1511}
1512
1513template <typename LHS, typename RHS>
1515 const RHS &R) {
1517}
1518
1519/// Match either "add" or "or disjoint".
1520template <typename LHS, typename RHS>
1523m_AddLike(const LHS &L, const RHS &R) {
1524 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1525}
1526
1527/// Match either "add nsw" or "or disjoint"
1528template <typename LHS, typename RHS>
1529inline match_combine_or<
1530 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1533m_NSWAddLike(const LHS &L, const RHS &R) {
1534 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1535}
1536
1537/// Match either "add nuw" or "or disjoint"
1538template <typename LHS, typename RHS>
1539inline match_combine_or<
1540 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1543m_NUWAddLike(const LHS &L, const RHS &R) {
1544 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1545}
1546
1547template <typename LHS, typename RHS>
1549 LHS L;
1550 RHS R;
1551
1552 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1553
1554 template <typename OpTy> bool match(OpTy *V) const {
1555 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1556 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1557 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1558 ; // Pass
1559 else if (Op->getOpcode() != Instruction::Xor)
1560 return false;
1561 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1562 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1563 }
1564 return false;
1565 }
1566};
1567
1568/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1569/// Only commutative matcher as the `sub` will need to swap the L and R.
1570template <typename LHS, typename RHS>
1571inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1572 return XorLike_match<LHS, RHS>(L, R);
1573}
1574
1575//===----------------------------------------------------------------------===//
1576// Class that matches a group of binary opcodes.
1577//
1578template <typename LHS_t, typename RHS_t, typename Predicate,
1579 bool Commutable = false>
1580struct BinOpPred_match : Predicate {
1583
1584 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1585
1586 template <typename OpTy> bool match(OpTy *V) const {
1587 if (auto *I = dyn_cast<Instruction>(V))
1588 return this->isOpType(I->getOpcode()) &&
1589 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1590 (Commutable && L.match(I->getOperand(1)) &&
1591 R.match(I->getOperand(0))));
1592 return false;
1593 }
1594};
1595
1597 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1598};
1599
1601 bool isOpType(unsigned Opcode) const {
1602 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1603 }
1604};
1605
1607 bool isOpType(unsigned Opcode) const {
1608 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1609 }
1610};
1611
1613 bool isOpType(unsigned Opcode) const {
1614 return Instruction::isBitwiseLogicOp(Opcode);
1615 }
1616};
1617
1619 bool isOpType(unsigned Opcode) const {
1620 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1621 }
1622};
1623
1625 bool isOpType(unsigned Opcode) const {
1626 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1627 }
1628};
1629
1630/// Matches shift operations.
1631template <typename LHS, typename RHS>
1633 const RHS &R) {
1635}
1636
1637/// Matches logical shift operations.
1638template <typename LHS, typename RHS>
1640 const RHS &R) {
1642}
1643
1644/// Matches logical shift operations.
1645template <typename LHS, typename RHS>
1647m_LogicalShift(const LHS &L, const RHS &R) {
1649}
1650
1651/// Matches bitwise logic operations.
1652template <typename LHS, typename RHS>
1654m_BitwiseLogic(const LHS &L, const RHS &R) {
1656}
1657
1658/// Matches bitwise logic operations in either order.
1659template <typename LHS, typename RHS>
1661m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1663}
1664
1665/// Matches integer division operations.
1666template <typename LHS, typename RHS>
1668 const RHS &R) {
1670}
1671
1672/// Matches integer remainder operations.
1673template <typename LHS, typename RHS>
1675 const RHS &R) {
1677}
1678
1679//===----------------------------------------------------------------------===//
1680// Class that matches exact binary ops.
1681//
1682template <typename SubPattern_t> struct Exact_match {
1683 SubPattern_t SubPattern;
1684
1685 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1686
1687 template <typename OpTy> bool match(OpTy *V) const {
1688 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1689 return PEO->isExact() && SubPattern.match(V);
1690 return false;
1691 }
1692};
1693
1694template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1695 return SubPattern;
1696}
1697
1698//===----------------------------------------------------------------------===//
1699// Matchers for CmpInst classes
1700//
1701
1702template <typename LHS_t, typename RHS_t, typename Class,
1703 bool Commutable = false>
1708
1709 // The evaluation order is always stable, regardless of Commutability.
1710 // The LHS is always matched first.
1711 CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
1712 : Predicate(&Pred), L(LHS), R(RHS) {}
1713 CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
1714 : Predicate(nullptr), L(LHS), R(RHS) {}
1715
1716 template <typename OpTy> bool match(OpTy *V) const {
1717 if (auto *I = dyn_cast<Class>(V)) {
1718 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1719 if (Predicate)
1721 return true;
1722 }
1723 if (Commutable && L.match(I->getOperand(1)) &&
1724 R.match(I->getOperand(0))) {
1725 if (Predicate)
1727 return true;
1728 }
1729 }
1730 return false;
1731 }
1732};
1733
1734template <typename LHS, typename RHS>
1736 const RHS &R) {
1737 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1738}
1739
1740template <typename LHS, typename RHS>
1742 const LHS &L, const RHS &R) {
1743 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1744}
1745
1746template <typename LHS, typename RHS>
1748 const LHS &L, const RHS &R) {
1749 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1750}
1751
1752template <typename LHS, typename RHS>
1753inline CmpClass_match<LHS, RHS, CmpInst> m_Cmp(const LHS &L, const RHS &R) {
1755}
1756
1757template <typename LHS, typename RHS>
1758inline CmpClass_match<LHS, RHS, ICmpInst> m_ICmp(const LHS &L, const RHS &R) {
1760}
1761
1762template <typename LHS, typename RHS>
1763inline CmpClass_match<LHS, RHS, FCmpInst> m_FCmp(const LHS &L, const RHS &R) {
1765}
1766
1767// Same as CmpClass, but instead of saving Pred as out output variable, match a
1768// specific input pred for equality.
1769template <typename LHS_t, typename RHS_t, typename Class,
1770 bool Commutable = false>
1775
1776 SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
1777 : Predicate(Pred), L(LHS), R(RHS) {}
1778
1779 template <typename OpTy> bool match(OpTy *V) const {
1780 if (auto *I = dyn_cast<Class>(V)) {
1782 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1783 return true;
1784 if constexpr (Commutable) {
1787 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1788 return true;
1789 }
1790 }
1791
1792 return false;
1793 }
1794};
1795
1796template <typename LHS, typename RHS>
1798m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1799 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1800}
1801
1802template <typename LHS, typename RHS>
1804m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1805 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1806}
1807
1808template <typename LHS, typename RHS>
1810m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1812}
1813
1814template <typename LHS, typename RHS>
1816m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1817 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1818}
1819
1820//===----------------------------------------------------------------------===//
1821// Matchers for instructions with a given opcode and number of operands.
1822//
1823
1824/// Matches instructions with Opcode and three operands.
1825template <typename T0, unsigned Opcode> struct OneOps_match {
1827
1828 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1829
1830 template <typename OpTy> bool match(OpTy *V) const {
1831 if (V->getValueID() == Value::InstructionVal + Opcode) {
1832 auto *I = cast<Instruction>(V);
1833 return Op1.match(I->getOperand(0));
1834 }
1835 return false;
1836 }
1837};
1838
1839/// Matches instructions with Opcode and three operands.
1840template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1843
1844 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1845
1846 template <typename OpTy> bool match(OpTy *V) const {
1847 if (V->getValueID() == Value::InstructionVal + Opcode) {
1848 auto *I = cast<Instruction>(V);
1849 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1850 }
1851 return false;
1852 }
1853};
1854
1855/// Matches instructions with Opcode and three operands.
1856template <typename T0, typename T1, typename T2, unsigned Opcode,
1857 bool CommutableOp2Op3 = false>
1862
1863 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1864 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1865
1866 template <typename OpTy> bool match(OpTy *V) const {
1867 if (V->getValueID() == Value::InstructionVal + Opcode) {
1868 auto *I = cast<Instruction>(V);
1869 if (!Op1.match(I->getOperand(0)))
1870 return false;
1871 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1872 return true;
1873 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1874 Op3.match(I->getOperand(1));
1875 }
1876 return false;
1877 }
1878};
1879
1880/// Matches instructions with Opcode and any number of operands
1881template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1882 std::tuple<OperandTypes...> Operands;
1883
1884 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1885
1886 // Operand matching works by recursively calling match_operands, matching the
1887 // operands left to right. The first version is called for each operand but
1888 // the last, for which the second version is called. The second version of
1889 // match_operands is also used to match each individual operand.
1890 template <int Idx, int Last>
1891 std::enable_if_t<Idx != Last, bool>
1895
1896 template <int Idx, int Last>
1897 std::enable_if_t<Idx == Last, bool>
1899 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1900 }
1901
1902 template <typename OpTy> bool match(OpTy *V) const {
1903 if (V->getValueID() == Value::InstructionVal + Opcode) {
1904 auto *I = cast<Instruction>(V);
1905 return I->getNumOperands() == sizeof...(OperandTypes) &&
1906 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1907 }
1908 return false;
1909 }
1910};
1911
1912/// Matches SelectInst.
1913template <typename Cond, typename LHS, typename RHS>
1915m_Select(const Cond &C, const LHS &L, const RHS &R) {
1917}
1918
1919/// This matches a select of two constants, e.g.:
1920/// m_SelectCst<-1, 0>(m_Value(V))
1921template <int64_t L, int64_t R, typename Cond>
1923 Instruction::Select>
1926}
1927
1928/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1929template <typename LHS, typename RHS>
1930inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1931m_c_Select(const LHS &L, const RHS &R) {
1932 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1933 true>(m_Value(), L, R);
1934}
1935
1936/// Matches FreezeInst.
1937template <typename OpTy>
1941
1942/// Matches InsertElementInst.
1943template <typename Val_t, typename Elt_t, typename Idx_t>
1945m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1947 Val, Elt, Idx);
1948}
1949
1950/// Matches ExtractElementInst.
1951template <typename Val_t, typename Idx_t>
1953m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1955}
1956
1957/// Matches shuffle.
1958template <typename T0, typename T1, typename T2> struct Shuffle_match {
1962
1963 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1964 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1965
1966 template <typename OpTy> bool match(OpTy *V) const {
1967 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1968 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1969 Mask.match(I->getShuffleMask());
1970 }
1971 return false;
1972 }
1973};
1974
1975struct m_Mask {
1978 bool match(ArrayRef<int> Mask) const {
1979 MaskRef = Mask;
1980 return true;
1981 }
1982};
1983
1985 bool match(ArrayRef<int> Mask) const {
1986 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1987 }
1988};
1989
1993 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
1994};
1995
1997 bool match(ArrayRef<int> Mask) const { return all_equal(Mask); }
1998};
1999
2003 bool match(ArrayRef<int> Mask) const {
2004 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2005 if (First == Mask.end())
2006 return false;
2007 SplatIndex = *First;
2008 return all_of(Mask,
2009 [First](int Elem) { return Elem == *First || Elem == -1; });
2010 }
2011};
2012
2013template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2014 PointerOpTy PointerOp;
2015 OffsetOpTy OffsetOp;
2016
2017 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2019
2020 template <typename OpTy> bool match(OpTy *V) const {
2021 auto *GEP = dyn_cast<GEPOperator>(V);
2022 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2023 PointerOp.match(GEP->getPointerOperand()) &&
2024 OffsetOp.match(GEP->idx_begin()->get());
2025 }
2026};
2027
2028/// Matches ShuffleVectorInst independently of mask value.
2029template <typename V1_t, typename V2_t>
2031m_Shuffle(const V1_t &v1, const V2_t &v2) {
2033}
2034
2035template <typename V1_t, typename V2_t, typename Mask_t>
2037m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2039}
2040
2041/// Matches LoadInst.
2042template <typename OpTy>
2046
2047/// Matches StoreInst.
2048template <typename ValueOpTy, typename PointerOpTy>
2050m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2052 PointerOp);
2053}
2054
2055/// Matches GetElementPtrInst.
2056template <typename... OperandTypes>
2057inline auto m_GEP(const OperandTypes &...Ops) {
2058 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2059}
2060
2061/// Matches GEP with i8 source element type
2062template <typename PointerOpTy, typename OffsetOpTy>
2064m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2066}
2067
2068//===----------------------------------------------------------------------===//
2069// Matchers for CastInst classes
2070//
2071
2072template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2073 Op_t Op;
2074
2075 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2076
2077 template <typename OpTy> bool match(OpTy *V) const {
2078 if (auto *O = dyn_cast<Operator>(V))
2079 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2080 return false;
2081 }
2082};
2083
2084template <typename Op_t, typename Class> struct CastInst_match {
2085 Op_t Op;
2086
2087 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2088
2089 template <typename OpTy> bool match(OpTy *V) const {
2090 if (auto *I = dyn_cast<Class>(V))
2091 return Op.match(I->getOperand(0));
2092 return false;
2093 }
2094};
2095
2096template <typename Op_t> struct PtrToIntSameSize_match {
2098 Op_t Op;
2099
2100 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2101 : DL(DL), Op(OpMatch) {}
2102
2103 template <typename OpTy> bool match(OpTy *V) const {
2104 if (auto *O = dyn_cast<Operator>(V))
2105 return O->getOpcode() == Instruction::PtrToInt &&
2106 DL.getTypeSizeInBits(O->getType()) ==
2107 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2108 Op.match(O->getOperand(0));
2109 return false;
2110 }
2111};
2112
2113template <typename Op_t> struct NNegZExt_match {
2114 Op_t Op;
2115
2116 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2117
2118 template <typename OpTy> bool match(OpTy *V) const {
2119 if (auto *I = dyn_cast<ZExtInst>(V))
2120 return I->hasNonNeg() && Op.match(I->getOperand(0));
2121 return false;
2122 }
2123};
2124
2125template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2126 Op_t Op;
2127
2128 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2129
2130 template <typename OpTy> bool match(OpTy *V) const {
2131 if (auto *I = dyn_cast<TruncInst>(V))
2132 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2133 Op.match(I->getOperand(0));
2134 return false;
2135 }
2136};
2137
2138/// Matches BitCast.
2139template <typename OpTy>
2144
2145template <typename Op_t> struct ElementWiseBitCast_match {
2146 Op_t Op;
2147
2148 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2149
2150 template <typename OpTy> bool match(OpTy *V) const {
2151 auto *I = dyn_cast<BitCastInst>(V);
2152 if (!I)
2153 return false;
2154 Type *SrcType = I->getSrcTy();
2155 Type *DstType = I->getType();
2156 // Make sure the bitcast doesn't change between scalar and vector and
2157 // doesn't change the number of vector elements.
2158 if (SrcType->isVectorTy() != DstType->isVectorTy())
2159 return false;
2160 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2161 SrcVecTy && SrcVecTy->getElementCount() !=
2162 cast<VectorType>(DstType)->getElementCount())
2163 return false;
2164 return Op.match(I->getOperand(0));
2165 }
2166};
2167
2168template <typename OpTy>
2172
2173/// Matches PtrToInt.
2174template <typename OpTy>
2179
2180template <typename OpTy>
2185
2186/// Matches PtrToAddr.
2187template <typename OpTy>
2192
2193/// Matches PtrToInt or PtrToAddr.
2194template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2196}
2197
2198/// Matches IntToPtr.
2199template <typename OpTy>
2204
2205/// Matches any cast or self. Used to ignore casts.
2206template <typename OpTy>
2208m_CastOrSelf(const OpTy &Op) {
2210}
2211
2212/// Matches Trunc.
2213template <typename OpTy>
2217
2218/// Matches trunc nuw.
2219template <typename OpTy>
2224
2225/// Matches trunc nsw.
2226template <typename OpTy>
2231
2232template <typename OpTy>
2234m_TruncOrSelf(const OpTy &Op) {
2235 return m_CombineOr(m_Trunc(Op), Op);
2236}
2237
2238/// Matches SExt.
2239template <typename OpTy>
2243
2244/// Matches ZExt.
2245template <typename OpTy>
2249
2250template <typename OpTy>
2252 return NNegZExt_match<OpTy>(Op);
2253}
2254
2255template <typename OpTy>
2257m_ZExtOrSelf(const OpTy &Op) {
2258 return m_CombineOr(m_ZExt(Op), Op);
2259}
2260
2261template <typename OpTy>
2263m_SExtOrSelf(const OpTy &Op) {
2264 return m_CombineOr(m_SExt(Op), Op);
2265}
2266
2267/// Match either "sext" or "zext nneg".
2268template <typename OpTy>
2270m_SExtLike(const OpTy &Op) {
2271 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2272}
2273
2274template <typename OpTy>
2277m_ZExtOrSExt(const OpTy &Op) {
2278 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2279}
2280
2281template <typename OpTy>
2284 OpTy>
2286 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2287}
2288
2289template <typename OpTy> inline auto m_ZExtOrTruncOrSelf(const OpTy &Op) {
2290 return m_CombineOr(m_ZExt(Op), m_Trunc(Op), Op);
2291}
2292
2293template <typename LHS_t, typename RHS_t> struct ICmpLike_match {
2297
2299 : Pred(P), L(Left), R(Right) {}
2300
2301 template <typename OpTy> bool match(OpTy *V) const {
2302 if (PatternMatch::match(V, m_ICmp(Pred, L, R)))
2303 return true;
2304 Value *A;
2305 // trunc nuw x to i1 is equivalent to icmp ne x, 0
2306 if (V->getType()->isIntOrIntVectorTy(1) &&
2307 PatternMatch::match(V, m_NUWTrunc(m_Value(A))) && L.match(A) &&
2308 R.match(ConstantInt::getNullValue(A->getType()))) {
2310 return true;
2311 }
2312 return false;
2313 }
2314};
2315
2316template <typename LHS, typename RHS>
2318 const RHS &R) {
2319 return ICmpLike_match<LHS, RHS>(Pred, L, R);
2320}
2321
2322template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2323 CondTy Cond;
2326
2327 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2328 : Cond(C), TrueC(TC), FalseC(FC) {}
2329
2330 template <typename OpTy> bool match(OpTy *V) const {
2331 // select(Cond, TrueC, FalseC) — captures both constants directly
2333 return true;
2334
2335 Type *Ty = V->getType();
2336 Value *CondV = nullptr;
2337
2338 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2339 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2340 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2341 TrueC.match(ConstantInt::get(Ty, 1)) &&
2342 FalseC.match(ConstantInt::get(Ty, 0)))
2343 return true;
2344
2345 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2346 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2347 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2348 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2349 FalseC.match(ConstantInt::get(Ty, 0)))
2350 return true;
2351
2352 return false;
2353 }
2354};
2355
2356/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2357/// select i1 Cond, TrueC, FalseC
2358/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2359/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2360///
2361/// The condition is matched against \p Cond, and the true/false constants
2362/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2363/// constants are bound to \p TrueC and \p FalseC via their matchers.
2364template <typename CondTy, typename LTy, typename RTy>
2366m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2367 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2368}
2369
2370template <typename OpTy>
2374
2375template <typename OpTy>
2379
2380template <typename OpTy>
2383m_IToFP(const OpTy &Op) {
2384 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2385}
2386
2387template <typename OpTy>
2391
2392template <typename OpTy>
2396
2397template <typename OpTy>
2400m_FPToI(const OpTy &Op) {
2401 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2402}
2403
2404template <typename OpTy>
2408
2409template <typename OpTy>
2413
2414//===----------------------------------------------------------------------===//
2415// Matchers for control flow.
2416//
2417
2418struct br_match {
2420
2422
2423 template <typename OpTy> bool match(OpTy *V) const {
2424 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2425 Succ = BI->getSuccessor();
2426 return true;
2427 }
2428 return false;
2429 }
2430};
2431
2432inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2433
2434template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2436 Cond_t Cond;
2437 TrueBlock_t T;
2438 FalseBlock_t F;
2439
2440 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2441 : Cond(C), T(t), F(f) {}
2442
2443 template <typename OpTy> bool match(OpTy *V) const {
2444 if (auto *BI = dyn_cast<CondBrInst>(V))
2445 if (Cond.match(BI->getCondition()))
2446 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2447 return false;
2448 }
2449};
2450
2451template <typename Cond_t>
2457
2458template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2460m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2462}
2463
2464//===----------------------------------------------------------------------===//
2465// Matchers for fmax/fmin idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2466//
2467
2468template <typename LHS_t, typename RHS_t, typename Pred_t>
2470 using PredType = Pred_t;
2473
2474 // The evaluation order is always stable, regardless of Commutability.
2475 // The LHS is always matched first.
2476 FMaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2477
2478 template <typename OpTy> bool match(OpTy *V) const {
2479 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2480 auto *SI = dyn_cast<SelectInst>(V);
2481 if (!SI)
2482 return false;
2483 auto *Cmp = dyn_cast<FCmpInst>(SI->getCondition());
2484 if (!Cmp)
2485 return false;
2486 // At this point we have a select conditioned on a comparison. Check that
2487 // it is the values returned by the select that are being compared.
2488 auto *TrueVal = SI->getTrueValue();
2489 auto *FalseVal = SI->getFalseValue();
2490 auto *LHS = Cmp->getOperand(0);
2491 auto *RHS = Cmp->getOperand(1);
2492 if ((TrueVal != LHS || FalseVal != RHS) &&
2493 (TrueVal != RHS || FalseVal != LHS))
2494 return false;
2495 FCmpInst::Predicate Pred =
2496 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2497 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2498 if (!Pred_t::match(Pred))
2499 return false;
2500 // It does! Bind the operands.
2501 return L.match(LHS) && R.match(RHS);
2502 }
2503};
2504
2505/// Helper class for identifying ordered max predicates.
2507 static bool match(FCmpInst::Predicate Pred) {
2508 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2509 }
2510};
2511
2512/// Helper class for identifying ordered min predicates.
2514 static bool match(FCmpInst::Predicate Pred) {
2515 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2516 }
2517};
2518
2519/// Helper class for identifying unordered max predicates.
2521 static bool match(FCmpInst::Predicate Pred) {
2522 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2523 }
2524};
2525
2526/// Helper class for identifying unordered min predicates.
2528 static bool match(FCmpInst::Predicate Pred) {
2529 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2530 }
2531};
2532
2533/// Match an 'ordered' floating point maximum function.
2534/// Floating point has one special value 'NaN'. Therefore, there is no total
2535/// order. However, if we can ignore the 'NaN' value (for example, because of a
2536/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2537/// semantics. In the presence of 'NaN' we have to preserve the original
2538/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2539///
2540/// max(L, R) iff L and R are not NaN
2541/// m_OrdFMax(L, R) = R iff L or R are NaN
2542template <typename LHS, typename RHS>
2544 const RHS &R) {
2546}
2547
2548/// Match an 'ordered' floating point minimum function.
2549/// Floating point has one special value 'NaN'. Therefore, there is no total
2550/// order. However, if we can ignore the 'NaN' value (for example, because of a
2551/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2552/// semantics. In the presence of 'NaN' we have to preserve the original
2553/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2554///
2555/// min(L, R) iff L and R are not NaN
2556/// m_OrdFMin(L, R) = R iff L or R are NaN
2557template <typename LHS, typename RHS>
2559 const RHS &R) {
2561}
2562
2563/// Match an 'unordered' floating point maximum function.
2564/// Floating point has one special value 'NaN'. Therefore, there is no total
2565/// order. However, if we can ignore the 'NaN' value (for example, because of a
2566/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2567/// semantics. In the presence of 'NaN' we have to preserve the original
2568/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2569///
2570/// max(L, R) iff L and R are not NaN
2571/// m_UnordFMax(L, R) = L iff L or R are NaN
2572template <typename LHS, typename RHS>
2574 const RHS &R) {
2576}
2577
2578/// Match an 'unordered' floating point minimum function.
2579/// Floating point has one special value 'NaN'. Therefore, there is no total
2580/// order. However, if we can ignore the 'NaN' value (for example, because of a
2581/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2582/// semantics. In the presence of 'NaN' we have to preserve the original
2583/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2584///
2585/// min(L, R) iff L and R are not NaN
2586/// m_UnordFMin(L, R) = L iff L or R are NaN
2587template <typename LHS, typename RHS>
2589 const RHS &R) {
2591}
2592
2593/// Match an 'ordered' or 'unordered' floating point maximum function.
2594/// Floating point has one special value 'NaN'. Therefore, there is no total
2595/// order. However, if we can ignore the 'NaN' value (for example, because of a
2596/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2597/// semantics.
2598template <typename LHS, typename RHS>
2601m_OrdOrUnordFMax(const LHS &L, const RHS &R) {
2604}
2605
2606/// Match an 'ordered' or 'unordered' floating point minimum function.
2607/// Floating point has one special value 'NaN'. Therefore, there is no total
2608/// order. However, if we can ignore the 'NaN' value (for example, because of a
2609/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2610/// semantics.
2611template <typename LHS, typename RHS>
2614m_OrdOrUnordFMin(const LHS &L, const RHS &R) {
2617}
2618
2619/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2620/// NOTE: we first match the 'Not' (by matching '-1'),
2621/// and only then match the inner matcher!
2622template <typename ValTy>
2623inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2624m_Not(const ValTy &V) {
2625 return m_c_Xor(m_AllOnes(), V);
2626}
2627
2628template <typename ValTy>
2629inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2630 true>
2631m_NotForbidPoison(const ValTy &V) {
2632 return m_c_Xor(m_AllOnesForbidPoison(), V);
2633}
2634
2635//===----------------------------------------------------------------------===//
2636// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2637// Note that S might be matched to other instructions than AddInst.
2638//
2639
2640template <typename LHS_t, typename RHS_t, typename Sum_t>
2644 Sum_t S;
2645
2646 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2647 : L(L), R(R), S(S) {}
2648
2649 template <typename OpTy> bool match(OpTy *V) const {
2650 Value *ICmpLHS, *ICmpRHS;
2651 CmpPredicate Pred;
2652 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2653 return false;
2654
2655 Value *AddLHS, *AddRHS;
2656 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2657
2658 // (a + b) u< a, (a + b) u< b
2659 if (Pred == ICmpInst::ICMP_ULT)
2660 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2661 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2662
2663 // a >u (a + b), b >u (a + b)
2664 if (Pred == ICmpInst::ICMP_UGT)
2665 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2666 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2667
2668 Value *Op1;
2669 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2670 // (~a) <u b
2671 if (Pred == ICmpInst::ICMP_ULT) {
2672 if (XorExpr.match(ICmpLHS))
2673 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2674 }
2675 // b > u (~a)
2676 if (Pred == ICmpInst::ICMP_UGT) {
2677 if (XorExpr.match(ICmpRHS))
2678 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2679 }
2680
2681 // Match special-case for increment-by-1.
2682 if (Pred == ICmpInst::ICMP_EQ) {
2683 // (a + 1) == 0
2684 // (1 + a) == 0
2685 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2686 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2687 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2688 // 0 == (a + 1)
2689 // 0 == (1 + a)
2690 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2691 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2692 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2693 }
2694
2695 return false;
2696 }
2697};
2698
2699/// Match an icmp instruction checking for unsigned overflow on addition.
2700///
2701/// S is matched to the addition whose result is being checked for overflow, and
2702/// L and R are matched to the LHS and RHS of S.
2703template <typename LHS_t, typename RHS_t, typename Sum_t>
2705m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2707}
2708
2709template <typename Opnd_t> struct Argument_match {
2710 unsigned OpI;
2711 Opnd_t Val;
2712
2713 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2714
2715 template <typename OpTy> bool match(OpTy *V) const {
2716 // FIXME: Should likely be switched to use `CallBase`.
2717 if (const auto *CI = dyn_cast<CallInst>(V))
2718 return Val.match(CI->getArgOperand(OpI));
2719 return false;
2720 }
2721};
2722
2723/// Match an argument.
2724template <unsigned OpI, typename Opnd_t>
2725inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2726 return Argument_match<Opnd_t>(OpI, Op);
2727}
2728
2729/// Intrinsic matchers.
2731 unsigned ID;
2732
2734
2735 template <typename OpTy> bool match(OpTy *V) const {
2736 if (const auto *CI = dyn_cast<CallInst>(V))
2737 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2738 return F->getIntrinsicID() == ID;
2739 return false;
2740 }
2741};
2742
2743/// Match intrinsic calls with any of the given IDs.
2744template <Intrinsic::ID... IntrIDs> struct IntrinsicIDs_match {
2745 template <typename OpTy> bool match(OpTy *V) const {
2746 if (const auto *CI = dyn_cast<CallInst>(V))
2747 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand())) {
2748 Intrinsic::ID ID = F->getIntrinsicID();
2749 return ((ID == IntrIDs) || ...);
2750 }
2751 return false;
2752 }
2753};
2754
2755/// Intrinsic matches are combinations of ID matchers, and argument
2756/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2757/// them with lower arity matchers. Here's some convenient typedefs for up to
2758/// several arguments, and more can be added as needed
2759template <typename T0 = void, typename T1 = void, typename T2 = void,
2760 typename T3 = void, typename T4 = void, typename T5 = void,
2761 typename T6 = void, typename T7 = void, typename T8 = void,
2762 typename T9 = void, typename T10 = void>
2764template <typename T0> struct m_Intrinsic_Ty<T0> {
2766};
2767template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2768 using Ty =
2770};
2771template <typename T0, typename T1, typename T2>
2776template <typename T0, typename T1, typename T2, typename T3>
2781
2782template <typename T0, typename T1, typename T2, typename T3, typename T4>
2787
2788template <typename T0, typename T1, typename T2, typename T3, typename T4,
2789 typename T5>
2794
2795/// Match intrinsic calls like this:
2796/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2797template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2798 return IntrinsicID_match(IntrID);
2799}
2800
2801/// Match intrinsic calls with any of the given IDs like this:
2802/// m_AnyIntrinsic<Intrinsic::fptosi_sat, Intrinsic::fptoui_sat>()
2803/// This is more efficient than using nested m_CombineOr with m_Intrinsic
2804/// because it performs the CallInst/Function cast only once.
2805template <Intrinsic::ID... IntrIDs>
2807 return IntrinsicIDs_match<IntrIDs...>();
2808}
2809
2810/// Matches MaskedLoad Intrinsic.
2811template <typename Opnd0, typename Opnd1, typename Opnd2>
2813m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2814 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2815}
2816
2817/// Matches MaskedStore Intrinsic.
2818template <typename Opnd0, typename Opnd1, typename Opnd2>
2820m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2821 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2822}
2823
2824/// Matches MaskedGather Intrinsic.
2825template <typename Opnd0, typename Opnd1, typename Opnd2>
2827m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2828 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2829}
2830
2831template <Intrinsic::ID IntrID, typename T0>
2832inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2834}
2835
2836template <Intrinsic::ID IntrID, typename T0, typename T1>
2837inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2838 const T1 &Op1) {
2840}
2841
2842template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2843inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2844m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2845 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2846}
2847
2848template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2849 typename T3>
2851m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2852 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2853}
2854
2855template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2856 typename T3, typename T4>
2858m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2859 const T4 &Op4) {
2860 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2861 m_Argument<4>(Op4));
2862}
2863
2864template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2865 typename T3, typename T4, typename T5>
2867m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2868 const T4 &Op4, const T5 &Op5) {
2869 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2870 m_Argument<5>(Op5));
2871}
2872
2873// Helper intrinsic matching specializations.
2874template <typename Opnd0>
2875inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2877}
2878
2879template <typename Opnd0>
2880inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2882}
2883template <typename Opnd0>
2884inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Ctpop(const Opnd0 &Op0) {
2886}
2887
2888template <typename Opnd0>
2889inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2890 return m_Intrinsic<Intrinsic::fabs>(Op0);
2891}
2892
2893template <typename Opnd0>
2894inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2896}
2897
2898template <typename Opnd0, typename Opnd1>
2899inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_Ctlz(const Opnd0 &Op0,
2900 const Opnd1 &Op1) {
2901 return m_Intrinsic<Intrinsic::ctlz>(Op0, Op1);
2902}
2903
2904template <typename Opnd0, typename Opnd1>
2905inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_Cttz(const Opnd0 &Op0,
2906 const Opnd1 &Op1) {
2907 return m_Intrinsic<Intrinsic::cttz>(Op0, Op1);
2908}
2909
2910template <typename Opnd0, typename Opnd1>
2911inline auto m_SMax(const Opnd0 &Op0, const Opnd1 &Op1) {
2912 return m_Intrinsic<Intrinsic::smax>(Op0, Op1);
2913}
2914
2915template <typename Opnd0, typename Opnd1>
2916inline auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1) {
2917 return m_Intrinsic<Intrinsic::smin>(Op0, Op1);
2918}
2919
2920template <typename Opnd0, typename Opnd1>
2921inline auto m_UMax(const Opnd0 &Op0, const Opnd1 &Op1) {
2922 return m_Intrinsic<Intrinsic::umax>(Op0, Op1);
2923}
2924
2925template <typename Opnd0, typename Opnd1>
2926inline auto m_UMin(const Opnd0 &Op0, const Opnd1 &Op1) {
2927 return m_Intrinsic<Intrinsic::umin>(Op0, Op1);
2928}
2929
2930template <typename Opnd0, typename Opnd1>
2931inline auto m_MaxOrMin(const Opnd0 &Op0, const Opnd1 &Op1) {
2932 return m_CombineOr(m_SMax(Op0, Op1), m_SMin(Op0, Op1), m_UMax(Op0, Op1),
2933 m_UMin(Op0, Op1));
2934}
2935
2936template <typename Opnd0, typename Opnd1>
2937inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2938 const Opnd1 &Op1) {
2939 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2940}
2941
2942template <typename Opnd0, typename Opnd1>
2943inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2944 const Opnd1 &Op1) {
2945 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2946}
2947
2948template <typename Opnd0, typename Opnd1>
2950m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2951 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2952}
2953
2954template <typename Opnd0, typename Opnd1>
2955inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2956 const Opnd1 &Op1) {
2957 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2958}
2959
2960template <typename Opnd0, typename Opnd1>
2961inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2962 const Opnd1 &Op1) {
2963 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2964}
2965
2966template <typename Opnd0, typename Opnd1>
2968m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2969 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2970}
2971
2972template <typename Opnd0, typename Opnd1>
2975m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2976 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
2977}
2978
2979template <typename Opnd0, typename Opnd1>
2982m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2983 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
2984}
2985
2986template <typename Opnd0, typename Opnd1, typename Opnd2>
2988m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2989 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2990}
2991
2992template <typename Opnd0, typename Opnd1, typename Opnd2>
2994m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2995 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2996}
2997
2998template <typename Opnd0>
2999inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3000 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3001}
3002
3003template <typename Opnd0, typename Opnd1>
3004inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3005 const Opnd1 &Op1) {
3006 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3007}
3008
3009template <typename Opnd0>
3010inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3012}
3013
3014template <typename Opnd0, typename Opnd1, typename Opnd2>
3016m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3017 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3018}
3019
3020//===----------------------------------------------------------------------===//
3021// Matchers for two-operands operators with the operators in either order
3022//
3023
3024/// Matches a BinaryOperator with LHS and RHS in either order.
3025template <typename LHS, typename RHS>
3026inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
3028}
3029
3030/// Matches an ICmp with a predicate over LHS and RHS in either order.
3031/// Swaps the predicate if operands are commuted.
3032template <typename LHS, typename RHS>
3034m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3036}
3037
3038template <typename LHS, typename RHS>
3040 const RHS &R) {
3042}
3043
3044/// Matches a specific opcode with LHS and RHS in either order.
3045template <typename LHS, typename RHS>
3047m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3048 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3049}
3050
3051/// Matches a Add with LHS and RHS in either order.
3052template <typename LHS, typename RHS>
3057
3058/// Matches a Mul with LHS and RHS in either order.
3059template <typename LHS, typename RHS>
3064
3065/// Matches an And with LHS and RHS in either order.
3066template <typename LHS, typename RHS>
3071
3072/// Matches an Or with LHS and RHS in either order.
3073template <typename LHS, typename RHS>
3075 const RHS &R) {
3077}
3078
3079/// Matches an Xor with LHS and RHS in either order.
3080template <typename LHS, typename RHS>
3085
3086/// Matches a 'Neg' as 'sub 0, V'.
3087template <typename ValTy>
3088inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3089m_Neg(const ValTy &V) {
3090 return m_Sub(m_ZeroInt(), V);
3091}
3092
3093/// Matches a 'Neg' as 'sub nsw 0, V'.
3094template <typename ValTy>
3096 Instruction::Sub,
3098m_NSWNeg(const ValTy &V) {
3099 return m_NSWSub(m_ZeroInt(), V);
3100}
3101
3102template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3104 LHS L;
3105 RHS R;
3106
3107 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3108
3109 template <typename OpTy> bool match(OpTy *V) const {
3110 const auto *II = dyn_cast<IntrinsicInst>(V);
3111 if (!II || II->getIntrinsicID() != IntrID)
3112 return false;
3113 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3114 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3115 }
3116};
3117
3118template <Intrinsic::ID IntrID, typename T0, typename T1>
3120m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3122}
3123
3124/// Matches an SMin with LHS and RHS in either order.
3125template <typename LHS, typename RHS>
3126inline auto m_c_SMin(const LHS &L, const RHS &R) {
3127 return m_c_Intrinsic<Intrinsic::smin>(L, R);
3128}
3129/// Matches an SMax with LHS and RHS in either order.
3130template <typename LHS, typename RHS>
3131inline auto m_c_SMax(const LHS &L, const RHS &R) {
3132 return m_c_Intrinsic<Intrinsic::smax>(L, R);
3133}
3134/// Matches a UMin with LHS and RHS in either order.
3135template <typename LHS, typename RHS>
3136inline auto m_c_UMin(const LHS &L, const RHS &R) {
3137 return m_c_Intrinsic<Intrinsic::umin>(L, R);
3138}
3139/// Matches a UMax with LHS and RHS in either order.
3140template <typename LHS, typename RHS>
3141inline auto m_c_UMax(const LHS &L, const RHS &R) {
3142 return m_c_Intrinsic<Intrinsic::umax>(L, R);
3143}
3144
3145template <typename LHS, typename RHS>
3146inline auto m_c_MaxOrMin(const LHS &L, const RHS &R) {
3147 return m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R), m_c_UMax(L, R),
3148 m_c_UMin(L, R));
3149}
3150
3151/// Matches FAdd with LHS and RHS in either order.
3152template <typename LHS, typename RHS>
3154m_c_FAdd(const LHS &L, const RHS &R) {
3156}
3157
3158/// Matches FMul with LHS and RHS in either order.
3159template <typename LHS, typename RHS>
3161m_c_FMul(const LHS &L, const RHS &R) {
3163}
3164
3165template <typename Opnd_t> struct Signum_match {
3166 Opnd_t Val;
3167 Signum_match(const Opnd_t &V) : Val(V) {}
3168
3169 template <typename OpTy> bool match(OpTy *V) const {
3170 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3171 if (TypeSize == 0)
3172 return false;
3173
3174 unsigned ShiftWidth = TypeSize - 1;
3175 Value *Op;
3176
3177 // This is the representation of signum we match:
3178 //
3179 // signum(x) == (x >> 63) | (-x >>u 63)
3180 //
3181 // An i1 value is its own signum, so it's correct to match
3182 //
3183 // signum(x) == (x >> 0) | (-x >>u 0)
3184 //
3185 // for i1 values.
3186
3187 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3188 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3189 auto Signum = m_c_Or(LHS, RHS);
3190
3191 return Signum.match(V) && Val.match(Op);
3192 }
3193};
3194
3195/// Matches a signum pattern.
3196///
3197/// signum(x) =
3198/// x > 0 -> 1
3199/// x == 0 -> 0
3200/// x < 0 -> -1
3201template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3202 return Signum_match<Val_t>(V);
3203}
3204
3205template <int Ind, typename Opnd_t> struct ExtractValue_match {
3206 Opnd_t Val;
3207 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3208
3209 template <typename OpTy> bool match(OpTy *V) const {
3210 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3211 // If Ind is -1, don't inspect indices
3212 if (Ind != -1 &&
3213 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3214 return false;
3215 return Val.match(I->getAggregateOperand());
3216 }
3217 return false;
3218 }
3219};
3220
3221/// Match a single index ExtractValue instruction.
3222/// For example m_ExtractValue<1>(...)
3223template <int Ind, typename Val_t>
3227
3228/// Match an ExtractValue instruction with any index.
3229/// For example m_ExtractValue(...)
3230template <typename Val_t>
3231inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3232 return ExtractValue_match<-1, Val_t>(V);
3233}
3234
3235/// Matcher for a single index InsertValue instruction.
3236template <int Ind, typename T0, typename T1> struct InsertValue_match {
3239
3240 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3241
3242 template <typename OpTy> bool match(OpTy *V) const {
3243 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3244 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3245 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3246 }
3247 return false;
3248 }
3249};
3250
3251/// Matches a single index InsertValue instruction.
3252template <int Ind, typename Val_t, typename Elt_t>
3254 const Elt_t &Elt) {
3255 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3256}
3257
3258/// Matches a call to `llvm.vscale()`.
3260
3261template <typename Opnd0, typename Opnd1>
3263m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3265}
3266
3267template <typename Opnd>
3271
3272template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3274 LHS L;
3275 RHS R;
3276
3277 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3278
3279 template <typename T> bool match(T *V) const {
3280 auto *I = dyn_cast<Instruction>(V);
3281 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3282 return false;
3283
3284 if (I->getOpcode() == Opcode) {
3285 auto *Op0 = I->getOperand(0);
3286 auto *Op1 = I->getOperand(1);
3287 return (L.match(Op0) && R.match(Op1)) ||
3288 (Commutable && L.match(Op1) && R.match(Op0));
3289 }
3290
3291 if (auto *Select = dyn_cast<SelectInst>(I)) {
3292 auto *Cond = Select->getCondition();
3293 auto *TVal = Select->getTrueValue();
3294 auto *FVal = Select->getFalseValue();
3295
3296 // Don't match a scalar select of bool vectors.
3297 // Transforms expect a single type for operands if this matches.
3298 if (Cond->getType() != Select->getType())
3299 return false;
3300
3301 if (Opcode == Instruction::And) {
3302 auto *C = dyn_cast<Constant>(FVal);
3303 if (C && C->isNullValue())
3304 return (L.match(Cond) && R.match(TVal)) ||
3305 (Commutable && L.match(TVal) && R.match(Cond));
3306 } else {
3307 assert(Opcode == Instruction::Or);
3308 auto *C = dyn_cast<Constant>(TVal);
3309 if (C && C->isOneValue())
3310 return (L.match(Cond) && R.match(FVal)) ||
3311 (Commutable && L.match(FVal) && R.match(Cond));
3312 }
3313 }
3314
3315 return false;
3316 }
3317};
3318
3319/// Matches L && R either in the form of L & R or L ? R : false.
3320/// Note that the latter form is poison-blocking.
3321template <typename LHS, typename RHS>
3323 const RHS &R) {
3325}
3326
3327/// Matches L && R where L and R are arbitrary values.
3328inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3329
3330/// Matches L && R with LHS and RHS in either order.
3331template <typename LHS, typename RHS>
3333m_c_LogicalAnd(const LHS &L, const RHS &R) {
3335}
3336
3337/// Matches L || R either in the form of L | R or L ? true : R.
3338/// Note that the latter form is poison-blocking.
3339template <typename LHS, typename RHS>
3341 const RHS &R) {
3343}
3344
3345/// Matches L || R where L and R are arbitrary values.
3346inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3347
3348/// Matches L || R with LHS and RHS in either order.
3349template <typename LHS, typename RHS>
3351m_c_LogicalOr(const LHS &L, const RHS &R) {
3353}
3354
3355/// Matches either L && R or L || R,
3356/// either one being in the either binary or logical form.
3357/// Note that the latter form is poison-blocking.
3358template <typename LHS, typename RHS, bool Commutable = false>
3364
3365/// Matches either L && R or L || R where L and R are arbitrary values.
3366inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3367
3368/// Matches either L && R or L || R with LHS and RHS in either order.
3369template <typename LHS, typename RHS>
3370inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3371 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3372}
3373
3374} // end namespace PatternMatch
3375} // end namespace llvm
3376
3377#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")
#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
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1575
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ 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.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
ShiftLike_match< LHS, Instruction::LShr > m_LShrOrSelf(const LHS &L, uint64_t &R)
Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
AllowFmf_match< T, FastMathFlags::NoSignedZeros > m_NoSignedZeros(const T &SubPattern)
auto m_Cmp()
Matches any compare instruction and ignore it.
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
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.
match_combine_or< typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty, typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty > m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, true > m_c_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, CastInst >, OpTy > m_CastOrSelf(const OpTy &Op)
Matches any cast or self. Used to ignore casts.
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
CommutativeBinaryIntrinsic_match< IntrID, T0, T1 > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
auto m_Poison()
Match an arbitrary poison constant.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
ap_match< APFloat > m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
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)
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
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
cstfp_pred_ty< is_signed_inf< true > > m_NegInf()
Match a negative infinity FP constant.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_c_XorLike(const LHS &L, const RHS &R)
Match either (xor L, R), (xor R, L) or (sub nuw R, L) iff R.isMask() Only commutative matcher as the ...
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
ap_match< APFloat > m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
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.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
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.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_or< CastInst_match< OpTy, UIToFPInst >, CastInst_match< OpTy, SIToFPInst > > m_IToFP(const OpTy &Op)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
ICmpLike_match< LHS, RHS > m_ICmpLike(CmpPredicate &Pred, const LHS &L, const RHS &R)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
auto m_Value()
Match an arbitrary value and ignore it.
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_UndefValue()
Match an arbitrary UndefValue constant.
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
auto m_Constant()
Match an arbitrary Constant and ignore it.
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.
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)
FMaxMin_match< LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
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)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
AllowFmf_match< T, FastMathFlags::ApproxFunc > m_ApproxFunc(const T &SubPattern)
cstfp_pred_ty< is_signed_inf< false > > m_PosInf()
Match a positive infinity FP constant.
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
auto m_ZExtOrTruncOrSelf(const OpTy &Op)
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
ShiftLike_match< LHS, Instruction::AShr > m_AShrOrSelf(const LHS &L, uint64_t &R)
Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedGather Intrinsic.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
auto m_c_MaxOrMin(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
m_Intrinsic_Ty< Opnd0 >::Ty m_Ctpop(const Opnd0 &Op0)
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
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)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Cttz(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_MaxOrMin(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
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)
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Ctlz(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
auto m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
match_combine_or< typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty, typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty > m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
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)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
AllowFmf_match< T, FastMathFlags::AllowReciprocal > m_AllowReciprocal(const T &SubPattern)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
m_Intrinsic_Ty< Opnd >::Ty m_Deinterleave2(const Opnd &Op)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr auto bind_back(FnT &&Fn, BindArgsT &&...BindArgs)
C++23 bind_back.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
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.
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)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3, T4 >::Ty, Argument_match< T5 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3 >::Ty, Argument_match< T4 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1 >::Ty, Argument_match< T2 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0 >::Ty, Argument_match< T1 > > Ty
match_combine_and< IntrinsicID_match, Argument_match< T0 > > Ty
Intrinsic matches are combinations of ID matchers, and argument matchers.
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
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)