Skip to content

Commit 211abe3

Browse files
authored
[SelectionDAG] Add space-optimized forms of OPC_CheckComplexPat (#73310)
We record the usage of each `ComplexPat` and sort the `ComplexPat`s by usage. For the top 8 `ComplexPat`s, we will emit a `OPC_CheckComplexPatN` to save one byte. Overall this reduces the llc binary size with all in-tree targets by about 89K.
1 parent 3d795bd commit 211abe3

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ class SelectionDAGISel : public MachineFunctionPass {
207207
OPC_CheckChild2CondCode,
208208
OPC_CheckValueType,
209209
OPC_CheckComplexPat,
210+
OPC_CheckComplexPat0,
211+
OPC_CheckComplexPat1,
212+
OPC_CheckComplexPat2,
213+
OPC_CheckComplexPat3,
214+
OPC_CheckComplexPat4,
215+
OPC_CheckComplexPat5,
216+
OPC_CheckComplexPat6,
217+
OPC_CheckComplexPat7,
210218
OPC_CheckAndImm,
211219
OPC_CheckOrImm,
212220
OPC_CheckImmAllOnesV,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,8 +3358,18 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
33583358
break;
33593359
continue;
33603360
}
3361-
case OPC_CheckComplexPat: {
3362-
unsigned CPNum = MatcherTable[MatcherIndex++];
3361+
case OPC_CheckComplexPat:
3362+
case OPC_CheckComplexPat0:
3363+
case OPC_CheckComplexPat1:
3364+
case OPC_CheckComplexPat2:
3365+
case OPC_CheckComplexPat3:
3366+
case OPC_CheckComplexPat4:
3367+
case OPC_CheckComplexPat5:
3368+
case OPC_CheckComplexPat6:
3369+
case OPC_CheckComplexPat7: {
3370+
unsigned CPNum = Opcode == OPC_CheckComplexPat
3371+
? MatcherTable[MatcherIndex++]
3372+
: Opcode - OPC_CheckComplexPat0;
33633373
unsigned RecNo = MatcherTable[MatcherIndex++];
33643374
assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat");
33653375

llvm/test/TableGen/dag-isel-complexpattern.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def CP32 : ComplexPattern<i32, 0, "SelectCP32">;
2222
def INSTR : Instruction {
2323
// CHECK-LABEL: OPC_CheckOpcode, TARGET_VAL(ISD::STORE)
2424
// CHECK: OPC_CheckTypeI32
25-
// CHECK: OPC_CheckComplexPat, /*CP*/0, /*#*/1, // SelectCP32:$
25+
// CHECK: OPC_CheckComplexPat0, /*#*/1, // SelectCP32:$
2626
// CHECK: Src: (st (add:{ *:[i32] } (CP32:{ *:[i32] }), (CP32:{ *:[i32] })), i64:{ *:[i64] }:$addr)
2727
let OutOperandList = (outs);
2828
let InOperandList = (ins GPR64:$addr);

llvm/utils/TableGen/DAGISelMatcherEmitter.cpp

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class MatcherTableEmitter {
6363
StringMap<unsigned> PatternPredicateMap;
6464
std::vector<std::string> PatternPredicates;
6565

66-
DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
6766
std::vector<const ComplexPattern*> ComplexPatterns;
6867

6968

@@ -84,8 +83,38 @@ class MatcherTableEmitter {
8483
}
8584

8685
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+
}
89118

90119
unsigned EmitMatcherList(const Matcher *N, const unsigned Indent,
91120
unsigned StartIdx, raw_ostream &OS);
@@ -146,12 +175,7 @@ class MatcherTableEmitter {
146175
return Entry-1;
147176
}
148177
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();
155179
}
156180

157181
unsigned getNodeXFormID(Record *Rec) {
@@ -652,8 +676,13 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
652676
case Matcher::CheckComplexPat: {
653677
const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
654678
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() << ',';
657686

658687
if (!OmitComments) {
659688
OS << " // " << Pattern.getSelectFunc();
@@ -665,7 +694,7 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
665694
OS << " + chain result";
666695
}
667696
OS << '\n';
668-
return 3;
697+
return PatternNo < 8 ? 2 : 3;
669698
}
670699

671700
case Matcher::CheckAndImm: {
@@ -1267,7 +1296,7 @@ void llvm::EmitMatcherTable(Matcher *TheMatcher,
12671296
OS << "#endif\n\n";
12681297

12691298
BeginEmitFunction(OS, "void", "SelectCode(SDNode *N)", false/*AddOverride*/);
1270-
MatcherTableEmitter MatcherEmitter(CGP);
1299+
MatcherTableEmitter MatcherEmitter(TheMatcher, CGP);
12711300

12721301
// First we size all the children of the three kinds of matchers that have
12731302
// them. This is done by sharing the code in EmitMatcher(). but we don't

0 commit comments

Comments
 (0)