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"
17 #include "llvm/Support/Format.h"
21 // Define the standard operators.
24 typedef SetTheory::RecSet RecSet;
25 typedef SetTheory::RecVec RecVec;
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 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts);
34 // (sub Add, Sub, ...) Set difference.
35 struct SubOp : public SetTheory::Operator {
36 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
37 if (Expr->arg_size() < 2)
38 throw "Set difference needs at least two arguments: " +
41 ST.evaluate(*Expr->arg_begin(), Add);
42 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub);
43 for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
49 // (and S1, S2) Set intersection.
50 struct AndOp : public SetTheory::Operator {
51 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
52 if (Expr->arg_size() != 2)
53 throw "Set intersection requires two arguments: " + Expr->getAsString();
55 ST.evaluate(Expr->arg_begin()[0], S1);
56 ST.evaluate(Expr->arg_begin()[1], S2);
57 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
63 // SetIntBinOp - Abstract base class for (Op S, N) operators.
64 struct SetIntBinOp : public SetTheory::Operator {
65 virtual void apply2(SetTheory &ST, DagInit *Expr,
66 RecSet &Set, int64_t N,
69 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
70 if (Expr->arg_size() != 2)
71 throw "Operator requires (Op Set, Int) arguments: " + Expr->getAsString();
73 ST.evaluate(Expr->arg_begin()[0], Set);
74 IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]);
76 throw "Second argument must be an integer: " + Expr->getAsString();
77 apply2(ST, Expr, Set, II->getValue(), Elts);
81 // (shl S, N) Shift left, remove the first N elements.
82 struct ShlOp : public SetIntBinOp {
83 void apply2(SetTheory &ST, DagInit *Expr,
84 RecSet &Set, int64_t N,
87 throw "Positive shift required: " + Expr->getAsString();
88 if (unsigned(N) < Set.size())
89 Elts.insert(Set.begin() + N, Set.end());
93 // (trunc S, N) Truncate after the first N elements.
94 struct TruncOp : public SetIntBinOp {
95 void apply2(SetTheory &ST, DagInit *Expr,
96 RecSet &Set, int64_t N,
99 throw "Positive length required: " + Expr->getAsString();
100 if (unsigned(N) > Set.size())
102 Elts.insert(Set.begin(), Set.begin() + N);
106 // Left/right rotation.
107 struct RotOp : public SetIntBinOp {
110 RotOp(bool Rev) : Reverse(Rev) {}
112 void apply2(SetTheory &ST, DagInit *Expr,
113 RecSet &Set, int64_t N,
117 // N > 0 -> rotate left, N < 0 -> rotate right.
121 N = Set.size() - (-N % Set.size());
124 Elts.insert(Set.begin() + N, Set.end());
125 Elts.insert(Set.begin(), Set.begin() + N);
129 // (decimate S, N) Pick every N'th element of S.
130 struct DecimateOp : public SetIntBinOp {
131 void apply2(SetTheory &ST, DagInit *Expr,
132 RecSet &Set, int64_t N,
135 throw "Positive stride required: " + Expr->getAsString();
136 for (unsigned I = 0; I < Set.size(); I += N)
141 // (sequence "Format", From, To) Generate a sequence of records by name.
142 struct SequenceOp : public SetTheory::Operator {
143 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
144 if (Expr->arg_size() != 3)
145 throw "Bad args to (sequence \"Format\", From, To): " +
148 if (StringInit *SI = dynamic_cast<StringInit*>(Expr->arg_begin()[0]))
149 Format = SI->getValue();
151 throw "Format must be a string: " + Expr->getAsString();
154 if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]))
155 From = II->getValue();
157 throw "From must be an integer: " + Expr->getAsString();
158 if (From < 0 || From >= UINT_MAX)
159 throw "From out of range";
161 if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[2]))
164 throw "From must be an integer: " + Expr->getAsString();
165 if (To < 0 || To >= UINT_MAX)
166 throw "To out of range";
168 RecordKeeper &Records =
169 dynamic_cast<DefInit&>(*Expr->getOperator()).getDef()->getRecords();
171 int Step = From <= To ? 1 : -1;
172 for (To += Step; From != To; From += Step) {
174 raw_string_ostream OS(Name);
175 OS << format(Format.c_str(), unsigned(From));
176 Record *Rec = Records.getDef(OS.str());
178 throw "No def named '" + Name + "': " + Expr->getAsString();
179 // Try to reevaluate Rec in case it is a set.
180 if (const RecVec *Result = ST.expand(Rec))
181 Elts.insert(Result->begin(), Result->end());
188 // Expand a Def into a set by evaluating one of its fields.
189 struct FieldExpander : public SetTheory::Expander {
192 FieldExpander(StringRef fn) : FieldName(fn) {}
194 void expand(SetTheory &ST, Record *Def, RecSet &Elts) {
195 ST.evaluate(Def->getValueInit(FieldName), Elts);
198 } // end anonymous namespace
200 SetTheory::SetTheory() {
201 addOperator("add", new AddOp);
202 addOperator("sub", new SubOp);
203 addOperator("and", new AndOp);
204 addOperator("shl", new ShlOp);
205 addOperator("trunc", new TruncOp);
206 addOperator("rotl", new RotOp(false));
207 addOperator("rotr", new RotOp(true));
208 addOperator("decimate", new DecimateOp);
209 addOperator("sequence", new SequenceOp);
212 void SetTheory::addOperator(StringRef Name, Operator *Op) {
213 Operators[Name] = Op;
216 void SetTheory::addExpander(StringRef ClassName, Expander *E) {
217 Expanders[ClassName] = E;
220 void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
221 addExpander(ClassName, new FieldExpander(FieldName));
224 void SetTheory::evaluate(Init *Expr, RecSet &Elts) {
225 // A def in a list can be a just an element, or it may expand.
226 if (DefInit *Def = dynamic_cast<DefInit*>(Expr)) {
227 if (const RecVec *Result = expand(Def->getDef()))
228 return Elts.insert(Result->begin(), Result->end());
229 Elts.insert(Def->getDef());
233 // Lists simply expand.
234 if (ListInit *LI = dynamic_cast<ListInit*>(Expr))
235 return evaluate(LI->begin(), LI->end(), Elts);
237 // Anything else must be a DAG.
238 DagInit *DagExpr = dynamic_cast<DagInit*>(Expr);
240 throw "Invalid set element: " + Expr->getAsString();
241 DefInit *OpInit = dynamic_cast<DefInit*>(DagExpr->getOperator());
243 throw "Bad set expression: " + Expr->getAsString();
244 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
246 throw "Unknown set operator: " + Expr->getAsString();
247 Op->apply(*this, DagExpr, Elts);
250 const RecVec *SetTheory::expand(Record *Set) {
251 // Check existing entries for Set and return early.
252 ExpandMap::iterator I = Expansions.find(Set);
253 if (I != Expansions.end())
256 // This is the first time we see Set. Find a suitable expander.
258 const std::vector<Record*> &SC = Set->getSuperClasses();
259 for (unsigned i = 0, e = SC.size(); i != e; ++i)
260 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
261 // This breaks recursive definitions.
262 RecVec &EltVec = Expansions[Set];
264 Exp->expand(*this, Set, Elts);
265 EltVec.assign(Elts.begin(), Elts.end());
268 } catch (const std::string &Error) {
269 throw TGError(Set->getLoc(), Error);
272 // Set is not expandable.