LLVM 19.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/InstrTypes.h"
37#include "llvm/IR/Instruction.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Operator.h"
42#include "llvm/IR/Value.h"
44#include <cstdint>
45
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return const_cast<Pattern &>(P).match(V);
51}
52
53template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
54 return const_cast<Pattern &>(P).match(Mask);
55}
56
57template <typename SubPattern_t> struct OneUse_match {
58 SubPattern_t SubPattern;
59
60 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
61
62 template <typename OpTy> bool match(OpTy *V) {
63 return V->hasOneUse() && SubPattern.match(V);
64 }
65};
66
67template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
68 return SubPattern;
69}
70
71template <typename SubPattern_t> struct AllowReassoc_match {
72 SubPattern_t SubPattern;
73
74 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
75
76 template <typename OpTy> bool match(OpTy *V) {
77 auto *I = dyn_cast<FPMathOperator>(V);
78 return I && I->hasAllowReassoc() && SubPattern.match(I);
79 }
80};
81
82template <typename T>
83inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
84 return SubPattern;
85}
86
87template <typename Class> struct class_match {
88 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
89};
90
91/// Match an arbitrary value and ignore it.
93
94/// Match an arbitrary unary operation and ignore it.
97}
98
99/// Match an arbitrary binary operation and ignore it.
102}
103
104/// Matches any compare instruction and ignore it.
106
108 static bool check(const Value *V) {
109 if (isa<UndefValue>(V))
110 return true;
111
112 const auto *CA = dyn_cast<ConstantAggregate>(V);
113 if (!CA)
114 return false;
115
118
119 // Either UndefValue, PoisonValue, or an aggregate that only contains
120 // these is accepted by matcher.
121 // CheckValue returns false if CA cannot satisfy this constraint.
122 auto CheckValue = [&](const ConstantAggregate *CA) {
123 for (const Value *Op : CA->operand_values()) {
124 if (isa<UndefValue>(Op))
125 continue;
126
127 const auto *CA = dyn_cast<ConstantAggregate>(Op);
128 if (!CA)
129 return false;
130 if (Seen.insert(CA).second)
131 Worklist.emplace_back(CA);
132 }
133
134 return true;
135 };
136
137 if (!CheckValue(CA))
138 return false;
139
140 while (!Worklist.empty()) {
141 if (!CheckValue(Worklist.pop_back_val()))
142 return false;
143 }
144 return true;
145 }
146 template <typename ITy> bool match(ITy *V) { return check(V); }
147};
148
149/// Match an arbitrary undef constant. This matches poison as well.
150/// If this is an aggregate and contains a non-aggregate element that is
151/// neither undef nor poison, the aggregate is not matched.
152inline auto m_Undef() { return undef_match(); }
153
154/// Match an arbitrary UndefValue constant.
157}
158
159/// Match an arbitrary poison constant.
162}
163
164/// Match an arbitrary Constant and ignore it.
166
167/// Match an arbitrary ConstantInt and ignore it.
170}
171
172/// Match an arbitrary ConstantFP and ignore it.
175}
176
178 template <typename ITy> bool match(ITy *V) {
179 auto *C = dyn_cast<Constant>(V);
180 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
181 }
182};
183
184/// Match a constant expression or a constant that contains a constant
185/// expression.
187
188/// Match an arbitrary basic block value and ignore it.
191}
192
193/// Inverting matcher
194template <typename Ty> struct match_unless {
195 Ty M;
196
197 match_unless(const Ty &Matcher) : M(Matcher) {}
198
199 template <typename ITy> bool match(ITy *V) { return !M.match(V); }
200};
201
202/// Match if the inner matcher does *NOT* match.
203template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
204 return match_unless<Ty>(M);
205}
206
207/// Matching combinators
208template <typename LTy, typename RTy> struct match_combine_or {
209 LTy L;
210 RTy R;
211
212 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
213
214 template <typename ITy> bool match(ITy *V) {
215 if (L.match(V))
216 return true;
217 if (R.match(V))
218 return true;
219 return false;
220 }
221};
222
223template <typename LTy, typename RTy> struct match_combine_and {
224 LTy L;
225 RTy R;
226
227 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
228
229 template <typename ITy> bool match(ITy *V) {
230 if (L.match(V))
231 if (R.match(V))
232 return true;
233 return false;
234 }
235};
236
237/// Combine two pattern matchers matching L || R
238template <typename LTy, typename RTy>
239inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
240 return match_combine_or<LTy, RTy>(L, R);
241}
242
243/// Combine two pattern matchers matching L && R
244template <typename LTy, typename RTy>
245inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
246 return match_combine_and<LTy, RTy>(L, R);
247}
248
250 const APInt *&Res;
252
255
256 template <typename ITy> bool match(ITy *V) {
257 if (auto *CI = dyn_cast<ConstantInt>(V)) {
258 Res = &CI->getValue();
259 return true;
260 }
261 if (V->getType()->isVectorTy())
262 if (const auto *C = dyn_cast<Constant>(V))
263 if (auto *CI =
264 dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison))) {
265 Res = &CI->getValue();
266 return true;
267 }
268 return false;
269 }
270};
271// Either constexpr if or renaming ConstantFP::getValueAPF to
272// ConstantFP::getValue is needed to do it via single template
273// function for both apint/apfloat.
275 const APFloat *&Res;
277
280
281 template <typename ITy> bool match(ITy *V) {
282 if (auto *CI = dyn_cast<ConstantFP>(V)) {
283 Res = &CI->getValueAPF();
284 return true;
285 }
286 if (V->getType()->isVectorTy())
287 if (const auto *C = dyn_cast<Constant>(V))
288 if (auto *CI =
289 dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowPoison))) {
290 Res = &CI->getValueAPF();
291 return true;
292 }
293 return false;
294 }
295};
296
297/// Match a ConstantInt or splatted ConstantVector, binding the
298/// specified pointer to the contained APInt.
299inline apint_match m_APInt(const APInt *&Res) {
300 // Forbid poison by default to maintain previous behavior.
301 return apint_match(Res, /* AllowPoison */ false);
302}
303
304/// Match APInt while allowing poison in splat vector constants.
306 return apint_match(Res, /* AllowPoison */ true);
307}
308
309/// Match APInt while forbidding poison in splat vector constants.
311 return apint_match(Res, /* AllowPoison */ false);
312}
313
314/// Match a ConstantFP or splatted ConstantVector, binding the
315/// specified pointer to the contained APFloat.
316inline apfloat_match m_APFloat(const APFloat *&Res) {
317 // Forbid undefs by default to maintain previous behavior.
318 return apfloat_match(Res, /* AllowPoison */ false);
319}
320
321/// Match APFloat while allowing poison in splat vector constants.
323 return apfloat_match(Res, /* AllowPoison */ true);
324}
325
326/// Match APFloat while forbidding poison in splat vector constants.
328 return apfloat_match(Res, /* AllowPoison */ false);
329}
330
331template <int64_t Val> struct constantint_match {
332 template <typename ITy> bool match(ITy *V) {
333 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
334 const APInt &CIV = CI->getValue();
335 if (Val >= 0)
336 return CIV == static_cast<uint64_t>(Val);
337 // If Val is negative, and CI is shorter than it, truncate to the right
338 // number of bits. If it is larger, then we have to sign extend. Just
339 // compare their negated values.
340 return -CIV == -Val;
341 }
342 return false;
343 }
344};
345
346/// Match a ConstantInt with a specific value.
347template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
348 return constantint_match<Val>();
349}
350
351/// This helper class is used to match constant scalars, vector splats,
352/// and fixed width vectors that satisfy a specified predicate.
353/// For fixed width vector constants, poison elements are ignored if AllowPoison
354/// is true.
355template <typename Predicate, typename ConstantVal, bool AllowPoison>
356struct cstval_pred_ty : public Predicate {
357 const Constant **Res = nullptr;
358 template <typename ITy> bool match_impl(ITy *V) {
359 if (const auto *CV = dyn_cast<ConstantVal>(V))
360 return this->isValue(CV->getValue());
361 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
362 if (const auto *C = dyn_cast<Constant>(V)) {
363 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
364 return this->isValue(CV->getValue());
365
366 // Number of elements of a scalable vector unknown at compile time
367 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
368 if (!FVTy)
369 return false;
370
371 // Non-splat vector constant: check each element for a match.
372 unsigned NumElts = FVTy->getNumElements();
373 assert(NumElts != 0 && "Constant vector with no elements?");
374 bool HasNonPoisonElements = false;
375 for (unsigned i = 0; i != NumElts; ++i) {
376 Constant *Elt = C->getAggregateElement(i);
377 if (!Elt)
378 return false;
379 if (AllowPoison && isa<PoisonValue>(Elt))
380 continue;
381 auto *CV = dyn_cast<ConstantVal>(Elt);
382 if (!CV || !this->isValue(CV->getValue()))
383 return false;
384 HasNonPoisonElements = true;
385 }
386 return HasNonPoisonElements;
387 }
388 }
389 return false;
390 }
391
392 template <typename ITy> bool match(ITy *V) {
393 if (this->match_impl(V)) {
394 if (Res)
395 *Res = cast<Constant>(V);
396 return true;
397 }
398 return false;
399 }
400};
401
402/// specialization of cstval_pred_ty for ConstantInt
403template <typename Predicate, bool AllowPoison = true>
405
406/// specialization of cstval_pred_ty for ConstantFP
407template <typename Predicate>
409 /*AllowPoison=*/true>;
410
411/// This helper class is used to match scalar and vector constants that
412/// satisfy a specified predicate, and bind them to an APInt.
413template <typename Predicate> struct api_pred_ty : public Predicate {
414 const APInt *&Res;
415
416 api_pred_ty(const APInt *&R) : Res(R) {}
417
418 template <typename ITy> bool match(ITy *V) {
419 if (const auto *CI = dyn_cast<ConstantInt>(V))
420 if (this->isValue(CI->getValue())) {
421 Res = &CI->getValue();
422 return true;
423 }
424 if (V->getType()->isVectorTy())
425 if (const auto *C = dyn_cast<Constant>(V))
426 if (auto *CI = dyn_cast_or_null<ConstantInt>(
427 C->getSplatValue(/*AllowPoison=*/true)))
428 if (this->isValue(CI->getValue())) {
429 Res = &CI->getValue();
430 return true;
431 }
432
433 return false;
434 }
435};
436
437/// This helper class is used to match scalar and vector constants that
438/// satisfy a specified predicate, and bind them to an APFloat.
439/// Poison is allowed in splat vector constants.
440template <typename Predicate> struct apf_pred_ty : public Predicate {
441 const APFloat *&Res;
442
443 apf_pred_ty(const APFloat *&R) : Res(R) {}
444
445 template <typename ITy> bool match(ITy *V) {
446 if (const auto *CI = dyn_cast<ConstantFP>(V))
447 if (this->isValue(CI->getValue())) {
448 Res = &CI->getValue();
449 return true;
450 }
451 if (V->getType()->isVectorTy())
452 if (const auto *C = dyn_cast<Constant>(V))
453 if (auto *CI = dyn_cast_or_null<ConstantFP>(
454 C->getSplatValue(/* AllowPoison */ true)))
455 if (this->isValue(CI->getValue())) {
456 Res = &CI->getValue();
457 return true;
458 }
459
460 return false;
461 }
462};
463
464///////////////////////////////////////////////////////////////////////////////
465//
466// Encapsulate constant value queries for use in templated predicate matchers.
467// This allows checking if constants match using compound predicates and works
468// with vector constants, possibly with relaxed constraints. For example, ignore
469// undef values.
470//
471///////////////////////////////////////////////////////////////////////////////
472
473template <typename APTy> struct custom_checkfn {
474 function_ref<bool(const APTy &)> CheckFn;
475 bool isValue(const APTy &C) { return CheckFn(C); }
476};
477
478/// Match an integer or vector where CheckFn(ele) for each element is true.
479/// For vectors, poison elements are assumed to match.
481m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
482 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
483}
484
486m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
487 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
488}
489
490/// Match a float or vector where CheckFn(ele) for each element is true.
491/// For vectors, poison elements are assumed to match.
493m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
494 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
495}
496
498m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
499 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
500}
501
503 bool isValue(const APInt &C) { return true; }
504};
505/// Match an integer or vector with any integral constant.
506/// For vectors, this includes constants with undefined elements.
509}
510
512 bool isValue(const APInt &C) { return C.isShiftedMask(); }
513};
514
517}
518
520 bool isValue(const APInt &C) { return C.isAllOnes(); }
521};
522/// Match an integer or vector with all bits set.
523/// For vectors, this includes constants with undefined elements.
526}
527
530}
531
533 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
534};
535/// Match an integer or vector with values having all bits except for the high
536/// bit set (0x7f...).
537/// For vectors, this includes constants with undefined elements.
540}
542 return V;
543}
544
546 bool isValue(const APInt &C) { return C.isNegative(); }
547};
548/// Match an integer or vector of negative values.
549/// For vectors, this includes constants with undefined elements.
552}
553inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
554
556 bool isValue(const APInt &C) { return C.isNonNegative(); }
557};
558/// Match an integer or vector of non-negative values.
559/// For vectors, this includes constants with undefined elements.
562}
563inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
564
566 bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
567};
568/// Match an integer or vector of strictly positive values.
569/// For vectors, this includes constants with undefined elements.
572}
574 return V;
575}
576
578 bool isValue(const APInt &C) { return C.isNonPositive(); }
579};
580/// Match an integer or vector of non-positive values.
581/// For vectors, this includes constants with undefined elements.
584}
585inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
586
587struct is_one {
588 bool isValue(const APInt &C) { return C.isOne(); }
589};
590/// Match an integer 1 or a vector with all elements equal to 1.
591/// For vectors, this includes constants with undefined elements.
593
595 bool isValue(const APInt &C) { return C.isZero(); }
596};
597/// Match an integer 0 or a vector with all elements equal to 0.
598/// For vectors, this includes constants with undefined elements.
601}
602
603struct is_zero {
604 template <typename ITy> bool match(ITy *V) {
605 auto *C = dyn_cast<Constant>(V);
606 // FIXME: this should be able to do something for scalable vectors
607 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
608 }
609};
610/// Match any null constant or a vector with all elements equal to 0.
611/// For vectors, this includes constants with undefined elements.
612inline is_zero m_Zero() { return is_zero(); }
613
614struct is_power2 {
615 bool isValue(const APInt &C) { return C.isPowerOf2(); }
616};
617/// Match an integer or vector power-of-2.
618/// For vectors, this includes constants with undefined elements.
620inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
621
623 bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); }
624};
625/// Match a integer or vector negated power-of-2.
626/// For vectors, this includes constants with undefined elements.
629}
631 return V;
632}
633
635 bool isValue(const APInt &C) { return !C || C.isNegatedPowerOf2(); }
636};
637/// Match a integer or vector negated power-of-2.
638/// For vectors, this includes constants with undefined elements.
641}
644 return V;
645}
646
648 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
649};
650/// Match an integer or vector of 0 or power-of-2 values.
651/// For vectors, this includes constants with undefined elements.
654}
656 return V;
657}
658
660 bool isValue(const APInt &C) { return C.isSignMask(); }
661};
662/// Match an integer or vector with only the sign bit(s) set.
663/// For vectors, this includes constants with undefined elements.
666}
667
669 bool isValue(const APInt &C) { return C.isMask(); }
670};
671/// Match an integer or vector with only the low bit(s) set.
672/// For vectors, this includes constants with undefined elements.
675}
676inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
677
679 bool isValue(const APInt &C) { return !C || C.isMask(); }
680};
681/// Match an integer or vector with only the low bit(s) set.
682/// For vectors, this includes constants with undefined elements.
685}
687 return V;
688}
689
692 const APInt *Thr;
693 bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); }
694};
695/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
696/// to Threshold. For vectors, this includes constants with undefined elements.
698m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
700 P.Pred = Predicate;
701 P.Thr = &Threshold;
702 return P;
703}
704
705struct is_nan {
706 bool isValue(const APFloat &C) { return C.isNaN(); }
707};
708/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
709/// For vectors, this includes constants with undefined elements.
711
712struct is_nonnan {
713 bool isValue(const APFloat &C) { return !C.isNaN(); }
714};
715/// Match a non-NaN FP constant.
716/// For vectors, this includes constants with undefined elements.
719}
720
721struct is_inf {
722 bool isValue(const APFloat &C) { return C.isInfinity(); }
723};
724/// Match a positive or negative infinity FP constant.
725/// For vectors, this includes constants with undefined elements.
727
728struct is_noninf {
729 bool isValue(const APFloat &C) { return !C.isInfinity(); }
730};
731/// Match a non-infinity FP constant, i.e. finite or NaN.
732/// For vectors, this includes constants with undefined elements.
735}
736
737struct is_finite {
738 bool isValue(const APFloat &C) { return C.isFinite(); }
739};
740/// Match a finite FP constant, i.e. not infinity or NaN.
741/// For vectors, this includes constants with undefined elements.
744}
745inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
746
748 bool isValue(const APFloat &C) { return C.isFiniteNonZero(); }
749};
750/// Match a finite non-zero FP constant.
751/// For vectors, this includes constants with undefined elements.
754}
756 return V;
757}
758
760 bool isValue(const APFloat &C) { return C.isZero(); }
761};
762/// Match a floating-point negative zero or positive zero.
763/// For vectors, this includes constants with undefined elements.
766}
767
769 bool isValue(const APFloat &C) { return C.isPosZero(); }
770};
771/// Match a floating-point positive zero.
772/// For vectors, this includes constants with undefined elements.
775}
776
778 bool isValue(const APFloat &C) { return C.isNegZero(); }
779};
780/// Match a floating-point negative zero.
781/// For vectors, this includes constants with undefined elements.
784}
785
787 bool isValue(const APFloat &C) { return C.isNonZero(); }
788};
789/// Match a floating-point non-zero.
790/// For vectors, this includes constants with undefined elements.
793}
794
795///////////////////////////////////////////////////////////////////////////////
796
797template <typename Class> struct bind_ty {
798 Class *&VR;
799
800 bind_ty(Class *&V) : VR(V) {}
801
802 template <typename ITy> bool match(ITy *V) {
803 if (auto *CV = dyn_cast<Class>(V)) {
804 VR = CV;
805 return true;
806 }
807 return false;
808 }
809};
810
811/// Match a value, capturing it if we match.
812inline bind_ty<Value> m_Value(Value *&V) { return V; }
813inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
814
815/// Match an instruction, capturing it if we match.
817/// Match a unary operator, capturing it if we match.
819/// Match a binary operator, capturing it if we match.
821/// Match a with overflow intrinsic, capturing it if we match.
823 return I;
824}
827 return I;
828}
829
830/// Match an UndefValue, capturing the value if we match.
832
833/// Match a Constant, capturing the value if we match.
835
836/// Match a ConstantInt, capturing the value if we match.
838
839/// Match a ConstantFP, capturing the value if we match.
841
842/// Match a ConstantExpr, capturing the value if we match.
844
845/// Match a basic block value, capturing it if we match.
848 return V;
849}
850
851/// Match an arbitrary immediate Constant and ignore it.
856}
857
858/// Match an immediate Constant, capturing the value if we match.
863}
864
865/// Match a specified Value*.
867 const Value *Val;
868
869 specificval_ty(const Value *V) : Val(V) {}
870
871 template <typename ITy> bool match(ITy *V) { return V == Val; }
872};
873
874/// Match if we have a specific specified value.
875inline specificval_ty m_Specific(const Value *V) { return V; }
876
877/// Stores a reference to the Value *, not the Value * itself,
878/// thus can be used in commutative matchers.
879template <typename Class> struct deferredval_ty {
880 Class *const &Val;
881
882 deferredval_ty(Class *const &V) : Val(V) {}
883
884 template <typename ITy> bool match(ITy *const V) { return V == Val; }
885};
886
887/// Like m_Specific(), but works if the specific value to match is determined
888/// as part of the same match() expression. For example:
889/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
890/// bind X before the pattern match starts.
891/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
892/// whichever value m_Value(X) populated.
893inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
895 return V;
896}
897
898/// Match a specified floating point value or vector of all elements of
899/// that value.
901 double Val;
902
903 specific_fpval(double V) : Val(V) {}
904
905 template <typename ITy> bool match(ITy *V) {
906 if (const auto *CFP = dyn_cast<ConstantFP>(V))
907 return CFP->isExactlyValue(Val);
908 if (V->getType()->isVectorTy())
909 if (const auto *C = dyn_cast<Constant>(V))
910 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
911 return CFP->isExactlyValue(Val);
912 return false;
913 }
914};
915
916/// Match a specific floating point value or vector with all elements
917/// equal to the value.
918inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
919
920/// Match a float 1.0 or vector with all elements equal to 1.0.
921inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
922
925
927
928 template <typename ITy> bool match(ITy *V) {
929 if (const auto *CV = dyn_cast<ConstantInt>(V))
930 if (CV->getValue().ule(UINT64_MAX)) {
931 VR = CV->getZExtValue();
932 return true;
933 }
934 return false;
935 }
936};
937
938/// Match a specified integer value or vector of all elements of that
939/// value.
940template <bool AllowPoison> struct specific_intval {
941 const APInt &Val;
942
943 specific_intval(const APInt &V) : Val(V) {}
944
945 template <typename ITy> bool match(ITy *V) {
946 const auto *CI = dyn_cast<ConstantInt>(V);
947 if (!CI && V->getType()->isVectorTy())
948 if (const auto *C = dyn_cast<Constant>(V))
949 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
950
951 return CI && APInt::isSameValue(CI->getValue(), Val);
952 }
953};
954
955template <bool AllowPoison> struct specific_intval64 {
957
959
960 template <typename ITy> bool match(ITy *V) {
961 const auto *CI = dyn_cast<ConstantInt>(V);
962 if (!CI && V->getType()->isVectorTy())
963 if (const auto *C = dyn_cast<Constant>(V))
964 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
965
966 return CI && CI->getValue() == Val;
967 }
968};
969
970/// Match a specific integer value or vector with all elements equal to
971/// the value.
973 return specific_intval<false>(V);
974}
975
977 return specific_intval64<false>(V);
978}
979
981 return specific_intval<true>(V);
982}
983
985 return specific_intval64<true>(V);
986}
987
988/// Match a ConstantInt and bind to its value. This does not match
989/// ConstantInts wider than 64-bits.
991
992/// Match a specified basic block value.
995
997
998 template <typename ITy> bool match(ITy *V) {
999 const auto *BB = dyn_cast<BasicBlock>(V);
1000 return BB && BB == Val;
1001 }
1002};
1003
1004/// Match a specific basic block value.
1006 return specific_bbval(BB);
1007}
1008
1009/// A commutative-friendly version of m_Specific().
1011 return BB;
1012}
1014m_Deferred(const BasicBlock *const &BB) {
1015 return BB;
1016}
1017
1018//===----------------------------------------------------------------------===//
1019// Matcher for any binary operator.
1020//
1021template <typename LHS_t, typename RHS_t, bool Commutable = false>
1025
1026 // The evaluation order is always stable, regardless of Commutability.
1027 // The LHS is always matched first.
1028 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1029
1030 template <typename OpTy> bool match(OpTy *V) {
1031 if (auto *I = dyn_cast<BinaryOperator>(V))
1032 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1033 (Commutable && L.match(I->getOperand(1)) &&
1034 R.match(I->getOperand(0)));
1035 return false;
1036 }
1037};
1038
1039template <typename LHS, typename RHS>
1040inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1041 return AnyBinaryOp_match<LHS, RHS>(L, R);
1042}
1043
1044//===----------------------------------------------------------------------===//
1045// Matcher for any unary operator.
1046// TODO fuse unary, binary matcher into n-ary matcher
1047//
1048template <typename OP_t> struct AnyUnaryOp_match {
1049 OP_t X;
1050
1051 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1052
1053 template <typename OpTy> bool match(OpTy *V) {
1054 if (auto *I = dyn_cast<UnaryOperator>(V))
1055 return X.match(I->getOperand(0));
1056 return false;
1057 }
1058};
1059
1060template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1061 return AnyUnaryOp_match<OP_t>(X);
1062}
1063
1064//===----------------------------------------------------------------------===//
1065// Matchers for specific binary operators.
1066//
1067
1068template <typename LHS_t, typename RHS_t, unsigned Opcode,
1069 bool Commutable = false>
1073
1074 // The evaluation order is always stable, regardless of Commutability.
1075 // The LHS is always matched first.
1076 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1077
1078 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) {
1079 if (V->getValueID() == Value::InstructionVal + Opc) {
1080 auto *I = cast<BinaryOperator>(V);
1081 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1082 (Commutable && L.match(I->getOperand(1)) &&
1083 R.match(I->getOperand(0)));
1084 }
1085 return false;
1086 }
1087
1088 template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); }
1089};
1090
1091template <typename LHS, typename RHS>
1093 const RHS &R) {
1095}
1096
1097template <typename LHS, typename RHS>
1099 const RHS &R) {
1101}
1102
1103template <typename LHS, typename RHS>
1105 const RHS &R) {
1107}
1108
1109template <typename LHS, typename RHS>
1111 const RHS &R) {
1113}
1114
1115template <typename Op_t> struct FNeg_match {
1116 Op_t X;
1117
1118 FNeg_match(const Op_t &Op) : X(Op) {}
1119 template <typename OpTy> bool match(OpTy *V) {
1120 auto *FPMO = dyn_cast<FPMathOperator>(V);
1121 if (!FPMO)
1122 return false;
1123
1124 if (FPMO->getOpcode() == Instruction::FNeg)
1125 return X.match(FPMO->getOperand(0));
1126
1127 if (FPMO->getOpcode() == Instruction::FSub) {
1128 if (FPMO->hasNoSignedZeros()) {
1129 // With 'nsz', any zero goes.
1130 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1131 return false;
1132 } else {
1133 // Without 'nsz', we need fsub -0.0, X exactly.
1134 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1135 return false;
1136 }
1137
1138 return X.match(FPMO->getOperand(1));
1139 }
1140
1141 return false;
1142 }
1143};
1144
1145/// Match 'fneg X' as 'fsub -0.0, X'.
1146template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1147 return FNeg_match<OpTy>(X);
1148}
1149
1150/// Match 'fneg X' as 'fsub +-0.0, X'.
1151template <typename RHS>
1152inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1153m_FNegNSZ(const RHS &X) {
1154 return m_FSub(m_AnyZeroFP(), X);
1155}
1156
1157template <typename LHS, typename RHS>
1159 const RHS &R) {
1161}
1162
1163template <typename LHS, typename RHS>
1165 const RHS &R) {
1167}
1168
1169template <typename LHS, typename RHS>
1171 const RHS &R) {
1173}
1174
1175template <typename LHS, typename RHS>
1177 const RHS &R) {
1179}
1180
1181template <typename LHS, typename RHS>
1183 const RHS &R) {
1185}
1186
1187template <typename LHS, typename RHS>
1189 const RHS &R) {
1191}
1192
1193template <typename LHS, typename RHS>
1195 const RHS &R) {
1197}
1198
1199template <typename LHS, typename RHS>
1201 const RHS &R) {
1203}
1204
1205template <typename LHS, typename RHS>
1207 const RHS &R) {
1209}
1210
1211template <typename LHS, typename RHS>
1213 const RHS &R) {
1215}
1216
1217template <typename LHS, typename RHS>
1219 const RHS &R) {
1221}
1222
1223template <typename LHS, typename RHS>
1225 const RHS &R) {
1227}
1228
1229template <typename LHS, typename RHS>
1231 const RHS &R) {
1233}
1234
1235template <typename LHS, typename RHS>
1237 const RHS &R) {
1239}
1240
1241template <typename LHS_t, typename RHS_t, unsigned Opcode,
1242 unsigned WrapFlags = 0, bool Commutable = false>
1246
1248 : L(LHS), R(RHS) {}
1249
1250 template <typename OpTy> bool match(OpTy *V) {
1251 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1252 if (Op->getOpcode() != Opcode)
1253 return false;
1255 !Op->hasNoUnsignedWrap())
1256 return false;
1257 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1258 !Op->hasNoSignedWrap())
1259 return false;
1260 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1261 (Commutable && L.match(Op->getOperand(1)) &&
1262 R.match(Op->getOperand(0)));
1263 }
1264 return false;
1265 }
1266};
1267
1268template <typename LHS, typename RHS>
1269inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1271m_NSWAdd(const LHS &L, const RHS &R) {
1272 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1274 R);
1275}
1276template <typename LHS, typename RHS>
1277inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1279m_NSWSub(const LHS &L, const RHS &R) {
1280 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1282 R);
1283}
1284template <typename LHS, typename RHS>
1285inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1287m_NSWMul(const LHS &L, const RHS &R) {
1288 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1290 R);
1291}
1292template <typename LHS, typename RHS>
1293inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1295m_NSWShl(const LHS &L, const RHS &R) {
1296 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1298 R);
1299}
1300
1301template <typename LHS, typename RHS>
1302inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1304m_NUWAdd(const LHS &L, const RHS &R) {
1305 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1307 L, R);
1308}
1309
1310template <typename LHS, typename RHS>
1312 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1313m_c_NUWAdd(const LHS &L, const RHS &R) {
1314 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1316 true>(L, R);
1317}
1318
1319template <typename LHS, typename RHS>
1320inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1322m_NUWSub(const LHS &L, const RHS &R) {
1323 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1325 L, R);
1326}
1327template <typename LHS, typename RHS>
1328inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1330m_NUWMul(const LHS &L, const RHS &R) {
1331 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1333 L, R);
1334}
1335template <typename LHS, typename RHS>
1336inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1338m_NUWShl(const LHS &L, const RHS &R) {
1339 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1341 L, R);
1342}
1343
1344template <typename LHS_t, typename RHS_t, bool Commutable = false>
1346 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1347 unsigned Opcode;
1348
1350 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1351
1352 template <typename OpTy> bool match(OpTy *V) {
1354 }
1355};
1356
1357/// Matches a specific opcode.
1358template <typename LHS, typename RHS>
1359inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1360 const RHS &R) {
1361 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1362}
1363
1364template <typename LHS, typename RHS, bool Commutable = false>
1368
1369 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1370
1371 template <typename OpTy> bool match(OpTy *V) {
1372 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1373 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1374 if (!PDI->isDisjoint())
1375 return false;
1376 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1377 (Commutable && L.match(PDI->getOperand(1)) &&
1378 R.match(PDI->getOperand(0)));
1379 }
1380 return false;
1381 }
1382};
1383
1384template <typename LHS, typename RHS>
1386 return DisjointOr_match<LHS, RHS>(L, R);
1387}
1388
1389template <typename LHS, typename RHS>
1391 const RHS &R) {
1393}
1394
1395/// Match either "add" or "or disjoint".
1396template <typename LHS, typename RHS>
1399m_AddLike(const LHS &L, const RHS &R) {
1400 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1401}
1402
1403/// Match either "add nsw" or "or disjoint"
1404template <typename LHS, typename RHS>
1405inline match_combine_or<
1406 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1409m_NSWAddLike(const LHS &L, const RHS &R) {
1410 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1411}
1412
1413/// Match either "add nuw" or "or disjoint"
1414template <typename LHS, typename RHS>
1415inline match_combine_or<
1416 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1419m_NUWAddLike(const LHS &L, const RHS &R) {
1420 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1421}
1422
1423//===----------------------------------------------------------------------===//
1424// Class that matches a group of binary opcodes.
1425//
1426template <typename LHS_t, typename RHS_t, typename Predicate,
1427 bool Commutable = false>
1428struct BinOpPred_match : Predicate {
1431
1432 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1433
1434 template <typename OpTy> bool match(OpTy *V) {
1435 if (auto *I = dyn_cast<Instruction>(V))
1436 return this->isOpType(I->getOpcode()) &&
1437 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1438 (Commutable && L.match(I->getOperand(1)) &&
1439 R.match(I->getOperand(0))));
1440 return false;
1441 }
1442};
1443
1445 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
1446};
1447
1449 bool isOpType(unsigned Opcode) {
1450 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1451 }
1452};
1453
1455 bool isOpType(unsigned Opcode) {
1456 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1457 }
1458};
1459
1461 bool isOpType(unsigned Opcode) {
1462 return Instruction::isBitwiseLogicOp(Opcode);
1463 }
1464};
1465
1467 bool isOpType(unsigned Opcode) {
1468 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1469 }
1470};
1471
1473 bool isOpType(unsigned Opcode) {
1474 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1475 }
1476};
1477
1478/// Matches shift operations.
1479template <typename LHS, typename RHS>
1481 const RHS &R) {
1483}
1484
1485/// Matches logical shift operations.
1486template <typename LHS, typename RHS>
1488 const RHS &R) {
1490}
1491
1492/// Matches logical shift operations.
1493template <typename LHS, typename RHS>
1495m_LogicalShift(const LHS &L, const RHS &R) {
1497}
1498
1499/// Matches bitwise logic operations.
1500template <typename LHS, typename RHS>
1502m_BitwiseLogic(const LHS &L, const RHS &R) {
1504}
1505
1506/// Matches bitwise logic operations in either order.
1507template <typename LHS, typename RHS>
1509m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1511}
1512
1513/// Matches integer division operations.
1514template <typename LHS, typename RHS>
1516 const RHS &R) {
1518}
1519
1520/// Matches integer remainder operations.
1521template <typename LHS, typename RHS>
1523 const RHS &R) {
1525}
1526
1527//===----------------------------------------------------------------------===//
1528// Class that matches exact binary ops.
1529//
1530template <typename SubPattern_t> struct Exact_match {
1531 SubPattern_t SubPattern;
1532
1533 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1534
1535 template <typename OpTy> bool match(OpTy *V) {
1536 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1537 return PEO->isExact() && SubPattern.match(V);
1538 return false;
1539 }
1540};
1541
1542template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1543 return SubPattern;
1544}
1545
1546//===----------------------------------------------------------------------===//
1547// Matchers for CmpInst classes
1548//
1549
1550template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1551 bool Commutable = false>
1553 PredicateTy &Predicate;
1556
1557 // The evaluation order is always stable, regardless of Commutability.
1558 // The LHS is always matched first.
1559 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1560 : Predicate(Pred), L(LHS), R(RHS) {}
1561
1562 template <typename OpTy> bool match(OpTy *V) {
1563 if (auto *I = dyn_cast<Class>(V)) {
1564 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1565 Predicate = I->getPredicate();
1566 return true;
1567 } else if (Commutable && L.match(I->getOperand(1)) &&
1568 R.match(I->getOperand(0))) {
1569 Predicate = I->getSwappedPredicate();
1570 return true;
1571 }
1572 }
1573 return false;
1574 }
1575};
1576
1577template <typename LHS, typename RHS>
1579m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1581}
1582
1583template <typename LHS, typename RHS>
1585m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1587}
1588
1589template <typename LHS, typename RHS>
1591m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1593}
1594
1595//===----------------------------------------------------------------------===//
1596// Matchers for instructions with a given opcode and number of operands.
1597//
1598
1599/// Matches instructions with Opcode and three operands.
1600template <typename T0, unsigned Opcode> struct OneOps_match {
1602
1603 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1604
1605 template <typename OpTy> bool match(OpTy *V) {
1606 if (V->getValueID() == Value::InstructionVal + Opcode) {
1607 auto *I = cast<Instruction>(V);
1608 return Op1.match(I->getOperand(0));
1609 }
1610 return false;
1611 }
1612};
1613
1614/// Matches instructions with Opcode and three operands.
1615template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1618
1619 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1620
1621 template <typename OpTy> bool match(OpTy *V) {
1622 if (V->getValueID() == Value::InstructionVal + Opcode) {
1623 auto *I = cast<Instruction>(V);
1624 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1625 }
1626 return false;
1627 }
1628};
1629
1630/// Matches instructions with Opcode and three operands.
1631template <typename T0, typename T1, typename T2, unsigned Opcode>
1636
1637 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1638 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1639
1640 template <typename OpTy> bool match(OpTy *V) {
1641 if (V->getValueID() == Value::InstructionVal + Opcode) {
1642 auto *I = cast<Instruction>(V);
1643 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1644 Op3.match(I->getOperand(2));
1645 }
1646 return false;
1647 }
1648};
1649
1650/// Matches instructions with Opcode and any number of operands
1651template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1652 std::tuple<OperandTypes...> Operands;
1653
1654 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1655
1656 // Operand matching works by recursively calling match_operands, matching the
1657 // operands left to right. The first version is called for each operand but
1658 // the last, for which the second version is called. The second version of
1659 // match_operands is also used to match each individual operand.
1660 template <int Idx, int Last>
1661 std::enable_if_t<Idx != Last, bool> match_operands(const Instruction *I) {
1662 return match_operands<Idx, Idx>(I) && match_operands<Idx + 1, Last>(I);
1663 }
1664
1665 template <int Idx, int Last>
1666 std::enable_if_t<Idx == Last, bool> match_operands(const Instruction *I) {
1667 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1668 }
1669
1670 template <typename OpTy> bool match(OpTy *V) {
1671 if (V->getValueID() == Value::InstructionVal + Opcode) {
1672 auto *I = cast<Instruction>(V);
1673 return I->getNumOperands() == sizeof...(OperandTypes) &&
1674 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1675 }
1676 return false;
1677 }
1678};
1679
1680/// Matches SelectInst.
1681template <typename Cond, typename LHS, typename RHS>
1683m_Select(const Cond &C, const LHS &L, const RHS &R) {
1685}
1686
1687/// This matches a select of two constants, e.g.:
1688/// m_SelectCst<-1, 0>(m_Value(V))
1689template <int64_t L, int64_t R, typename Cond>
1691 Instruction::Select>
1693 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1694}
1695
1696/// Matches FreezeInst.
1697template <typename OpTy>
1700}
1701
1702/// Matches InsertElementInst.
1703template <typename Val_t, typename Elt_t, typename Idx_t>
1705m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1707 Val, Elt, Idx);
1708}
1709
1710/// Matches ExtractElementInst.
1711template <typename Val_t, typename Idx_t>
1713m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1715}
1716
1717/// Matches shuffle.
1718template <typename T0, typename T1, typename T2> struct Shuffle_match {
1722
1723 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1724 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1725
1726 template <typename OpTy> bool match(OpTy *V) {
1727 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1728 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1729 Mask.match(I->getShuffleMask());
1730 }
1731 return false;
1732 }
1733};
1734
1735struct m_Mask {
1739 MaskRef = Mask;
1740 return true;
1741 }
1742};
1743
1746 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1747 }
1748};
1749
1753 bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
1754};
1755
1760 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
1761 if (First == Mask.end())
1762 return false;
1763 SplatIndex = *First;
1764 return all_of(Mask,
1765 [First](int Elem) { return Elem == *First || Elem == -1; });
1766 }
1767};
1768
1769template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1770 PointerOpTy PointerOp;
1771 OffsetOpTy OffsetOp;
1772
1773 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1775
1776 template <typename OpTy> bool match(OpTy *V) {
1777 auto *GEP = dyn_cast<GEPOperator>(V);
1778 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
1779 PointerOp.match(GEP->getPointerOperand()) &&
1780 OffsetOp.match(GEP->idx_begin()->get());
1781 }
1782};
1783
1784/// Matches ShuffleVectorInst independently of mask value.
1785template <typename V1_t, typename V2_t>
1787m_Shuffle(const V1_t &v1, const V2_t &v2) {
1789}
1790
1791template <typename V1_t, typename V2_t, typename Mask_t>
1793m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
1794 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
1795}
1796
1797/// Matches LoadInst.
1798template <typename OpTy>
1801}
1802
1803/// Matches StoreInst.
1804template <typename ValueOpTy, typename PointerOpTy>
1806m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1808 PointerOp);
1809}
1810
1811/// Matches GetElementPtrInst.
1812template <typename... OperandTypes>
1813inline auto m_GEP(const OperandTypes &...Ops) {
1814 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
1815}
1816
1817/// Matches GEP with i8 source element type
1818template <typename PointerOpTy, typename OffsetOpTy>
1820m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
1821 return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
1822}
1823
1824//===----------------------------------------------------------------------===//
1825// Matchers for CastInst classes
1826//
1827
1828template <typename Op_t, unsigned Opcode> struct CastOperator_match {
1829 Op_t Op;
1830
1831 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
1832
1833 template <typename OpTy> bool match(OpTy *V) {
1834 if (auto *O = dyn_cast<Operator>(V))
1835 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1836 return false;
1837 }
1838};
1839
1840template <typename Op_t, typename Class> struct CastInst_match {
1841 Op_t Op;
1842
1843 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
1844
1845 template <typename OpTy> bool match(OpTy *V) {
1846 if (auto *I = dyn_cast<Class>(V))
1847 return Op.match(I->getOperand(0));
1848 return false;
1849 }
1850};
1851
1852template <typename Op_t> struct PtrToIntSameSize_match {
1854 Op_t Op;
1855
1856 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
1857 : DL(DL), Op(OpMatch) {}
1858
1859 template <typename OpTy> bool match(OpTy *V) {
1860 if (auto *O = dyn_cast<Operator>(V))
1861 return O->getOpcode() == Instruction::PtrToInt &&
1862 DL.getTypeSizeInBits(O->getType()) ==
1863 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
1864 Op.match(O->getOperand(0));
1865 return false;
1866 }
1867};
1868
1869template <typename Op_t> struct NNegZExt_match {
1870 Op_t Op;
1871
1872 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
1873
1874 template <typename OpTy> bool match(OpTy *V) {
1875 if (auto *I = dyn_cast<ZExtInst>(V))
1876 return I->hasNonNeg() && Op.match(I->getOperand(0));
1877 return false;
1878 }
1879};
1880
1881template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
1882 Op_t Op;
1883
1884 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
1885
1886 template <typename OpTy> bool match(OpTy *V) {
1887 if (auto *I = dyn_cast<TruncInst>(V))
1888 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
1889 Op.match(I->getOperand(0));
1890 return false;
1891 }
1892};
1893
1894/// Matches BitCast.
1895template <typename OpTy>
1897m_BitCast(const OpTy &Op) {
1899}
1900
1901template <typename Op_t> struct ElementWiseBitCast_match {
1902 Op_t Op;
1903
1904 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
1905
1906 template <typename OpTy> bool match(OpTy *V) {
1907 auto *I = dyn_cast<BitCastInst>(V);
1908 if (!I)
1909 return false;
1910 Type *SrcType = I->getSrcTy();
1911 Type *DstType = I->getType();
1912 // Make sure the bitcast doesn't change between scalar and vector and
1913 // doesn't change the number of vector elements.
1914 if (SrcType->isVectorTy() != DstType->isVectorTy())
1915 return false;
1916 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
1917 SrcVecTy && SrcVecTy->getElementCount() !=
1918 cast<VectorType>(DstType)->getElementCount())
1919 return false;
1920 return Op.match(I->getOperand(0));
1921 }
1922};
1923
1924template <typename OpTy>
1927}
1928
1929/// Matches PtrToInt.
1930template <typename OpTy>
1932m_PtrToInt(const OpTy &Op) {
1934}
1935
1936template <typename OpTy>
1938 const OpTy &Op) {
1940}
1941
1942/// Matches IntToPtr.
1943template <typename OpTy>
1945m_IntToPtr(const OpTy &Op) {
1947}
1948
1949/// Matches Trunc.
1950template <typename OpTy>
1953}
1954
1955/// Matches trunc nuw.
1956template <typename OpTy>
1958m_NUWTrunc(const OpTy &Op) {
1960}
1961
1962/// Matches trunc nsw.
1963template <typename OpTy>
1965m_NSWTrunc(const OpTy &Op) {
1967}
1968
1969template <typename OpTy>
1971m_TruncOrSelf(const OpTy &Op) {
1972 return m_CombineOr(m_Trunc(Op), Op);
1973}
1974
1975/// Matches SExt.
1976template <typename OpTy>
1979}
1980
1981/// Matches ZExt.
1982template <typename OpTy>
1985}
1986
1987template <typename OpTy>
1989 return NNegZExt_match<OpTy>(Op);
1990}
1991
1992template <typename OpTy>
1994m_ZExtOrSelf(const OpTy &Op) {
1995 return m_CombineOr(m_ZExt(Op), Op);
1996}
1997
1998template <typename OpTy>
2000m_SExtOrSelf(const OpTy &Op) {
2001 return m_CombineOr(m_SExt(Op), Op);
2002}
2003
2004/// Match either "sext" or "zext nneg".
2005template <typename OpTy>
2007m_SExtLike(const OpTy &Op) {
2008 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2009}
2010
2011template <typename OpTy>
2014m_ZExtOrSExt(const OpTy &Op) {
2015 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2016}
2017
2018template <typename OpTy>
2021 OpTy>
2023 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2024}
2025
2026template <typename OpTy>
2029}
2030
2031template <typename OpTy>
2034}
2035
2036template <typename OpTy>
2039}
2040
2041template <typename OpTy>
2044}
2045
2046template <typename OpTy>
2049}
2050
2051template <typename OpTy>
2054}
2055
2056//===----------------------------------------------------------------------===//
2057// Matchers for control flow.
2058//
2059
2060struct br_match {
2062
2064
2065 template <typename OpTy> bool match(OpTy *V) {
2066 if (auto *BI = dyn_cast<BranchInst>(V))
2067 if (BI->isUnconditional()) {
2068 Succ = BI->getSuccessor(0);
2069 return true;
2070 }
2071 return false;
2072 }
2073};
2074
2075inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2076
2077template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2079 Cond_t Cond;
2080 TrueBlock_t T;
2081 FalseBlock_t F;
2082
2083 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2084 : Cond(C), T(t), F(f) {}
2085
2086 template <typename OpTy> bool match(OpTy *V) {
2087 if (auto *BI = dyn_cast<BranchInst>(V))
2088 if (BI->isConditional() && Cond.match(BI->getCondition()))
2089 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2090 return false;
2091 }
2092};
2093
2094template <typename Cond_t>
2096m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
2099}
2100
2101template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2103m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2105}
2106
2107//===----------------------------------------------------------------------===//
2108// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2109//
2110
2111template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2112 bool Commutable = false>
2114 using PredType = Pred_t;
2117
2118 // The evaluation order is always stable, regardless of Commutability.
2119 // The LHS is always matched first.
2120 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2121
2122 template <typename OpTy> bool match(OpTy *V) {
2123 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2124 Intrinsic::ID IID = II->getIntrinsicID();
2125 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2126 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2127 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2128 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2129 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2130 return (L.match(LHS) && R.match(RHS)) ||
2131 (Commutable && L.match(RHS) && R.match(LHS));
2132 }
2133 }
2134 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2135 auto *SI = dyn_cast<SelectInst>(V);
2136 if (!SI)
2137 return false;
2138 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2139 if (!Cmp)
2140 return false;
2141 // At this point we have a select conditioned on a comparison. Check that
2142 // it is the values returned by the select that are being compared.
2143 auto *TrueVal = SI->getTrueValue();
2144 auto *FalseVal = SI->getFalseValue();
2145 auto *LHS = Cmp->getOperand(0);
2146 auto *RHS = Cmp->getOperand(1);
2147 if ((TrueVal != LHS || FalseVal != RHS) &&
2148 (TrueVal != RHS || FalseVal != LHS))
2149 return false;
2150 typename CmpInst_t::Predicate Pred =
2151 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2152 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2153 if (!Pred_t::match(Pred))
2154 return false;
2155 // It does! Bind the operands.
2156 return (L.match(LHS) && R.match(RHS)) ||
2157 (Commutable && L.match(RHS) && R.match(LHS));
2158 }
2159};
2160
2161/// Helper class for identifying signed max predicates.
2163 static bool match(ICmpInst::Predicate Pred) {
2164 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2165 }
2166};
2167
2168/// Helper class for identifying signed min predicates.
2170 static bool match(ICmpInst::Predicate Pred) {
2171 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2172 }
2173};
2174
2175/// Helper class for identifying unsigned max predicates.
2177 static bool match(ICmpInst::Predicate Pred) {
2178 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2179 }
2180};
2181
2182/// Helper class for identifying unsigned min predicates.
2184 static bool match(ICmpInst::Predicate Pred) {
2185 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2186 }
2187};
2188
2189/// Helper class for identifying ordered max predicates.
2191 static bool match(FCmpInst::Predicate Pred) {
2192 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2193 }
2194};
2195
2196/// Helper class for identifying ordered min predicates.
2198 static bool match(FCmpInst::Predicate Pred) {
2199 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2200 }
2201};
2202
2203/// Helper class for identifying unordered max predicates.
2205 static bool match(FCmpInst::Predicate Pred) {
2206 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2207 }
2208};
2209
2210/// Helper class for identifying unordered min predicates.
2212 static bool match(FCmpInst::Predicate Pred) {
2213 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2214 }
2215};
2216
2217template <typename LHS, typename RHS>
2219 const RHS &R) {
2221}
2222
2223template <typename LHS, typename RHS>
2225 const RHS &R) {
2227}
2228
2229template <typename LHS, typename RHS>
2231 const RHS &R) {
2233}
2234
2235template <typename LHS, typename RHS>
2237 const RHS &R) {
2239}
2240
2241template <typename LHS, typename RHS>
2242inline match_combine_or<
2247m_MaxOrMin(const LHS &L, const RHS &R) {
2248 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2249 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2250}
2251
2252/// Match an 'ordered' floating point maximum function.
2253/// Floating point has one special value 'NaN'. Therefore, there is no total
2254/// order. However, if we can ignore the 'NaN' value (for example, because of a
2255/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2256/// semantics. In the presence of 'NaN' we have to preserve the original
2257/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2258///
2259/// max(L, R) iff L and R are not NaN
2260/// m_OrdFMax(L, R) = R iff L or R are NaN
2261template <typename LHS, typename RHS>
2263 const RHS &R) {
2265}
2266
2267/// Match an 'ordered' floating point minimum function.
2268/// Floating point has one special value 'NaN'. Therefore, there is no total
2269/// order. However, if we can ignore the 'NaN' value (for example, because of a
2270/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2271/// semantics. In the presence of 'NaN' we have to preserve the original
2272/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2273///
2274/// min(L, R) iff L and R are not NaN
2275/// m_OrdFMin(L, R) = R iff L or R are NaN
2276template <typename LHS, typename RHS>
2278 const RHS &R) {
2280}
2281
2282/// Match an 'unordered' floating point maximum function.
2283/// Floating point has one special value 'NaN'. Therefore, there is no total
2284/// order. However, if we can ignore the 'NaN' value (for example, because of a
2285/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2286/// semantics. In the presence of 'NaN' we have to preserve the original
2287/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2288///
2289/// max(L, R) iff L and R are not NaN
2290/// m_UnordFMax(L, R) = L iff L or R are NaN
2291template <typename LHS, typename RHS>
2293m_UnordFMax(const LHS &L, const RHS &R) {
2295}
2296
2297/// Match an 'unordered' floating point minimum function.
2298/// Floating point has one special value 'NaN'. Therefore, there is no total
2299/// order. However, if we can ignore the 'NaN' value (for example, because of a
2300/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2301/// semantics. In the presence of 'NaN' we have to preserve the original
2302/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2303///
2304/// min(L, R) iff L and R are not NaN
2305/// m_UnordFMin(L, R) = L iff L or R are NaN
2306template <typename LHS, typename RHS>
2308m_UnordFMin(const LHS &L, const RHS &R) {
2310}
2311
2312//===----------------------------------------------------------------------===//
2313// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2314// Note that S might be matched to other instructions than AddInst.
2315//
2316
2317template <typename LHS_t, typename RHS_t, typename Sum_t>
2321 Sum_t S;
2322
2323 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2324 : L(L), R(R), S(S) {}
2325
2326 template <typename OpTy> bool match(OpTy *V) {
2327 Value *ICmpLHS, *ICmpRHS;
2329 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2330 return false;
2331
2332 Value *AddLHS, *AddRHS;
2333 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2334
2335 // (a + b) u< a, (a + b) u< b
2336 if (Pred == ICmpInst::ICMP_ULT)
2337 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2338 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2339
2340 // a >u (a + b), b >u (a + b)
2341 if (Pred == ICmpInst::ICMP_UGT)
2342 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2343 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2344
2345 Value *Op1;
2346 auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
2347 // (a ^ -1) <u b
2348 if (Pred == ICmpInst::ICMP_ULT) {
2349 if (XorExpr.match(ICmpLHS))
2350 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2351 }
2352 // b > u (a ^ -1)
2353 if (Pred == ICmpInst::ICMP_UGT) {
2354 if (XorExpr.match(ICmpRHS))
2355 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2356 }
2357
2358 // Match special-case for increment-by-1.
2359 if (Pred == ICmpInst::ICMP_EQ) {
2360 // (a + 1) == 0
2361 // (1 + a) == 0
2362 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2363 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2364 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2365 // 0 == (a + 1)
2366 // 0 == (1 + a)
2367 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2368 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2369 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2370 }
2371
2372 return false;
2373 }
2374};
2375
2376/// Match an icmp instruction checking for unsigned overflow on addition.
2377///
2378/// S is matched to the addition whose result is being checked for overflow, and
2379/// L and R are matched to the LHS and RHS of S.
2380template <typename LHS_t, typename RHS_t, typename Sum_t>
2382m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2384}
2385
2386template <typename Opnd_t> struct Argument_match {
2387 unsigned OpI;
2388 Opnd_t Val;
2389
2390 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2391
2392 template <typename OpTy> bool match(OpTy *V) {
2393 // FIXME: Should likely be switched to use `CallBase`.
2394 if (const auto *CI = dyn_cast<CallInst>(V))
2395 return Val.match(CI->getArgOperand(OpI));
2396 return false;
2397 }
2398};
2399
2400/// Match an argument.
2401template <unsigned OpI, typename Opnd_t>
2402inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2403 return Argument_match<Opnd_t>(OpI, Op);
2404}
2405
2406/// Intrinsic matchers.
2408 unsigned ID;
2409
2411
2412 template <typename OpTy> bool match(OpTy *V) {
2413 if (const auto *CI = dyn_cast<CallInst>(V))
2414 if (const auto *F = CI->getCalledFunction())
2415 return F->getIntrinsicID() == ID;
2416 return false;
2417 }
2418};
2419
2420/// Intrinsic matches are combinations of ID matchers, and argument
2421/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2422/// them with lower arity matchers. Here's some convenient typedefs for up to
2423/// several arguments, and more can be added as needed
2424template <typename T0 = void, typename T1 = void, typename T2 = void,
2425 typename T3 = void, typename T4 = void, typename T5 = void,
2426 typename T6 = void, typename T7 = void, typename T8 = void,
2427 typename T9 = void, typename T10 = void>
2429template <typename T0> struct m_Intrinsic_Ty<T0> {
2431};
2432template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2433 using Ty =
2435};
2436template <typename T0, typename T1, typename T2>
2437struct m_Intrinsic_Ty<T0, T1, T2> {
2440};
2441template <typename T0, typename T1, typename T2, typename T3>
2442struct m_Intrinsic_Ty<T0, T1, T2, T3> {
2445};
2446
2447template <typename T0, typename T1, typename T2, typename T3, typename T4>
2448struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
2451};
2452
2453template <typename T0, typename T1, typename T2, typename T3, typename T4,
2454 typename T5>
2455struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> {
2458};
2459
2460/// Match intrinsic calls like this:
2461/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2462template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2463 return IntrinsicID_match(IntrID);
2464}
2465
2466/// Matches MaskedLoad Intrinsic.
2467template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2469m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2470 const Opnd3 &Op3) {
2471 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
2472}
2473
2474/// Matches MaskedGather Intrinsic.
2475template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2477m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2478 const Opnd3 &Op3) {
2479 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
2480}
2481
2482template <Intrinsic::ID IntrID, typename T0>
2483inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2484 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
2485}
2486
2487template <Intrinsic::ID IntrID, typename T0, typename T1>
2488inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2489 const T1 &Op1) {
2490 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
2491}
2492
2493template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2494inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2495m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2496 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2497}
2498
2499template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2500 typename T3>
2502m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2503 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2504}
2505
2506template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2507 typename T3, typename T4>
2509m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2510 const T4 &Op4) {
2511 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2512 m_Argument<4>(Op4));
2513}
2514
2515template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2516 typename T3, typename T4, typename T5>
2518m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2519 const T4 &Op4, const T5 &Op5) {
2520 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2521 m_Argument<5>(Op5));
2522}
2523
2524// Helper intrinsic matching specializations.
2525template <typename Opnd0>
2526inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2527 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
2528}
2529
2530template <typename Opnd0>
2531inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2532 return m_Intrinsic<Intrinsic::bswap>(Op0);
2533}
2534
2535template <typename Opnd0>
2536inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2537 return m_Intrinsic<Intrinsic::fabs>(Op0);
2538}
2539
2540template <typename Opnd0>
2541inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2542 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
2543}
2544
2545template <typename Opnd0, typename Opnd1>
2546inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
2547 const Opnd1 &Op1) {
2548 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2549}
2550
2551template <typename Opnd0, typename Opnd1>
2552inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
2553 const Opnd1 &Op1) {
2554 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2555}
2556
2557template <typename Opnd0, typename Opnd1, typename Opnd2>
2559m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2560 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2561}
2562
2563template <typename Opnd0, typename Opnd1, typename Opnd2>
2565m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2566 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2567}
2568
2569template <typename Opnd0>
2570inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2571 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2572}
2573
2574template <typename Opnd0, typename Opnd1>
2575inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2576 const Opnd1 &Op1) {
2577 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2578}
2579
2580template <typename Opnd0>
2581inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2582 return m_Intrinsic<Intrinsic::vector_reverse>(Op0);
2583}
2584
2585//===----------------------------------------------------------------------===//
2586// Matchers for two-operands operators with the operators in either order
2587//
2588
2589/// Matches a BinaryOperator with LHS and RHS in either order.
2590template <typename LHS, typename RHS>
2593}
2594
2595/// Matches an ICmp with a predicate over LHS and RHS in either order.
2596/// Swaps the predicate if operands are commuted.
2597template <typename LHS, typename RHS>
2599m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
2601 R);
2602}
2603
2604/// Matches a specific opcode with LHS and RHS in either order.
2605template <typename LHS, typename RHS>
2607m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2608 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2609}
2610
2611/// Matches a Add with LHS and RHS in either order.
2612template <typename LHS, typename RHS>
2614 const RHS &R) {
2616}
2617
2618/// Matches a Mul with LHS and RHS in either order.
2619template <typename LHS, typename RHS>
2621 const RHS &R) {
2623}
2624
2625/// Matches an And with LHS and RHS in either order.
2626template <typename LHS, typename RHS>
2628 const RHS &R) {
2630}
2631
2632/// Matches an Or with LHS and RHS in either order.
2633template <typename LHS, typename RHS>
2635 const RHS &R) {
2637}
2638
2639/// Matches an Xor with LHS and RHS in either order.
2640template <typename LHS, typename RHS>
2642 const RHS &R) {
2644}
2645
2646/// Matches a 'Neg' as 'sub 0, V'.
2647template <typename ValTy>
2648inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2649m_Neg(const ValTy &V) {
2650 return m_Sub(m_ZeroInt(), V);
2651}
2652
2653/// Matches a 'Neg' as 'sub nsw 0, V'.
2654template <typename ValTy>
2656 Instruction::Sub,
2658m_NSWNeg(const ValTy &V) {
2659 return m_NSWSub(m_ZeroInt(), V);
2660}
2661
2662/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2663/// NOTE: we first match the 'Not' (by matching '-1'),
2664/// and only then match the inner matcher!
2665template <typename ValTy>
2666inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2667m_Not(const ValTy &V) {
2668 return m_c_Xor(m_AllOnes(), V);
2669}
2670
2671template <typename ValTy>
2672inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2673 true>
2674m_NotForbidPoison(const ValTy &V) {
2675 return m_c_Xor(m_AllOnesForbidPoison(), V);
2676}
2677
2678/// Matches an SMin with LHS and RHS in either order.
2679template <typename LHS, typename RHS>
2681m_c_SMin(const LHS &L, const RHS &R) {
2683}
2684/// Matches an SMax with LHS and RHS in either order.
2685template <typename LHS, typename RHS>
2687m_c_SMax(const LHS &L, const RHS &R) {
2689}
2690/// Matches a UMin with LHS and RHS in either order.
2691template <typename LHS, typename RHS>
2693m_c_UMin(const LHS &L, const RHS &R) {
2695}
2696/// Matches a UMax with LHS and RHS in either order.
2697template <typename LHS, typename RHS>
2699m_c_UMax(const LHS &L, const RHS &R) {
2701}
2702
2703template <typename LHS, typename RHS>
2704inline match_combine_or<
2709m_c_MaxOrMin(const LHS &L, const RHS &R) {
2710 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
2711 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
2712}
2713
2714template <Intrinsic::ID IntrID, typename T0, typename T1>
2717m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
2718 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
2719 m_Intrinsic<IntrID>(Op1, Op0));
2720}
2721
2722/// Matches FAdd with LHS and RHS in either order.
2723template <typename LHS, typename RHS>
2725m_c_FAdd(const LHS &L, const RHS &R) {
2727}
2728
2729/// Matches FMul with LHS and RHS in either order.
2730template <typename LHS, typename RHS>
2732m_c_FMul(const LHS &L, const RHS &R) {
2734}
2735
2736template <typename Opnd_t> struct Signum_match {
2737 Opnd_t Val;
2738 Signum_match(const Opnd_t &V) : Val(V) {}
2739
2740 template <typename OpTy> bool match(OpTy *V) {
2741 unsigned TypeSize = V->getType()->getScalarSizeInBits();
2742 if (TypeSize == 0)
2743 return false;
2744
2745 unsigned ShiftWidth = TypeSize - 1;
2746 Value *OpL = nullptr, *OpR = nullptr;
2747
2748 // This is the representation of signum we match:
2749 //
2750 // signum(x) == (x >> 63) | (-x >>u 63)
2751 //
2752 // An i1 value is its own signum, so it's correct to match
2753 //
2754 // signum(x) == (x >> 0) | (-x >>u 0)
2755 //
2756 // for i1 values.
2757
2758 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
2759 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
2760 auto Signum = m_Or(LHS, RHS);
2761
2762 return Signum.match(V) && OpL == OpR && Val.match(OpL);
2763 }
2764};
2765
2766/// Matches a signum pattern.
2767///
2768/// signum(x) =
2769/// x > 0 -> 1
2770/// x == 0 -> 0
2771/// x < 0 -> -1
2772template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
2773 return Signum_match<Val_t>(V);
2774}
2775
2776template <int Ind, typename Opnd_t> struct ExtractValue_match {
2777 Opnd_t Val;
2778 ExtractValue_match(const Opnd_t &V) : Val(V) {}
2779
2780 template <typename OpTy> bool match(OpTy *V) {
2781 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
2782 // If Ind is -1, don't inspect indices
2783 if (Ind != -1 &&
2784 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
2785 return false;
2786 return Val.match(I->getAggregateOperand());
2787 }
2788 return false;
2789 }
2790};
2791
2792/// Match a single index ExtractValue instruction.
2793/// For example m_ExtractValue<1>(...)
2794template <int Ind, typename Val_t>
2797}
2798
2799/// Match an ExtractValue instruction with any index.
2800/// For example m_ExtractValue(...)
2801template <typename Val_t>
2802inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
2803 return ExtractValue_match<-1, Val_t>(V);
2804}
2805
2806/// Matcher for a single index InsertValue instruction.
2807template <int Ind, typename T0, typename T1> struct InsertValue_match {
2810
2811 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
2812
2813 template <typename OpTy> bool match(OpTy *V) {
2814 if (auto *I = dyn_cast<InsertValueInst>(V)) {
2815 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
2816 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
2817 }
2818 return false;
2819 }
2820};
2821
2822/// Matches a single index InsertValue instruction.
2823template <int Ind, typename Val_t, typename Elt_t>
2825 const Elt_t &Elt) {
2826 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
2827}
2828
2829/// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
2830/// the constant expression
2831/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
2832/// under the right conditions determined by DataLayout.
2834 template <typename ITy> bool match(ITy *V) {
2835 if (m_Intrinsic<Intrinsic::vscale>().match(V))
2836 return true;
2837
2838 Value *Ptr;
2839 if (m_PtrToInt(m_Value(Ptr)).match(V)) {
2840 if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
2841 auto *DerefTy =
2842 dyn_cast<ScalableVectorType>(GEP->getSourceElementType());
2843 if (GEP->getNumIndices() == 1 && DerefTy &&
2844 DerefTy->getElementType()->isIntegerTy(8) &&
2845 m_Zero().match(GEP->getPointerOperand()) &&
2846 m_SpecificInt(1).match(GEP->idx_begin()->get()))
2847 return true;
2848 }
2849 }
2850
2851 return false;
2852 }
2853};
2854
2856 return VScaleVal_match();
2857}
2858
2859template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
2863
2864 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
2865
2866 template <typename T> bool match(T *V) {
2867 auto *I = dyn_cast<Instruction>(V);
2868 if (!I || !I->getType()->isIntOrIntVectorTy(1))
2869 return false;
2870
2871 if (I->getOpcode() == Opcode) {
2872 auto *Op0 = I->getOperand(0);
2873 auto *Op1 = I->getOperand(1);
2874 return (L.match(Op0) && R.match(Op1)) ||
2875 (Commutable && L.match(Op1) && R.match(Op0));
2876 }
2877
2878 if (auto *Select = dyn_cast<SelectInst>(I)) {
2879 auto *Cond = Select->getCondition();
2880 auto *TVal = Select->getTrueValue();
2881 auto *FVal = Select->getFalseValue();
2882
2883 // Don't match a scalar select of bool vectors.
2884 // Transforms expect a single type for operands if this matches.
2885 if (Cond->getType() != Select->getType())
2886 return false;
2887
2888 if (Opcode == Instruction::And) {
2889 auto *C = dyn_cast<Constant>(FVal);
2890 if (C && C->isNullValue())
2891 return (L.match(Cond) && R.match(TVal)) ||
2892 (Commutable && L.match(TVal) && R.match(Cond));
2893 } else {
2894 assert(Opcode == Instruction::Or);
2895 auto *C = dyn_cast<Constant>(TVal);
2896 if (C && C->isOneValue())
2897 return (L.match(Cond) && R.match(FVal)) ||
2898 (Commutable && L.match(FVal) && R.match(Cond));
2899 }
2900 }
2901
2902 return false;
2903 }
2904};
2905
2906/// Matches L && R either in the form of L & R or L ? R : false.
2907/// Note that the latter form is poison-blocking.
2908template <typename LHS, typename RHS>
2910 const RHS &R) {
2912}
2913
2914/// Matches L && R where L and R are arbitrary values.
2915inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
2916
2917/// Matches L && R with LHS and RHS in either order.
2918template <typename LHS, typename RHS>
2920m_c_LogicalAnd(const LHS &L, const RHS &R) {
2922}
2923
2924/// Matches L || R either in the form of L | R or L ? true : R.
2925/// Note that the latter form is poison-blocking.
2926template <typename LHS, typename RHS>
2928 const RHS &R) {
2930}
2931
2932/// Matches L || R where L and R are arbitrary values.
2933inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
2934
2935/// Matches L || R with LHS and RHS in either order.
2936template <typename LHS, typename RHS>
2938m_c_LogicalOr(const LHS &L, const RHS &R) {
2940}
2941
2942/// Matches either L && R or L || R,
2943/// either one being in the either binary or logical form.
2944/// Note that the latter form is poison-blocking.
2945template <typename LHS, typename RHS, bool Commutable = false>
2946inline auto m_LogicalOp(const LHS &L, const RHS &R) {
2947 return m_CombineOr(
2950}
2951
2952/// Matches either L && R or L || R where L and R are arbitrary values.
2953inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
2954
2955/// Matches either L && R or L || R with LHS and RHS in either order.
2956template <typename LHS, typename RHS>
2957inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
2958 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
2959}
2960
2961} // end namespace PatternMatch
2962} // end namespace llvm
2963
2964#endif // LLVM_IR_PATTERNMATCH_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu 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...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define check(cond)
Hexagon Common GEP
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define T1
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:76
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition: APInt.h:531
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
Base class for aggregate constants (with operands).
Definition: Constants.h:399
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1017
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:672
static 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.
Definition: Instruction.h:306
bool isShift() const
Definition: Instruction.h:259
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
'undef' values are things that do not have specified contents.
Definition: Constants.h:1348
LLVM Value Representation.
Definition: Value.h:74
Base class of all SIMD vector types.
Definition: DerivedTypes.h:403
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:641
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
#define UINT64_MAX
Definition: DataTypes.h:77
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
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.
Definition: PatternMatch.h:524
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
Definition: PatternMatch.h:160
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
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.
apfloat_match m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
Definition: PatternMatch.h:327
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:550
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
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.
Definition: PatternMatch.h:664
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)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
Definition: PatternMatch.h:726
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.
Definition: PatternMatch.h:619
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)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
AllowReassoc_match< T > m_AllowReassoc(const T &SubPattern)
Definition: PatternMatch.h:83
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
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.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
Definition: PatternMatch.h:652
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
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.
Definition: PatternMatch.h:972
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)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMax(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
Definition: PatternMatch.h:515
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:816
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:875
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
Definition: PatternMatch.h:186
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
Definition: PatternMatch.h:980
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
Definition: PatternMatch.h:742
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
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.
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:782
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
Definition: PatternMatch.h:918
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_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
Definition: PatternMatch.h:507
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:822
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)
match_combine_or< typename m_Intrinsic_Ty< T0, T1 >::Ty, typename m_Intrinsic_Ty< T1, T0 >::Ty > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:893
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
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)
Definition: PatternMatch.h:67
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:854
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
Definition: PatternMatch.h:570
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)
class_match< ConstantFP > m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
Definition: PatternMatch.h:173
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
Definition: PatternMatch.h:717
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedLoad Intrinsic.
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
apint_match m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
Definition: PatternMatch.h:310
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)
cst_pred_ty< is_all_ones, false > m_AllOnesForbidPoison()
Definition: PatternMatch.h:528
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.
class_match< UndefValue > m_UndefValue()
Match an arbitrary UndefValue constant.
Definition: PatternMatch.h:155
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:105
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:627
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:639
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.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
Definition: PatternMatch.h:481
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:683
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:921
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
match_combine_or< CastOperator_match< OpTy, Instruction::Trunc >, OpTy > m_TruncOrSelf(const OpTy &Op)
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.
Definition: PatternMatch.h:752
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
Definition: PatternMatch.h:95
VScaleVal_match m_VScale()
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(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.
Definition: PatternMatch.h:493
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
Definition: PatternMatch.h:538
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
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.
Definition: PatternMatch.h:773
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.
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.
Definition: PatternMatch.h:791
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.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
ThreeOps_match< Cond, constantint_match< L >, constantint_match< R >, Instruction::Select > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.: m_SelectCst<-1, 0>(m_Value(V))
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition: PatternMatch.h:189
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
Definition: PatternMatch.h:582
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
Definition: PatternMatch.h:710
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'.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMin(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.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
Definition: PatternMatch.h:733
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedGather Intrinsic.
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
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.
Definition: PatternMatch.h:698
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1722
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
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:1749
AllowReassoc_match(const SubPattern_t &SP)
Definition: PatternMatch.h:74
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)
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I)
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)
CastInst_match(const Op_t &OpMatch)
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
NoWrapTrunc_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
Definition: PatternMatch.h:60
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)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
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)
Matches patterns for vscale.
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Definition: PatternMatch.h:440
apf_pred_ty(const APFloat *&R)
Definition: PatternMatch.h:443
apfloat_match(const APFloat *&Res, bool AllowPoison)
Definition: PatternMatch.h:278
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Definition: PatternMatch.h:413
apint_match(const APInt *&Res, bool AllowPoison)
Definition: PatternMatch.h:253
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...
Definition: PatternMatch.h:356
function_ref< bool(const APTy &)> CheckFn
Definition: PatternMatch.h:474
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
Definition: PatternMatch.h:879
bool isValue(const APInt &C)
Definition: PatternMatch.h:520
bool isValue(const APInt &C)
Definition: PatternMatch.h:503
bool isValue(const APFloat &C)
Definition: PatternMatch.h:760
bool isValue(const APFloat &C)
Definition: PatternMatch.h:738
bool isValue(const APFloat &C)
Definition: PatternMatch.h:748
bool isOpType(unsigned Opcode)
bool isValue(const APFloat &C)
Definition: PatternMatch.h:722
bool isOpType(unsigned Opcode)
bool isValue(const APFloat &C)
Definition: PatternMatch.h:706
bool isValue(const APFloat &C)
Definition: PatternMatch.h:778
bool isValue(const APInt &C)
Definition: PatternMatch.h:546
bool isValue(const APFloat &C)
Definition: PatternMatch.h:787
bool isValue(const APFloat &C)
Definition: PatternMatch.h:729
bool isValue(const APFloat &C)
Definition: PatternMatch.h:713
bool isValue(const APInt &C)
Definition: PatternMatch.h:588
bool isValue(const APFloat &C)
Definition: PatternMatch.h:769
bool isValue(const APInt &C)
Definition: PatternMatch.h:615
bool isOpType(unsigned Opcode)
bool isValue(const APInt &C)
Definition: PatternMatch.h:660
bool isValue(const APInt &C)
Definition: PatternMatch.h:595
Intrinsic matches are combinations of ID matchers, and argument matchers.
bool match(ArrayRef< int > Mask)
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask)
m_SpecificMask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask)
bool match(ArrayRef< int > Mask)
match_combine_and(const LTy &Left, const RTy &Right)
Definition: PatternMatch.h:227
match_combine_or(const LTy &Left, const RTy &Right)
Definition: PatternMatch.h:212
match_unless(const Ty &Matcher)
Definition: PatternMatch.h:197
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying signed max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying signed min predicates.
static bool match(ICmpInst::Predicate Pred)
Match a specified basic block value.
Definition: PatternMatch.h:993
Match a specified floating point value or vector of all elements of that value.
Definition: PatternMatch.h:900
Match a specified integer value or vector of all elements of that value.
Definition: PatternMatch.h:940
Match a specified Value*.
Definition: PatternMatch.h:866
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)
Definition: PatternMatch.h:108