[NFC][TableGen][DecoderEmitter] Eliminate indent for a few functions (#148718)

Eliminate the `indent` argument for functions which are always called
with `indent(0)`.
This commit is contained in:
Rahul Joshi 2025-07-14 15:23:41 -07:00 committed by GitHub
parent 99875733fc
commit 633728f3b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -227,14 +227,14 @@ public:
// Emit the decoder state machine table. Returns a mask of MCD decoder ops // Emit the decoder state machine table. Returns a mask of MCD decoder ops
// that were emitted. // that were emitted.
unsigned emitTable(formatted_raw_ostream &OS, DecoderTable &Table, unsigned emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
indent Indent, unsigned BitWidth, StringRef Namespace, unsigned BitWidth, StringRef Namespace,
const EncodingIDsVec &EncodingIDs) const; const EncodingIDsVec &EncodingIDs) const;
void emitInstrLenTable(formatted_raw_ostream &OS, void emitInstrLenTable(formatted_raw_ostream &OS,
ArrayRef<unsigned> InstrLen) const; ArrayRef<unsigned> InstrLen) const;
void emitPredicateFunction(formatted_raw_ostream &OS, void emitPredicateFunction(formatted_raw_ostream &OS,
PredicateSet &Predicates, indent Indent) const; PredicateSet &Predicates) const;
void emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders, void emitDecoderFunction(formatted_raw_ostream &OS,
indent Indent) const; DecoderSet &Decoders) const;
// run - Output the code emitter // run - Output the code emitter
void run(raw_ostream &o); void run(raw_ostream &o);
@ -833,8 +833,8 @@ unsigned Filter::usefulness() const {
// Emit the decoder state machine table. Returns a mask of MCD decoder ops // Emit the decoder state machine table. Returns a mask of MCD decoder ops
// that were emitted. // that were emitted.
unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS, unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
DecoderTable &Table, indent Indent, DecoderTable &Table, unsigned BitWidth,
unsigned BitWidth, StringRef Namespace, StringRef Namespace,
const EncodingIDsVec &EncodingIDs) const { const EncodingIDsVec &EncodingIDs) const {
// We'll need to be able to map from a decoded opcode into the corresponding // We'll need to be able to map from a decoded opcode into the corresponding
// EncodingID for this specific combination of BitWidth and Namespace. This // EncodingID for this specific combination of BitWidth and Namespace. This
@ -844,11 +844,9 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
for (const auto &EI : EncodingIDs) for (const auto &EI : EncodingIDs)
OpcodeToEncodingID[EI.Opcode] = EI.EncodingID; OpcodeToEncodingID[EI.Opcode] = EI.EncodingID;
OS << Indent << "static const uint8_t DecoderTable" << Namespace << BitWidth OS << "static const uint8_t DecoderTable" << Namespace << BitWidth
<< "[] = {\n"; << "[] = {\n";
Indent += 2;
// Emit ULEB128 encoded value to OS, returning the number of bytes emitted. // Emit ULEB128 encoded value to OS, returning the number of bytes emitted.
auto emitULEB128 = [](DecoderTable::const_iterator &I, auto emitULEB128 = [](DecoderTable::const_iterator &I,
formatted_raw_ostream &OS) { formatted_raw_ostream &OS) {
@ -905,7 +903,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
PrintFatalError("Invalid decode table opcode: " + Twine((int)DecoderOp) + PrintFatalError("Invalid decode table opcode: " + Twine((int)DecoderOp) +
" at index " + Twine(Pos)); " at index " + Twine(Pos));
case MCD::OPC_ExtractField: { case MCD::OPC_ExtractField: {
OS << Indent << "MCD::OPC_ExtractField, "; OS << " MCD::OPC_ExtractField, ";
// ULEB128 encoded start value. // ULEB128 encoded start value.
const char *ErrMsg = nullptr; const char *ErrMsg = nullptr;
@ -923,7 +921,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
case MCD::OPC_FilterValue: case MCD::OPC_FilterValue:
case MCD::OPC_FilterValueOrFail: { case MCD::OPC_FilterValueOrFail: {
bool IsFail = DecoderOp == MCD::OPC_FilterValueOrFail; bool IsFail = DecoderOp == MCD::OPC_FilterValueOrFail;
OS << Indent << "MCD::OPC_FilterValue" << (IsFail ? "OrFail, " : ", "); OS << " MCD::OPC_FilterValue" << (IsFail ? "OrFail, " : ", ");
// The filter value is ULEB128 encoded. // The filter value is ULEB128 encoded.
emitULEB128(I, OS); emitULEB128(I, OS);
@ -937,7 +935,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
case MCD::OPC_CheckField: case MCD::OPC_CheckField:
case MCD::OPC_CheckFieldOrFail: { case MCD::OPC_CheckFieldOrFail: {
bool IsFail = DecoderOp == MCD::OPC_CheckFieldOrFail; bool IsFail = DecoderOp == MCD::OPC_CheckFieldOrFail;
OS << Indent << "MCD::OPC_CheckField" << (IsFail ? "OrFail, " : ", "); OS << " MCD::OPC_CheckField" << (IsFail ? "OrFail, " : ", ");
// ULEB128 encoded start value. // ULEB128 encoded start value.
emitULEB128(I, OS); emitULEB128(I, OS);
// 8-bit length. // 8-bit length.
@ -957,7 +955,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
case MCD::OPC_CheckPredicateOrFail: { case MCD::OPC_CheckPredicateOrFail: {
bool IsFail = DecoderOp == MCD::OPC_CheckPredicateOrFail; bool IsFail = DecoderOp == MCD::OPC_CheckPredicateOrFail;
OS << Indent << "MCD::OPC_CheckPredicate" << (IsFail ? "OrFail, " : ", "); OS << " MCD::OPC_CheckPredicate" << (IsFail ? "OrFail, " : ", ");
emitULEB128(I, OS); emitULEB128(I, OS);
if (!IsFail) { if (!IsFail) {
@ -977,7 +975,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
unsigned Opc = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg); unsigned Opc = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
assert(ErrMsg == nullptr && "ULEB128 value too large!"); assert(ErrMsg == nullptr && "ULEB128 value too large!");
OS << Indent << "MCD::OPC_" << (IsTry ? "Try" : "") << "Decode" OS << " MCD::OPC_" << (IsTry ? "Try" : "") << "Decode"
<< (IsFail ? "OrFail, " : ", "); << (IsFail ? "OrFail, " : ", ");
emitULEB128(I, OS); emitULEB128(I, OS);
@ -1007,7 +1005,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
break; break;
} }
case MCD::OPC_SoftFail: { case MCD::OPC_SoftFail: {
OS << Indent << "MCD::OPC_SoftFail, "; OS << " MCD::OPC_SoftFail, ";
// Decode the positive mask. // Decode the positive mask.
const char *ErrMsg = nullptr; const char *ErrMsg = nullptr;
uint64_t PositiveMask = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg); uint64_t PositiveMask = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
@ -1026,15 +1024,12 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
break; break;
} }
case MCD::OPC_Fail: case MCD::OPC_Fail:
OS << Indent << "MCD::OPC_Fail,\n"; OS << " MCD::OPC_Fail,\n";
break; break;
} }
} }
OS << Indent << "0\n"; OS << " 0\n";
OS << "};\n\n";
Indent -= 2;
OS << Indent << "};\n\n";
return OpcodeMask; return OpcodeMask;
} }
@ -1048,27 +1043,23 @@ void DecoderEmitter::emitInstrLenTable(formatted_raw_ostream &OS,
} }
void DecoderEmitter::emitPredicateFunction(formatted_raw_ostream &OS, void DecoderEmitter::emitPredicateFunction(formatted_raw_ostream &OS,
PredicateSet &Predicates, PredicateSet &Predicates) const {
indent Indent) const {
// The predicate function is just a big switch statement based on the // The predicate function is just a big switch statement based on the
// input predicate index. // input predicate index.
OS << Indent << "static bool checkDecoderPredicate(unsigned Idx, " OS << "static bool checkDecoderPredicate(unsigned Idx, const FeatureBitset "
<< "const FeatureBitset &Bits) {\n"; "&Bits) {\n";
Indent += 2; OS << " switch (Idx) {\n";
OS << Indent << "switch (Idx) {\n"; OS << " default: llvm_unreachable(\"Invalid index!\");\n";
OS << Indent << "default: llvm_unreachable(\"Invalid index!\");\n";
for (const auto &[Index, Predicate] : enumerate(Predicates)) { for (const auto &[Index, Predicate] : enumerate(Predicates)) {
OS << Indent << "case " << Index << ":\n"; OS << " case " << Index << ":\n";
OS << Indent + 2 << "return (" << Predicate << ");\n"; OS << " return (" << Predicate << ");\n";
} }
OS << Indent << "}\n"; OS << " }\n";
Indent -= 2; OS << "}\n\n";
OS << Indent << "}\n\n";
} }
void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream &OS, void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream &OS,
DecoderSet &Decoders, DecoderSet &Decoders) const {
indent Indent) const {
// The decoder function is just a big switch statement or a table of function // The decoder function is just a big switch statement or a table of function
// pointers based on the input decoder index. // pointers based on the input decoder index.
@ -1085,53 +1076,46 @@ void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream &OS,
if (UseFnTableInDecodeToMCInst) { if (UseFnTableInDecodeToMCInst) {
// Emit a function for each case first. // Emit a function for each case first.
for (const auto &[Index, Decoder] : enumerate(Decoders)) { for (const auto &[Index, Decoder] : enumerate(Decoders)) {
OS << Indent << "template <typename InsnType>\n"; OS << "template <typename InsnType>\n";
OS << Indent << "DecodeStatus decodeFn" << Index << "(" << DecodeParams OS << "DecodeStatus decodeFn" << Index << "(" << DecodeParams << ") {\n";
<< ") {\n"; OS << " " << TmpTypeDecl;
Indent += 2; OS << " [[maybe_unused]] TmpType tmp;\n";
OS << Indent << TmpTypeDecl;
OS << Indent << "[[maybe_unused]] TmpType tmp;\n";
OS << Decoder; OS << Decoder;
OS << Indent << "return S;\n"; OS << " return S;\n";
Indent -= 2; OS << "}\n\n";
OS << Indent << "}\n\n";
} }
} }
OS << Indent << "// Handling " << Decoders.size() << " cases.\n"; OS << "// Handling " << Decoders.size() << " cases.\n";
OS << Indent << "template <typename InsnType>\n"; OS << "template <typename InsnType>\n";
OS << Indent << "static DecodeStatus decodeToMCInst(unsigned Idx, " OS << "static DecodeStatus decodeToMCInst(unsigned Idx, " << DecodeParams
<< DecodeParams << ") {\n"; << ") {\n";
Indent += 2; OS << " DecodeComplete = true;\n";
OS << Indent << "DecodeComplete = true;\n";
if (UseFnTableInDecodeToMCInst) { if (UseFnTableInDecodeToMCInst) {
// Build a table of function pointers. // Build a table of function pointers.
OS << Indent << "using DecodeFnTy = DecodeStatus (*)(" << DecodeParams OS << " using DecodeFnTy = DecodeStatus (*)(" << DecodeParams << ");\n";
<< ");\n"; OS << " static constexpr DecodeFnTy decodeFnTable[] = {\n";
OS << Indent << "static constexpr DecodeFnTy decodeFnTable[] = {\n";
for (size_t Index : llvm::seq(Decoders.size())) for (size_t Index : llvm::seq(Decoders.size()))
OS << Indent + 2 << "decodeFn" << Index << ",\n"; OS << " decodeFn" << Index << ",\n";
OS << Indent << "};\n"; OS << " };\n";
OS << Indent << "if (Idx >= " << Decoders.size() << ")\n"; OS << " if (Idx >= " << Decoders.size() << ")\n";
OS << Indent + 2 << "llvm_unreachable(\"Invalid index!\");\n"; OS << " llvm_unreachable(\"Invalid index!\");\n";
OS << Indent OS << " return decodeFnTable[Idx](S, insn, MI, Address, Decoder, "
<< "return decodeFnTable[Idx](S, insn, MI, Address, Decoder, "
"DecodeComplete);\n"; "DecodeComplete);\n";
} else { } else {
OS << Indent << TmpTypeDecl; OS << " " << TmpTypeDecl;
OS << Indent << "TmpType tmp;\n"; OS << " TmpType tmp;\n";
OS << Indent << "switch (Idx) {\n"; OS << " switch (Idx) {\n";
OS << Indent << "default: llvm_unreachable(\"Invalid index!\");\n"; OS << " default: llvm_unreachable(\"Invalid index!\");\n";
for (const auto &[Index, Decoder] : enumerate(Decoders)) { for (const auto &[Index, Decoder] : enumerate(Decoders)) {
OS << Indent << "case " << Index << ":\n"; OS << " case " << Index << ":\n";
OS << Decoder; OS << Decoder;
OS << Indent + 2 << "return S;\n"; OS << " return S;\n";
} }
OS << Indent << "}\n"; OS << " }\n";
} }
Indent -= 2; OS << "}\n";
OS << Indent << "}\n";
} }
// Populates the field of the insn given the start position and the number of // Populates the field of the insn given the start position and the number of
@ -2673,7 +2657,7 @@ namespace {
TableInfo.Table.push_back(MCD::OPC_Fail); TableInfo.Table.push_back(MCD::OPC_Fail);
// Print the table to the output stream. // Print the table to the output stream.
OpcodeMask |= emitTable(OS, TableInfo.Table, indent(0), FC.getBitWidth(), OpcodeMask |= emitTable(OS, TableInfo.Table, FC.getBitWidth(),
DecoderNamespace, EncodingIDs); DecoderNamespace, EncodingIDs);
} }
@ -2689,10 +2673,10 @@ namespace {
// Emit the predicate function. // Emit the predicate function.
if (HasCheckPredicate) if (HasCheckPredicate)
emitPredicateFunction(OS, TableInfo.Predicates, indent(0)); emitPredicateFunction(OS, TableInfo.Predicates);
// Emit the decoder function. // Emit the decoder function.
emitDecoderFunction(OS, TableInfo.Decoders, indent(0)); emitDecoderFunction(OS, TableInfo.Decoders);
// Emit the main entry point for the decoder, decodeInstruction(). // Emit the main entry point for the decoder, decodeInstruction().
emitDecodeInstruction(OS, IsVarLenInst, OpcodeMask); emitDecodeInstruction(OS, IsVarLenInst, OpcodeMask);