Skip to content

Commit 82acc31

Browse files
authored
[TableGen][DecoderEmitter] Merge OPC_Decode with OPC_TryDecode (#159178)
OPC_Decode is a specialized OPC_TryDecode. The difference between them is that OPC_TryDecode performs a "completeness check", while OPC_Decode asserts that the check passes. The check is just a boolean test, which is nothing compared to the complexity of the decoding process, so there is no point in having a special opcode that optimizes the check.
1 parent e87e028 commit 82acc31

File tree

7 files changed

+23
-59
lines changed

7 files changed

+23
-59
lines changed

llvm/include/llvm/MC/MCDecoderOps.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ enum DecoderOps {
2424
// uleb128 Val)
2525
OPC_CheckPredicate, // OPC_CheckPredicate(uleb128 PIdx)
2626
OPC_Decode, // OPC_Decode(uleb128 Opcode, uleb128 DIdx)
27-
OPC_TryDecode, // OPC_TryDecode(uleb128 Opcode, uleb128 DIdx)
2827
OPC_SoftFail, // OPC_SoftFail(uleb128 PMask, uleb128 NMask)
2928
};
3029

llvm/test/TableGen/DecoderEmitter/additional-encoding.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ class I<dag out_ops, dag in_ops> : Instruction {
3535
// CHECK-NEXT: /* 7 */ OPC_Scope, 8, 0, // end scope at 18
3636
// CHECK-NEXT: /* 10 */ OPC_CheckField, 6, 6, 0, // if Inst{11-6} != 0x0
3737
// CHECK-NEXT: /* 14 */ OPC_Decode, {{[0-9]+}}, 2, 0, // Opcode: {{.*}}:NOP, DecodeIdx: 0
38-
// CHECK-NEXT: /* 18 */ OPC_TryDecode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT0, DecodeIdx: 1
38+
// CHECK-NEXT: /* 18 */ OPC_Decode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT0, DecodeIdx: 1
3939
// CHECK-NEXT: /* 22 */ OPC_FilterValueOrSkip, 1, 15, 0, // if Field != 0x1 skip to 41
4040
// CHECK-NEXT: /* 26 */ OPC_Scope, 8, 0, // end scope at 37
4141
// CHECK-NEXT: /* 29 */ OPC_CheckField, 6, 6, 0, // if Inst{11-6} != 0x0
4242
// CHECK-NEXT: /* 33 */ OPC_Decode, {{[0-9]+}}, 2, 0, // Opcode: {{.*}}:NOP, DecodeIdx: 0
43-
// CHECK-NEXT: /* 37 */ OPC_TryDecode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT1, DecodeIdx: 1
43+
// CHECK-NEXT: /* 37 */ OPC_Decode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT1, DecodeIdx: 1
4444
// CHECK-NEXT: /* 41 */ OPC_FilterValueOrSkip, 2, 15, 0, // if Field != 0x2 skip to 60
4545
// CHECK-NEXT: /* 45 */ OPC_Scope, 8, 0, // end scope at 56
4646
// CHECK-NEXT: /* 48 */ OPC_CheckField, 6, 6, 0, // if Inst{11-6} != 0x0
4747
// CHECK-NEXT: /* 52 */ OPC_Decode, {{[0-9]+}}, 2, 0, // Opcode: {{.*}}:NOP, DecodeIdx: 0
48-
// CHECK-NEXT: /* 56 */ OPC_TryDecode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT2, DecodeIdx: 1
48+
// CHECK-NEXT: /* 56 */ OPC_Decode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT2, DecodeIdx: 1
4949
// CHECK-NEXT: /* 60 */ OPC_FilterValue, 3, // if Field != 0x3
5050
// CHECK-NEXT: /* 62 */ OPC_Scope, 8, 0, // end scope at 73
5151
// CHECK-NEXT: /* 65 */ OPC_CheckField, 6, 6, 0, // if Inst{11-6} != 0x0
5252
// CHECK-NEXT: /* 69 */ OPC_Decode, {{[0-9]+}}, 2, 0, // Opcode: {{.*}}:NOP, DecodeIdx: 0
53-
// CHECK-NEXT: /* 73 */ OPC_TryDecode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT3, DecodeIdx: 1
53+
// CHECK-NEXT: /* 73 */ OPC_Decode, {{[0-9]+}}, 2, 1, // Opcode: SHIFT3, DecodeIdx: 1
5454

5555

5656
class SHIFT<bits<2> opc> : I<(outs), (ins ShAmtOp:$shamt)>, EncSHIFT<opc>;

llvm/test/TableGen/DecoderEmitter/trydecode-emission.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def InstB : TestInstruction {
3737
// CHECK: /* 0 */ OPC_CheckField, 4, 4, 0, // if Inst{7-4} != 0x0
3838
// CHECK-NEXT: /* 4 */ OPC_Scope, 8, 0, // end scope at 15
3939
// CHECK-NEXT: /* 7 */ OPC_CheckField, 2, 2, 0, // if Inst{3-2} != 0x0
40-
// CHECK-NEXT: /* 11 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0, // Opcode: InstB, DecodeIdx: 0
40+
// CHECK-NEXT: /* 11 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0, // Opcode: InstB, DecodeIdx: 0
4141
// CHECK-NEXT: /* 15 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: InstA, DecodeIdx: 1
4242
// CHECK-NEXT: };
4343

@@ -50,7 +50,7 @@ def InstB : TestInstruction {
5050
// CHECK-LARGE: /* 0 */ OPC_CheckField, 4, 4, 0, // if Inst{7-4} != 0x0
5151
// CHECK-LARGE-NEXT: /* 4 */ OPC_Scope, 8, 0, 0, // end scope at 16
5252
// CHECK-LARGE-NEXT: /* 8 */ OPC_CheckField, 2, 2, 0, // if Inst{3-2} != 0x0
53-
// CHECK-LARGE-NEXT: /* 12 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0, // Opcode: InstB, DecodeIdx: 0
53+
// CHECK-LARGE-NEXT: /* 12 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0, // Opcode: InstB, DecodeIdx: 0
5454
// CHECK-LARGE-NEXT: /* 16 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: InstA, DecodeIdx: 1
5555
// CHECK-LARGE-NEXT: };
5656

llvm/test/TableGen/DecoderEmitter/trydecode-emission2.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def InstB : TestInstruction {
3535
// CHECK-NEXT: /* 4 */ OPC_CheckField, 5, 3, 0,
3636
// CHECK-NEXT: /* 8 */ OPC_Scope, 8, 0, // end scope at 19
3737
// CHECK-NEXT: /* 11 */ OPC_CheckField, 0, 2, 3,
38-
// CHECK-NEXT: /* 15 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0,
38+
// CHECK-NEXT: /* 15 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0,
3939
// CHECK-NEXT: /* 19 */ OPC_CheckField, 3, 2, 0,
40-
// CHECK-NEXT: /* 23 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 1,
40+
// CHECK-NEXT: /* 23 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1,
4141

4242
// CHECK: if (!Check(S, DecodeInstB(MI, insn, Address, Decoder))) { DecodeComplete = false; return MCDisassembler::Fail; }
4343
// CHECK: if (!Check(S, DecodeInstA(MI, insn, Address, Decoder))) { DecodeComplete = false; return MCDisassembler::Fail; }
@@ -46,9 +46,9 @@ def InstB : TestInstruction {
4646
// CHECK-LARGE-NEXT: /* 4 */ OPC_CheckField, 5, 3, 0,
4747
// CHECK-LARGE-NEXT: /* 8 */ OPC_Scope, 8, 0, 0, // end scope at 20
4848
// CHECK-LARGE-NEXT: /* 12 */ OPC_CheckField, 0, 2, 3,
49-
// CHECK-LARGE-NEXT: /* 16 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0,
49+
// CHECK-LARGE-NEXT: /* 16 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0,
5050
// CHECK-LARGE-NEXT: /* 20 */ OPC_CheckField, 3, 2, 0,
51-
// CHECK-LARGE-NEXT: /* 24 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 1,
51+
// CHECK-LARGE-NEXT: /* 24 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1,
5252
// CHECK-LARGE-NEXT: };
5353

5454
// CHECK-LARGE: if (!Check(S, DecodeInstB(MI, insn, Address, Decoder))) { DecodeComplete = false; return MCDisassembler::Fail; }

llvm/test/TableGen/DecoderEmitter/trydecode-emission3.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def InstB : TestInstruction {
3838
// CHECK: /* 0 */ OPC_CheckField, 4, 4, 0,
3939
// CHECK-NEXT: /* 4 */ OPC_Scope, 8, 0, // end scope at 15
4040
// CHECK-NEXT: /* 7 */ OPC_CheckField, 2, 2, 0,
41-
// CHECK-NEXT: /* 11 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0,
41+
// CHECK-NEXT: /* 11 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0,
4242
// CHECK-NEXT: /* 15 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: InstA, DecodeIdx: 1
4343
// CHECK-NEXT: };
4444

@@ -47,7 +47,7 @@ def InstB : TestInstruction {
4747
// CHECK-LARGE: /* 0 */ OPC_CheckField, 4, 4, 0,
4848
// CHECK-LARGE-NEXT: /* 4 */ OPC_Scope, 8, 0, 0, // end scope at 16
4949
// CHECK-LARGE-NEXT: /* 8 */ OPC_CheckField, 2, 2, 0,
50-
// CHECK-LARGE-NEXT: /* 12 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0,
50+
// CHECK-LARGE-NEXT: /* 12 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0,
5151
// CHECK-LARGE-NEXT: /* 16 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: InstA, DecodeIdx: 1
5252
// CHECK-LARGE-NEXT: };
5353

llvm/test/TableGen/DecoderEmitter/trydecode-emission4.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def InstB : TestInstruction {
3636
// CHECK: /* 0 */ OPC_CheckField, 250, 3, 4, 0,
3737
// CHECK-NEXT: /* 5 */ OPC_Scope, 9, 0, // end scope at 17
3838
// CHECK-NEXT: /* 8 */ OPC_CheckField, 248, 3, 2, 0,
39-
// CHECK-NEXT: /* 13 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0,
39+
// CHECK-NEXT: /* 13 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0,
4040
// CHECK-NEXT: /* 17 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: InstA, DecodeIdx: 1
4141
// CHECK-NEXT: };
4242

@@ -46,7 +46,7 @@ def InstB : TestInstruction {
4646
// CHECK-LARGE: /* 0 */ OPC_CheckField, 250, 3, 4, 0,
4747
// CHECK-LARGE-NEXT: /* 5 */ OPC_Scope, 9, 0, 0, // end scope at 18
4848
// CHECK-LARGE-NEXT: /* 9 */ OPC_CheckField, 248, 3, 2, 0,
49-
// CHECK-LARGE-NEXT: /* 14 */ OPC_TryDecode, {{[0-9]+}}, {{[0-9]+}}, 0,
49+
// CHECK-LARGE-NEXT: /* 14 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0,
5050
// CHECK-LARGE-NEXT: /* 18 */ OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: InstA, DecodeIdx: 1
5151
// CHECK-LARGE-NEXT: };
5252

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ struct DecoderTableInfo {
228228
DecoderSet Decoders;
229229
bool HasCheckPredicate;
230230
bool HasSoftFail;
231-
bool HasTryDecode;
232231

233232
void insertPredicate(StringRef Predicate) {
234233
Predicates.insert(CachedHashString(Predicate));
@@ -624,7 +623,6 @@ static StringRef getDecoderOpName(DecoderOps Op) {
624623
CASE(OPC_CheckField);
625624
CASE(OPC_CheckPredicate);
626625
CASE(OPC_Decode);
627-
CASE(OPC_TryDecode);
628626
CASE(OPC_SoftFail);
629627
}
630628
#undef CASE
@@ -792,8 +790,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS,
792790
OS << "if !checkPredicate(" << PIdx << ") pop scope";
793791
break;
794792
}
795-
case OPC_Decode:
796-
case OPC_TryDecode: {
793+
case OPC_Decode: {
797794
// Decode the Opcode value.
798795
unsigned Opc = DecodeAndEmitULEB128(I, OS);
799796

@@ -1164,22 +1161,10 @@ void DecoderTableBuilder::emitSingletonTableEntry(
11641161
TableInfo.insertDecoder(Decoder);
11651162
unsigned DecoderIndex = TableInfo.getDecoderIndex(Decoder);
11661163

1167-
// Produce OPC_Decode or OPC_TryDecode opcode based on the information
1168-
// whether the instruction decoder is complete or not. If it is complete
1169-
// then it handles all possible values of remaining variable/unfiltered bits
1170-
// and for any value can determine if the bitpattern is a valid instruction
1171-
// or not. This means OPC_Decode will be the final step in the decoding
1172-
// process. If it is not complete, then the Fail return code from the
1173-
// decoder method indicates that additional processing should be done to see
1174-
// if there is any other instruction that also matches the bitpattern and
1175-
// can decode it.
1176-
const DecoderOps DecoderOp =
1177-
Encoding.hasCompleteDecoder() ? OPC_Decode : OPC_TryDecode;
1178-
TableInfo.Table.insertOpcode(DecoderOp);
1164+
TableInfo.Table.insertOpcode(MCD::OPC_Decode);
11791165
const Record *InstDef = Encodings[EncodingID].getInstruction()->TheDef;
11801166
TableInfo.Table.insertULEB128(Target.getInstrIntValue(InstDef));
11811167
TableInfo.Table.insertULEB128(DecoderIndex);
1182-
TableInfo.HasTryDecode |= DecoderOp == OPC_TryDecode;
11831168
}
11841169

11851170
std::unique_ptr<Filter>
@@ -1689,48 +1674,28 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
16891674
<< " makeUp(insn, Len);";
16901675
}
16911676
OS << R"(
1692-
S = decodeToMCInst(DecodeIdx, S, insn, MI, Address, DisAsm, DecodeComplete);
1693-
assert(DecodeComplete);
1694-
1677+
S = decodeToMCInst(DecodeIdx, S, insn, MI, Address, DisAsm,
1678+
DecodeComplete);
16951679
LLVM_DEBUG(dbgs() << Loc << ": OPC_Decode: opcode " << Opc
1696-
<< ", using decoder " << DecodeIdx << ": "
1697-
<< (S != MCDisassembler::Fail ? "PASS\n" : "FAIL\n"));
1698-
return S;
1699-
})";
1700-
if (TableInfo.HasTryDecode) {
1701-
OS << R"(
1702-
case OPC_TryDecode: {
1703-
// Decode the Opcode value.
1704-
unsigned Opc = decodeULEB128AndIncUnsafe(Ptr);
1705-
unsigned DecodeIdx = decodeULEB128AndIncUnsafe(Ptr);
1706-
1707-
// Perform the decode operation.
1708-
MCInst TmpMI;
1709-
TmpMI.setOpcode(Opc);
1710-
bool DecodeComplete;
1711-
S = decodeToMCInst(DecodeIdx, S, insn, TmpMI, Address, DisAsm, DecodeComplete);
1712-
LLVM_DEBUG(dbgs() << Loc << ": OPC_TryDecode: opcode " << Opc
1713-
<< ", using decoder " << DecodeIdx << ": ");
1680+
<< ", using decoder " << DecodeIdx << ": "
1681+
<< (S ? "PASS, " : "FAIL, "));
17141682
17151683
if (DecodeComplete) {
1716-
// Decoding complete.
1717-
LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? "PASS\n" : "FAIL\n"));
1718-
MI = TmpMI;
1684+
LLVM_DEBUG(dbgs() << "decoding complete\n");
17191685
return S;
17201686
}
17211687
assert(S == MCDisassembler::Fail);
17221688
if (ScopeStack.empty()) {
1723-
LLVM_DEBUG(dbgs() << "FAIL, returning FAIL\n");
1689+
LLVM_DEBUG(dbgs() << "returning Fail\n");
17241690
return MCDisassembler::Fail;
17251691
}
17261692
Ptr = ScopeStack.pop_back_val();
1727-
LLVM_DEBUG(dbgs() << "FAIL, continuing at " << Ptr - DecodeTable << '\n');
1693+
LLVM_DEBUG(dbgs() << "continuing at " << Ptr - DecodeTable << '\n');
17281694
// Reset decode status. This also drops a SoftFail status that could be
17291695
// set before the decode attempt.
17301696
S = MCDisassembler::Success;
17311697
break;
17321698
})";
1733-
}
17341699
if (TableInfo.HasSoftFail) {
17351700
OS << R"(
17361701
case OPC_SoftFail: {

0 commit comments

Comments
 (0)