Skip to content

Commit c92137e

Browse files
authored
[NFC][TableGen] Adopt scaled indent in PredicateExpander (#109801)
Adopt scaled indent in PredicateExpander. Added pre/post inc/dec operators to `indent` and related unit tests. Verified by comparing *.inc files generated by LLVM build with/without the change.
1 parent 74d9f7c commit c92137e

File tree

7 files changed

+106
-92
lines changed

7 files changed

+106
-92
lines changed

llvm/include/llvm/Support/raw_ostream.h

+24
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,30 @@ struct indent {
797797
assert(NumIndents >= N && "Indentation undeflow");
798798
return indent(NumIndents - N, Scale);
799799
}
800+
indent &operator++() { // Prefix ++.
801+
++NumIndents;
802+
return *this;
803+
}
804+
indent operator++(int) { // Postfix ++.
805+
indent Old = *this;
806+
++NumIndents;
807+
return Old;
808+
}
809+
indent &operator--() { // Prefix --.
810+
assert(NumIndents >= 1);
811+
--NumIndents;
812+
return *this;
813+
}
814+
indent operator--(int) { // Postfix --.
815+
indent Old = *this;
816+
assert(NumIndents >= 1);
817+
--NumIndents;
818+
return Old;
819+
}
820+
indent &operator=(unsigned N) {
821+
NumIndents = N;
822+
return *this;
823+
}
800824
};
801825

802826
inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {

llvm/unittests/Support/raw_ostream_test.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ TEST(raw_ostreamTest, Indent) {
198198
EXPECT_EQ(Spaces(10), printToString(Scaled));
199199
Scaled -= 1;
200200
EXPECT_EQ(Spaces(8), printToString(Scaled));
201+
202+
// Operators.
203+
Indent = 10;
204+
EXPECT_EQ(Spaces(10), printToString(Indent));
205+
206+
indent Temp = Indent++;
207+
EXPECT_EQ(Spaces(11), printToString(Indent));
208+
EXPECT_EQ(Spaces(10), printToString(Temp));
209+
210+
Temp = Indent--;
211+
EXPECT_EQ(Spaces(10), printToString(Indent));
212+
EXPECT_EQ(Spaces(11), printToString(Temp));
213+
214+
Temp = ++Indent;
215+
EXPECT_EQ(Spaces(11), printToString(Indent));
216+
EXPECT_EQ(Spaces(11), printToString(Temp));
217+
218+
Temp = --Indent;
219+
EXPECT_EQ(Spaces(10), printToString(Indent));
220+
EXPECT_EQ(Spaces(10), printToString(Temp));
201221
}
202222

203223
TEST(raw_ostreamTest, FormatHex) {

llvm/utils/TableGen/Common/PredicateExpander.cpp

+40-67
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,18 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
153153
}
154154

155155
OS << '(';
156-
increaseIndentLevel();
156+
++Indent;
157157
for (const Record *Rec : Opcodes) {
158-
OS << '\n';
159-
OS.indent(getIndentLevel() * 2);
158+
OS << '\n' << Indent;
160159
if (!First)
161160
OS << (shouldNegate() ? "&& " : "|| ");
162161

163162
expandCheckOpcode(OS, Rec);
164163
First = false;
165164
}
166165

167-
OS << '\n';
168-
decreaseIndentLevel();
169-
OS.indent(getIndentLevel() * 2);
170-
OS << ')';
166+
--Indent;
167+
OS << '\n' << Indent << ')';
171168
}
172169

173170
void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
@@ -187,22 +184,19 @@ void PredicateExpander::expandPredicateSequence(
187184
// Okay, there is more than one predicate in the set.
188185
bool First = true;
189186
OS << (shouldNegate() ? "!(" : "(");
190-
increaseIndentLevel();
187+
++Indent;
191188

192189
bool OldValue = shouldNegate();
193190
setNegatePredicate(false);
194191
for (const Record *Rec : Sequence) {
195-
OS << '\n';
196-
OS.indent(getIndentLevel() * 2);
192+
OS << '\n' << Indent;
197193
if (!First)
198194
OS << (IsCheckAll ? "&& " : "|| ");
199195
expandPredicate(OS, Rec);
200196
First = false;
201197
}
202-
OS << '\n';
203-
decreaseIndentLevel();
204-
OS.indent(getIndentLevel() * 2);
205-
OS << ')';
198+
--Indent;
199+
OS << '\n' << Indent << ')';
206200
setNegatePredicate(OldValue);
207201
}
208202

@@ -269,15 +263,14 @@ void PredicateExpander::expandReturnStatement(raw_ostream &OS,
269263
void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
270264
const Record *Rec) {
271265
for (const Record *Opcode : Rec->getValueAsListOfDefs("Opcodes")) {
272-
OS.indent(getIndentLevel() * 2);
273-
OS << "case " << Opcode->getValueAsString("Namespace")
266+
OS << Indent << "case " << Opcode->getValueAsString("Namespace")
274267
<< "::" << Opcode->getName() << ":\n";
275268
}
276269

277-
increaseIndentLevel();
278-
OS.indent(getIndentLevel() * 2);
270+
++Indent;
271+
OS << Indent;
279272
expandStatement(OS, Rec->getValueAsDef("CaseStmt"));
280-
decreaseIndentLevel();
273+
--Indent;
281274
}
282275

283276
void PredicateExpander::expandOpcodeSwitchStatement(
@@ -292,17 +285,12 @@ void PredicateExpander::expandOpcodeSwitchStatement(
292285
}
293286

294287
// Expand the default case.
295-
SS.indent(getIndentLevel() * 2);
296-
SS << "default:\n";
288+
SS << Indent << "default:\n";
297289

298-
increaseIndentLevel();
299-
SS.indent(getIndentLevel() * 2);
290+
++Indent;
291+
SS << Indent;
300292
expandStatement(SS, Default);
301-
decreaseIndentLevel();
302-
SS << '\n';
303-
304-
SS.indent(getIndentLevel() * 2);
305-
SS << "} // end of switch-stmt";
293+
SS << '\n' << Indent << "} // end of switch-stmt";
306294
OS << Buffer;
307295
}
308296

@@ -436,8 +424,7 @@ void STIPredicateExpander::expandHeader(raw_ostream &OS,
436424
const Record *Rec = Fn.getDeclaration();
437425
StringRef FunctionName = Rec->getValueAsString("Name");
438426

439-
OS.indent(getIndentLevel() * 2);
440-
OS << "bool ";
427+
OS << Indent << "bool ";
441428
if (shouldExpandDefinition())
442429
OS << getClassPrefix() << "::";
443430
OS << FunctionName << "(";
@@ -463,26 +450,22 @@ void STIPredicateExpander::expandPrologue(raw_ostream &OS,
463450
bool UpdatesOpcodeMask =
464451
Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
465452

466-
increaseIndentLevel();
467-
unsigned IndentLevel = getIndentLevel();
453+
++Indent;
468454
for (const Record *Delegate :
469455
Fn.getDeclaration()->getValueAsListOfDefs("Delegates")) {
470-
OS.indent(IndentLevel * 2);
471-
OS << "if (" << Delegate->getValueAsString("Name") << "(MI";
456+
OS << Indent << "if (" << Delegate->getValueAsString("Name") << "(MI";
472457
if (UpdatesOpcodeMask)
473458
OS << ", Mask";
474459
if (shouldExpandForMC())
475460
OS << ", ProcessorID";
476461
OS << "))\n";
477-
OS.indent((1 + IndentLevel) * 2);
478-
OS << "return true;\n\n";
462+
OS << Indent + 1 << "return true;\n\n";
479463
}
480464

481465
if (shouldExpandForMC())
482466
return;
483467

484-
OS.indent(IndentLevel * 2);
485-
OS << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
468+
OS << Indent << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
486469
}
487470

488471
void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
@@ -497,8 +480,7 @@ void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
497480
continue;
498481

499482
if (FirstProcID) {
500-
OS.indent(getIndentLevel() * 2);
501-
OS << "if (ProcessorID == " << I;
483+
OS << Indent << "if (ProcessorID == " << I;
502484
} else {
503485
OS << " || ProcessorID == " << I;
504486
}
@@ -507,21 +489,20 @@ void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
507489

508490
OS << ") {\n";
509491

510-
increaseIndentLevel();
511-
OS.indent(getIndentLevel() * 2);
492+
++Indent;
493+
OS << Indent;
512494
if (ShouldUpdateOpcodeMask) {
513495
if (PI.OperandMask.isZero())
514496
OS << "Mask.clearAllBits();\n";
515497
else
516498
OS << "Mask = " << PI.OperandMask << ";\n";
517-
OS.indent(getIndentLevel() * 2);
499+
OS << Indent;
518500
}
519501
OS << "return ";
520502
expandPredicate(OS, PI.Predicate);
521503
OS << ";\n";
522-
decreaseIndentLevel();
523-
OS.indent(getIndentLevel() * 2);
524-
OS << "}\n";
504+
--Indent;
505+
OS << Indent << "}\n";
525506
}
526507
}
527508

@@ -530,46 +511,38 @@ void STIPredicateExpander::expandBody(raw_ostream &OS,
530511
bool UpdatesOpcodeMask =
531512
Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
532513

533-
unsigned IndentLevel = getIndentLevel();
534-
OS.indent(IndentLevel * 2);
535-
OS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
536-
OS.indent(IndentLevel * 2);
537-
OS << "default:\n";
538-
OS.indent(IndentLevel * 2);
539-
OS << " break;";
514+
OS << Indent << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
515+
OS << Indent << "default:\n";
516+
OS << Indent << " break;";
540517

541518
for (const OpcodeGroup &Group : Fn.getGroups()) {
542519
for (const Record *Opcode : Group.getOpcodes()) {
543-
OS << '\n';
544-
OS.indent(IndentLevel * 2);
545-
OS << "case " << getTargetName() << "::" << Opcode->getName() << ":";
520+
OS << '\n'
521+
<< Indent << "case " << getTargetName() << "::" << Opcode->getName()
522+
<< ":";
546523
}
547524

548525
OS << '\n';
549-
increaseIndentLevel();
526+
++Indent;
550527
expandOpcodeGroup(OS, Group, UpdatesOpcodeMask);
551528

552-
OS.indent(getIndentLevel() * 2);
553-
OS << "break;\n";
554-
decreaseIndentLevel();
529+
OS << Indent << "break;\n";
530+
--Indent;
555531
}
556532

557-
OS.indent(IndentLevel * 2);
558-
OS << "}\n";
533+
OS << Indent << "}\n";
559534
}
560535

561536
void STIPredicateExpander::expandEpilogue(raw_ostream &OS,
562537
const STIPredicateFunction &Fn) {
563-
OS << '\n';
564-
OS.indent(getIndentLevel() * 2);
538+
OS << '\n' << Indent;
565539
OS << "return ";
566540
expandPredicate(OS, Fn.getDefaultReturnPredicate());
567541
OS << ";\n";
568542

569-
decreaseIndentLevel();
570-
OS.indent(getIndentLevel() * 2);
543+
--Indent;
571544
StringRef FunctionName = Fn.getDeclaration()->getValueAsString("Name");
572-
OS << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
545+
OS << Indent << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
573546
}
574547

575548
void STIPredicateExpander::expandSTIPredicate(raw_ostream &OS,

llvm/utils/TableGen/Common/PredicateExpander.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,38 @@
1818

1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/Support/raw_ostream.h"
2122

2223
namespace llvm {
2324

24-
class raw_ostream;
2525
class Record;
2626

2727
class PredicateExpander {
2828
bool EmitCallsByRef;
2929
bool NegatePredicate;
3030
bool ExpandForMC;
31-
unsigned IndentLevel;
3231
StringRef TargetName;
3332

3433
PredicateExpander(const PredicateExpander &) = delete;
3534
PredicateExpander &operator=(const PredicateExpander &) = delete;
3635

36+
protected:
37+
indent Indent;
38+
3739
public:
38-
PredicateExpander(StringRef Target)
40+
explicit PredicateExpander(StringRef Target, unsigned Indent = 1)
3941
: EmitCallsByRef(true), NegatePredicate(false), ExpandForMC(false),
40-
IndentLevel(1U), TargetName(Target) {}
42+
TargetName(Target), Indent(Indent, 2) {}
4143
bool isByRef() const { return EmitCallsByRef; }
4244
bool shouldNegate() const { return NegatePredicate; }
4345
bool shouldExpandForMC() const { return ExpandForMC; }
44-
unsigned getIndentLevel() const { return IndentLevel; }
46+
indent &getIndent() { return Indent; }
4547
StringRef getTargetName() const { return TargetName; }
4648

4749
void setByRef(bool Value) { EmitCallsByRef = Value; }
4850
void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
4951
void setNegatePredicate(bool Value) { NegatePredicate = Value; }
5052
void setExpandForMC(bool Value) { ExpandForMC = Value; }
51-
void setIndentLevel(unsigned Level) { IndentLevel = Level; }
52-
void increaseIndentLevel() { ++IndentLevel; }
53-
void decreaseIndentLevel() { --IndentLevel; }
5453

5554
void expandTrue(raw_ostream &OS);
5655
void expandFalse(raw_ostream &OS);
@@ -116,8 +115,8 @@ class STIPredicateExpander : public PredicateExpander {
116115
void expandEpilogue(raw_ostream &OS, const STIPredicateFunction &Fn);
117116

118117
public:
119-
STIPredicateExpander(StringRef Target)
120-
: PredicateExpander(Target), ExpandDefinition(false) {}
118+
explicit STIPredicateExpander(StringRef Target, unsigned Indent = 1)
119+
: PredicateExpander(Target, Indent), ExpandDefinition(false) {}
121120

122121
bool shouldExpandDefinition() const { return ExpandDefinition; }
123122
StringRef getClassPrefix() const { return ClassPrefix; }

llvm/utils/TableGen/InstrInfoEmitter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS,
711711
OS << "bool " << Rec->getValueAsString("FunctionName");
712712
OS << "(const MCInst &MI) {\n";
713713

714-
OS.indent(PE.getIndentLevel() * 2);
714+
OS << PE.getIndent();
715715
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
716716
OS << "\n}\n\n";
717717
}
@@ -914,7 +914,7 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
914914
}
915915

916916
OS << " {\n";
917-
OS.indent(PE.getIndentLevel() * 2);
917+
OS << PE.getIndent();
918918
PE.expandStatement(OS, Rec->getValueAsDef("Body"));
919919
OS << "\n}\n\n";
920920
}

llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void MacroFusionPredicatorEmitter::emitFirstPredicate(const Record *Predicate,
160160
OS.indent(4) << "const MachineInstr *MI = FirstMI;\n";
161161
OS.indent(4) << "if (";
162162
PE.setNegatePredicate(true);
163-
PE.setIndentLevel(3);
163+
PE.getIndent() = 3;
164164
PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate"));
165165
OS << ")\n";
166166
OS.indent(4) << " return false;\n";
@@ -181,7 +181,7 @@ void MacroFusionPredicatorEmitter::emitSecondPredicate(const Record *Predicate,
181181
OS.indent(4) << "const MachineInstr *MI = &SecondMI;\n";
182182
OS.indent(4) << "if (";
183183
PE.setNegatePredicate(true);
184-
PE.setIndentLevel(3);
184+
PE.getIndent() = 3;
185185
PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate"));
186186
OS << ")\n";
187187
OS.indent(4) << " return false;\n";

0 commit comments

Comments
 (0)