[llvm] 2f9f92a - [TableGen] Use getValueAsOptionalDef to simplify code (NFC) (#153170)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 12 07:44:05 PDT 2025
Author: Sergei Barannikov
Date: 2025-08-12T17:44:01+03:00
New Revision: 2f9f92ad01c06c7b02d15a730cd609db95705882
URL: https://github.com/llvm/llvm-project/commit/2f9f92ad01c06c7b02d15a730cd609db95705882
DIFF: https://github.com/llvm/llvm-project/commit/2f9f92ad01c06c7b02d15a730cd609db95705882.diff
LOG: [TableGen] Use getValueAsOptionalDef to simplify code (NFC) (#153170)
Added:
Modified:
llvm/utils/TableGen/CodeEmitterGen.cpp
llvm/utils/TableGen/Common/CodeGenRegisters.cpp
llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
llvm/utils/TableGen/DecoderEmitter.cpp
Removed:
################################################################################
diff --git a/llvm/utils/TableGen/CodeEmitterGen.cpp b/llvm/utils/TableGen/CodeEmitterGen.cpp
index 3ab6f40a6deec..d7b5e21c3f1fb 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());
diff --git a/llvm/utils/TableGen/Common/CodeGenRegisters.cpp b/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
index f78427940b276..e873b3eaa4b7e 100644
--- a/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
@@ -57,9 +57,8 @@ CodeGenSubRegIndex::CodeGenSubRegIndex(const Record *R, unsigned Enum,
if (R->getValue("Namespace"))
Namespace = R->getValueAsString("Namespace").str();
- if (const RecordVal *RV = R->getValue("SubRegRanges"))
- if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
- Range = SubRegRangeByHwMode(DI->getDef(), CGH);
+ if (const Record *RV = R->getValueAsOptionalDef("SubRegRanges"))
+ Range = SubRegRangeByHwMode(RV, CGH);
if (!Range.hasDefault())
Range.insertSubRegRangeForMode(DefaultMode, SubRegRange(R));
}
@@ -732,9 +731,8 @@ CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
Namespace = R->getValueAsString("Namespace");
- if (const RecordVal *RV = R->getValue("RegInfos"))
- if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
- RSI = RegSizeInfoByHwMode(DI->getDef(), RegBank.getHwModes());
+ if (const Record *RV = R->getValueAsOptionalDef("RegInfos"))
+ RSI = RegSizeInfoByHwMode(RV, RegBank.getHwModes());
unsigned Size = R->getValueAsInt("Size");
assert((RSI.hasDefault() || Size != 0 || VTs[0].isSimple()) &&
"Impossible to determine register size");
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
index 77a2ae60ad6f9..6201d101cd46c 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
@@ -236,18 +236,16 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
continue;
// Setup alternative encodings according to HwModes
- if (const RecordVal *RV = R->getValue("EncodingInfos")) {
- if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
- const CodeGenHwModes &HWM = Target.getHwModes();
- EncodingInfoByHwMode EBM(DI->getDef(), HWM);
- for (const auto [Mode, EncodingDef] : EBM) {
- Modes.try_emplace(Mode, "_" + HWM.getMode(Mode).Name.str());
- const RecordVal *RV = EncodingDef->getValue("Inst");
- const DagInit *DI = cast<DagInit>(RV->getValue());
- VarLenInsts[R].try_emplace(Mode, VarLenInst(DI, RV));
- }
- continue;
+ if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) {
+ const CodeGenHwModes &HWM = Target.getHwModes();
+ EncodingInfoByHwMode EBM(RV, HWM);
+ for (const auto [Mode, EncodingDef] : EBM) {
+ Modes.try_emplace(Mode, "_" + HWM.getMode(Mode).Name.str());
+ const RecordVal *RV = EncodingDef->getValue("Inst");
+ const DagInit *DI = cast<DagInit>(RV->getValue());
+ VarLenInsts[R].try_emplace(Mode, VarLenInst(DI, RV));
}
+ continue;
}
const RecordVal *RV = R->getValue("Inst");
const DagInit *DI = cast<DagInit>(RV->getValue());
diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 21c611e364a8d..9cc26c3cb3cae 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -2557,20 +2557,18 @@ namespace {
NumberedEncodings.reserve(NumberedInstructions.size());
for (const auto &NumberedInstruction : NumberedInstructions) {
const Record *InstDef = NumberedInstruction->TheDef;
- if (const RecordVal *RV = InstDef->getValue("EncodingInfos")) {
- if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
- EncodingInfoByHwMode EBM(DI->getDef(), HWM);
- for (auto &[ModeId, Encoding] : EBM) {
- // DecoderTables with DefaultMode should not have any suffix.
- if (ModeId == DefaultMode) {
- NumberedEncodings.emplace_back(Encoding, NumberedInstruction, "");
- } else {
- NumberedEncodings.emplace_back(Encoding, NumberedInstruction,
- HWM.getMode(ModeId).Name);
- }
+ if (const Record *RV = InstDef->getValueAsOptionalDef("EncodingInfos")) {
+ EncodingInfoByHwMode EBM(RV, HWM);
+ for (auto &[ModeId, Encoding] : EBM) {
+ // DecoderTables with DefaultMode should not have any suffix.
+ if (ModeId == DefaultMode) {
+ NumberedEncodings.emplace_back(Encoding, NumberedInstruction, "");
+ } else {
+ NumberedEncodings.emplace_back(Encoding, NumberedInstruction,
+ HWM.getMode(ModeId).Name);
}
- continue;
}
+ continue;
}
// This instruction is encoded the same on all HwModes.
// According to user needs, provide varying degrees of suppression.
More information about the llvm-commits
mailing list