forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGenDAGPatterns.cpp
4752 lines (4102 loc) · 170 KB
/
CodeGenDAGPatterns.cpp
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
//===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
//
// 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 implements the CodeGenDAGPatterns class, which is used to read and
// represent the patterns present in a .td file for instructions.
//
//===----------------------------------------------------------------------===//
#include "CodeGenDAGPatterns.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TypeSize.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include <algorithm>
#include <cstdio>
#include <iterator>
#include <set>
using namespace llvm;
#define DEBUG_TYPE "dag-patterns"
static inline bool isIntegerOrPtr(MVT VT) {
return VT.isInteger() || VT == MVT::iPTR;
}
static inline bool isFloatingPoint(MVT VT) {
return VT.isFloatingPoint();
}
static inline bool isVector(MVT VT) {
return VT.isVector();
}
static inline bool isScalar(MVT VT) {
return !VT.isVector();
}
template <typename Predicate>
static bool berase_if(MachineValueTypeSet &S, Predicate P) {
bool Erased = false;
// It is ok to iterate over MachineValueTypeSet and remove elements from it
// at the same time.
for (MVT T : S) {
if (!P(T))
continue;
Erased = true;
S.erase(T);
}
return Erased;
}
// --- TypeSetByHwMode
// This is a parameterized type-set class. For each mode there is a list
// of types that are currently possible for a given tree node. Type
// inference will apply to each mode separately.
TypeSetByHwMode::TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList) {
for (const ValueTypeByHwMode &VVT : VTList) {
insert(VVT);
AddrSpaces.push_back(VVT.PtrAddrSpace);
}
}
bool TypeSetByHwMode::isValueTypeByHwMode(bool AllowEmpty) const {
for (const auto &I : *this) {
if (I.second.size() > 1)
return false;
if (!AllowEmpty && I.second.empty())
return false;
}
return true;
}
ValueTypeByHwMode TypeSetByHwMode::getValueTypeByHwMode() const {
assert(isValueTypeByHwMode(true) &&
"The type set has multiple types for at least one HW mode");
ValueTypeByHwMode VVT;
auto ASI = AddrSpaces.begin();
for (const auto &I : *this) {
MVT T = I.second.empty() ? MVT::Other : *I.second.begin();
VVT.getOrCreateTypeForMode(I.first, T);
if (ASI != AddrSpaces.end())
VVT.PtrAddrSpace = *ASI++;
}
return VVT;
}
bool TypeSetByHwMode::isPossible() const {
for (const auto &I : *this)
if (!I.second.empty())
return true;
return false;
}
bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
bool Changed = false;
bool ContainsDefault = false;
MVT DT = MVT::Other;
for (const auto &P : VVT) {
unsigned M = P.first;
// Make sure there exists a set for each specific mode from VVT.
Changed |= getOrCreate(M).insert(P.second).second;
// Cache VVT's default mode.
if (DefaultMode == M) {
ContainsDefault = true;
DT = P.second;
}
}
// If VVT has a default mode, add the corresponding type to all
// modes in "this" that do not exist in VVT.
if (ContainsDefault)
for (auto &I : *this)
if (!VVT.hasMode(I.first))
Changed |= I.second.insert(DT).second;
return Changed;
}
// Constrain the type set to be the intersection with VTS.
bool TypeSetByHwMode::constrain(const TypeSetByHwMode &VTS) {
bool Changed = false;
if (hasDefault()) {
for (const auto &I : VTS) {
unsigned M = I.first;
if (M == DefaultMode || hasMode(M))
continue;
Map.insert({M, Map.at(DefaultMode)});
Changed = true;
}
}
for (auto &I : *this) {
unsigned M = I.first;
SetType &S = I.second;
if (VTS.hasMode(M) || VTS.hasDefault()) {
Changed |= intersect(I.second, VTS.get(M));
} else if (!S.empty()) {
S.clear();
Changed = true;
}
}
return Changed;
}
template <typename Predicate>
bool TypeSetByHwMode::constrain(Predicate P) {
bool Changed = false;
for (auto &I : *this)
Changed |= berase_if(I.second, [&P](MVT VT) { return !P(VT); });
return Changed;
}
template <typename Predicate>
bool TypeSetByHwMode::assign_if(const TypeSetByHwMode &VTS, Predicate P) {
assert(empty());
for (const auto &I : VTS) {
SetType &S = getOrCreate(I.first);
for (auto J : I.second)
if (P(J))
S.insert(J);
}
return !empty();
}
void TypeSetByHwMode::writeToStream(raw_ostream &OS) const {
SmallVector<unsigned, 4> Modes;
Modes.reserve(Map.size());
for (const auto &I : *this)
Modes.push_back(I.first);
if (Modes.empty()) {
OS << "{}";
return;
}
array_pod_sort(Modes.begin(), Modes.end());
OS << '{';
for (unsigned M : Modes) {
OS << ' ' << getModeName(M) << ':';
writeToStream(get(M), OS);
}
OS << " }";
}
void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) {
SmallVector<MVT, 4> Types(S.begin(), S.end());
array_pod_sort(Types.begin(), Types.end());
OS << '[';
ListSeparator LS(" ");
for (const MVT &T : Types)
OS << LS << ValueTypeByHwMode::getMVTName(T);
OS << ']';
}
bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
// The isSimple call is much quicker than hasDefault - check this first.
bool IsSimple = isSimple();
bool VTSIsSimple = VTS.isSimple();
if (IsSimple && VTSIsSimple)
return *begin() == *VTS.begin();
// Speedup: We have a default if the set is simple.
bool HaveDefault = IsSimple || hasDefault();
bool VTSHaveDefault = VTSIsSimple || VTS.hasDefault();
if (HaveDefault != VTSHaveDefault)
return false;
SmallSet<unsigned, 4> Modes;
for (auto &I : *this)
Modes.insert(I.first);
for (const auto &I : VTS)
Modes.insert(I.first);
if (HaveDefault) {
// Both sets have default mode.
for (unsigned M : Modes) {
if (get(M) != VTS.get(M))
return false;
}
} else {
// Neither set has default mode.
for (unsigned M : Modes) {
// If there is no default mode, an empty set is equivalent to not having
// the corresponding mode.
bool NoModeThis = !hasMode(M) || get(M).empty();
bool NoModeVTS = !VTS.hasMode(M) || VTS.get(M).empty();
if (NoModeThis != NoModeVTS)
return false;
if (!NoModeThis)
if (get(M) != VTS.get(M))
return false;
}
}
return true;
}
namespace llvm {
raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T) {
T.writeToStream(OS);
return OS;
}
}
LLVM_DUMP_METHOD
void TypeSetByHwMode::dump() const {
dbgs() << *this << '\n';
}
bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
bool OutP = Out.count(MVT::iPTR), InP = In.count(MVT::iPTR);
auto Int = [&In](MVT T) -> bool { return !In.count(T); };
if (OutP == InP)
return berase_if(Out, Int);
// Compute the intersection of scalars separately to account for only
// one set containing iPTR.
// The intersection of iPTR with a set of integer scalar types that does not
// include iPTR will result in the most specific scalar type:
// - iPTR is more specific than any set with two elements or more
// - iPTR is less specific than any single integer scalar type.
// For example
// { iPTR } * { i32 } -> { i32 }
// { iPTR } * { i32 i64 } -> { iPTR }
// and
// { iPTR i32 } * { i32 } -> { i32 }
// { iPTR i32 } * { i32 i64 } -> { i32 i64 }
// { iPTR i32 } * { i32 i64 i128 } -> { iPTR i32 }
// Compute the difference between the two sets in such a way that the
// iPTR is in the set that is being subtracted. This is to see if there
// are any extra scalars in the set without iPTR that are not in the
// set containing iPTR. Then the iPTR could be considered a "wildcard"
// matching these scalars. If there is only one such scalar, it would
// replace the iPTR, if there are more, the iPTR would be retained.
SetType Diff;
if (InP) {
Diff = Out;
berase_if(Diff, [&In](MVT T) { return In.count(T); });
// Pre-remove these elements and rely only on InP/OutP to determine
// whether a change has been made.
berase_if(Out, [&Diff](MVT T) { return Diff.count(T); });
} else {
Diff = In;
berase_if(Diff, [&Out](MVT T) { return Out.count(T); });
Out.erase(MVT::iPTR);
}
// The actual intersection.
bool Changed = berase_if(Out, Int);
unsigned NumD = Diff.size();
if (NumD == 0)
return Changed;
if (NumD == 1) {
Out.insert(*Diff.begin());
// This is a change only if Out was the one with iPTR (which is now
// being replaced).
Changed |= OutP;
} else {
// Multiple elements from Out are now replaced with iPTR.
Out.insert(MVT::iPTR);
Changed |= !OutP;
}
return Changed;
}
bool TypeSetByHwMode::validate() const {
#ifndef NDEBUG
if (empty())
return true;
bool AllEmpty = true;
for (const auto &I : *this)
AllEmpty &= I.second.empty();
return !AllEmpty;
#endif
return true;
}
// --- TypeInfer
bool TypeInfer::MergeInTypeInfo(TypeSetByHwMode &Out,
const TypeSetByHwMode &In) {
ValidateOnExit _1(Out, *this);
In.validate();
if (In.empty() || Out == In || TP.hasError())
return false;
if (Out.empty()) {
Out = In;
return true;
}
bool Changed = Out.constrain(In);
if (Changed && Out.empty())
TP.error("Type contradiction");
return Changed;
}
bool TypeInfer::forceArbitrary(TypeSetByHwMode &Out) {
ValidateOnExit _1(Out, *this);
if (TP.hasError())
return false;
assert(!Out.empty() && "cannot pick from an empty set");
bool Changed = false;
for (auto &I : Out) {
TypeSetByHwMode::SetType &S = I.second;
if (S.size() <= 1)
continue;
MVT T = *S.begin(); // Pick the first element.
S.clear();
S.insert(T);
Changed = true;
}
return Changed;
}
bool TypeInfer::EnforceInteger(TypeSetByHwMode &Out) {
ValidateOnExit _1(Out, *this);
if (TP.hasError())
return false;
if (!Out.empty())
return Out.constrain(isIntegerOrPtr);
return Out.assign_if(getLegalTypes(), isIntegerOrPtr);
}
bool TypeInfer::EnforceFloatingPoint(TypeSetByHwMode &Out) {
ValidateOnExit _1(Out, *this);
if (TP.hasError())
return false;
if (!Out.empty())
return Out.constrain(isFloatingPoint);
return Out.assign_if(getLegalTypes(), isFloatingPoint);
}
bool TypeInfer::EnforceScalar(TypeSetByHwMode &Out) {
ValidateOnExit _1(Out, *this);
if (TP.hasError())
return false;
if (!Out.empty())
return Out.constrain(isScalar);
return Out.assign_if(getLegalTypes(), isScalar);
}
bool TypeInfer::EnforceVector(TypeSetByHwMode &Out) {
ValidateOnExit _1(Out, *this);
if (TP.hasError())
return false;
if (!Out.empty())
return Out.constrain(isVector);
return Out.assign_if(getLegalTypes(), isVector);
}
bool TypeInfer::EnforceAny(TypeSetByHwMode &Out) {
ValidateOnExit _1(Out, *this);
if (TP.hasError() || !Out.empty())
return false;
Out = getLegalTypes();
return true;
}
template <typename Iter, typename Pred, typename Less>
static Iter min_if(Iter B, Iter E, Pred P, Less L) {
if (B == E)
return E;
Iter Min = E;
for (Iter I = B; I != E; ++I) {
if (!P(*I))
continue;
if (Min == E || L(*I, *Min))
Min = I;
}
return Min;
}
template <typename Iter, typename Pred, typename Less>
static Iter max_if(Iter B, Iter E, Pred P, Less L) {
if (B == E)
return E;
Iter Max = E;
for (Iter I = B; I != E; ++I) {
if (!P(*I))
continue;
if (Max == E || L(*Max, *I))
Max = I;
}
return Max;
}
/// Make sure that for each type in Small, there exists a larger type in Big.
bool TypeInfer::EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big,
bool SmallIsVT) {
ValidateOnExit _1(Small, *this), _2(Big, *this);
if (TP.hasError())
return false;
bool Changed = false;
assert((!SmallIsVT || !Small.empty()) &&
"Small should not be empty for SDTCisVTSmallerThanOp");
if (Small.empty())
Changed |= EnforceAny(Small);
if (Big.empty())
Changed |= EnforceAny(Big);
assert(Small.hasDefault() && Big.hasDefault());
SmallVector<unsigned, 4> Modes;
union_modes(Small, Big, Modes);
// 1. Only allow integer or floating point types and make sure that
// both sides are both integer or both floating point.
// 2. Make sure that either both sides have vector types, or neither
// of them does.
for (unsigned M : Modes) {
TypeSetByHwMode::SetType &S = Small.get(M);
TypeSetByHwMode::SetType &B = Big.get(M);
assert((!SmallIsVT || !S.empty()) && "Expected non-empty type");
if (any_of(S, isIntegerOrPtr) && any_of(B, isIntegerOrPtr)) {
auto NotInt = [](MVT VT) { return !isIntegerOrPtr(VT); };
Changed |= berase_if(S, NotInt);
Changed |= berase_if(B, NotInt);
} else if (any_of(S, isFloatingPoint) && any_of(B, isFloatingPoint)) {
auto NotFP = [](MVT VT) { return !isFloatingPoint(VT); };
Changed |= berase_if(S, NotFP);
Changed |= berase_if(B, NotFP);
} else if (SmallIsVT && B.empty()) {
// B is empty and since S is a specific VT, it will never be empty. Don't
// report this as a change, just clear S and continue. This prevents an
// infinite loop.
S.clear();
} else if (S.empty() || B.empty()) {
Changed = !S.empty() || !B.empty();
S.clear();
B.clear();
} else {
TP.error("Incompatible types");
return Changed;
}
if (none_of(S, isVector) || none_of(B, isVector)) {
Changed |= berase_if(S, isVector);
Changed |= berase_if(B, isVector);
}
}
auto LT = [](MVT A, MVT B) -> bool {
// Always treat non-scalable MVTs as smaller than scalable MVTs for the
// purposes of ordering.
auto ASize = std::make_tuple(A.isScalableVector(), A.getScalarSizeInBits(),
A.getSizeInBits().getKnownMinSize());
auto BSize = std::make_tuple(B.isScalableVector(), B.getScalarSizeInBits(),
B.getSizeInBits().getKnownMinSize());
return ASize < BSize;
};
auto SameKindLE = [](MVT A, MVT B) -> bool {
// This function is used when removing elements: when a vector is compared
// to a non-vector or a scalable vector to any non-scalable MVT, it should
// return false (to avoid removal).
if (std::make_tuple(A.isVector(), A.isScalableVector()) !=
std::make_tuple(B.isVector(), B.isScalableVector()))
return false;
return std::make_tuple(A.getScalarSizeInBits(),
A.getSizeInBits().getKnownMinSize()) <=
std::make_tuple(B.getScalarSizeInBits(),
B.getSizeInBits().getKnownMinSize());
};
for (unsigned M : Modes) {
TypeSetByHwMode::SetType &S = Small.get(M);
TypeSetByHwMode::SetType &B = Big.get(M);
// MinS = min scalar in Small, remove all scalars from Big that are
// smaller-or-equal than MinS.
auto MinS = min_if(S.begin(), S.end(), isScalar, LT);
if (MinS != S.end())
Changed |= berase_if(B, std::bind(SameKindLE,
std::placeholders::_1, *MinS));
// MaxS = max scalar in Big, remove all scalars from Small that are
// larger than MaxS.
auto MaxS = max_if(B.begin(), B.end(), isScalar, LT);
if (MaxS != B.end())
Changed |= berase_if(S, std::bind(SameKindLE,
*MaxS, std::placeholders::_1));
// MinV = min vector in Small, remove all vectors from Big that are
// smaller-or-equal than MinV.
auto MinV = min_if(S.begin(), S.end(), isVector, LT);
if (MinV != S.end())
Changed |= berase_if(B, std::bind(SameKindLE,
std::placeholders::_1, *MinV));
// MaxV = max vector in Big, remove all vectors from Small that are
// larger than MaxV.
auto MaxV = max_if(B.begin(), B.end(), isVector, LT);
if (MaxV != B.end())
Changed |= berase_if(S, std::bind(SameKindLE,
*MaxV, std::placeholders::_1));
}
return Changed;
}
/// 1. Ensure that for each type T in Vec, T is a vector type, and that
/// for each type U in Elem, U is a scalar type.
/// 2. Ensure that for each (scalar) type U in Elem, there exists a (vector)
/// type T in Vec, such that U is the element type of T.
bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
TypeSetByHwMode &Elem) {
ValidateOnExit _1(Vec, *this), _2(Elem, *this);
if (TP.hasError())
return false;
bool Changed = false;
if (Vec.empty())
Changed |= EnforceVector(Vec);
if (Elem.empty())
Changed |= EnforceScalar(Elem);
SmallVector<unsigned, 4> Modes;
union_modes(Vec, Elem, Modes);
for (unsigned M : Modes) {
TypeSetByHwMode::SetType &V = Vec.get(M);
TypeSetByHwMode::SetType &E = Elem.get(M);
Changed |= berase_if(V, isScalar); // Scalar = !vector
Changed |= berase_if(E, isVector); // Vector = !scalar
assert(!V.empty() && !E.empty());
MachineValueTypeSet VT, ST;
// Collect element types from the "vector" set.
for (MVT T : V)
VT.insert(T.getVectorElementType());
// Collect scalar types from the "element" set.
for (MVT T : E)
ST.insert(T);
// Remove from V all (vector) types whose element type is not in S.
Changed |= berase_if(V, [&ST](MVT T) -> bool {
return !ST.count(T.getVectorElementType());
});
// Remove from E all (scalar) types, for which there is no corresponding
// type in V.
Changed |= berase_if(E, [&VT](MVT T) -> bool { return !VT.count(T); });
}
return Changed;
}
bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
const ValueTypeByHwMode &VVT) {
TypeSetByHwMode Tmp(VVT);
ValidateOnExit _1(Vec, *this), _2(Tmp, *this);
return EnforceVectorEltTypeIs(Vec, Tmp);
}
/// Ensure that for each type T in Sub, T is a vector type, and there
/// exists a type U in Vec such that U is a vector type with the same
/// element type as T and at least as many elements as T.
bool TypeInfer::EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
TypeSetByHwMode &Sub) {
ValidateOnExit _1(Vec, *this), _2(Sub, *this);
if (TP.hasError())
return false;
/// Return true if B is a suB-vector of P, i.e. P is a suPer-vector of B.
auto IsSubVec = [](MVT B, MVT P) -> bool {
if (!B.isVector() || !P.isVector())
return false;
// Logically a <4 x i32> is a valid subvector of <n x 4 x i32>
// but until there are obvious use-cases for this, keep the
// types separate.
if (B.isScalableVector() != P.isScalableVector())
return false;
if (B.getVectorElementType() != P.getVectorElementType())
return false;
return B.getVectorMinNumElements() < P.getVectorMinNumElements();
};
/// Return true if S has no element (vector type) that T is a sub-vector of,
/// i.e. has the same element type as T and more elements.
auto NoSubV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool {
for (auto I : S)
if (IsSubVec(T, I))
return false;
return true;
};
/// Return true if S has no element (vector type) that T is a super-vector
/// of, i.e. has the same element type as T and fewer elements.
auto NoSupV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool {
for (auto I : S)
if (IsSubVec(I, T))
return false;
return true;
};
bool Changed = false;
if (Vec.empty())
Changed |= EnforceVector(Vec);
if (Sub.empty())
Changed |= EnforceVector(Sub);
SmallVector<unsigned, 4> Modes;
union_modes(Vec, Sub, Modes);
for (unsigned M : Modes) {
TypeSetByHwMode::SetType &S = Sub.get(M);
TypeSetByHwMode::SetType &V = Vec.get(M);
Changed |= berase_if(S, isScalar);
// Erase all types from S that are not sub-vectors of a type in V.
Changed |= berase_if(S, std::bind(NoSubV, V, std::placeholders::_1));
// Erase all types from V that are not super-vectors of a type in S.
Changed |= berase_if(V, std::bind(NoSupV, S, std::placeholders::_1));
}
return Changed;
}
/// 1. Ensure that V has a scalar type iff W has a scalar type.
/// 2. Ensure that for each vector type T in V, there exists a vector
/// type U in W, such that T and U have the same number of elements.
/// 3. Ensure that for each vector type U in W, there exists a vector
/// type T in V, such that T and U have the same number of elements
/// (reverse of 2).
bool TypeInfer::EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W) {
ValidateOnExit _1(V, *this), _2(W, *this);
if (TP.hasError())
return false;
bool Changed = false;
if (V.empty())
Changed |= EnforceAny(V);
if (W.empty())
Changed |= EnforceAny(W);
// An actual vector type cannot have 0 elements, so we can treat scalars
// as zero-length vectors. This way both vectors and scalars can be
// processed identically.
auto NoLength = [](const SmallDenseSet<ElementCount> &Lengths,
MVT T) -> bool {
return !Lengths.count(T.isVector() ? T.getVectorElementCount()
: ElementCount::getNull());
};
SmallVector<unsigned, 4> Modes;
union_modes(V, W, Modes);
for (unsigned M : Modes) {
TypeSetByHwMode::SetType &VS = V.get(M);
TypeSetByHwMode::SetType &WS = W.get(M);
SmallDenseSet<ElementCount> VN, WN;
for (MVT T : VS)
VN.insert(T.isVector() ? T.getVectorElementCount()
: ElementCount::getNull());
for (MVT T : WS)
WN.insert(T.isVector() ? T.getVectorElementCount()
: ElementCount::getNull());
Changed |= berase_if(VS, std::bind(NoLength, WN, std::placeholders::_1));
Changed |= berase_if(WS, std::bind(NoLength, VN, std::placeholders::_1));
}
return Changed;
}
namespace {
struct TypeSizeComparator {
bool operator()(const TypeSize &LHS, const TypeSize &RHS) const {
return std::make_tuple(LHS.isScalable(), LHS.getKnownMinValue()) <
std::make_tuple(RHS.isScalable(), RHS.getKnownMinValue());
}
};
} // end anonymous namespace
/// 1. Ensure that for each type T in A, there exists a type U in B,
/// such that T and U have equal size in bits.
/// 2. Ensure that for each type U in B, there exists a type T in A
/// such that T and U have equal size in bits (reverse of 1).
bool TypeInfer::EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B) {
ValidateOnExit _1(A, *this), _2(B, *this);
if (TP.hasError())
return false;
bool Changed = false;
if (A.empty())
Changed |= EnforceAny(A);
if (B.empty())
Changed |= EnforceAny(B);
typedef SmallSet<TypeSize, 2, TypeSizeComparator> TypeSizeSet;
auto NoSize = [](const TypeSizeSet &Sizes, MVT T) -> bool {
return !Sizes.count(T.getSizeInBits());
};
SmallVector<unsigned, 4> Modes;
union_modes(A, B, Modes);
for (unsigned M : Modes) {
TypeSetByHwMode::SetType &AS = A.get(M);
TypeSetByHwMode::SetType &BS = B.get(M);
TypeSizeSet AN, BN;
for (MVT T : AS)
AN.insert(T.getSizeInBits());
for (MVT T : BS)
BN.insert(T.getSizeInBits());
Changed |= berase_if(AS, std::bind(NoSize, BN, std::placeholders::_1));
Changed |= berase_if(BS, std::bind(NoSize, AN, std::placeholders::_1));
}
return Changed;
}
void TypeInfer::expandOverloads(TypeSetByHwMode &VTS) {
ValidateOnExit _1(VTS, *this);
const TypeSetByHwMode &Legal = getLegalTypes();
assert(Legal.isDefaultOnly() && "Default-mode only expected");
const TypeSetByHwMode::SetType &LegalTypes = Legal.get(DefaultMode);
for (auto &I : VTS)
expandOverloads(I.second, LegalTypes);
}
void TypeInfer::expandOverloads(TypeSetByHwMode::SetType &Out,
const TypeSetByHwMode::SetType &Legal) {
std::set<MVT> Ovs;
for (MVT T : Out) {
if (!T.isOverloaded())
continue;
Ovs.insert(T);
// MachineValueTypeSet allows iteration and erasing.
Out.erase(T);
}
for (MVT Ov : Ovs) {
switch (Ov.SimpleTy) {
case MVT::iPTRAny:
Out.insert(MVT::iPTR);
return;
case MVT::iAny:
for (MVT T : MVT::integer_valuetypes())
if (Legal.count(T))
Out.insert(T);
for (MVT T : MVT::integer_fixedlen_vector_valuetypes())
if (Legal.count(T))
Out.insert(T);
for (MVT T : MVT::integer_scalable_vector_valuetypes())
if (Legal.count(T))
Out.insert(T);
return;
case MVT::fAny:
for (MVT T : MVT::fp_valuetypes())
if (Legal.count(T))
Out.insert(T);
for (MVT T : MVT::fp_fixedlen_vector_valuetypes())
if (Legal.count(T))
Out.insert(T);
for (MVT T : MVT::fp_scalable_vector_valuetypes())
if (Legal.count(T))
Out.insert(T);
return;
case MVT::vAny:
for (MVT T : MVT::vector_valuetypes())
if (Legal.count(T))
Out.insert(T);
return;
case MVT::Any:
for (MVT T : MVT::all_valuetypes())
if (Legal.count(T))
Out.insert(T);
return;
default:
break;
}
}
}
const TypeSetByHwMode &TypeInfer::getLegalTypes() {
if (!LegalTypesCached) {
TypeSetByHwMode::SetType &LegalTypes = LegalCache.getOrCreate(DefaultMode);
// Stuff all types from all modes into the default mode.
const TypeSetByHwMode <S = TP.getDAGPatterns().getLegalTypes();
for (const auto &I : LTS)
LegalTypes.insert(I.second);
LegalTypesCached = true;
}
assert(LegalCache.isDefaultOnly() && "Default-mode only expected");
return LegalCache;
}
#ifndef NDEBUG
TypeInfer::ValidateOnExit::~ValidateOnExit() {
if (Infer.Validate && !VTS.validate()) {
dbgs() << "Type set is empty for each HW mode:\n"
"possible type contradiction in the pattern below "
"(use -print-records with llvm-tblgen to see all "
"expanded records).\n";
Infer.TP.dump();
dbgs() << "Generated from record:\n";
Infer.TP.getRecord()->dump();
PrintFatalError(Infer.TP.getRecord()->getLoc(),
"Type set is empty for each HW mode in '" +
Infer.TP.getRecord()->getName() + "'");
}
}
#endif
//===----------------------------------------------------------------------===//
// ScopedName Implementation
//===----------------------------------------------------------------------===//
bool ScopedName::operator==(const ScopedName &o) const {
return Scope == o.Scope && Identifier == o.Identifier;
}
bool ScopedName::operator!=(const ScopedName &o) const {
return !(*this == o);
}
//===----------------------------------------------------------------------===//
// TreePredicateFn Implementation
//===----------------------------------------------------------------------===//
/// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) {
assert(
(!hasPredCode() || !hasImmCode()) &&
".td file corrupt: can't have a node predicate *and* an imm predicate");
}
bool TreePredicateFn::hasPredCode() const {
return isLoad() || isStore() || isAtomic() ||
!PatFragRec->getRecord()->getValueAsString("PredicateCode").empty();
}
std::string TreePredicateFn::getPredCode() const {
std::string Code;
if (!isLoad() && !isStore() && !isAtomic()) {
Record *MemoryVT = getMemoryVT();
if (MemoryVT)
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"MemoryVT requires IsLoad or IsStore");
}
if (!isLoad() && !isStore()) {
if (isUnindexed())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsUnindexed requires IsLoad or IsStore");
Record *ScalarMemoryVT = getScalarMemoryVT();
if (ScalarMemoryVT)
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"ScalarMemoryVT requires IsLoad or IsStore");
}
if (isLoad() + isStore() + isAtomic() > 1)
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsLoad, IsStore, and IsAtomic are mutually exclusive");
if (isLoad()) {
if (!isUnindexed() && !isNonExtLoad() && !isAnyExtLoad() &&
!isSignExtLoad() && !isZeroExtLoad() && getMemoryVT() == nullptr &&
getScalarMemoryVT() == nullptr && getAddressSpaces() == nullptr &&
getMinAlignment() < 1)
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsLoad cannot be used by itself");
} else {
if (isNonExtLoad())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsNonExtLoad requires IsLoad");
if (isAnyExtLoad())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAnyExtLoad requires IsLoad");
if (isSignExtLoad())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsSignExtLoad requires IsLoad");
if (isZeroExtLoad())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsZeroExtLoad requires IsLoad");
}
if (isStore()) {
if (!isUnindexed() && !isTruncStore() && !isNonTruncStore() &&
getMemoryVT() == nullptr && getScalarMemoryVT() == nullptr &&
getAddressSpaces() == nullptr && getMinAlignment() < 1)
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsStore cannot be used by itself");
} else {
if (isNonTruncStore())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsNonTruncStore requires IsStore");
if (isTruncStore())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsTruncStore requires IsStore");
}
if (isAtomic()) {
if (getMemoryVT() == nullptr && !isAtomicOrderingMonotonic() &&
getAddressSpaces() == nullptr &&
!isAtomicOrderingAcquire() && !isAtomicOrderingRelease() &&
!isAtomicOrderingAcquireRelease() &&
!isAtomicOrderingSequentiallyConsistent() &&
!isAtomicOrderingAcquireOrStronger() &&
!isAtomicOrderingReleaseOrStronger() &&
!isAtomicOrderingWeakerThanAcquire() &&
!isAtomicOrderingWeakerThanRelease())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAtomic cannot be used by itself");
} else {
if (isAtomicOrderingMonotonic())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAtomicOrderingMonotonic requires IsAtomic");
if (isAtomicOrderingAcquire())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAtomicOrderingAcquire requires IsAtomic");
if (isAtomicOrderingRelease())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAtomicOrderingRelease requires IsAtomic");
if (isAtomicOrderingAcquireRelease())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAtomicOrderingAcquireRelease requires IsAtomic");
if (isAtomicOrderingSequentiallyConsistent())
PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
"IsAtomicOrderingSequentiallyConsistent requires IsAtomic");
if (isAtomicOrderingAcquireOrStronger())