1 //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
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.
22 // - (add S1, S2 ...) Union sets. This is also how sets are created from element
25 // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
26 // elements in S2, ...
28 // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
30 // - (shl S, N) Shift left. Remove the first N elements from S.
32 // - (trunc S, N) Truncate. The first N elements of S.
34 // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
36 // - (rotr S, N) Rotate right.
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
42 // - (sequence "Format", From, To) Generate a sequence of defs with printf.
43 // For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ]
45 //===----------------------------------------------------------------------===//
47 #ifndef LLVM_TABLEGEN_SETTHEORY_H
48 #define LLVM_TABLEGEN_SETTHEORY_H
50 #include "llvm/ADT/SetVector.h"
51 #include "llvm/ADT/StringMap.h"
52 #include "llvm/Support/SourceMgr.h"
64 typedef std::vector<Record*> RecVec;
65 typedef SmallSetVector<Record*, 16> RecSet;
67 /// Operator - A callback representing a DAG operator.
69 virtual void anchor();
71 virtual ~Operator() {}
73 /// apply - Apply this operator to Expr's arguments and insert the result
75 virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
76 ArrayRef<SMLoc> Loc) =0;
79 /// Expander - A callback function that can transform a Record representing a
80 /// set into a fully expanded list of elements. Expanders provide a way for
81 /// users to define named sets that can be used in DAG expressions.
83 virtual void anchor();
85 virtual ~Expander() {}
87 virtual void expand(SetTheory&, Record*, RecSet &Elts) =0;
91 // Map set defs to their fully expanded contents. This serves as a memoization
92 // cache and it makes it possible to return const references on queries.
93 typedef std::map<Record*, RecVec> ExpandMap;
96 // Known DAG operators by name.
97 StringMap<std::unique_ptr<Operator>> Operators;
99 // Typed expanders by class name.
100 StringMap<std::unique_ptr<Expander>> Expanders;
103 /// Create a SetTheory instance with only the standard operators.
106 /// addExpander - Add an expander for Records with the named super class.
107 void addExpander(StringRef ClassName, std::unique_ptr<Expander>);
109 /// addFieldExpander - Add an expander for ClassName that simply evaluates
110 /// FieldName in the Record to get the set elements. That is all that is
111 /// needed for a class like:
113 /// class Set<dag d> {
117 void addFieldExpander(StringRef ClassName, StringRef FieldName);
119 /// addOperator - Add a DAG operator.
120 void addOperator(StringRef Name, std::unique_ptr<Operator>);
122 /// evaluate - Evaluate Expr and append the resulting set to Elts.
123 void evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
125 /// evaluate - Evaluate a sequence of Inits and append to Elts.
126 template<typename Iter>
127 void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef<SMLoc> Loc) {
129 evaluate(*begin++, Elts, Loc);
132 /// expand - Expand a record into a set of elements if possible. Return a
133 /// pointer to the expanded elements, or NULL if Set cannot be expanded
135 const RecVec *expand(Record *Set);
138 } // end namespace llvm