Skip to content

Commit 9b8f534

Browse files
authored
Improve readability of <Target>GenCompressionInstEmitter. NFC (#134834)
Use indent() instead of manually indenting the code in the CompressInstEmitter.cpp. Also modify the current indentation in a few places.
1 parent 154d360 commit 9b8f534

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

llvm/utils/TableGen/CompressInstEmitter.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -573,17 +573,18 @@ static void printPredicates(ArrayRef<const Record *> Predicates, StringRef Name,
573573
raw_ostream &OS) {
574574
for (unsigned I = 0; I < Predicates.size(); ++I) {
575575
StringRef Pred = Predicates[I]->getValueAsString(Name);
576-
OS << " case " << I + 1 << ": {\n"
577-
<< " // " << Predicates[I]->getName() << "\n"
578-
<< " " << Pred << "\n"
579-
<< " }\n";
576+
Pred = Pred.trim();
577+
OS.indent(2) << "case " << I + 1 << ": {\n";
578+
OS.indent(4) << "// " << Predicates[I]->getName() << "\n";
579+
OS.indent(4) << Pred << "\n";
580+
OS.indent(2) << "}\n";
580581
}
581582
}
582583

583584
static void mergeCondAndCode(raw_ostream &CombinedStream, StringRef CondStr,
584585
StringRef CodeStr) {
585586
// Remove first indentation and last '&&'.
586-
CondStr = CondStr.drop_front(6).drop_back(4);
587+
CondStr = CondStr.drop_front(8).drop_back(4);
587588
CombinedStream.indent(4) << "if (" << CondStr << ") {\n";
588589
CombinedStream << CodeStr;
589590
CombinedStream.indent(4) << " return true;\n";
@@ -721,14 +722,14 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
721722
// Emit checks for all required features.
722723
for (auto &Op : FeaturesSet) {
723724
StringRef Not = Op.first ? "!" : "";
724-
CondStream.indent(6) << Not << "STI.getFeatureBits()[" << TargetName
725+
CondStream.indent(8) << Not << "STI.getFeatureBits()[" << TargetName
725726
<< "::" << Op.second << "]"
726727
<< " &&\n";
727728
}
728729

729730
// Emit checks for all required feature groups.
730731
for (auto &Set : AnyOfFeatureSets) {
731-
CondStream.indent(6) << "(";
732+
CondStream.indent(8) << "(";
732733
for (auto &Op : Set) {
733734
bool IsLast = &Op == &*Set.rbegin();
734735
StringRef Not = Op.first ? "!" : "";
@@ -745,10 +746,10 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
745746
for (const auto &SourceOperand : Source.Operands) {
746747
if (SourceOperandMap[OpNo].TiedOpIdx != -1) {
747748
if (Source.Operands[OpNo].Rec->isSubClassOf("RegisterClass"))
748-
CondStream.indent(6)
749+
CondStream.indent(8)
749750
<< "(MI.getOperand(" << OpNo << ").isReg()) && (MI.getOperand("
750751
<< SourceOperandMap[OpNo].TiedOpIdx << ").isReg()) &&\n"
751-
<< " (MI.getOperand(" << OpNo
752+
<< indent(8) << "(MI.getOperand(" << OpNo
752753
<< ").getReg() == MI.getOperand("
753754
<< SourceOperandMap[OpNo].TiedOpIdx << ").getReg()) &&\n";
754755
else
@@ -761,16 +762,16 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
761762
// We don't need to do anything for source instruction operand checks.
762763
break;
763764
case OpData::Imm:
764-
CondStream.indent(6)
765+
CondStream.indent(8)
765766
<< "(MI.getOperand(" << OpNo << ").isImm()) &&\n"
766767
<< " (MI.getOperand(" << OpNo
767768
<< ").getImm() == " << SourceOperandMap[OpNo].Data.Imm
768769
<< ") &&\n";
769770
break;
770771
case OpData::Reg: {
771772
const Record *Reg = SourceOperandMap[OpNo].Data.Reg;
772-
CondStream.indent(6) << "(MI.getOperand(" << OpNo << ").isReg()) &&\n"
773-
<< " (MI.getOperand(" << OpNo
773+
CondStream.indent(8) << "(MI.getOperand(" << OpNo << ").isReg()) &&\n"
774+
<< indent(8) << "(MI.getOperand(" << OpNo
774775
<< ").getReg() == " << TargetName
775776
<< "::" << Reg->getName() << ") &&\n";
776777
break;
@@ -808,12 +809,12 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
808809
// Don't check register class if this is a tied operand, it was done
809810
// for the operand its tied to.
810811
if (DestOperand.getTiedRegister() == -1) {
811-
CondStream.indent(6) << "MI.getOperand(" << OpIdx << ").isReg()";
812+
CondStream.indent(8) << "MI.getOperand(" << OpIdx << ").isReg()";
812813
if (EType == EmitterType::CheckCompress)
813814
CondStream << " && MI.getOperand(" << OpIdx
814815
<< ").getReg().isPhysical()";
815816
CondStream << " &&\n"
816-
<< indent(6) << TargetName << "MCRegisterClasses["
817+
<< indent(8) << TargetName << "MCRegisterClasses["
817818
<< TargetName << "::" << ClassRec->getName()
818819
<< "RegClassID].contains(MI.getOperand(" << OpIdx
819820
<< ").getReg()) &&\n";
@@ -827,16 +828,16 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
827828
if (CompressOrUncompress) {
828829
unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates,
829830
DestRec, "MCOperandPredicate");
830-
CondStream.indent(6) << ValidatorName << "("
831+
CondStream.indent(8) << ValidatorName << "("
831832
<< "MI.getOperand(" << OpIdx << "), STI, "
832833
<< Entry << ") &&\n";
833834
} else {
834835
unsigned Entry =
835836
getPredicates(ImmLeafPredicateMap, ImmLeafPredicates, DestRec,
836837
"ImmediateCode");
837-
CondStream.indent(6)
838+
CondStream.indent(8)
838839
<< "MI.getOperand(" << OpIdx << ").isImm() &&\n";
839-
CondStream.indent(6) << TargetName << "ValidateMachineOperand("
840+
CondStream.indent(8) << TargetName << "ValidateMachineOperand("
840841
<< "MI.getOperand(" << OpIdx << "), &STI, "
841842
<< Entry << ") &&\n";
842843
}
@@ -850,15 +851,15 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
850851
if (CompressOrUncompress) {
851852
unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates,
852853
DestRec, "MCOperandPredicate");
853-
CondStream.indent(6)
854+
CondStream.indent(8)
854855
<< ValidatorName << "("
855856
<< "MCOperand::createImm(" << DestOperandMap[OpNo].Data.Imm
856857
<< "), STI, " << Entry << ") &&\n";
857858
} else {
858859
unsigned Entry =
859860
getPredicates(ImmLeafPredicateMap, ImmLeafPredicates, DestRec,
860861
"ImmediateCode");
861-
CondStream.indent(6)
862+
CondStream.indent(8)
862863
<< TargetName
863864
<< "ValidateMachineOperand(MachineOperand::CreateImm("
864865
<< DestOperandMap[OpNo].Data.Imm << "), &STI, " << Entry
@@ -893,10 +894,11 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
893894
Func.indent(2) << "return false;\n}\n";
894895

895896
if (!MCOpPredicates.empty()) {
896-
OS << "static bool " << ValidatorName << "(const MCOperand &MCOp,\n"
897-
<< " const MCSubtargetInfo &STI,\n"
898-
<< " unsigned PredicateIndex) {\n"
899-
<< " switch (PredicateIndex) {\n"
897+
auto IndentLength = ValidatorName.size() + 13;
898+
OS << "static bool " << ValidatorName << "(const MCOperand &MCOp,\n";
899+
OS.indent(IndentLength) << "const MCSubtargetInfo &STI,\n";
900+
OS.indent(IndentLength) << "unsigned PredicateIndex) {\n";
901+
OS << " switch (PredicateIndex) {\n"
900902
<< " default:\n"
901903
<< " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
902904
<< " break;\n";
@@ -908,15 +910,18 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
908910
}
909911

910912
if (!ImmLeafPredicates.empty()) {
913+
auto IndentLength = TargetName.size() + 35;
911914
OS << "static bool " << TargetName
912-
<< "ValidateMachineOperand(const MachineOperand &MO,\n"
913-
<< " const " << TargetName << "Subtarget *Subtarget,\n"
914-
<< " unsigned PredicateIndex) {\n"
915-
<< " int64_t Imm = MO.getImm();\n"
916-
<< " switch (PredicateIndex) {\n"
917-
<< " default:\n"
918-
<< " llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n"
919-
<< " break;\n";
915+
<< "ValidateMachineOperand(const MachineOperand &MO,\n";
916+
OS.indent(IndentLength)
917+
<< "const " << TargetName << "Subtarget *Subtarget,\n";
918+
OS.indent(IndentLength)
919+
<< "unsigned PredicateIndex) {\n"
920+
<< " int64_t Imm = MO.getImm();\n"
921+
<< " switch (PredicateIndex) {\n"
922+
<< " default:\n"
923+
<< " llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n"
924+
<< " break;\n";
920925

921926
printPredicates(ImmLeafPredicates, "ImmediateCode", OS);
922927

0 commit comments

Comments
 (0)