Skip to content

Commit 2f9f92a

Browse files
authored
[TableGen] Use getValueAsOptionalDef to simplify code (NFC) (#153170)
1 parent d8ce19a commit 2f9f92a

File tree

4 files changed

+83
-96
lines changed

4 files changed

+83
-96
lines changed

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -290,55 +290,52 @@ CodeEmitterGen::getInstructionCases(const Record *R,
290290
BitOffsetCase += S;
291291
};
292292

293-
if (const RecordVal *RV = R->getValue("EncodingInfos")) {
294-
if (const auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
295-
const CodeGenHwModes &HWM = Target.getHwModes();
296-
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
297-
298-
// Invoke the interface to obtain the HwMode ID controlling the
299-
// EncodingInfo for the current subtarget. This interface will
300-
// mask off irrelevant HwMode IDs.
301-
Append(" unsigned HwMode = "
302-
"STI.getHwMode(MCSubtargetInfo::HwMode_EncodingInfo);\n");
303-
Case += " switch (HwMode) {\n";
304-
Case += " default: llvm_unreachable(\"Unknown hardware mode!\"); "
305-
"break;\n";
306-
for (auto &[ModeId, Encoding] : EBM) {
307-
if (ModeId == DefaultMode) {
308-
Case +=
309-
" case " + itostr(DefaultMode) + ": InstBitsByHw = InstBits";
310-
} else {
311-
Case += " case " + itostr(ModeId) +
312-
": InstBitsByHw = InstBits_" + HWM.getMode(ModeId).Name.str();
313-
}
314-
Case += "; break;\n";
315-
}
316-
Case += " };\n";
317-
318-
// We need to remodify the 'Inst' value from the table we found above.
319-
if (UseAPInt) {
320-
int NumWords = APInt::getNumWords(BitWidth);
321-
Case += " Inst = APInt(" + itostr(BitWidth);
322-
Case += ", ArrayRef(InstBitsByHw + opcode * " + itostr(NumWords) +
323-
", " + itostr(NumWords);
324-
Case += "));\n";
325-
Case += " Value = Inst;\n";
293+
if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) {
294+
const CodeGenHwModes &HWM = Target.getHwModes();
295+
EncodingInfoByHwMode EBM(RV, HWM);
296+
297+
// Invoke the interface to obtain the HwMode ID controlling the
298+
// EncodingInfo for the current subtarget. This interface will
299+
// mask off irrelevant HwMode IDs.
300+
Append(" unsigned HwMode = "
301+
"STI.getHwMode(MCSubtargetInfo::HwMode_EncodingInfo);\n");
302+
Case += " switch (HwMode) {\n";
303+
Case += " default: llvm_unreachable(\"Unknown hardware mode!\"); "
304+
"break;\n";
305+
for (auto &[ModeId, Encoding] : EBM) {
306+
if (ModeId == DefaultMode) {
307+
Case +=
308+
" case " + itostr(DefaultMode) + ": InstBitsByHw = InstBits";
326309
} else {
327-
Case += " Value = InstBitsByHw[opcode];\n";
310+
Case += " case " + itostr(ModeId) + ": InstBitsByHw = InstBits_" +
311+
HWM.getMode(ModeId).Name.str();
328312
}
313+
Case += "; break;\n";
314+
}
315+
Case += " };\n";
329316

330-
Append(" switch (HwMode) {\n");
331-
Append(" default: llvm_unreachable(\"Unhandled HwMode\");\n");
332-
for (auto &[ModeId, Encoding] : EBM) {
333-
Append(" case " + itostr(ModeId) + ": {\n");
334-
addInstructionCasesForEncoding(R, Encoding, Target, Case,
335-
BitOffsetCase);
336-
Append(" break;\n");
337-
Append(" }\n");
338-
}
317+
// We need to remodify the 'Inst' value from the table we found above.
318+
if (UseAPInt) {
319+
int NumWords = APInt::getNumWords(BitWidth);
320+
Case += " Inst = APInt(" + itostr(BitWidth);
321+
Case += ", ArrayRef(InstBitsByHw + opcode * " + itostr(NumWords) + ", " +
322+
itostr(NumWords);
323+
Case += "));\n";
324+
Case += " Value = Inst;\n";
325+
} else {
326+
Case += " Value = InstBitsByHw[opcode];\n";
327+
}
328+
329+
Append(" switch (HwMode) {\n");
330+
Append(" default: llvm_unreachable(\"Unhandled HwMode\");\n");
331+
for (auto &[ModeId, Encoding] : EBM) {
332+
Append(" case " + itostr(ModeId) + ": {\n");
333+
addInstructionCasesForEncoding(R, Encoding, Target, Case, BitOffsetCase);
334+
Append(" break;\n");
339335
Append(" }\n");
340-
return {std::move(Case), std::move(BitOffsetCase)};
341336
}
337+
Append(" }\n");
338+
return {std::move(Case), std::move(BitOffsetCase)};
342339
}
343340
addInstructionCasesForEncoding(R, R, Target, Case, BitOffsetCase);
344341
return {std::move(Case), std::move(BitOffsetCase)};
@@ -417,20 +414,18 @@ void CodeEmitterGen::emitInstructionBaseValues(
417414
}
418415

419416
const Record *EncodingDef = R;
420-
if (const RecordVal *RV = R->getValue("EncodingInfos")) {
421-
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
422-
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
423-
if (EBM.hasMode(HwMode)) {
424-
EncodingDef = EBM.get(HwMode);
425-
} else {
426-
// If the HwMode does not match, then Encoding '0'
427-
// should be generated.
428-
APInt Value(BitWidth, 0);
429-
O << " ";
430-
emitInstBits(O, Value);
431-
O << "," << '\t' << "// " << R->getName() << "\n";
432-
continue;
433-
}
417+
if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) {
418+
EncodingInfoByHwMode EBM(RV, HWM);
419+
if (EBM.hasMode(HwMode)) {
420+
EncodingDef = EBM.get(HwMode);
421+
} else {
422+
// If the HwMode does not match, then Encoding '0'
423+
// should be generated.
424+
APInt Value(BitWidth, 0);
425+
O << " ";
426+
emitInstBits(O, Value);
427+
O << "," << '\t' << "// " << R->getName() << "\n";
428+
continue;
434429
}
435430
}
436431
const BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst");
@@ -490,16 +485,14 @@ void CodeEmitterGen::run(raw_ostream &O) {
490485
R->getValueAsBit("isPseudo"))
491486
continue;
492487

493-
if (const RecordVal *RV = R->getValue("EncodingInfos")) {
494-
if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
495-
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
496-
for (const auto &[Key, Value] : EBM) {
497-
const BitsInit *BI = Value->getValueAsBitsInit("Inst");
498-
BitWidth = std::max(BitWidth, BI->getNumBits());
499-
HwModes.insert(Key);
500-
}
501-
continue;
488+
if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) {
489+
EncodingInfoByHwMode EBM(RV, HWM);
490+
for (const auto &[Key, Value] : EBM) {
491+
const BitsInit *BI = Value->getValueAsBitsInit("Inst");
492+
BitWidth = std::max(BitWidth, BI->getNumBits());
493+
HwModes.insert(Key);
502494
}
495+
continue;
503496
}
504497
const BitsInit *BI = R->getValueAsBitsInit("Inst");
505498
BitWidth = std::max(BitWidth, BI->getNumBits());

llvm/utils/TableGen/Common/CodeGenRegisters.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ CodeGenSubRegIndex::CodeGenSubRegIndex(const Record *R, unsigned Enum,
5757
if (R->getValue("Namespace"))
5858
Namespace = R->getValueAsString("Namespace").str();
5959

60-
if (const RecordVal *RV = R->getValue("SubRegRanges"))
61-
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
62-
Range = SubRegRangeByHwMode(DI->getDef(), CGH);
60+
if (const Record *RV = R->getValueAsOptionalDef("SubRegRanges"))
61+
Range = SubRegRangeByHwMode(RV, CGH);
6362
if (!Range.hasDefault())
6463
Range.insertSubRegRangeForMode(DefaultMode, SubRegRange(R));
6564
}
@@ -732,9 +731,8 @@ CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
732731

733732
Namespace = R->getValueAsString("Namespace");
734733

735-
if (const RecordVal *RV = R->getValue("RegInfos"))
736-
if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
737-
RSI = RegSizeInfoByHwMode(DI->getDef(), RegBank.getHwModes());
734+
if (const Record *RV = R->getValueAsOptionalDef("RegInfos"))
735+
RSI = RegSizeInfoByHwMode(RV, RegBank.getHwModes());
738736
unsigned Size = R->getValueAsInt("Size");
739737
assert((RSI.hasDefault() || Size != 0 || VTs[0].isSimple()) &&
740738
"Impossible to determine register size");

llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,16 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
236236
continue;
237237

238238
// Setup alternative encodings according to HwModes
239-
if (const RecordVal *RV = R->getValue("EncodingInfos")) {
240-
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
241-
const CodeGenHwModes &HWM = Target.getHwModes();
242-
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
243-
for (const auto [Mode, EncodingDef] : EBM) {
244-
Modes.try_emplace(Mode, "_" + HWM.getMode(Mode).Name.str());
245-
const RecordVal *RV = EncodingDef->getValue("Inst");
246-
const DagInit *DI = cast<DagInit>(RV->getValue());
247-
VarLenInsts[R].try_emplace(Mode, VarLenInst(DI, RV));
248-
}
249-
continue;
239+
if (const Record *RV = R->getValueAsOptionalDef("EncodingInfos")) {
240+
const CodeGenHwModes &HWM = Target.getHwModes();
241+
EncodingInfoByHwMode EBM(RV, HWM);
242+
for (const auto [Mode, EncodingDef] : EBM) {
243+
Modes.try_emplace(Mode, "_" + HWM.getMode(Mode).Name.str());
244+
const RecordVal *RV = EncodingDef->getValue("Inst");
245+
const DagInit *DI = cast<DagInit>(RV->getValue());
246+
VarLenInsts[R].try_emplace(Mode, VarLenInst(DI, RV));
250247
}
248+
continue;
251249
}
252250
const RecordVal *RV = R->getValue("Inst");
253251
const DagInit *DI = cast<DagInit>(RV->getValue());

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,20 +2557,18 @@ namespace {
25572557
NumberedEncodings.reserve(NumberedInstructions.size());
25582558
for (const auto &NumberedInstruction : NumberedInstructions) {
25592559
const Record *InstDef = NumberedInstruction->TheDef;
2560-
if (const RecordVal *RV = InstDef->getValue("EncodingInfos")) {
2561-
if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
2562-
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
2563-
for (auto &[ModeId, Encoding] : EBM) {
2564-
// DecoderTables with DefaultMode should not have any suffix.
2565-
if (ModeId == DefaultMode) {
2566-
NumberedEncodings.emplace_back(Encoding, NumberedInstruction, "");
2567-
} else {
2568-
NumberedEncodings.emplace_back(Encoding, NumberedInstruction,
2569-
HWM.getMode(ModeId).Name);
2570-
}
2560+
if (const Record *RV = InstDef->getValueAsOptionalDef("EncodingInfos")) {
2561+
EncodingInfoByHwMode EBM(RV, HWM);
2562+
for (auto &[ModeId, Encoding] : EBM) {
2563+
// DecoderTables with DefaultMode should not have any suffix.
2564+
if (ModeId == DefaultMode) {
2565+
NumberedEncodings.emplace_back(Encoding, NumberedInstruction, "");
2566+
} else {
2567+
NumberedEncodings.emplace_back(Encoding, NumberedInstruction,
2568+
HWM.getMode(ModeId).Name);
25712569
}
2572-
continue;
25732570
}
2571+
continue;
25742572
}
25752573
// This instruction is encoded the same on all HwModes.
25762574
// According to user needs, provide varying degrees of suppression.

0 commit comments

Comments
 (0)