LLVM API Documentation

SetTheory.cpp
Go to the documentation of this file.
00001 //===- SetTheory.cpp - Generate ordered sets from DAG expressions ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SetTheory class that computes ordered sets of
00011 // Records from DAG expressions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Support/Format.h"
00016 #include "llvm/TableGen/Error.h"
00017 #include "llvm/TableGen/Record.h"
00018 #include "llvm/TableGen/SetTheory.h"
00019 
00020 using namespace llvm;
00021 
00022 // Define the standard operators.
00023 namespace {
00024 
00025 typedef SetTheory::RecSet RecSet;
00026 typedef SetTheory::RecVec RecVec;
00027 
00028 // (add a, b, ...) Evaluate and union all arguments.
00029 struct AddOp : public SetTheory::Operator {
00030   void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
00031              ArrayRef<SMLoc> Loc) override {
00032     ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
00033   }
00034 };
00035 
00036 // (sub Add, Sub, ...) Set difference.
00037 struct SubOp : public SetTheory::Operator {
00038   void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
00039              ArrayRef<SMLoc> Loc) override {
00040     if (Expr->arg_size() < 2)
00041       PrintFatalError(Loc, "Set difference needs at least two arguments: " +
00042         Expr->getAsString());
00043     RecSet Add, Sub;
00044     ST.evaluate(*Expr->arg_begin(), Add, Loc);
00045     ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub, Loc);
00046     for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
00047       if (!Sub.count(*I))
00048         Elts.insert(*I);
00049   }
00050 };
00051 
00052 // (and S1, S2) Set intersection.
00053 struct AndOp : public SetTheory::Operator {
00054   void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
00055              ArrayRef<SMLoc> Loc) override {
00056     if (Expr->arg_size() != 2)
00057       PrintFatalError(Loc, "Set intersection requires two arguments: " +
00058         Expr->getAsString());
00059     RecSet S1, S2;
00060     ST.evaluate(Expr->arg_begin()[0], S1, Loc);
00061     ST.evaluate(Expr->arg_begin()[1], S2, Loc);
00062     for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
00063       if (S2.count(*I))
00064         Elts.insert(*I);
00065   }
00066 };
00067 
00068 // SetIntBinOp - Abstract base class for (Op S, N) operators.
00069 struct SetIntBinOp : public SetTheory::Operator {
00070   virtual void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
00071                       RecSet &Elts, ArrayRef<SMLoc> Loc) = 0;
00072 
00073   void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
00074              ArrayRef<SMLoc> Loc) override {
00075     if (Expr->arg_size() != 2)
00076       PrintFatalError(Loc, "Operator requires (Op Set, Int) arguments: " +
00077         Expr->getAsString());
00078     RecSet Set;
00079     ST.evaluate(Expr->arg_begin()[0], Set, Loc);
00080     IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]);
00081     if (!II)
00082       PrintFatalError(Loc, "Second argument must be an integer: " +
00083         Expr->getAsString());
00084     apply2(ST, Expr, Set, II->getValue(), Elts, Loc);
00085   }
00086 };
00087 
00088 // (shl S, N) Shift left, remove the first N elements.
00089 struct ShlOp : public SetIntBinOp {
00090   void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
00091               RecSet &Elts, ArrayRef<SMLoc> Loc) override {
00092     if (N < 0)
00093       PrintFatalError(Loc, "Positive shift required: " +
00094         Expr->getAsString());
00095     if (unsigned(N) < Set.size())
00096       Elts.insert(Set.begin() + N, Set.end());
00097   }
00098 };
00099 
00100 // (trunc S, N) Truncate after the first N elements.
00101 struct TruncOp : public SetIntBinOp {
00102   void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
00103               RecSet &Elts, ArrayRef<SMLoc> Loc) override {
00104     if (N < 0)
00105       PrintFatalError(Loc, "Positive length required: " +
00106         Expr->getAsString());
00107     if (unsigned(N) > Set.size())
00108       N = Set.size();
00109     Elts.insert(Set.begin(), Set.begin() + N);
00110   }
00111 };
00112 
00113 // Left/right rotation.
00114 struct RotOp : public SetIntBinOp {
00115   const bool Reverse;
00116 
00117   RotOp(bool Rev) : Reverse(Rev) {}
00118 
00119   void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
00120               RecSet &Elts, ArrayRef<SMLoc> Loc) override {
00121     if (Reverse)
00122       N = -N;
00123     // N > 0 -> rotate left, N < 0 -> rotate right.
00124     if (Set.empty())
00125       return;
00126     if (N < 0)
00127       N = Set.size() - (-N % Set.size());
00128     else
00129       N %= Set.size();
00130     Elts.insert(Set.begin() + N, Set.end());
00131     Elts.insert(Set.begin(), Set.begin() + N);
00132   }
00133 };
00134 
00135 // (decimate S, N) Pick every N'th element of S.
00136 struct DecimateOp : public SetIntBinOp {
00137   void apply2(SetTheory &ST, DagInit *Expr, RecSet &Set, int64_t N,
00138               RecSet &Elts, ArrayRef<SMLoc> Loc) override {
00139     if (N <= 0)
00140       PrintFatalError(Loc, "Positive stride required: " +
00141         Expr->getAsString());
00142     for (unsigned I = 0; I < Set.size(); I += N)
00143       Elts.insert(Set[I]);
00144   }
00145 };
00146 
00147 // (interleave S1, S2, ...) Interleave elements of the arguments.
00148 struct InterleaveOp : public SetTheory::Operator {
00149   void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
00150              ArrayRef<SMLoc> Loc) override {
00151     // Evaluate the arguments individually.
00152     SmallVector<RecSet, 4> Args(Expr->getNumArgs());
00153     unsigned MaxSize = 0;
00154     for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
00155       ST.evaluate(Expr->getArg(i), Args[i], Loc);
00156       MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
00157     }
00158     // Interleave arguments into Elts.
00159     for (unsigned n = 0; n != MaxSize; ++n)
00160       for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i)
00161         if (n < Args[i].size())
00162           Elts.insert(Args[i][n]);
00163   }
00164 };
00165 
00166 // (sequence "Format", From, To) Generate a sequence of records by name.
00167 struct SequenceOp : public SetTheory::Operator {
00168   void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts,
00169              ArrayRef<SMLoc> Loc) override {
00170     int Step = 1;
00171     if (Expr->arg_size() > 4)
00172       PrintFatalError(Loc, "Bad args to (sequence \"Format\", From, To): " +
00173         Expr->getAsString());
00174     else if (Expr->arg_size() == 4) {
00175       if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[3])) {
00176         Step = II->getValue();
00177       } else
00178         PrintFatalError(Loc, "Stride must be an integer: " +
00179           Expr->getAsString());
00180     }
00181 
00182     std::string Format;
00183     if (StringInit *SI = dyn_cast<StringInit>(Expr->arg_begin()[0]))
00184       Format = SI->getValue();
00185     else
00186       PrintFatalError(Loc,  "Format must be a string: " + Expr->getAsString());
00187 
00188     int64_t From, To;
00189     if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[1]))
00190       From = II->getValue();
00191     else
00192       PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
00193     if (From < 0 || From >= (1 << 30))
00194       PrintFatalError(Loc, "From out of range");
00195 
00196     if (IntInit *II = dyn_cast<IntInit>(Expr->arg_begin()[2]))
00197       To = II->getValue();
00198     else
00199       PrintFatalError(Loc, "From must be an integer: " + Expr->getAsString());
00200     if (To < 0 || To >= (1 << 30))
00201       PrintFatalError(Loc, "To out of range");
00202 
00203     RecordKeeper &Records =
00204       cast<DefInit>(Expr->getOperator())->getDef()->getRecords();
00205 
00206     Step *= From <= To ? 1 : -1;
00207     while (true) {
00208       if (Step > 0 && From > To)
00209         break;
00210       else if (Step < 0 && From < To)
00211         break;
00212       std::string Name;
00213       raw_string_ostream OS(Name);
00214       OS << format(Format.c_str(), unsigned(From));
00215       Record *Rec = Records.getDef(OS.str());
00216       if (!Rec)
00217         PrintFatalError(Loc, "No def named '" + Name + "': " +
00218           Expr->getAsString());
00219       // Try to reevaluate Rec in case it is a set.
00220       if (const RecVec *Result = ST.expand(Rec))
00221         Elts.insert(Result->begin(), Result->end());
00222       else
00223         Elts.insert(Rec);
00224 
00225       From += Step;
00226     }
00227   }
00228 };
00229 
00230 // Expand a Def into a set by evaluating one of its fields.
00231 struct FieldExpander : public SetTheory::Expander {
00232   StringRef FieldName;
00233 
00234   FieldExpander(StringRef fn) : FieldName(fn) {}
00235 
00236   void expand(SetTheory &ST, Record *Def, RecSet &Elts) override {
00237     ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
00238   }
00239 };
00240 } // end anonymous namespace
00241 
00242 // Pin the vtables to this file.
00243 void SetTheory::Operator::anchor() {}
00244 void SetTheory::Expander::anchor() {}
00245 
00246 
00247 SetTheory::SetTheory() {
00248   addOperator("add", new AddOp);
00249   addOperator("sub", new SubOp);
00250   addOperator("and", new AndOp);
00251   addOperator("shl", new ShlOp);
00252   addOperator("trunc", new TruncOp);
00253   addOperator("rotl", new RotOp(false));
00254   addOperator("rotr", new RotOp(true));
00255   addOperator("decimate", new DecimateOp);
00256   addOperator("interleave", new InterleaveOp);
00257   addOperator("sequence", new SequenceOp);
00258 }
00259 
00260 void SetTheory::addOperator(StringRef Name, Operator *Op) {
00261   Operators[Name] = Op;
00262 }
00263 
00264 void SetTheory::addExpander(StringRef ClassName, Expander *E) {
00265   Expanders[ClassName] = E;
00266 }
00267 
00268 void SetTheory::addFieldExpander(StringRef ClassName, StringRef FieldName) {
00269   addExpander(ClassName, new FieldExpander(FieldName));
00270 }
00271 
00272 void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc) {
00273   // A def in a list can be a just an element, or it may expand.
00274   if (DefInit *Def = dyn_cast<DefInit>(Expr)) {
00275     if (const RecVec *Result = expand(Def->getDef()))
00276       return Elts.insert(Result->begin(), Result->end());
00277     Elts.insert(Def->getDef());
00278     return;
00279   }
00280 
00281   // Lists simply expand.
00282   if (ListInit *LI = dyn_cast<ListInit>(Expr))
00283     return evaluate(LI->begin(), LI->end(), Elts, Loc);
00284 
00285   // Anything else must be a DAG.
00286   DagInit *DagExpr = dyn_cast<DagInit>(Expr);
00287   if (!DagExpr)
00288     PrintFatalError(Loc, "Invalid set element: " + Expr->getAsString());
00289   DefInit *OpInit = dyn_cast<DefInit>(DagExpr->getOperator());
00290   if (!OpInit)
00291     PrintFatalError(Loc, "Bad set expression: " + Expr->getAsString());
00292   Operator *Op = Operators.lookup(OpInit->getDef()->getName());
00293   if (!Op)
00294     PrintFatalError(Loc, "Unknown set operator: " + Expr->getAsString());
00295   Op->apply(*this, DagExpr, Elts, Loc);
00296 }
00297 
00298 const RecVec *SetTheory::expand(Record *Set) {
00299   // Check existing entries for Set and return early.
00300   ExpandMap::iterator I = Expansions.find(Set);
00301   if (I != Expansions.end())
00302     return &I->second;
00303 
00304   // This is the first time we see Set. Find a suitable expander.
00305   const std::vector<Record*> &SC = Set->getSuperClasses();
00306   for (unsigned i = 0, e = SC.size(); i != e; ++i) {
00307     // Skip unnamed superclasses.
00308     if (!dyn_cast<StringInit>(SC[i]->getNameInit()))
00309       continue;
00310     if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
00311       // This breaks recursive definitions.
00312       RecVec &EltVec = Expansions[Set];
00313       RecSet Elts;
00314       Exp->expand(*this, Set, Elts);
00315       EltVec.assign(Elts.begin(), Elts.end());
00316       return &EltVec;
00317     }
00318   }
00319 
00320   // Set is not expandable.
00321   return nullptr;
00322 }
00323