Skip to content

Commit e052c68

Browse files
authored
[SelectionDAG] Add instantiated OPC_CheckType (#73283)
The most common type is i32 or i64 so we add `OPC_CheckTypeI32` and `OPC_CheckTypeI64` to save one byte. Overall this reduces the llc binary size with all in-tree targets by about 29K.
1 parent ceb0237 commit e052c68

File tree

4 files changed

+69
-30
lines changed

4 files changed

+69
-30
lines changed

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ class SelectionDAGISel : public MachineFunctionPass {
156156
OPC_CheckOpcode,
157157
OPC_SwitchOpcode,
158158
OPC_CheckType,
159+
// Space-optimized forms that implicitly encode VT.
160+
OPC_CheckTypeI32,
161+
OPC_CheckTypeI64,
159162
OPC_CheckTypeRes,
160163
OPC_SwitchType,
161164
OPC_CheckChild0Type,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,11 +2711,10 @@ CheckOpcode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
27112711
return N->getOpcode() == Opc;
27122712
}
27132713

2714-
LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
2715-
CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N,
2716-
const TargetLowering *TLI, const DataLayout &DL) {
2717-
MVT::SimpleValueType VT =
2718-
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
2714+
LLVM_ATTRIBUTE_ALWAYS_INLINE static bool CheckType(MVT::SimpleValueType VT,
2715+
SDValue N,
2716+
const TargetLowering *TLI,
2717+
const DataLayout &DL) {
27192718
if (N.getValueType() == VT)
27202719
return true;
27212720

@@ -2724,13 +2723,11 @@ CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N,
27242723
}
27252724

27262725
LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
2727-
CheckChildType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
2728-
SDValue N, const TargetLowering *TLI, const DataLayout &DL,
2729-
unsigned ChildNo) {
2726+
CheckChildType(MVT::SimpleValueType VT, SDValue N, const TargetLowering *TLI,
2727+
const DataLayout &DL, unsigned ChildNo) {
27302728
if (ChildNo >= N.getNumOperands())
2731-
return false; // Match fails if out of range child #.
2732-
return ::CheckType(MatcherTable, MatcherIndex, N.getOperand(ChildNo), TLI,
2733-
DL);
2729+
return false; // Match fails if out of range child #.
2730+
return ::CheckType(VT, N.getOperand(ChildNo), TLI, DL);
27342731
}
27352732

27362733
LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
@@ -2829,7 +2826,8 @@ static unsigned IsPredicateKnownToFail(const unsigned char *Table,
28292826
bool &Result,
28302827
const SelectionDAGISel &SDISel,
28312828
SmallVectorImpl<std::pair<SDValue, SDNode*>> &RecordedNodes) {
2832-
switch (Table[Index++]) {
2829+
unsigned Opcode = Table[Index++];
2830+
switch (Opcode) {
28332831
default:
28342832
Result = false;
28352833
return Index-1; // Could not evaluate this predicate.
@@ -2856,12 +2854,27 @@ static unsigned IsPredicateKnownToFail(const unsigned char *Table,
28562854
Result = !::CheckOpcode(Table, Index, N.getNode());
28572855
return Index;
28582856
case SelectionDAGISel::OPC_CheckType:
2859-
Result = !::CheckType(Table, Index, N, SDISel.TLI,
2860-
SDISel.CurDAG->getDataLayout());
2857+
case SelectionDAGISel::OPC_CheckTypeI32:
2858+
case SelectionDAGISel::OPC_CheckTypeI64: {
2859+
MVT::SimpleValueType VT;
2860+
switch (Opcode) {
2861+
case SelectionDAGISel::OPC_CheckTypeI32:
2862+
VT = MVT::i32;
2863+
break;
2864+
case SelectionDAGISel::OPC_CheckTypeI64:
2865+
VT = MVT::i64;
2866+
break;
2867+
default:
2868+
VT = static_cast<MVT::SimpleValueType>(Table[Index++]);
2869+
break;
2870+
}
2871+
Result = !::CheckType(VT, N, SDISel.TLI, SDISel.CurDAG->getDataLayout());
28612872
return Index;
2873+
}
28622874
case SelectionDAGISel::OPC_CheckTypeRes: {
28632875
unsigned Res = Table[Index++];
2864-
Result = !::CheckType(Table, Index, N.getValue(Res), SDISel.TLI,
2876+
Result = !::CheckType(static_cast<MVT::SimpleValueType>(Table[Index++]),
2877+
N.getValue(Res), SDISel.TLI,
28652878
SDISel.CurDAG->getDataLayout());
28662879
return Index;
28672880
}
@@ -2872,11 +2885,13 @@ static unsigned IsPredicateKnownToFail(const unsigned char *Table,
28722885
case SelectionDAGISel::OPC_CheckChild4Type:
28732886
case SelectionDAGISel::OPC_CheckChild5Type:
28742887
case SelectionDAGISel::OPC_CheckChild6Type:
2875-
case SelectionDAGISel::OPC_CheckChild7Type:
2876-
Result = !::CheckChildType(
2877-
Table, Index, N, SDISel.TLI, SDISel.CurDAG->getDataLayout(),
2878-
Table[Index - 1] - SelectionDAGISel::OPC_CheckChild0Type);
2888+
case SelectionDAGISel::OPC_CheckChild7Type: {
2889+
Result =
2890+
!::CheckChildType(static_cast<MVT::SimpleValueType>(Table[Index++]), N,
2891+
SDISel.TLI, SDISel.CurDAG->getDataLayout(),
2892+
Opcode - SelectionDAGISel::OPC_CheckChild0Type);
28792893
return Index;
2894+
}
28802895
case SelectionDAGISel::OPC_CheckCondCode:
28812896
Result = !::CheckCondCode(Table, Index, N);
28822897
return Index;
@@ -3306,15 +3321,29 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
33063321
continue;
33073322

33083323
case OPC_CheckType:
3309-
if (!::CheckType(MatcherTable, MatcherIndex, N, TLI,
3310-
CurDAG->getDataLayout()))
3324+
case OPC_CheckTypeI32:
3325+
case OPC_CheckTypeI64:
3326+
MVT::SimpleValueType VT;
3327+
switch (Opcode) {
3328+
case OPC_CheckTypeI32:
3329+
VT = MVT::i32;
3330+
break;
3331+
case OPC_CheckTypeI64:
3332+
VT = MVT::i64;
3333+
break;
3334+
default:
3335+
VT = static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3336+
break;
3337+
}
3338+
if (!::CheckType(VT, N, TLI, CurDAG->getDataLayout()))
33113339
break;
33123340
continue;
33133341

33143342
case OPC_CheckTypeRes: {
33153343
unsigned Res = MatcherTable[MatcherIndex++];
3316-
if (!::CheckType(MatcherTable, MatcherIndex, N.getValue(Res), TLI,
3317-
CurDAG->getDataLayout()))
3344+
if (!::CheckType(
3345+
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]),
3346+
N.getValue(Res), TLI, CurDAG->getDataLayout()))
33183347
break;
33193348
continue;
33203349
}
@@ -3387,9 +3416,9 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
33873416
case OPC_CheckChild2Type: case OPC_CheckChild3Type:
33883417
case OPC_CheckChild4Type: case OPC_CheckChild5Type:
33893418
case OPC_CheckChild6Type: case OPC_CheckChild7Type:
3390-
if (!::CheckChildType(MatcherTable, MatcherIndex, N, TLI,
3391-
CurDAG->getDataLayout(),
3392-
Opcode - OPC_CheckChild0Type))
3419+
if (!::CheckChildType(
3420+
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]),
3421+
N, TLI, CurDAG->getDataLayout(), Opcode - OPC_CheckChild0Type))
33933422
break;
33943423
continue;
33953424
case OPC_CheckCondCode:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def CP32 : ComplexPattern<i32, 0, "SelectCP32">;
2121
// (CP32) still worked.
2222
def INSTR : Instruction {
2323
// CHECK-LABEL: OPC_CheckOpcode, TARGET_VAL(ISD::STORE)
24-
// CHECK: OPC_CheckType, MVT::i32
24+
// CHECK: OPC_CheckTypeI32
2525
// CHECK: OPC_CheckComplexPat, /*CP*/0, /*#*/1, // SelectCP32:$
2626
// CHECK: Src: (st (add:{ *:[i32] } (CP32:{ *:[i32] }), (CP32:{ *:[i32] })), i64:{ *:[i64] }:$addr)
2727
let OutOperandList = (outs);

llvm/utils/TableGen/DAGISelMatcherEmitter.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,16 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
578578

579579
case Matcher::CheckType:
580580
if (cast<CheckTypeMatcher>(N)->getResNo() == 0) {
581-
OS << "OPC_CheckType, "
582-
<< getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
583-
return 2;
581+
MVT::SimpleValueType VT = cast<CheckTypeMatcher>(N)->getType();
582+
switch (VT) {
583+
case MVT::i32:
584+
case MVT::i64:
585+
OS << "OPC_CheckTypeI" << MVT(VT).getSizeInBits() << ",\n";
586+
return 1;
587+
default:
588+
OS << "OPC_CheckType, " << getEnumName(VT) << ",\n";
589+
return 2;
590+
}
584591
}
585592
OS << "OPC_CheckTypeRes, " << cast<CheckTypeMatcher>(N)->getResNo()
586593
<< ", " << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";

0 commit comments

Comments
 (0)