[Tablegen] Replace uses of formatted_raw_ostream with raw_ostream in the predicate expander. NFCI

This is a follow-up of r339552.

As pointed out by Craig in D50566, we don't need a formatted_raw_ostream to
indent strings. We can use instead raw_ostream::indent().

Internally, class PredicateExpander already keeps track of the current
indentation level. Also, the grammar for predicates is well parenthesized, and
therefore we don't need to use a formatted_raw_ostream to continuously track the
column number. Instead we can safely replace all the uses of
formatted_raw_ostream::PadToColumn() with uses of raw_ostream::indent().

By replacing formatted_raw_ostream with a simpler raw_ostream, we also avoid the
implicit check on the newline character on every print to stream.

No functional change intended.

llvm-svn: 339577
This commit is contained in:
Andrea Di Biagio 2018-08-13 15:13:35 +00:00
parent cce15c76d3
commit 2c6cbc8bb2
4 changed files with 136 additions and 139 deletions

View File

@ -358,45 +358,47 @@ void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS) {
CodeGenTarget &Target = CDP.getTargetInfo();
const StringRef TargetName = Target.getName();
formatted_raw_ostream FOS(OS);
FOS << "#ifdef GET_GENINSTRINFO_MC_DECL\n";
FOS << "#undef GET_GENINSTRINFO_MC_DECL\n\n";
OS << "#ifdef GET_GENINSTRINFO_MC_DECL\n";
OS << "#undef GET_GENINSTRINFO_MC_DECL\n\n";
FOS << "namespace llvm {\n";
FOS << "class MCInst;\n\n";
OS << "namespace llvm {\n";
OS << "class MCInst;\n\n";
FOS << "namespace " << TargetName << "_MC {\n\n";
OS << "namespace " << TargetName << "_MC {\n\n";
for (const Record *Rec : TIIPredicates) {
FOS << "bool " << Rec->getValueAsString("FunctionName")
OS << "bool " << Rec->getValueAsString("FunctionName")
<< "(const MCInst &MI);\n";
}
FOS << "\n} // end " << TargetName << "_MC namespace\n";
FOS << "} // end llvm namespace\n\n";
OS << "\n} // end " << TargetName << "_MC namespace\n";
OS << "} // end llvm namespace\n\n";
FOS << "#endif // GET_GENINSTRINFO_MC_DECL\n\n";
OS << "#endif // GET_GENINSTRINFO_MC_DECL\n\n";
FOS << "#ifdef GET_GENINSTRINFO_MC_HELPERS\n";
FOS << "#undef GET_GENINSTRINFO_MC_HELPERS\n\n";
OS << "#ifdef GET_GENINSTRINFO_MC_HELPERS\n";
OS << "#undef GET_GENINSTRINFO_MC_HELPERS\n\n";
FOS << "namespace llvm {\n";
FOS << "namespace " << TargetName << "_MC {\n\n";
OS << "namespace llvm {\n";
OS << "namespace " << TargetName << "_MC {\n\n";
PredicateExpander PE;
PE.setExpandForMC(true);
for (const Record *Rec : TIIPredicates) {
FOS << "bool " << Rec->getValueAsString("FunctionName");
FOS << "(const MCInst &MI) {\n";
PE.expandStatement(FOS, Rec->getValueAsDef("Body"));
FOS << "\n}\n";
OS << "bool " << Rec->getValueAsString("FunctionName");
OS << "(const MCInst &MI) {\n";
OS.indent(PE.getIndentLevel() * 2);
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
OS << "\n}\n";
}
FOS << "\n} // end " << TargetName << "_MC namespace\n";
FOS << "} // end llvm namespace\n\n";
OS << "\n} // end " << TargetName << "_MC namespace\n";
OS << "} // end llvm namespace\n\n";
FOS << "#endif // GET_GENISTRINFO_MC_HELPERS\n";
OS << "#endif // GET_GENISTRINFO_MC_HELPERS\n";
}
void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
@ -404,16 +406,17 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
if (TIIPredicates.empty())
return;
formatted_raw_ostream FOS(OS);
PredicateExpander PE;
PE.setExpandForMC(false);
PE.setIndentLevel(2);
for (const Record *Rec : TIIPredicates) {
FOS << "\n static bool " << Rec->getValueAsString("FunctionName");
FOS << "(const MachineInstr &MI) {\n";
PE.expandStatement(FOS, Rec->getValueAsDef("Body"));
FOS << "\n }\n";
OS << "\n static bool " << Rec->getValueAsString("FunctionName");
OS << "(const MachineInstr &MI) {\n";
OS.indent(PE.getIndentLevel() * 2);
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
OS << "\n }\n";
}
}

View File

@ -15,25 +15,23 @@
namespace llvm {
void PredicateExpander::expandTrue(formatted_raw_ostream &OS) { OS << "true"; }
void PredicateExpander::expandFalse(formatted_raw_ostream &OS) {
OS << "false";
}
void PredicateExpander::expandTrue(raw_ostream &OS) { OS << "true"; }
void PredicateExpander::expandFalse(raw_ostream &OS) { OS << "false"; }
void PredicateExpander::expandCheckImmOperand(formatted_raw_ostream &OS,
int OpIndex, int ImmVal) {
void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
int ImmVal) {
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
<< ").getImm() " << (shouldNegate() ? "!= " : "== ") << ImmVal;
}
void PredicateExpander::expandCheckImmOperand(formatted_raw_ostream &OS,
int OpIndex, StringRef ImmVal) {
void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
StringRef ImmVal) {
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
<< ").getImm() " << (shouldNegate() ? "!= " : "== ") << ImmVal;
}
void PredicateExpander::expandCheckRegOperand(formatted_raw_ostream &OS,
int OpIndex, const Record *Reg) {
void PredicateExpander::expandCheckRegOperand(raw_ostream &OS, int OpIndex,
const Record *Reg) {
assert(Reg->isSubClassOf("Register") && "Expected a register Record!");
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
@ -44,33 +42,31 @@ void PredicateExpander::expandCheckRegOperand(formatted_raw_ostream &OS,
OS << Reg->getName();
}
void PredicateExpander::expandCheckInvalidRegOperand(formatted_raw_ostream &OS,
void PredicateExpander::expandCheckInvalidRegOperand(raw_ostream &OS,
int OpIndex) {
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
<< ").getReg() " << (shouldNegate() ? "!= " : "== ") << "0";
}
void PredicateExpander::expandCheckSameRegOperand(formatted_raw_ostream &OS,
int First, int Second) {
void PredicateExpander::expandCheckSameRegOperand(raw_ostream &OS, int First,
int Second) {
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << First
<< ").getReg() " << (shouldNegate() ? "!=" : "==") << " MI"
<< (isByRef() ? "." : "->") << "getOperand(" << Second << ").getReg()";
}
void PredicateExpander::expandCheckNumOperands(formatted_raw_ostream &OS,
int NumOps) {
void PredicateExpander::expandCheckNumOperands(raw_ostream &OS, int NumOps) {
OS << "MI" << (isByRef() ? "." : "->") << "getNumOperands() "
<< (shouldNegate() ? "!= " : "== ") << NumOps;
}
void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
const Record *Inst) {
void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) {
OS << "MI" << (isByRef() ? "." : "->") << "getOpcode() "
<< (shouldNegate() ? "!= " : "== ") << Inst->getValueAsString("Namespace")
<< "::" << Inst->getName();
}
void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
const RecVec &Opcodes) {
assert(!Opcodes.empty() && "Expected at least one opcode to check!");
bool First = true;
@ -86,7 +82,7 @@ void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
increaseIndentLevel();
for (const Record *Rec : Opcodes) {
OS << '\n';
OS.PadToColumn(getIndentLevel() * 2);
OS.indent(getIndentLevel() * 2);
if (!First)
OS << (shouldNegate() ? "&& " : "|| ");
@ -96,11 +92,11 @@ void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
OS << '\n';
decreaseIndentLevel();
OS.PadToColumn(getIndentLevel() * 2);
OS.indent(getIndentLevel() * 2);
OS << ')';
}
void PredicateExpander::expandCheckPseudo(formatted_raw_ostream &OS,
void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
const RecVec &Opcodes) {
if (shouldExpandForMC())
expandFalse(OS);
@ -108,7 +104,7 @@ void PredicateExpander::expandCheckPseudo(formatted_raw_ostream &OS,
expandCheckOpcode(OS, Opcodes);
}
void PredicateExpander::expandPredicateSequence(formatted_raw_ostream &OS,
void PredicateExpander::expandPredicateSequence(raw_ostream &OS,
const RecVec &Sequence,
bool IsCheckAll) {
assert(!Sequence.empty() && "Found an invalid empty predicate set!");
@ -124,7 +120,7 @@ void PredicateExpander::expandPredicateSequence(formatted_raw_ostream &OS,
setNegatePredicate(false);
for (const Record *Rec : Sequence) {
OS << '\n';
OS.PadToColumn(getIndentLevel() * 2);
OS.indent(getIndentLevel() * 2);
if (!First)
OS << (IsCheckAll ? "&& " : "|| ");
expandPredicate(OS, Rec);
@ -132,12 +128,12 @@ void PredicateExpander::expandPredicateSequence(formatted_raw_ostream &OS,
}
OS << '\n';
decreaseIndentLevel();
OS.PadToColumn(getIndentLevel() * 2);
OS.indent(getIndentLevel() * 2);
OS << ')';
setNegatePredicate(OldValue);
}
void PredicateExpander::expandTIIFunctionCall(formatted_raw_ostream &OS,
void PredicateExpander::expandTIIFunctionCall(raw_ostream &OS,
StringRef TargetName,
StringRef MethodName) {
OS << (shouldNegate() ? "!" : "");
@ -149,26 +145,24 @@ void PredicateExpander::expandTIIFunctionCall(formatted_raw_ostream &OS,
OS << MethodName << (isByRef() ? "(MI)" : "(*MI)");
}
void PredicateExpander::expandCheckIsRegOperand(formatted_raw_ostream &OS,
int OpIndex) {
void PredicateExpander::expandCheckIsRegOperand(raw_ostream &OS, int OpIndex) {
OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
<< "getOperand(" << OpIndex << ").isReg() ";
}
void PredicateExpander::expandCheckIsImmOperand(formatted_raw_ostream &OS,
int OpIndex) {
void PredicateExpander::expandCheckIsImmOperand(raw_ostream &OS, int OpIndex) {
OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
<< "getOperand(" << OpIndex << ").isImm() ";
}
void PredicateExpander::expandCheckFunctionPredicate(formatted_raw_ostream &OS,
void PredicateExpander::expandCheckFunctionPredicate(raw_ostream &OS,
StringRef MCInstFn,
StringRef MachineInstrFn) {
OS << (shouldExpandForMC() ? MCInstFn : MachineInstrFn)
<< (isByRef() ? "(MI)" : "(*MI)");
}
void PredicateExpander::expandCheckNonPortable(formatted_raw_ostream &OS,
void PredicateExpander::expandCheckNonPortable(raw_ostream &OS,
StringRef Code) {
if (shouldExpandForMC())
return expandFalse(OS);
@ -176,58 +170,63 @@ void PredicateExpander::expandCheckNonPortable(formatted_raw_ostream &OS,
OS << '(' << Code << ')';
}
void PredicateExpander::expandReturnStatement(formatted_raw_ostream &OS,
void PredicateExpander::expandReturnStatement(raw_ostream &OS,
const Record *Rec) {
OS << "return ";
expandPredicate(OS, Rec);
OS << ";";
std::string Buffer;
raw_string_ostream SS(Buffer);
SS << "return ";
expandPredicate(SS, Rec);
SS << ";";
SS.flush();
OS << Buffer;
}
void PredicateExpander::expandOpcodeSwitchCase(formatted_raw_ostream &OS,
void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
const Record *Rec) {
const RecVec &Opcodes = Rec->getValueAsListOfDefs("Opcodes");
for (const Record *Opcode : Opcodes) {
OS.PadToColumn(getIndentLevel() * 2);
OS.indent(getIndentLevel() * 2);
OS << "case " << Opcode->getValueAsString("Namespace")
<< "::" << Opcode->getName() << " :\n";
}
increaseIndentLevel();
OS.indent(getIndentLevel() * 2);
expandStatement(OS, Rec->getValueAsDef("CaseStmt"));
decreaseIndentLevel();
}
void PredicateExpander::expandOpcodeSwitchStatement(formatted_raw_ostream &OS,
void PredicateExpander::expandOpcodeSwitchStatement(raw_ostream &OS,
const RecVec &Cases,
const Record *Default) {
OS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
std::string Buffer;
raw_string_ostream SS(Buffer);
SS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
for (const Record *Rec : Cases) {
expandOpcodeSwitchCase(OS, Rec);
OS << '\n';
expandOpcodeSwitchCase(SS, Rec);
SS << '\n';
}
unsigned ColNum = getIndentLevel() * 2;
OS.PadToColumn(ColNum);
// Expand the default case.
OS << "default :\n";
increaseIndentLevel();
expandStatement(OS, Default);
decreaseIndentLevel();
OS << '\n';
SS.indent(getIndentLevel() * 2);
SS << "default :\n";
OS.PadToColumn(ColNum);
OS << "} // end of switch-stmt";
increaseIndentLevel();
SS.indent(getIndentLevel() * 2);
expandStatement(SS, Default);
decreaseIndentLevel();
SS << '\n';
SS.indent(getIndentLevel() * 2);
SS << "} // end of switch-stmt";
SS.flush();
OS << Buffer;
}
void PredicateExpander::expandStatement(formatted_raw_ostream &OS,
const Record *Rec) {
OS.flush();
unsigned ColNum = getIndentLevel() * 2;
if (OS.getColumn() < ColNum)
OS.PadToColumn(ColNum);
void PredicateExpander::expandStatement(raw_ostream &OS, const Record *Rec) {
// Assume that padding has been added by the caller.
if (Rec->isSubClassOf("MCOpcodeSwitchStatement")) {
expandOpcodeSwitchStatement(OS, Rec->getValueAsListOfDefs("Cases"),
Rec->getValueAsDef("DefaultCase"));
@ -242,13 +241,8 @@ void PredicateExpander::expandStatement(formatted_raw_ostream &OS,
llvm_unreachable("No known rules to expand this MCStatement");
}
void PredicateExpander::expandPredicate(formatted_raw_ostream &OS,
const Record *Rec) {
OS.flush();
unsigned ColNum = getIndentLevel() * 2;
if (OS.getColumn() < ColNum)
OS.PadToColumn(ColNum);
void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
// Assume that padding has been added by the caller.
if (Rec->isSubClassOf("MCTrue")) {
if (shouldNegate())
return expandFalse(OS);

View File

@ -18,12 +18,12 @@
#define LLVM_UTILS_TABLEGEN_PREDICATEEXPANDER_H
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Record.h"
namespace llvm {
class formatted_raw_ostream;
class raw_ostream;
class PredicateExpander {
bool EmitCallsByRef;
@ -52,38 +52,33 @@ public:
void setIndentLevel(unsigned Level) { IndentLevel = Level; }
using RecVec = std::vector<Record *>;
void expandTrue(formatted_raw_ostream &OS);
void expandFalse(formatted_raw_ostream &OS);
void expandCheckImmOperand(formatted_raw_ostream &OS, int OpIndex,
int ImmVal);
void expandCheckImmOperand(formatted_raw_ostream &OS, int OpIndex,
StringRef ImmVal);
void expandCheckRegOperand(formatted_raw_ostream &OS, int OpIndex,
const Record *Reg);
void expandCheckSameRegOperand(formatted_raw_ostream &OS, int First,
int Second);
void expandCheckNumOperands(formatted_raw_ostream &OS, int NumOps);
void expandCheckOpcode(formatted_raw_ostream &OS, const Record *Inst);
void expandTrue(raw_ostream &OS);
void expandFalse(raw_ostream &OS);
void expandCheckImmOperand(raw_ostream &OS, int OpIndex, int ImmVal);
void expandCheckImmOperand(raw_ostream &OS, int OpIndex, StringRef ImmVal);
void expandCheckRegOperand(raw_ostream &OS, int OpIndex, const Record *Reg);
void expandCheckSameRegOperand(raw_ostream &OS, int First, int Second);
void expandCheckNumOperands(raw_ostream &OS, int NumOps);
void expandCheckOpcode(raw_ostream &OS, const Record *Inst);
void expandCheckPseudo(formatted_raw_ostream &OS, const RecVec &Opcodes);
void expandCheckOpcode(formatted_raw_ostream &OS, const RecVec &Opcodes);
void expandPredicateSequence(formatted_raw_ostream &OS,
const RecVec &Sequence, bool IsCheckAll);
void expandTIIFunctionCall(formatted_raw_ostream &OS, StringRef TargetName,
void expandCheckPseudo(raw_ostream &OS, const RecVec &Opcodes);
void expandCheckOpcode(raw_ostream &OS, const RecVec &Opcodes);
void expandPredicateSequence(raw_ostream &OS, const RecVec &Sequence,
bool IsCheckAll);
void expandTIIFunctionCall(raw_ostream &OS, StringRef TargetName,
StringRef MethodName);
void expandCheckIsRegOperand(formatted_raw_ostream &OS, int OpIndex);
void expandCheckIsImmOperand(formatted_raw_ostream &OS, int OpIndex);
void expandCheckInvalidRegOperand(formatted_raw_ostream &OS, int OpIndex);
void expandCheckFunctionPredicate(formatted_raw_ostream &OS,
StringRef MCInstFn,
void expandCheckIsRegOperand(raw_ostream &OS, int OpIndex);
void expandCheckIsImmOperand(raw_ostream &OS, int OpIndex);
void expandCheckInvalidRegOperand(raw_ostream &OS, int OpIndex);
void expandCheckFunctionPredicate(raw_ostream &OS, StringRef MCInstFn,
StringRef MachineInstrFn);
void expandCheckNonPortable(formatted_raw_ostream &OS, StringRef CodeBlock);
void expandPredicate(formatted_raw_ostream &OS, const Record *Rec);
void expandReturnStatement(formatted_raw_ostream &OS, const Record *Rec);
void expandOpcodeSwitchCase(formatted_raw_ostream &OS, const Record *Rec);
void expandOpcodeSwitchStatement(formatted_raw_ostream &OS,
const RecVec &Cases, const Record *Default);
void expandStatement(formatted_raw_ostream &OS, const Record *Rec);
void expandCheckNonPortable(raw_ostream &OS, StringRef CodeBlock);
void expandPredicate(raw_ostream &OS, const Record *Rec);
void expandReturnStatement(raw_ostream &OS, const Record *Rec);
void expandOpcodeSwitchCase(raw_ostream &OS, const Record *Rec);
void expandOpcodeSwitchStatement(raw_ostream &OS, const RecVec &Cases,
const Record *Default);
void expandStatement(raw_ostream &OS, const Record *Rec);
};
} // namespace llvm

View File

@ -1483,10 +1483,7 @@ static void emitPredicates(const CodeGenSchedTransition &T,
const CodeGenSchedClass &SC, PredicateExpander &PE,
raw_ostream &OS) {
std::string Buffer;
raw_string_ostream StringStream(Buffer);
formatted_raw_ostream FOS(StringStream);
FOS.PadToColumn(6);
raw_string_ostream SS(Buffer);
auto IsTruePredicate = [](const Record *Rec) {
return Rec->isSubClassOf("MCSchedPredicate") &&
@ -1496,40 +1493,48 @@ static void emitPredicates(const CodeGenSchedTransition &T,
// If not all predicates are MCTrue, then we need an if-stmt.
unsigned NumNonTruePreds =
T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
SS.indent(PE.getIndentLevel() * 2);
if (NumNonTruePreds) {
bool FirstNonTruePredicate = true;
SS << "if (";
PE.setIndentLevel(PE.getIndentLevel() + 2);
for (const Record *Rec : T.PredTerm) {
// Skip predicates that evaluate to "true".
if (IsTruePredicate(Rec))
continue;
if (FirstNonTruePredicate) {
FOS << "if (";
FirstNonTruePredicate = false;
} else {
FOS << "\n";
FOS.PadToColumn(8);
FOS << "&& ";
SS << "\n";
SS.indent(PE.getIndentLevel() * 2);
SS << "&& ";
}
if (Rec->isSubClassOf("MCSchedPredicate")) {
PE.expandPredicate(FOS, Rec->getValueAsDef("Pred"));
PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
continue;
}
// Expand this legacy predicate and wrap it around braces if there is more
// than one predicate to expand.
FOS << ((NumNonTruePreds > 1) ? "(" : "")
<< Rec->getValueAsString("Predicate")
<< ((NumNonTruePreds > 1) ? ")" : "");
SS << ((NumNonTruePreds > 1) ? "(" : "")
<< Rec->getValueAsString("Predicate")
<< ((NumNonTruePreds > 1) ? ")" : "");
}
FOS << ")\n"; // end of if-stmt
FOS.PadToColumn(8);
SS << ")\n"; // end of if-stmt
PE.decreaseIndentLevel();
SS.indent(PE.getIndentLevel() * 2);
PE.decreaseIndentLevel();
}
FOS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
FOS.flush();
SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
SS.flush();
OS << Buffer;
}
@ -1629,7 +1634,7 @@ void SubtargetEmitter::emitSchedModelHelpersImpl(
for (const CodeGenSchedTransition &T : SC.Transitions) {
if (PI != 0 && !count(T.ProcIndices, PI))
continue;
PE.setIndentLevel(4);
PE.setIndentLevel(3);
emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
}