Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 1 | //===- 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 Collingbourne | 7c78888 | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 16 | #include "llvm/TableGen/Error.h" |
| 17 | #include "llvm/TableGen/Record.h" |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | // Define the standard operators. |
| 23 | namespace { |
| 24 | |
| 25 | typedef SetTheory::RecSet RecSet; |
| 26 | typedef SetTheory::RecVec RecVec; |
| 27 | |
| 28 | // (add a, b, ...) Evaluate and union all arguments. |
| 29 | struct AddOp : public SetTheory::Operator { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 30 | void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 31 | ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | // (sub Add, Sub, ...) Set difference. |
| 36 | struct SubOp : public SetTheory::Operator { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 37 | void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 38 | 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. |
| 51 | struct AndOp : public SetTheory::Operator { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 52 | void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 53 | 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. |
| 65 | struct SetIntBinOp : public SetTheory::Operator { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 66 | virtual void apply2(SetTheory &ST, DagInit *Expr, |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 67 | RecSet &Set, int64_t N, |
| 68 | RecSet &Elts) =0; |
| 69 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 70 | void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 71 | 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 Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 75 | IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]); |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 76 | if (!II) |
| 77 | throw "Second argument must be an integer: " + Expr->getAsString(); |
Jakob Stoklund Olesen | 1023f5a | 2011-06-04 05:09:36 +0000 | [diff] [blame] | 78 | apply2(ST, Expr, Set, II->getValue(), Elts); |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
| 82 | // (shl S, N) Shift left, remove the first N elements. |
| 83 | struct ShlOp : public SetIntBinOp { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 84 | void apply2(SetTheory &ST, DagInit *Expr, |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 85 | 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. |
| 95 | struct TruncOp : public SetIntBinOp { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 96 | void apply2(SetTheory &ST, DagInit *Expr, |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 97 | 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. |
| 108 | struct RotOp : public SetIntBinOp { |
| 109 | const bool Reverse; |
| 110 | |
| 111 | RotOp(bool Rev) : Reverse(Rev) {} |
| 112 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 113 | void apply2(SetTheory &ST, DagInit *Expr, |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 114 | 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. |
| 131 | struct DecimateOp : public SetIntBinOp { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 132 | void apply2(SetTheory &ST, DagInit *Expr, |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 133 | 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 | |
Jakob Stoklund Olesen | 5b52f6d | 2012-01-24 18:06:05 +0000 | [diff] [blame] | 142 | // (interleave S1, S2, ...) Interleave elements of the arguments. |
| 143 | struct InterleaveOp : public SetTheory::Operator { |
| 144 | void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) { |
| 145 | // Evaluate the arguments individually. |
| 146 | SmallVector<RecSet, 4> Args(Expr->getNumArgs()); |
| 147 | unsigned MaxSize = 0; |
| 148 | for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) { |
| 149 | ST.evaluate(Expr->getArg(i), Args[i]); |
| 150 | MaxSize = std::max(MaxSize, unsigned(Args[i].size())); |
| 151 | } |
| 152 | // Interleave arguments into Elts. |
| 153 | for (unsigned n = 0; n != MaxSize; ++n) |
| 154 | for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) |
| 155 | if (n < Args[i].size()) |
| 156 | Elts.insert(Args[i][n]); |
| 157 | } |
| 158 | }; |
| 159 | |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 160 | // (sequence "Format", From, To) Generate a sequence of records by name. |
| 161 | struct SequenceOp : public SetTheory::Operator { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 162 | void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) { |
Owen Anderson | 6b31d4e | 2012-05-24 21:37:08 +0000 | [diff] [blame] | 163 | int Step = 1; |
| 164 | if (Expr->arg_size() > 4) |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 165 | throw "Bad args to (sequence \"Format\", From, To): " + |
| 166 | Expr->getAsString(); |
Owen Anderson | 6b31d4e | 2012-05-24 21:37:08 +0000 | [diff] [blame] | 167 | else if (Expr->arg_size() == 4) { |
| 168 | if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[3])) { |
| 169 | Step = II->getValue(); |
| 170 | } else |
| 171 | throw "Stride must be an integer: " + Expr->getAsString(); |
| 172 | } |
| 173 | |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 174 | std::string Format; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 175 | if (StringInit *SI = dynamic_cast<StringInit*>(Expr->arg_begin()[0])) |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 176 | Format = SI->getValue(); |
| 177 | else |
| 178 | throw "Format must be a string: " + Expr->getAsString(); |
| 179 | |
| 180 | int64_t From, To; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 181 | if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1])) |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 182 | From = II->getValue(); |
| 183 | else |
| 184 | throw "From must be an integer: " + Expr->getAsString(); |
Jakob Stoklund Olesen | 2559011 | 2011-06-16 03:07:40 +0000 | [diff] [blame] | 185 | if (From < 0 || From >= (1 << 30)) |
Jakob Stoklund Olesen | 0cc0929 | 2011-06-16 02:55:56 +0000 | [diff] [blame] | 186 | throw "From out of range"; |
| 187 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 188 | if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[2])) |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 189 | To = II->getValue(); |
| 190 | else |
| 191 | throw "From must be an integer: " + Expr->getAsString(); |
Jakob Stoklund Olesen | 2559011 | 2011-06-16 03:07:40 +0000 | [diff] [blame] | 192 | if (To < 0 || To >= (1 << 30)) |
Jakob Stoklund Olesen | 0cc0929 | 2011-06-16 02:55:56 +0000 | [diff] [blame] | 193 | throw "To out of range"; |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 194 | |
Jakob Stoklund Olesen | c017bc1 | 2011-06-04 07:49:55 +0000 | [diff] [blame] | 195 | RecordKeeper &Records = |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 196 | dynamic_cast<DefInit&>(*Expr->getOperator()).getDef()->getRecords(); |
Jakob Stoklund Olesen | c017bc1 | 2011-06-04 07:49:55 +0000 | [diff] [blame] | 197 | |
Owen Anderson | 6b31d4e | 2012-05-24 21:37:08 +0000 | [diff] [blame] | 198 | Step *= From <= To ? 1 : -1; |
| 199 | while (true) { |
| 200 | if (Step > 0 && From > To) |
| 201 | break; |
| 202 | else if (Step < 0 && From < To) |
| 203 | break; |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 204 | std::string Name; |
| 205 | raw_string_ostream OS(Name); |
Jakob Stoklund Olesen | 0cc0929 | 2011-06-16 02:55:56 +0000 | [diff] [blame] | 206 | OS << format(Format.c_str(), unsigned(From)); |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 207 | Record *Rec = Records.getDef(OS.str()); |
| 208 | if (!Rec) |
| 209 | throw "No def named '" + Name + "': " + Expr->getAsString(); |
| 210 | // Try to reevaluate Rec in case it is a set. |
| 211 | if (const RecVec *Result = ST.expand(Rec)) |
| 212 | Elts.insert(Result->begin(), Result->end()); |
| 213 | else |
| 214 | Elts.insert(Rec); |
Owen Anderson | 6b31d4e | 2012-05-24 21:37:08 +0000 | [diff] [blame] | 215 | |
| 216 | From += Step; |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | // Expand a Def into a set by evaluating one of its fields. |
| 222 | struct FieldExpander : public SetTheory::Expander { |
| 223 | StringRef FieldName; |
| 224 | |
| 225 | FieldExpander(StringRef fn) : FieldName(fn) {} |
| 226 | |
| 227 | void expand(SetTheory &ST, Record *Def, RecSet &Elts) { |
| 228 | ST.evaluate(Def->getValueInit(FieldName), Elts); |
| 229 | } |
| 230 | }; |
| 231 | } // end anonymous namespace |
| 232 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 233 | void SetTheory::Operator::anchor() { } |
| 234 | |
| 235 | void SetTheory::Expander::anchor() { } |
| 236 | |
Jakob Stoklund Olesen | c017bc1 | 2011-06-04 07:49:55 +0000 | [diff] [blame] | 237 | SetTheory::SetTheory() { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 238 | addOperator("add", new AddOp); |
| 239 | addOperator("sub", new SubOp); |
| 240 | addOperator("and", new AndOp); |
| 241 | addOperator("shl", new ShlOp); |
| 242 | addOperator("trunc", new TruncOp); |
| 243 | addOperator("rotl", new RotOp(false)); |
| 244 | addOperator("rotr", new RotOp(true)); |
| 245 | addOperator("decimate", new DecimateOp); |
Jakob Stoklund Olesen | 5b52f6d | 2012-01-24 18:06:05 +0000 | [diff] [blame] | 246 | addOperator("interleave", new InterleaveOp); |
Jakob Stoklund Olesen | c017bc1 | 2011-06-04 07:49:55 +0000 | [diff] [blame] | 247 | addOperator("sequence", new SequenceOp); |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void SetTheory::addOperator(StringRef Name, Operator *Op) { |
| 251 | Operators[Name] = Op; |
| 252 | } |
| 253 | |
| 254 | void SetTheory::addExpander(StringRef ClassName, Expander *E) { |
| 255 | Expanders[ClassName] = E; |
| 256 | } |
| 257 | |
| 258 | void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) { |
| 259 | addExpander(ClassName, new FieldExpander(FieldName)); |
| 260 | } |
| 261 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 262 | void SetTheory::evaluate(Init *Expr, RecSet &Elts) { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 263 | // A def in a list can be a just an element, or it may expand. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 264 | if (DefInit *Def = dynamic_cast<DefInit*>(Expr)) { |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 265 | if (const RecVec *Result = expand(Def->getDef())) |
| 266 | return Elts.insert(Result->begin(), Result->end()); |
| 267 | Elts.insert(Def->getDef()); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // Lists simply expand. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 272 | if (ListInit *LI = dynamic_cast<ListInit*>(Expr)) |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 273 | return evaluate(LI->begin(), LI->end(), Elts); |
| 274 | |
| 275 | // Anything else must be a DAG. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 276 | DagInit *DagExpr = dynamic_cast<DagInit*>(Expr); |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 277 | if (!DagExpr) |
| 278 | throw "Invalid set element: " + Expr->getAsString(); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 279 | DefInit *OpInit = dynamic_cast<DefInit*>(DagExpr->getOperator()); |
Jakob Stoklund Olesen | 1de9982 | 2011-06-04 04:11:37 +0000 | [diff] [blame] | 280 | if (!OpInit) |
| 281 | throw "Bad set expression: " + Expr->getAsString(); |
| 282 | Operator *Op = Operators.lookup(OpInit->getDef()->getName()); |
| 283 | if (!Op) |
| 284 | throw "Unknown set operator: " + Expr->getAsString(); |
| 285 | Op->apply(*this, DagExpr, Elts); |
| 286 | } |
| 287 | |
| 288 | const RecVec *SetTheory::expand(Record *Set) { |
| 289 | // Check existing entries for Set and return early. |
| 290 | ExpandMap::iterator I = Expansions.find(Set); |
| 291 | if (I != Expansions.end()) |
| 292 | return &I->second; |
| 293 | |
| 294 | // This is the first time we see Set. Find a suitable expander. |
| 295 | try { |
| 296 | const std::vector<Record*> &SC = Set->getSuperClasses(); |
| 297 | for (unsigned i = 0, e = SC.size(); i != e; ++i) |
| 298 | if (Expander *Exp = Expanders.lookup(SC[i]->getName())) { |
| 299 | // This breaks recursive definitions. |
| 300 | RecVec &EltVec = Expansions[Set]; |
| 301 | RecSet Elts; |
| 302 | Exp->expand(*this, Set, Elts); |
| 303 | EltVec.assign(Elts.begin(), Elts.end()); |
| 304 | return &EltVec; |
| 305 | } |
| 306 | } catch (const std::string &Error) { |
| 307 | throw TGError(Set->getLoc(), Error); |
| 308 | } |
| 309 | |
| 310 | // Set is not expandable. |
| 311 | return 0; |
| 312 | } |
| 313 | |