blob: bef73f33effe089fed35cc689635cdc223bf0322 [file] [log] [blame]
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +00001//===- SetTheory.cpp - Generate ordered sets from DAG expressions ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SetTheory class that computes ordered sets of
11// Records from DAG expressions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SetTheory.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000016#include "llvm/TableGen/Error.h"
17#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000018#include "llvm/Support/Format.h"
19
20using namespace llvm;
21
22// Define the standard operators.
23namespace {
24
25typedef SetTheory::RecSet RecSet;
26typedef SetTheory::RecVec RecVec;
27
28// (add a, b, ...) Evaluate and union all arguments.
29struct AddOp : public SetTheory::Operator {
David Greene05bce0b2011-07-29 22:43:06 +000030 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000031 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts);
32 }
33};
34
35// (sub Add, Sub, ...) Set difference.
36struct SubOp : public SetTheory::Operator {
David Greene05bce0b2011-07-29 22:43:06 +000037 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000038 if (Expr->arg_size() < 2)
39 throw "Set difference needs at least two arguments: " +
40 Expr->getAsString();
41 RecSet Add, Sub;
42 ST.evaluate(*Expr->arg_begin(), Add);
43 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub);
44 for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
45 if (!Sub.count(*I))
46 Elts.insert(*I);
47 }
48};
49
50// (and S1, S2) Set intersection.
51struct AndOp : public SetTheory::Operator {
David Greene05bce0b2011-07-29 22:43:06 +000052 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000053 if (Expr->arg_size() != 2)
54 throw "Set intersection requires two arguments: " + Expr->getAsString();
55 RecSet S1, S2;
56 ST.evaluate(Expr->arg_begin()[0], S1);
57 ST.evaluate(Expr->arg_begin()[1], S2);
58 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
59 if (S2.count(*I))
60 Elts.insert(*I);
61 }
62};
63
64// SetIntBinOp - Abstract base class for (Op S, N) operators.
65struct SetIntBinOp : public SetTheory::Operator {
David Greene05bce0b2011-07-29 22:43:06 +000066 virtual void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000067 RecSet &Set, int64_t N,
68 RecSet &Elts) =0;
69
David Greene05bce0b2011-07-29 22:43:06 +000070 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000071 if (Expr->arg_size() != 2)
72 throw "Operator requires (Op Set, Int) arguments: " + Expr->getAsString();
73 RecSet Set;
74 ST.evaluate(Expr->arg_begin()[0], Set);
David Greene05bce0b2011-07-29 22:43:06 +000075 IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]);
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000076 if (!II)
77 throw "Second argument must be an integer: " + Expr->getAsString();
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +000078 apply2(ST, Expr, Set, II->getValue(), Elts);
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000079 }
80};
81
82// (shl S, N) Shift left, remove the first N elements.
83struct ShlOp : public SetIntBinOp {
David Greene05bce0b2011-07-29 22:43:06 +000084 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000085 RecSet &Set, int64_t N,
86 RecSet &Elts) {
87 if (N < 0)
88 throw "Positive shift required: " + Expr->getAsString();
89 if (unsigned(N) < Set.size())
90 Elts.insert(Set.begin() + N, Set.end());
91 }
92};
93
94// (trunc S, N) Truncate after the first N elements.
95struct TruncOp : public SetIntBinOp {
David Greene05bce0b2011-07-29 22:43:06 +000096 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000097 RecSet &Set, int64_t N,
98 RecSet &Elts) {
99 if (N < 0)
100 throw "Positive length required: " + Expr->getAsString();
101 if (unsigned(N) > Set.size())
102 N = Set.size();
103 Elts.insert(Set.begin(), Set.begin() + N);
104 }
105};
106
107// Left/right rotation.
108struct RotOp : public SetIntBinOp {
109 const bool Reverse;
110
111 RotOp(bool Rev) : Reverse(Rev) {}
112
David Greene05bce0b2011-07-29 22:43:06 +0000113 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000114 RecSet &Set, int64_t N,
115 RecSet &Elts) {
116 if (Reverse)
117 N = -N;
118 // N > 0 -> rotate left, N < 0 -> rotate right.
119 if (Set.empty())
120 return;
121 if (N < 0)
122 N = Set.size() - (-N % Set.size());
123 else
124 N %= Set.size();
125 Elts.insert(Set.begin() + N, Set.end());
126 Elts.insert(Set.begin(), Set.begin() + N);
127 }
128};
129
130// (decimate S, N) Pick every N'th element of S.
131struct DecimateOp : public SetIntBinOp {
David Greene05bce0b2011-07-29 22:43:06 +0000132 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000133 RecSet &Set, int64_t N,
134 RecSet &Elts) {
135 if (N <= 0)
136 throw "Positive stride required: " + Expr->getAsString();
137 for (unsigned I = 0; I < Set.size(); I += N)
138 Elts.insert(Set[I]);
139 }
140};
141
142// (sequence "Format", From, To) Generate a sequence of records by name.
143struct SequenceOp : public SetTheory::Operator {
David Greene05bce0b2011-07-29 22:43:06 +0000144 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000145 if (Expr->arg_size() != 3)
146 throw "Bad args to (sequence \"Format\", From, To): " +
147 Expr->getAsString();
148 std::string Format;
David Greene05bce0b2011-07-29 22:43:06 +0000149 if (StringInit *SI = dynamic_cast<StringInit*>(Expr->arg_begin()[0]))
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000150 Format = SI->getValue();
151 else
152 throw "Format must be a string: " + Expr->getAsString();
153
154 int64_t From, To;
David Greene05bce0b2011-07-29 22:43:06 +0000155 if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]))
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000156 From = II->getValue();
157 else
158 throw "From must be an integer: " + Expr->getAsString();
Jakob Stoklund Olesen25590112011-06-16 03:07:40 +0000159 if (From < 0 || From >= (1 << 30))
Jakob Stoklund Olesen0cc09292011-06-16 02:55:56 +0000160 throw "From out of range";
161
David Greene05bce0b2011-07-29 22:43:06 +0000162 if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[2]))
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000163 To = II->getValue();
164 else
165 throw "From must be an integer: " + Expr->getAsString();
Jakob Stoklund Olesen25590112011-06-16 03:07:40 +0000166 if (To < 0 || To >= (1 << 30))
Jakob Stoklund Olesen0cc09292011-06-16 02:55:56 +0000167 throw "To out of range";
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000168
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000169 RecordKeeper &Records =
David Greene05bce0b2011-07-29 22:43:06 +0000170 dynamic_cast<DefInit&>(*Expr->getOperator()).getDef()->getRecords();
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000171
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000172 int Step = From <= To ? 1 : -1;
173 for (To += Step; From != To; From += Step) {
174 std::string Name;
175 raw_string_ostream OS(Name);
Jakob Stoklund Olesen0cc09292011-06-16 02:55:56 +0000176 OS << format(Format.c_str(), unsigned(From));
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000177 Record *Rec = Records.getDef(OS.str());
178 if (!Rec)
179 throw "No def named '" + Name + "': " + Expr->getAsString();
180 // Try to reevaluate Rec in case it is a set.
181 if (const RecVec *Result = ST.expand(Rec))
182 Elts.insert(Result->begin(), Result->end());
183 else
184 Elts.insert(Rec);
185 }
186 }
187};
188
189// Expand a Def into a set by evaluating one of its fields.
190struct FieldExpander : public SetTheory::Expander {
191 StringRef FieldName;
192
193 FieldExpander(StringRef fn) : FieldName(fn) {}
194
195 void expand(SetTheory &ST, Record *Def, RecSet &Elts) {
196 ST.evaluate(Def->getValueInit(FieldName), Elts);
197 }
198};
199} // end anonymous namespace
200
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000201SetTheory::SetTheory() {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000202 addOperator("add", new AddOp);
203 addOperator("sub", new SubOp);
204 addOperator("and", new AndOp);
205 addOperator("shl", new ShlOp);
206 addOperator("trunc", new TruncOp);
207 addOperator("rotl", new RotOp(false));
208 addOperator("rotr", new RotOp(true));
209 addOperator("decimate", new DecimateOp);
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000210 addOperator("sequence", new SequenceOp);
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000211}
212
213void SetTheory::addOperator(StringRef Name, Operator *Op) {
214 Operators[Name] = Op;
215}
216
217void SetTheory::addExpander(StringRef ClassName, Expander *E) {
218 Expanders[ClassName] = E;
219}
220
221void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
222 addExpander(ClassName, new FieldExpander(FieldName));
223}
224
David Greene05bce0b2011-07-29 22:43:06 +0000225void SetTheory::evaluate(Init *Expr, RecSet &Elts) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000226 // A def in a list can be a just an element, or it may expand.
David Greene05bce0b2011-07-29 22:43:06 +0000227 if (DefInit *Def = dynamic_cast<DefInit*>(Expr)) {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000228 if (const RecVec *Result = expand(Def->getDef()))
229 return Elts.insert(Result->begin(), Result->end());
230 Elts.insert(Def->getDef());
231 return;
232 }
233
234 // Lists simply expand.
David Greene05bce0b2011-07-29 22:43:06 +0000235 if (ListInit *LI = dynamic_cast<ListInit*>(Expr))
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000236 return evaluate(LI->begin(), LI->end(), Elts);
237
238 // Anything else must be a DAG.
David Greene05bce0b2011-07-29 22:43:06 +0000239 DagInit *DagExpr = dynamic_cast<DagInit*>(Expr);
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000240 if (!DagExpr)
241 throw "Invalid set element: " + Expr->getAsString();
David Greene05bce0b2011-07-29 22:43:06 +0000242 DefInit *OpInit = dynamic_cast<DefInit*>(DagExpr->getOperator());
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000243 if (!OpInit)
244 throw "Bad set expression: " + Expr->getAsString();
245 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
246 if (!Op)
247 throw "Unknown set operator: " + Expr->getAsString();
248 Op->apply(*this, DagExpr, Elts);
249}
250
251const RecVec *SetTheory::expand(Record *Set) {
252 // Check existing entries for Set and return early.
253 ExpandMap::iterator I = Expansions.find(Set);
254 if (I != Expansions.end())
255 return &I->second;
256
257 // This is the first time we see Set. Find a suitable expander.
258 try {
259 const std::vector<Record*> &SC = Set->getSuperClasses();
260 for (unsigned i = 0, e = SC.size(); i != e; ++i)
261 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
262 // This breaks recursive definitions.
263 RecVec &EltVec = Expansions[Set];
264 RecSet Elts;
265 Exp->expand(*this, Set, Elts);
266 EltVec.assign(Elts.begin(), Elts.end());
267 return &EltVec;
268 }
269 } catch (const std::string &Error) {
270 throw TGError(Set->getLoc(), Error);
271 }
272
273 // Set is not expandable.
274 return 0;
275}
276