1//===- GlobalISelMatchTableExecutorEmitter.h ------------------------------===//
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/// \file
10/// This file contains common code related to emitting
11/// GIMatchTableExecutor-derived classes.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_GLOBALISELMATCHTABLEEXECUTOREMITTER_H
16#define LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_GLOBALISELMATCHTABLEEXECUTOREMITTER_H
17
18#include "Common/SubtargetFeatureInfo.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include <functional>
23
24namespace llvm {
25class CodeGenTarget;
26
27namespace gi {
28class RuleMatcher;
29class LLTCodeGen;
30class MatchTable;
31} // namespace gi
32
33/// Abstract base class for TableGen backends that emit a
34/// `GIMatchTableExecutor`-derived class.
35class GlobalISelMatchTableExecutorEmitter {
36 /// Emits logic to check features required by \p Rules using the
37
38 /// SubtargetFeatures map.
39 void emitSubtargetFeatureBitsetImpl(raw_ostream &OS,
40 ArrayRef<gi::RuleMatcher> Rules);
41
42 /// Emits an enum + an array that stores references to
43 /// \p ComplexOperandMatchers.
44 void emitComplexPredicates(raw_ostream &OS,
45 ArrayRef<const Record *> ComplexOperandMatchers);
46
47 /// Emits an enum + an array that stores references to
48 /// \p CustomOperandRenderers.
49 void emitCustomOperandRenderers(raw_ostream &OS,
50 ArrayRef<StringRef> CustomOperandRenderers);
51
52 /// Emits an enum + an array to reference \p TypeObjects (LLTs) in the match
53 /// table.
54 void emitTypeObjects(raw_ostream &OS, ArrayRef<gi::LLTCodeGen> TypeObjects);
55
56 /// Emits the getMatchTable function which contains all of the match table's
57 /// opcodes.
58 void emitMatchTable(raw_ostream &OS, const gi::MatchTable &Table);
59
60 /// Emits the getLLTForHwMode function which returns the type for an index
61 /// used by the *ByHwMode opcodes.
62 void emitLLTForHwModeFn(raw_ostream &OS);
63
64 /// Helper function to emit `test` functions for the executor. This emits both
65 /// an enum to reference predicates in the MatchTable, and a function to
66 /// switch over the enum & execute the predicate's C++ code.
67 ///
68 /// \tparam PredicateObject An object representing a predicate to emit.
69 /// \param OS Output stream
70 /// \param TypeIdentifier Identifier used for the type of the predicate,
71 /// e.g. `MI` for MachineInstrs.
72 /// \param ArgType Full type of the argument, e.g. `const MachineInstr &`
73 /// \param ArgName Name of the argument, e.g. `MI` for MachineInstrs.
74 /// \param AdditionalArgs Optional additional argument declarations.
75 /// \param AdditionalDeclarations Optional declarations to write at the start
76 /// of the function, before switching over the predicates enum.
77 /// \param Predicates Predicates to emit.
78 /// \param GetPredEnumName Returns an enum name for a given predicate.
79 /// \param GetPredCode Returns the C++ code of a given predicate.
80 /// \param Comment Optional comment for the enum declaration.
81 template <typename PredicateObject>
82 void emitCxxPredicateFns(
83 raw_ostream &OS, StringRef TypeIdentifier, StringRef ArgType,
84 StringRef ArgName, StringRef AdditionalArgs,
85 StringRef AdditionalDeclarations, ArrayRef<PredicateObject> Predicates,
86 llvm::function_ref<StringRef(PredicateObject)> GetPredEnumName,
87 llvm::function_ref<StringRef(PredicateObject)> GetPredCode,
88 StringRef Comment) {
89 if (!Comment.empty())
90 OS << "// " << Comment << "\n";
91 if (!Predicates.empty()) {
92 OS << "enum {\n";
93 StringRef EnumeratorSeparator = " = GICXXPred_Invalid + 1,\n";
94 for (const auto &Pred : Predicates) {
95 OS << " GICXXPred_" << TypeIdentifier << "_Predicate_"
96 << GetPredEnumName(Pred) << EnumeratorSeparator;
97 EnumeratorSeparator = ",\n";
98 }
99 OS << "};\n";
100 }
101
102 OS << "bool " << getClassName() << "::test" << ArgName << "Predicate_"
103 << TypeIdentifier << "(unsigned PredicateID, " << ArgType << " "
104 << ArgName << AdditionalArgs << ") const {\n"
105 << AdditionalDeclarations;
106 if (!AdditionalDeclarations.empty())
107 OS << "\n";
108 if (!Predicates.empty()) {
109 OS << " switch (PredicateID) {\n";
110 for (const auto &Pred : Predicates) {
111 // Ensure all code is indented.
112 const auto Code = join(split(GetPredCode(Pred).str(), "\n"), "\n ");
113 OS << " case GICXXPred_" << TypeIdentifier << "_Predicate_"
114 << GetPredEnumName(Pred) << ": {\n"
115 << " " << Code << "\n";
116 if (!StringRef(Code).ltrim().starts_with(Prefix: "return")) {
117 OS << " llvm_unreachable(\"" << GetPredEnumName(Pred)
118 << " should have returned\");\n";
119 }
120 OS << " }\n";
121 }
122 OS << " }\n";
123 }
124 OS << " llvm_unreachable(\"Unknown predicate\");\n"
125 << " return false;\n"
126 << "}\n";
127 }
128
129protected:
130 /// Emits `testMIPredicate_MI`.
131 /// \tparam PredicateObject An object representing a predicate to emit.
132 /// \param OS Output stream
133 /// \param AdditionalDecls Additional C++ variable declarations.
134 /// \param Predicates Predicates to emit.
135 /// \param GetPredEnumName Returns an enum name for a given predicate.
136 /// \param GetPredCode Returns the C++ code of a given predicate.
137 /// \param Comment Optional comment for the enum declaration.
138 template <typename PredicateObject>
139 void emitMIPredicateFnsImpl(
140 raw_ostream &OS, StringRef AdditionalDecls,
141 ArrayRef<PredicateObject> Predicates,
142 llvm::function_ref<StringRef(PredicateObject)> GetPredEnumName,
143 llvm::function_ref<StringRef(PredicateObject)> GetPredCode,
144 StringRef Comment = "") {
145 return emitCxxPredicateFns(
146 OS, "MI", "const MachineInstr &", "MI", ", const MatcherState &State",
147 AdditionalDecls, Predicates, GetPredEnumName, GetPredCode, Comment);
148 }
149
150 /// Emits `testMOPredicate_MO`.
151 /// \tparam PredicateObject An object representing a predicate to emit.
152 /// \param OS Output stream.
153 /// \param AdditionalDecls Additional C++ variable declarations.
154 /// \param Predicates Predicates to emit.
155 /// \param GetPredEnumName Returns an enum name for a given predicate.
156 /// \param GetPredCode Returns the C++ code of a given predicate.
157 /// \param Comment Optional comment for the enum declaration.
158 template <typename PredicateObject>
159 void emitLeafPredicateFnsImpl(
160 raw_ostream &OS, StringRef AdditionalDecls,
161 ArrayRef<PredicateObject> Predicates,
162 llvm::function_ref<StringRef(PredicateObject)> GetPredEnumName,
163 llvm::function_ref<StringRef(PredicateObject)> GetPredCode,
164 StringRef Comment = "") {
165 return emitCxxPredicateFns(
166 OS, "MO", "const MachineOperand &", "MO", ", const MatcherState &State",
167 AdditionalDecls, Predicates, GetPredEnumName, GetPredCode, Comment);
168 }
169
170 /// Helper function to emit the following executor functions:
171 /// * testImmPredicate_I64 (TypeIdentifier=I64)
172 /// * testImmPredicate_APInt (TypeIdentifier=APInt)
173 /// * testImmPredicate_APFloat (TypeIdentifier=APFloat)
174 ///
175 /// \tparam PredicateObject An object representing a predicate to emit.
176 /// \param OS Output stream
177 /// \param TypeIdentifier Identifier used for the type of the predicate
178 /// \param ArgType Full type of the argument
179 /// \param Predicates Predicates to emit.
180 /// \param GetPredEnumName Returns an enum name for a given predicate.
181 /// \param GetPredCode Returns the C++ code of a given predicate.
182 /// \param Comment Optional comment for the enum declaration.
183 template <typename PredicateObject>
184 void emitImmPredicateFnsImpl(
185 raw_ostream &OS, StringRef TypeIdentifier, StringRef ArgType,
186 ArrayRef<PredicateObject> Predicates,
187 llvm::function_ref<StringRef(PredicateObject)> GetPredEnumName,
188 llvm::function_ref<StringRef(PredicateObject)> GetPredCode,
189 StringRef Comment = "") {
190 return emitCxxPredicateFns(OS, TypeIdentifier, ArgType, "Imm", "", "",
191 Predicates, GetPredEnumName, GetPredCode,
192 Comment);
193 }
194
195 GlobalISelMatchTableExecutorEmitter();
196
197public:
198 virtual ~GlobalISelMatchTableExecutorEmitter() = default;
199
200 virtual const CodeGenTarget &getTarget() const = 0;
201
202 /// \returns the name of the class being emitted including any prefixes, e.g.
203 /// `AMDGPUInstructionSelector`.
204 virtual StringRef getClassName() const = 0;
205
206 /// Emit additional content in emitExecutorImpl
207 virtual void emitAdditionalImpl(raw_ostream &OS) {}
208
209 /// Emit additional content in emitTemporariesInit.
210 virtual void emitAdditionalTemporariesInit(raw_ostream &OS) {}
211
212 /// Emit the `testMIPredicate_MI` function.
213 /// Note: `emitMIPredicateFnsImpl` can be used to do most of the work.
214 virtual void emitMIPredicateFns(raw_ostream &OS) = 0;
215
216 /// Emit the `testLeafPredicate` function
217 /// Note `emitLeafPredicateFnsImpl` can be used to do most of the work.
218 virtual void emitLeafPredicateFns(raw_ostream &OS) = 0;
219
220 /// Emit the `testImmPredicate_I64` function.
221 /// Note: `emitImmPredicateFnsImpl` can be used to do most of the work.
222 virtual void emitI64ImmPredicateFns(raw_ostream &OS) = 0;
223
224 /// Emit the `testImmPredicate_APFloat` function.
225 /// Note: `emitImmPredicateFnsImpl` can be used to do most of the work.
226 virtual void emitAPFloatImmPredicateFns(raw_ostream &OS) = 0;
227
228 /// Emit the `testImmPredicate_APInt` function.
229 /// Note: `emitImmPredicateFnsImpl` can be used to do most of the work.
230 virtual void emitAPIntImmPredicateFns(raw_ostream &OS) = 0;
231 virtual void emitTestSimplePredicate(raw_ostream &OS) = 0;
232 virtual void emitRunCustomAction(raw_ostream &OS) = 0;
233
234 void emitExecutorImpl(raw_ostream &OS, const gi::MatchTable &Table,
235 ArrayRef<gi::LLTCodeGen> TypeObjects,
236 ArrayRef<gi::RuleMatcher> Rules,
237 ArrayRef<const Record *> ComplexOperandMatchers,
238 ArrayRef<StringRef> CustomOperandRenderers,
239 StringRef IfDefName);
240 void emitPredicateBitset(raw_ostream &OS, StringRef IfDefName);
241 void emitTemporariesDecl(raw_ostream &OS, StringRef IfDefName);
242 void emitTemporariesInit(raw_ostream &OS, unsigned MaxTemporaries,
243 StringRef IfDefName);
244 void emitPredicatesDecl(raw_ostream &OS, StringRef IfDefName);
245 void emitPredicatesInit(raw_ostream &OS, StringRef IfDefName);
246
247 // Map of predicates to their subtarget features.
248 SubtargetFeatureInfoMap SubtargetFeatures;
249
250 std::map<std::string, unsigned> HwModes;
251};
252} // namespace llvm
253
254#endif // LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_GLOBALISELMATCHTABLEEXECUTOREMITTER_H
255