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_COMMON_SUBTARGETFEATUREINFO_H
10#define LLVM_UTIL_TABLEGEN_COMMON_SUBTARGETFEATUREINFO_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/TableGen/Record.h"
14#include <map>
15#include <set>
16#include <string>
17#include <tuple>
18#include <utility>
19#include <vector>
20
21namespace llvm {
22struct SubtargetFeatureInfo;
23using SubtargetFeatureInfoMap =
24 std::map<const Record *, SubtargetFeatureInfo, LessRecordByID>;
25using SubtargetFeaturesInfoVec =
26 std::vector<std::pair<const Record *, SubtargetFeatureInfo>>;
27
28/// Helper class for storing information on a subtarget feature which
29/// participates in instruction matching.
30struct SubtargetFeatureInfo {
31 /// The predicate record for this feature.
32 const Record *TheDef;
33
34 /// An unique index assigned to represent this feature.
35 uint64_t Index;
36
37 SubtargetFeatureInfo(const Record *D, uint64_t Idx) : TheDef(D), Index(Idx) {}
38
39 /// The name of the enumerated constant identifying this feature.
40 std::string getEnumName() const {
41 return "Feature_" + TheDef->getName().str();
42 }
43
44 /// The name of the enumerated constant identifying the bitnumber for
45 /// this feature.
46 std::string getEnumBitName() const {
47 return "Feature_" + TheDef->getName().str() + "Bit";
48 }
49
50 bool mustRecomputePerFunction() const {
51 return TheDef->getValueAsBit(FieldName: "RecomputePerFunction");
52 }
53
54 void dump() const;
55
56 static SubtargetFeaturesInfoVec getAll(const RecordKeeper &Records);
57
58 /// Emit the subtarget feature flag definitions.
59 ///
60 /// This version emits the bit index for the feature and can therefore support
61 /// more than 64 feature bits.
62 static void emitSubtargetFeatureBitEnumeration(
63 const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
64 const std::map<std::string, unsigned> *HwModes = nullptr);
65
66 static void emitNameTable(SubtargetFeatureInfoMap &SubtargetFeatures,
67 raw_ostream &OS);
68
69 /// Emit the function to compute the list of available features given a
70 /// subtarget.
71 ///
72 /// This version is used for subtarget features defined using Predicate<>
73 /// and supports more than 64 feature bits.
74 ///
75 /// \param TargetName The name of the target as used in class prefixes (e.g.
76 /// <TargetName>Subtarget)
77 /// \param ClassName The name of the class that will contain the generated
78 /// functions (including the target prefix.)
79 /// \param FuncName The name of the function to emit.
80 /// \param SubtargetFeatures A map of TableGen records to the
81 /// SubtargetFeatureInfo equivalent.
82 /// \param ExtraParams Additional arguments to the generated function.
83 /// \param HwModes Map of HwMode conditions to check.
84 static void emitComputeAvailableFeatures(
85 StringRef TargetName, StringRef ClassName, StringRef FuncName,
86 const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
87 StringRef ExtraParams = "",
88 const std::map<std::string, unsigned> *HwModes = nullptr);
89
90 /// Emit the function to compute the list of available features given a
91 /// subtarget.
92 ///
93 /// This version is used for subtarget features defined using
94 /// AssemblerPredicate<> and supports up to 64 feature bits.
95 ///
96 /// \param TargetName The name of the target as used in class prefixes (e.g.
97 /// <TargetName>Subtarget)
98 /// \param ClassName The name of the class (without the <Target> prefix)
99 /// that will contain the generated functions.
100 /// \param FuncName The name of the function to emit.
101 /// \param SubtargetFeatures A map of TableGen records to the
102 /// SubtargetFeatureInfo equivalent.
103 static void emitComputeAssemblerAvailableFeatures(
104 StringRef TargetName, StringRef ClassName, StringRef FuncName,
105 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS);
106
107 static void emitPredicateCheck(raw_ostream &OS,
108 ArrayRef<const Record *> Predicates);
109
110 static void emitMCPredicateCheck(raw_ostream &OS, StringRef TargetName,
111 ArrayRef<const Record *> Predicates);
112};
113
114struct SubtargetFeatureLiteral {
115 StringRef Feature;
116 bool IsNot;
117
118 bool operator<(const SubtargetFeatureLiteral &Other) const {
119 return std::tie(args: Feature, args: IsNot) < std::tie(args: Other.Feature, args: Other.IsNot);
120 }
121 bool operator==(const SubtargetFeatureLiteral &Other) const {
122 return Feature == Other.Feature && IsNot == Other.IsNot;
123 }
124};
125
126/// Populates \p FeaturesSet and \p AnyOfFeatureSets from \p ReqPredicates.
127/// Returns true if all predicates were assembler predicates with a valid
128/// AssemblerCondDag, or false if any non-assembler predicates were skipped.
129bool getRequiredFeatures(
130 std::set<SubtargetFeatureLiteral> &FeaturesSet,
131 std::set<std::set<SubtargetFeatureLiteral>> &AnyOfFeatureSets,
132 ArrayRef<const Record *> ReqPredicates);
133} // end namespace llvm
134
135#endif // LLVM_UTIL_TABLEGEN_COMMON_SUBTARGETFEATUREINFO_H
136