SetTheory.cpp revision 2c6d71388fb1b68ce6fdbb88642a95a24b27b2a7
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" 16#include "llvm/TableGen/Error.h" 17#include "llvm/TableGen/Record.h" 18#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 { 30 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 31 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc); 32 } 33}; 34 35// (sub Add, Sub, ...) Set difference. 36struct SubOp : public SetTheory::Operator { 37 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 38 if (Expr->arg_size() < 2) 39 throw TGError(Loc, "Set difference needs at least two arguments: " + 40 Expr->getAsString()); 41 RecSet Add, Sub; 42 ST.evaluate(*Expr->arg_begin(), Add, Loc); 43 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub, Loc); 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 { 52 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 53 if (Expr->arg_size() != 2) 54 throw TGError(Loc, "Set intersection requires two arguments: " + 55 Expr->getAsString()); 56 RecSet S1, S2; 57 ST.evaluate(Expr->arg_begin()[0], S1, Loc); 58 ST.evaluate(Expr->arg_begin()[1], S2, Loc); 59 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I) 60 if (S2.count(*I)) 61 Elts.insert(*I); 62 } 63}; 64 65// SetIntBinOp - Abstract base class for (Op S, N) operators. 66struct SetIntBinOp : public SetTheory::Operator { 67 virtual void apply2(SetTheory &ST, DagInit *Expr, 68 RecSet &Set, int64_t N, 69 RecSet &Elts, ArrayRef<SMLoc> Loc) =0; 70 71 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 72 if (Expr->arg_size() != 2) 73 throw TGError(Loc, "Operator requires (Op Set, Int) arguments: " + 74 Expr->getAsString()); 75 RecSet Set; 76 ST.evaluate(Expr->arg_begin()[0], Set, Loc); 77 IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]); 78 if (!II) 79 throw TGError(Loc, "Second argument must be an integer: " + 80 Expr->getAsString()); 81 apply2(ST, Expr, Set, II->getValue(), Elts, Loc); 82 } 83}; 84 85// (shl S, N) Shift left, remove the first N elements. 86struct ShlOp : public SetIntBinOp { 87 void apply2(SetTheory &ST, DagInit *Expr, 88 RecSet &Set, int64_t N, 89 RecSet &Elts, ArrayRef<SMLoc> Loc) { 90 if (N < 0) 91 throw TGError(Loc, "Positive shift required: " + 92 Expr->getAsString()); 93 if (unsigned(N) < Set.size()) 94 Elts.insert(Set.begin() + N, Set.end()); 95 } 96}; 97 98// (trunc S, N) Truncate after the first N elements. 99struct TruncOp : public SetIntBinOp { 100 void apply2(SetTheory &ST, DagInit *Expr, 101 RecSet &Set, int64_t N, 102 RecSet &Elts, ArrayRef<SMLoc> Loc) { 103 if (N < 0) 104 throw TGError(Loc, "Positive length required: " + 105 Expr->getAsString()); 106 if (unsigned(N) > Set.size()) 107 N = Set.size(); 108 Elts.insert(Set.begin(), Set.begin() + N); 109 } 110}; 111 112// Left/right rotation. 113struct RotOp : public SetIntBinOp { 114 const bool Reverse; 115 116 RotOp(bool Rev) : Reverse(Rev) {} 117 118 void apply2(SetTheory &ST, DagInit *Expr, 119 RecSet &Set, int64_t N, 120 RecSet &Elts, ArrayRef<SMLoc> Loc) { 121 if (Reverse) 122 N = -N; 123 // N > 0 -> rotate left, N < 0 -> rotate right. 124 if (Set.empty()) 125 return; 126 if (N < 0) 127 N = Set.size() - (-N % Set.size()); 128 else 129 N %= Set.size(); 130 Elts.insert(Set.begin() + N, Set.end()); 131 Elts.insert(Set.begin(), Set.begin() + N); 132 } 133}; 134 135// (decimate S, N) Pick every N'th element of S. 136struct DecimateOp : public SetIntBinOp { 137 void apply2(SetTheory &ST, DagInit *Expr, 138 RecSet &Set, int64_t N, 139 RecSet &Elts, ArrayRef<SMLoc> Loc) { 140 if (N <= 0) 141 throw TGError(Loc, "Positive stride required: " + 142 Expr->getAsString()); 143 for (unsigned I = 0; I < Set.size(); I += N) 144 Elts.insert(Set[I]); 145 } 146}; 147 148// (interleave S1, S2, ...) Interleave elements of the arguments. 149struct InterleaveOp : public SetTheory::Operator { 150 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 151 // Evaluate the arguments individually. 152 SmallVector<RecSet, 4> Args(Expr->getNumArgs()); 153 unsigned MaxSize = 0; 154 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) { 155 ST.evaluate(Expr->getArg(i), Args[i], Loc); 156 MaxSize = std::max(MaxSize, unsigned(Args[i].size())); 157 } 158 // Interleave arguments into Elts. 159 for (unsigned n = 0; n != MaxSize; ++n) 160 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) 161 if (n < Args[i].size()) 162 Elts.insert(Args[i][n]); 163 } 164}; 165 166// (sequence "Format", From, To) Generate a sequence of records by name. 167struct SequenceOp : public SetTheory::Operator { 168 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 169 int Step = 1; 170 if (Expr->arg_size() > 4) 171 throw TGError(Loc, "Bad args to (sequence \"Format\", From, To): " + 172 Expr->getAsString()); 173 else if (Expr->arg_size() == 4) { 174 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[3])) { 175 Step = II->getValue(); 176 } else 177 throw TGError(Loc, "Stride must be an integer: " + Expr->getAsString()); 178 } 179 180 std::string Format; 181 if (StringInit *SI = dyn_cast<StringInit>(Expr->arg_begin()[0])) 182 Format = SI->getValue(); 183 else 184 throw TGError(Loc, "Format must be a string: " + Expr->getAsString()); 185 186 int64_t From, To; 187 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1])) 188 From = II->getValue(); 189 else 190 throw TGError(Loc, "From must be an integer: " + Expr->getAsString()); 191 if (From < 0 || From >= (1 << 30)) 192 throw TGError(Loc, "From out of range"); 193 194 if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[2])) 195 To = II->getValue(); 196 else 197 throw TGError(Loc, "From must be an integer: " + Expr->getAsString()); 198 if (To < 0 || To >= (1 << 30)) 199 throw TGError(Loc, "To out of range"); 200 201 RecordKeeper &Records = 202 cast<DefInit>(Expr->getOperator())->getDef()->getRecords(); 203 204 Step *= From <= To ? 1 : -1; 205 while (true) { 206 if (Step > 0 && From > To) 207 break; 208 else if (Step < 0 && From < To) 209 break; 210 std::string Name; 211 raw_string_ostream OS(Name); 212 OS << format(Format.c_str(), unsigned(From)); 213 Record *Rec = Records.getDef(OS.str()); 214 if (!Rec) 215 throw TGError(Loc, "No def named '" + Name + "': " + 216 Expr->getAsString()); 217 // Try to reevaluate Rec in case it is a set. 218 if (const RecVec *Result = ST.expand(Rec)) 219 Elts.insert(Result->begin(), Result->end()); 220 else 221 Elts.insert(Rec); 222 223 From += Step; 224 } 225 } 226}; 227 228// Expand a Def into a set by evaluating one of its fields. 229struct FieldExpander : public SetTheory::Expander { 230 StringRef FieldName; 231 232 FieldExpander(StringRef fn) : FieldName(fn) {} 233 234 void expand(SetTheory &ST, Record *Def, RecSet &Elts) { 235 ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc()); 236 } 237}; 238} // end anonymous namespace 239 240void SetTheory::Operator::anchor() { } 241 242void SetTheory::Expander::anchor() { } 243 244SetTheory::SetTheory() { 245 addOperator("add", new AddOp); 246 addOperator("sub", new SubOp); 247 addOperator("and", new AndOp); 248 addOperator("shl", new ShlOp); 249 addOperator("trunc", new TruncOp); 250 addOperator("rotl", new RotOp(false)); 251 addOperator("rotr", new RotOp(true)); 252 addOperator("decimate", new DecimateOp); 253 addOperator("interleave", new InterleaveOp); 254 addOperator("sequence", new SequenceOp); 255} 256 257void SetTheory::addOperator(StringRef Name, Operator *Op) { 258 Operators[Name] = Op; 259} 260 261void SetTheory::addExpander(StringRef ClassName, Expander *E) { 262 Expanders[ClassName] = E; 263} 264 265void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) { 266 addExpander(ClassName, new FieldExpander(FieldName)); 267} 268 269void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) { 270 // A def in a list can be a just an element, or it may expand. 271 if (DefInit *Def = dyn_cast<DefInit>(Expr)) { 272 if (const RecVec *Result = expand(Def->getDef())) 273 return Elts.insert(Result->begin(), Result->end()); 274 Elts.insert(Def->getDef()); 275 return; 276 } 277 278 // Lists simply expand. 279 if (ListInit *LI = dyn_cast<ListInit>(Expr)) 280 return evaluate(LI->begin(), LI->end(), Elts, Loc); 281 282 // Anything else must be a DAG. 283 DagInit *DagExpr = dyn_cast<DagInit>(Expr); 284 if (!DagExpr) 285 throw TGError(Loc, "Invalid set element: " + Expr->getAsString()); 286 DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator()); 287 if (!OpInit) 288 throw TGError(Loc, "Bad set expression: " + Expr->getAsString()); 289 Operator *Op = Operators.lookup(OpInit->getDef()->getName()); 290 if (!Op) 291 throw TGError(Loc, "Unknown set operator: " + Expr->getAsString()); 292 Op->apply(*this, DagExpr, Elts, Loc); 293} 294 295const RecVec *SetTheory::expand(Record *Set) { 296 // Check existing entries for Set and return early. 297 ExpandMap::iterator I = Expansions.find(Set); 298 if (I != Expansions.end()) 299 return &I->second; 300 301 // This is the first time we see Set. Find a suitable expander. 302 const std::vector<Record*> &SC = Set->getSuperClasses(); 303 for (unsigned i = 0, e = SC.size(); i != e; ++i) { 304 // Skip unnamed superclasses. 305 if (!dyn_cast<StringInit>(SC[i]->getNameInit())) 306 continue; 307 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) { 308 // This breaks recursive definitions. 309 RecVec &EltVec = Expansions[Set]; 310 RecSet Elts; 311 Exp->expand(*this, Set, Elts); 312 EltVec.assign(Elts.begin(), Elts.end()); 313 return &EltVec; 314 } 315 } 316 317 // Set is not expandable. 318 return 0; 319} 320 321