1 //===- SetTheory.cpp - Generate ordered sets from DAG expressions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SetTheory class that computes ordered sets of
11 // Records from DAG expressions.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/Format.h"
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/TableGen/SetTheory.h"
22 // Define the standard operators.
25 typedef SetTheory::RecSet RecSet;
26 typedef SetTheory::RecVec RecVec;
28 // (add a, b, ...) Evaluate and union all arguments.
29 struct AddOp : public SetTheory::Operator {
30 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
31 ArrayRef<SMLoc> Loc) override {
32 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
36 // (sub Add, Sub, ...) Set difference.
37 struct SubOp : public SetTheory::Operator {
38 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
39 ArrayRef<SMLoc> Loc) override {
40 if (Expr->arg_size() < 2)
41 PrintFatalError(Loc, "Set difference needs at least two arguments: " +
44 ST.evaluate(*Expr->arg_begin(), Add, Loc);
45 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub, Loc);
46 for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
52 // (and S1, S2) Set intersection.
53 struct AndOp : public SetTheory::Operator {
54 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
55 ArrayRef<SMLoc> Loc) override {
56 if (Expr->arg_size() != 2)
57 PrintFatalError(Loc, "Set intersection requires two arguments: " +
60 ST.evaluate(Expr->arg_begin()[0], S1, Loc);
61 ST.evaluate(Expr->arg_begin()[1], S2, Loc);
62 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
68 // SetIntBinOp - Abstract base class for (Op S, N) operators.
69 struct SetIntBinOp : public SetTheory::Operator {
70 virtual void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
71 RecSet &Elts, ArrayRef<SMLoc> Loc) = 0;
73 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
74 ArrayRef<SMLoc> Loc) override {
75 if (Expr->arg_size() != 2)
76 PrintFatalError(Loc, "Operator requires (Op Set, Int) arguments: " +
79 ST.evaluate(Expr->arg_begin()[0], Set, Loc);
80 IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]);
82 PrintFatalError(Loc, "Second argument must be an integer: " +
84 apply2(ST, Expr, Set, II->getValue(), Elts, Loc);
88 // (shl S, N) Shift left, remove the first N elements.
89 struct ShlOp : public SetIntBinOp {
90 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
91 RecSet &Elts, ArrayRef<SMLoc> Loc) override {
93 PrintFatalError(Loc, "Positive shift required: " +
95 if (unsigned(N) < Set.size())
96 Elts.insert(Set.begin() + N, Set.end());
100 // (trunc S, N) Truncate after the first N elements.
101 struct TruncOp : public SetIntBinOp {
102 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
103 RecSet &Elts, ArrayRef<SMLoc> Loc) override {
105 PrintFatalError(Loc, "Positive length required: " +
106 Expr->getAsString());
107 if (unsigned(N) > Set.size())
109 Elts.insert(Set.begin(), Set.begin() + N);
113 // Left/right rotation.
114 struct RotOp : public SetIntBinOp {
117 RotOp(bool Rev) : Reverse(Rev) {}
119 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
120 RecSet &Elts, ArrayRef<SMLoc> Loc) override {
123 // N > 0 -> rotate left, N < 0 -> rotate right.
127 N = Set.size() - (-N % Set.size());
130 Elts.insert(Set.begin() + N, Set.end());
131 Elts.insert(Set.begin(), Set.begin() + N);
135 // (decimate S, N) Pick every N'th element of S.
136 struct DecimateOp : public SetIntBinOp {
137 void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
138 RecSet &Elts, ArrayRef<SMLoc> Loc) override {
140 PrintFatalError(Loc, "Positive stride required: " +
141 Expr->getAsString());
142 for (unsigned I = 0; I < Set.size(); I += N)
147 // (interleave S1, S2, ...) Interleave elements of the arguments.
148 struct InterleaveOp : public SetTheory::Operator {
149 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
150 ArrayRef<SMLoc> Loc) override {
151 // Evaluate the arguments individually.
152 SmallVector<RecSet, 4> Args(Expr->getNumArgs());
153 unsigned MaxSize = 0;
154 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
155 ST.evaluate(Expr->getArg(i), Args[i], Loc);
156 MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
158 // Interleave arguments into Elts.
159 for (unsigned n = 0; n != MaxSize; ++n)
160 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i)
161 if (n < Args[i].size())
162 Elts.insert(Args[i][n]);
166 // (sequence "Format", From, To) Generate a sequence of records by name.
167 struct SequenceOp : public SetTheory::Operator {
168 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
169 ArrayRef<SMLoc> Loc) override {
171 if (Expr->arg_size() > 4)
172 PrintFatalError(Loc, "Bad args to (sequence \"Format\", From, To): " +
173 Expr->getAsString());
174 else if (Expr->arg_size() == 4) {
175 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[3])) {
176 Step = II->getValue();
178 PrintFatalError(Loc, "Stride must be an integer: " +
179 Expr->getAsString());
183 if (StringInit *SI = dyn_cast<StringInit>(Expr->arg_begin()[0]))
184 Format = SI->getValue();
186 PrintFatalError(Loc, "Format must be a string: " + Expr->getAsString());
189 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]))
190 From = II->getValue();
192 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
193 if (From < 0 || From >= (1 << 30))
194 PrintFatalError(Loc, "From out of range");
196 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[2]))
199 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
200 if (To < 0 || To >= (1 << 30))
201 PrintFatalError(Loc, "To out of range");
203 RecordKeeper &Records =
204 cast<DefInit>(Expr->getOperator())->getDef()->getRecords();
206 Step *= From <= To ? 1 : -1;
208 if (Step > 0 && From > To)
210 else if (Step < 0 && From < To)
213 raw_string_ostream OS(Name);
214 OS << format(Format.c_str(), unsigned(From));
215 Record *Rec = Records.getDef(OS.str());
217 PrintFatalError(Loc, "No def named '" + Name + "': " +
218 Expr->getAsString());
219 // Try to reevaluate Rec in case it is a set.
220 if (const RecVec *Result = ST.expand(Rec))
221 Elts.insert(Result->begin(), Result->end());
230 // Expand a Def into a set by evaluating one of its fields.
231 struct FieldExpander : public SetTheory::Expander {
234 FieldExpander(StringRef fn) : FieldName(fn) {}
236 void expand(SetTheory &ST, Record *Def, RecSet &Elts) override {
237 ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
240 } // end anonymous namespace
242 // Pin the vtables to this file.
243 void SetTheory::Operator::anchor() {}
244 void SetTheory::Expander::anchor() {}
247 SetTheory::SetTheory() {
248 addOperator("add", new AddOp);
249 addOperator("sub", new SubOp);
250 addOperator("and", new AndOp);
251 addOperator("shl", new ShlOp);
252 addOperator("trunc", new TruncOp);
253 addOperator("rotl", new RotOp(false));
254 addOperator("rotr", new RotOp(true));
255 addOperator("decimate", new DecimateOp);
256 addOperator("interleave", new InterleaveOp);
257 addOperator("sequence", new SequenceOp);
260 void SetTheory::addOperator(StringRef Name, Operator *Op) {
261 Operators[Name] = Op;
264 void SetTheory::addExpander(StringRef ClassName, Expander *E) {
265 Expanders[ClassName] = E;
268 void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
269 addExpander(ClassName, new FieldExpander(FieldName));
272 void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
273 // A def in a list can be a just an element, or it may expand.
274 if (DefInit *Def = dyn_cast<DefInit>(Expr)) {
275 if (const RecVec *Result = expand(Def->getDef()))
276 return Elts.insert(Result->begin(), Result->end());
277 Elts.insert(Def->getDef());
281 // Lists simply expand.
282 if (ListInit *LI = dyn_cast<ListInit>(Expr))
283 return evaluate(LI->begin(), LI->end(), Elts, Loc);
285 // Anything else must be a DAG.
286 DagInit *DagExpr = dyn_cast<DagInit>(Expr);
288 PrintFatalError(Loc, "Invalid set element: " + Expr->getAsString());
289 DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator());
291 PrintFatalError(Loc, "Bad set expression: " + Expr->getAsString());
292 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
294 PrintFatalError(Loc, "Unknown set operator: " + Expr->getAsString());
295 Op->apply(*this, DagExpr, Elts, Loc);
298 const RecVec *SetTheory::expand(Record *Set) {
299 // Check existing entries for Set and return early.
300 ExpandMap::iterator I = Expansions.find(Set);
301 if (I != Expansions.end())
304 // This is the first time we see Set. Find a suitable expander.
305 const std::vector<Record*> &SC = Set->getSuperClasses();
306 for (unsigned i = 0, e = SC.size(); i != e; ++i) {
307 // Skip unnamed superclasses.
308 if (!dyn_cast<StringInit>(SC[i]->getNameInit()))
310 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
311 // This breaks recursive definitions.
312 RecVec &EltVec = Expansions[Set];
314 Exp->expand(*this, Set, Elts);
315 EltVec.assign(Elts.begin(), Elts.end());
320 // Set is not expandable.