llvm.org GIT mirror llvm / 3b2a256
Move SetTheory from utils/TableGen into lib/TableGen so Clang can use it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211100 91177308-0d34-0410-b5e6-96231b3b80d8 James Molloy 5 years ago
9 changed file(s) with 469 addition(s) and 469 deletion(s). Raw diff Collapse all Expand all
0 //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the SetTheory class that computes ordered sets of
10 // Records from DAG expressions. Operators for standard set operations are
11 // predefined, and it is possible to add special purpose set operators as well.
12 //
13 // The user may define named sets as Records of predefined classes. Set
14 // expanders can be added to a SetTheory instance to teach it how to find the
15 // elements of such a named set.
16 //
17 // These are the predefined operators. The argument lists can be individual
18 // elements (defs), other sets (defs of expandable classes), lists, or DAG
19 // expressions that are evaluated recursively.
20 //
21 // - (add S1, S2 ...) Union sets. This is also how sets are created from element
22 // lists.
23 //
24 // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
25 // elements in S2, ...
26 //
27 // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
28 //
29 // - (shl S, N) Shift left. Remove the first N elements from S.
30 //
31 // - (trunc S, N) Truncate. The first N elements of S.
32 //
33 // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
34 //
35 // - (rotr S, N) Rotate right.
36 //
37 // - (decimate S, N) Decimate S by picking every N'th element, starting with
38 // the first one. For instance, (decimate S, 2) returns the even elements of
39 // S.
40 //
41 // - (sequence "Format", From, To) Generate a sequence of defs with printf.
42 // For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ]
43 //
44 //===----------------------------------------------------------------------===//
45
46 #ifndef SETTHEORY_H
47 #define SETTHEORY_H
48
49 #include "llvm/ADT/SetVector.h"
50 #include "llvm/ADT/StringMap.h"
51 #include "llvm/Support/SourceMgr.h"
52 #include
53 #include
54
55 namespace llvm {
56
57 class DagInit;
58 class Init;
59 class Record;
60 class RecordKeeper;
61
62 class SetTheory {
63 public:
64 typedef std::vector RecVec;
65 typedef SmallSetVector RecSet;
66
67 /// Operator - A callback representing a DAG operator.
68 class Operator {
69 virtual void anchor();
70 public:
71 virtual ~Operator() {}
72
73 /// apply - Apply this operator to Expr's arguments and insert the result
74 /// in Elts.
75 virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
76 ArrayRef Loc) =0;
77 };
78
79 /// Expander - A callback function that can transform a Record representing a
80 /// set into a fully expanded list of elements. Expanders provide a way for
81 /// users to define named sets that can be used in DAG expressions.
82 class Expander {
83 virtual void anchor();
84 public:
85 virtual ~Expander() {}
86
87 virtual void expand(SetTheory&, Record*, RecSet &Elts) =0;
88 };
89
90 private:
91 // Map set defs to their fully expanded contents. This serves as a memoization
92 // cache and it makes it possible to return const references on queries.
93 typedef std::map ExpandMap;
94 ExpandMap Expansions;
95
96 // Known DAG operators by name.
97 StringMap Operators;
98
99 // Typed expanders by class name.
100 StringMap Expanders;
101
102 public:
103 /// Create a SetTheory instance with only the standard operators.
104 SetTheory();
105
106 /// addExpander - Add an expander for Records with the named super class.
107 void addExpander(StringRef ClassName, Expander*);
108
109 /// addFieldExpander - Add an expander for ClassName that simply evaluates
110 /// FieldName in the Record to get the set elements. That is all that is
111 /// needed for a class like:
112 ///
113 /// class Set {
114 /// dag Elts = d;
115 /// }
116 ///
117 void addFieldExpander(StringRef ClassName, StringRef FieldName);
118
119 /// addOperator - Add a DAG operator.
120 void addOperator(StringRef Name, Operator*);
121
122 /// evaluate - Evaluate Expr and append the resulting set to Elts.
123 void evaluate(Init *Expr, RecSet &Elts, ArrayRef Loc);
124
125 /// evaluate - Evaluate a sequence of Inits and append to Elts.
126 template
127 void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef Loc) {
128 while (begin != end)
129 evaluate(*begin++, Elts, Loc);
130 }
131
132 /// expand - Expand a record into a set of elements if possible. Return a
133 /// pointer to the expanded elements, or NULL if Set cannot be expanded
134 /// further.
135 const RecVec *expand(Record *Set);
136 };
137
138 } // end namespace llvm
139
140 #endif
141
11 Error.cpp
22 Main.cpp
33 Record.cpp
4 SetTheory.cpp
45 StringMatcher.cpp
56 TableGenBackend.cpp
67 TGLexer.cpp
0 //===- SetTheory.cpp - Generate ordered sets from DAG expressions ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the SetTheory class that computes ordered sets of
10 // Records from DAG expressions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Format.h"
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/TableGen/Record.h"
17 #include "llvm/TableGen/SetTheory.h"
18
19 using namespace llvm;
20
21 // Define the standard operators.
22 namespace {
23
24 typedef SetTheory::RecSet RecSet;
25 typedef SetTheory::RecVec RecVec;
26
27 // (add a, b, ...) Evaluate and union all arguments.
28 struct AddOp : public SetTheory::Operator {
29 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
30 ArrayRef Loc) override {
31 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
32 }
33 };
34
35 // (sub Add, Sub, ...) Set difference.
36 struct SubOp : public SetTheory::Operator {
37 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
38 ArrayRef Loc) override {
39 if (Expr->arg_size() < 2)
40 PrintFatalError(Loc, "Set difference needs at least two arguments: " +
41 Expr->getAsString());
42 RecSet Add, Sub;
43 ST.evaluate(*Expr->arg_begin(), Add, Loc);
44 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub, Loc);
45 for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
46 if (!Sub.count(*I))
47 Elts.insert(*I);
48 }
49 };
50
51 // (and S1, S2) Set intersection.
52 struct AndOp : public SetTheory::Operator {
53 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
54 ArrayRef Loc) override {
55 if (Expr->arg_size() != 2)
56 PrintFatalError(Loc, "Set intersection requires two arguments: " +
57 Expr->getAsString());
58 RecSet S1, S2;
59 ST.evaluate(Expr->arg_begin()[0], S1, Loc);
60 ST.evaluate(Expr->arg_begin()[1], S2, Loc);
61 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
62 if (S2.count(*I))
63 Elts.insert(*I);
64 }
65 };
66
67 // SetIntBinOp - Abstract base class for (Op S, N) operators.
68 struct SetIntBinOp : public SetTheory::Operator {
69 virtual void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
70 RecSet &Elts, ArrayRef Loc) = 0;
71
72 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
73 ArrayRef Loc) override {
74 if (Expr->arg_size() != 2)
75 PrintFatalError(Loc, "Operator requires (Op Set, Int) arguments: " +
76 Expr->getAsString());
77 RecSet Set;
78 ST.evaluate(Expr->arg_begin()[0], Set, Loc);
79 IntInit *II = dyn_cast(Expr->arg_begin()[1]);
80 if (!II)
81 PrintFatalError(Loc, "Second argument must be an integer: " +
82 Expr->getAsString());
83 apply2(ST, Expr, Set, II->getValue(), Elts, Loc);
84 }
85 };
86
87 // (shl S, N) Shift left, remove the first N elements.
88 struct ShlOp : public SetIntBinOp {
89 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
90 RecSet &Elts, ArrayRef Loc) override {
91 if (N < 0)
92 PrintFatalError(Loc, "Positive shift required: " +
93 Expr->getAsString());
94 if (unsigned(N) < Set.size())
95 Elts.insert(Set.begin() + N, Set.end());
96 }
97 };
98
99 // (trunc S, N) Truncate after the first N elements.
100 struct TruncOp : public SetIntBinOp {
101 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
102 RecSet &Elts, ArrayRef Loc) override {
103 if (N < 0)
104 PrintFatalError(Loc, "Positive length required: " +
105 Expr->getAsString());
106 if (unsigned(N) > Set.size())
107 N = Set.size();
108 Elts.insert(Set.begin(), Set.begin() + N);
109 }
110 };
111
112 // Left/right rotation.
113 struct RotOp : public SetIntBinOp {
114 const bool Reverse;
115
116 RotOp(bool Rev) : Reverse(Rev) {}
117
118 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
119 RecSet &Elts, ArrayRef Loc) override {
120 if (Reverse)
121 N = -N;
122 // N > 0 -> rotate left, N < 0 -> rotate right.
123 if (Set.empty())
124 return;
125 if (N < 0)
126 N = Set.size() - (-N % Set.size());
127 else
128 N %= Set.size();
129 Elts.insert(Set.begin() + N, Set.end());
130 Elts.insert(Set.begin(), Set.begin() + N);
131 }
132 };
133
134 // (decimate S, N) Pick every N'th element of S.
135 struct DecimateOp : public SetIntBinOp {
136 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
137 RecSet &Elts, ArrayRef Loc) override {
138 if (N <= 0)
139 PrintFatalError(Loc, "Positive stride required: " +
140 Expr->getAsString());
141 for (unsigned I = 0; I < Set.size(); I += N)
142 Elts.insert(Set[I]);
143 }
144 };
145
146 // (interleave S1, S2, ...) Interleave elements of the arguments.
147 struct InterleaveOp : public SetTheory::Operator {
148 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
149 ArrayRef Loc) override {
150 // Evaluate the arguments individually.
151 SmallVector Args(Expr->getNumArgs());
152 unsigned MaxSize = 0;
153 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
154 ST.evaluate(Expr->getArg(i), Args[i], Loc);
155 MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
156 }
157 // Interleave arguments into Elts.
158 for (unsigned n = 0; n != MaxSize; ++n)
159 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i)
160 if (n < Args[i].size())
161 Elts.insert(Args[i][n]);
162 }
163 };
164
165 // (sequence "Format", From, To) Generate a sequence of records by name.
166 struct SequenceOp : public SetTheory::Operator {
167 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
168 ArrayRef Loc) override {
169 int Step = 1;
170 if (Expr->arg_size() > 4)
171 PrintFatalError(Loc, "Bad args to (sequence \"Format\", From, To): " +
172 Expr->getAsString());
173 else if (Expr->arg_size() == 4) {
174 if (IntInit *II = dyn_cast(Expr->arg_begin()[3])) {
175 Step = II->getValue();
176 } else
177 PrintFatalError(Loc, "Stride must be an integer: " +
178 Expr->getAsString());
179 }
180
181 std::string Format;
182 if (StringInit *SI = dyn_cast(Expr->arg_begin()[0]))
183 Format = SI->getValue();
184 else
185 PrintFatalError(Loc, "Format must be a string: " + Expr->getAsString());
186
187 int64_t From, To;
188 if (IntInit *II = dyn_cast(Expr->arg_begin()[1]))
189 From = II->getValue();
190 else
191 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
192 if (From < 0 || From >= (1 << 30))
193 PrintFatalError(Loc, "From out of range");
194
195 if (IntInit *II = dyn_cast(Expr->arg_begin()[2]))
196 To = II->getValue();
197 else
198 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
199 if (To < 0 || To >= (1 << 30))
200 PrintFatalError(Loc, "To out of range");
201
202 RecordKeeper &Records =
203 cast(Expr->getOperator())->getDef()->getRecords();
204
205 Step *= From <= To ? 1 : -1;
206 while (true) {
207 if (Step > 0 && From > To)
208 break;
209 else if (Step < 0 && From < To)
210 break;
211 std::string Name;
212 raw_string_ostream OS(Name);
213 OS << format(Format.c_str(), unsigned(From));
214 Record *Rec = Records.getDef(OS.str());
215 if (!Rec)
216 PrintFatalError(Loc, "No def named '" + Name + "': " +
217 Expr->getAsString());
218 // Try to reevaluate Rec in case it is a set.
219 if (const RecVec *Result = ST.expand(Rec))
220 Elts.insert(Result->begin(), Result->end());
221 else
222 Elts.insert(Rec);
223
224 From += Step;
225 }
226 }
227 };
228
229 // Expand a Def into a set by evaluating one of its fields.
230 struct FieldExpander : public SetTheory::Expander {
231 StringRef FieldName;
232
233 FieldExpander(StringRef fn) : FieldName(fn) {}
234
235 void expand(SetTheory &ST, Record *Def, RecSet &Elts) override {
236 ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
237 }
238 };
239 } // end anonymous namespace
240
241 // Pin the vtables to this file.
242 void SetTheory::Operator::anchor() {}
243 void SetTheory::Expander::anchor() {}
244
245
246 SetTheory::SetTheory() {
247 addOperator("add", new AddOp);
248 addOperator("sub", new SubOp);
249 addOperator("and", new AndOp);
250 addOperator("shl", new ShlOp);
251 addOperator("trunc", new TruncOp);
252 addOperator("rotl", new RotOp(false));
253 addOperator("rotr", new RotOp(true));
254 addOperator("decimate", new DecimateOp);
255 addOperator("interleave", new InterleaveOp);
256 addOperator("sequence", new SequenceOp);
257 }
258
259 void SetTheory::addOperator(StringRef Name, Operator *Op) {
260 Operators[Name] = Op;
261 }
262
263 void SetTheory::addExpander(StringRef ClassName, Expander *E) {
264 Expanders[ClassName] = E;
265 }
266
267 void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
268 addExpander(ClassName, new FieldExpander(FieldName));
269 }
270
271 void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef Loc) {
272 // A def in a list can be a just an element, or it may expand.
273 if (DefInit *Def = dyn_cast(Expr)) {
274 if (const RecVec *Result = expand(Def->getDef()))
275 return Elts.insert(Result->begin(), Result->end());
276 Elts.insert(Def->getDef());
277 return;
278 }
279
280 // Lists simply expand.
281 if (ListInit *LI = dyn_cast(Expr))
282 return evaluate(LI->begin(), LI->end(), Elts, Loc);
283
284 // Anything else must be a DAG.
285 DagInit *DagExpr = dyn_cast(Expr);
286 if (!DagExpr)
287 PrintFatalError(Loc, "Invalid set element: " + Expr->getAsString());
288 DefInit *OpInit = dyn_cast(DagExpr->getOperator());
289 if (!OpInit)
290 PrintFatalError(Loc, "Bad set expression: " + Expr->getAsString());
291 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
292 if (!Op)
293 PrintFatalError(Loc, "Unknown set operator: " + Expr->getAsString());
294 Op->apply(*this, DagExpr, Elts, Loc);
295 }
296
297 const RecVec *SetTheory::expand(Record *Set) {
298 // Check existing entries for Set and return early.
299 ExpandMap::iterator I = Expansions.find(Set);
300 if (I != Expansions.end())
301 return &I->second;
302
303 // This is the first time we see Set. Find a suitable expander.
304 const std::vector &SC = Set->getSuperClasses();
305 for (unsigned i = 0, e = SC.size(); i != e; ++i) {
306 // Skip unnamed superclasses.
307 if (!dyn_cast(SC[i]->getNameInit()))
308 continue;
309 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
310 // This breaks recursive definitions.
311 RecVec &EltVec = Expansions[Set];
312 RecSet Elts;
313 Exp->expand(*this, Set, Elts);
314 EltVec.assign(Elts.begin(), Elts.end());
315 return &EltVec;
316 }
317 }
318
319 // Set is not expandable.
320 return nullptr;
321 }
322
2525 OptParserEmitter.cpp
2626 PseudoLoweringEmitter.cpp
2727 RegisterInfoEmitter.cpp
28 SetTheory.cpp
2928 SubtargetEmitter.cpp
3029 TableGen.cpp
3130 X86DisassemblerTables.cpp
1414 #ifndef CODEGEN_REGISTERS_H
1515 #define CODEGEN_REGISTERS_H
1616
17 #include "SetTheory.h"
1817 #include "llvm/ADT/ArrayRef.h"
1918 #include "llvm/ADT/BitVector.h"
2019 #include "llvm/ADT/DenseMap.h"
2221 #include "llvm/CodeGen/MachineValueType.h"
2322 #include "llvm/Support/ErrorHandling.h"
2423 #include "llvm/TableGen/Record.h"
24 #include "llvm/TableGen/SetTheory.h"
2525 #include
2626 #include
2727 #include
1414 #ifndef CODEGEN_SCHEDULE_H
1515 #define CODEGEN_SCHEDULE_H
1616
17 #include "SetTheory.h"
1817 #include "llvm/ADT/DenseMap.h"
1918 #include "llvm/ADT/StringMap.h"
2019 #include "llvm/Support/ErrorHandling.h"
2120 #include "llvm/TableGen/Record.h"
21 #include "llvm/TableGen/SetTheory.h"
2222
2323 namespace llvm {
2424
+0
-323
utils/TableGen/SetTheory.cpp less more
None //===- SetTheory.cpp - Generate ordered sets from DAG expressions ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the SetTheory class that computes ordered sets of
10 // Records from DAG expressions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SetTheory.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
18
19 using namespace llvm;
20
21 // Define the standard operators.
22 namespace {
23
24 typedef SetTheory::RecSet RecSet;
25 typedef SetTheory::RecVec RecVec;
26
27 // (add a, b, ...) Evaluate and union all arguments.
28 struct AddOp : public SetTheory::Operator {
29 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
30 ArrayRef Loc) override {
31 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
32 }
33 };
34
35 // (sub Add, Sub, ...) Set difference.
36 struct SubOp : public SetTheory::Operator {
37 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
38 ArrayRef Loc) override {
39 if (Expr->arg_size() < 2)
40 PrintFatalError(Loc, "Set difference needs at least two arguments: " +
41 Expr->getAsString());
42 RecSet Add, Sub;
43 ST.evaluate(*Expr->arg_begin(), Add, Loc);
44 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub, Loc);
45 for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
46 if (!Sub.count(*I))
47 Elts.insert(*I);
48 }
49 };
50
51 // (and S1, S2) Set intersection.
52 struct AndOp : public SetTheory::Operator {
53 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
54 ArrayRef Loc) override {
55 if (Expr->arg_size() != 2)
56 PrintFatalError(Loc, "Set intersection requires two arguments: " +
57 Expr->getAsString());
58 RecSet S1, S2;
59 ST.evaluate(Expr->arg_begin()[0], S1, Loc);
60 ST.evaluate(Expr->arg_begin()[1], S2, Loc);
61 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
62 if (S2.count(*I))
63 Elts.insert(*I);
64 }
65 };
66
67 // SetIntBinOp - Abstract base class for (Op S, N) operators.
68 struct SetIntBinOp : public SetTheory::Operator {
69 virtual void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
70 RecSet &Elts, ArrayRef Loc) = 0;
71
72 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
73 ArrayRef Loc) override {
74 if (Expr->arg_size() != 2)
75 PrintFatalError(Loc, "Operator requires (Op Set, Int) arguments: " +
76 Expr->getAsString());
77 RecSet Set;
78 ST.evaluate(Expr->arg_begin()[0], Set, Loc);
79 IntInit *II = dyn_cast(Expr->arg_begin()[1]);
80 if (!II)
81 PrintFatalError(Loc, "Second argument must be an integer: " +
82 Expr->getAsString());
83 apply2(ST, Expr, Set, II->getValue(), Elts, Loc);
84 }
85 };
86
87 // (shl S, N) Shift left, remove the first N elements.
88 struct ShlOp : public SetIntBinOp {
89 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
90 RecSet &Elts, ArrayRef Loc) override {
91 if (N < 0)
92 PrintFatalError(Loc, "Positive shift required: " +
93 Expr->getAsString());
94 if (unsigned(N) < Set.size())
95 Elts.insert(Set.begin() + N, Set.end());
96 }
97 };
98
99 // (trunc S, N) Truncate after the first N elements.
100 struct TruncOp : public SetIntBinOp {
101 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
102 RecSet &Elts, ArrayRef Loc) override {
103 if (N < 0)
104 PrintFatalError(Loc, "Positive length required: " +
105 Expr->getAsString());
106 if (unsigned(N) > Set.size())
107 N = Set.size();
108 Elts.insert(Set.begin(), Set.begin() + N);
109 }
110 };
111
112 // Left/right rotation.
113 struct RotOp : public SetIntBinOp {
114 const bool Reverse;
115
116 RotOp(bool Rev) : Reverse(Rev) {}
117
118 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
119 RecSet &Elts, ArrayRef Loc) override {
120 if (Reverse)
121 N = -N;
122 // N > 0 -> rotate left, N < 0 -> rotate right.
123 if (Set.empty())
124 return;
125 if (N < 0)
126 N = Set.size() - (-N % Set.size());
127 else
128 N %= Set.size();
129 Elts.insert(Set.begin() + N, Set.end());
130 Elts.insert(Set.begin(), Set.begin() + N);
131 }
132 };
133
134 // (decimate S, N) Pick every N'th element of S.
135 struct DecimateOp : public SetIntBinOp {
136 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
137 RecSet &Elts, ArrayRef Loc) override {
138 if (N <= 0)
139 PrintFatalError(Loc, "Positive stride required: " +
140 Expr->getAsString());
141 for (unsigned I = 0; I < Set.size(); I += N)
142 Elts.insert(Set[I]);
143 }
144 };
145
146 // (interleave S1, S2, ...) Interleave elements of the arguments.
147 struct InterleaveOp : public SetTheory::Operator {
148 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
149 ArrayRef Loc) override {
150 // Evaluate the arguments individually.
151 SmallVector Args(Expr->getNumArgs());
152 unsigned MaxSize = 0;
153 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
154 ST.evaluate(Expr->getArg(i), Args[i], Loc);
155 MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
156 }
157 // Interleave arguments into Elts.
158 for (unsigned n = 0; n != MaxSize; ++n)
159 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i)
160 if (n < Args[i].size())
161 Elts.insert(Args[i][n]);
162 }
163 };
164
165 // (sequence "Format", From, To) Generate a sequence of records by name.
166 struct SequenceOp : public SetTheory::Operator {
167 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
168 ArrayRef Loc) override {
169 int Step = 1;
170 if (Expr->arg_size() > 4)
171 PrintFatalError(Loc, "Bad args to (sequence \"Format\", From, To): " +
172 Expr->getAsString());
173 else if (Expr->arg_size() == 4) {
174 if (IntInit *II = dyn_cast(Expr->arg_begin()[3])) {
175 Step = II->getValue();
176 } else
177 PrintFatalError(Loc, "Stride must be an integer: " +
178 Expr->getAsString());
179 }
180
181 std::string Format;
182 if (StringInit *SI = dyn_cast(Expr->arg_begin()[0]))
183 Format = SI->getValue();
184 else
185 PrintFatalError(Loc, "Format must be a string: " + Expr->getAsString());
186
187 int64_t From, To;
188 if (IntInit *II = dyn_cast(Expr->arg_begin()[1]))
189 From = II->getValue();
190 else
191 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
192 if (From < 0 || From >= (1 << 30))
193 PrintFatalError(Loc, "From out of range");
194
195 if (IntInit *II = dyn_cast(Expr->arg_begin()[2]))
196 To = II->getValue();
197 else
198 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
199 if (To < 0 || To >= (1 << 30))
200 PrintFatalError(Loc, "To out of range");
201
202 RecordKeeper &Records =
203 cast(Expr->getOperator())->getDef()->getRecords();
204
205 Step *= From <= To ? 1 : -1;
206 while (true) {
207 if (Step > 0 && From > To)
208 break;
209 else if (Step < 0 && From < To)
210 break;
211 std::string Name;
212 raw_string_ostream OS(Name);
213 OS << format(Format.c_str(), unsigned(From));
214 Record *Rec = Records.getDef(OS.str());
215 if (!Rec)
216 PrintFatalError(Loc, "No def named '" + Name + "': " +
217 Expr->getAsString());
218 // Try to reevaluate Rec in case it is a set.
219 if (const RecVec *Result = ST.expand(Rec))
220 Elts.insert(Result->begin(), Result->end());
221 else
222 Elts.insert(Rec);
223
224 From += Step;
225 }
226 }
227 };
228
229 // Expand a Def into a set by evaluating one of its fields.
230 struct FieldExpander : public SetTheory::Expander {
231 StringRef FieldName;
232
233 FieldExpander(StringRef fn) : FieldName(fn) {}
234
235 void expand(SetTheory &ST, Record *Def, RecSet &Elts) override {
236 ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
237 }
238 };
239 } // end anonymous namespace
240
241 // Pin the vtables to this file.
242 void SetTheory::Operator::anchor() {}
243 void SetTheory::Expander::anchor() {}
244
245
246 SetTheory::SetTheory() {
247 addOperator("add", new AddOp);
248 addOperator("sub", new SubOp);
249 addOperator("and", new AndOp);
250 addOperator("shl", new ShlOp);
251 addOperator("trunc", new TruncOp);
252 addOperator("rotl", new RotOp(false));
253 addOperator("rotr", new RotOp(true));
254 addOperator("decimate", new DecimateOp);
255 addOperator("interleave", new InterleaveOp);
256 addOperator("sequence", new SequenceOp);
257 }
258
259 void SetTheory::addOperator(StringRef Name, Operator *Op) {
260 Operators[Name] = Op;
261 }
262
263 void SetTheory::addExpander(StringRef ClassName, Expander *E) {
264 Expanders[ClassName] = E;
265 }
266
267 void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
268 addExpander(ClassName, new FieldExpander(FieldName));
269 }
270
271 void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef Loc) {
272 // A def in a list can be a just an element, or it may expand.
273 if (DefInit *Def = dyn_cast(Expr)) {
274 if (const RecVec *Result = expand(Def->getDef()))
275 return Elts.insert(Result->begin(), Result->end());
276 Elts.insert(Def->getDef());
277 return;
278 }
279
280 // Lists simply expand.
281 if (ListInit *LI = dyn_cast(Expr))
282 return evaluate(LI->begin(), LI->end(), Elts, Loc);
283
284 // Anything else must be a DAG.
285 DagInit *DagExpr = dyn_cast(Expr);
286 if (!DagExpr)
287 PrintFatalError(Loc, "Invalid set element: " + Expr->getAsString());
288 DefInit *OpInit = dyn_cast(DagExpr->getOperator());
289 if (!OpInit)
290 PrintFatalError(Loc, "Bad set expression: " + Expr->getAsString());
291 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
292 if (!Op)
293 PrintFatalError(Loc, "Unknown set operator: " + Expr->getAsString());
294 Op->apply(*this, DagExpr, Elts, Loc);
295 }
296
297 const RecVec *SetTheory::expand(Record *Set) {
298 // Check existing entries for Set and return early.
299 ExpandMap::iterator I = Expansions.find(Set);
300 if (I != Expansions.end())
301 return &I->second;
302
303 // This is the first time we see Set. Find a suitable expander.
304 const std::vector &SC = Set->getSuperClasses();
305 for (unsigned i = 0, e = SC.size(); i != e; ++i) {
306 // Skip unnamed superclasses.
307 if (!dyn_cast(SC[i]->getNameInit()))
308 continue;
309 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
310 // This breaks recursive definitions.
311 RecVec &EltVec = Expansions[Set];
312 RecSet Elts;
313 Exp->expand(*this, Set, Elts);
314 EltVec.assign(Elts.begin(), Elts.end());
315 return &EltVec;
316 }
317 }
318
319 // Set is not expandable.
320 return nullptr;
321 }
322
+0
-142
utils/TableGen/SetTheory.h less more
None //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the SetTheory class that computes ordered sets of
10 // Records from DAG expressions. Operators for standard set operations are
11 // predefined, and it is possible to add special purpose set operators as well.
12 //
13 // The user may define named sets as Records of predefined classes. Set
14 // expanders can be added to a SetTheory instance to teach it how to find the
15 // elements of such a named set.
16 //
17 // These are the predefined operators. The argument lists can be individual
18 // elements (defs), other sets (defs of expandable classes), lists, or DAG
19 // expressions that are evaluated recursively.
20 //
21 // - (add S1, S2 ...) Union sets. This is also how sets are created from element
22 // lists.
23 //
24 // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
25 // elements in S2, ...
26 //
27 // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
28 //
29 // - (shl S, N) Shift left. Remove the first N elements from S.
30 //
31 // - (trunc S, N) Truncate. The first N elements of S.
32 //
33 // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
34 //
35 // - (rotr S, N) Rotate right.
36 //
37 // - (decimate S, N) Decimate S by picking every N'th element, starting with
38 // the first one. For instance, (decimate S, 2) returns the even elements of
39 // S.
40 //
41 // - (sequence "Format", From, To) Generate a sequence of defs with printf.
42 // For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ]
43 //
44 //===----------------------------------------------------------------------===//
45
46 #ifndef SETTHEORY_H
47 #define SETTHEORY_H
48
49 #include "llvm/ADT/SetVector.h"
50 #include "llvm/ADT/StringMap.h"
51 #include "llvm/Support/SourceMgr.h"
52 #include
53 #include
54
55 namespace llvm {
56
57 class DagInit;
58 class Init;
59 class Record;
60 class RecordKeeper;
61
62 class SetTheory {
63 public:
64 typedef std::vector RecVec;
65 typedef SmallSetVector RecSet;
66
67 /// Operator - A callback representing a DAG operator.
68 class Operator {
69 virtual void anchor();
70 public:
71 virtual ~Operator() {}
72
73 /// apply - Apply this operator to Expr's arguments and insert the result
74 /// in Elts.
75 virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
76 ArrayRef Loc) =0;
77 };
78
79 /// Expander - A callback function that can transform a Record representing a
80 /// set into a fully expanded list of elements. Expanders provide a way for
81 /// users to define named sets that can be used in DAG expressions.
82 class Expander {
83 virtual void anchor();
84 public:
85 virtual ~Expander() {}
86
87 virtual void expand(SetTheory&, Record*, RecSet &Elts) =0;
88 };
89
90 private:
91 // Map set defs to their fully expanded contents. This serves as a memoization
92 // cache and it makes it possible to return const references on queries.
93 typedef std::map ExpandMap;
94 ExpandMap Expansions;
95
96 // Known DAG operators by name.
97 StringMap Operators;
98
99 // Typed expanders by class name.
100 StringMap Expanders;
101
102 public:
103 /// Create a SetTheory instance with only the standard operators.
104 SetTheory();
105
106 /// addExpander - Add an expander for Records with the named super class.
107 void addExpander(StringRef ClassName, Expander*);
108
109 /// addFieldExpander - Add an expander for ClassName that simply evaluates
110 /// FieldName in the Record to get the set elements. That is all that is
111 /// needed for a class like:
112 ///
113 /// class Set {
114 /// dag Elts = d;
115 /// }
116 ///
117 void addFieldExpander(StringRef ClassName, StringRef FieldName);
118
119 /// addOperator - Add a DAG operator.
120 void addOperator(StringRef Name, Operator*);
121
122 /// evaluate - Evaluate Expr and append the resulting set to Elts.
123 void evaluate(Init *Expr, RecSet &Elts, ArrayRef Loc);
124
125 /// evaluate - Evaluate a sequence of Inits and append to Elts.
126 template
127 void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef Loc) {
128 while (begin != end)
129 evaluate(*begin++, Elts, Loc);
130 }
131
132 /// expand - Expand a record into a set of elements if possible. Return a
133 /// pointer to the expanded elements, or NULL if Set cannot be expanded
134 /// further.
135 const RecVec *expand(Record *Set);
136 };
137
138 } // end namespace llvm
139
140 #endif
141
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "TableGenBackends.h" // Declares all backends.
14 #include "SetTheory.h"
1514 #include "llvm/Support/CommandLine.h"
1615 #include "llvm/Support/PrettyStackTrace.h"
1716 #include "llvm/Support/Signals.h"
1817 #include "llvm/TableGen/Error.h"
1918 #include "llvm/TableGen/Main.h"
2019 #include "llvm/TableGen/Record.h"
20 #include "llvm/TableGen/SetTheory.h"
2121
2222 using namespace llvm;
2323