LLVM API Documentation

SetTheory.h
Go to the documentation of this file.
00001 //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
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.  Operators for standard set operations are
00012 // predefined, and it is possible to add special purpose set operators as well.
00013 //
00014 // The user may define named sets as Records of predefined classes. Set
00015 // expanders can be added to a SetTheory instance to teach it how to find the
00016 // elements of such a named set.
00017 //
00018 // These are the predefined operators. The argument lists can be individual
00019 // elements (defs), other sets (defs of expandable classes), lists, or DAG
00020 // expressions that are evaluated recursively.
00021 //
00022 // - (add S1, S2 ...) Union sets. This is also how sets are created from element
00023 //   lists.
00024 //
00025 // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
00026 //   elements in S2, ...
00027 //
00028 // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
00029 //
00030 // - (shl S, N) Shift left. Remove the first N elements from S.
00031 //
00032 // - (trunc S, N) Truncate. The first N elements of S.
00033 //
00034 // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
00035 //
00036 // - (rotr S, N) Rotate right.
00037 //
00038 // - (decimate S, N) Decimate S by picking every N'th element, starting with
00039 //   the first one. For instance, (decimate S, 2) returns the even elements of
00040 //   S.
00041 //
00042 // - (sequence "Format", From, To) Generate a sequence of defs with printf.
00043 //   For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ]
00044 //
00045 //===----------------------------------------------------------------------===//
00046 
00047 #ifndef LLVM_TABLEGEN_SETTHEORY_H
00048 #define LLVM_TABLEGEN_SETTHEORY_H
00049 
00050 #include "llvm/ADT/SetVector.h"
00051 #include "llvm/ADT/StringMap.h"
00052 #include "llvm/Support/SourceMgr.h"
00053 #include <map>
00054 #include <vector>
00055 
00056 namespace llvm {
00057 
00058 class DagInit;
00059 class Init;
00060 class Record;
00061 class RecordKeeper;
00062 
00063 class SetTheory {
00064 public:
00065   typedef std::vector<Record*> RecVec;
00066   typedef SmallSetVector<Record*, 16> RecSet;
00067 
00068   /// Operator - A callback representing a DAG operator.
00069   class Operator {
00070     virtual void anchor();
00071   public:
00072     virtual ~Operator() {}
00073 
00074     /// apply - Apply this operator to Expr's arguments and insert the result
00075     /// in Elts.
00076     virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
00077                        ArrayRef<SMLoc> Loc) =0;
00078   };
00079 
00080   /// Expander - A callback function that can transform a Record representing a
00081   /// set into a fully expanded list of elements. Expanders provide a way for
00082   /// users to define named sets that can be used in DAG expressions.
00083   class Expander {
00084     virtual void anchor();
00085   public:
00086     virtual ~Expander() {}
00087 
00088     virtual void expand(SetTheory&, Record*, RecSet &Elts) =0;
00089   };
00090 
00091 private:
00092   // Map set defs to their fully expanded contents. This serves as a memoization
00093   // cache and it makes it possible to return const references on queries.
00094   typedef std::map<Record*, RecVec> ExpandMap;
00095   ExpandMap Expansions;
00096 
00097   // Known DAG operators by name.
00098   StringMap<Operator*> Operators;
00099 
00100   // Typed expanders by class name.
00101   StringMap<Expander*> Expanders;
00102 
00103 public:
00104   /// Create a SetTheory instance with only the standard operators.
00105   SetTheory();
00106 
00107   /// addExpander - Add an expander for Records with the named super class.
00108   void addExpander(StringRef ClassName, Expander*);
00109 
00110   /// addFieldExpander - Add an expander for ClassName that simply evaluates
00111   /// FieldName in the Record to get the set elements.  That is all that is
00112   /// needed for a class like:
00113   ///
00114   ///   class Set<dag d> {
00115   ///     dag Elts = d;
00116   ///   }
00117   ///
00118   void addFieldExpander(StringRef ClassName, StringRef FieldName);
00119 
00120   /// addOperator - Add a DAG operator.
00121   void addOperator(StringRef Name, Operator*);
00122 
00123   /// evaluate - Evaluate Expr and append the resulting set to Elts.
00124   void evaluate(Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
00125 
00126   /// evaluate - Evaluate a sequence of Inits and append to Elts.
00127   template<typename Iter>
00128   void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef<SMLoc> Loc) {
00129     while (begin != end)
00130       evaluate(*begin++, Elts, Loc);
00131   }
00132 
00133   /// expand - Expand a record into a set of elements if possible.  Return a
00134   /// pointer to the expanded elements, or NULL if Set cannot be expanded
00135   /// further.
00136   const RecVec *expand(Record *Set);
00137 };
00138 
00139 } // end namespace llvm
00140 
00141 #endif
00142