SetTheory.cpp 11 KB

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