Skip to content

Commit 2c6cbc8

Browse files
committedAug 13, 2018
[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
1 parent cce15c7 commit 2c6cbc8

File tree

4 files changed

+134
-137
lines changed

4 files changed

+134
-137
lines changed
 

‎llvm/utils/TableGen/InstrInfoEmitter.cpp

+29-26
Original file line numberDiff line numberDiff line change
@@ -358,62 +358,65 @@ void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS) {
358358

359359
CodeGenTarget &Target = CDP.getTargetInfo();
360360
const StringRef TargetName = Target.getName();
361-
formatted_raw_ostream FOS(OS);
362361

363-
FOS << "#ifdef GET_GENINSTRINFO_MC_DECL\n";
364-
FOS << "#undef GET_GENINSTRINFO_MC_DECL\n\n";
362+
OS << "#ifdef GET_GENINSTRINFO_MC_DECL\n";
363+
OS << "#undef GET_GENINSTRINFO_MC_DECL\n\n";
365364

366-
FOS << "namespace llvm {\n";
367-
FOS << "class MCInst;\n\n";
365+
OS << "namespace llvm {\n";
366+
OS << "class MCInst;\n\n";
368367

369-
FOS << "namespace " << TargetName << "_MC {\n\n";
368+
OS << "namespace " << TargetName << "_MC {\n\n";
370369

371370
for (const Record *Rec : TIIPredicates) {
372-
FOS << "bool " << Rec->getValueAsString("FunctionName")
371+
OS << "bool " << Rec->getValueAsString("FunctionName")
373372
<< "(const MCInst &MI);\n";
374373
}
375374

376-
FOS << "\n} // end " << TargetName << "_MC namespace\n";
377-
FOS << "} // end llvm namespace\n\n";
375+
OS << "\n} // end " << TargetName << "_MC namespace\n";
376+
OS << "} // end llvm namespace\n\n";
378377

379-
FOS << "#endif // GET_GENINSTRINFO_MC_DECL\n\n";
378+
OS << "#endif // GET_GENINSTRINFO_MC_DECL\n\n";
380379

381-
FOS << "#ifdef GET_GENINSTRINFO_MC_HELPERS\n";
382-
FOS << "#undef GET_GENINSTRINFO_MC_HELPERS\n\n";
380+
OS << "#ifdef GET_GENINSTRINFO_MC_HELPERS\n";
381+
OS << "#undef GET_GENINSTRINFO_MC_HELPERS\n\n";
383382

384-
FOS << "namespace llvm {\n";
385-
FOS << "namespace " << TargetName << "_MC {\n\n";
383+
OS << "namespace llvm {\n";
384+
OS << "namespace " << TargetName << "_MC {\n\n";
386385

387386
PredicateExpander PE;
388387
PE.setExpandForMC(true);
388+
389389
for (const Record *Rec : TIIPredicates) {
390-
FOS << "bool " << Rec->getValueAsString("FunctionName");
391-
FOS << "(const MCInst &MI) {\n";
392-
PE.expandStatement(FOS, Rec->getValueAsDef("Body"));
393-
FOS << "\n}\n";
390+
OS << "bool " << Rec->getValueAsString("FunctionName");
391+
OS << "(const MCInst &MI) {\n";
392+
393+
OS.indent(PE.getIndentLevel() * 2);
394+
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
395+
OS << "\n}\n";
394396
}
395397

396-
FOS << "\n} // end " << TargetName << "_MC namespace\n";
397-
FOS << "} // end llvm namespace\n\n";
398+
OS << "\n} // end " << TargetName << "_MC namespace\n";
399+
OS << "} // end llvm namespace\n\n";
398400

399-
FOS << "#endif // GET_GENISTRINFO_MC_HELPERS\n";
401+
OS << "#endif // GET_GENISTRINFO_MC_HELPERS\n";
400402
}
401403

402404
void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
403405
RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
404406
if (TIIPredicates.empty())
405407
return;
406408

407-
formatted_raw_ostream FOS(OS);
408409
PredicateExpander PE;
409410
PE.setExpandForMC(false);
410411
PE.setIndentLevel(2);
411412

412413
for (const Record *Rec : TIIPredicates) {
413-
FOS << "\n static bool " << Rec->getValueAsString("FunctionName");
414-
FOS << "(const MachineInstr &MI) {\n";
415-
PE.expandStatement(FOS, Rec->getValueAsDef("Body"));
416-
FOS << "\n }\n";
414+
OS << "\n static bool " << Rec->getValueAsString("FunctionName");
415+
OS << "(const MachineInstr &MI) {\n";
416+
417+
OS.indent(PE.getIndentLevel() * 2);
418+
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
419+
OS << "\n }\n";
417420
}
418421
}
419422

‎llvm/utils/TableGen/PredicateExpander.cpp

+57-63
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@
1515

1616
namespace llvm {
1717

18-
void PredicateExpander::expandTrue(formatted_raw_ostream &OS) { OS << "true"; }
19-
void PredicateExpander::expandFalse(formatted_raw_ostream &OS) {
20-
OS << "false";
21-
}
18+
void PredicateExpander::expandTrue(raw_ostream &OS) { OS << "true"; }
19+
void PredicateExpander::expandFalse(raw_ostream &OS) { OS << "false"; }
2220

23-
void PredicateExpander::expandCheckImmOperand(formatted_raw_ostream &OS,
24-
int OpIndex, int ImmVal) {
21+
void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
22+
int ImmVal) {
2523
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
2624
<< ").getImm() " << (shouldNegate() ? "!= " : "== ") << ImmVal;
2725
}
2826

29-
void PredicateExpander::expandCheckImmOperand(formatted_raw_ostream &OS,
30-
int OpIndex, StringRef ImmVal) {
27+
void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
28+
StringRef ImmVal) {
3129
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
3230
<< ").getImm() " << (shouldNegate() ? "!= " : "== ") << ImmVal;
3331
}
3432

35-
void PredicateExpander::expandCheckRegOperand(formatted_raw_ostream &OS,
36-
int OpIndex, const Record *Reg) {
33+
void PredicateExpander::expandCheckRegOperand(raw_ostream &OS, int OpIndex,
34+
const Record *Reg) {
3735
assert(Reg->isSubClassOf("Register") && "Expected a register Record!");
3836

3937
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
@@ -44,33 +42,31 @@ void PredicateExpander::expandCheckRegOperand(formatted_raw_ostream &OS,
4442
OS << Reg->getName();
4543
}
4644

47-
void PredicateExpander::expandCheckInvalidRegOperand(formatted_raw_ostream &OS,
45+
void PredicateExpander::expandCheckInvalidRegOperand(raw_ostream &OS,
4846
int OpIndex) {
4947
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
5048
<< ").getReg() " << (shouldNegate() ? "!= " : "== ") << "0";
5149
}
5250

53-
void PredicateExpander::expandCheckSameRegOperand(formatted_raw_ostream &OS,
54-
int First, int Second) {
51+
void PredicateExpander::expandCheckSameRegOperand(raw_ostream &OS, int First,
52+
int Second) {
5553
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << First
5654
<< ").getReg() " << (shouldNegate() ? "!=" : "==") << " MI"
5755
<< (isByRef() ? "." : "->") << "getOperand(" << Second << ").getReg()";
5856
}
5957

60-
void PredicateExpander::expandCheckNumOperands(formatted_raw_ostream &OS,
61-
int NumOps) {
58+
void PredicateExpander::expandCheckNumOperands(raw_ostream &OS, int NumOps) {
6259
OS << "MI" << (isByRef() ? "." : "->") << "getNumOperands() "
6360
<< (shouldNegate() ? "!= " : "== ") << NumOps;
6461
}
6562

66-
void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
67-
const Record *Inst) {
63+
void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) {
6864
OS << "MI" << (isByRef() ? "." : "->") << "getOpcode() "
6965
<< (shouldNegate() ? "!= " : "== ") << Inst->getValueAsString("Namespace")
7066
<< "::" << Inst->getName();
7167
}
7268

73-
void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
69+
void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
7470
const RecVec &Opcodes) {
7571
assert(!Opcodes.empty() && "Expected at least one opcode to check!");
7672
bool First = true;
@@ -86,7 +82,7 @@ void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
8682
increaseIndentLevel();
8783
for (const Record *Rec : Opcodes) {
8884
OS << '\n';
89-
OS.PadToColumn(getIndentLevel() * 2);
85+
OS.indent(getIndentLevel() * 2);
9086
if (!First)
9187
OS << (shouldNegate() ? "&& " : "|| ");
9288

@@ -96,19 +92,19 @@ void PredicateExpander::expandCheckOpcode(formatted_raw_ostream &OS,
9692

9793
OS << '\n';
9894
decreaseIndentLevel();
99-
OS.PadToColumn(getIndentLevel() * 2);
95+
OS.indent(getIndentLevel() * 2);
10096
OS << ')';
10197
}
10298

103-
void PredicateExpander::expandCheckPseudo(formatted_raw_ostream &OS,
99+
void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
104100
const RecVec &Opcodes) {
105101
if (shouldExpandForMC())
106102
expandFalse(OS);
107103
else
108104
expandCheckOpcode(OS, Opcodes);
109105
}
110106

111-
void PredicateExpander::expandPredicateSequence(formatted_raw_ostream &OS,
107+
void PredicateExpander::expandPredicateSequence(raw_ostream &OS,
112108
const RecVec &Sequence,
113109
bool IsCheckAll) {
114110
assert(!Sequence.empty() && "Found an invalid empty predicate set!");
@@ -124,20 +120,20 @@ void PredicateExpander::expandPredicateSequence(formatted_raw_ostream &OS,
124120
setNegatePredicate(false);
125121
for (const Record *Rec : Sequence) {
126122
OS << '\n';
127-
OS.PadToColumn(getIndentLevel() * 2);
123+
OS.indent(getIndentLevel() * 2);
128124
if (!First)
129125
OS << (IsCheckAll ? "&& " : "|| ");
130126
expandPredicate(OS, Rec);
131127
First = false;
132128
}
133129
OS << '\n';
134130
decreaseIndentLevel();
135-
OS.PadToColumn(getIndentLevel() * 2);
131+
OS.indent(getIndentLevel() * 2);
136132
OS << ')';
137133
setNegatePredicate(OldValue);
138134
}
139135

140-
void PredicateExpander::expandTIIFunctionCall(formatted_raw_ostream &OS,
136+
void PredicateExpander::expandTIIFunctionCall(raw_ostream &OS,
141137
StringRef TargetName,
142138
StringRef MethodName) {
143139
OS << (shouldNegate() ? "!" : "");
@@ -149,85 +145,88 @@ void PredicateExpander::expandTIIFunctionCall(formatted_raw_ostream &OS,
149145
OS << MethodName << (isByRef() ? "(MI)" : "(*MI)");
150146
}
151147

152-
void PredicateExpander::expandCheckIsRegOperand(formatted_raw_ostream &OS,
153-
int OpIndex) {
148+
void PredicateExpander::expandCheckIsRegOperand(raw_ostream &OS, int OpIndex) {
154149
OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
155150
<< "getOperand(" << OpIndex << ").isReg() ";
156151
}
157152

158-
void PredicateExpander::expandCheckIsImmOperand(formatted_raw_ostream &OS,
159-
int OpIndex) {
153+
void PredicateExpander::expandCheckIsImmOperand(raw_ostream &OS, int OpIndex) {
160154
OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
161155
<< "getOperand(" << OpIndex << ").isImm() ";
162156
}
163157

164-
void PredicateExpander::expandCheckFunctionPredicate(formatted_raw_ostream &OS,
158+
void PredicateExpander::expandCheckFunctionPredicate(raw_ostream &OS,
165159
StringRef MCInstFn,
166160
StringRef MachineInstrFn) {
167161
OS << (shouldExpandForMC() ? MCInstFn : MachineInstrFn)
168162
<< (isByRef() ? "(MI)" : "(*MI)");
169163
}
170164

171-
void PredicateExpander::expandCheckNonPortable(formatted_raw_ostream &OS,
165+
void PredicateExpander::expandCheckNonPortable(raw_ostream &OS,
172166
StringRef Code) {
173167
if (shouldExpandForMC())
174168
return expandFalse(OS);
175169

176170
OS << '(' << Code << ')';
177171
}
178172

179-
void PredicateExpander::expandReturnStatement(formatted_raw_ostream &OS,
173+
void PredicateExpander::expandReturnStatement(raw_ostream &OS,
180174
const Record *Rec) {
181-
OS << "return ";
182-
expandPredicate(OS, Rec);
183-
OS << ";";
175+
std::string Buffer;
176+
raw_string_ostream SS(Buffer);
177+
178+
SS << "return ";
179+
expandPredicate(SS, Rec);
180+
SS << ";";
181+
SS.flush();
182+
OS << Buffer;
184183
}
185184

186-
void PredicateExpander::expandOpcodeSwitchCase(formatted_raw_ostream &OS,
185+
void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
187186
const Record *Rec) {
188187
const RecVec &Opcodes = Rec->getValueAsListOfDefs("Opcodes");
189188
for (const Record *Opcode : Opcodes) {
190-
OS.PadToColumn(getIndentLevel() * 2);
189+
OS.indent(getIndentLevel() * 2);
191190
OS << "case " << Opcode->getValueAsString("Namespace")
192191
<< "::" << Opcode->getName() << " :\n";
193192
}
194193

195194
increaseIndentLevel();
195+
OS.indent(getIndentLevel() * 2);
196196
expandStatement(OS, Rec->getValueAsDef("CaseStmt"));
197197
decreaseIndentLevel();
198198
}
199199

200-
void PredicateExpander::expandOpcodeSwitchStatement(formatted_raw_ostream &OS,
200+
void PredicateExpander::expandOpcodeSwitchStatement(raw_ostream &OS,
201201
const RecVec &Cases,
202202
const Record *Default) {
203-
OS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
203+
std::string Buffer;
204+
raw_string_ostream SS(Buffer);
204205

206+
SS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
205207
for (const Record *Rec : Cases) {
206-
expandOpcodeSwitchCase(OS, Rec);
207-
OS << '\n';
208+
expandOpcodeSwitchCase(SS, Rec);
209+
SS << '\n';
208210
}
209211

210-
unsigned ColNum = getIndentLevel() * 2;
211-
OS.PadToColumn(ColNum);
212-
213212
// Expand the default case.
214-
OS << "default :\n";
213+
SS.indent(getIndentLevel() * 2);
214+
SS << "default :\n";
215+
215216
increaseIndentLevel();
216-
expandStatement(OS, Default);
217+
SS.indent(getIndentLevel() * 2);
218+
expandStatement(SS, Default);
217219
decreaseIndentLevel();
218-
OS << '\n';
220+
SS << '\n';
219221

220-
OS.PadToColumn(ColNum);
221-
OS << "} // end of switch-stmt";
222+
SS.indent(getIndentLevel() * 2);
223+
SS << "} // end of switch-stmt";
224+
SS.flush();
225+
OS << Buffer;
222226
}
223227

224-
void PredicateExpander::expandStatement(formatted_raw_ostream &OS,
225-
const Record *Rec) {
226-
OS.flush();
227-
unsigned ColNum = getIndentLevel() * 2;
228-
if (OS.getColumn() < ColNum)
229-
OS.PadToColumn(ColNum);
230-
228+
void PredicateExpander::expandStatement(raw_ostream &OS, const Record *Rec) {
229+
// Assume that padding has been added by the caller.
231230
if (Rec->isSubClassOf("MCOpcodeSwitchStatement")) {
232231
expandOpcodeSwitchStatement(OS, Rec->getValueAsListOfDefs("Cases"),
233232
Rec->getValueAsDef("DefaultCase"));
@@ -242,13 +241,8 @@ void PredicateExpander::expandStatement(formatted_raw_ostream &OS,
242241
llvm_unreachable("No known rules to expand this MCStatement");
243242
}
244243

245-
void PredicateExpander::expandPredicate(formatted_raw_ostream &OS,
246-
const Record *Rec) {
247-
OS.flush();
248-
unsigned ColNum = getIndentLevel() * 2;
249-
if (OS.getColumn() < ColNum)
250-
OS.PadToColumn(ColNum);
251-
244+
void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
245+
// Assume that padding has been added by the caller.
252246
if (Rec->isSubClassOf("MCTrue")) {
253247
if (shouldNegate())
254248
return expandFalse(OS);

0 commit comments

Comments
 (0)
Please sign in to comment.