-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Expand file tree
/
Copy pathPatternMatch.h
More file actions
3318 lines (2804 loc) · 112 KB
/
Copy pathPatternMatch.h
File metadata and controls
3318 lines (2804 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides a simple and efficient mechanism for performing general
// tree-based pattern matches on the LLVM IR. The power of these routines is
// that it allows you to write concise patterns that are expressive and easy to
// understand. The other major advantage of this is that it allows you to
// trivially capture/bind elements in the pattern to variables. For example,
// you can do something like this:
//
// Value *Exp = ...
// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
// m_And(m_Value(Y), m_ConstantInt(C2))))) {
// ... Pattern is matched and variables are bound ...
// }
//
// This is primarily useful to things like the instruction combiner, but can
// also be useful for static analysis tools or code generators.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_IR_PATTERNMATCH_H
#define LLVM_IR_PATTERNMATCH_H
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/FMF.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/PatternMatchHelpers.h"
#include <cstdint>
#include <utility>
namespace llvm {
namespace PatternMatch {
using namespace llvm::PatternMatchHelpers;
template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
return P.match(V);
}
/// A match functor that can be used as a UnaryPredicate in functional
/// algorithms like all_of.
template <typename Val = const Value, typename Pattern>
auto match_fn(const Pattern &P) {
return bind_back<match<Val, Pattern>>(P);
}
template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
return P.match(Mask);
}
template <typename SubPattern_t> struct OneUse_match {
SubPattern_t SubPattern;
OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
template <typename OpTy> bool match(OpTy *V) const {
return V->hasOneUse() && SubPattern.match(V);
}
};
template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
return SubPattern;
}
template <typename SubPattern_t, int Flag> struct AllowFmf_match {
SubPattern_t SubPattern;
FastMathFlags FMF;
AllowFmf_match(const SubPattern_t &SP) : SubPattern(SP), FMF(Flag) {}
template <typename OpTy> bool match(OpTy *V) const {
auto *I = dyn_cast<FPMathOperator>(V);
return I && ((I->getFastMathFlags() & FMF) == FMF) && SubPattern.match(I);
}
};
template <typename T>
inline AllowFmf_match<T, FastMathFlags::AllowReassoc>
m_AllowReassoc(const T &SubPattern) {
return SubPattern;
}
template <typename T>
inline AllowFmf_match<T, FastMathFlags::AllowReciprocal>
m_AllowReciprocal(const T &SubPattern) {
return SubPattern;
}
template <typename T>
inline AllowFmf_match<T, FastMathFlags::AllowContract>
m_AllowContract(const T &SubPattern) {
return SubPattern;
}
template <typename T>
inline AllowFmf_match<T, FastMathFlags::ApproxFunc>
m_ApproxFunc(const T &SubPattern) {
return SubPattern;
}
template <typename T>
inline AllowFmf_match<T, FastMathFlags::NoNaNs> m_NoNaNs(const T &SubPattern) {
return SubPattern;
}
template <typename T>
inline AllowFmf_match<T, FastMathFlags::NoInfs> m_NoInfs(const T &SubPattern) {
return SubPattern;
}
template <typename T>
inline AllowFmf_match<T, FastMathFlags::NoSignedZeros>
m_NoSignedZeros(const T &SubPattern) {
return SubPattern;
}
/// Match an arbitrary value and ignore it.
inline auto m_Value() { return m_Isa<Value>(); }
/// Match an arbitrary unary operation and ignore it.
inline auto m_UnOp() { return m_Isa<UnaryOperator>(); }
/// Match an arbitrary binary operation and ignore it.
inline auto m_BinOp() { return m_Isa<BinaryOperator>(); }
/// Matches any compare instruction and ignore it.
inline auto m_Cmp() { return m_Isa<CmpInst>(); }
/// Matches any intrinsic call and ignore it.
inline auto m_AnyIntrinsic() { return m_Isa<IntrinsicInst>(); }
struct undef_match {
private:
LLVM_ABI static bool checkAggregate(const ConstantAggregate *CA);
public:
static bool check(const Value *V) {
if (isa<UndefValue>(V))
return true;
if (const auto *CA = dyn_cast<ConstantAggregate>(V))
return checkAggregate(CA);
return false;
}
template <typename ITy> bool match(ITy *V) const { return check(V); }
};
/// Match an arbitrary undef constant. This matches poison as well.
/// If this is an aggregate and contains a non-aggregate element that is
/// neither undef nor poison, the aggregate is not matched.
inline auto m_Undef() { return undef_match(); }
/// Match an arbitrary UndefValue constant.
inline auto m_UndefValue() { return m_Isa<UndefValue>(); }
/// Match an arbitrary poison constant.
inline auto m_Poison() { return m_Isa<PoisonValue>(); }
/// Match an arbitrary Constant and ignore it.
inline auto m_Constant() { return m_Isa<Constant>(); }
/// Match an arbitrary ConstantInt and ignore it.
inline auto m_ConstantInt() { return m_Isa<ConstantInt>(); }
/// Match an arbitrary ConstantFP and ignore it.
inline auto m_ConstantFP() { return m_Isa<ConstantFP>(); }
template <typename SPTy> struct ContainsMatchingVectorElement_match {
SPTy SubPattern;
ContainsMatchingVectorElement_match(const SPTy &SP) : SubPattern(SP) {}
template <typename ITy> bool match(ITy *V) const {
auto *C = dyn_cast<Constant>(V);
return C && C->containsMatchingVectorElement(
[&](Constant *E) { return SubPattern.match(E); });
}
};
/// Match a vector constant where at least one of its elements matches the
/// subpattern. Scalable vector constants are not matched. Any bindings in the
/// subpattern will be bound to the first match.
template <typename SPTy>
inline ContainsMatchingVectorElement_match<SPTy>
m_ContainsMatchingVectorElement(const SPTy &SubPattern) {
return SubPattern;
}
/// Match a constant expression or a constant that contains a constant
/// expression.
inline auto m_ConstantExpr() {
return m_CombineOr(m_Isa<ConstantExpr>(),
m_ContainsMatchingVectorElement(m_Isa<ConstantExpr>()));
}
template <typename SubPattern_t> struct Splat_match {
SubPattern_t SubPattern;
Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
template <typename OpTy> bool match(OpTy *V) const {
if (auto *C = dyn_cast<Constant>(V)) {
auto *Splat = C->getSplatValue();
return Splat ? SubPattern.match(Splat) : false;
}
// TODO: Extend to other cases (e.g. shufflevectors).
return false;
}
};
/// Match a constant splat. TODO: Extend this to non-constant splats.
template <typename T>
inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
return SubPattern;
}
/// Match an arbitrary basic block value and ignore it.
inline auto m_BasicBlock() { return m_Isa<BasicBlock>(); }
template <typename APTy> struct ap_match {
static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
using ConstantTy =
std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
const APTy *&Res;
bool AllowPoison;
ap_match(const APTy *&Res, bool AllowPoison)
: Res(Res), AllowPoison(AllowPoison) {}
template <typename ITy> bool match(ITy *V) const {
if (auto *CI = dyn_cast<ConstantTy>(V)) {
Res = &CI->getValue();
return true;
}
if (V->getType()->isVectorTy())
if (const auto *C = dyn_cast<Constant>(V))
if (auto *CI =
dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
Res = &CI->getValue();
return true;
}
return false;
}
};
/// Match a ConstantInt or splatted ConstantVector, binding the
/// specified pointer to the contained APInt.
inline ap_match<APInt> m_APInt(const APInt *&Res) {
// Forbid poison by default to maintain previous behavior.
return ap_match<APInt>(Res, /* AllowPoison */ false);
}
/// Match APInt while allowing poison in splat vector constants.
inline ap_match<APInt> m_APIntAllowPoison(const APInt *&Res) {
return ap_match<APInt>(Res, /* AllowPoison */ true);
}
/// Match APInt while forbidding poison in splat vector constants.
inline ap_match<APInt> m_APIntForbidPoison(const APInt *&Res) {
return ap_match<APInt>(Res, /* AllowPoison */ false);
}
/// Match a ConstantFP or splatted ConstantVector, binding the
/// specified pointer to the contained APFloat.
inline ap_match<APFloat> m_APFloat(const APFloat *&Res) {
// Forbid undefs by default to maintain previous behavior.
return ap_match<APFloat>(Res, /* AllowPoison */ false);
}
/// Match APFloat while allowing poison in splat vector constants.
inline ap_match<APFloat> m_APFloatAllowPoison(const APFloat *&Res) {
return ap_match<APFloat>(Res, /* AllowPoison */ true);
}
/// Match APFloat while forbidding poison in splat vector constants.
inline ap_match<APFloat> m_APFloatForbidPoison(const APFloat *&Res) {
return ap_match<APFloat>(Res, /* AllowPoison */ false);
}
template <int64_t Val> struct constantint_match {
template <typename ITy> bool match(ITy *V) const {
if (const auto *CI = dyn_cast<ConstantInt>(V)) {
const APInt &CIV = CI->getValue();
if (Val >= 0)
return CIV == static_cast<uint64_t>(Val);
// If Val is negative, and CI is shorter than it, truncate to the right
// number of bits. If it is larger, then we have to sign extend. Just
// compare their negated values.
return -CIV == -Val;
}
return false;
}
};
/// Match a ConstantInt with a specific value.
template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
return constantint_match<Val>();
}
/// This helper class is used to match constant scalars, vector splats,
/// and fixed width vectors that satisfy a specified predicate.
/// For fixed width vector constants, poison elements are ignored if AllowPoison
/// is true.
template <typename Predicate, typename ConstantVal, bool AllowPoison>
struct cstval_pred_ty : public Predicate {
private:
bool matchVector(const Value *V) const {
if (const auto *C = dyn_cast<Constant>(V)) {
if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
return this->isValue(CV->getValue());
// Number of elements of a scalable vector unknown at compile time
auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
if (!FVTy)
return false;
// Non-splat vector constant: check each element for a match.
unsigned NumElts = FVTy->getNumElements();
assert(NumElts != 0 && "Constant vector with no elements?");
bool HasNonPoisonElements = false;
for (unsigned i = 0; i != NumElts; ++i) {
Constant *Elt = C->getAggregateElement(i);
if (!Elt)
return false;
if (AllowPoison && isa<PoisonValue>(Elt))
continue;
auto *CV = dyn_cast<ConstantVal>(Elt);
if (!CV || !this->isValue(CV->getValue()))
return false;
HasNonPoisonElements = true;
}
return HasNonPoisonElements;
}
return false;
}
public:
const Constant **Res = nullptr;
template <typename ITy> bool match_impl(ITy *V) const {
if (const auto *CV = dyn_cast<ConstantVal>(V))
return this->isValue(CV->getValue());
if (isa<VectorType>(V->getType()))
return matchVector(V);
return false;
}
template <typename ITy> bool match(ITy *V) const {
if (this->match_impl(V)) {
if (Res)
*Res = cast<Constant>(V);
return true;
}
return false;
}
};
/// specialization of cstval_pred_ty for ConstantInt
template <typename Predicate, bool AllowPoison = true>
using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt, AllowPoison>;
/// specialization of cstval_pred_ty for ConstantFP
template <typename Predicate>
using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP,
/*AllowPoison=*/true>;
/// This helper class is used to match scalar and vector constants that
/// satisfy a specified predicate, and bind them to an APInt.
template <typename Predicate> struct api_pred_ty : public Predicate {
const APInt *&Res;
api_pred_ty(const APInt *&R) : Res(R) {}
template <typename ITy> bool match(ITy *V) const {
if (const auto *CI = dyn_cast<ConstantInt>(V))
if (this->isValue(CI->getValue())) {
Res = &CI->getValue();
return true;
}
if (V->getType()->isVectorTy())
if (const auto *C = dyn_cast<Constant>(V))
if (auto *CI = dyn_cast_or_null<ConstantInt>(
C->getSplatValue(/*AllowPoison=*/true)))
if (this->isValue(CI->getValue())) {
Res = &CI->getValue();
return true;
}
return false;
}
};
/// This helper class is used to match scalar and vector constants that
/// satisfy a specified predicate, and bind them to an APFloat.
/// Poison is allowed in splat vector constants.
template <typename Predicate> struct apf_pred_ty : public Predicate {
const APFloat *&Res;
apf_pred_ty(const APFloat *&R) : Res(R) {}
template <typename ITy> bool match(ITy *V) const {
if (const auto *CI = dyn_cast<ConstantFP>(V))
if (this->isValue(CI->getValue())) {
Res = &CI->getValue();
return true;
}
if (V->getType()->isVectorTy())
if (const auto *C = dyn_cast<Constant>(V))
if (auto *CI = dyn_cast_or_null<ConstantFP>(
C->getSplatValue(/* AllowPoison */ true)))
if (this->isValue(CI->getValue())) {
Res = &CI->getValue();
return true;
}
return false;
}
};
///////////////////////////////////////////////////////////////////////////////
//
// Encapsulate constant value queries for use in templated predicate matchers.
// This allows checking if constants match using compound predicates and works
// with vector constants, possibly with relaxed constraints. For example, ignore
// undef values.
//
///////////////////////////////////////////////////////////////////////////////
template <typename APTy> struct custom_checkfn {
function_ref<bool(const APTy &)> CheckFn;
bool isValue(const APTy &C) const { return CheckFn(C); }
};
/// Match an integer or vector where CheckFn(ele) for each element is true.
/// For vectors, poison elements are assumed to match.
inline cst_pred_ty<custom_checkfn<APInt>>
m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
}
inline cst_pred_ty<custom_checkfn<APInt>>
m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
}
/// Match a float or vector where CheckFn(ele) for each element is true.
/// For vectors, poison elements are assumed to match.
inline cstfp_pred_ty<custom_checkfn<APFloat>>
m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
}
inline cstfp_pred_ty<custom_checkfn<APFloat>>
m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
}
struct is_any_apint {
bool isValue(const APInt &C) const { return true; }
};
/// Match an integer or vector with any integral constant.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
return cst_pred_ty<is_any_apint>();
}
struct is_shifted_mask {
bool isValue(const APInt &C) const { return C.isShiftedMask(); }
};
inline cst_pred_ty<is_shifted_mask> m_ShiftedMask() {
return cst_pred_ty<is_shifted_mask>();
}
struct is_all_ones {
bool isValue(const APInt &C) const { return C.isAllOnes(); }
};
/// Match an integer or vector with all bits set.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_all_ones> m_AllOnes() {
return cst_pred_ty<is_all_ones>();
}
inline cst_pred_ty<is_all_ones, false> m_AllOnesForbidPoison() {
return cst_pred_ty<is_all_ones, false>();
}
inline auto m_AllOnesOrPoison() { return m_CombineOr(m_AllOnes(), m_Poison()); }
struct is_maxsignedvalue {
bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
};
/// Match an integer or vector with values having all bits except for the high
/// bit set (0x7f...).
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
return cst_pred_ty<is_maxsignedvalue>();
}
inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
return V;
}
struct is_negative {
bool isValue(const APInt &C) const { return C.isNegative(); }
};
/// Match an integer or vector of negative values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_negative> m_Negative() {
return cst_pred_ty<is_negative>();
}
inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
struct is_nonnegative {
bool isValue(const APInt &C) const { return C.isNonNegative(); }
};
/// Match an integer or vector of non-negative values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_nonnegative> m_NonNegative() {
return cst_pred_ty<is_nonnegative>();
}
inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
struct is_strictlypositive {
bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
};
/// Match an integer or vector of strictly positive values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
return cst_pred_ty<is_strictlypositive>();
}
inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
return V;
}
struct is_nonpositive {
bool isValue(const APInt &C) const { return C.isNonPositive(); }
};
/// Match an integer or vector of non-positive values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_nonpositive> m_NonPositive() {
return cst_pred_ty<is_nonpositive>();
}
inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
struct is_one {
bool isValue(const APInt &C) const { return C.isOne(); }
};
/// Match an integer 1 or a vector with all elements equal to 1.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
struct is_zero_int {
bool isValue(const APInt &C) const { return C.isZero(); }
};
/// Match an integer 0 or a vector with all elements equal to 0.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_zero_int> m_ZeroInt() {
return cst_pred_ty<is_zero_int>();
}
struct is_non_zero_int {
bool isValue(const APInt &C) const { return !C.isZero(); }
};
/// Match a non-zero integer or a vector with all non-zero elements.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_non_zero_int> m_NonZeroInt() {
return cst_pred_ty<is_non_zero_int>();
}
struct is_zero {
template <typename ITy> bool match(ITy *V) const {
auto *C = dyn_cast<Constant>(V);
// FIXME: this should be able to do something for scalable vectors
return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
}
};
/// Match any null constant or a vector with all elements equal to 0.
/// For vectors, this includes constants with undefined elements.
inline is_zero m_Zero() { return is_zero(); }
inline auto m_ZeroOrPoison() { return m_CombineOr(m_Zero(), m_Poison()); }
struct is_power2 {
bool isValue(const APInt &C) const { return C.isPowerOf2(); }
};
/// Match an integer or vector power-of-2.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
struct is_negated_power2 {
bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
};
/// Match a integer or vector negated power-of-2.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
return cst_pred_ty<is_negated_power2>();
}
inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
return V;
}
struct is_negated_power2_or_zero {
bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
};
/// Match a integer or vector negated power-of-2.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_negated_power2_or_zero> m_NegatedPower2OrZero() {
return cst_pred_ty<is_negated_power2_or_zero>();
}
inline api_pred_ty<is_negated_power2_or_zero>
m_NegatedPower2OrZero(const APInt *&V) {
return V;
}
struct is_power2_or_zero {
bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
};
/// Match an integer or vector of 0 or power-of-2 values.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
return cst_pred_ty<is_power2_or_zero>();
}
inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
return V;
}
struct is_sign_mask {
bool isValue(const APInt &C) const { return C.isSignMask(); }
};
/// Match an integer or vector with only the sign bit(s) set.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_sign_mask> m_SignMask() {
return cst_pred_ty<is_sign_mask>();
}
struct is_lowbit_mask {
bool isValue(const APInt &C) const { return C.isMask(); }
};
/// Match an integer or vector with only the low bit(s) set.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
return cst_pred_ty<is_lowbit_mask>();
}
inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
struct is_lowbit_mask_or_zero {
bool isValue(const APInt &C) const { return !C || C.isMask(); }
};
/// Match an integer or vector with only the low bit(s) set.
/// For vectors, this includes constants with undefined elements.
inline cst_pred_ty<is_lowbit_mask_or_zero> m_LowBitMaskOrZero() {
return cst_pred_ty<is_lowbit_mask_or_zero>();
}
inline api_pred_ty<is_lowbit_mask_or_zero> m_LowBitMaskOrZero(const APInt *&V) {
return V;
}
struct icmp_pred_with_threshold {
CmpPredicate Pred;
const APInt *Thr;
bool isValue(const APInt &C) const {
return ICmpInst::compare(C, *Thr, Pred);
}
};
/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
/// to Threshold. For vectors, this includes constants with undefined elements.
inline cst_pred_ty<icmp_pred_with_threshold>
m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
cst_pred_ty<icmp_pred_with_threshold> P;
P.Pred = Predicate;
P.Thr = &Threshold;
return P;
}
struct is_nan {
bool isValue(const APFloat &C) const { return C.isNaN(); }
};
/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_nan> m_NaN() { return cstfp_pred_ty<is_nan>(); }
struct is_nonnan {
bool isValue(const APFloat &C) const { return !C.isNaN(); }
};
/// Match a non-NaN FP constant.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_nonnan> m_NonNaN() {
return cstfp_pred_ty<is_nonnan>();
}
struct is_inf {
bool isValue(const APFloat &C) const { return C.isInfinity(); }
};
/// Match a positive or negative infinity FP constant.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_inf> m_Inf() { return cstfp_pred_ty<is_inf>(); }
template <bool IsNegative> struct is_signed_inf {
bool isValue(const APFloat &C) const {
return C.isInfinity() && IsNegative == C.isNegative();
}
};
/// Match a positive infinity FP constant.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_signed_inf<false>> m_PosInf() {
return cstfp_pred_ty<is_signed_inf<false>>();
}
/// Match a negative infinity FP constant.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_signed_inf<true>> m_NegInf() {
return cstfp_pred_ty<is_signed_inf<true>>();
}
struct is_noninf {
bool isValue(const APFloat &C) const { return !C.isInfinity(); }
};
/// Match a non-infinity FP constant, i.e. finite or NaN.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_noninf> m_NonInf() {
return cstfp_pred_ty<is_noninf>();
}
struct is_finite {
bool isValue(const APFloat &C) const { return C.isFinite(); }
};
/// Match a finite FP constant, i.e. not infinity or NaN.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_finite> m_Finite() {
return cstfp_pred_ty<is_finite>();
}
inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
struct is_finitenonzero {
bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
};
/// Match a finite non-zero FP constant.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() {
return cstfp_pred_ty<is_finitenonzero>();
}
inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) {
return V;
}
struct is_any_zero_fp {
bool isValue(const APFloat &C) const { return C.isZero(); }
};
/// Match a floating-point negative zero or positive zero.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
return cstfp_pred_ty<is_any_zero_fp>();
}
struct is_pos_zero_fp {
bool isValue(const APFloat &C) const { return C.isPosZero(); }
};
/// Match a floating-point positive zero.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
return cstfp_pred_ty<is_pos_zero_fp>();
}
struct is_neg_zero_fp {
bool isValue(const APFloat &C) const { return C.isNegZero(); }
};
/// Match a floating-point negative zero.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
return cstfp_pred_ty<is_neg_zero_fp>();
}
struct is_non_zero_fp {
bool isValue(const APFloat &C) const { return C.isNonZero(); }
};
/// Match a floating-point non-zero.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() {
return cstfp_pred_ty<is_non_zero_fp>();
}
struct is_non_zero_not_denormal_fp {
bool isValue(const APFloat &C) const {
return !C.isDenormal() && C.isNonZero();
}
};
/// Match a floating-point non-zero that is not a denormal.
/// For vectors, this includes constants with undefined elements.
inline cstfp_pred_ty<is_non_zero_not_denormal_fp> m_NonZeroNotDenormalFP() {
return cstfp_pred_ty<is_non_zero_not_denormal_fp>();
}
///////////////////////////////////////////////////////////////////////////////
/// Match a value, capturing it if we match.
inline match_bind<Value> m_Value(Value *&V) { return V; }
inline match_bind<const Value> m_Value(const Value *&V) { return V; }
/// Match against the nested pattern, and capture the value if we match.
template <typename Pattern> inline auto m_Value(Value *&V, const Pattern &P) {
return m_CombineAnd(P, match_bind<Value>(V));
}
/// Match against the nested pattern, and capture the value if we match.
template <typename Pattern>
inline auto m_Value(const Value *&V, const Pattern &P) {
return m_CombineAnd(P, match_bind<const Value>(V));
}
/// Match an instruction, capturing it if we match.
inline match_bind<Instruction> m_Instruction(Instruction *&I) { return I; }
inline match_bind<const Instruction> m_Instruction(const Instruction *&I) {
return I;
}
/// Match against the nested pattern, and capture the instruction if we match.
template <typename Pattern>
inline auto m_Instruction(Instruction *&I, const Pattern &P) {
return m_CombineAnd(P, match_bind<Instruction>(I));
}
template <typename Pattern>
inline auto m_Instruction(const Instruction *&I, const Pattern &P) {
return m_CombineAnd(P, match_bind<const Instruction>(I));
}
/// Match a unary operator, capturing it if we match.
inline match_bind<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; }
inline match_bind<const UnaryOperator> m_UnOp(const UnaryOperator *&I) {
return I;
}
/// Match a binary operator, capturing it if we match.
inline match_bind<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
inline match_bind<const BinaryOperator> m_BinOp(const BinaryOperator *&I) {
return I;
}
/// Match any intrinsic call, capturing it if we match.
inline match_bind<IntrinsicInst> m_AnyIntrinsic(IntrinsicInst *&I) { return I; }
inline match_bind<const IntrinsicInst> m_AnyIntrinsic(const IntrinsicInst *&I) {
return I;
}
/// Match a with overflow intrinsic, capturing it if we match.
inline match_bind<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) {
return I;
}
inline match_bind<const WithOverflowInst>
m_WithOverflowInst(const WithOverflowInst *&I) {
return I;
}
/// Match a PHI node, capturing it if we match.
inline match_bind<PHINode> m_Phi(PHINode *&PN) { return PN; }
/// Match an UndefValue, capturing the value if we match.
inline match_bind<UndefValue> m_UndefValue(UndefValue *&U) { return U; }
/// Match a Constant, capturing the value if we match.
inline match_bind<Constant> m_Constant(Constant *&C) { return C; }
/// Match a ConstantInt, capturing the value if we match.
inline match_bind<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
/// Match a ConstantFP, capturing the value if we match.
inline match_bind<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
/// Match a ConstantExpr, capturing the value if we match.
inline match_bind<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; }
/// Match a basic block value, capturing it if we match.
inline match_bind<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; }
inline match_bind<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
return V;
}
// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
// and use m_Unless(m_ConstantExpr).
struct immconstant_ty {
template <typename ITy> static bool isImmConstant(ITy *V) {
if (auto *CV = dyn_cast<Constant>(V)) {
if (!match(CV, m_ConstantExpr()))
return true;
if (CV->getType()->isVectorTy()) {
if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
if (!match(Splat, m_ConstantExpr())) {
return true;
}
}
}
}
return false;
}
};
struct match_immconstant_ty : immconstant_ty {
template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
};
/// Match an arbitrary immediate Constant and ignore it.
inline match_immconstant_ty m_ImmConstant() { return match_immconstant_ty(); }
struct bind_immconstant_ty : immconstant_ty {
Constant *&VR;
bind_immconstant_ty(Constant *&V) : VR(V) {}
template <typename ITy> bool match(ITy *V) const {
if (isImmConstant(V)) {
VR = cast<Constant>(V);
return true;
}
return false;
}
};
/// Match an immediate Constant, capturing the value if we match.
inline bind_immconstant_ty m_ImmConstant(Constant *&C) {
return bind_immconstant_ty(C);
}
/// Matcher for specified Value*.
struct specificval_ty {
const Value *Val;
specificval_ty(const Value *V) : Val(V) {}
template <typename ITy> bool match(ITy *V) const { return V == Val; }
};
/// Match if we have a specific specified value.
inline specificval_ty m_Specific(const Value *V) { return V; }
/// Like m_Specific(), but works if the specific value to match is determined
/// as part of the same match() expression. For example:
/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
/// bind X before the pattern match starts.
/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
/// whichever value m_Value(X) populated.
inline match_deferred<Value> m_Deferred(Value *const &V) { return V; }
inline match_deferred<const Value> m_Deferred(const Value *const &V) {
return V;
}
/// Match a specified floating point value or vector of all elements of
/// that value.
struct specific_fpval {
double Val;
specific_fpval(double V) : Val(V) {}
template <typename ITy> bool match(ITy *V) const {
if (const auto *CFP = dyn_cast<ConstantFP>(V))
return CFP->isExactlyValue(Val);
if (V->getType()->isVectorTy())
if (const auto *C = dyn_cast<Constant>(V))
if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
return CFP->isExactlyValue(Val);
return false;
}
};
/// Match a specific floating point value or vector with all elements
/// equal to the value.
inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
/// Match a float 1.0 or vector with all elements equal to 1.0.
inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
struct bind_const_intval_ty {
uint64_t &VR;
bind_const_intval_ty(uint64_t &V) : VR(V) {}
template <typename ITy> bool match(ITy *V) const {
const APInt *ConstInt;
if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
return false;
std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
if (!ZExtVal)
return false;
VR = *ZExtVal;
return true;
}