SetTheory.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
  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. Operators for standard set operations are
  12. // predefined, and it is possible to add special purpose set operators as well.
  13. //
  14. // The user may define named sets as Records of predefined classes. Set
  15. // expanders can be added to a SetTheory instance to teach it how to find the
  16. // elements of such a named set.
  17. //
  18. // These are the predefined operators. The argument lists can be individual
  19. // elements (defs), other sets (defs of expandable classes), lists, or DAG
  20. // expressions that are evaluated recursively.
  21. //
  22. // - (add S1, S2 ...) Union sets. This is also how sets are created from element
  23. // lists.
  24. //
  25. // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
  26. // elements in S2, ...
  27. //
  28. // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
  29. //
  30. // - (shl S, N) Shift left. Remove the first N elements from S.
  31. //
  32. // - (trunc S, N) Truncate. The first N elements of S.
  33. //
  34. // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
  35. //
  36. // - (rotr S, N) Rotate right.
  37. //
  38. // - (decimate S, N) Decimate S by picking every N'th element, starting with
  39. // the first one. For instance, (decimate S, 2) returns the even elements of
  40. // S.
  41. //
  42. // - (sequence "Format", From, To) Generate a sequence of defs with printf.
  43. // For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ]
  44. //
  45. //===----------------------------------------------------------------------===//
  46. #ifndef LLVM_TABLEGEN_SETTHEORY_H
  47. #define LLVM_TABLEGEN_SETTHEORY_H
  48. #include "llvm/ADT/ArrayRef.h"
  49. #include "llvm/ADT/SetVector.h"
  50. #include "llvm/ADT/StringMap.h"
  51. #include "llvm/Support/SMLoc.h"
  52. #include <map>
  53. #include <vector>
  54. namespace llvm {
  55. class DagInit;
  56. class Init;
  57. class Record;
  58. class SetTheory {
  59. public:
  60. typedef std::vector<Record*> RecVec;
  61. typedef SmallSetVector<Record*, 16> RecSet;
  62. /// Operator - A callback representing a DAG operator.
  63. class Operator {
  64. virtual void anchor();
  65. public:
  66. virtual ~Operator() {}
  67. /// apply - Apply this operator to Expr's arguments and insert the result
  68. /// in Elts.
  69. virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
  70. ArrayRef<SMLoc> Loc) =0;
  71. };
  72. /// Expander - A callback function that can transform a Record representing a
  73. /// set into a fully expanded list of elements. Expanders provide a way for
  74. /// users to define named sets that can be used in DAG expressions.
  75. class Expander {
  76. virtual void anchor();
  77. public:
  78. virtual ~Expander() {}
  79. virtual void expand(SetTheory&, Record*, RecSet &Elts) =0;
  80. };
  81. private:
  82. // Map set defs to their fully expanded contents. This serves as a memoization
  83. // cache and it makes it possible to return const references on queries.
  84. typedef std::map<Record*, RecVec> ExpandMap;
  85. ExpandMap Expansions;
  86. // Known DAG operators by name.
  87. StringMap<std::unique_ptr<Operator>> Operators;
  88. // Typed expanders by class name.
  89. StringMap<std::unique_ptr<Expander>> Expanders;
  90. public:
  91. /// Create a SetTheory instance with only the standard operators.
  92. SetTheory();
  93. /// addExpander - Add an expander for Records with the named super class.
  94. void addExpander(StringRef ClassName, std::unique_ptr<Expander>);
  95. /// addFieldExpander - Add an expander for ClassName that simply evaluates
  96. /// FieldName in the Record to get the set elements. That is all that is
  97. /// needed for a class like:
  98. ///
  99. /// class Set<dag d> {
  100. /// dag Elts = d;
  101. /// }
  102. ///
  103. void addFieldExpander(StringRef ClassName, StringRef FieldName);
  104. /// addOperator - Add a DAG operator.
  105. void addOperator(StringRef Name, std::unique_ptr<Operator>);
  106. /// evaluate - Evaluate Expr and append the resulting set to Elts.
  107. void evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
  108. /// evaluate - Evaluate a sequence of Inits and append to Elts.
  109. template<typename Iter>
  110. void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef<SMLoc> Loc) {
  111. while (begin != end)
  112. evaluate(*begin++, Elts, Loc);
  113. }
  114. /// expand - Expand a record into a set of elements if possible. Return a
  115. /// pointer to the expanded elements, or NULL if Set cannot be expanded
  116. /// further.
  117. const RecVec *expand(Record *Set);
  118. };
  119. } // end namespace llvm
  120. #endif