@@ -52,8 +52,9 @@ class MatcherTableEmitter {
52
52
53
53
SmallVector<unsigned , Matcher::HighestKind+1 > OpcodeCounts;
54
54
55
- std::vector<TreePattern *> NodePredicates;
56
- std::vector<TreePattern *> NodePredicatesWithOperands;
55
+ DenseMap<TreePattern *, unsigned > NodePredicateMap;
56
+ std::vector<TreePredicateFn> NodePredicates;
57
+ std::vector<TreePredicateFn> NodePredicatesWithOperands;
57
58
58
59
// We de-duplicate the predicates by code string, and use this map to track
59
60
// all the patterns with "identical" predicates.
@@ -87,8 +88,6 @@ class MatcherTableEmitter {
87
88
DenseMap<const ComplexPattern *, unsigned > ComplexPatternUsage;
88
89
// Record the usage of PatternPredicate.
89
90
std::map<StringRef, unsigned > PatternPredicateUsage;
90
- // Record the usage of Predicate.
91
- DenseMap<TreePattern *, unsigned > PredicateUsage;
92
91
93
92
// Iterate the whole MatcherTable once and do some statistics.
94
93
std::function<void (const Matcher *)> Statistic = [&](const Matcher *N) {
@@ -106,8 +105,6 @@ class MatcherTableEmitter {
106
105
++ComplexPatternUsage[&CPM->getPattern ()];
107
106
else if (auto *CPPM = dyn_cast<CheckPatternPredicateMatcher>(N))
108
107
++PatternPredicateUsage[CPPM->getPredicate ()];
109
- else if (auto *PM = dyn_cast<CheckPredicateMatcher>(N))
110
- ++PredicateUsage[PM->getPredicate ().getOrigPatFragRecord ()];
111
108
N = N->getNext ();
112
109
}
113
110
};
@@ -128,39 +125,6 @@ class MatcherTableEmitter {
128
125
[](const auto &A, const auto &B) { return A.second > B.second ; });
129
126
for (const auto &PatternPredicate : PatternPredicateList)
130
127
PatternPredicates.push_back (PatternPredicate.first );
131
-
132
- // Sort Predicates by usage.
133
- // Merge predicates with same code.
134
- for (const auto &Usage : PredicateUsage) {
135
- TreePattern *TP = Usage.first ;
136
- TreePredicateFn Pred (TP);
137
- NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode ()].push_back (TP);
138
- }
139
-
140
- std::vector<std::pair<TreePattern *, unsigned >> PredicateList;
141
- // Sum the usage.
142
- for (auto &Predicate : NodePredicatesByCodeToRun) {
143
- TinyPtrVector<TreePattern *> &TPs = Predicate.second ;
144
- sort (TPs, [](const auto *A, const auto *B) {
145
- return A->getRecord ()->getName () < B->getRecord ()->getName ();
146
- });
147
- unsigned Uses = 0 ;
148
- for (TreePattern *TP : TPs)
149
- Uses += PredicateUsage.at (TP);
150
-
151
- // We only add the first predicate here since they are with the same code.
152
- PredicateList.push_back ({TPs[0 ], Uses});
153
- }
154
-
155
- sort (PredicateList,
156
- [](const auto &A, const auto &B) { return A.second > B.second ; });
157
- for (const auto &Predicate : PredicateList) {
158
- TreePattern *TP = Predicate.first ;
159
- if (TreePredicateFn (TP).usesOperands ())
160
- NodePredicatesWithOperands.push_back (TP);
161
- else
162
- NodePredicates.push_back (TP);
163
- }
164
128
}
165
129
166
130
unsigned EmitMatcherList (const Matcher *N, const unsigned Indent,
@@ -175,7 +139,7 @@ class MatcherTableEmitter {
175
139
void EmitPatternMatchTable (raw_ostream &OS);
176
140
177
141
private:
178
- void EmitNodePredicatesFunction (const std::vector<TreePattern * > &Preds,
142
+ void EmitNodePredicatesFunction (const std::vector<TreePredicateFn > &Preds,
179
143
StringRef Decl, raw_ostream &OS);
180
144
181
145
unsigned SizeMatcher (Matcher *N, raw_ostream &OS);
@@ -184,13 +148,33 @@ class MatcherTableEmitter {
184
148
raw_ostream &OS);
185
149
186
150
unsigned getNodePredicate (TreePredicateFn Pred) {
187
- // We use the first predicate.
188
- TreePattern *PredPat =
189
- NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode ()][0 ];
190
- return Pred.usesOperands ()
191
- ? llvm::find (NodePredicatesWithOperands, PredPat) -
192
- NodePredicatesWithOperands.begin ()
193
- : llvm::find (NodePredicates, PredPat) - NodePredicates.begin ();
151
+ TreePattern *TP = Pred.getOrigPatFragRecord ();
152
+ unsigned &Entry = NodePredicateMap[TP];
153
+ if (Entry == 0 ) {
154
+ TinyPtrVector<TreePattern *> &SameCodePreds =
155
+ NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode ()];
156
+ if (SameCodePreds.empty ()) {
157
+ // We've never seen a predicate with the same code: allocate an entry.
158
+ if (Pred.usesOperands ()) {
159
+ NodePredicatesWithOperands.push_back (Pred);
160
+ Entry = NodePredicatesWithOperands.size ();
161
+ } else {
162
+ NodePredicates.push_back (Pred);
163
+ Entry = NodePredicates.size ();
164
+ }
165
+ } else {
166
+ // We did see an identical predicate: re-use it.
167
+ Entry = NodePredicateMap[SameCodePreds.front ()];
168
+ assert (Entry != 0 );
169
+ assert (TreePredicateFn (SameCodePreds.front ()).usesOperands () ==
170
+ Pred.usesOperands () &&
171
+ " PatFrags with some code must have same usesOperands setting" );
172
+ }
173
+ // In both cases, we've never seen this particular predicate before, so
174
+ // mark it in the list of predicates sharing the same code.
175
+ SameCodePreds.push_back (TP);
176
+ }
177
+ return Entry-1 ;
194
178
}
195
179
196
180
unsigned getPatternPredicate (StringRef PredName) {
@@ -545,7 +529,6 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
545
529
case Matcher::CheckPredicate: {
546
530
TreePredicateFn Pred = cast<CheckPredicateMatcher>(N)->getPredicate ();
547
531
unsigned OperandBytes = 0 ;
548
- unsigned PredNo = getNodePredicate (Pred);
549
532
550
533
if (Pred.usesOperands ()) {
551
534
unsigned NumOps = cast<CheckPredicateMatcher>(N)->getNumOperands ();
@@ -554,15 +537,10 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
554
537
OS << cast<CheckPredicateMatcher>(N)->getOperandNo (i) << " , " ;
555
538
OperandBytes = 1 + NumOps;
556
539
} else {
557
- if (PredNo < 8 ) {
558
- OperandBytes = -1 ;
559
- OS << " OPC_CheckPredicate" << PredNo << " , " ;
560
- } else
561
- OS << " OPC_CheckPredicate, " ;
540
+ OS << " OPC_CheckPredicate, " ;
562
541
}
563
542
564
- if (PredNo >= 8 || Pred.usesOperands ())
565
- OS << PredNo << ' ,' ;
543
+ OS << getNodePredicate (Pred) << ' ,' ;
566
544
if (!OmitComments)
567
545
OS << " // " << Pred.getFnName ();
568
546
OS << ' \n ' ;
@@ -1051,7 +1029,8 @@ EmitMatcherList(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
1051
1029
}
1052
1030
1053
1031
void MatcherTableEmitter::EmitNodePredicatesFunction (
1054
- const std::vector<TreePattern *> &Preds, StringRef Decl, raw_ostream &OS) {
1032
+ const std::vector<TreePredicateFn> &Preds, StringRef Decl,
1033
+ raw_ostream &OS) {
1055
1034
if (Preds.empty ())
1056
1035
return ;
1057
1036
@@ -1061,7 +1040,7 @@ void MatcherTableEmitter::EmitNodePredicatesFunction(
1061
1040
OS << " default: llvm_unreachable(\" Invalid predicate in table?\" );\n " ;
1062
1041
for (unsigned i = 0 , e = Preds.size (); i != e; ++i) {
1063
1042
// Emit the predicate code corresponding to this pattern.
1064
- TreePredicateFn PredFn ( Preds[i]) ;
1043
+ const TreePredicateFn PredFn = Preds[i];
1065
1044
assert (!PredFn.isAlwaysTrue () && " No code in this predicate" );
1066
1045
std::string PredFnCodeStr = PredFn.getCodeToRunOnSDNode ();
1067
1046
0 commit comments