[Alignment][NFC] Support compile time constants
[llvm-core.git] / include / llvm / TableGen / SetTheory.h
blob35156424b0d30eb0e3e6f0974a9d40e7fb915674
1 //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the SetTheory class that computes ordered sets of
10 // Records from DAG expressions. Operators for standard set operations are
11 // predefined, and it is possible to add special purpose set operators as well.
13 // The user may define named sets as Records of predefined classes. Set
14 // expanders can be added to a SetTheory instance to teach it how to find the
15 // elements of such a named set.
17 // These are the predefined operators. The argument lists can be individual
18 // elements (defs), other sets (defs of expandable classes), lists, or DAG
19 // expressions that are evaluated recursively.
21 // - (add S1, S2 ...) Union sets. This is also how sets are created from element
22 // lists.
24 // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
25 // elements in S2, ...
27 // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
29 // - (shl S, N) Shift left. Remove the first N elements from S.
31 // - (trunc S, N) Truncate. The first N elements of S.
33 // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
35 // - (rotr S, N) Rotate right.
37 // - (decimate S, N) Decimate S by picking every N'th element, starting with
38 // the first one. For instance, (decimate S, 2) returns the even elements of
39 // S.
41 // - (sequence "Format", From, To) Generate a sequence of defs with printf.
42 // For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ]
44 //===----------------------------------------------------------------------===//
46 #ifndef LLVM_TABLEGEN_SETTHEORY_H
47 #define LLVM_TABLEGEN_SETTHEORY_H
49 #include "llvm/ADT/ArrayRef.h"
50 #include "llvm/ADT/SetVector.h"
51 #include "llvm/ADT/StringMap.h"
52 #include "llvm/ADT/StringRef.h"
53 #include "llvm/Support/SMLoc.h"
54 #include <map>
55 #include <memory>
56 #include <vector>
58 namespace llvm {
60 class DagInit;
61 class Init;
62 class Record;
64 class SetTheory {
65 public:
66 using RecVec = std::vector<Record *>;
67 using RecSet = SmallSetVector<Record *, 16>;
69 /// Operator - A callback representing a DAG operator.
70 class Operator {
71 virtual void anchor();
73 public:
74 virtual ~Operator() = default;
76 /// apply - Apply this operator to Expr's arguments and insert the result
77 /// in Elts.
78 virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
79 ArrayRef<SMLoc> Loc) = 0;
82 /// Expander - A callback function that can transform a Record representing a
83 /// set into a fully expanded list of elements. Expanders provide a way for
84 /// users to define named sets that can be used in DAG expressions.
85 class Expander {
86 virtual void anchor();
88 public:
89 virtual ~Expander() = default;
91 virtual void expand(SetTheory&, Record*, RecSet &Elts) = 0;
94 private:
95 // Map set defs to their fully expanded contents. This serves as a memoization
96 // cache and it makes it possible to return const references on queries.
97 using ExpandMap = std::map<Record *, RecVec>;
98 ExpandMap Expansions;
100 // Known DAG operators by name.
101 StringMap<std::unique_ptr<Operator>> Operators;
103 // Typed expanders by class name.
104 StringMap<std::unique_ptr<Expander>> Expanders;
106 public:
107 /// Create a SetTheory instance with only the standard operators.
108 SetTheory();
110 /// addExpander - Add an expander for Records with the named super class.
111 void addExpander(StringRef ClassName, std::unique_ptr<Expander>);
113 /// addFieldExpander - Add an expander for ClassName that simply evaluates
114 /// FieldName in the Record to get the set elements. That is all that is
115 /// needed for a class like:
117 /// class Set<dag d> {
118 /// dag Elts = d;
119 /// }
121 void addFieldExpander(StringRef ClassName, StringRef FieldName);
123 /// addOperator - Add a DAG operator.
124 void addOperator(StringRef Name, std::unique_ptr<Operator>);
126 /// evaluate - Evaluate Expr and append the resulting set to Elts.
127 void evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
129 /// evaluate - Evaluate a sequence of Inits and append to Elts.
130 template<typename Iter>
131 void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef<SMLoc> Loc) {
132 while (begin != end)
133 evaluate(*begin++, Elts, Loc);
136 /// expand - Expand a record into a set of elements if possible. Return a
137 /// pointer to the expanded elements, or NULL if Set cannot be expanded
138 /// further.
139 const RecVec *expand(Record *Set);
142 } // end namespace llvm
144 #endif // LLVM_TABLEGEN_SETTHEORY_H