[llvm] [NFC][TableGen] Adopt scaled indent in PredicateExpander (PR #109801)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 24 16:53:20 PDT 2024
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/109801
>From d8e494846c1afd4669ef286fcf17f617ba3c1cb6 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 23 Sep 2024 20:42:17 -0700
Subject: [PATCH] [NFC][TableGen] Adopt scaled indent in PredicateExpander
---
llvm/include/llvm/Support/raw_ostream.h | 24 ++++
llvm/unittests/Support/raw_ostream_test.cpp | 20 ++++
.../TableGen/Common/PredicateExpander.cpp | 107 +++++++-----------
.../utils/TableGen/Common/PredicateExpander.h | 19 ++--
llvm/utils/TableGen/InstrInfoEmitter.cpp | 4 +-
.../TableGen/MacroFusionPredicatorEmitter.cpp | 4 +-
llvm/utils/TableGen/SubtargetEmitter.cpp | 20 ++--
7 files changed, 106 insertions(+), 92 deletions(-)
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index c2f2299ed96455..d3b411590e7fd7 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -797,6 +797,30 @@ struct indent {
assert(NumIndents >= N && "Indentation undeflow");
return indent(NumIndents - N, Scale);
}
+ indent &operator++() { // Prefix ++.
+ ++NumIndents;
+ return *this;
+ }
+ indent operator++(int) { // Postfix ++.
+ indent Old = *this;
+ ++NumIndents;
+ return Old;
+ }
+ indent &operator--() { // Prefix --.
+ assert(NumIndents >= 1);
+ --NumIndents;
+ return *this;
+ }
+ indent operator--(int) { // Postfix --.
+ indent Old = *this;
+ assert(NumIndents >= 1);
+ --NumIndents;
+ return Old;
+ }
+ indent &operator=(unsigned N) {
+ NumIndents = N;
+ return *this;
+ }
};
inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {
diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index a35edd61685296..fbeff37d26a354 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -198,6 +198,26 @@ TEST(raw_ostreamTest, Indent) {
EXPECT_EQ(Spaces(10), printToString(Scaled));
Scaled -= 1;
EXPECT_EQ(Spaces(8), printToString(Scaled));
+
+ // Operators.
+ Indent = 10;
+ EXPECT_EQ(Spaces(10), printToString(Indent));
+
+ indent Temp = Indent++;
+ EXPECT_EQ(Spaces(11), printToString(Indent));
+ EXPECT_EQ(Spaces(10), printToString(Temp));
+
+ Temp = Indent--;
+ EXPECT_EQ(Spaces(10), printToString(Indent));
+ EXPECT_EQ(Spaces(11), printToString(Temp));
+
+ Temp = ++Indent;
+ EXPECT_EQ(Spaces(11), printToString(Indent));
+ EXPECT_EQ(Spaces(11), printToString(Temp));
+
+ Temp = --Indent;
+ EXPECT_EQ(Spaces(10), printToString(Indent));
+ EXPECT_EQ(Spaces(10), printToString(Temp));
}
TEST(raw_ostreamTest, FormatHex) {
diff --git a/llvm/utils/TableGen/Common/PredicateExpander.cpp b/llvm/utils/TableGen/Common/PredicateExpander.cpp
index 2afaa8cc21aa66..314e563ba90bb4 100644
--- a/llvm/utils/TableGen/Common/PredicateExpander.cpp
+++ b/llvm/utils/TableGen/Common/PredicateExpander.cpp
@@ -153,10 +153,9 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
}
OS << '(';
- increaseIndentLevel();
+ ++Indent;
for (const Record *Rec : Opcodes) {
- OS << '\n';
- OS.indent(getIndentLevel() * 2);
+ OS << '\n' << Indent;
if (!First)
OS << (shouldNegate() ? "&& " : "|| ");
@@ -164,10 +163,8 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
First = false;
}
- OS << '\n';
- decreaseIndentLevel();
- OS.indent(getIndentLevel() * 2);
- OS << ')';
+ --Indent;
+ OS << '\n' << Indent << ')';
}
void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
@@ -187,22 +184,19 @@ void PredicateExpander::expandPredicateSequence(
// Okay, there is more than one predicate in the set.
bool First = true;
OS << (shouldNegate() ? "!(" : "(");
- increaseIndentLevel();
+ ++Indent;
bool OldValue = shouldNegate();
setNegatePredicate(false);
for (const Record *Rec : Sequence) {
- OS << '\n';
- OS.indent(getIndentLevel() * 2);
+ OS << '\n' << Indent;
if (!First)
OS << (IsCheckAll ? "&& " : "|| ");
expandPredicate(OS, Rec);
First = false;
}
- OS << '\n';
- decreaseIndentLevel();
- OS.indent(getIndentLevel() * 2);
- OS << ')';
+ --Indent;
+ OS << '\n' << Indent << ')';
setNegatePredicate(OldValue);
}
@@ -269,15 +263,14 @@ void PredicateExpander::expandReturnStatement(raw_ostream &OS,
void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
const Record *Rec) {
for (const Record *Opcode : Rec->getValueAsListOfDefs("Opcodes")) {
- OS.indent(getIndentLevel() * 2);
- OS << "case " << Opcode->getValueAsString("Namespace")
+ OS << Indent << "case " << Opcode->getValueAsString("Namespace")
<< "::" << Opcode->getName() << ":\n";
}
- increaseIndentLevel();
- OS.indent(getIndentLevel() * 2);
+ ++Indent;
+ OS << Indent;
expandStatement(OS, Rec->getValueAsDef("CaseStmt"));
- decreaseIndentLevel();
+ --Indent;
}
void PredicateExpander::expandOpcodeSwitchStatement(
@@ -292,17 +285,12 @@ void PredicateExpander::expandOpcodeSwitchStatement(
}
// Expand the default case.
- SS.indent(getIndentLevel() * 2);
- SS << "default:\n";
+ SS << Indent << "default:\n";
- increaseIndentLevel();
- SS.indent(getIndentLevel() * 2);
+ ++Indent;
+ SS << Indent;
expandStatement(SS, Default);
- decreaseIndentLevel();
- SS << '\n';
-
- SS.indent(getIndentLevel() * 2);
- SS << "} // end of switch-stmt";
+ SS << '\n' << Indent << "} // end of switch-stmt";
OS << Buffer;
}
@@ -436,8 +424,7 @@ void STIPredicateExpander::expandHeader(raw_ostream &OS,
const Record *Rec = Fn.getDeclaration();
StringRef FunctionName = Rec->getValueAsString("Name");
- OS.indent(getIndentLevel() * 2);
- OS << "bool ";
+ OS << Indent << "bool ";
if (shouldExpandDefinition())
OS << getClassPrefix() << "::";
OS << FunctionName << "(";
@@ -463,26 +450,22 @@ void STIPredicateExpander::expandPrologue(raw_ostream &OS,
bool UpdatesOpcodeMask =
Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
- increaseIndentLevel();
- unsigned IndentLevel = getIndentLevel();
+ ++Indent;
for (const Record *Delegate :
Fn.getDeclaration()->getValueAsListOfDefs("Delegates")) {
- OS.indent(IndentLevel * 2);
- OS << "if (" << Delegate->getValueAsString("Name") << "(MI";
+ OS << Indent << "if (" << Delegate->getValueAsString("Name") << "(MI";
if (UpdatesOpcodeMask)
OS << ", Mask";
if (shouldExpandForMC())
OS << ", ProcessorID";
OS << "))\n";
- OS.indent((1 + IndentLevel) * 2);
- OS << "return true;\n\n";
+ OS << Indent + 1 << "return true;\n\n";
}
if (shouldExpandForMC())
return;
- OS.indent(IndentLevel * 2);
- OS << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
+ OS << Indent << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
}
void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
@@ -497,8 +480,7 @@ void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
continue;
if (FirstProcID) {
- OS.indent(getIndentLevel() * 2);
- OS << "if (ProcessorID == " << I;
+ OS << Indent << "if (ProcessorID == " << I;
} else {
OS << " || ProcessorID == " << I;
}
@@ -507,21 +489,20 @@ void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
OS << ") {\n";
- increaseIndentLevel();
- OS.indent(getIndentLevel() * 2);
+ ++Indent;
+ OS << Indent;
if (ShouldUpdateOpcodeMask) {
if (PI.OperandMask.isZero())
OS << "Mask.clearAllBits();\n";
else
OS << "Mask = " << PI.OperandMask << ";\n";
- OS.indent(getIndentLevel() * 2);
+ OS << Indent;
}
OS << "return ";
expandPredicate(OS, PI.Predicate);
OS << ";\n";
- decreaseIndentLevel();
- OS.indent(getIndentLevel() * 2);
- OS << "}\n";
+ --Indent;
+ OS << Indent << "}\n";
}
}
@@ -530,46 +511,38 @@ void STIPredicateExpander::expandBody(raw_ostream &OS,
bool UpdatesOpcodeMask =
Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
- unsigned IndentLevel = getIndentLevel();
- OS.indent(IndentLevel * 2);
- OS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
- OS.indent(IndentLevel * 2);
- OS << "default:\n";
- OS.indent(IndentLevel * 2);
- OS << " break;";
+ OS << Indent << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
+ OS << Indent << "default:\n";
+ OS << Indent << " break;";
for (const OpcodeGroup &Group : Fn.getGroups()) {
for (const Record *Opcode : Group.getOpcodes()) {
- OS << '\n';
- OS.indent(IndentLevel * 2);
- OS << "case " << getTargetName() << "::" << Opcode->getName() << ":";
+ OS << '\n'
+ << Indent << "case " << getTargetName() << "::" << Opcode->getName()
+ << ":";
}
OS << '\n';
- increaseIndentLevel();
+ ++Indent;
expandOpcodeGroup(OS, Group, UpdatesOpcodeMask);
- OS.indent(getIndentLevel() * 2);
- OS << "break;\n";
- decreaseIndentLevel();
+ OS << Indent << "break;\n";
+ --Indent;
}
- OS.indent(IndentLevel * 2);
- OS << "}\n";
+ OS << Indent << "}\n";
}
void STIPredicateExpander::expandEpilogue(raw_ostream &OS,
const STIPredicateFunction &Fn) {
- OS << '\n';
- OS.indent(getIndentLevel() * 2);
+ OS << '\n' << Indent;
OS << "return ";
expandPredicate(OS, Fn.getDefaultReturnPredicate());
OS << ";\n";
- decreaseIndentLevel();
- OS.indent(getIndentLevel() * 2);
+ --Indent;
StringRef FunctionName = Fn.getDeclaration()->getValueAsString("Name");
- OS << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
+ OS << Indent << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
}
void STIPredicateExpander::expandSTIPredicate(raw_ostream &OS,
diff --git a/llvm/utils/TableGen/Common/PredicateExpander.h b/llvm/utils/TableGen/Common/PredicateExpander.h
index c0cd69e3cb1f85..0c3a8718a473f1 100644
--- a/llvm/utils/TableGen/Common/PredicateExpander.h
+++ b/llvm/utils/TableGen/Common/PredicateExpander.h
@@ -18,39 +18,38 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
namespace llvm {
-class raw_ostream;
class Record;
class PredicateExpander {
bool EmitCallsByRef;
bool NegatePredicate;
bool ExpandForMC;
- unsigned IndentLevel;
StringRef TargetName;
PredicateExpander(const PredicateExpander &) = delete;
PredicateExpander &operator=(const PredicateExpander &) = delete;
+protected:
+ indent Indent;
+
public:
- PredicateExpander(StringRef Target)
+ explicit PredicateExpander(StringRef Target, unsigned Indent = 1)
: EmitCallsByRef(true), NegatePredicate(false), ExpandForMC(false),
- IndentLevel(1U), TargetName(Target) {}
+ TargetName(Target), Indent(Indent, 2) {}
bool isByRef() const { return EmitCallsByRef; }
bool shouldNegate() const { return NegatePredicate; }
bool shouldExpandForMC() const { return ExpandForMC; }
- unsigned getIndentLevel() const { return IndentLevel; }
+ indent &getIndent() { return Indent; }
StringRef getTargetName() const { return TargetName; }
void setByRef(bool Value) { EmitCallsByRef = Value; }
void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
void setNegatePredicate(bool Value) { NegatePredicate = Value; }
void setExpandForMC(bool Value) { ExpandForMC = Value; }
- void setIndentLevel(unsigned Level) { IndentLevel = Level; }
- void increaseIndentLevel() { ++IndentLevel; }
- void decreaseIndentLevel() { --IndentLevel; }
void expandTrue(raw_ostream &OS);
void expandFalse(raw_ostream &OS);
@@ -116,8 +115,8 @@ class STIPredicateExpander : public PredicateExpander {
void expandEpilogue(raw_ostream &OS, const STIPredicateFunction &Fn);
public:
- STIPredicateExpander(StringRef Target)
- : PredicateExpander(Target), ExpandDefinition(false) {}
+ explicit STIPredicateExpander(StringRef Target, unsigned Indent = 1)
+ : PredicateExpander(Target, Indent), ExpandDefinition(false) {}
bool shouldExpandDefinition() const { return ExpandDefinition; }
StringRef getClassPrefix() const { return ClassPrefix; }
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index cc5ef49385bb86..46605095ba85f8 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -711,7 +711,7 @@ void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS,
OS << "bool " << Rec->getValueAsString("FunctionName");
OS << "(const MCInst &MI) {\n";
- OS.indent(PE.getIndentLevel() * 2);
+ OS << PE.getIndent();
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
OS << "\n}\n\n";
}
@@ -914,7 +914,7 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
}
OS << " {\n";
- OS.indent(PE.getIndentLevel() * 2);
+ OS << PE.getIndent();
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
OS << "\n}\n\n";
}
diff --git a/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp b/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp
index c4f238b67476a7..6ca2fea41230b8 100644
--- a/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp
+++ b/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp
@@ -160,7 +160,7 @@ void MacroFusionPredicatorEmitter::emitFirstPredicate(const Record *Predicate,
OS.indent(4) << "const MachineInstr *MI = FirstMI;\n";
OS.indent(4) << "if (";
PE.setNegatePredicate(true);
- PE.setIndentLevel(3);
+ PE.getIndent() = 3;
PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate"));
OS << ")\n";
OS.indent(4) << " return false;\n";
@@ -181,7 +181,7 @@ void MacroFusionPredicatorEmitter::emitSecondPredicate(const Record *Predicate,
OS.indent(4) << "const MachineInstr *MI = &SecondMI;\n";
OS.indent(4) << "if (";
PE.setNegatePredicate(true);
- PE.setIndentLevel(3);
+ PE.getIndent() = 3;
PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate"));
OS << ")\n";
OS.indent(4) << " return false;\n";
diff --git a/llvm/utils/TableGen/SubtargetEmitter.cpp b/llvm/utils/TableGen/SubtargetEmitter.cpp
index 78d80ff82d6a4f..d21e19c060afc5 100644
--- a/llvm/utils/TableGen/SubtargetEmitter.cpp
+++ b/llvm/utils/TableGen/SubtargetEmitter.cpp
@@ -1576,13 +1576,13 @@ static void emitPredicates(const CodeGenSchedTransition &T,
unsigned NumNonTruePreds =
T.PredTerm.size() - count_if(T.PredTerm, isTruePredicate);
- SS.indent(PE.getIndentLevel() * 2);
+ SS << PE.getIndent();
if (NumNonTruePreds) {
bool FirstNonTruePredicate = true;
SS << "if (";
- PE.setIndentLevel(PE.getIndentLevel() + 2);
+ PE.getIndent() += 2;
for (const Record *Rec : T.PredTerm) {
// Skip predicates that evaluate to "true".
@@ -1593,7 +1593,7 @@ static void emitPredicates(const CodeGenSchedTransition &T,
FirstNonTruePredicate = false;
} else {
SS << "\n";
- SS.indent(PE.getIndentLevel() * 2);
+ SS << PE.getIndent();
SS << "&& ";
}
@@ -1610,9 +1610,9 @@ static void emitPredicates(const CodeGenSchedTransition &T,
}
SS << ")\n"; // end of if-stmt
- PE.decreaseIndentLevel();
- SS.indent(PE.getIndentLevel() * 2);
- PE.decreaseIndentLevel();
+ --PE.getIndent();
+ SS << PE.getIndent();
+ --PE.getIndent();
}
SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
@@ -1736,7 +1736,7 @@ void SubtargetEmitter::emitSchedModelHelpersImpl(
FinalT = &T;
continue;
}
- PE.setIndentLevel(3);
+ PE.getIndent() = 3;
emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
}
if (FinalT)
@@ -1780,11 +1780,10 @@ void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
<< "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n"
<< "} // " << ClassName << "::resolveVariantSchedClass\n\n";
- STIPredicateExpander PE(Target);
+ STIPredicateExpander PE(Target, /*Indent=*/0);
PE.setClassPrefix(ClassName);
PE.setExpandDefinition(true);
PE.setByRef(false);
- PE.setIndentLevel(0);
for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
PE.expandSTIPredicate(OS, Fn);
@@ -1962,7 +1961,7 @@ void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
- STIPredicateExpander PE(Target);
+ STIPredicateExpander PE(Target, /*Indent=*/0);
PE.setExpandForMC(true);
PE.setByRef(true);
for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
@@ -1976,7 +1975,6 @@ void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
std::string ClassPrefix = Target + "MCInstrAnalysis";
PE.setExpandDefinition(true);
PE.setClassPrefix(ClassPrefix);
- PE.setIndentLevel(0);
for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
PE.expandSTIPredicate(OS, Fn);
More information about the llvm-commits
mailing list