@@ -63,7 +63,6 @@ class MatcherTableEmitter {
63
63
StringMap<unsigned > PatternPredicateMap;
64
64
std::vector<std::string> PatternPredicates;
65
65
66
- DenseMap<const ComplexPattern*, unsigned > ComplexPatternMap;
67
66
std::vector<const ComplexPattern*> ComplexPatterns;
68
67
69
68
@@ -84,8 +83,38 @@ class MatcherTableEmitter {
84
83
}
85
84
86
85
public:
87
- MatcherTableEmitter (const CodeGenDAGPatterns &cgp)
88
- : CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1 , 0 ) {}
86
+ MatcherTableEmitter (const Matcher *TheMatcher, const CodeGenDAGPatterns &cgp)
87
+ : CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1 , 0 ) {
88
+ // Record the usage of ComplexPattern.
89
+ DenseMap<const ComplexPattern *, unsigned > ComplexPatternUsage;
90
+
91
+ // Iterate the whole MatcherTable once and do some statistics.
92
+ std::function<void (const Matcher *)> Statistic = [&](const Matcher *N) {
93
+ while (N) {
94
+ if (auto *SM = dyn_cast<ScopeMatcher>(N))
95
+ for (unsigned I = 0 ; I < SM->getNumChildren (); I++)
96
+ Statistic (SM->getChild (I));
97
+ else if (auto *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
98
+ for (unsigned I = 0 ; I < SOM->getNumCases (); I++)
99
+ Statistic (SOM->getCaseMatcher (I));
100
+ else if (auto *STM = dyn_cast<SwitchTypeMatcher>(N))
101
+ for (unsigned I = 0 ; I < STM->getNumCases (); I++)
102
+ Statistic (STM->getCaseMatcher (I));
103
+ else if (auto *CPM = dyn_cast<CheckComplexPatMatcher>(N))
104
+ ++ComplexPatternUsage[&CPM->getPattern ()];
105
+ N = N->getNext ();
106
+ }
107
+ };
108
+ Statistic (TheMatcher);
109
+
110
+ // Sort ComplexPatterns by usage.
111
+ std::vector<std::pair<const ComplexPattern *, unsigned >> ComplexPatternList (
112
+ ComplexPatternUsage.begin (), ComplexPatternUsage.end ());
113
+ sort (ComplexPatternList,
114
+ [](const auto &A, const auto &B) { return A.second > B.second ; });
115
+ for (const auto &ComplexPattern : ComplexPatternList)
116
+ ComplexPatterns.push_back (ComplexPattern.first );
117
+ }
89
118
90
119
unsigned EmitMatcherList (const Matcher *N, const unsigned Indent,
91
120
unsigned StartIdx, raw_ostream &OS);
@@ -146,12 +175,7 @@ class MatcherTableEmitter {
146
175
return Entry-1 ;
147
176
}
148
177
unsigned getComplexPat (const ComplexPattern &P) {
149
- unsigned &Entry = ComplexPatternMap[&P];
150
- if (Entry == 0 ) {
151
- ComplexPatterns.push_back (&P);
152
- Entry = ComplexPatterns.size ();
153
- }
154
- return Entry-1 ;
178
+ return llvm::find (ComplexPatterns, &P) - ComplexPatterns.begin ();
155
179
}
156
180
157
181
unsigned getNodeXFormID (Record *Rec) {
@@ -652,8 +676,13 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
652
676
case Matcher::CheckComplexPat: {
653
677
const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
654
678
const ComplexPattern &Pattern = CCPM->getPattern ();
655
- OS << " OPC_CheckComplexPat, /*CP*/" << getComplexPat (Pattern) << " , /*#*/"
656
- << CCPM->getMatchNumber () << ' ,' ;
679
+ unsigned PatternNo = getComplexPat (Pattern);
680
+ if (PatternNo < 8 )
681
+ OS << " OPC_CheckComplexPat" << PatternNo << " , /*#*/"
682
+ << CCPM->getMatchNumber () << ' ,' ;
683
+ else
684
+ OS << " OPC_CheckComplexPat, /*CP*/" << PatternNo << " , /*#*/"
685
+ << CCPM->getMatchNumber () << ' ,' ;
657
686
658
687
if (!OmitComments) {
659
688
OS << " // " << Pattern.getSelectFunc ();
@@ -665,7 +694,7 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
665
694
OS << " + chain result" ;
666
695
}
667
696
OS << ' \n ' ;
668
- return 3 ;
697
+ return PatternNo < 8 ? 2 : 3 ;
669
698
}
670
699
671
700
case Matcher::CheckAndImm: {
@@ -1267,7 +1296,7 @@ void llvm::EmitMatcherTable(Matcher *TheMatcher,
1267
1296
OS << " #endif\n\n " ;
1268
1297
1269
1298
BeginEmitFunction (OS, " void" , " SelectCode(SDNode *N)" , false /* AddOverride*/ );
1270
- MatcherTableEmitter MatcherEmitter (CGP);
1299
+ MatcherTableEmitter MatcherEmitter (TheMatcher, CGP);
1271
1300
1272
1301
// First we size all the children of the three kinds of matchers that have
1273
1302
// them. This is done by sharing the code in EmitMatcher(). but we don't
0 commit comments