1//===-------- CompressInstEmitter.cpp - Generator for Compression ---------===//
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// CompressInstEmitter implements a tablegen-driven CompressPat based
8// Instruction Compression mechanism.
9//
10//===----------------------------------------------------------------------===//
11//
12// CompressInstEmitter implements a tablegen-driven CompressPat Instruction
13// Compression mechanism for generating compressed instructions from the
14// expanded instruction form.
15
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
21// immediate inputs.
22//
23// Example:
24// /// Defines a Pat match between compressed and uncompressed instruction.
25// /// The relationship and helper function generation are handled by
26// /// CompressInstEmitter backend.
27// class CompressPat<dag input, dag output, list<Predicate> predicates = []> {
28// /// Uncompressed instruction description.
29// dag Input = input;
30// /// Compressed instruction description.
31// dag Output = output;
32// /// Predicates that must be true for this to match.
33// list<Predicate> Predicates = predicates;
34// /// Duplicate match when tied operand is just different.
35// bit isCompressOnly = false;
36// }
37//
38// let Predicates = [HasStdExtZca] in {
39// def : CompressPat<(ADD GPRNoX0:$rs1, GPRNoX0:$rs1, GPRNoX0:$rs2),
40// (C_ADD GPRNoX0:$rs1, GPRNoX0:$rs2)>;
41// }
42//
43// The <TargetName>GenCompressInstEmitter.inc is an auto-generated header
44// file which exports two functions for compressing/uncompressing MCInst
45// instructions, plus some helper functions:
46//
47// bool compressInst(MCInst &OutInst, const MCInst &MI,
48// const MCSubtargetInfo &STI);
49//
50// bool uncompressInst(MCInst &OutInst, const MCInst &MI,
51// const MCSubtargetInfo &STI);
52//
53// In addition, it exports a function that returns the compressed instruction
54// size, or zero when the instruction is not compressible:
55//
56// unsigned getCompressedSize(const MachineInstr &MI,
57// const <TargetName>Subtarget &STI);
58//
59//
60// The clients that include this auto-generated header file and
61// invoke these functions can compress an instruction before emitting
62// it in the target-specific ASM or ELF streamer or can uncompress
63// an instruction before printing it when the expanded instruction
64// format aliases is favored.
65
66//===----------------------------------------------------------------------===//
67
68#include "Common/CodeGenHwModes.h"
69#include "Common/CodeGenInstruction.h"
70#include "Common/CodeGenRegisters.h"
71#include "Common/CodeGenTarget.h"
72#include "Common/InfoByHwMode.h"
73#include "Common/SubtargetFeatureInfo.h"
74#include "llvm/ADT/IndexedMap.h"
75#include "llvm/ADT/SmallVector.h"
76#include "llvm/ADT/StringMap.h"
77#include "llvm/Support/Debug.h"
78#include "llvm/Support/ErrorHandling.h"
79#include "llvm/TableGen/CodeGenHelpers.h"
80#include "llvm/TableGen/Error.h"
81#include "llvm/TableGen/Record.h"
82#include "llvm/TableGen/TableGenBackend.h"
83#include <set>
84#include <vector>
85using namespace llvm;
86
87#define DEBUG_TYPE "compress-inst-emitter"
88
89namespace {
90class CompressInstEmitter {
91 struct OpData {
92 enum MapKind { Operand, Imm, Reg } Kind;
93 // Info for an operand.
94 struct OpndInfo {
95 // Record from the Dag.
96 const Record *DagRec;
97 // Operand number mapped to.
98 unsigned Idx;
99 // Tied operand index within the instruction.
100 int TiedOpIdx;
101 };
102 union {
103 OpndInfo OpInfo;
104 // Integer immediate value.
105 int64_t ImmVal;
106 // Physical register.
107 const Record *RegRec;
108 };
109 };
110 struct ArgData {
111 unsigned DAGOpNo;
112 unsigned MIOpNo;
113 };
114 struct CompressPat {
115 // The source instruction definition.
116 CodeGenInstruction Source;
117 // The destination instruction to transform to.
118 CodeGenInstruction Dest;
119 // Required target features to enable pattern.
120 std::vector<const Record *> PatReqFeatures;
121 // Maps operands in the Source Instruction to
122 // the corresponding Dest instruction operand.
123 IndexedMap<OpData> SourceOperandMap;
124 // Maps operands in the Dest Instruction
125 // to the corresponding Source instruction operand.
126 IndexedMap<OpData> DestOperandMap;
127
128 bool IsCompressOnly;
129 CompressPat(const CodeGenInstruction &S, const CodeGenInstruction &D,
130 std::vector<const Record *> RF,
131 const IndexedMap<OpData> &SourceMap,
132 const IndexedMap<OpData> &DestMap, bool IsCompressOnly)
133 : Source(S), Dest(D), PatReqFeatures(std::move(RF)),
134 SourceOperandMap(SourceMap), DestOperandMap(DestMap),
135 IsCompressOnly(IsCompressOnly) {}
136 };
137 enum EmitterType { Compress, Uncompress, CheckCompress };
138 const RecordKeeper &Records;
139 const CodeGenTarget Target;
140 std::vector<CompressPat> CompressPatterns;
141 void addDagOperandMapping(const Record *Rec, const DagInit *Dag,
142 const CodeGenInstruction &Inst,
143 IndexedMap<OpData> &OperandMap,
144 StringMap<ArgData> &Operands, bool IsSourceInst,
145 ArrayRef<const Record *> PatPreds);
146 void evaluateCompressPat(const Record *Compress);
147 void emitCompressInstEmitter(raw_ostream &OS, EmitterType EType);
148 bool validateTypes(const Record *DagOpType, const Record *InstOpType,
149 bool IsSourceInst, ArrayRef<const Record *> PatPreds);
150 bool validateRegister(const Record *Reg, const Record *RegClass,
151 ArrayRef<SMLoc> Loc, ArrayRef<const Record *> PatPreds);
152 void checkDagOperandMapping(const Record *Rec,
153 const StringMap<ArgData> &DestOperands,
154 const DagInit *SourceDag, const DagInit *DestDag);
155
156 void createInstOperandMapping(const Record *Rec, const DagInit *SourceDag,
157 const DagInit *DestDag,
158 IndexedMap<OpData> &SourceOperandMap,
159 IndexedMap<OpData> &DestOperandMap,
160 StringMap<ArgData> &SourceOperands,
161 const CodeGenInstruction &DestInst);
162
163public:
164 CompressInstEmitter(const RecordKeeper &R) : Records(R), Target(R) {}
165
166 void run(raw_ostream &OS);
167};
168} // End anonymous namespace.
169
170bool CompressInstEmitter::validateRegister(const Record *Reg,
171 const Record *RegClass,
172 ArrayRef<SMLoc> Loc,
173 ArrayRef<const Record *> PatPreds) {
174 assert((Reg->isSubClassOf("Register") ||
175 Reg->isSubClassOf("RegisterByHwMode")) &&
176 "Reg record should be a Register");
177 assert(RegClass->isSubClassOf("RegisterClassLike") &&
178 "RegClass record should be RegisterClassLike");
179
180 if (const Record *RC =
181 Target.getHwModes().resolveModeSelect(SelectRec: RegClass, PatPreds))
182 RegClass = RC;
183 if (const Record *R = Target.getHwModes().resolveModeSelect(SelectRec: Reg, PatPreds))
184 Reg = R;
185
186 return Target.getRegBank().regClassContainsReg(RegClass, RegDef: Reg, Loc);
187}
188
189bool CompressInstEmitter::validateTypes(const Record *DagOpType,
190 const Record *InstOpType,
191 bool IsSourceInst,
192 ArrayRef<const Record *> PatPreds) {
193 if (DagOpType == InstOpType)
194 return true;
195
196 const Record *ResolvedDag =
197 Target.getHwModes().resolveModeSelect(SelectRec: DagOpType, PatPreds);
198 const Record *ResolvedInst =
199 Target.getHwModes().resolveModeSelect(SelectRec: InstOpType, PatPreds);
200
201 if (ResolvedDag && ResolvedInst) {
202 DagOpType = ResolvedDag;
203 InstOpType = ResolvedInst;
204 } else if (DagOpType->isSubClassOf(Name: "RegClassByHwMode") &&
205 InstOpType->isSubClassOf(Name: "RegClassByHwMode")) {
206 RegClassByHwMode DagRC(DagOpType, Target.getRegBank());
207 RegClassByHwMode InstRC(InstOpType, Target.getRegBank());
208 const CodeGenHwModes &CGH = Target.getHwModes();
209 HwModePredicates PatPredsSet(PatPreds);
210 bool HasCompatibleMode = false;
211 for (const auto &[Mode, DagClass] : DagRC) {
212 if (CGH.getModePredicates(ModeId: Mode).conflictsWith(Other: PatPredsSet))
213 continue;
214 if (!InstRC.hasMode(M: Mode))
215 return false;
216 const CodeGenRegisterClass *InstClass = InstRC.get(Mode);
217 if (!InstClass->hasSubClass(RC: DagClass))
218 return false;
219 HasCompatibleMode = true;
220 }
221 for (const auto &[Mode, InstClass] : InstRC) {
222 if (CGH.getModePredicates(ModeId: Mode).conflictsWith(Other: PatPredsSet))
223 continue;
224 if (!DagRC.hasMode(M: Mode))
225 return false;
226 }
227 return HasCompatibleMode;
228 } else if (DagOpType->isSubClassOf(Name: "HwModeSelect") ||
229 InstOpType->isSubClassOf(Name: "HwModeSelect")) {
230 return false;
231 }
232
233 // Concrete check (when resolved uniquely).
234 if (DagOpType == InstOpType)
235 return true;
236
237 if (DagOpType->isSubClassOf(Name: "RegisterClass") &&
238 InstOpType->isSubClassOf(Name: "RegisterClass")) {
239 const CodeGenRegisterClass &RC = Target.getRegisterClass(R: InstOpType);
240 const CodeGenRegisterClass &SubRC = Target.getRegisterClass(R: DagOpType);
241 return RC.hasSubClass(RC: &SubRC);
242 }
243
244 // At this point either or both types are not registers, reject the pattern.
245 if (DagOpType->isSubClassOf(Name: "RegisterClass") ||
246 InstOpType->isSubClassOf(Name: "RegisterClass"))
247 return false;
248
249 // Let further validation happen when compress()/uncompress() functions are
250 // invoked.
251 LLVM_DEBUG(dbgs() << (IsSourceInst ? "Input" : "Output")
252 << " Dag Operand Type: '" << DagOpType->getName()
253 << "' and "
254 << "Instruction Operand Type: '" << InstOpType->getName()
255 << "' can't be checked at pattern validation time!\n");
256 return true;
257}
258
259static bool validateArgsTypes(const Init *Arg1, const Init *Arg2) {
260 return cast<DefInit>(Val: Arg1)->getDef() == cast<DefInit>(Val: Arg2)->getDef();
261}
262
263/// The patterns in the Dag contain different types of operands:
264/// Register operands, e.g.: GPRC:$rs1; Fixed registers, e.g: X1; Immediate
265/// operands, e.g.: simm6:$imm; Fixed immediate operands, e.g.: 0. This function
266/// maps Dag operands to its corresponding instruction operands. For register
267/// operands and fixed registers it expects the Dag operand type to be contained
268/// in the instantiated instruction operand type. For immediate operands and
269/// immediates no validation checks are enforced at pattern validation time.
270void CompressInstEmitter::addDagOperandMapping(
271 const Record *Rec, const DagInit *Dag, const CodeGenInstruction &Inst,
272 IndexedMap<OpData> &OperandMap, StringMap<ArgData> &Operands,
273 bool IsSourceInst, ArrayRef<const Record *> PatPreds) {
274 unsigned NumMIOperands = 0;
275 if (!Inst.Operands.empty())
276 NumMIOperands =
277 Inst.Operands.back().MIOperandNo + Inst.Operands.back().MINumOperands;
278 OperandMap.grow(N: NumMIOperands);
279
280 // Tied operands are not represented in the DAG so we count them separately.
281 unsigned DAGOpNo = 0;
282 unsigned OpNo = 0;
283 for (const auto &Opnd : Inst.Operands) {
284 int TiedOpIdx = Opnd.getTiedRegister();
285 if (-1 != TiedOpIdx) {
286 assert((unsigned)TiedOpIdx < OpNo);
287 // Set the entry in OperandMap for the tied operand we're skipping.
288 OperandMap[OpNo] = OperandMap[TiedOpIdx];
289 ++OpNo;
290
291 // Source instructions can have at most 1 tied operand.
292 if (IsSourceInst && (OpNo - DAGOpNo > 1))
293 PrintFatalError(ErrorLoc: Rec->getLoc(),
294 Msg: "Input operands for Inst '" + Inst.getName() +
295 "' and input Dag operand count mismatch");
296
297 continue;
298 }
299 for (unsigned SubOp = 0; SubOp != Opnd.MINumOperands;
300 ++SubOp, ++OpNo, ++DAGOpNo) {
301 const Record *OpndRec = Opnd.Rec;
302 if (Opnd.MINumOperands > 1)
303 OpndRec = cast<DefInit>(Val: Opnd.MIOperandInfo->getArg(Num: SubOp))->getDef();
304
305 if (DAGOpNo >= Dag->getNumArgs())
306 PrintFatalError(ErrorLoc: Rec->getLoc(), Msg: "Inst '" + Inst.getName() +
307 "' and Dag operand count mismatch");
308
309 if (const auto *DI = dyn_cast<DefInit>(Val: Dag->getArg(Num: DAGOpNo))) {
310 if (DI->getDef()->isSubClassOf(Name: "Register") ||
311 DI->getDef()->isSubClassOf(Name: "RegisterByHwMode")) {
312 // Check if the fixed register belongs to the Register class.
313 if (!validateRegister(Reg: DI->getDef(), RegClass: OpndRec, Loc: Rec->getLoc(), PatPreds))
314 PrintFatalError(ErrorLoc: Rec->getLoc(),
315 Msg: "Error in Dag '" + Dag->getAsString() +
316 "': Register '" + DI->getDef()->getName() +
317 "' is not in register class '" +
318 OpndRec->getName() + "'");
319 OperandMap[OpNo].Kind = OpData::Reg;
320 OperandMap[OpNo].RegRec = DI->getDef();
321 continue;
322 }
323 // Validate that Dag operand type matches the type defined in the
324 // corresponding instruction. Operands in the input and output Dag
325 // patterns are allowed to be a subclass of the type specified in the
326 // corresponding instruction operand instead of being an exact match.
327 if (!validateTypes(DagOpType: DI->getDef(), InstOpType: OpndRec, IsSourceInst, PatPreds))
328 PrintFatalError(ErrorLoc: Rec->getLoc(),
329 Msg: "Error in Dag '" + Dag->getAsString() +
330 "'. Operand '" + Dag->getArgNameStr(Num: DAGOpNo) +
331 "' has type '" + DI->getDef()->getName() +
332 "' which does not match the type '" +
333 OpndRec->getName() +
334 "' in the corresponding instruction operand!");
335
336 OperandMap[OpNo].Kind = OpData::Operand;
337 OperandMap[OpNo].OpInfo.DagRec = DI->getDef();
338 OperandMap[OpNo].OpInfo.TiedOpIdx = -1;
339
340 // Create a mapping between the operand name in the Dag (e.g. $rs1) and
341 // its index in the list of Dag operands and check that operands with
342 // the same name have the same type. For example in 'C_ADD $rs1, $rs2'
343 // we generate the mapping $rs1 --> 0, $rs2 ---> 1. If the operand
344 // appears twice in the same Dag (tied in the compressed instruction),
345 // we note the previous index in the TiedOpIdx field.
346 StringRef ArgName = Dag->getArgNameStr(Num: DAGOpNo);
347 if (ArgName.empty())
348 continue;
349
350 if (IsSourceInst) {
351 auto It = Operands.find(Key: ArgName);
352 if (It != Operands.end()) {
353 OperandMap[OpNo].OpInfo.TiedOpIdx = It->getValue().MIOpNo;
354 if (OperandMap[It->getValue().MIOpNo].OpInfo.DagRec != DI->getDef())
355 PrintFatalError(ErrorLoc: Rec->getLoc(),
356 Msg: "Input Operand '" + ArgName +
357 "' has a mismatched tied operand!");
358 }
359 }
360
361 Operands[ArgName] = {.DAGOpNo: DAGOpNo, .MIOpNo: OpNo};
362 } else if (const auto *II = dyn_cast<IntInit>(Val: Dag->getArg(Num: DAGOpNo))) {
363 // Validate that corresponding instruction operand expects an immediate.
364 if (!OpndRec->isSubClassOf(Name: "Operand"))
365 PrintFatalError(ErrorLoc: Rec->getLoc(), Msg: "Error in Dag '" + Dag->getAsString() +
366 "' Found immediate: '" +
367 II->getAsString() +
368 "' but corresponding instruction "
369 "operand expected a register!");
370 // No pattern validation check possible for values of fixed immediate.
371 OperandMap[OpNo].Kind = OpData::Imm;
372 OperandMap[OpNo].ImmVal = II->getValue();
373 LLVM_DEBUG(
374 dbgs() << " Found immediate '" << II->getValue() << "' at "
375 << (IsSourceInst ? "input " : "output ")
376 << "Dag. No validation time check possible for values of "
377 "fixed immediate.\n");
378 } else {
379 llvm_unreachable("Unhandled CompressPat argument type!");
380 }
381 }
382 }
383
384 // We shouldn't have extra Dag operands.
385 if (DAGOpNo != Dag->getNumArgs())
386 PrintFatalError(ErrorLoc: Rec->getLoc(), Msg: "Inst '" + Inst.getName() +
387 "' and Dag operand count mismatch");
388}
389
390// Check that all names in the source DAG appear in the destionation DAG.
391void CompressInstEmitter::checkDagOperandMapping(
392 const Record *Rec, const StringMap<ArgData> &DestOperands,
393 const DagInit *SourceDag, const DagInit *DestDag) {
394
395 for (unsigned I = 0; I < SourceDag->getNumArgs(); ++I) {
396 // Skip fixed immediates and registers, they were handled in
397 // addDagOperandMapping.
398 StringRef ArgName = SourceDag->getArgNameStr(Num: I);
399 if (ArgName.empty())
400 continue;
401
402 auto It = DestOperands.find(Key: ArgName);
403 if (It == DestOperands.end())
404 PrintFatalError(ErrorLoc: Rec->getLoc(), Msg: "Operand " + ArgName +
405 " defined in Input Dag but not used in"
406 " Output Dag!");
407 // Input Dag operand types must match output Dag operand type.
408 if (!validateArgsTypes(Arg1: DestDag->getArg(Num: It->getValue().DAGOpNo),
409 Arg2: SourceDag->getArg(Num: I)))
410 PrintFatalError(ErrorLoc: Rec->getLoc(), Msg: "Type mismatch between Input and "
411 "Output Dag operand '" +
412 ArgName + "'!");
413 }
414}
415
416/// Map operand names in the Dag to their index in both corresponding input and
417/// output instructions. Validate that operands defined in the input are
418/// used in the output pattern while populating the maps.
419void CompressInstEmitter::createInstOperandMapping(
420 const Record *Rec, const DagInit *SourceDag, const DagInit *DestDag,
421 IndexedMap<OpData> &SourceOperandMap, IndexedMap<OpData> &DestOperandMap,
422 StringMap<ArgData> &SourceOperands, const CodeGenInstruction &DestInst) {
423 // TiedCount keeps track of the number of operands skipped in Inst
424 // operands list to get to the corresponding Dag operand.
425 unsigned TiedCount = 0;
426 LLVM_DEBUG(dbgs() << " Operand mapping:\n Source Dest\n");
427 unsigned OpNo = 0;
428 for (const auto &Operand : DestInst.Operands) {
429 int TiedInstOpIdx = Operand.getTiedRegister();
430 if (TiedInstOpIdx != -1) {
431 ++TiedCount;
432 assert((unsigned)TiedInstOpIdx < OpNo);
433 DestOperandMap[OpNo] = DestOperandMap[TiedInstOpIdx];
434 if (DestOperandMap[OpNo].Kind == OpData::Operand)
435 // No need to fill the SourceOperandMap here since it was mapped to
436 // destination operand 'TiedInstOpIdx' in a previous iteration.
437 LLVM_DEBUG(dbgs() << " " << DestOperandMap[OpNo].OpInfo.Idx
438 << " ====> " << OpNo
439 << " Dest operand tied with operand '"
440 << TiedInstOpIdx << "'\n");
441 ++OpNo;
442 continue;
443 }
444
445 for (unsigned SubOp = 0; SubOp != Operand.MINumOperands; ++SubOp, ++OpNo) {
446 // Skip fixed immediates and registers, they were handled in
447 // addDagOperandMapping.
448 if (DestOperandMap[OpNo].Kind != OpData::Operand)
449 continue;
450
451 unsigned DagArgIdx = OpNo - TiedCount;
452 StringRef ArgName = DestDag->getArgNameStr(Num: DagArgIdx);
453 auto SourceOp = SourceOperands.find(Key: ArgName);
454 if (SourceOp == SourceOperands.end())
455 PrintFatalError(ErrorLoc: Rec->getLoc(),
456 Msg: "Output Dag operand '" + ArgName +
457 "' has no matching input Dag operand.");
458
459 assert(ArgName ==
460 SourceDag->getArgNameStr(SourceOp->getValue().DAGOpNo) &&
461 "Incorrect operand mapping detected!\n");
462
463 unsigned SourceOpNo = SourceOp->getValue().MIOpNo;
464 DestOperandMap[OpNo].OpInfo.Idx = SourceOpNo;
465 SourceOperandMap[SourceOpNo].OpInfo.Idx = OpNo;
466 LLVM_DEBUG(dbgs() << " " << SourceOpNo << " ====> " << OpNo << "\n");
467 }
468 }
469}
470
471/// Validates the CompressPattern and create operand mapping.
472/// These are the checks to validate a CompressPat pattern declarations.
473/// Error out with message under these conditions:
474/// - Dag Input opcode is an expanded instruction and Dag Output opcode is a
475/// compressed instruction.
476/// - Operands in Dag Input must be all used in Dag Output.
477/// Register Operand type in Dag Input Type must be contained in the
478/// corresponding Source Instruction type.
479/// - Register Operand type in Dag Input must be the same as in Dag Ouput.
480/// - Register Operand type in Dag Output must be the same as the
481/// corresponding Destination Inst type.
482/// - Immediate Operand type in Dag Input must be the same as in Dag Ouput.
483/// - Immediate Operand type in Dag Ouput must be the same as the corresponding
484/// Destination Instruction type.
485/// - Fixed register must be contained in the corresponding Source Instruction
486/// type.
487/// - Fixed register must be contained in the corresponding Destination
488/// Instruction type.
489/// Warning message printed under these conditions:
490/// - Fixed immediate in Dag Input or Dag Ouput cannot be checked at this time
491/// and generate warning.
492/// - Immediate operand type in Dag Input differs from the corresponding Source
493/// Instruction type and generate a warning.
494void CompressInstEmitter::evaluateCompressPat(const Record *Rec) {
495 // Validate input Dag operands.
496 const DagInit *SourceDag = Rec->getValueAsDag(FieldName: "Input");
497 assert(SourceDag && "Missing 'Input' in compress pattern!");
498 LLVM_DEBUG(dbgs() << "Input: " << *SourceDag << "\n");
499
500 // Checking we are transforming from compressed to uncompressed instructions.
501 const Record *SourceOperator = SourceDag->getOperatorAsDef(Loc: Rec->getLoc());
502 CodeGenInstruction SourceInst(SourceOperator);
503
504 // Validate output Dag operands.
505 const DagInit *DestDag = Rec->getValueAsDag(FieldName: "Output");
506 assert(DestDag && "Missing 'Output' in compress pattern!");
507 LLVM_DEBUG(dbgs() << "Output: " << *DestDag << "\n");
508
509 const Record *DestOperator = DestDag->getOperatorAsDef(Loc: Rec->getLoc());
510 CodeGenInstruction DestInst(DestOperator);
511
512 if (SourceOperator->getValueAsInt(FieldName: "Size") <=
513 DestOperator->getValueAsInt(FieldName: "Size"))
514 PrintFatalError(
515 ErrorLoc: Rec->getLoc(),
516 Msg: "Compressed instruction '" + DestOperator->getName() +
517 "'is not strictly smaller than the uncompressed instruction '" +
518 SourceOperator->getName() + "' !");
519
520 // Get the target features for the CompressPat.
521 std::vector<const Record *> PatReqFeatures;
522 std::vector<const Record *> RF = Rec->getValueAsListOfDefs(FieldName: "Predicates");
523 copy_if(Range&: RF, Out: std::back_inserter(x&: PatReqFeatures), P: [](const Record *R) {
524 return R->getValueAsBit(FieldName: "AssemblerMatcherPredicate");
525 });
526
527 // Fill the mapping from the source to destination instructions.
528
529 IndexedMap<OpData> SourceOperandMap;
530 // Map from arg name to DAG operand number and MI operand number.
531 StringMap<ArgData> SourceOperands;
532 // Create a mapping between source Dag operands and source Inst operands.
533 addDagOperandMapping(Rec, Dag: SourceDag, Inst: SourceInst, OperandMap&: SourceOperandMap,
534 Operands&: SourceOperands, /*IsSourceInst*/ true, PatPreds: PatReqFeatures);
535
536 IndexedMap<OpData> DestOperandMap;
537 // Map from arg name to DAG operand number and MI operand number.
538 StringMap<ArgData> DestOperands;
539 // Create a mapping between destination Dag operands and destination Inst
540 // operands.
541 addDagOperandMapping(Rec, Dag: DestDag, Inst: DestInst, OperandMap&: DestOperandMap, Operands&: DestOperands,
542 /*IsSourceInst*/ false, PatPreds: PatReqFeatures);
543
544 checkDagOperandMapping(Rec, DestOperands, SourceDag, DestDag);
545 // Create operand mapping between the source and destination instructions.
546 createInstOperandMapping(Rec, SourceDag, DestDag, SourceOperandMap,
547 DestOperandMap, SourceOperands, DestInst);
548
549 CompressPatterns.emplace_back(args&: SourceInst, args&: DestInst, args: std::move(PatReqFeatures),
550 args&: SourceOperandMap, args&: DestOperandMap,
551 args: Rec->getValueAsBit(FieldName: "isCompressOnly"));
552}
553
554static unsigned getPredicates(DenseMap<const Record *, unsigned> &PredicateMap,
555 std::vector<const Record *> &Predicates,
556 const Record *Rec, StringRef Name) {
557 unsigned &Entry = PredicateMap[Rec];
558 if (Entry)
559 return Entry;
560
561 if (!Rec->isValueUnset(FieldName: Name)) {
562 Predicates.push_back(x: Rec);
563 Entry = Predicates.size();
564 return Entry;
565 }
566
567 PrintFatalError(ErrorLoc: Rec->getLoc(), Msg: "No " + Name +
568 " predicate on this operand at all: '" +
569 Rec->getName() + "'");
570 return 0;
571}
572
573static void printPredicates(ArrayRef<const Record *> Predicates, StringRef Name,
574 raw_ostream &OS) {
575 for (unsigned I = 0; I < Predicates.size(); ++I) {
576 StringRef Pred = Predicates[I]->getValueAsString(FieldName: Name);
577 Pred = Pred.trim();
578 OS.indent(NumSpaces: 2) << "case " << I + 1 << ": {\n";
579 OS.indent(NumSpaces: 4) << "// " << Predicates[I]->getName() << "\n";
580 OS.indent(NumSpaces: 4) << Pred << "\n";
581 OS.indent(NumSpaces: 2) << "}\n";
582 }
583}
584
585static void mergeCondAndCode(raw_ostream &CombinedStream, StringRef CondStr,
586 StringRef CodeStr, unsigned CompressedSize,
587 bool ReturnSize) {
588 CombinedStream.indent(NumSpaces: 4) << "if (" << CondStr << ") {\n";
589 CombinedStream << CodeStr;
590 CombinedStream.indent(NumSpaces: 4) << " return "
591 << (ReturnSize ? std::to_string(val: CompressedSize)
592 : "true")
593 << ";\n";
594 CombinedStream.indent(NumSpaces: 4) << "} // if\n";
595}
596
597void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
598 EmitterType EType) {
599 const Record *AsmWriter = Target.getAsmWriter();
600 if (!AsmWriter->getValueAsInt(FieldName: "PassSubtarget"))
601 PrintFatalError(ErrorLoc: AsmWriter->getLoc(),
602 Msg: "'PassSubtarget' is false. SubTargetInfo object is needed "
603 "for target features.");
604
605 StringRef TargetName = Target.getName();
606
607 // Sort entries in CompressPatterns to handle instructions that can have more
608 // than one candidate for compression\uncompression, e.g ADD can be
609 // transformed to a C_ADD or a C_MV. When emitting 'uncompress()' function the
610 // source and destination are flipped and the sort key needs to change
611 // accordingly.
612 llvm::stable_sort(Range&: CompressPatterns, C: [EType](const CompressPat &LHS,
613 const CompressPat &RHS) {
614 if (EType == EmitterType::Compress || EType == EmitterType::CheckCompress)
615 return LHS.Source.getName() < RHS.Source.getName();
616 return LHS.Dest.getName() < RHS.Dest.getName();
617 });
618
619 // A list of MCOperandPredicates for all operands in use, and the reverse map.
620 std::vector<const Record *> MCOpPredicates;
621 DenseMap<const Record *, unsigned> MCOpPredicateMap;
622 // A list of ImmLeaf Predicates for all operands in use, and the reverse map.
623 std::vector<const Record *> ImmLeafPredicates;
624 DenseMap<const Record *, unsigned> ImmLeafPredicateMap;
625
626 std::string F;
627 std::string FH;
628 raw_string_ostream Func(F);
629 raw_string_ostream FuncH(FH);
630
631 auto GetEmitterGuard = [EType]() -> StringRef {
632 switch (EType) {
633 case EmitterType::Compress:
634 return "GEN_COMPRESS_INSTR";
635 case EmitterType::Uncompress:
636 return "GEN_UNCOMPRESS_INSTR";
637 case EmitterType::CheckCompress:
638 return "GEN_CHECK_COMPRESS_INSTR";
639 }
640 llvm_unreachable("Invalid emitter type");
641 };
642
643 IfDefEmitter IfDef(OS, GetEmitterGuard());
644
645 if (EType == EmitterType::Compress) {
646 FuncH << "static bool compressInst(MCInst &OutInst,\n";
647 FuncH.indent(NumSpaces: 25) << "const MCInst &MI,\n";
648 FuncH.indent(NumSpaces: 25) << "const MCSubtargetInfo &STI) {\n";
649 } else if (EType == EmitterType::Uncompress) {
650 FuncH << "static bool uncompressInst(MCInst &OutInst,\n";
651 FuncH.indent(NumSpaces: 27) << "const MCInst &MI,\n";
652 FuncH.indent(NumSpaces: 27) << "const MCSubtargetInfo &STI) {\n";
653 } else if (EType == EmitterType::CheckCompress) {
654 FuncH << "static unsigned getCompressedSize(const MachineInstr &MI,\n";
655 FuncH.indent(NumSpaces: 34) << "const " << TargetName << "Subtarget &STI) {\n";
656 FuncH.indent(NumSpaces: 2)
657 << "// Returns the compressed size, or zero if not compressible.\n";
658 }
659 // HwModeId is used if we have any RegClassByHwMode patterns
660 if (!Target.getAllRegClassByHwMode().empty())
661 FuncH.indent(NumSpaces: 2) << "[[maybe_unused]] unsigned HwModeId = "
662 << "STI.getHwMode(MCSubtargetInfo::HwMode_RegInfo);\n";
663
664 if (CompressPatterns.empty()) {
665 OS << FH;
666 OS.indent(NumSpaces: 2) << "return "
667 << (EType == EmitterType::CheckCompress ? "0" : "false")
668 << ";\n}\n";
669 return;
670 }
671
672 std::string CaseString;
673 raw_string_ostream CaseStream(CaseString);
674 StringRef PrevOp;
675 StringRef CurOp;
676 CaseStream << " switch (MI.getOpcode()) {\n";
677 CaseStream << " default: return "
678 << (EType == EmitterType::CheckCompress ? "0" : "false") << ";\n";
679
680 bool CompressOrCheck =
681 EType == EmitterType::Compress || EType == EmitterType::CheckCompress;
682 bool CompressOrUncompress =
683 EType == EmitterType::Compress || EType == EmitterType::Uncompress;
684 std::string ValidatorName =
685 CompressOrUncompress
686 ? (TargetName + "ValidateMCOperandFor" +
687 (EType == EmitterType::Compress ? "Compress" : "Uncompress"))
688 .str()
689 : "";
690
691 for (const auto &CompressPat : CompressPatterns) {
692 if (EType == EmitterType::Uncompress && CompressPat.IsCompressOnly)
693 continue;
694
695 std::string CondString;
696 std::string CodeString;
697 raw_string_ostream CondStream(CondString);
698 raw_string_ostream CodeStream(CodeString);
699 const CodeGenInstruction &Source =
700 CompressOrCheck ? CompressPat.Source : CompressPat.Dest;
701 const CodeGenInstruction &Dest =
702 CompressOrCheck ? CompressPat.Dest : CompressPat.Source;
703 const IndexedMap<OpData> &SourceOperandMap =
704 CompressOrCheck ? CompressPat.SourceOperandMap
705 : CompressPat.DestOperandMap;
706 const IndexedMap<OpData> &DestOperandMap =
707 CompressOrCheck ? CompressPat.DestOperandMap
708 : CompressPat.SourceOperandMap;
709
710 CurOp = Source.getName();
711 // Check current and previous opcode to decide to continue or end a case.
712 if (CurOp != PrevOp) {
713 if (!PrevOp.empty()) {
714 CaseStream.indent(NumSpaces: 4) << "break;\n";
715 CaseStream.indent(NumSpaces: 2) << "} // case " + PrevOp + "\n";
716 }
717 CaseStream.indent(NumSpaces: 2) << "case " + TargetName + "::" + CurOp + ": {\n";
718 }
719
720 std::set<SubtargetFeatureLiteral> FeaturesSet;
721 std::set<std::set<SubtargetFeatureLiteral>> AnyOfFeatureSets;
722 // Add CompressPat required features.
723 getRequiredFeatures(FeaturesSet, AnyOfFeatureSets,
724 ReqPredicates: CompressPat.PatReqFeatures);
725
726 // Add Dest instruction required features.
727 std::vector<const Record *> ReqFeatures;
728 std::vector<const Record *> RF =
729 Dest.TheDef->getValueAsListOfDefs(FieldName: "Predicates");
730 copy_if(Range&: RF, Out: std::back_inserter(x&: ReqFeatures), P: [](const Record *R) {
731 return R->getValueAsBit(FieldName: "AssemblerMatcherPredicate");
732 });
733 getRequiredFeatures(FeaturesSet, AnyOfFeatureSets, ReqPredicates: ReqFeatures);
734
735 ListSeparator CondSep(" &&\n ");
736
737 // Emit checks for all required features.
738 for (const auto &Op : FeaturesSet) {
739 StringRef Not = Op.IsNot ? "!" : "";
740 CondStream << CondSep << Not << "STI.getFeatureBits()[" << TargetName
741 << "::" << Op.Feature << "]";
742 }
743
744 // Emit checks for all required feature groups.
745 for (const auto &Set : AnyOfFeatureSets) {
746 CondStream << CondSep << "(";
747 for (const auto &Op : Set) {
748 bool IsLast = &Op == &*Set.rbegin();
749 StringRef Not = Op.IsNot ? "!" : "";
750 CondStream << Not << "STI.getFeatureBits()[" << TargetName
751 << "::" << Op.Feature << "]";
752 if (!IsLast)
753 CondStream << " || ";
754 }
755 CondStream << ")";
756 }
757
758 // Start Source Inst operands validation.
759 unsigned OpNo = 0;
760 for (const auto &SourceOperand : Source.Operands) {
761 for (unsigned SubOp = 0; SubOp != SourceOperand.MINumOperands; ++SubOp) {
762 // Check for fixed immediates\registers in the source instruction.
763 switch (SourceOperandMap[OpNo].Kind) {
764 case OpData::Operand:
765 if (SourceOperandMap[OpNo].OpInfo.TiedOpIdx != -1) {
766 if (Source.Operands[OpNo].Rec->isSubClassOf(Name: "RegisterClassLike"))
767 CondStream << CondSep << "MI.getOperand(" << OpNo
768 << ").isReg() && MI.getOperand("
769 << SourceOperandMap[OpNo].OpInfo.TiedOpIdx
770 << ").isReg()" << CondSep << "(MI.getOperand(" << OpNo
771 << ").getReg() == MI.getOperand("
772 << SourceOperandMap[OpNo].OpInfo.TiedOpIdx
773 << ").getReg())";
774 else
775 PrintFatalError(Msg: "Unexpected tied operand types!");
776 }
777
778 // We don't need to do anything for source instruction operand checks.
779 break;
780 case OpData::Imm:
781 CondStream << CondSep << "MI.getOperand(" << OpNo << ").isImm()"
782 << CondSep << "(MI.getOperand(" << OpNo
783 << ").getImm() == " << SourceOperandMap[OpNo].ImmVal
784 << ")";
785 break;
786 case OpData::Reg: {
787 const Record *Reg = SourceOperandMap[OpNo].RegRec;
788 CondStream << CondSep << "MI.getOperand(" << OpNo << ").isReg()"
789 << CondSep << "(MI.getOperand(" << OpNo
790 << ").getReg() == ";
791 if (Reg->isSubClassOf(Name: "RegisterByHwMode")) {
792 RegisterByHwMode(Reg, Target.getRegBank())
793 .emitResolverCall(OS&: CondStream, HwMode: "HwModeId");
794 } else {
795 CondStream << TargetName << "::" << Reg->getName();
796 }
797 CondStream << ")";
798 break;
799 }
800 }
801 ++OpNo;
802 }
803 }
804 CodeStream.indent(NumSpaces: 6) << "// " << Dest.AsmString << "\n";
805 if (CompressOrUncompress)
806 CodeStream.indent(NumSpaces: 6) << "OutInst.setOpcode(" << TargetName
807 << "::" << Dest.getName() << ");\n";
808 OpNo = 0;
809 for (const auto &DestOperand : Dest.Operands) {
810 CodeStream.indent(NumSpaces: 6) << "// Operand: " << DestOperand.Name << "\n";
811
812 for (unsigned SubOp = 0; SubOp != DestOperand.MINumOperands; ++SubOp) {
813 const Record *DestRec = DestOperand.Rec;
814
815 if (DestOperand.MINumOperands > 1)
816 DestRec =
817 cast<DefInit>(Val: DestOperand.MIOperandInfo->getArg(Num: SubOp))->getDef();
818
819 switch (DestOperandMap[OpNo].Kind) {
820 case OpData::Operand: {
821 unsigned OpIdx = DestOperandMap[OpNo].OpInfo.Idx;
822 const Record *DagRec = DestOperandMap[OpNo].OpInfo.DagRec;
823 // Check that the operand in the Source instruction fits
824 // the type for the Dest instruction.
825 if (auto *ClassRec = Target.getAsRegClassLike(V: DagRec)) {
826 // This is a register operand. Check the register class.
827 // Don't check register class if this is a tied operand, it was done
828 // for the operand it's tied to.
829 if (DestOperand.getTiedRegister() == -1) {
830 CondStream << CondSep << "MI.getOperand(" << OpIdx << ").isReg()";
831 if (EType == EmitterType::CheckCompress)
832 CondStream << " && MI.getOperand(" << OpIdx
833 << ").getReg().isPhysical()";
834 CondStream << CondSep << "get" << TargetName
835 << "MCRegisterClass(";
836 if (ClassRec->isSubClassOf(Name: "RegClassByHwMode")) {
837 CondStream << TargetName << "RegClassByHwModeTables[HwModeId]["
838 << TargetName << "::" << ClassRec->getName() << "]";
839 } else {
840 CondStream << TargetName << "::" << ClassRec->getName()
841 << "RegClassID";
842 }
843 CondStream << ").contains(MI.getOperand(" << OpIdx
844 << ").getReg())";
845 }
846
847 if (CompressOrUncompress)
848 CodeStream.indent(NumSpaces: 6)
849 << "OutInst.addOperand(MI.getOperand(" << OpIdx << "));\n";
850 } else {
851 // Handling immediate operands.
852 if (CompressOrUncompress) {
853 unsigned Entry = getPredicates(PredicateMap&: MCOpPredicateMap, Predicates&: MCOpPredicates,
854 Rec: DagRec, Name: "MCOperandPredicate");
855 CondStream << CondSep << ValidatorName << "("
856 << "MI.getOperand(" << OpIdx << "), STI, " << Entry
857 << " /* " << DagRec->getName() << " */)";
858 // Also check DestRec if different than DagRec.
859 if (DagRec != DestRec) {
860 Entry = getPredicates(PredicateMap&: MCOpPredicateMap, Predicates&: MCOpPredicates, Rec: DestRec,
861 Name: "MCOperandPredicate");
862 CondStream << CondSep << ValidatorName << "("
863 << "MI.getOperand(" << OpIdx << "), STI, " << Entry
864 << " /* " << DestRec->getName() << " */)";
865 }
866 } else {
867 unsigned Entry =
868 getPredicates(PredicateMap&: ImmLeafPredicateMap, Predicates&: ImmLeafPredicates, Rec: DagRec,
869 Name: "ImmediateCode");
870 CondStream << CondSep << "MI.getOperand(" << OpIdx << ").isImm()";
871 CondStream << CondSep << TargetName << "ValidateMachineOperand("
872 << "MI.getOperand(" << OpIdx << "), &STI, " << Entry
873 << " /* " << DagRec->getName() << " */)";
874 if (DagRec != DestRec) {
875 Entry = getPredicates(PredicateMap&: ImmLeafPredicateMap, Predicates&: ImmLeafPredicates,
876 Rec: DestRec, Name: "ImmediateCode");
877 CondStream << CondSep << "MI.getOperand(" << OpIdx
878 << ").isImm()";
879 CondStream << CondSep << TargetName << "ValidateMachineOperand("
880 << "MI.getOperand(" << OpIdx << "), &STI, " << Entry
881 << " /* " << DestRec->getName() << " */)";
882 }
883 }
884 if (CompressOrUncompress)
885 CodeStream.indent(NumSpaces: 6)
886 << "OutInst.addOperand(MI.getOperand(" << OpIdx << "));\n";
887 }
888 break;
889 }
890 case OpData::Imm: {
891 if (CompressOrUncompress) {
892 unsigned Entry = getPredicates(PredicateMap&: MCOpPredicateMap, Predicates&: MCOpPredicates,
893 Rec: DestRec, Name: "MCOperandPredicate");
894 CondStream << CondSep << ValidatorName << "("
895 << "MCOperand::createImm(" << DestOperandMap[OpNo].ImmVal
896 << "), STI, " << Entry << " /* " << DestRec->getName()
897 << " */)";
898 } else {
899 unsigned Entry =
900 getPredicates(PredicateMap&: ImmLeafPredicateMap, Predicates&: ImmLeafPredicates, Rec: DestRec,
901 Name: "ImmediateCode");
902 CondStream << CondSep << TargetName
903 << "ValidateMachineOperand(MachineOperand::CreateImm("
904 << DestOperandMap[OpNo].ImmVal << "), &STI, " << Entry
905 << " /* " << DestRec->getName() << " */)";
906 }
907 if (CompressOrUncompress)
908 CodeStream.indent(NumSpaces: 6) << "OutInst.addOperand(MCOperand::createImm("
909 << DestOperandMap[OpNo].ImmVal << "));\n";
910 } break;
911 case OpData::Reg: {
912 if (CompressOrUncompress) {
913 // Fixed register has been validated at pattern validation time.
914 const Record *Reg = DestOperandMap[OpNo].RegRec;
915 CodeStream.indent(NumSpaces: 6) << "OutInst.addOperand(MCOperand::createReg(";
916 if (Reg->isSubClassOf(Name: "RegisterByHwMode")) {
917 RegisterByHwMode(Reg, Target.getRegBank())
918 .emitResolverCall(OS&: CodeStream, HwMode: "HwModeId");
919 } else {
920 CodeStream << TargetName << "::" << Reg->getName();
921 }
922 CodeStream << "));\n";
923 }
924 } break;
925 }
926 ++OpNo;
927 }
928 }
929 if (CompressOrUncompress)
930 CodeStream.indent(NumSpaces: 6) << "OutInst.setLoc(MI.getLoc());\n";
931 mergeCondAndCode(CombinedStream&: CaseStream, CondStr: CondString, CodeStr: CodeString,
932 CompressedSize: Dest.TheDef->getValueAsInt(FieldName: "Size"),
933 ReturnSize: EType == EmitterType::CheckCompress);
934 PrevOp = CurOp;
935 }
936 Func << CaseString;
937 Func.indent(NumSpaces: 4) << "break;\n";
938 // Close brace for the last case.
939 Func.indent(NumSpaces: 2) << "} // case " << CurOp << "\n";
940 Func.indent(NumSpaces: 2) << "} // switch\n";
941 Func.indent(NumSpaces: 2) << "return "
942 << (EType == EmitterType::CheckCompress ? "0" : "false")
943 << ";\n}\n";
944
945 if (!MCOpPredicates.empty()) {
946 auto IndentLength = ValidatorName.size() + 13;
947 OS << "static bool " << ValidatorName << "(const MCOperand &MCOp,\n";
948 OS.indent(NumSpaces: IndentLength) << "const MCSubtargetInfo &STI,\n";
949 OS.indent(NumSpaces: IndentLength) << "unsigned PredicateIndex) {\n";
950 OS << " switch (PredicateIndex) {\n"
951 << " default:\n"
952 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
953 << " break;\n";
954
955 printPredicates(Predicates: MCOpPredicates, Name: "MCOperandPredicate", OS);
956
957 OS << " }\n"
958 << "}\n\n";
959 }
960
961 if (!ImmLeafPredicates.empty()) {
962 auto IndentLength = TargetName.size() + 35;
963 OS << "static bool " << TargetName
964 << "ValidateMachineOperand(const MachineOperand &MO,\n";
965 OS.indent(NumSpaces: IndentLength)
966 << "const " << TargetName << "Subtarget *Subtarget,\n";
967 OS.indent(NumSpaces: IndentLength)
968 << "unsigned PredicateIndex) {\n"
969 << " int64_t Imm = MO.getImm();\n"
970 << " switch (PredicateIndex) {\n"
971 << " default:\n"
972 << " llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n"
973 << " break;\n";
974
975 printPredicates(Predicates: ImmLeafPredicates, Name: "ImmediateCode", OS);
976
977 OS << " }\n"
978 << "}\n\n";
979 }
980
981 OS << FH;
982 OS << F;
983}
984
985void CompressInstEmitter::run(raw_ostream &OS) {
986 // Process the CompressPat definitions, validating them as we do so.
987 for (const Record *Pat : Records.getAllDerivedDefinitions(ClassName: "CompressPat"))
988 evaluateCompressPat(Rec: Pat);
989
990 // Emit file header.
991 emitSourceFileHeader(Desc: "Compress instruction Source Fragment", OS, Record: Records);
992 // Generate compressInst() function.
993 emitCompressInstEmitter(OS, EType: EmitterType::Compress);
994 // Generate uncompressInst() function.
995 emitCompressInstEmitter(OS, EType: EmitterType::Uncompress);
996 // Generate getCompressedSize() function.
997 emitCompressInstEmitter(OS, EType: EmitterType::CheckCompress);
998}
999
1000static TableGen::Emitter::OptClass<CompressInstEmitter>
1001 X("gen-compress-inst-emitter", "Generate compressed instructions.");
1002