13
13
14
14
#include " llvm/TableGen/SetTheory.h"
15
15
#include " llvm/ADT/ArrayRef.h"
16
+ #include " llvm/ADT/STLExtras.h"
16
17
#include " llvm/ADT/SmallVector.h"
17
18
#include " llvm/ADT/StringRef.h"
18
19
#include " llvm/Support/Casting.h"
@@ -36,15 +37,15 @@ using RecVec = SetTheory::RecVec;
36
37
37
38
// (add a, b, ...) Evaluate and union all arguments.
38
39
struct AddOp : public SetTheory ::Operator {
39
- void apply (SetTheory &ST, DagInit *Expr, RecSet &Elts,
40
+ void apply (SetTheory &ST, const DagInit *Expr, RecSet &Elts,
40
41
ArrayRef<SMLoc> Loc) override {
41
42
ST.evaluate (Expr->arg_begin (), Expr->arg_end (), Elts, Loc);
42
43
}
43
44
};
44
45
45
46
// (sub Add, Sub, ...) Set difference.
46
47
struct SubOp : public SetTheory ::Operator {
47
- void apply (SetTheory &ST, DagInit *Expr, RecSet &Elts,
48
+ void apply (SetTheory &ST, const DagInit *Expr, RecSet &Elts,
48
49
ArrayRef<SMLoc> Loc) override {
49
50
if (Expr->arg_size () < 2 )
50
51
PrintFatalError (Loc, " Set difference needs at least two arguments: " +
@@ -60,7 +61,7 @@ struct SubOp : public SetTheory::Operator {
60
61
61
62
// (and S1, S2) Set intersection.
62
63
struct AndOp : public SetTheory ::Operator {
63
- void apply (SetTheory &ST, DagInit *Expr, RecSet &Elts,
64
+ void apply (SetTheory &ST, const DagInit *Expr, RecSet &Elts,
64
65
ArrayRef<SMLoc> Loc) override {
65
66
if (Expr->arg_size () != 2 )
66
67
PrintFatalError (Loc, " Set intersection requires two arguments: " +
@@ -76,17 +77,17 @@ struct AndOp : public SetTheory::Operator {
76
77
77
78
// SetIntBinOp - Abstract base class for (Op S, N) operators.
78
79
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;
81
82
82
- void apply (SetTheory &ST, DagInit *Expr, RecSet &Elts,
83
+ void apply (SetTheory &ST, const DagInit *Expr, RecSet &Elts,
83
84
ArrayRef<SMLoc> Loc) override {
84
85
if (Expr->arg_size () != 2 )
85
86
PrintFatalError (Loc, " Operator requires (Op Set, Int) arguments: " +
86
87
Expr->getAsString ());
87
88
RecSet Set;
88
89
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 ]);
90
91
if (!II)
91
92
PrintFatalError (Loc, " Second argument must be an integer: " +
92
93
Expr->getAsString ());
@@ -96,7 +97,7 @@ struct SetIntBinOp : public SetTheory::Operator {
96
97
97
98
// (shl S, N) Shift left, remove the first N elements.
98
99
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,
100
101
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
101
102
if (N < 0 )
102
103
PrintFatalError (Loc, " Positive shift required: " +
@@ -108,7 +109,7 @@ struct ShlOp : public SetIntBinOp {
108
109
109
110
// (trunc S, N) Truncate after the first N elements.
110
111
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,
112
113
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
113
114
if (N < 0 )
114
115
PrintFatalError (Loc, " Positive length required: " +
@@ -125,7 +126,7 @@ struct RotOp : public SetIntBinOp {
125
126
126
127
RotOp (bool Rev) : Reverse(Rev) {}
127
128
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,
129
130
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
130
131
if (Reverse)
131
132
N = -N;
@@ -143,7 +144,7 @@ struct RotOp : public SetIntBinOp {
143
144
144
145
// (decimate S, N) Pick every N'th element of S.
145
146
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,
147
148
RecSet &Elts, ArrayRef<SMLoc> Loc) override {
148
149
if (N <= 0 )
149
150
PrintFatalError (Loc, " Positive stride required: " +
@@ -155,62 +156,62 @@ struct DecimateOp : public SetIntBinOp {
155
156
156
157
// (interleave S1, S2, ...) Interleave elements of the arguments.
157
158
struct InterleaveOp : public SetTheory ::Operator {
158
- void apply (SetTheory &ST, DagInit *Expr, RecSet &Elts,
159
+ void apply (SetTheory &ST, const DagInit *Expr, RecSet &Elts,
159
160
ArrayRef<SMLoc> Loc) override {
160
161
// Evaluate the arguments individually.
161
- SmallVector<RecSet, 4 > Args (Expr->getNumArgs ());
162
+ SmallVector<RecSet, 4 > Values (Expr->getNumArgs ());
162
163
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 ()));
166
167
}
167
168
// Interleave arguments into Elts.
168
169
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]);
172
173
}
173
174
};
174
175
175
176
// (sequence "Format", From, To) Generate a sequence of records by name.
176
177
struct SequenceOp : public SetTheory ::Operator {
177
- void apply (SetTheory &ST, DagInit *Expr, RecSet &Elts,
178
+ void apply (SetTheory &ST, const DagInit *Expr, RecSet &Elts,
178
179
ArrayRef<SMLoc> Loc) override {
179
180
int Step = 1 ;
180
181
if (Expr->arg_size () > 4 )
181
182
PrintFatalError (Loc, " Bad args to (sequence \" Format\" , From, To): " +
182
183
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 ]))
185
186
Step = II->getValue ();
186
- } else
187
+ else
187
188
PrintFatalError (Loc, " Stride must be an integer: " +
188
189
Expr->getAsString ());
189
190
}
190
191
191
192
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 ]))
193
194
Format = std::string (SI->getValue ());
194
195
else
195
196
PrintFatalError (Loc, " Format must be a string: " + Expr->getAsString ());
196
197
197
198
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 ]))
199
200
From = II->getValue ();
200
201
else
201
202
PrintFatalError (Loc, " From must be an integer: " + Expr->getAsString ());
202
203
if (From < 0 || From >= (1 << 30 ))
203
204
PrintFatalError (Loc, " From out of range" );
204
205
205
- if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin ()[2 ]))
206
+ if (const auto *II = dyn_cast<IntInit>(Expr->arg_begin ()[2 ]))
206
207
To = II->getValue ();
207
208
else
208
209
PrintFatalError (Loc, " To must be an integer: " + Expr->getAsString ());
209
210
if (To < 0 || To >= (1 << 30 ))
210
211
PrintFatalError (Loc, " To out of range" );
211
212
212
- RecordKeeper &Records =
213
- cast<DefInit>(Expr->getOperator ())->getDef ()->getRecords ();
213
+ const RecordKeeper &Records =
214
+ cast<DefInit>(Expr->getOperator ())->getDef ()->getRecords ();
214
215
215
216
Step *= From <= To ? 1 : -1 ;
216
217
while (true ) {
@@ -242,7 +243,7 @@ struct FieldExpander : public SetTheory::Expander {
242
243
243
244
FieldExpander (StringRef fn) : FieldName(fn) {}
244
245
245
- void expand (SetTheory &ST, Record *Def, RecSet &Elts) override {
246
+ void expand (SetTheory &ST, const Record *Def, RecSet &Elts) override {
246
247
ST.evaluate (Def->getValueInit (FieldName), Elts, Def->getLoc ());
247
248
}
248
249
};
@@ -278,24 +279,24 @@ void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
278
279
addExpander (ClassName, std::make_unique<FieldExpander>(FieldName));
279
280
}
280
281
281
- void SetTheory::evaluate (Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
282
+ void SetTheory::evaluate (const Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
282
283
// 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)) {
284
285
if (const RecVec *Result = expand (Def->getDef ()))
285
286
return Elts.insert (Result->begin (), Result->end ());
286
287
Elts.insert (Def->getDef ());
287
288
return ;
288
289
}
289
290
290
291
// Lists simply expand.
291
- if (ListInit *LI = dyn_cast<ListInit>(Expr))
292
+ if (const auto *LI = dyn_cast<ListInit>(Expr))
292
293
return evaluate (LI->begin (), LI->end (), Elts, Loc);
293
294
294
295
// Anything else must be a DAG.
295
- DagInit *DagExpr = dyn_cast<DagInit>(Expr);
296
+ const auto *DagExpr = dyn_cast<DagInit>(Expr);
296
297
if (!DagExpr)
297
298
PrintFatalError (Loc, " Invalid set element: " + Expr->getAsString ());
298
- DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator ());
299
+ const DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator ());
299
300
if (!OpInit)
300
301
PrintFatalError (Loc, " Bad set expression: " + Expr->getAsString ());
301
302
auto I = Operators.find (OpInit->getDef ()->getName ());
@@ -304,27 +305,26 @@ void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
304
305
I->second ->apply (*this , DagExpr, Elts, Loc);
305
306
}
306
307
307
- const RecVec *SetTheory::expand (Record *Set) {
308
+ const RecVec *SetTheory::expand (const Record *Set) {
308
309
// Check existing entries for Set and return early.
309
310
ExpandMap::iterator I = Expansions.find (Set);
310
311
if (I != Expansions.end ())
311
312
return &I->second ;
312
313
313
314
// 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 ()) {
316
316
// Skip unnamed superclasses.
317
- if (!isa<StringInit>(SCPair. first ->getNameInit ()))
317
+ if (!isa<StringInit>(SuperClass ->getNameInit ()))
318
318
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;
328
328
}
329
329
330
330
// Set is not expandable.
0 commit comments