Skip to content

Commit d7da79f

Browse files
authored
[NFC][SetTheory] Refactor to use const pointers and range loops (#105544)
- Refactor SetTheory code to use const pointers when possible. - Use auto for variables initialized using dyn_cast<>. - Use range based for loops and early continue.
1 parent 6932f47 commit d7da79f

File tree

5 files changed

+61
-59
lines changed

5 files changed

+61
-59
lines changed

clang/utils/TableGen/NeonEmitter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
15691569
// See the documentation in arm_neon.td for a description of these operators.
15701570
class LowHalf : public SetTheory::Operator {
15711571
public:
1572-
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1572+
void apply(SetTheory &ST, const DagInit *Expr, SetTheory::RecSet &Elts,
15731573
ArrayRef<SMLoc> Loc) override {
15741574
SetTheory::RecSet Elts2;
15751575
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
@@ -1579,7 +1579,7 @@ std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
15791579

15801580
class HighHalf : public SetTheory::Operator {
15811581
public:
1582-
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1582+
void apply(SetTheory &ST, const DagInit *Expr, SetTheory::RecSet &Elts,
15831583
ArrayRef<SMLoc> Loc) override {
15841584
SetTheory::RecSet Elts2;
15851585
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
@@ -1593,7 +1593,7 @@ std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
15931593
public:
15941594
Rev(unsigned ElementSize) : ElementSize(ElementSize) {}
15951595

1596-
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1596+
void apply(SetTheory &ST, const DagInit *Expr, SetTheory::RecSet &Elts,
15971597
ArrayRef<SMLoc> Loc) override {
15981598
SetTheory::RecSet Elts2;
15991599
ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc);
@@ -1618,7 +1618,8 @@ std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
16181618
public:
16191619
MaskExpander(unsigned N) : N(N) {}
16201620

1621-
void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override {
1621+
void expand(SetTheory &ST, const Record *R,
1622+
SetTheory::RecSet &Elts) override {
16221623
unsigned Addend = 0;
16231624
if (R->getName() == "mask0")
16241625
Addend = 0;

llvm/include/llvm/TableGen/SetTheory.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SetTheory {
7676

7777
/// apply - Apply this operator to Expr's arguments and insert the result
7878
/// in Elts.
79-
virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
79+
virtual void apply(SetTheory &, const DagInit *Expr, RecSet &Elts,
8080
ArrayRef<SMLoc> Loc) = 0;
8181
};
8282

@@ -89,13 +89,13 @@ class SetTheory {
8989
public:
9090
virtual ~Expander() = default;
9191

92-
virtual void expand(SetTheory&, Record*, RecSet &Elts) = 0;
92+
virtual void expand(SetTheory &, const Record *, RecSet &Elts) = 0;
9393
};
9494

9595
private:
9696
// Map set defs to their fully expanded contents. This serves as a memoization
9797
// cache and it makes it possible to return const references on queries.
98-
using ExpandMap = std::map<Record *, RecVec>;
98+
using ExpandMap = std::map<const Record *, RecVec>;
9999
ExpandMap Expansions;
100100

101101
// Known DAG operators by name.
@@ -125,7 +125,7 @@ class SetTheory {
125125
void addOperator(StringRef Name, std::unique_ptr<Operator>);
126126

127127
/// evaluate - Evaluate Expr and append the resulting set to Elts.
128-
void evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
128+
void evaluate(const Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
129129

130130
/// evaluate - Evaluate a sequence of Inits and append to Elts.
131131
template<typename Iter>
@@ -137,7 +137,7 @@ class SetTheory {
137137
/// expand - Expand a record into a set of elements if possible. Return a
138138
/// pointer to the expanded elements, or NULL if Set cannot be expanded
139139
/// further.
140-
const RecVec *expand(Record *Set);
140+
const RecVec *expand(const Record *Set);
141141
};
142142

143143
} // end namespace llvm

llvm/lib/TableGen/SetTheory.cpp

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "llvm/TableGen/SetTheory.h"
1515
#include "llvm/ADT/ArrayRef.h"
16+
#include "llvm/ADT/STLExtras.h"
1617
#include "llvm/ADT/SmallVector.h"
1718
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/Support/Casting.h"
@@ -36,15 +37,15 @@ using RecVec = SetTheory::RecVec;
3637

3738
// (add a, b, ...) Evaluate and union all arguments.
3839
struct AddOp : public SetTheory::Operator {
39-
void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
40+
void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts,
4041
ArrayRef<SMLoc> Loc) override {
4142
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
4243
}
4344
};
4445

4546
// (sub Add, Sub, ...) Set difference.
4647
struct SubOp : public SetTheory::Operator {
47-
void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
48+
void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts,
4849
ArrayRef<SMLoc> Loc) override {
4950
if (Expr->arg_size() < 2)
5051
PrintFatalError(Loc, "Set difference needs at least two arguments: " +
@@ -60,7 +61,7 @@ struct SubOp : public SetTheory::Operator {
6061

6162
// (and S1, S2) Set intersection.
6263
struct AndOp : public SetTheory::Operator {
63-
void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
64+
void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts,
6465
ArrayRef<SMLoc> Loc) override {
6566
if (Expr->arg_size() != 2)
6667
PrintFatalError(Loc, "Set intersection requires two arguments: " +
@@ -76,17 +77,17 @@ struct AndOp : public SetTheory::Operator {
7677

7778
// SetIntBinOp - Abstract base class for (Op S, N) operators.
7879
struct SetIntBinOp : public SetTheory::Operator {
79-
virtual void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
80-
RecSet &Elts, ArrayRef<SMLoc> Loc) = 0;
80+
virtual void apply2(SetTheory &ST, const DagInit *Expr, RecSet &Set,
81+
int64_t N, RecSet &Elts, ArrayRef<SMLoc> Loc) = 0;
8182

82-
void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
83+
void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts,
8384
ArrayRef<SMLoc> Loc) override {
8485
if (Expr->arg_size() != 2)
8586
PrintFatalError(Loc, "Operator requires (Op Set, Int) arguments: " +
8687
Expr->getAsString());
8788
RecSet Set;
8889
ST.evaluate(Expr->arg_begin()[0], Set, Loc);
89-
IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]);
90+
const auto *II = dyn_cast<IntInit>(Expr->arg_begin()[1]);
9091
if (!II)
9192
PrintFatalError(Loc, "Second argument must be an integer: " +
9293
Expr->getAsString());
@@ -96,7 +97,7 @@ struct SetIntBinOp : public SetTheory::Operator {
9697

9798
// (shl S, N) Shift left, remove the first N elements.
9899
struct ShlOp : public SetIntBinOp {
99-
void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
100+
void apply2(SetTheory &ST, const DagInit *Expr, RecSet &Set, int64_t N,
100101
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
101102
if (N < 0)
102103
PrintFatalError(Loc, "Positive shift required: " +
@@ -108,7 +109,7 @@ struct ShlOp : public SetIntBinOp {
108109

109110
// (trunc S, N) Truncate after the first N elements.
110111
struct TruncOp : public SetIntBinOp {
111-
void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
112+
void apply2(SetTheory &ST, const DagInit *Expr, RecSet &Set, int64_t N,
112113
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
113114
if (N < 0)
114115
PrintFatalError(Loc, "Positive length required: " +
@@ -125,7 +126,7 @@ struct RotOp : public SetIntBinOp {
125126

126127
RotOp(bool Rev) : Reverse(Rev) {}
127128

128-
void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
129+
void apply2(SetTheory &ST, const DagInit *Expr, RecSet &Set, int64_t N,
129130
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
130131
if (Reverse)
131132
N = -N;
@@ -143,7 +144,7 @@ struct RotOp : public SetIntBinOp {
143144

144145
// (decimate S, N) Pick every N'th element of S.
145146
struct DecimateOp : public SetIntBinOp {
146-
void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
147+
void apply2(SetTheory &ST, const DagInit *Expr, RecSet &Set, int64_t N,
147148
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
148149
if (N <= 0)
149150
PrintFatalError(Loc, "Positive stride required: " +
@@ -155,62 +156,62 @@ struct DecimateOp : public SetIntBinOp {
155156

156157
// (interleave S1, S2, ...) Interleave elements of the arguments.
157158
struct InterleaveOp : public SetTheory::Operator {
158-
void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
159+
void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts,
159160
ArrayRef<SMLoc> Loc) override {
160161
// Evaluate the arguments individually.
161-
SmallVector<RecSet, 4> Args(Expr->getNumArgs());
162+
SmallVector<RecSet, 4> Values(Expr->getNumArgs());
162163
unsigned MaxSize = 0;
163-
for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
164-
ST.evaluate(Expr->getArg(i), Args[i], Loc);
165-
MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
164+
for (auto [Arg, Value] : zip(Expr->getArgs(), Values)) {
165+
ST.evaluate(Arg, Value, Loc);
166+
MaxSize = std::max(MaxSize, unsigned(Value.size()));
166167
}
167168
// Interleave arguments into Elts.
168169
for (unsigned n = 0; n != MaxSize; ++n)
169-
for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i)
170-
if (n < Args[i].size())
171-
Elts.insert(Args[i][n]);
170+
for (const RecSet &Value : Values)
171+
if (n < Value.size())
172+
Elts.insert(Value[n]);
172173
}
173174
};
174175

175176
// (sequence "Format", From, To) Generate a sequence of records by name.
176177
struct SequenceOp : public SetTheory::Operator {
177-
void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
178+
void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts,
178179
ArrayRef<SMLoc> Loc) override {
179180
int Step = 1;
180181
if (Expr->arg_size() > 4)
181182
PrintFatalError(Loc, "Bad args to (sequence \"Format\", From, To): " +
182183
Expr->getAsString());
183-
else if (Expr->arg_size() == 4) {
184-
if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[3])) {
184+
if (Expr->arg_size() == 4) {
185+
if (const auto *II = dyn_cast<IntInit>(Expr->arg_begin()[3]))
185186
Step = II->getValue();
186-
} else
187+
else
187188
PrintFatalError(Loc, "Stride must be an integer: " +
188189
Expr->getAsString());
189190
}
190191

191192
std::string Format;
192-
if (StringInit *SI = dyn_cast<StringInit>(Expr->arg_begin()[0]))
193+
if (const auto *SI = dyn_cast<StringInit>(Expr->arg_begin()[0]))
193194
Format = std::string(SI->getValue());
194195
else
195196
PrintFatalError(Loc, "Format must be a string: " + Expr->getAsString());
196197

197198
int64_t From, To;
198-
if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]))
199+
if (const auto *II = dyn_cast<IntInit>(Expr->arg_begin()[1]))
199200
From = II->getValue();
200201
else
201202
PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
202203
if (From < 0 || From >= (1 << 30))
203204
PrintFatalError(Loc, "From out of range");
204205

205-
if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[2]))
206+
if (const auto *II = dyn_cast<IntInit>(Expr->arg_begin()[2]))
206207
To = II->getValue();
207208
else
208209
PrintFatalError(Loc, "To must be an integer: " + Expr->getAsString());
209210
if (To < 0 || To >= (1 << 30))
210211
PrintFatalError(Loc, "To out of range");
211212

212-
RecordKeeper &Records =
213-
cast<DefInit>(Expr->getOperator())->getDef()->getRecords();
213+
const RecordKeeper &Records =
214+
cast<DefInit>(Expr->getOperator())->getDef()->getRecords();
214215

215216
Step *= From <= To ? 1 : -1;
216217
while (true) {
@@ -242,7 +243,7 @@ struct FieldExpander : public SetTheory::Expander {
242243

243244
FieldExpander(StringRef fn) : FieldName(fn) {}
244245

245-
void expand(SetTheory &ST, Record *Def, RecSet &Elts) override {
246+
void expand(SetTheory &ST, const Record *Def, RecSet &Elts) override {
246247
ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
247248
}
248249
};
@@ -278,24 +279,24 @@ void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
278279
addExpander(ClassName, std::make_unique<FieldExpander>(FieldName));
279280
}
280281

281-
void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
282+
void SetTheory::evaluate(const Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
282283
// A def in a list can be a just an element, or it may expand.
283-
if (DefInit *Def = dyn_cast<DefInit>(Expr)) {
284+
if (const auto *Def = dyn_cast<DefInit>(Expr)) {
284285
if (const RecVec *Result = expand(Def->getDef()))
285286
return Elts.insert(Result->begin(), Result->end());
286287
Elts.insert(Def->getDef());
287288
return;
288289
}
289290

290291
// Lists simply expand.
291-
if (ListInit *LI = dyn_cast<ListInit>(Expr))
292+
if (const auto *LI = dyn_cast<ListInit>(Expr))
292293
return evaluate(LI->begin(), LI->end(), Elts, Loc);
293294

294295
// Anything else must be a DAG.
295-
DagInit *DagExpr = dyn_cast<DagInit>(Expr);
296+
const auto *DagExpr = dyn_cast<DagInit>(Expr);
296297
if (!DagExpr)
297298
PrintFatalError(Loc, "Invalid set element: " + Expr->getAsString());
298-
DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator());
299+
const DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator());
299300
if (!OpInit)
300301
PrintFatalError(Loc, "Bad set expression: " + Expr->getAsString());
301302
auto I = Operators.find(OpInit->getDef()->getName());
@@ -304,27 +305,26 @@ void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
304305
I->second->apply(*this, DagExpr, Elts, Loc);
305306
}
306307

307-
const RecVec *SetTheory::expand(Record *Set) {
308+
const RecVec *SetTheory::expand(const Record *Set) {
308309
// Check existing entries for Set and return early.
309310
ExpandMap::iterator I = Expansions.find(Set);
310311
if (I != Expansions.end())
311312
return &I->second;
312313

313314
// This is the first time we see Set. Find a suitable expander.
314-
ArrayRef<std::pair<Record *, SMRange>> SC = Set->getSuperClasses();
315-
for (const auto &SCPair : SC) {
315+
for (const auto &[SuperClass, Loc] : Set->getSuperClasses()) {
316316
// Skip unnamed superclasses.
317-
if (!isa<StringInit>(SCPair.first->getNameInit()))
317+
if (!isa<StringInit>(SuperClass->getNameInit()))
318318
continue;
319-
auto I = Expanders.find(SCPair.first->getName());
320-
if (I != Expanders.end()) {
321-
// This breaks recursive definitions.
322-
RecVec &EltVec = Expansions[Set];
323-
RecSet Elts;
324-
I->second->expand(*this, Set, Elts);
325-
EltVec.assign(Elts.begin(), Elts.end());
326-
return &EltVec;
327-
}
319+
auto I = Expanders.find(SuperClass->getName());
320+
if (I == Expanders.end())
321+
continue;
322+
// This breaks recursive definitions.
323+
RecVec &EltVec = Expansions[Set];
324+
RecSet Elts;
325+
I->second->expand(*this, Set, Elts);
326+
EltVec.assign(Elts.begin(), Elts.end());
327+
return &EltVec;
328328
}
329329

330330
// Set is not expandable.

llvm/utils/TableGen/Common/CodeGenRegisters.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ struct TupleExpander : SetTheory::Expander {
625625
TupleExpander(std::vector<std::unique_ptr<Record>> &SynthDefs)
626626
: SynthDefs(SynthDefs) {}
627627

628-
void expand(SetTheory &ST, Record *Def, SetTheory::RecSet &Elts) override {
628+
void expand(SetTheory &ST, const Record *Def,
629+
SetTheory::RecSet &Elts) override {
629630
std::vector<Record *> Indices = Def->getValueAsListOfDefs("SubRegIndices");
630631
unsigned Dim = Indices.size();
631632
ListInit *SubRegs = Def->getValueAsListInit("SubRegs");

llvm/utils/TableGen/Common/CodeGenSchedule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace {
4343

4444
// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
4545
struct InstrsOp : public SetTheory::Operator {
46-
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
46+
void apply(SetTheory &ST, const DagInit *Expr, SetTheory::RecSet &Elts,
4747
ArrayRef<SMLoc> Loc) override {
4848
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
4949
}
@@ -75,7 +75,7 @@ struct InstRegexOp : public SetTheory::Operator {
7575
return Result;
7676
}
7777

78-
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
78+
void apply(SetTheory &ST, const DagInit *Expr, SetTheory::RecSet &Elts,
7979
ArrayRef<SMLoc> Loc) override {
8080
ArrayRef<const CodeGenInstruction *> Instructions =
8181
Target.getInstructionsByEnumValue();

0 commit comments

Comments
 (0)