1 //===- SubtargetFeatureInfo.h - Helpers for subtarget features --*- 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 #ifndef LLVM_UTIL_TABLEGEN_SUBTARGETFEATUREINFO_H
10 #define LLVM_UTIL_TABLEGEN_SUBTARGETFEATUREINFO_H
11 
12 #include "llvm/TableGen/Record.h"
13 #include <map>
14 #include <string>
15 #include <vector>
16 
17 namespace llvm {
18 struct SubtargetFeatureInfo;
19 using SubtargetFeatureInfoMap = std::map<Record *, SubtargetFeatureInfo, LessRecordByID>;
20 
21 /// Helper class for storing information on a subtarget feature which
22 /// participates in instruction matching.
23 struct SubtargetFeatureInfo {
24   /// The predicate record for this feature.
25   Record *TheDef;
26 
27   /// An unique index assigned to represent this feature.
28   uint64_t Index;
29 
30   SubtargetFeatureInfo(Record *D, uint64_t Idx) : TheDef(D), Index(Idx) {}
31 
32   /// The name of the enumerated constant identifying this feature.
33   std::string getEnumName() const {
34     return "Feature_" + TheDef->getName().str();
35   }
36 
37   /// The name of the enumerated constant identifying the bitnumber for
38   /// this feature.
39   std::string getEnumBitName() const {
40     return "Feature_" + TheDef->getName().str() + "Bit";
41   }
42 
43   bool mustRecomputePerFunction() const {
44     return TheDef->getValueAsBit("RecomputePerFunction");
45   }
46 
47   void dump() const;
48   static std::vector<std::pair<Record *, SubtargetFeatureInfo>>
49   getAll(const RecordKeeper &Records);
50 
51   /// Emit the subtarget feature flag definitions.
52   ///
53   /// This version emits the bit index for the feature and can therefore support
54   /// more than 64 feature bits.
55   static void
56   emitSubtargetFeatureBitEnumeration(SubtargetFeatureInfoMap &SubtargetFeatures,
57                                      raw_ostream &OS);
58 
59   static void emitNameTable(SubtargetFeatureInfoMap &SubtargetFeatures,
60                             raw_ostream &OS);
61 
62   /// Emit the function to compute the list of available features given a
63   /// subtarget.
64   ///
65   /// This version is used for subtarget features defined using Predicate<>
66   /// and supports more than 64 feature bits.
67   ///
68   /// \param TargetName The name of the target as used in class prefixes (e.g.
69   ///                   <TargetName>Subtarget)
70   /// \param ClassName  The name of the class (without the <Target> prefix)
71   ///                   that will contain the generated functions.
72   /// \param FuncName   The name of the function to emit.
73   /// \param SubtargetFeatures A map of TableGen records to the
74   ///                          SubtargetFeatureInfo equivalent.
75   /// \param ExtraParams Additional arguments to the generated function.
76   static void
77   emitComputeAvailableFeatures(StringRef TargetName, StringRef ClassName,
78                                StringRef FuncName,
79                                SubtargetFeatureInfoMap &SubtargetFeatures,
80                                raw_ostream &OS, StringRef ExtraParams = "");
81 
82   /// Emit the function to compute the list of available features given a
83   /// subtarget.
84   ///
85   /// This version is used for subtarget features defined using
86   /// AssemblerPredicate<> and supports up to 64 feature bits.
87   ///
88   /// \param TargetName The name of the target as used in class prefixes (e.g.
89   ///                   <TargetName>Subtarget)
90   /// \param ClassName  The name of the class (without the <Target> prefix)
91   ///                   that will contain the generated functions.
92   /// \param FuncName   The name of the function to emit.
93   /// \param SubtargetFeatures A map of TableGen records to the
94   ///                          SubtargetFeatureInfo equivalent.
95   static void emitComputeAssemblerAvailableFeatures(
96       StringRef TargetName, StringRef ClassName, StringRef FuncName,
97       SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS);
98 };
99 } // end namespace llvm
100 
101 #endif // LLVM_UTIL_TABLEGEN_SUBTARGETFEATUREINFO_H
102