1 //===- RISCVCompressInstEmitter.cpp - Generator for RISCV Compression -===//
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 // RISCVCompressInstEmitter implements a tablegen-driven CompressPat based
8 // RISCV Instruction Compression mechanism.
10 //===--------------------------------------------------------------===//
12 // RISCVCompressInstEmitter implements a tablegen-driven CompressPat Instruction
13 // Compression mechanism for generating RISCV compressed instructions
14 // (C ISA Extension) from the expanded instruction form.
16 // This tablegen backend processes CompressPat declarations in a
17 // td file and generates all the required checks to validate the pattern
18 // declarations; validate the input and output operands to generate the correct
19 // compressed instructions. The checks include validating different types of
20 // operands; register operands, immediate operands, fixed register and fixed
24 // class CompressPat<dag input, dag output> {
26 // dag Output = output;
27 // list<Predicate> Predicates = [];
30 // let Predicates = [HasStdExtC] in {
31 // def : CompressPat<(ADD GPRNoX0:$rs1, GPRNoX0:$rs1, GPRNoX0:$rs2),
32 // (C_ADD GPRNoX0:$rs1, GPRNoX0:$rs2)>;
35 // The result is an auto-generated header file
36 // 'RISCVGenCompressInstEmitter.inc' which exports two functions for
37 // compressing/uncompressing MCInst instructions, plus
38 // some helper functions:
40 // bool compressInst(MCInst& OutInst, const MCInst &MI,
41 // const MCSubtargetInfo &STI,
42 // MCContext &Context);
44 // bool uncompressInst(MCInst& OutInst, const MCInst &MI,
45 // const MCRegisterInfo &MRI,
46 // const MCSubtargetInfo &STI);
48 // The clients that include this auto-generated header file and
49 // invoke these functions can compress an instruction before emitting
50 // it in the target-specific ASM or ELF streamer or can uncompress
51 // an instruction before printing it when the expanded instruction
52 // format aliases is favored.
54 //===----------------------------------------------------------------------===//
56 #include "CodeGenInstruction.h"
57 #include "CodeGenTarget.h"
58 #include "llvm/ADT/IndexedMap.h"
59 #include "llvm/ADT/SmallVector.h"
60 #include "llvm/ADT/StringExtras.h"
61 #include "llvm/ADT/StringMap.h"
62 #include "llvm/Support/Debug.h"
63 #include "llvm/Support/ErrorHandling.h"
64 #include "llvm/TableGen/Error.h"
65 #include "llvm/TableGen/Record.h"
66 #include "llvm/TableGen/TableGenBackend.h"
71 #define DEBUG_TYPE "compress-inst-emitter"
74 class RISCVCompressInstEmitter
{
76 enum MapKind
{ Operand
, Imm
, Reg
};
79 unsigned Operand
; // Operand number mapped to.
80 uint64_t Imm
; // Integer immediate value.
81 Record
*Reg
; // Physical register.
83 int TiedOpIdx
= -1; // Tied operand index within the instruction.
86 CodeGenInstruction Source
; // The source instruction definition.
87 CodeGenInstruction Dest
; // The destination instruction to transform to.
89 PatReqFeatures
; // Required target features to enable pattern.
91 SourceOperandMap
; // Maps operands in the Source Instruction to
92 // the corresponding Dest instruction operand.
94 DestOperandMap
; // Maps operands in the Dest Instruction
95 // to the corresponding Source instruction operand.
96 CompressPat(CodeGenInstruction
&S
, CodeGenInstruction
&D
,
97 std::vector
<Record
*> RF
, IndexedMap
<OpData
> &SourceMap
,
98 IndexedMap
<OpData
> &DestMap
)
99 : Source(S
), Dest(D
), PatReqFeatures(RF
), SourceOperandMap(SourceMap
),
100 DestOperandMap(DestMap
) {}
103 RecordKeeper
&Records
;
104 CodeGenTarget Target
;
105 SmallVector
<CompressPat
, 4> CompressPatterns
;
107 void addDagOperandMapping(Record
*Rec
, DagInit
*Dag
, CodeGenInstruction
&Inst
,
108 IndexedMap
<OpData
> &OperandMap
, bool IsSourceInst
);
109 void evaluateCompressPat(Record
*Compress
);
110 void emitCompressInstEmitter(raw_ostream
&o
, bool Compress
);
111 bool validateTypes(Record
*SubType
, Record
*Type
, bool IsSourceInst
);
112 bool validateRegister(Record
*Reg
, Record
*RegClass
);
113 void createDagOperandMapping(Record
*Rec
, StringMap
<unsigned> &SourceOperands
,
114 StringMap
<unsigned> &DestOperands
,
115 DagInit
*SourceDag
, DagInit
*DestDag
,
116 IndexedMap
<OpData
> &SourceOperandMap
);
118 void createInstOperandMapping(Record
*Rec
, DagInit
*SourceDag
,
120 IndexedMap
<OpData
> &SourceOperandMap
,
121 IndexedMap
<OpData
> &DestOperandMap
,
122 StringMap
<unsigned> &SourceOperands
,
123 CodeGenInstruction
&DestInst
);
126 RISCVCompressInstEmitter(RecordKeeper
&R
) : Records(R
), Target(R
) {}
128 void run(raw_ostream
&o
);
130 } // End anonymous namespace.
132 bool RISCVCompressInstEmitter::validateRegister(Record
*Reg
, Record
*RegClass
) {
133 assert(Reg
->isSubClassOf("Register") && "Reg record should be a Register\n");
134 assert(RegClass
->isSubClassOf("RegisterClass") && "RegClass record should be"
135 " a RegisterClass\n");
136 CodeGenRegisterClass RC
= Target
.getRegisterClass(RegClass
);
137 const CodeGenRegister
*R
= Target
.getRegisterByName(Reg
->getName().lower());
138 assert((R
!= nullptr) &&
139 ("Register" + Reg
->getName().str() + " not defined!!\n").c_str());
140 return RC
.contains(R
);
143 bool RISCVCompressInstEmitter::validateTypes(Record
*DagOpType
,
146 if (DagOpType
== InstOpType
)
148 // Only source instruction operands are allowed to not match Input Dag
153 if (DagOpType
->isSubClassOf("RegisterClass") &&
154 InstOpType
->isSubClassOf("RegisterClass")) {
155 CodeGenRegisterClass RC
= Target
.getRegisterClass(InstOpType
);
156 CodeGenRegisterClass SubRC
= Target
.getRegisterClass(DagOpType
);
157 return RC
.hasSubClass(&SubRC
);
160 // At this point either or both types are not registers, reject the pattern.
161 if (DagOpType
->isSubClassOf("RegisterClass") ||
162 InstOpType
->isSubClassOf("RegisterClass"))
165 // Let further validation happen when compress()/uncompress() functions are
167 LLVM_DEBUG(dbgs() << (IsSourceInst
? "Input" : "Output")
168 << " Dag Operand Type: '" << DagOpType
->getName()
170 << "Instruction Operand Type: '" << InstOpType
->getName()
171 << "' can't be checked at pattern validation time!\n");
175 /// The patterns in the Dag contain different types of operands:
176 /// Register operands, e.g.: GPRC:$rs1; Fixed registers, e.g: X1; Immediate
177 /// operands, e.g.: simm6:$imm; Fixed immediate operands, e.g.: 0. This function
178 /// maps Dag operands to its corresponding instruction operands. For register
179 /// operands and fixed registers it expects the Dag operand type to be contained
180 /// in the instantiated instruction operand type. For immediate operands and
181 /// immediates no validation checks are enforced at pattern validation time.
182 void RISCVCompressInstEmitter::addDagOperandMapping(
183 Record
*Rec
, DagInit
*Dag
, CodeGenInstruction
&Inst
,
184 IndexedMap
<OpData
> &OperandMap
, bool IsSourceInst
) {
185 // TiedCount keeps track of the number of operands skipped in Inst
186 // operands list to get to the corresponding Dag operand. This is
187 // necessary because the number of operands in Inst might be greater
188 // than number of operands in the Dag due to how tied operands
190 unsigned TiedCount
= 0;
191 for (unsigned i
= 0, e
= Inst
.Operands
.size(); i
!= e
; ++i
) {
192 int TiedOpIdx
= Inst
.Operands
[i
].getTiedRegister();
193 if (-1 != TiedOpIdx
) {
194 // Set the entry in OperandMap for the tied operand we're skipping.
195 OperandMap
[i
].Kind
= OperandMap
[TiedOpIdx
].Kind
;
196 OperandMap
[i
].Data
= OperandMap
[TiedOpIdx
].Data
;
200 if (DefInit
*DI
= dyn_cast
<DefInit
>(Dag
->getArg(i
- TiedCount
))) {
201 if (DI
->getDef()->isSubClassOf("Register")) {
202 // Check if the fixed register belongs to the Register class.
203 if (!validateRegister(DI
->getDef(), Inst
.Operands
[i
].Rec
))
204 PrintFatalError(Rec
->getLoc(),
205 "Error in Dag '" + Dag
->getAsString() +
206 "'Register: '" + DI
->getDef()->getName() +
207 "' is not in register class '" +
208 Inst
.Operands
[i
].Rec
->getName() + "'");
209 OperandMap
[i
].Kind
= OpData::Reg
;
210 OperandMap
[i
].Data
.Reg
= DI
->getDef();
213 // Validate that Dag operand type matches the type defined in the
214 // corresponding instruction. Operands in the input Dag pattern are
215 // allowed to be a subclass of the type specified in corresponding
216 // instruction operand instead of being an exact match.
217 if (!validateTypes(DI
->getDef(), Inst
.Operands
[i
].Rec
, IsSourceInst
))
218 PrintFatalError(Rec
->getLoc(),
219 "Error in Dag '" + Dag
->getAsString() + "'. Operand '" +
220 Dag
->getArgNameStr(i
- TiedCount
) + "' has type '" +
221 DI
->getDef()->getName() +
222 "' which does not match the type '" +
223 Inst
.Operands
[i
].Rec
->getName() +
224 "' in the corresponding instruction operand!");
226 OperandMap
[i
].Kind
= OpData::Operand
;
227 } else if (IntInit
*II
= dyn_cast
<IntInit
>(Dag
->getArg(i
- TiedCount
))) {
228 // Validate that corresponding instruction operand expects an immediate.
229 if (Inst
.Operands
[i
].Rec
->isSubClassOf("RegisterClass"))
232 ("Error in Dag '" + Dag
->getAsString() + "' Found immediate: '" +
234 "' but corresponding instruction operand expected a register!"));
235 // No pattern validation check possible for values of fixed immediate.
236 OperandMap
[i
].Kind
= OpData::Imm
;
237 OperandMap
[i
].Data
.Imm
= II
->getValue();
239 dbgs() << " Found immediate '" << II
->getValue() << "' at "
240 << (IsSourceInst
? "input " : "output ")
241 << "Dag. No validation time check possible for values of "
242 "fixed immediate.\n");
244 llvm_unreachable("Unhandled CompressPat argument type!");
248 // Verify the Dag operand count is enough to build an instruction.
249 static bool verifyDagOpCount(CodeGenInstruction
&Inst
, DagInit
*Dag
,
251 if (Dag
->getNumArgs() == Inst
.Operands
.size())
253 // Source instructions are non compressed instructions and don't have tied
256 PrintFatalError(Inst
.TheDef
->getLoc(),
257 "Input operands for Inst '" + Inst
.TheDef
->getName() +
258 "' and input Dag operand count mismatch");
259 // The Dag can't have more arguments than the Instruction.
260 if (Dag
->getNumArgs() > Inst
.Operands
.size())
261 PrintFatalError(Inst
.TheDef
->getLoc(),
262 "Inst '" + Inst
.TheDef
->getName() +
263 "' and Dag operand count mismatch");
265 // The Instruction might have tied operands so the Dag might have
266 // a fewer operand count.
267 unsigned RealCount
= Inst
.Operands
.size();
268 for (unsigned i
= 0; i
< Inst
.Operands
.size(); i
++)
269 if (Inst
.Operands
[i
].getTiedRegister() != -1)
272 if (Dag
->getNumArgs() != RealCount
)
273 PrintFatalError(Inst
.TheDef
->getLoc(),
274 "Inst '" + Inst
.TheDef
->getName() +
275 "' and Dag operand count mismatch");
279 static bool validateArgsTypes(Init
*Arg1
, Init
*Arg2
) {
280 DefInit
*Type1
= dyn_cast
<DefInit
>(Arg1
);
281 DefInit
*Type2
= dyn_cast
<DefInit
>(Arg2
);
282 assert(Type1
&& ("Arg1 type not found\n"));
283 assert(Type2
&& ("Arg2 type not found\n"));
284 return Type1
->getDef() == Type2
->getDef();
287 // Creates a mapping between the operand name in the Dag (e.g. $rs1) and
288 // its index in the list of Dag operands and checks that operands with the same
289 // name have the same types. For example in 'C_ADD $rs1, $rs2' we generate the
290 // mapping $rs1 --> 0, $rs2 ---> 1. If the operand appears twice in the (tied)
291 // same Dag we use the last occurrence for indexing.
292 void RISCVCompressInstEmitter::createDagOperandMapping(
293 Record
*Rec
, StringMap
<unsigned> &SourceOperands
,
294 StringMap
<unsigned> &DestOperands
, DagInit
*SourceDag
, DagInit
*DestDag
,
295 IndexedMap
<OpData
> &SourceOperandMap
) {
296 for (unsigned i
= 0; i
< DestDag
->getNumArgs(); ++i
) {
297 // Skip fixed immediates and registers, they were handled in
298 // addDagOperandMapping.
299 if ("" == DestDag
->getArgNameStr(i
))
301 DestOperands
[DestDag
->getArgNameStr(i
)] = i
;
304 for (unsigned i
= 0; i
< SourceDag
->getNumArgs(); ++i
) {
305 // Skip fixed immediates and registers, they were handled in
306 // addDagOperandMapping.
307 if ("" == SourceDag
->getArgNameStr(i
))
310 StringMap
<unsigned>::iterator it
=
311 SourceOperands
.find(SourceDag
->getArgNameStr(i
));
312 if (it
!= SourceOperands
.end()) {
313 // Operand sharing the same name in the Dag should be mapped as tied.
314 SourceOperandMap
[i
].TiedOpIdx
= it
->getValue();
315 if (!validateArgsTypes(SourceDag
->getArg(it
->getValue()),
316 SourceDag
->getArg(i
)))
317 PrintFatalError(Rec
->getLoc(),
318 "Input Operand '" + SourceDag
->getArgNameStr(i
) +
319 "' has a mismatched tied operand!\n");
321 it
= DestOperands
.find(SourceDag
->getArgNameStr(i
));
322 if (it
== DestOperands
.end())
323 PrintFatalError(Rec
->getLoc(), "Operand " + SourceDag
->getArgNameStr(i
) +
324 " defined in Input Dag but not used in"
326 // Input Dag operand types must match output Dag operand type.
327 if (!validateArgsTypes(DestDag
->getArg(it
->getValue()),
328 SourceDag
->getArg(i
)))
329 PrintFatalError(Rec
->getLoc(), "Type mismatch between Input and "
330 "Output Dag operand '" +
331 SourceDag
->getArgNameStr(i
) + "'!");
332 SourceOperands
[SourceDag
->getArgNameStr(i
)] = i
;
336 /// Map operand names in the Dag to their index in both corresponding input and
337 /// output instructions. Validate that operands defined in the input are
338 /// used in the output pattern while populating the maps.
339 void RISCVCompressInstEmitter::createInstOperandMapping(
340 Record
*Rec
, DagInit
*SourceDag
, DagInit
*DestDag
,
341 IndexedMap
<OpData
> &SourceOperandMap
, IndexedMap
<OpData
> &DestOperandMap
,
342 StringMap
<unsigned> &SourceOperands
, CodeGenInstruction
&DestInst
) {
343 // TiedCount keeps track of the number of operands skipped in Inst
344 // operands list to get to the corresponding Dag operand.
345 unsigned TiedCount
= 0;
346 LLVM_DEBUG(dbgs() << " Operand mapping:\n Source Dest\n");
347 for (unsigned i
= 0, e
= DestInst
.Operands
.size(); i
!= e
; ++i
) {
348 int TiedInstOpIdx
= DestInst
.Operands
[i
].getTiedRegister();
349 if (TiedInstOpIdx
!= -1) {
351 DestOperandMap
[i
].Data
= DestOperandMap
[TiedInstOpIdx
].Data
;
352 DestOperandMap
[i
].Kind
= DestOperandMap
[TiedInstOpIdx
].Kind
;
353 if (DestOperandMap
[i
].Kind
== OpData::Operand
)
354 // No need to fill the SourceOperandMap here since it was mapped to
355 // destination operand 'TiedInstOpIdx' in a previous iteration.
356 LLVM_DEBUG(dbgs() << " " << DestOperandMap
[i
].Data
.Operand
358 << " Dest operand tied with operand '"
359 << TiedInstOpIdx
<< "'\n");
362 // Skip fixed immediates and registers, they were handled in
363 // addDagOperandMapping.
364 if (DestOperandMap
[i
].Kind
!= OpData::Operand
)
367 unsigned DagArgIdx
= i
- TiedCount
;
368 StringMap
<unsigned>::iterator SourceOp
=
369 SourceOperands
.find(DestDag
->getArgNameStr(DagArgIdx
));
370 if (SourceOp
== SourceOperands
.end())
371 PrintFatalError(Rec
->getLoc(),
372 "Output Dag operand '" +
373 DestDag
->getArgNameStr(DagArgIdx
) +
374 "' has no matching input Dag operand.");
376 assert(DestDag
->getArgNameStr(DagArgIdx
) ==
377 SourceDag
->getArgNameStr(SourceOp
->getValue()) &&
378 "Incorrect operand mapping detected!\n");
379 DestOperandMap
[i
].Data
.Operand
= SourceOp
->getValue();
380 SourceOperandMap
[SourceOp
->getValue()].Data
.Operand
= i
;
381 LLVM_DEBUG(dbgs() << " " << SourceOp
->getValue() << " ====> " << i
386 /// Validates the CompressPattern and create operand mapping.
387 /// These are the checks to validate a CompressPat pattern declarations.
388 /// Error out with message under these conditions:
389 /// - Dag Input opcode is an expanded instruction and Dag Output opcode is a
390 /// compressed instruction.
391 /// - Operands in Dag Input must be all used in Dag Output.
392 /// Register Operand type in Dag Input Type must be contained in the
393 /// corresponding Source Instruction type.
394 /// - Register Operand type in Dag Input must be the same as in Dag Ouput.
395 /// - Register Operand type in Dag Output must be the same as the
396 /// corresponding Destination Inst type.
397 /// - Immediate Operand type in Dag Input must be the same as in Dag Ouput.
398 /// - Immediate Operand type in Dag Ouput must be the same as the corresponding
399 /// Destination Instruction type.
400 /// - Fixed register must be contained in the corresponding Source Instruction
402 /// - Fixed register must be contained in the corresponding Destination
403 /// Instruction type. Warning message printed under these conditions:
404 /// - Fixed immediate in Dag Input or Dag Ouput cannot be checked at this time
405 /// and generate warning.
406 /// - Immediate operand type in Dag Input differs from the corresponding Source
407 /// Instruction type and generate a warning.
408 void RISCVCompressInstEmitter::evaluateCompressPat(Record
*Rec
) {
409 // Validate input Dag operands.
410 DagInit
*SourceDag
= Rec
->getValueAsDag("Input");
411 assert(SourceDag
&& "Missing 'Input' in compress pattern!");
412 LLVM_DEBUG(dbgs() << "Input: " << *SourceDag
<< "\n");
414 DefInit
*OpDef
= dyn_cast
<DefInit
>(SourceDag
->getOperator());
416 PrintFatalError(Rec
->getLoc(),
417 Rec
->getName() + " has unexpected operator type!");
418 // Checking we are transforming from compressed to uncompressed instructions.
419 Record
*Operator
= OpDef
->getDef();
420 if (!Operator
->isSubClassOf("RVInst"))
421 PrintFatalError(Rec
->getLoc(), "Input instruction '" + Operator
->getName() +
422 "' is not a 32 bit wide instruction!");
423 CodeGenInstruction
SourceInst(Operator
);
424 verifyDagOpCount(SourceInst
, SourceDag
, true);
426 // Validate output Dag operands.
427 DagInit
*DestDag
= Rec
->getValueAsDag("Output");
428 assert(DestDag
&& "Missing 'Output' in compress pattern!");
429 LLVM_DEBUG(dbgs() << "Output: " << *DestDag
<< "\n");
431 DefInit
*DestOpDef
= dyn_cast
<DefInit
>(DestDag
->getOperator());
433 PrintFatalError(Rec
->getLoc(),
434 Rec
->getName() + " has unexpected operator type!");
436 Record
*DestOperator
= DestOpDef
->getDef();
437 if (!DestOperator
->isSubClassOf("RVInst16"))
438 PrintFatalError(Rec
->getLoc(), "Output instruction '" +
439 DestOperator
->getName() +
440 "' is not a 16 bit wide instruction!");
441 CodeGenInstruction
DestInst(DestOperator
);
442 verifyDagOpCount(DestInst
, DestDag
, false);
444 // Fill the mapping from the source to destination instructions.
446 IndexedMap
<OpData
> SourceOperandMap
;
447 SourceOperandMap
.grow(SourceInst
.Operands
.size());
448 // Create a mapping between source Dag operands and source Inst operands.
449 addDagOperandMapping(Rec
, SourceDag
, SourceInst
, SourceOperandMap
,
450 /*IsSourceInst*/ true);
452 IndexedMap
<OpData
> DestOperandMap
;
453 DestOperandMap
.grow(DestInst
.Operands
.size());
454 // Create a mapping between destination Dag operands and destination Inst
456 addDagOperandMapping(Rec
, DestDag
, DestInst
, DestOperandMap
,
457 /*IsSourceInst*/ false);
459 StringMap
<unsigned> SourceOperands
;
460 StringMap
<unsigned> DestOperands
;
461 createDagOperandMapping(Rec
, SourceOperands
, DestOperands
, SourceDag
, DestDag
,
463 // Create operand mapping between the source and destination instructions.
464 createInstOperandMapping(Rec
, SourceDag
, DestDag
, SourceOperandMap
,
465 DestOperandMap
, SourceOperands
, DestInst
);
467 // Get the target features for the CompressPat.
468 std::vector
<Record
*> PatReqFeatures
;
469 std::vector
<Record
*> RF
= Rec
->getValueAsListOfDefs("Predicates");
470 copy_if(RF
, std::back_inserter(PatReqFeatures
), [](Record
*R
) {
471 return R
->getValueAsBit("AssemblerMatcherPredicate");
474 CompressPatterns
.push_back(CompressPat(SourceInst
, DestInst
, PatReqFeatures
,
475 SourceOperandMap
, DestOperandMap
));
478 static void getReqFeatures(std::set
<StringRef
> &FeaturesSet
,
479 const std::vector
<Record
*> &ReqFeatures
) {
480 for (auto &R
: ReqFeatures
) {
481 StringRef AsmCondString
= R
->getValueAsString("AssemblerCondString");
483 // AsmCondString has syntax [!]F(,[!]F)*
484 SmallVector
<StringRef
, 4> Ops
;
485 SplitString(AsmCondString
, Ops
, ",");
486 assert(!Ops
.empty() && "AssemblerCondString cannot be empty");
487 for (auto &Op
: Ops
) {
488 assert(!Op
.empty() && "Empty operator");
489 FeaturesSet
.insert(Op
);
494 unsigned getMCOpPredicate(DenseMap
<const Record
*, unsigned> &MCOpPredicateMap
,
495 std::vector
<const Record
*> &MCOpPredicates
,
497 unsigned Entry
= MCOpPredicateMap
[Rec
];
501 if (!Rec
->isValueUnset("MCOperandPredicate")) {
502 MCOpPredicates
.push_back(Rec
);
503 Entry
= MCOpPredicates
.size();
504 MCOpPredicateMap
[Rec
] = Entry
;
508 PrintFatalError(Rec
->getLoc(),
509 "No MCOperandPredicate on this operand at all: " +
510 Rec
->getName().str() + "'");
514 static std::string
mergeCondAndCode(raw_string_ostream
&CondStream
,
515 raw_string_ostream
&CodeStream
) {
517 raw_string_ostream
CombinedStream(S
);
518 CombinedStream
.indent(4)
520 << CondStream
.str().substr(
521 6, CondStream
.str().length() -
522 10) // remove first indentation and last '&&'.
524 CombinedStream
<< CodeStream
.str();
525 CombinedStream
.indent(4) << " return true;\n";
526 CombinedStream
.indent(4) << "} // if\n";
527 return CombinedStream
.str();
530 void RISCVCompressInstEmitter::emitCompressInstEmitter(raw_ostream
&o
,
532 Record
*AsmWriter
= Target
.getAsmWriter();
533 if (!AsmWriter
->getValueAsInt("PassSubtarget"))
534 PrintFatalError(AsmWriter
->getLoc(),
535 "'PassSubtarget' is false. SubTargetInfo object is needed "
536 "for target features.\n");
538 std::string Namespace
= Target
.getName();
540 // Sort entries in CompressPatterns to handle instructions that can have more
541 // than one candidate for compression\uncompression, e.g ADD can be
542 // transformed to a C_ADD or a C_MV. When emitting 'uncompress()' function the
543 // source and destination are flipped and the sort key needs to change
545 llvm::stable_sort(CompressPatterns
,
546 [Compress
](const CompressPat
&LHS
, const CompressPat
&RHS
) {
548 return (LHS
.Source
.TheDef
->getName().str() <
549 RHS
.Source
.TheDef
->getName().str());
551 return (LHS
.Dest
.TheDef
->getName().str() <
552 RHS
.Dest
.TheDef
->getName().str());
555 // A list of MCOperandPredicates for all operands in use, and the reverse map.
556 std::vector
<const Record
*> MCOpPredicates
;
557 DenseMap
<const Record
*, unsigned> MCOpPredicateMap
;
561 raw_string_ostream
Func(F
);
562 raw_string_ostream
FuncH(FH
);
563 bool NeedMRI
= false;
566 o
<< "\n#ifdef GEN_COMPRESS_INSTR\n"
567 << "#undef GEN_COMPRESS_INSTR\n\n";
569 o
<< "\n#ifdef GEN_UNCOMPRESS_INSTR\n"
570 << "#undef GEN_UNCOMPRESS_INSTR\n\n";
573 FuncH
<< "static bool compressInst(MCInst& OutInst,\n";
574 FuncH
.indent(25) << "const MCInst &MI,\n";
575 FuncH
.indent(25) << "const MCSubtargetInfo &STI,\n";
576 FuncH
.indent(25) << "MCContext &Context) {\n";
578 FuncH
<< "static bool uncompressInst(MCInst& OutInst,\n";
579 FuncH
.indent(27) << "const MCInst &MI,\n";
580 FuncH
.indent(27) << "const MCRegisterInfo &MRI,\n";
581 FuncH
.indent(27) << "const MCSubtargetInfo &STI) {\n";
584 if (CompressPatterns
.empty()) {
586 o
.indent(2) << "return false;\n}\n";
588 o
<< "\n#endif //GEN_COMPRESS_INSTR\n";
590 o
<< "\n#endif //GEN_UNCOMPRESS_INSTR\n\n";
594 std::string
CaseString("");
595 raw_string_ostream
CaseStream(CaseString
);
596 std::string
PrevOp("");
597 std::string
CurOp("");
598 CaseStream
<< " switch (MI.getOpcode()) {\n";
599 CaseStream
<< " default: return false;\n";
601 for (auto &CompressPat
: CompressPatterns
) {
602 std::string CondString
;
603 std::string CodeString
;
604 raw_string_ostream
CondStream(CondString
);
605 raw_string_ostream
CodeStream(CodeString
);
606 CodeGenInstruction
&Source
=
607 Compress
? CompressPat
.Source
: CompressPat
.Dest
;
608 CodeGenInstruction
&Dest
= Compress
? CompressPat
.Dest
: CompressPat
.Source
;
609 IndexedMap
<OpData
> SourceOperandMap
=
610 Compress
? CompressPat
.SourceOperandMap
: CompressPat
.DestOperandMap
;
611 IndexedMap
<OpData
> &DestOperandMap
=
612 Compress
? CompressPat
.DestOperandMap
: CompressPat
.SourceOperandMap
;
614 CurOp
= Source
.TheDef
->getName().str();
615 // Check current and previous opcode to decide to continue or end a case.
616 if (CurOp
!= PrevOp
) {
618 CaseStream
.indent(6) << "break;\n } // case " + PrevOp
+ "\n";
619 CaseStream
.indent(4) << "case " + Namespace
+ "::" + CurOp
+ ": {\n";
622 std::set
<StringRef
> FeaturesSet
;
623 // Add CompressPat required features.
624 getReqFeatures(FeaturesSet
, CompressPat
.PatReqFeatures
);
626 // Add Dest instruction required features.
627 std::vector
<Record
*> ReqFeatures
;
628 std::vector
<Record
*> RF
= Dest
.TheDef
->getValueAsListOfDefs("Predicates");
629 copy_if(RF
, std::back_inserter(ReqFeatures
), [](Record
*R
) {
630 return R
->getValueAsBit("AssemblerMatcherPredicate");
632 getReqFeatures(FeaturesSet
, ReqFeatures
);
634 // Emit checks for all required features.
635 for (auto &Op
: FeaturesSet
) {
637 CondStream
.indent(6) << ("!STI.getFeatureBits()[" + Namespace
+
638 "::" + Op
.substr(1) + "]")
643 << ("STI.getFeatureBits()[" + Namespace
+ "::" + Op
+ "]").str() +
647 // Start Source Inst operands validation.
649 for (OpNo
= 0; OpNo
< Source
.Operands
.size(); ++OpNo
) {
650 if (SourceOperandMap
[OpNo
].TiedOpIdx
!= -1) {
651 if (Source
.Operands
[OpNo
].Rec
->isSubClassOf("RegisterClass"))
654 << std::to_string(OpNo
) + ").getReg() == MI.getOperand("
655 << std::to_string(SourceOperandMap
[OpNo
].TiedOpIdx
)
656 << ").getReg()) &&\n";
658 PrintFatalError("Unexpected tied operand types!\n");
660 // Check for fixed immediates\registers in the source instruction.
661 switch (SourceOperandMap
[OpNo
].Kind
) {
662 case OpData::Operand
:
663 // We don't need to do anything for source instruction operand checks.
667 << "(MI.getOperand(" + std::to_string(OpNo
) + ").isImm()) &&\n" +
668 " (MI.getOperand(" + std::to_string(OpNo
) +
670 std::to_string(SourceOperandMap
[OpNo
].Data
.Imm
) + ") &&\n";
673 Record
*Reg
= SourceOperandMap
[OpNo
].Data
.Reg
;
674 CondStream
.indent(6) << "(MI.getOperand(" + std::to_string(OpNo
) +
675 ").getReg() == " + Namespace
+
676 "::" + Reg
->getName().str() + ") &&\n";
681 CodeStream
.indent(6) << "// " + Dest
.AsmString
+ "\n";
682 CodeStream
.indent(6) << "OutInst.setOpcode(" + Namespace
+
683 "::" + Dest
.TheDef
->getName().str() + ");\n";
685 for (const auto &DestOperand
: Dest
.Operands
) {
686 CodeStream
.indent(6) << "// Operand: " + DestOperand
.Name
+ "\n";
687 switch (DestOperandMap
[OpNo
].Kind
) {
688 case OpData::Operand
: {
689 unsigned OpIdx
= DestOperandMap
[OpNo
].Data
.Operand
;
690 // Check that the operand in the Source instruction fits
691 // the type for the Dest instruction.
692 if (DestOperand
.Rec
->isSubClassOf("RegisterClass")) {
694 // This is a register operand. Check the register class.
695 // Don't check register class if this is a tied operand, it was done
696 // for the operand its tied to.
697 if (DestOperand
.getTiedRegister() == -1)
699 << "(MRI.getRegClass(" + Namespace
+
700 "::" + DestOperand
.Rec
->getName().str() +
701 "RegClassID).contains(" + "MI.getOperand(" +
702 std::to_string(OpIdx
) + ").getReg())) &&\n";
704 CodeStream
.indent(6) << "OutInst.addOperand(MI.getOperand(" +
705 std::to_string(OpIdx
) + "));\n";
707 // Handling immediate operands.
708 unsigned Entry
= getMCOpPredicate(MCOpPredicateMap
, MCOpPredicates
,
710 CondStream
.indent(6) << Namespace
+ "ValidateMCOperand(" +
711 "MI.getOperand(" + std::to_string(OpIdx
) +
712 "), STI, " + std::to_string(Entry
) +
714 CodeStream
.indent(6) << "OutInst.addOperand(MI.getOperand(" +
715 std::to_string(OpIdx
) + "));\n";
721 getMCOpPredicate(MCOpPredicateMap
, MCOpPredicates
, DestOperand
.Rec
);
723 << Namespace
+ "ValidateMCOperand(" + "MCOperand::createImm(" +
724 std::to_string(DestOperandMap
[OpNo
].Data
.Imm
) + "), STI, " +
725 std::to_string(Entry
) + ") &&\n";
727 << "OutInst.addOperand(MCOperand::createImm(" +
728 std::to_string(DestOperandMap
[OpNo
].Data
.Imm
) + "));\n";
731 // Fixed register has been validated at pattern validation time.
732 Record
*Reg
= DestOperandMap
[OpNo
].Data
.Reg
;
733 CodeStream
.indent(6) << "OutInst.addOperand(MCOperand::createReg(" +
734 Namespace
+ "::" + Reg
->getName().str() +
740 CaseStream
<< mergeCondAndCode(CondStream
, CodeStream
);
743 Func
<< CaseStream
.str() << "\n";
744 // Close brace for the last case.
745 Func
.indent(4) << "} // case " + CurOp
+ "\n";
746 Func
.indent(2) << "} // switch\n";
747 Func
.indent(2) << "return false;\n}\n";
749 if (!MCOpPredicates
.empty()) {
750 o
<< "static bool " << Namespace
751 << "ValidateMCOperand(const MCOperand &MCOp,\n"
752 << " const MCSubtargetInfo &STI,\n"
753 << " unsigned PredicateIndex) {\n"
754 << " switch (PredicateIndex) {\n"
756 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
759 for (unsigned i
= 0; i
< MCOpPredicates
.size(); ++i
) {
760 Init
*MCOpPred
= MCOpPredicates
[i
]->getValueInit("MCOperandPredicate");
761 if (CodeInit
*SI
= dyn_cast
<CodeInit
>(MCOpPred
))
762 o
<< " case " << i
+ 1 << ": {\n"
763 << " // " << MCOpPredicates
[i
]->getName().str() << SI
->getValue()
767 llvm_unreachable("Unexpected MCOperandPredicate field!");
774 if (NeedMRI
&& Compress
)
775 o
.indent(2) << "const MCRegisterInfo &MRI = *Context.getRegisterInfo();\n";
779 o
<< "\n#endif //GEN_COMPRESS_INSTR\n";
781 o
<< "\n#endif //GEN_UNCOMPRESS_INSTR\n\n";
784 void RISCVCompressInstEmitter::run(raw_ostream
&o
) {
785 Record
*CompressClass
= Records
.getClass("CompressPat");
786 assert(CompressClass
&& "Compress class definition missing!");
787 std::vector
<Record
*> Insts
;
788 for (const auto &D
: Records
.getDefs()) {
789 if (D
.second
->isSubClassOf(CompressClass
))
790 Insts
.push_back(D
.second
.get());
793 // Process the CompressPat definitions, validating them as we do so.
794 for (unsigned i
= 0, e
= Insts
.size(); i
!= e
; ++i
)
795 evaluateCompressPat(Insts
[i
]);
798 emitSourceFileHeader("Compress instruction Source Fragment", o
);
799 // Generate compressInst() function.
800 emitCompressInstEmitter(o
, true);
801 // Generate uncompressInst() function.
802 emitCompressInstEmitter(o
, false);
807 void EmitCompressInst(RecordKeeper
&RK
, raw_ostream
&OS
) {
808 RISCVCompressInstEmitter(RK
).run(OS
);