Skip to content

Commit 5c8d123

Browse files
authored
[SelectionDAG] Add space-optimized forms of OPC_CheckPatternPredicate (llvm#73319)
We record the usage of each `PatternPredicate` and sort them by usage. For the top 8 `PatternPredicate`s, we will emit a `OPC_CheckPatternPredicateN` to save one byte. The old `OPC_CheckPatternPredicate2` is renamed to `OPC_CheckPatternPredicateTwoByte`. Overall this reduces the llc binary size with all in-tree targets by about 93K.
1 parent 211abe3 commit 5c8d123

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ class SelectionDAGISel : public MachineFunctionPass {
159159
OPC_CheckChild2Same,
160160
OPC_CheckChild3Same,
161161
OPC_CheckPatternPredicate,
162+
OPC_CheckPatternPredicate0,
163+
OPC_CheckPatternPredicate1,
162164
OPC_CheckPatternPredicate2,
165+
OPC_CheckPatternPredicate3,
166+
OPC_CheckPatternPredicate4,
167+
OPC_CheckPatternPredicate5,
168+
OPC_CheckPatternPredicate6,
169+
OPC_CheckPatternPredicate7,
170+
OPC_CheckPatternPredicateTwoByte,
163171
OPC_CheckPredicate,
164172
OPC_CheckPredicateWithOperands,
165173
OPC_CheckOpcode,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,9 +2697,14 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE static bool CheckChildSame(
26972697

26982698
/// CheckPatternPredicate - Implements OP_CheckPatternPredicate.
26992699
LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
2700-
CheckPatternPredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
2701-
const SelectionDAGISel &SDISel, bool TwoBytePredNo) {
2702-
unsigned PredNo = MatcherTable[MatcherIndex++];
2700+
CheckPatternPredicate(unsigned Opcode, const unsigned char *MatcherTable,
2701+
unsigned &MatcherIndex, const SelectionDAGISel &SDISel) {
2702+
bool TwoBytePredNo =
2703+
Opcode == SelectionDAGISel::OPC_CheckPatternPredicateTwoByte;
2704+
unsigned PredNo =
2705+
TwoBytePredNo || Opcode == SelectionDAGISel::OPC_CheckPatternPredicate
2706+
? MatcherTable[MatcherIndex++]
2707+
: Opcode - SelectionDAGISel::OPC_CheckPatternPredicate0;
27032708
if (TwoBytePredNo)
27042709
PredNo |= MatcherTable[MatcherIndex++] << 8;
27052710
return SDISel.CheckPatternPredicate(PredNo);
@@ -2851,10 +2856,16 @@ static unsigned IsPredicateKnownToFail(const unsigned char *Table,
28512856
Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Same);
28522857
return Index;
28532858
case SelectionDAGISel::OPC_CheckPatternPredicate:
2859+
case SelectionDAGISel::OPC_CheckPatternPredicate0:
2860+
case SelectionDAGISel::OPC_CheckPatternPredicate1:
28542861
case SelectionDAGISel::OPC_CheckPatternPredicate2:
2855-
Result = !::CheckPatternPredicate(
2856-
Table, Index, SDISel,
2857-
Table[Index - 1] == SelectionDAGISel::OPC_CheckPatternPredicate2);
2862+
case SelectionDAGISel::OPC_CheckPatternPredicate3:
2863+
case SelectionDAGISel::OPC_CheckPatternPredicate4:
2864+
case SelectionDAGISel::OPC_CheckPatternPredicate5:
2865+
case SelectionDAGISel::OPC_CheckPatternPredicate6:
2866+
case SelectionDAGISel::OPC_CheckPatternPredicate7:
2867+
case SelectionDAGISel::OPC_CheckPatternPredicateTwoByte:
2868+
Result = !::CheckPatternPredicate(Opcode, Table, Index, SDISel);
28582869
return Index;
28592870
case SelectionDAGISel::OPC_CheckPredicate:
28602871
Result = !::CheckNodePredicate(Table, Index, SDISel, N.getNode());
@@ -3336,9 +3347,16 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
33363347
continue;
33373348

33383349
case OPC_CheckPatternPredicate:
3350+
case OPC_CheckPatternPredicate0:
3351+
case OPC_CheckPatternPredicate1:
33393352
case OPC_CheckPatternPredicate2:
3340-
if (!::CheckPatternPredicate(MatcherTable, MatcherIndex, *this,
3341-
Opcode == OPC_CheckPatternPredicate2))
3353+
case OPC_CheckPatternPredicate3:
3354+
case OPC_CheckPatternPredicate4:
3355+
case OPC_CheckPatternPredicate5:
3356+
case OPC_CheckPatternPredicate6:
3357+
case OPC_CheckPatternPredicate7:
3358+
case OPC_CheckPatternPredicateTwoByte:
3359+
if (!::CheckPatternPredicate(Opcode, MatcherTable, MatcherIndex, *this))
33423360
break;
33433361
continue;
33443362
case OPC_CheckPredicate:

llvm/utils/TableGen/DAGISelMatcherEmitter.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class MatcherTableEmitter {
6060
// all the patterns with "identical" predicates.
6161
StringMap<TinyPtrVector<TreePattern *>> NodePredicatesByCodeToRun;
6262

63-
StringMap<unsigned> PatternPredicateMap;
6463
std::vector<std::string> PatternPredicates;
6564

6665
std::vector<const ComplexPattern*> ComplexPatterns;
@@ -87,6 +86,8 @@ class MatcherTableEmitter {
8786
: CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1, 0) {
8887
// Record the usage of ComplexPattern.
8988
DenseMap<const ComplexPattern *, unsigned> ComplexPatternUsage;
89+
// Record the usage of PatternPredicate.
90+
std::map<StringRef, unsigned> PatternPredicateUsage;
9091

9192
// Iterate the whole MatcherTable once and do some statistics.
9293
std::function<void(const Matcher *)> Statistic = [&](const Matcher *N) {
@@ -102,6 +103,8 @@ class MatcherTableEmitter {
102103
Statistic(STM->getCaseMatcher(I));
103104
else if (auto *CPM = dyn_cast<CheckComplexPatMatcher>(N))
104105
++ComplexPatternUsage[&CPM->getPattern()];
106+
else if (auto *CPPM = dyn_cast<CheckPatternPredicateMatcher>(N))
107+
++PatternPredicateUsage[CPPM->getPredicate()];
105108
N = N->getNext();
106109
}
107110
};
@@ -114,6 +117,14 @@ class MatcherTableEmitter {
114117
[](const auto &A, const auto &B) { return A.second > B.second; });
115118
for (const auto &ComplexPattern : ComplexPatternList)
116119
ComplexPatterns.push_back(ComplexPattern.first);
120+
121+
// Sort PatternPredicates by usage.
122+
std::vector<std::pair<std::string, unsigned>> PatternPredicateList(
123+
PatternPredicateUsage.begin(), PatternPredicateUsage.end());
124+
sort(PatternPredicateList,
125+
[](const auto &A, const auto &B) { return A.second > B.second; });
126+
for (const auto &PatternPredicate : PatternPredicateList)
127+
PatternPredicates.push_back(PatternPredicate.first);
117128
}
118129

119130
unsigned EmitMatcherList(const Matcher *N, const unsigned Indent,
@@ -167,12 +178,7 @@ class MatcherTableEmitter {
167178
}
168179

169180
unsigned getPatternPredicate(StringRef PredName) {
170-
unsigned &Entry = PatternPredicateMap[PredName];
171-
if (Entry == 0) {
172-
PatternPredicates.push_back(PredName.str());
173-
Entry = PatternPredicates.size();
174-
}
175-
return Entry-1;
181+
return llvm::find(PatternPredicates, PredName) - PatternPredicates.begin();
176182
}
177183
unsigned getComplexPat(const ComplexPattern &P) {
178184
return llvm::find(ComplexPatterns, &P) - ComplexPatterns.begin();
@@ -510,13 +516,15 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
510516
StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
511517
unsigned PredNo = getPatternPredicate(Pred);
512518
if (PredNo > 255)
513-
OS << "OPC_CheckPatternPredicate2, TARGET_VAL(" << PredNo << "),";
519+
OS << "OPC_CheckPatternPredicateTwoByte, TARGET_VAL(" << PredNo << "),";
520+
else if (PredNo < 8)
521+
OS << "OPC_CheckPatternPredicate" << PredNo << ',';
514522
else
515523
OS << "OPC_CheckPatternPredicate, " << PredNo << ',';
516524
if (!OmitComments)
517525
OS << " // " << Pred;
518526
OS << '\n';
519-
return 2 + (PredNo > 255);
527+
return 2 + (PredNo > 255) - (PredNo < 8);
520528
}
521529
case Matcher::CheckPredicate: {
522530
TreePredicateFn Pred = cast<CheckPredicateMatcher>(N)->getPredicate();

0 commit comments

Comments
 (0)