Skip to content

Commit 0864965

Browse files
authored
[TableGen][DecoderEmitter] Replace opcode mask with booleans (NFC) (#159113)
Extracted from #155889, which removes inclusion of `MCDecoderOps.h`.
1 parent 3fe05ba commit 0864965

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ struct DecoderTableInfo {
226226
DecoderTable Table;
227227
PredicateSet Predicates;
228228
DecoderSet Decoders;
229+
bool HasCheckPredicate;
230+
bool HasSoftFail;
231+
bool HasTryDecode;
229232

230233
void insertPredicate(StringRef Predicate) {
231234
Predicates.insert(CachedHashString(Predicate));
@@ -266,11 +269,10 @@ class DecoderEmitter {
266269

267270
const CodeGenTarget &getTarget() const { return Target; }
268271

269-
// Emit the decoder state machine table. Returns a mask of MCD decoder ops
270-
// that were emitted.
271-
unsigned emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
272-
StringRef Namespace, unsigned HwModeID, unsigned BitWidth,
273-
ArrayRef<unsigned> EncodingIDs) const;
272+
// Emit the decoder state machine table.
273+
void emitTable(formatted_raw_ostream &OS, DecoderTableInfo &TableInfo,
274+
StringRef Namespace, unsigned HwModeID, unsigned BitWidth,
275+
ArrayRef<unsigned> EncodingIDs) const;
274276
void emitInstrLenTable(formatted_raw_ostream &OS,
275277
ArrayRef<unsigned> InstrLen) const;
276278
void emitPredicateFunction(formatted_raw_ostream &OS,
@@ -629,12 +631,11 @@ static StringRef getDecoderOpName(DecoderOps Op) {
629631
llvm_unreachable("Unknown decoder op");
630632
}
631633

632-
// Emit the decoder state machine table. Returns a mask of MCD decoder ops
633-
// that were emitted.
634-
unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
635-
DecoderTable &Table, StringRef Namespace,
636-
unsigned HwModeID, unsigned BitWidth,
637-
ArrayRef<unsigned> EncodingIDs) const {
634+
// Emit the decoder state machine table.
635+
void DecoderEmitter::emitTable(formatted_raw_ostream &OS,
636+
DecoderTableInfo &TableInfo, StringRef Namespace,
637+
unsigned HwModeID, unsigned BitWidth,
638+
ArrayRef<unsigned> EncodingIDs) const {
638639
// We'll need to be able to map from a decoded opcode into the corresponding
639640
// EncodingID for this specific combination of BitWidth and Namespace. This
640641
// is used below to index into Encodings.
@@ -648,7 +649,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
648649
OS << "static const uint8_t DecoderTable" << Namespace;
649650
if (HwModeID != DefaultMode)
650651
OS << '_' << Target.getHwModes().getModeName(HwModeID);
651-
OS << BitWidth << "[" << Table.size() << "] = {\n";
652+
OS << BitWidth << "[" << TableInfo.Table.size() << "] = {\n";
652653

653654
// Emit ULEB128 encoded value to OS, returning the number of bytes emitted.
654655
auto EmitULEB128 = [](DecoderTable::const_iterator &I,
@@ -678,6 +679,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
678679

679680
// FIXME: We may be able to use the NumToSkip values to recover
680681
// appropriate indentation levels.
682+
DecoderTable &Table = TableInfo.Table;
681683
DecoderTable::const_iterator I = Table.begin();
682684
DecoderTable::const_iterator E = Table.end();
683685
const uint8_t *const EndPtr = Table.data() + Table.size();
@@ -719,15 +721,12 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
719721
return Value;
720722
};
721723

722-
unsigned OpcodeMask = 0;
723-
724724
while (I != E) {
725725
assert(I < E && "incomplete decode table entry!");
726726

727727
uint32_t Pos = I - Table.begin();
728728
EmitPos(Pos);
729729
const uint8_t DecoderOp = *I++;
730-
OpcodeMask |= (1 << DecoderOp);
731730
OS << getDecoderOpName(static_cast<DecoderOps>(DecoderOp)) << ", ";
732731
switch (DecoderOp) {
733732
default:
@@ -826,8 +825,6 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
826825
OS << '\n';
827826
}
828827
OS << "};\n\n";
829-
830-
return OpcodeMask;
831828
}
832829

833830
void DecoderEmitter::emitInstrLenTable(formatted_raw_ostream &OS,
@@ -1114,6 +1111,7 @@ void DecoderTableBuilder::emitPredicateTableEntry(unsigned EncodingID) const {
11141111

11151112
TableInfo.Table.insertOpcode(OPC_CheckPredicate);
11161113
TableInfo.Table.insertULEB128(PredicateIndex);
1114+
TableInfo.HasCheckPredicate = true;
11171115
}
11181116

11191117
void DecoderTableBuilder::emitSoftFailTableEntry(unsigned EncodingID) const {
@@ -1130,6 +1128,7 @@ void DecoderTableBuilder::emitSoftFailTableEntry(unsigned EncodingID) const {
11301128
TableInfo.Table.insertOpcode(OPC_SoftFail);
11311129
TableInfo.Table.insertULEB128(PositiveMask.getZExtValue());
11321130
TableInfo.Table.insertULEB128(NegativeMask.getZExtValue());
1131+
TableInfo.HasSoftFail = true;
11331132
}
11341133

11351134
// Emits table entries to decode the singleton.
@@ -1180,6 +1179,7 @@ void DecoderTableBuilder::emitSingletonTableEntry(
11801179
const Record *InstDef = Encodings[EncodingID].getInstruction()->TheDef;
11811180
TableInfo.Table.insertULEB128(Target.getInstrIntValue(InstDef));
11821181
TableInfo.Table.insertULEB128(DecoderIndex);
1182+
TableInfo.HasTryDecode |= DecoderOp == OPC_TryDecode;
11831183
}
11841184

11851185
std::unique_ptr<Filter>
@@ -1523,11 +1523,7 @@ void DecoderTableBuilder::emitTableEntries(const FilterChooser &FC) const {
15231523
// emitDecodeInstruction - Emit the templated helper function
15241524
// decodeInstruction().
15251525
static void emitDecodeInstruction(formatted_raw_ostream &OS, bool IsVarLenInst,
1526-
unsigned OpcodeMask) {
1527-
const bool HasTryDecode = OpcodeMask & (1 << OPC_TryDecode);
1528-
const bool HasCheckPredicate = OpcodeMask & (1 << OPC_CheckPredicate);
1529-
const bool HasSoftFail = OpcodeMask & (1 << OPC_SoftFail);
1530-
1526+
const DecoderTableInfo &TableInfo) {
15311527
OS << R"(
15321528
static unsigned decodeNumToSkip(const uint8_t *&Ptr) {
15331529
unsigned NumToSkip = *Ptr++;
@@ -1548,7 +1544,7 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
15481544
"llvm::function_ref<void(APInt &, uint64_t)> makeUp";
15491545
}
15501546
OS << ") {\n";
1551-
if (HasCheckPredicate)
1547+
if (TableInfo.HasCheckPredicate)
15521548
OS << " const FeatureBitset &Bits = STI.getFeatureBits();\n";
15531549
OS << " const uint8_t *Ptr = DecodeTable;\n";
15541550

@@ -1657,7 +1653,7 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
16571653
}
16581654
break;
16591655
})";
1660-
if (HasCheckPredicate) {
1656+
if (TableInfo.HasCheckPredicate) {
16611657
OS << R"(
16621658
case OPC_CheckPredicate: {
16631659
// Decode the Predicate Index value.
@@ -1701,7 +1697,7 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
17011697
<< (S != MCDisassembler::Fail ? "PASS\n" : "FAIL\n"));
17021698
return S;
17031699
})";
1704-
if (HasTryDecode) {
1700+
if (TableInfo.HasTryDecode) {
17051701
OS << R"(
17061702
case OPC_TryDecode: {
17071703
// Decode the Opcode value.
@@ -1735,7 +1731,7 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
17351731
break;
17361732
})";
17371733
}
1738-
if (HasSoftFail) {
1734+
if (TableInfo.HasSoftFail) {
17391735
OS << R"(
17401736
case OPC_SoftFail: {
17411737
// Decode the mask values.
@@ -1994,9 +1990,8 @@ template <typename T> constexpr uint32_t InsnBitWidth = 0;
19941990

19951991
// Entries in `EncMap` are already sorted by bitwidth. So bucketing per
19961992
// bitwidth can be done on-the-fly as we iterate over the map.
1997-
DecoderTableInfo TableInfo;
1993+
DecoderTableInfo TableInfo{};
19981994
DecoderTableBuilder TableBuilder(Target, Encodings, TableInfo);
1999-
unsigned OpcodeMask = 0;
20001995

20011996
bool HasConflict = false;
20021997
for (const auto &[BitWidth, BWMap] : EncMap) {
@@ -2021,8 +2016,8 @@ template <typename T> constexpr uint32_t InsnBitWidth = 0;
20212016
TableBuilder.buildTable(FC, BitWidth);
20222017

20232018
// Print the table to the output stream.
2024-
OpcodeMask |= emitTable(OS, TableInfo.Table, DecoderNamespace, HwModeID,
2025-
BitWidth, EncodingIDs);
2019+
emitTable(OS, TableInfo, DecoderNamespace, HwModeID, BitWidth,
2020+
EncodingIDs);
20262021
}
20272022

20282023
// Each BitWidth get's its own decoders and decoder function if
@@ -2041,14 +2036,12 @@ template <typename T> constexpr uint32_t InsnBitWidth = 0;
20412036
if (!SpecializeDecodersPerBitwidth)
20422037
emitDecoderFunction(OS, TableInfo.Decoders, 0);
20432038

2044-
const bool HasCheckPredicate = OpcodeMask & (1 << OPC_CheckPredicate);
2045-
20462039
// Emit the predicate function.
2047-
if (HasCheckPredicate)
2040+
if (TableInfo.HasCheckPredicate)
20482041
emitPredicateFunction(OS, TableInfo.Predicates);
20492042

20502043
// Emit the main entry point for the decoder, decodeInstruction().
2051-
emitDecodeInstruction(OS, IsVarLenInst, OpcodeMask);
2044+
emitDecodeInstruction(OS, IsVarLenInst, TableInfo);
20522045

20532046
OS << "\n} // namespace\n";
20542047
}

0 commit comments

Comments
 (0)