diff options
| author | Sergei Barannikov <barannikov88@gmail.com> | 2025-08-12 17:44:01 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-12 17:44:01 +0300 |
| commit | 2f9f92ad01c06c7b02d15a730cd609db95705882 (patch) | |
| tree | 1f97740d9e02fb661d3ac3647609a6eca0bff1f3 /llvm/utils/TableGen/CodeEmitterGen.cpp | |
| parent | d8ce19ae6b759d762fe067bc42548e425c1ea060 (diff) | |
[TableGen] Use getValueAsOptionalDef to simplify code (NFC) (#153170)
Diffstat (limited to 'llvm/utils/TableGen/CodeEmitterGen.cpp')
| -rw-r--r-- | llvm/utils/TableGen/CodeEmitterGen.cpp | 127 |
1 files changed, 60 insertions, 67 deletions
diff --git a/llvm/utils/TableGen/CodeEmitterGen.cpp b/llvm/utils/TableGen/CodeEmitterGen.cpp index 3ab6f40a6dee..d7b5e21c3f1f 100644 --- a/llvm/utils/TableGen/CodeEmitterGen.cpp +++ b/llvm/utils/TableGen/CodeEmitterGen.cpp @@ -290,55 +290,52 @@ CodeEmitterGen::getInstructionCases(const Record *R, BitOffsetCase += S; }; - if (const RecordVal *RV = R->getValue("EncodingInfos")) { - if (const auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { - const CodeGenHwModes &HWM = Target.getHwModes(); - EncodingInfoByHwMode EBM(DI->getDef(), HWM); - - // Invoke the interface to obtain the HwMode ID controlling the - // EncodingInfo for the current subtarget. This interface will - // mask off irrelevant HwMode IDs. - Append(" unsigned HwMode = " - "STI.getHwMode(MCSubtargetInfo::HwMode_EncodingInfo);\n"); - Case += " switch (HwMode) {\n"; - Case += " default: llvm_unreachable(\"Unknown hardware mode!\"); " - "break;\n"; - for (auto &[ModeId, Encoding] : EBM) { - if (ModeId == DefaultMode) { - Case += - " case " + itostr(DefaultMode) + ": InstBitsByHw = InstBits"; - } else { - Case += " case " + itostr(ModeId) + - ": InstBitsByHw = InstBits_" + HWM.getMode(ModeId).Name.str(); - } - Case += "; break;\n"; - } - Case += " };\n"; - - // We need to remodify the 'Inst' value from the table we found above. - if (UseAPInt) { - int NumWords = APInt::getNumWords(BitWidth); - Case += " Inst = APInt(" + itostr(BitWidth); - Case += ", ArrayRef(InstBitsByHw + opcode * " + itostr(NumWords) + - ", " + itostr(NumWords); - Case += "));\n"; - Case += " Value = Inst;\n"; + if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) { + const CodeGenHwModes &HWM = Target.getHwModes(); + EncodingInfoByHwMode EBM(RV, HWM); + + // Invoke the interface to obtain the HwMode ID controlling the + // EncodingInfo for the current subtarget. This interface will + // mask off irrelevant HwMode IDs. + Append(" unsigned HwMode = " + "STI.getHwMode(MCSubtargetInfo::HwMode_EncodingInfo);\n"); + Case += " switch (HwMode) {\n"; + Case += " default: llvm_unreachable(\"Unknown hardware mode!\"); " + "break;\n"; + for (auto &[ModeId, Encoding] : EBM) { + if (ModeId == DefaultMode) { + Case += + " case " + itostr(DefaultMode) + ": InstBitsByHw = InstBits"; } else { - Case += " Value = InstBitsByHw[opcode];\n"; + Case += " case " + itostr(ModeId) + ": InstBitsByHw = InstBits_" + + HWM.getMode(ModeId).Name.str(); } + Case += "; break;\n"; + } + Case += " };\n"; - Append(" switch (HwMode) {\n"); - Append(" default: llvm_unreachable(\"Unhandled HwMode\");\n"); - for (auto &[ModeId, Encoding] : EBM) { - Append(" case " + itostr(ModeId) + ": {\n"); - addInstructionCasesForEncoding(R, Encoding, Target, Case, - BitOffsetCase); - Append(" break;\n"); - Append(" }\n"); - } + // We need to remodify the 'Inst' value from the table we found above. + if (UseAPInt) { + int NumWords = APInt::getNumWords(BitWidth); + Case += " Inst = APInt(" + itostr(BitWidth); + Case += ", ArrayRef(InstBitsByHw + opcode * " + itostr(NumWords) + ", " + + itostr(NumWords); + Case += "));\n"; + Case += " Value = Inst;\n"; + } else { + Case += " Value = InstBitsByHw[opcode];\n"; + } + + Append(" switch (HwMode) {\n"); + Append(" default: llvm_unreachable(\"Unhandled HwMode\");\n"); + for (auto &[ModeId, Encoding] : EBM) { + Append(" case " + itostr(ModeId) + ": {\n"); + addInstructionCasesForEncoding(R, Encoding, Target, Case, BitOffsetCase); + Append(" break;\n"); Append(" }\n"); - return {std::move(Case), std::move(BitOffsetCase)}; } + Append(" }\n"); + return {std::move(Case), std::move(BitOffsetCase)}; } addInstructionCasesForEncoding(R, R, Target, Case, BitOffsetCase); return {std::move(Case), std::move(BitOffsetCase)}; @@ -417,20 +414,18 @@ void CodeEmitterGen::emitInstructionBaseValues( } const Record *EncodingDef = R; - if (const RecordVal *RV = R->getValue("EncodingInfos")) { - if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { - EncodingInfoByHwMode EBM(DI->getDef(), HWM); - if (EBM.hasMode(HwMode)) { - EncodingDef = EBM.get(HwMode); - } else { - // If the HwMode does not match, then Encoding '0' - // should be generated. - APInt Value(BitWidth, 0); - O << " "; - emitInstBits(O, Value); - O << "," << '\t' << "// " << R->getName() << "\n"; - continue; - } + if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) { + EncodingInfoByHwMode EBM(RV, HWM); + if (EBM.hasMode(HwMode)) { + EncodingDef = EBM.get(HwMode); + } else { + // If the HwMode does not match, then Encoding '0' + // should be generated. + APInt Value(BitWidth, 0); + O << " "; + emitInstBits(O, Value); + O << "," << '\t' << "// " << R->getName() << "\n"; + continue; } } const BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst"); @@ -490,16 +485,14 @@ void CodeEmitterGen::run(raw_ostream &O) { R->getValueAsBit("isPseudo")) continue; - if (const RecordVal *RV = R->getValue("EncodingInfos")) { - if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { - EncodingInfoByHwMode EBM(DI->getDef(), HWM); - for (const auto &[Key, Value] : EBM) { - const BitsInit *BI = Value->getValueAsBitsInit("Inst"); - BitWidth = std::max(BitWidth, BI->getNumBits()); - HwModes.insert(Key); - } - continue; + if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) { + EncodingInfoByHwMode EBM(RV, HWM); + for (const auto &[Key, Value] : EBM) { + const BitsInit *BI = Value->getValueAsBitsInit("Inst"); + BitWidth = std::max(BitWidth, BI->getNumBits()); + HwModes.insert(Key); } + continue; } const BitsInit *BI = R->getValueAsBitsInit("Inst"); BitWidth = std::max(BitWidth, BI->getNumBits()); |
