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 "SetTheory.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/Record.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 virtual void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
31 ArrayRef<SMLoc> Loc) {
32 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
36 // (sub Add, Sub, ...) Set difference.
37 struct SubOp : public SetTheory::Operator {
38 virtual void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
39 ArrayRef<SMLoc> Loc) {
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 virtual void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
55 ArrayRef<SMLoc> Loc) {
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,
71 RecSet &Set, int64_t N,
72 RecSet &Elts, ArrayRef<SMLoc> Loc) =0;
74 virtual void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
75 ArrayRef<SMLoc> Loc) {
76 if (Expr->arg_size() != 2)
77 PrintFatalError(Loc, "Operator requires (Op Set, Int) arguments: " +
80 ST.evaluate(Expr->arg_begin()[0], Set, Loc);
81 IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]);
83 PrintFatalError(Loc, "Second argument must be an integer: " +
85 apply2(ST, Expr, Set, II->getValue(), Elts, Loc);
89 // (shl S, N) Shift left, remove the first N elements.
90 struct ShlOp : public SetIntBinOp {
91 virtual void apply2(SetTheory &ST, DagInit *Expr,
92 RecSet &Set, int64_t N,
93 RecSet &Elts, ArrayRef<SMLoc> Loc) {
95 PrintFatalError(Loc, "Positive shift required: " +
97 if (unsigned(N) < Set.size())
98 Elts.insert(Set.begin() + N, Set.end());
102 // (trunc S, N) Truncate after the first N elements.
103 struct TruncOp : public SetIntBinOp {
104 virtual void apply2(SetTheory &ST, DagInit *Expr,
105 RecSet &Set, int64_t N,
106 RecSet &Elts, ArrayRef<SMLoc> Loc) {
108 PrintFatalError(Loc, "Positive length required: " +
109 Expr->getAsString());
110 if (unsigned(N) > Set.size())
112 Elts.insert(Set.begin(), Set.begin() + N);
116 // Left/right rotation.
117 struct RotOp : public SetIntBinOp {
120 RotOp(bool Rev) : Reverse(Rev) {}
122 virtual void apply2(SetTheory &ST, DagInit *Expr,
123 RecSet &Set, int64_t N,
124 RecSet &Elts, ArrayRef<SMLoc> Loc) {
127 // N > 0 -> rotate left, N < 0 -> rotate right.
131 N = Set.size() - (-N % Set.size());
134 Elts.insert(Set.begin() + N, Set.end());
135 Elts.insert(Set.begin(), Set.begin() + N);
139 // (decimate S, N) Pick every N'th element of S.
140 struct DecimateOp : public SetIntBinOp {
141 virtual void apply2(SetTheory &ST, DagInit *Expr,
142 RecSet &Set, int64_t N,
143 RecSet &Elts, ArrayRef<SMLoc> Loc) {
145 PrintFatalError(Loc, "Positive stride required: " +
146 Expr->getAsString());
147 for (unsigned I = 0; I < Set.size(); I += N)
152 // (interleave S1, S2, ...) Interleave elements of the arguments.
153 struct InterleaveOp : public SetTheory::Operator {
154 virtual void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
155 ArrayRef<SMLoc> Loc) {
156 // Evaluate the arguments individually.
157 SmallVector<RecSet, 4> Args(Expr->getNumArgs());
158 unsigned MaxSize = 0;
159 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
160 ST.evaluate(Expr->getArg(i), Args[i], Loc);
161 MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
163 // Interleave arguments into Elts.
164 for (unsigned n = 0; n != MaxSize; ++n)
165 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i)
166 if (n < Args[i].size())
167 Elts.insert(Args[i][n]);
171 // (sequence "Format", From, To) Generate a sequence of records by name.
172 struct SequenceOp : public SetTheory::Operator {
173 virtual void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
174 ArrayRef<SMLoc> Loc) {
176 if (Expr->arg_size() > 4)
177 PrintFatalError(Loc, "Bad args to (sequence \"Format\", From, To): " +
178 Expr->getAsString());
179 else if (Expr->arg_size() == 4) {
180 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[3])) {
181 Step = II->getValue();
183 PrintFatalError(Loc, "Stride must be an integer: " +
184 Expr->getAsString());
188 if (StringInit *SI = dyn_cast<StringInit>(Expr->arg_begin()[0]))
189 Format = SI->getValue();
191 PrintFatalError(Loc, "Format must be a string: " + Expr->getAsString());
194 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]))
195 From = II->getValue();
197 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
198 if (From < 0 || From >= (1 << 30))
199 PrintFatalError(Loc, "From out of range");
201 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[2]))
204 PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
205 if (To < 0 || To >= (1 << 30))
206 PrintFatalError(Loc, "To out of range");
208 RecordKeeper &Records =
209 cast<DefInit>(Expr->getOperator())->getDef()->getRecords();
211 Step *= From <= To ? 1 : -1;
213 if (Step > 0 && From > To)
215 else if (Step < 0 && From < To)
218 raw_string_ostream OS(Name);
219 OS << format(Format.c_str(), unsigned(From));
220 Record *Rec = Records.getDef(OS.str());
222 PrintFatalError(Loc, "No def named '" + Name + "': " +
223 Expr->getAsString());
224 // Try to reevaluate Rec in case it is a set.
225 if (const RecVec *Result = ST.expand(Rec))
226 Elts.insert(Result->begin(), Result->end());
235 // Expand a Def into a set by evaluating one of its fields.
236 struct FieldExpander : public SetTheory::Expander {
239 FieldExpander(StringRef fn) : FieldName(fn) {}
241 virtual void expand(SetTheory &ST, Record *Def, RecSet &Elts) {
242 ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
245 } // end anonymous namespace
247 // Pin the vtables to this file.
248 void SetTheory::Operator::anchor() {}
249 void SetTheory::Expander::anchor() {}
252 SetTheory::SetTheory() {
253 addOperator("add", new AddOp);
254 addOperator("sub", new SubOp);
255 addOperator("and", new AndOp);
256 addOperator("shl", new ShlOp);
257 addOperator("trunc", new TruncOp);
258 addOperator("rotl", new RotOp(false));
259 addOperator("rotr", new RotOp(true));
260 addOperator("decimate", new DecimateOp);
261 addOperator("interleave", new InterleaveOp);
262 addOperator("sequence", new SequenceOp);
265 void SetTheory::addOperator(StringRef Name, Operator *Op) {
266 Operators[Name] = Op;
269 void SetTheory::addExpander(StringRef ClassName, Expander *E) {
270 Expanders[ClassName] = E;
273 void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
274 addExpander(ClassName, new FieldExpander(FieldName));
277 void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
278 // A def in a list can be a just an element, or it may expand.
279 if (DefInit *Def = dyn_cast<DefInit>(Expr)) {
280 if (const RecVec *Result = expand(Def->getDef()))
281 return Elts.insert(Result->begin(), Result->end());
282 Elts.insert(Def->getDef());
286 // Lists simply expand.
287 if (ListInit *LI = dyn_cast<ListInit>(Expr))
288 return evaluate(LI->begin(), LI->end(), Elts, Loc);
290 // Anything else must be a DAG.
291 DagInit *DagExpr = dyn_cast<DagInit>(Expr);
293 PrintFatalError(Loc, "Invalid set element: " + Expr->getAsString());
294 DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator());
296 PrintFatalError(Loc, "Bad set expression: " + Expr->getAsString());
297 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
299 PrintFatalError(Loc, "Unknown set operator: " + Expr->getAsString());
300 Op->apply(*this, DagExpr, Elts, Loc);
303 const RecVec *SetTheory::expand(Record *Set) {
304 // Check existing entries for Set and return early.
305 ExpandMap::iterator I = Expansions.find(Set);
306 if (I != Expansions.end())
309 // This is the first time we see Set. Find a suitable expander.
310 const std::vector<Record*> &SC = Set->getSuperClasses();
311 for (unsigned i = 0, e = SC.size(); i != e; ++i) {
312 // Skip unnamed superclasses.
313 if (!dyn_cast<StringInit>(SC[i]->getNameInit()))
315 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
316 // This breaks recursive definitions.
317 RecVec &EltVec = Expansions[Set];
319 Exp->expand(*this, Set, Elts);
320 EltVec.assign(Elts.begin(), Elts.end());
325 // Set is not expandable.