Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:36

0001 //===- SetTheory.h - Generate ordered sets from DAG expressions -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements the SetTheory class that computes ordered sets of
0010 // Records from DAG expressions.  Operators for standard set operations are
0011 // predefined, and it is possible to add special purpose set operators as well.
0012 //
0013 // The user may define named sets as Records of predefined classes. Set
0014 // expanders can be added to a SetTheory instance to teach it how to find the
0015 // elements of such a named set.
0016 //
0017 // These are the predefined operators. The argument lists can be individual
0018 // elements (defs), other sets (defs of expandable classes), lists, or DAG
0019 // expressions that are evaluated recursively.
0020 //
0021 // - (add S1, S2 ...) Union sets. This is also how sets are created from element
0022 //   lists.
0023 //
0024 // - (sub S1, S2, ...) Set difference. Every element in S1 except for the
0025 //   elements in S2, ...
0026 //
0027 // - (and S1, S2) Set intersection. Every element in S1 that is also in S2.
0028 //
0029 // - (shl S, N) Shift left. Remove the first N elements from S.
0030 //
0031 // - (trunc S, N) Truncate. The first N elements of S.
0032 //
0033 // - (rotl S, N) Rotate left. Same as (add (shl S, N), (trunc S, N)).
0034 //
0035 // - (rotr S, N) Rotate right.
0036 //
0037 // - (decimate S, N) Decimate S by picking every N'th element, starting with
0038 //   the first one. For instance, (decimate S, 2) returns the even elements of
0039 //   S.
0040 //
0041 // - (sequence "Format", From, To, [Stride]) Generate a sequence of defs with
0042 //   printf. For instance, (sequence "R%u", 0, 3) -> [ R0, R1, R2, R3 ] and
0043 //   (sequence "R%u", 20, 30, 5) -> [ R20, R25, R30 ].
0044 //
0045 //===----------------------------------------------------------------------===//
0046 
0047 #ifndef LLVM_TABLEGEN_SETTHEORY_H
0048 #define LLVM_TABLEGEN_SETTHEORY_H
0049 
0050 #include "llvm/ADT/ArrayRef.h"
0051 #include "llvm/ADT/SetVector.h"
0052 #include "llvm/ADT/StringMap.h"
0053 #include "llvm/ADT/StringRef.h"
0054 #include "llvm/Support/SMLoc.h"
0055 #include <map>
0056 #include <memory>
0057 #include <vector>
0058 
0059 namespace llvm {
0060 
0061 class DagInit;
0062 class Init;
0063 class Record;
0064 
0065 class SetTheory {
0066 public:
0067   using RecVec = std::vector<const Record *>;
0068   using RecSet = SmallSetVector<const Record *, 16>;
0069 
0070   /// Operator - A callback representing a DAG operator.
0071   class Operator {
0072     virtual void anchor();
0073 
0074   public:
0075     virtual ~Operator() = default;
0076 
0077     /// apply - Apply this operator to Expr's arguments and insert the result
0078     /// in Elts.
0079     virtual void apply(SetTheory &, const DagInit *Expr, RecSet &Elts,
0080                        ArrayRef<SMLoc> Loc) = 0;
0081   };
0082 
0083   /// Expander - A callback function that can transform a Record representing a
0084   /// set into a fully expanded list of elements. Expanders provide a way for
0085   /// users to define named sets that can be used in DAG expressions.
0086   class Expander {
0087     virtual void anchor();
0088 
0089   public:
0090     virtual ~Expander() = default;
0091 
0092     virtual void expand(SetTheory &, const Record *, RecSet &Elts) = 0;
0093   };
0094 
0095 private:
0096   // Map set defs to their fully expanded contents. This serves as a memoization
0097   // cache and it makes it possible to return const references on queries.
0098   using ExpandMap = std::map<const Record *, RecVec>;
0099   ExpandMap Expansions;
0100 
0101   // Known DAG operators by name.
0102   StringMap<std::unique_ptr<Operator>> Operators;
0103 
0104   // Typed expanders by class name.
0105   StringMap<std::unique_ptr<Expander>> Expanders;
0106 
0107 public:
0108   /// Create a SetTheory instance with only the standard operators.
0109   SetTheory();
0110 
0111   /// addExpander - Add an expander for Records with the named super class.
0112   void addExpander(StringRef ClassName, std::unique_ptr<Expander>);
0113 
0114   /// addFieldExpander - Add an expander for ClassName that simply evaluates
0115   /// FieldName in the Record to get the set elements.  That is all that is
0116   /// needed for a class like:
0117   ///
0118   ///   class Set<dag d> {
0119   ///     dag Elts = d;
0120   ///   }
0121   ///
0122   void addFieldExpander(StringRef ClassName, StringRef FieldName);
0123 
0124   /// addOperator - Add a DAG operator.
0125   void addOperator(StringRef Name, std::unique_ptr<Operator>);
0126 
0127   /// evaluate - Evaluate Expr and append the resulting set to Elts.
0128   void evaluate(const Init *Expr, RecSet &Elts, ArrayRef<SMLoc> Loc);
0129 
0130   /// evaluate - Evaluate a sequence of Inits and append to Elts.
0131   template<typename Iter>
0132   void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef<SMLoc> Loc) {
0133     while (begin != end)
0134       evaluate(*begin++, Elts, Loc);
0135   }
0136 
0137   /// expand - Expand a record into a set of elements if possible.  Return a
0138   /// pointer to the expanded elements, or NULL if Set cannot be expanded
0139   /// further.
0140   const RecVec *expand(const Record *Set);
0141 };
0142 
0143 } // end namespace llvm
0144 
0145 #endif // LLVM_TABLEGEN_SETTHEORY_H