blob: ade182557678da15d336a33f99b21a340098a4cb [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"
16#include "Record.h"
17#include "llvm/Support/Format.h"
18
19using namespace llvm;
20
21// Define the standard operators.
22namespace {
23
24typedef SetTheory::RecSet RecSet;
25typedef SetTheory::RecVec RecVec;
26
27// (add a, b, ...) Evaluate and union all arguments.
28struct AddOp : public SetTheory::Operator {
29 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
30 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts);
31 }
32};
33
34// (sub Add, Sub, ...) Set difference.
35struct 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: " +
39 Expr->getAsString();
40 RecSet Add, Sub;
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)
44 if (!Sub.count(*I))
45 Elts.insert(*I);
46 }
47};
48
49// (and S1, S2) Set intersection.
50struct 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();
54 RecSet S1, S2;
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)
58 if (S2.count(*I))
59 Elts.insert(*I);
60 }
61};
62
63// SetIntBinOp - Abstract base class for (Op S, N) operators.
64struct SetIntBinOp : public SetTheory::Operator {
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +000065 virtual void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000066 RecSet &Set, int64_t N,
67 RecSet &Elts) =0;
68
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();
72 RecSet Set;
73 ST.evaluate(Expr->arg_begin()[0], Set);
74 IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]);
75 if (!II)
76 throw "Second argument must be an integer: " + Expr->getAsString();
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +000077 apply2(ST, Expr, Set, II->getValue(), Elts);
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000078 }
79};
80
81// (shl S, N) Shift left, remove the first N elements.
82struct ShlOp : public SetIntBinOp {
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +000083 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000084 RecSet &Set, int64_t N,
85 RecSet &Elts) {
86 if (N < 0)
87 throw "Positive shift required: " + Expr->getAsString();
88 if (unsigned(N) < Set.size())
89 Elts.insert(Set.begin() + N, Set.end());
90 }
91};
92
93// (trunc S, N) Truncate after the first N elements.
94struct TruncOp : public SetIntBinOp {
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +000095 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +000096 RecSet &Set, int64_t N,
97 RecSet &Elts) {
98 if (N < 0)
99 throw "Positive length required: " + Expr->getAsString();
100 if (unsigned(N) > Set.size())
101 N = Set.size();
102 Elts.insert(Set.begin(), Set.begin() + N);
103 }
104};
105
106// Left/right rotation.
107struct RotOp : public SetIntBinOp {
108 const bool Reverse;
109
110 RotOp(bool Rev) : Reverse(Rev) {}
111
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +0000112 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000113 RecSet &Set, int64_t N,
114 RecSet &Elts) {
115 if (Reverse)
116 N = -N;
117 // N > 0 -> rotate left, N < 0 -> rotate right.
118 if (Set.empty())
119 return;
120 if (N < 0)
121 N = Set.size() - (-N % Set.size());
122 else
123 N %= Set.size();
124 Elts.insert(Set.begin() + N, Set.end());
125 Elts.insert(Set.begin(), Set.begin() + N);
126 }
127};
128
129// (decimate S, N) Pick every N'th element of S.
130struct DecimateOp : public SetIntBinOp {
Jakob Stoklund Olesen1023f5a2011-06-04 05:09:36 +0000131 void apply2(SetTheory &ST, DagInit *Expr,
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000132 RecSet &Set, int64_t N,
133 RecSet &Elts) {
134 if (N <= 0)
135 throw "Positive stride required: " + Expr->getAsString();
136 for (unsigned I = 0; I < Set.size(); I += N)
137 Elts.insert(Set[I]);
138 }
139};
140
141// (sequence "Format", From, To) Generate a sequence of records by name.
142struct SequenceOp : public SetTheory::Operator {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000143 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
144 if (Expr->arg_size() != 3)
145 throw "Bad args to (sequence \"Format\", From, To): " +
146 Expr->getAsString();
147 std::string Format;
148 if (StringInit *SI = dynamic_cast<StringInit*>(Expr->arg_begin()[0]))
149 Format = SI->getValue();
150 else
151 throw "Format must be a string: " + Expr->getAsString();
152
153 int64_t From, To;
154 if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]))
155 From = II->getValue();
156 else
157 throw "From must be an integer: " + Expr->getAsString();
158 if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[2]))
159 To = II->getValue();
160 else
161 throw "From must be an integer: " + Expr->getAsString();
162
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000163 RecordKeeper &Records =
164 dynamic_cast<DefInit&>(*Expr->getOperator()).getDef()->getRecords();
165
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000166 int Step = From <= To ? 1 : -1;
167 for (To += Step; From != To; From += Step) {
168 std::string Name;
169 raw_string_ostream OS(Name);
170 OS << format(Format.c_str(), From);
171 Record *Rec = Records.getDef(OS.str());
172 if (!Rec)
173 throw "No def named '" + Name + "': " + Expr->getAsString();
174 // Try to reevaluate Rec in case it is a set.
175 if (const RecVec *Result = ST.expand(Rec))
176 Elts.insert(Result->begin(), Result->end());
177 else
178 Elts.insert(Rec);
179 }
180 }
181};
182
183// Expand a Def into a set by evaluating one of its fields.
184struct FieldExpander : public SetTheory::Expander {
185 StringRef FieldName;
186
187 FieldExpander(StringRef fn) : FieldName(fn) {}
188
189 void expand(SetTheory &ST, Record *Def, RecSet &Elts) {
190 ST.evaluate(Def->getValueInit(FieldName), Elts);
191 }
192};
193} // end anonymous namespace
194
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000195SetTheory::SetTheory() {
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000196 addOperator("add", new AddOp);
197 addOperator("sub", new SubOp);
198 addOperator("and", new AndOp);
199 addOperator("shl", new ShlOp);
200 addOperator("trunc", new TruncOp);
201 addOperator("rotl", new RotOp(false));
202 addOperator("rotr", new RotOp(true));
203 addOperator("decimate", new DecimateOp);
Jakob Stoklund Olesenc017bc12011-06-04 07:49:55 +0000204 addOperator("sequence", new SequenceOp);
Jakob Stoklund Olesen1de99822011-06-04 04:11:37 +0000205}
206
207void SetTheory::addOperator(StringRef Name, Operator *Op) {
208 Operators[Name] = Op;
209}
210
211void SetTheory::addExpander(StringRef ClassName, Expander *E) {
212 Expanders[ClassName] = E;
213}
214
215void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
216 addExpander(ClassName, new FieldExpander(FieldName));
217}
218
219void SetTheory::evaluate(Init *Expr, RecSet &Elts) {
220 // A def in a list can be a just an element, or it may expand.
221 if (DefInit *Def = dynamic_cast<DefInit*>(Expr)) {
222 if (const RecVec *Result = expand(Def->getDef()))
223 return Elts.insert(Result->begin(), Result->end());
224 Elts.insert(Def->getDef());
225 return;
226 }
227
228 // Lists simply expand.
229 if (ListInit *LI = dynamic_cast<ListInit*>(Expr))
230 return evaluate(LI->begin(), LI->end(), Elts);
231
232 // Anything else must be a DAG.
233 DagInit *DagExpr = dynamic_cast<DagInit*>(Expr);
234 if (!DagExpr)
235 throw "Invalid set element: " + Expr->getAsString();
236 DefInit *OpInit = dynamic_cast<DefInit*>(DagExpr->getOperator());
237 if (!OpInit)
238 throw "Bad set expression: " + Expr->getAsString();
239 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
240 if (!Op)
241 throw "Unknown set operator: " + Expr->getAsString();
242 Op->apply(*this, DagExpr, Elts);
243}
244
245const RecVec *SetTheory::expand(Record *Set) {
246 // Check existing entries for Set and return early.
247 ExpandMap::iterator I = Expansions.find(Set);
248 if (I != Expansions.end())
249 return &I->second;
250
251 // This is the first time we see Set. Find a suitable expander.
252 try {
253 const std::vector<Record*> &SC = Set->getSuperClasses();
254 for (unsigned i = 0, e = SC.size(); i != e; ++i)
255 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
256 // This breaks recursive definitions.
257 RecVec &EltVec = Expansions[Set];
258 RecSet Elts;
259 Exp->expand(*this, Set, Elts);
260 EltVec.assign(Elts.begin(), Elts.end());
261 return &EltVec;
262 }
263 } catch (const std::string &Error) {
264 throw TGError(Set->getLoc(), Error);
265 }
266
267 // Set is not expandable.
268 return 0;
269}
270