1//===- CodeGenTarget.h - Target Class Wrapper -------------------*- 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// This file defines wrappers for the Target class and related global
10// functionality. This makes it easier to access the data and provides a single
11// place that needs to check it for validity. All of these classes abort
12// on error conditions.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H
17#define LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H
18
19#include "Basic/CodeGenIntrinsics.h"
20#include "Basic/SDNodeProperties.h"
21#include "CodeGenHwModes.h"
22#include "CodeGenInstruction.h"
23#include "InfoByHwMode.h"
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/CodeGenTypes/MachineValueType.h"
29#include "llvm/Support/Allocator.h"
30#include <cassert>
31#include <memory>
32#include <optional>
33#include <string>
34#include <vector>
35
36namespace llvm {
37
38class RecordKeeper;
39class Record;
40class CodeGenRegBank;
41class CodeGenRegister;
42class CodeGenRegisterClass;
43class CodeGenSchedModels;
44class CodeGenSubRegIndex;
45
46/// Returns the MVT that the specified TableGen
47/// record corresponds to.
48MVT getValueType(const Record *Rec);
49
50StringRef getEnumName(MVT T);
51
52/// getQualifiedName - Return the name of the specified record, with a
53/// namespace qualifier if the record contains one.
54std::string getQualifiedName(const Record *R);
55
56/// CodeGenTarget - This class corresponds to the Target class in the .td files.
57///
58class CodeGenTarget {
59 const RecordKeeper &Records;
60 const Record *TargetRec;
61
62 mutable DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>>
63 InstructionMap;
64 mutable std::unique_ptr<CodeGenRegBank> RegBank;
65 mutable ArrayRef<const Record *> RegAltNameIndices;
66 mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
67 mutable std::optional<std::vector<const Record *>> RegClassByHwModeList;
68 CodeGenHwModes CGH;
69 std::vector<const Record *> MacroFusions;
70 mutable bool HasVariableLengthEncodings = false;
71
72 void ReadInstructions() const;
73 void ReadLegalValueTypes() const;
74
75 mutable std::unique_ptr<CodeGenSchedModels> SchedModels;
76
77 mutable StringRef InstNamespace;
78 mutable std::vector<const CodeGenInstruction *> InstrsByEnum;
79 mutable DenseMap<const Record *, ArrayRef<const Init *>> ReversedEncodingBits;
80 mutable BumpPtrAllocator ReversedEncodingBitsAllocator;
81 mutable CodeGenIntrinsicMap Intrinsics;
82
83 mutable unsigned NumPseudoInstructions = 0;
84
85public:
86 CodeGenTarget(const RecordKeeper &Records);
87 ~CodeGenTarget();
88
89 const Record *getTargetRecord() const { return TargetRec; }
90 StringRef getName() const;
91
92 /// getInstNamespace - Return the target-specific instruction namespace.
93 ///
94 StringRef getInstNamespace() const;
95
96 /// getRegNamespace - Return the target-specific register namespace.
97 StringRef getRegNamespace() const;
98
99 /// getInstructionSet - Return the InstructionSet object.
100 ///
101 const Record *getInstructionSet() const;
102
103 /// getAllowRegisterRenaming - Return the AllowRegisterRenaming flag value for
104 /// this target.
105 ///
106 bool getAllowRegisterRenaming() const;
107
108 /// getRegistersAreIntervals - Return the RegistersAreIntervals flag value for
109 /// this target.
110 ///
111 bool getRegistersAreIntervals() const;
112
113 /// getAsmParser - Return the AssemblyParser definition for this target.
114 ///
115 const Record *getAsmParser() const;
116
117 /// getAsmParserVariant - Return the AssemblyParserVariant definition for
118 /// this target.
119 ///
120 const Record *getAsmParserVariant(unsigned i) const;
121
122 /// getAsmParserVariantCount - Return the AssemblyParserVariant definition
123 /// available for this target.
124 ///
125 unsigned getAsmParserVariantCount() const;
126
127 /// getAsmWriter - Return the AssemblyWriter definition for this target.
128 ///
129 const Record *getAsmWriter() const;
130
131 /// getRegBank - Return the register bank description.
132 CodeGenRegBank &getRegBank() const;
133
134 /// getRegisterByName - If there is a register with the specific AsmName,
135 /// return it.
136 const CodeGenRegister *getRegisterByName(StringRef Name) const;
137
138 ArrayRef<const Record *> getRegAltNameIndices() const {
139 if (RegAltNameIndices.empty())
140 RegAltNameIndices = Records.getAllDerivedDefinitions(ClassName: "RegAltNameIndex");
141 return RegAltNameIndices;
142 }
143
144 const CodeGenRegisterClass &getRegisterClass(const Record *R,
145 ArrayRef<SMLoc> Loc = {}) const;
146
147 /// Convenience wrapper to avoid hardcoding the name of RegClassByHwMode
148 /// everywhere. This is here instead of CodeGenRegBank to avoid the fatal
149 /// error that occurs when no RegisterClasses are defined when constructing
150 /// the bank. The empty ptr_rc placeholder is excluded: it carries no register
151 /// classes and only exists to be substituted per target.
152 ArrayRef<const Record *> getAllRegClassByHwMode() const;
153
154 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
155 /// specified physical register.
156 std::vector<ValueTypeByHwMode> getRegisterVTs(const Record *R) const;
157
158 ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const {
159 if (LegalValueTypes.empty())
160 ReadLegalValueTypes();
161 return LegalValueTypes;
162 }
163
164 /// If \p V is a DefInit that can be interpreted as a RegisterClass (e.g.,
165 /// it's a RegisterOperand, or a direct RegisterClass reference), return the
166 /// Record for that RegisterClass.
167 ///
168 /// AssumeRegClassByHwModeIsDefault is a hack which should be removed. It only
169 /// happens to be adequate for the current GlobalISel usage.
170 const Record *
171 getInitValueAsRegClass(const Init *V,
172 bool AssumeRegClassByHwModeIsDefault = false) const;
173
174 /// If \p V is a DefInit that can be interpreted as a RegisterClassLike,
175 /// return the Record. This is used as a convenience function to handle direct
176 /// RegisterClass references, or those wrapped in a RegisterOperand.
177 const Record *getInitValueAsRegClassLike(const Init *V) const;
178 const Record *getAsRegClassLike(const Record *V) const;
179
180 CodeGenSchedModels &getSchedModels() const;
181
182 const CodeGenHwModes &getHwModes() const { return CGH; }
183
184 bool hasMacroFusion() const { return !MacroFusions.empty(); }
185
186 /// Return the macro fusions, sorted by name.
187 ArrayRef<const Record *> getMacroFusions() const { return MacroFusions; }
188
189 /// Return the name of a macro fusion, which is the name of the subtarget
190 /// feature that enables it.
191 static StringRef getMacroFusionName(const Record *Fusion);
192
193private:
194 DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &
195 getInstructionMap() const {
196 if (InstructionMap.empty())
197 ReadInstructions();
198 return InstructionMap;
199 }
200
201public:
202 CodeGenInstruction &getInstruction(const Record *InstRec) const {
203 auto I = getInstructionMap().find(Val: InstRec);
204 assert(I != InstructionMap.end() && "Not an instruction");
205 return *I->second;
206 }
207
208 /// Returns the number of predefined instructions.
209 static unsigned getNumFixedInstructions();
210
211 /// Return all of the instructions defined by the target, ordered by their
212 /// enum value.
213 /// The following order of instructions is also guaranteed:
214 /// - fixed / generic instructions as declared in TargetOpcodes.def, in order;
215 /// - pseudo instructions in lexicographical order sorted by name;
216 /// - other instructions in lexicographical order sorted by name.
217 ArrayRef<const CodeGenInstruction *> getInstructions() const {
218 if (InstrsByEnum.empty())
219 ComputeInstrsByEnum();
220 return InstrsByEnum;
221 }
222
223 // Functions that return various slices of `getInstructions`, ordered by
224 // their enum values.
225 ArrayRef<const CodeGenInstruction *> getGenericInstructions() const {
226 return getInstructions().take_front(N: getNumFixedInstructions());
227 }
228
229 ArrayRef<const CodeGenInstruction *> getTargetInstructions() const {
230 return getInstructions().drop_front(N: getNumFixedInstructions());
231 }
232
233 ArrayRef<const CodeGenInstruction *> getTargetPseudoInstructions() const {
234 return getTargetInstructions().take_front(N: NumPseudoInstructions);
235 }
236
237 ArrayRef<const CodeGenInstruction *> getTargetNonPseudoInstructions() const {
238 return getTargetInstructions().drop_front(N: NumPseudoInstructions);
239 }
240
241 /// Return the integer enum value corresponding to this instruction record.
242 unsigned getInstrIntValue(const Record *R) const {
243 if (InstrsByEnum.empty())
244 ComputeInstrsByEnum();
245 return getInstruction(InstRec: R).EnumVal;
246 }
247
248 /// Return whether instructions have variable length encodings on this target.
249 bool hasVariableLengthEncodings() const { return HasVariableLengthEncodings; }
250
251 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
252 ///
253 bool isLittleEndianEncoding() const;
254
255 /// Return the bits of the "Inst" field of \p EncodingDef, which must be an
256 /// InstructionEncoding with a fixed length encoding. For little-endian
257 /// instruction bit encodings the bits are returned in reverse order.
258 ArrayRef<const Init *>
259 getInstructionEncodingBits(const Record *EncodingDef) const;
260
261 /// guessInstructionProperties - should we just guess unset instruction
262 /// properties?
263 bool guessInstructionProperties() const;
264
265 const CodeGenIntrinsic &getIntrinsic(const Record *Def) const {
266 return Intrinsics[Def];
267 }
268
269private:
270 void ComputeInstrsByEnum() const;
271};
272
273/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
274/// tablegen class in TargetSelectionDAG.td
275class ComplexPattern {
276 const Record *Ty;
277 unsigned NumOperands;
278 std::string SelectFunc;
279 std::vector<const Record *> RootNodes;
280 unsigned Properties; // Node properties
281 unsigned Complexity;
282 bool WantsRoot;
283 bool WantsParent;
284
285public:
286 ComplexPattern(const Record *R);
287
288 const Record *getValueType() const { return Ty; }
289 unsigned getNumOperands() const { return NumOperands; }
290 const std::string &getSelectFunc() const { return SelectFunc; }
291 ArrayRef<const Record *> getRootNodes() const { return RootNodes; }
292 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
293 unsigned getComplexity() const { return Complexity; }
294 bool wantsRoot() const { return WantsRoot; }
295 bool wantsParent() const { return WantsParent; }
296};
297
298} // namespace llvm
299
300#endif // LLVM_UTILS_TABLEGEN_COMMON_CODEGENTARGET_H
301