1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This class wraps target description classes used by the various code
10 // generation TableGen backends. This makes it easier to access the data and
11 // provides a single place that needs to check it for validity. All of these
12 // classes abort on error conditions.
14 //===----------------------------------------------------------------------===//
16 #include "CodeGenTarget.h"
17 #include "CodeGenInstruction.h"
18 #include "CodeGenRegisters.h"
19 #include "CodeGenSchedule.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
31 cl::OptionCategory
AsmParserCat("Options for -gen-asm-parser");
32 cl::OptionCategory
AsmWriterCat("Options for -gen-asm-writer");
34 static cl::opt
<unsigned>
35 AsmParserNum("asmparsernum", cl::init(0),
36 cl::desc("Make -gen-asm-parser emit assembly parser #N"),
37 cl::cat(AsmParserCat
));
39 static cl::opt
<unsigned>
40 AsmWriterNum("asmwriternum", cl::init(0),
41 cl::desc("Make -gen-asm-writer emit assembly writer #N"),
42 cl::cat(AsmWriterCat
));
44 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
45 /// record corresponds to.
46 MVT::SimpleValueType
llvm::getValueType(const Record
*Rec
) {
47 return (MVT::SimpleValueType
)Rec
->getValueAsInt("Value");
50 StringRef
llvm::getName(MVT::SimpleValueType T
) {
52 case MVT::Other
: return "UNKNOWN";
53 case MVT::iPTR
: return "TLI.getPointerTy()";
54 case MVT::iPTRAny
: return "TLI.getPointerTy()";
55 default: return getEnumName(T
);
59 StringRef
llvm::getEnumName(MVT::SimpleValueType T
) {
62 case MVT::Other
: return "MVT::Other";
63 case MVT::i1
: return "MVT::i1";
64 case MVT::i2
: return "MVT::i2";
65 case MVT::i4
: return "MVT::i4";
66 case MVT::i8
: return "MVT::i8";
67 case MVT::i16
: return "MVT::i16";
68 case MVT::i32
: return "MVT::i32";
69 case MVT::i64
: return "MVT::i64";
70 case MVT::i128
: return "MVT::i128";
71 case MVT::Any
: return "MVT::Any";
72 case MVT::iAny
: return "MVT::iAny";
73 case MVT::fAny
: return "MVT::fAny";
74 case MVT::vAny
: return "MVT::vAny";
75 case MVT::f16
: return "MVT::f16";
76 case MVT::bf16
: return "MVT::bf16";
77 case MVT::f32
: return "MVT::f32";
78 case MVT::f64
: return "MVT::f64";
79 case MVT::f80
: return "MVT::f80";
80 case MVT::f128
: return "MVT::f128";
81 case MVT::ppcf128
: return "MVT::ppcf128";
82 case MVT::x86mmx
: return "MVT::x86mmx";
83 case MVT::x86amx
: return "MVT::x86amx";
84 case MVT::aarch64svcount
: return "MVT::aarch64svcount";
85 case MVT::i64x8
: return "MVT::i64x8";
86 case MVT::Glue
: return "MVT::Glue";
87 case MVT::isVoid
: return "MVT::isVoid";
88 case MVT::v1i1
: return "MVT::v1i1";
89 case MVT::v2i1
: return "MVT::v2i1";
90 case MVT::v4i1
: return "MVT::v4i1";
91 case MVT::v8i1
: return "MVT::v8i1";
92 case MVT::v16i1
: return "MVT::v16i1";
93 case MVT::v32i1
: return "MVT::v32i1";
94 case MVT::v64i1
: return "MVT::v64i1";
95 case MVT::v128i1
: return "MVT::v128i1";
96 case MVT::v256i1
: return "MVT::v256i1";
97 case MVT::v512i1
: return "MVT::v512i1";
98 case MVT::v1024i1
: return "MVT::v1024i1";
99 case MVT::v2048i1
: return "MVT::v2048i1";
100 case MVT::v128i2
: return "MVT::v128i2";
101 case MVT::v256i2
: return "MVT::v256i2";
102 case MVT::v64i4
: return "MVT::v64i4";
103 case MVT::v128i4
: return "MVT::v128i4";
104 case MVT::v1i8
: return "MVT::v1i8";
105 case MVT::v2i8
: return "MVT::v2i8";
106 case MVT::v4i8
: return "MVT::v4i8";
107 case MVT::v8i8
: return "MVT::v8i8";
108 case MVT::v16i8
: return "MVT::v16i8";
109 case MVT::v32i8
: return "MVT::v32i8";
110 case MVT::v64i8
: return "MVT::v64i8";
111 case MVT::v128i8
: return "MVT::v128i8";
112 case MVT::v256i8
: return "MVT::v256i8";
113 case MVT::v512i8
: return "MVT::v512i8";
114 case MVT::v1024i8
: return "MVT::v1024i8";
115 case MVT::v1i16
: return "MVT::v1i16";
116 case MVT::v2i16
: return "MVT::v2i16";
117 case MVT::v3i16
: return "MVT::v3i16";
118 case MVT::v4i16
: return "MVT::v4i16";
119 case MVT::v8i16
: return "MVT::v8i16";
120 case MVT::v16i16
: return "MVT::v16i16";
121 case MVT::v32i16
: return "MVT::v32i16";
122 case MVT::v64i16
: return "MVT::v64i16";
123 case MVT::v128i16
: return "MVT::v128i16";
124 case MVT::v256i16
: return "MVT::v256i16";
125 case MVT::v512i16
: return "MVT::v512i16";
126 case MVT::v1i32
: return "MVT::v1i32";
127 case MVT::v2i32
: return "MVT::v2i32";
128 case MVT::v3i32
: return "MVT::v3i32";
129 case MVT::v4i32
: return "MVT::v4i32";
130 case MVT::v5i32
: return "MVT::v5i32";
131 case MVT::v6i32
: return "MVT::v6i32";
132 case MVT::v7i32
: return "MVT::v7i32";
133 case MVT::v8i32
: return "MVT::v8i32";
134 case MVT::v9i32
: return "MVT::v9i32";
135 case MVT::v10i32
: return "MVT::v10i32";
136 case MVT::v11i32
: return "MVT::v11i32";
137 case MVT::v12i32
: return "MVT::v12i32";
138 case MVT::v16i32
: return "MVT::v16i32";
139 case MVT::v32i32
: return "MVT::v32i32";
140 case MVT::v64i32
: return "MVT::v64i32";
141 case MVT::v128i32
: return "MVT::v128i32";
142 case MVT::v256i32
: return "MVT::v256i32";
143 case MVT::v512i32
: return "MVT::v512i32";
144 case MVT::v1024i32
: return "MVT::v1024i32";
145 case MVT::v2048i32
: return "MVT::v2048i32";
146 case MVT::v1i64
: return "MVT::v1i64";
147 case MVT::v2i64
: return "MVT::v2i64";
148 case MVT::v3i64
: return "MVT::v3i64";
149 case MVT::v4i64
: return "MVT::v4i64";
150 case MVT::v8i64
: return "MVT::v8i64";
151 case MVT::v16i64
: return "MVT::v16i64";
152 case MVT::v32i64
: return "MVT::v32i64";
153 case MVT::v64i64
: return "MVT::v64i64";
154 case MVT::v128i64
: return "MVT::v128i64";
155 case MVT::v256i64
: return "MVT::v256i64";
156 case MVT::v1i128
: return "MVT::v1i128";
157 case MVT::v1f16
: return "MVT::v1f16";
158 case MVT::v2f16
: return "MVT::v2f16";
159 case MVT::v3f16
: return "MVT::v3f16";
160 case MVT::v4f16
: return "MVT::v4f16";
161 case MVT::v8f16
: return "MVT::v8f16";
162 case MVT::v16f16
: return "MVT::v16f16";
163 case MVT::v32f16
: return "MVT::v32f16";
164 case MVT::v64f16
: return "MVT::v64f16";
165 case MVT::v128f16
: return "MVT::v128f16";
166 case MVT::v256f16
: return "MVT::v256f16";
167 case MVT::v512f16
: return "MVT::v512f16";
168 case MVT::v2bf16
: return "MVT::v2bf16";
169 case MVT::v3bf16
: return "MVT::v3bf16";
170 case MVT::v4bf16
: return "MVT::v4bf16";
171 case MVT::v8bf16
: return "MVT::v8bf16";
172 case MVT::v16bf16
: return "MVT::v16bf16";
173 case MVT::v32bf16
: return "MVT::v32bf16";
174 case MVT::v64bf16
: return "MVT::v64bf16";
175 case MVT::v128bf16
: return "MVT::v128bf16";
176 case MVT::v1f32
: return "MVT::v1f32";
177 case MVT::v2f32
: return "MVT::v2f32";
178 case MVT::v3f32
: return "MVT::v3f32";
179 case MVT::v4f32
: return "MVT::v4f32";
180 case MVT::v5f32
: return "MVT::v5f32";
181 case MVT::v6f32
: return "MVT::v6f32";
182 case MVT::v7f32
: return "MVT::v7f32";
183 case MVT::v8f32
: return "MVT::v8f32";
184 case MVT::v9f32
: return "MVT::v9f32";
185 case MVT::v10f32
: return "MVT::v10f32";
186 case MVT::v11f32
: return "MVT::v11f32";
187 case MVT::v12f32
: return "MVT::v12f32";
188 case MVT::v16f32
: return "MVT::v16f32";
189 case MVT::v32f32
: return "MVT::v32f32";
190 case MVT::v64f32
: return "MVT::v64f32";
191 case MVT::v128f32
: return "MVT::v128f32";
192 case MVT::v256f32
: return "MVT::v256f32";
193 case MVT::v512f32
: return "MVT::v512f32";
194 case MVT::v1024f32
: return "MVT::v1024f32";
195 case MVT::v2048f32
: return "MVT::v2048f32";
196 case MVT::v1f64
: return "MVT::v1f64";
197 case MVT::v2f64
: return "MVT::v2f64";
198 case MVT::v3f64
: return "MVT::v3f64";
199 case MVT::v4f64
: return "MVT::v4f64";
200 case MVT::v8f64
: return "MVT::v8f64";
201 case MVT::v16f64
: return "MVT::v16f64";
202 case MVT::v32f64
: return "MVT::v32f64";
203 case MVT::v64f64
: return "MVT::v64f64";
204 case MVT::v128f64
: return "MVT::v128f64";
205 case MVT::v256f64
: return "MVT::v256f64";
206 case MVT::nxv1i1
: return "MVT::nxv1i1";
207 case MVT::nxv2i1
: return "MVT::nxv2i1";
208 case MVT::nxv4i1
: return "MVT::nxv4i1";
209 case MVT::nxv8i1
: return "MVT::nxv8i1";
210 case MVT::nxv16i1
: return "MVT::nxv16i1";
211 case MVT::nxv32i1
: return "MVT::nxv32i1";
212 case MVT::nxv64i1
: return "MVT::nxv64i1";
213 case MVT::nxv1i8
: return "MVT::nxv1i8";
214 case MVT::nxv2i8
: return "MVT::nxv2i8";
215 case MVT::nxv4i8
: return "MVT::nxv4i8";
216 case MVT::nxv8i8
: return "MVT::nxv8i8";
217 case MVT::nxv16i8
: return "MVT::nxv16i8";
218 case MVT::nxv32i8
: return "MVT::nxv32i8";
219 case MVT::nxv64i8
: return "MVT::nxv64i8";
220 case MVT::nxv1i16
: return "MVT::nxv1i16";
221 case MVT::nxv2i16
: return "MVT::nxv2i16";
222 case MVT::nxv4i16
: return "MVT::nxv4i16";
223 case MVT::nxv8i16
: return "MVT::nxv8i16";
224 case MVT::nxv16i16
: return "MVT::nxv16i16";
225 case MVT::nxv32i16
: return "MVT::nxv32i16";
226 case MVT::nxv1i32
: return "MVT::nxv1i32";
227 case MVT::nxv2i32
: return "MVT::nxv2i32";
228 case MVT::nxv4i32
: return "MVT::nxv4i32";
229 case MVT::nxv8i32
: return "MVT::nxv8i32";
230 case MVT::nxv16i32
: return "MVT::nxv16i32";
231 case MVT::nxv32i32
: return "MVT::nxv32i32";
232 case MVT::nxv1i64
: return "MVT::nxv1i64";
233 case MVT::nxv2i64
: return "MVT::nxv2i64";
234 case MVT::nxv4i64
: return "MVT::nxv4i64";
235 case MVT::nxv8i64
: return "MVT::nxv8i64";
236 case MVT::nxv16i64
: return "MVT::nxv16i64";
237 case MVT::nxv32i64
: return "MVT::nxv32i64";
238 case MVT::nxv1f16
: return "MVT::nxv1f16";
239 case MVT::nxv2f16
: return "MVT::nxv2f16";
240 case MVT::nxv4f16
: return "MVT::nxv4f16";
241 case MVT::nxv8f16
: return "MVT::nxv8f16";
242 case MVT::nxv16f16
: return "MVT::nxv16f16";
243 case MVT::nxv32f16
: return "MVT::nxv32f16";
244 case MVT::nxv1bf16
: return "MVT::nxv1bf16";
245 case MVT::nxv2bf16
: return "MVT::nxv2bf16";
246 case MVT::nxv4bf16
: return "MVT::nxv4bf16";
247 case MVT::nxv8bf16
: return "MVT::nxv8bf16";
248 case MVT::nxv16bf16
: return "MVT::nxv16bf16";
249 case MVT::nxv32bf16
: return "MVT::nxv32bf16";
250 case MVT::nxv1f32
: return "MVT::nxv1f32";
251 case MVT::nxv2f32
: return "MVT::nxv2f32";
252 case MVT::nxv4f32
: return "MVT::nxv4f32";
253 case MVT::nxv8f32
: return "MVT::nxv8f32";
254 case MVT::nxv16f32
: return "MVT::nxv16f32";
255 case MVT::nxv1f64
: return "MVT::nxv1f64";
256 case MVT::nxv2f64
: return "MVT::nxv2f64";
257 case MVT::nxv4f64
: return "MVT::nxv4f64";
258 case MVT::nxv8f64
: return "MVT::nxv8f64";
259 case MVT::token
: return "MVT::token";
260 case MVT::Metadata
: return "MVT::Metadata";
261 case MVT::iPTR
: return "MVT::iPTR";
262 case MVT::iPTRAny
: return "MVT::iPTRAny";
263 case MVT::Untyped
: return "MVT::Untyped";
264 case MVT::funcref
: return "MVT::funcref";
265 case MVT::externref
: return "MVT::externref";
266 default: llvm_unreachable("ILLEGAL VALUE TYPE!");
271 /// getQualifiedName - Return the name of the specified record, with a
272 /// namespace qualifier if the record contains one.
274 std::string
llvm::getQualifiedName(const Record
*R
) {
275 std::string Namespace
;
276 if (R
->getValue("Namespace"))
277 Namespace
= std::string(R
->getValueAsString("Namespace"));
278 if (Namespace
.empty())
279 return std::string(R
->getName());
280 return Namespace
+ "::" + R
->getName().str();
284 /// getTarget - Return the current instance of the Target class.
286 CodeGenTarget::CodeGenTarget(RecordKeeper
&records
)
287 : Records(records
), CGH(records
) {
288 std::vector
<Record
*> Targets
= Records
.getAllDerivedDefinitions("Target");
289 if (Targets
.size() == 0)
290 PrintFatalError("No 'Target' subclasses defined!");
291 if (Targets
.size() != 1)
292 PrintFatalError("Multiple subclasses of Target defined!");
293 TargetRec
= Targets
[0];
294 MacroFusions
= Records
.getAllDerivedDefinitions("Fusion");
297 CodeGenTarget::~CodeGenTarget() {
300 StringRef
CodeGenTarget::getName() const { return TargetRec
->getName(); }
302 /// getInstNamespace - Find and return the target machine's instruction
303 /// namespace. The namespace is cached because it is requested multiple times.
304 StringRef
CodeGenTarget::getInstNamespace() const {
305 if (InstNamespace
.empty()) {
306 for (const CodeGenInstruction
*Inst
: getInstructionsByEnumValue()) {
307 // We are not interested in the "TargetOpcode" namespace.
308 if (Inst
->Namespace
!= "TargetOpcode") {
309 InstNamespace
= Inst
->Namespace
;
315 return InstNamespace
;
318 StringRef
CodeGenTarget::getRegNamespace() const {
319 auto &RegClasses
= RegBank
->getRegClasses();
320 return RegClasses
.size() > 0 ? RegClasses
.front().Namespace
: "";
323 Record
*CodeGenTarget::getInstructionSet() const {
324 return TargetRec
->getValueAsDef("InstructionSet");
327 bool CodeGenTarget::getAllowRegisterRenaming() const {
328 return TargetRec
->getValueAsInt("AllowRegisterRenaming");
331 /// getAsmParser - Return the AssemblyParser definition for this target.
333 Record
*CodeGenTarget::getAsmParser() const {
334 std::vector
<Record
*> LI
= TargetRec
->getValueAsListOfDefs("AssemblyParsers");
335 if (AsmParserNum
>= LI
.size())
336 PrintFatalError("Target does not have an AsmParser #" +
337 Twine(AsmParserNum
) + "!");
338 return LI
[AsmParserNum
];
341 /// getAsmParserVariant - Return the AssemblyParserVariant definition for
344 Record
*CodeGenTarget::getAsmParserVariant(unsigned i
) const {
345 std::vector
<Record
*> LI
=
346 TargetRec
->getValueAsListOfDefs("AssemblyParserVariants");
348 PrintFatalError("Target does not have an AsmParserVariant #" + Twine(i
) +
353 /// getAsmParserVariantCount - Return the AssemblyParserVariant definition
354 /// available for this target.
356 unsigned CodeGenTarget::getAsmParserVariantCount() const {
357 std::vector
<Record
*> LI
=
358 TargetRec
->getValueAsListOfDefs("AssemblyParserVariants");
362 /// getAsmWriter - Return the AssemblyWriter definition for this target.
364 Record
*CodeGenTarget::getAsmWriter() const {
365 std::vector
<Record
*> LI
= TargetRec
->getValueAsListOfDefs("AssemblyWriters");
366 if (AsmWriterNum
>= LI
.size())
367 PrintFatalError("Target does not have an AsmWriter #" +
368 Twine(AsmWriterNum
) + "!");
369 return LI
[AsmWriterNum
];
372 CodeGenRegBank
&CodeGenTarget::getRegBank() const {
374 RegBank
= std::make_unique
<CodeGenRegBank
>(Records
, getHwModes());
378 std::optional
<CodeGenRegisterClass
*> CodeGenTarget::getSuperRegForSubReg(
379 const ValueTypeByHwMode
&ValueTy
, CodeGenRegBank
&RegBank
,
380 const CodeGenSubRegIndex
*SubIdx
, bool MustBeAllocatable
) const {
381 std::vector
<CodeGenRegisterClass
*> Candidates
;
382 auto &RegClasses
= RegBank
.getRegClasses();
384 // Try to find a register class which supports ValueTy, and also contains
386 for (CodeGenRegisterClass
&RC
: RegClasses
) {
387 // Is there a subclass of this class which contains this subregister index?
388 CodeGenRegisterClass
*SubClassWithSubReg
= RC
.getSubClassWithSubReg(SubIdx
);
389 if (!SubClassWithSubReg
)
392 // We have a class. Check if it supports this value type.
393 if (!llvm::is_contained(SubClassWithSubReg
->VTs
, ValueTy
))
396 // If necessary, check that it is allocatable.
397 if (MustBeAllocatable
&& !SubClassWithSubReg
->Allocatable
)
400 // We have a register class which supports both the value type and
401 // subregister index. Remember it.
402 Candidates
.push_back(SubClassWithSubReg
);
405 // If we didn't find anything, we're done.
406 if (Candidates
.empty())
409 // Find and return the largest of our candidate classes.
410 llvm::stable_sort(Candidates
, [&](const CodeGenRegisterClass
*A
,
411 const CodeGenRegisterClass
*B
) {
412 if (A
->getMembers().size() > B
->getMembers().size())
415 if (A
->getMembers().size() < B
->getMembers().size())
418 // Order by name as a tie-breaker.
419 return StringRef(A
->getName()) < B
->getName();
422 return Candidates
[0];
425 void CodeGenTarget::ReadRegAltNameIndices() const {
426 RegAltNameIndices
= Records
.getAllDerivedDefinitions("RegAltNameIndex");
427 llvm::sort(RegAltNameIndices
, LessRecord());
430 /// getRegisterByName - If there is a register with the specific AsmName,
432 const CodeGenRegister
*CodeGenTarget::getRegisterByName(StringRef Name
) const {
433 return getRegBank().getRegistersByName().lookup(Name
);
436 const CodeGenRegisterClass
&CodeGenTarget::getRegisterClass(Record
*R
) const {
437 return *getRegBank().getRegClass(R
);
440 std::vector
<ValueTypeByHwMode
> CodeGenTarget::getRegisterVTs(Record
*R
)
442 const CodeGenRegister
*Reg
= getRegBank().getReg(R
);
443 std::vector
<ValueTypeByHwMode
> Result
;
444 for (const auto &RC
: getRegBank().getRegClasses()) {
445 if (RC
.contains(Reg
)) {
446 ArrayRef
<ValueTypeByHwMode
> InVTs
= RC
.getValueTypes();
447 llvm::append_range(Result
, InVTs
);
451 // Remove duplicates.
453 Result
.erase(std::unique(Result
.begin(), Result
.end()), Result
.end());
458 void CodeGenTarget::ReadLegalValueTypes() const {
459 for (const auto &RC
: getRegBank().getRegClasses())
460 llvm::append_range(LegalValueTypes
, RC
.VTs
);
462 // Remove duplicates.
463 llvm::sort(LegalValueTypes
);
464 LegalValueTypes
.erase(std::unique(LegalValueTypes
.begin(),
465 LegalValueTypes
.end()),
466 LegalValueTypes
.end());
469 CodeGenSchedModels
&CodeGenTarget::getSchedModels() const {
471 SchedModels
= std::make_unique
<CodeGenSchedModels
>(Records
, *this);
475 void CodeGenTarget::ReadInstructions() const {
476 std::vector
<Record
*> Insts
= Records
.getAllDerivedDefinitions("Instruction");
477 if (Insts
.size() <= 2)
478 PrintFatalError("No 'Instruction' subclasses defined!");
480 // Parse the instructions defined in the .td file.
481 for (unsigned i
= 0, e
= Insts
.size(); i
!= e
; ++i
)
482 Instructions
[Insts
[i
]] = std::make_unique
<CodeGenInstruction
>(Insts
[i
]);
485 static const CodeGenInstruction
*
486 GetInstByName(const char *Name
,
487 const DenseMap
<const Record
*,
488 std::unique_ptr
<CodeGenInstruction
>> &Insts
,
489 RecordKeeper
&Records
) {
490 const Record
*Rec
= Records
.getDef(Name
);
492 const auto I
= Insts
.find(Rec
);
493 if (!Rec
|| I
== Insts
.end())
494 PrintFatalError(Twine("Could not find '") + Name
+ "' instruction!");
495 return I
->second
.get();
498 static const char *FixedInstrs
[] = {
499 #define HANDLE_TARGET_OPCODE(OPC) #OPC,
500 #include "llvm/Support/TargetOpcodes.def"
503 unsigned CodeGenTarget::getNumFixedInstructions() {
504 return std::size(FixedInstrs
) - 1;
507 /// Return all of the instructions defined by the target, ordered by
508 /// their enum value.
509 void CodeGenTarget::ComputeInstrsByEnum() const {
510 const auto &Insts
= getInstructions();
511 for (const char *const *p
= FixedInstrs
; *p
; ++p
) {
512 const CodeGenInstruction
*Instr
= GetInstByName(*p
, Insts
, Records
);
513 assert(Instr
&& "Missing target independent instruction");
514 assert(Instr
->Namespace
== "TargetOpcode" && "Bad namespace");
515 InstrsByEnum
.push_back(Instr
);
517 unsigned EndOfPredefines
= InstrsByEnum
.size();
518 assert(EndOfPredefines
== getNumFixedInstructions() &&
519 "Missing generic opcode");
521 for (const auto &I
: Insts
) {
522 const CodeGenInstruction
*CGI
= I
.second
.get();
523 if (CGI
->Namespace
!= "TargetOpcode") {
524 InstrsByEnum
.push_back(CGI
);
525 if (CGI
->TheDef
->getValueAsBit("isPseudo"))
526 ++NumPseudoInstructions
;
530 assert(InstrsByEnum
.size() == Insts
.size() && "Missing predefined instr");
532 // All of the instructions are now in random order based on the map iteration.
534 InstrsByEnum
.begin() + EndOfPredefines
, InstrsByEnum
.end(),
535 [](const CodeGenInstruction
*Rec1
, const CodeGenInstruction
*Rec2
) {
536 const auto &D1
= *Rec1
->TheDef
;
537 const auto &D2
= *Rec2
->TheDef
;
538 return std::make_tuple(!D1
.getValueAsBit("isPseudo"), D1
.getName()) <
539 std::make_tuple(!D2
.getValueAsBit("isPseudo"), D2
.getName());
544 /// isLittleEndianEncoding - Return whether this target encodes its instruction
545 /// in little-endian format, i.e. bits laid out in the order [0..n]
547 bool CodeGenTarget::isLittleEndianEncoding() const {
548 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
551 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
552 /// encodings, reverse the bit order of all instructions.
553 void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
554 if (!isLittleEndianEncoding())
557 std::vector
<Record
*> Insts
=
558 Records
.getAllDerivedDefinitions("InstructionEncoding");
559 for (Record
*R
: Insts
) {
560 if (R
->getValueAsString("Namespace") == "TargetOpcode" ||
561 R
->getValueAsBit("isPseudo"))
564 BitsInit
*BI
= R
->getValueAsBitsInit("Inst");
566 unsigned numBits
= BI
->getNumBits();
568 SmallVector
<Init
*, 16> NewBits(numBits
);
570 for (unsigned bit
= 0, end
= numBits
/ 2; bit
!= end
; ++bit
) {
571 unsigned bitSwapIdx
= numBits
- bit
- 1;
572 Init
*OrigBit
= BI
->getBit(bit
);
573 Init
*BitSwap
= BI
->getBit(bitSwapIdx
);
574 NewBits
[bit
] = BitSwap
;
575 NewBits
[bitSwapIdx
] = OrigBit
;
578 unsigned middle
= (numBits
+ 1) / 2;
579 NewBits
[middle
] = BI
->getBit(middle
);
582 BitsInit
*NewBI
= BitsInit::get(Records
, NewBits
);
584 // Update the bits in reversed order so that emitInstrOpBits will get the
585 // correct endianness.
586 R
->getValue("Inst")->setValue(NewBI
);
590 /// guessInstructionProperties - Return true if it's OK to guess instruction
591 /// properties instead of raising an error.
593 /// This is configurable as a temporary migration aid. It will eventually be
594 /// permanently false.
595 bool CodeGenTarget::guessInstructionProperties() const {
596 return getInstructionSet()->getValueAsBit("guessInstructionProperties");
599 //===----------------------------------------------------------------------===//
600 // ComplexPattern implementation
602 ComplexPattern::ComplexPattern(Record
*R
) {
603 Ty
= R
->getValueAsDef("Ty");
604 NumOperands
= R
->getValueAsInt("NumOperands");
605 SelectFunc
= std::string(R
->getValueAsString("SelectFunc"));
606 RootNodes
= R
->getValueAsListOfDefs("RootNodes");
608 // FIXME: This is a hack to statically increase the priority of patterns which
609 // maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. To get best
610 // possible pattern match we'll need to dynamically calculate the complexity
611 // of all patterns a dag can potentially map to.
612 int64_t RawComplexity
= R
->getValueAsInt("Complexity");
613 if (RawComplexity
== -1)
614 Complexity
= NumOperands
* 3;
616 Complexity
= RawComplexity
;
618 // FIXME: Why is this different from parseSDPatternOperatorProperties?
619 // Parse the properties.
621 std::vector
<Record
*> PropList
= R
->getValueAsListOfDefs("Properties");
622 for (unsigned i
= 0, e
= PropList
.size(); i
!= e
; ++i
)
623 if (PropList
[i
]->getName() == "SDNPHasChain") {
624 Properties
|= 1 << SDNPHasChain
;
625 } else if (PropList
[i
]->getName() == "SDNPOptInGlue") {
626 Properties
|= 1 << SDNPOptInGlue
;
627 } else if (PropList
[i
]->getName() == "SDNPMayStore") {
628 Properties
|= 1 << SDNPMayStore
;
629 } else if (PropList
[i
]->getName() == "SDNPMayLoad") {
630 Properties
|= 1 << SDNPMayLoad
;
631 } else if (PropList
[i
]->getName() == "SDNPSideEffect") {
632 Properties
|= 1 << SDNPSideEffect
;
633 } else if (PropList
[i
]->getName() == "SDNPMemOperand") {
634 Properties
|= 1 << SDNPMemOperand
;
635 } else if (PropList
[i
]->getName() == "SDNPVariadic") {
636 Properties
|= 1 << SDNPVariadic
;
637 } else if (PropList
[i
]->getName() == "SDNPWantRoot") {
638 Properties
|= 1 << SDNPWantRoot
;
639 } else if (PropList
[i
]->getName() == "SDNPWantParent") {
640 Properties
|= 1 << SDNPWantParent
;
642 PrintFatalError(R
->getLoc(), "Unsupported SD Node property '" +
643 PropList
[i
]->getName() +
644 "' on ComplexPattern '" + R
->getName() +