1 //===- RISCVCompressInstEmitter.cpp - Generator for RISCV 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 // RISCVCompressInstEmitter implements a tablegen-driven CompressPat based
8 // RISCV Instruction Compression mechanism.
9 //
10 //===--------------------------------------------------------------===//
11 //
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.
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 // class CompressPat<dag input, dag output> {
25 //   dag Input  = input;
26 //   dag Output    = output;
27 //   list<Predicate> Predicates = [];
28 // }
29 //
30 // let Predicates = [HasStdExtC] in {
31 // def : CompressPat<(ADD GPRNoX0:$rs1, GPRNoX0:$rs1, GPRNoX0:$rs2),
32 //                   (C_ADD GPRNoX0:$rs1, GPRNoX0:$rs2)>;
33 // }
34 //
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:
39 //
40 // bool compressInst(MCInst &OutInst, const MCInst &MI,
41 //                   const MCSubtargetInfo &STI,
42 //                   MCContext &Context);
43 //
44 // bool uncompressInst(MCInst &OutInst, const MCInst &MI,
45 //                     const MCRegisterInfo &MRI,
46 //                     const MCSubtargetInfo &STI);
47 //
48 // In addition, it exports a function for checking whether
49 // an instruction is compressable:
50 //
51 // bool isCompressibleInst(const MachineInstr& MI,
52 //                           const RISCVSubtarget *Subtarget,
53 //                           const MCRegisterInfo &MRI,
54 //                           const MCSubtargetInfo &STI);
55 //
56 // The clients that include this auto-generated header file and
57 // invoke these functions can compress an instruction before emitting
58 // it in the target-specific ASM or ELF streamer or can uncompress
59 // an instruction before printing it when the expanded instruction
60 // format aliases is favored.
61 
62 //===----------------------------------------------------------------------===//
63 
64 #include "CodeGenInstruction.h"
65 #include "CodeGenTarget.h"
66 #include "llvm/ADT/IndexedMap.h"
67 #include "llvm/ADT/SmallVector.h"
68 #include "llvm/ADT/StringExtras.h"
69 #include "llvm/ADT/StringMap.h"
70 #include "llvm/Support/Debug.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/TableGen/Error.h"
73 #include "llvm/TableGen/Record.h"
74 #include "llvm/TableGen/TableGenBackend.h"
75 #include <set>
76 #include <vector>
77 using namespace llvm;
78 
79 #define DEBUG_TYPE "compress-inst-emitter"
80 
81 namespace {
82 class RISCVCompressInstEmitter {
83   struct OpData {
84     enum MapKind { Operand, Imm, Reg };
85     MapKind Kind;
86     union {
87       unsigned Operand; // Operand number mapped to.
88       int64_t Imm;      // Integer immediate value.
89       Record *Reg;      // Physical register.
90     } Data;
91     int TiedOpIdx = -1; // Tied operand index within the instruction.
92   };
93   struct CompressPat {
94     CodeGenInstruction Source; // The source instruction definition.
95     CodeGenInstruction Dest;   // The destination instruction to transform to.
96     std::vector<Record *>
97         PatReqFeatures; // Required target features to enable pattern.
98     IndexedMap<OpData>
99         SourceOperandMap; // Maps operands in the Source Instruction to
100                           // the corresponding Dest instruction operand.
101     IndexedMap<OpData>
102         DestOperandMap; // Maps operands in the Dest Instruction
103                         // to the corresponding Source instruction operand.
104     bool IsCompressOnly;
CompressPat__anon2457e3f40111::RISCVCompressInstEmitter::CompressPat105     CompressPat(CodeGenInstruction &S, CodeGenInstruction &D,
106                 std::vector<Record *> RF, IndexedMap<OpData> &SourceMap,
107                 IndexedMap<OpData> &DestMap, bool IsCompressOnly)
108         : Source(S), Dest(D), PatReqFeatures(RF), SourceOperandMap(SourceMap),
109           DestOperandMap(DestMap), IsCompressOnly(IsCompressOnly) {}
110   };
111   enum EmitterType { Compress, Uncompress, CheckCompress };
112   RecordKeeper &Records;
113   CodeGenTarget Target;
114   SmallVector<CompressPat, 4> CompressPatterns;
115 
116   void addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Inst,
117                             IndexedMap<OpData> &OperandMap, bool IsSourceInst);
118   void evaluateCompressPat(Record *Compress);
119   void emitCompressInstEmitter(raw_ostream &o, EmitterType EType);
120   bool validateTypes(Record *SubType, Record *Type, bool IsSourceInst);
121   bool validateRegister(Record *Reg, Record *RegClass);
122   void createDagOperandMapping(Record *Rec, StringMap<unsigned> &SourceOperands,
123                                StringMap<unsigned> &DestOperands,
124                                DagInit *SourceDag, DagInit *DestDag,
125                                IndexedMap<OpData> &SourceOperandMap);
126 
127   void createInstOperandMapping(Record *Rec, DagInit *SourceDag,
128                                 DagInit *DestDag,
129                                 IndexedMap<OpData> &SourceOperandMap,
130                                 IndexedMap<OpData> &DestOperandMap,
131                                 StringMap<unsigned> &SourceOperands,
132                                 CodeGenInstruction &DestInst);
133 
134 public:
RISCVCompressInstEmitter(RecordKeeper & R)135   RISCVCompressInstEmitter(RecordKeeper &R) : Records(R), Target(R) {}
136 
137   void run(raw_ostream &o);
138 };
139 } // End anonymous namespace.
140 
validateRegister(Record * Reg,Record * RegClass)141 bool RISCVCompressInstEmitter::validateRegister(Record *Reg, Record *RegClass) {
142   assert(Reg->isSubClassOf("Register") && "Reg record should be a Register");
143   assert(RegClass->isSubClassOf("RegisterClass") &&
144          "RegClass record should be a RegisterClass");
145   const CodeGenRegisterClass &RC = Target.getRegisterClass(RegClass);
146   const CodeGenRegister *R = Target.getRegisterByName(Reg->getName().lower());
147   assert((R != nullptr) && "Register not defined!!");
148   return RC.contains(R);
149 }
150 
validateTypes(Record * DagOpType,Record * InstOpType,bool IsSourceInst)151 bool RISCVCompressInstEmitter::validateTypes(Record *DagOpType,
152                                              Record *InstOpType,
153                                              bool IsSourceInst) {
154   if (DagOpType == InstOpType)
155     return true;
156   // Only source instruction operands are allowed to not match Input Dag
157   // operands.
158   if (!IsSourceInst)
159     return false;
160 
161   if (DagOpType->isSubClassOf("RegisterClass") &&
162       InstOpType->isSubClassOf("RegisterClass")) {
163     const CodeGenRegisterClass &RC = Target.getRegisterClass(InstOpType);
164     const CodeGenRegisterClass &SubRC = Target.getRegisterClass(DagOpType);
165     return RC.hasSubClass(&SubRC);
166   }
167 
168   // At this point either or both types are not registers, reject the pattern.
169   if (DagOpType->isSubClassOf("RegisterClass") ||
170       InstOpType->isSubClassOf("RegisterClass"))
171     return false;
172 
173   // Let further validation happen when compress()/uncompress() functions are
174   // invoked.
175   LLVM_DEBUG(dbgs() << (IsSourceInst ? "Input" : "Output")
176                     << " Dag Operand Type: '" << DagOpType->getName()
177                     << "' and "
178                     << "Instruction Operand Type: '" << InstOpType->getName()
179                     << "' can't be checked at pattern validation time!\n");
180   return true;
181 }
182 
183 /// The patterns in the Dag contain different types of operands:
184 /// Register operands, e.g.: GPRC:$rs1; Fixed registers, e.g: X1; Immediate
185 /// operands, e.g.: simm6:$imm; Fixed immediate operands, e.g.: 0. This function
186 /// maps Dag operands to its corresponding instruction operands. For register
187 /// operands and fixed registers it expects the Dag operand type to be contained
188 /// in the instantiated instruction operand type. For immediate operands and
189 /// immediates no validation checks are enforced at pattern validation time.
addDagOperandMapping(Record * Rec,DagInit * Dag,CodeGenInstruction & Inst,IndexedMap<OpData> & OperandMap,bool IsSourceInst)190 void RISCVCompressInstEmitter::addDagOperandMapping(
191     Record *Rec, DagInit *Dag, CodeGenInstruction &Inst,
192     IndexedMap<OpData> &OperandMap, bool IsSourceInst) {
193   // TiedCount keeps track of the number of operands skipped in Inst
194   // operands list to get to the corresponding Dag operand. This is
195   // necessary because the number of operands in Inst might be greater
196   // than number of operands in the Dag due to how tied operands
197   // are represented.
198   unsigned TiedCount = 0;
199   for (unsigned i = 0, e = Inst.Operands.size(); i != e; ++i) {
200     int TiedOpIdx = Inst.Operands[i].getTiedRegister();
201     if (-1 != TiedOpIdx) {
202       // Set the entry in OperandMap for the tied operand we're skipping.
203       OperandMap[i].Kind = OperandMap[TiedOpIdx].Kind;
204       OperandMap[i].Data = OperandMap[TiedOpIdx].Data;
205       TiedCount++;
206       continue;
207     }
208     if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i - TiedCount))) {
209       if (DI->getDef()->isSubClassOf("Register")) {
210         // Check if the fixed register belongs to the Register class.
211         if (!validateRegister(DI->getDef(), Inst.Operands[i].Rec))
212           PrintFatalError(Rec->getLoc(),
213                           "Error in Dag '" + Dag->getAsString() +
214                               "'Register: '" + DI->getDef()->getName() +
215                               "' is not in register class '" +
216                               Inst.Operands[i].Rec->getName() + "'");
217         OperandMap[i].Kind = OpData::Reg;
218         OperandMap[i].Data.Reg = DI->getDef();
219         continue;
220       }
221       // Validate that Dag operand type matches the type defined in the
222       // corresponding instruction. Operands in the input Dag pattern are
223       // allowed to be a subclass of the type specified in corresponding
224       // instruction operand instead of being an exact match.
225       if (!validateTypes(DI->getDef(), Inst.Operands[i].Rec, IsSourceInst))
226         PrintFatalError(Rec->getLoc(),
227                         "Error in Dag '" + Dag->getAsString() + "'. Operand '" +
228                             Dag->getArgNameStr(i - TiedCount) + "' has type '" +
229                             DI->getDef()->getName() +
230                             "' which does not match the type '" +
231                             Inst.Operands[i].Rec->getName() +
232                             "' in the corresponding instruction operand!");
233 
234       OperandMap[i].Kind = OpData::Operand;
235     } else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i - TiedCount))) {
236       // Validate that corresponding instruction operand expects an immediate.
237       if (Inst.Operands[i].Rec->isSubClassOf("RegisterClass"))
238         PrintFatalError(
239             Rec->getLoc(),
240             "Error in Dag '" + Dag->getAsString() + "' Found immediate: '" +
241                 II->getAsString() +
242                 "' but corresponding instruction operand expected a register!");
243       // No pattern validation check possible for values of fixed immediate.
244       OperandMap[i].Kind = OpData::Imm;
245       OperandMap[i].Data.Imm = II->getValue();
246       LLVM_DEBUG(
247           dbgs() << "  Found immediate '" << II->getValue() << "' at "
248                  << (IsSourceInst ? "input " : "output ")
249                  << "Dag. No validation time check possible for values of "
250                     "fixed immediate.\n");
251     } else
252       llvm_unreachable("Unhandled CompressPat argument type!");
253   }
254 }
255 
256 // Verify the Dag operand count is enough to build an instruction.
verifyDagOpCount(CodeGenInstruction & Inst,DagInit * Dag,bool IsSource)257 static bool verifyDagOpCount(CodeGenInstruction &Inst, DagInit *Dag,
258                              bool IsSource) {
259   if (Dag->getNumArgs() == Inst.Operands.size())
260     return true;
261   // Source instructions are non compressed instructions and don't have tied
262   // operands.
263   if (IsSource)
264     PrintFatalError(Inst.TheDef->getLoc(),
265                     "Input operands for Inst '" + Inst.TheDef->getName() +
266                         "' and input Dag operand count mismatch");
267   // The Dag can't have more arguments than the Instruction.
268   if (Dag->getNumArgs() > Inst.Operands.size())
269     PrintFatalError(Inst.TheDef->getLoc(),
270                     "Inst '" + Inst.TheDef->getName() +
271                         "' and Dag operand count mismatch");
272 
273   // The Instruction might have tied operands so the Dag might have
274   //  a fewer operand count.
275   unsigned RealCount = Inst.Operands.size();
276   for (const auto &Operand : Inst.Operands)
277     if (Operand.getTiedRegister() != -1)
278       --RealCount;
279 
280   if (Dag->getNumArgs() != RealCount)
281     PrintFatalError(Inst.TheDef->getLoc(),
282                     "Inst '" + Inst.TheDef->getName() +
283                         "' and Dag operand count mismatch");
284   return true;
285 }
286 
validateArgsTypes(Init * Arg1,Init * Arg2)287 static bool validateArgsTypes(Init *Arg1, Init *Arg2) {
288   return cast<DefInit>(Arg1)->getDef() == cast<DefInit>(Arg2)->getDef();
289 }
290 
291 // Creates a mapping between the operand name in the Dag (e.g. $rs1) and
292 // its index in the list of Dag operands and checks that operands with the same
293 // name have the same types. For example in 'C_ADD $rs1, $rs2' we generate the
294 // mapping $rs1 --> 0, $rs2 ---> 1. If the operand appears twice in the (tied)
295 // same Dag we use the last occurrence for indexing.
createDagOperandMapping(Record * Rec,StringMap<unsigned> & SourceOperands,StringMap<unsigned> & DestOperands,DagInit * SourceDag,DagInit * DestDag,IndexedMap<OpData> & SourceOperandMap)296 void RISCVCompressInstEmitter::createDagOperandMapping(
297     Record *Rec, StringMap<unsigned> &SourceOperands,
298     StringMap<unsigned> &DestOperands, DagInit *SourceDag, DagInit *DestDag,
299     IndexedMap<OpData> &SourceOperandMap) {
300   for (unsigned i = 0; i < DestDag->getNumArgs(); ++i) {
301     // Skip fixed immediates and registers, they were handled in
302     // addDagOperandMapping.
303     if ("" == DestDag->getArgNameStr(i))
304       continue;
305     DestOperands[DestDag->getArgNameStr(i)] = i;
306   }
307 
308   for (unsigned i = 0; i < SourceDag->getNumArgs(); ++i) {
309     // Skip fixed immediates and registers, they were handled in
310     // addDagOperandMapping.
311     if ("" == SourceDag->getArgNameStr(i))
312       continue;
313 
314     StringMap<unsigned>::iterator it =
315         SourceOperands.find(SourceDag->getArgNameStr(i));
316     if (it != SourceOperands.end()) {
317       // Operand sharing the same name in the Dag should be mapped as tied.
318       SourceOperandMap[i].TiedOpIdx = it->getValue();
319       if (!validateArgsTypes(SourceDag->getArg(it->getValue()),
320                              SourceDag->getArg(i)))
321         PrintFatalError(Rec->getLoc(),
322                         "Input Operand '" + SourceDag->getArgNameStr(i) +
323                             "' has a mismatched tied operand!\n");
324     }
325     it = DestOperands.find(SourceDag->getArgNameStr(i));
326     if (it == DestOperands.end())
327       PrintFatalError(Rec->getLoc(), "Operand " + SourceDag->getArgNameStr(i) +
328                                          " defined in Input Dag but not used in"
329                                          " Output Dag!\n");
330     // Input Dag operand types must match output Dag operand type.
331     if (!validateArgsTypes(DestDag->getArg(it->getValue()),
332                            SourceDag->getArg(i)))
333       PrintFatalError(Rec->getLoc(), "Type mismatch between Input and "
334                                      "Output Dag operand '" +
335                                          SourceDag->getArgNameStr(i) + "'!");
336     SourceOperands[SourceDag->getArgNameStr(i)] = i;
337   }
338 }
339 
340 /// Map operand names in the Dag to their index in both corresponding input and
341 /// output instructions. Validate that operands defined in the input are
342 /// used in the output pattern while populating the maps.
createInstOperandMapping(Record * Rec,DagInit * SourceDag,DagInit * DestDag,IndexedMap<OpData> & SourceOperandMap,IndexedMap<OpData> & DestOperandMap,StringMap<unsigned> & SourceOperands,CodeGenInstruction & DestInst)343 void RISCVCompressInstEmitter::createInstOperandMapping(
344     Record *Rec, DagInit *SourceDag, DagInit *DestDag,
345     IndexedMap<OpData> &SourceOperandMap, IndexedMap<OpData> &DestOperandMap,
346     StringMap<unsigned> &SourceOperands, CodeGenInstruction &DestInst) {
347   // TiedCount keeps track of the number of operands skipped in Inst
348   // operands list to get to the corresponding Dag operand.
349   unsigned TiedCount = 0;
350   LLVM_DEBUG(dbgs() << "  Operand mapping:\n  Source   Dest\n");
351   for (unsigned i = 0, e = DestInst.Operands.size(); i != e; ++i) {
352     int TiedInstOpIdx = DestInst.Operands[i].getTiedRegister();
353     if (TiedInstOpIdx != -1) {
354       ++TiedCount;
355       DestOperandMap[i].Data = DestOperandMap[TiedInstOpIdx].Data;
356       DestOperandMap[i].Kind = DestOperandMap[TiedInstOpIdx].Kind;
357       if (DestOperandMap[i].Kind == OpData::Operand)
358         // No need to fill the SourceOperandMap here since it was mapped to
359         // destination operand 'TiedInstOpIdx' in a previous iteration.
360         LLVM_DEBUG(dbgs() << "    " << DestOperandMap[i].Data.Operand
361                           << " ====> " << i
362                           << "  Dest operand tied with operand '"
363                           << TiedInstOpIdx << "'\n");
364       continue;
365     }
366     // Skip fixed immediates and registers, they were handled in
367     // addDagOperandMapping.
368     if (DestOperandMap[i].Kind != OpData::Operand)
369       continue;
370 
371     unsigned DagArgIdx = i - TiedCount;
372     StringMap<unsigned>::iterator SourceOp =
373         SourceOperands.find(DestDag->getArgNameStr(DagArgIdx));
374     if (SourceOp == SourceOperands.end())
375       PrintFatalError(Rec->getLoc(),
376                       "Output Dag operand '" +
377                           DestDag->getArgNameStr(DagArgIdx) +
378                           "' has no matching input Dag operand.");
379 
380     assert(DestDag->getArgNameStr(DagArgIdx) ==
381                SourceDag->getArgNameStr(SourceOp->getValue()) &&
382            "Incorrect operand mapping detected!\n");
383     DestOperandMap[i].Data.Operand = SourceOp->getValue();
384     SourceOperandMap[SourceOp->getValue()].Data.Operand = i;
385     LLVM_DEBUG(dbgs() << "    " << SourceOp->getValue() << " ====> " << i
386                       << "\n");
387   }
388 }
389 
390 /// Validates the CompressPattern and create operand mapping.
391 /// These are the checks to validate a CompressPat pattern declarations.
392 /// Error out with message under these conditions:
393 /// - Dag Input opcode is an expanded instruction and Dag Output opcode is a
394 ///   compressed instruction.
395 /// - Operands in Dag Input must be all used in Dag Output.
396 ///   Register Operand type in Dag Input Type  must be contained in the
397 ///   corresponding Source Instruction type.
398 /// - Register Operand type in Dag Input must be the  same as in  Dag Ouput.
399 /// - Register Operand type in  Dag Output must be the same  as the
400 ///   corresponding Destination Inst type.
401 /// - Immediate Operand type in Dag Input must be the same as in Dag Ouput.
402 /// - Immediate Operand type in Dag Ouput must be the same as the corresponding
403 ///   Destination Instruction type.
404 /// - Fixed register must be contained in the corresponding Source Instruction
405 ///   type.
406 /// - Fixed register must be contained in the corresponding Destination
407 ///   Instruction type. Warning message printed under these conditions:
408 /// - Fixed immediate in Dag Input or Dag Ouput cannot be checked at this time
409 ///   and generate warning.
410 /// - Immediate operand type in Dag Input differs from the corresponding Source
411 ///   Instruction type  and generate a warning.
evaluateCompressPat(Record * Rec)412 void RISCVCompressInstEmitter::evaluateCompressPat(Record *Rec) {
413   // Validate input Dag operands.
414   DagInit *SourceDag = Rec->getValueAsDag("Input");
415   assert(SourceDag && "Missing 'Input' in compress pattern!");
416   LLVM_DEBUG(dbgs() << "Input: " << *SourceDag << "\n");
417 
418   // Checking we are transforming from compressed to uncompressed instructions.
419   Record *Operator = SourceDag->getOperatorAsDef(Rec->getLoc());
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);
425 
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");
430 
431   Record *DestOperator = DestDag->getOperatorAsDef(Rec->getLoc());
432   if (!DestOperator->isSubClassOf("RVInst16"))
433     PrintFatalError(Rec->getLoc(), "Output instruction  '" +
434                                        DestOperator->getName() +
435                                        "' is not a 16 bit wide instruction!");
436   CodeGenInstruction DestInst(DestOperator);
437   verifyDagOpCount(DestInst, DestDag, false);
438 
439   // Fill the mapping from the source to destination instructions.
440 
441   IndexedMap<OpData> SourceOperandMap;
442   SourceOperandMap.grow(SourceInst.Operands.size());
443   // Create a mapping between source Dag operands and source Inst operands.
444   addDagOperandMapping(Rec, SourceDag, SourceInst, SourceOperandMap,
445                        /*IsSourceInst*/ true);
446 
447   IndexedMap<OpData> DestOperandMap;
448   DestOperandMap.grow(DestInst.Operands.size());
449   // Create a mapping between destination Dag operands and destination Inst
450   // operands.
451   addDagOperandMapping(Rec, DestDag, DestInst, DestOperandMap,
452                        /*IsSourceInst*/ false);
453 
454   StringMap<unsigned> SourceOperands;
455   StringMap<unsigned> DestOperands;
456   createDagOperandMapping(Rec, SourceOperands, DestOperands, SourceDag, DestDag,
457                           SourceOperandMap);
458   // Create operand mapping between the source and destination instructions.
459   createInstOperandMapping(Rec, SourceDag, DestDag, SourceOperandMap,
460                            DestOperandMap, SourceOperands, DestInst);
461 
462   // Get the target features for the CompressPat.
463   std::vector<Record *> PatReqFeatures;
464   std::vector<Record *> RF = Rec->getValueAsListOfDefs("Predicates");
465   copy_if(RF, std::back_inserter(PatReqFeatures), [](Record *R) {
466     return R->getValueAsBit("AssemblerMatcherPredicate");
467   });
468 
469   CompressPatterns.push_back(CompressPat(SourceInst, DestInst, PatReqFeatures,
470                                          SourceOperandMap, DestOperandMap,
471                                          Rec->getValueAsBit("isCompressOnly")));
472 }
473 
474 static void
getReqFeatures(std::set<std::pair<bool,StringRef>> & FeaturesSet,std::set<std::set<std::pair<bool,StringRef>>> & AnyOfFeatureSets,const std::vector<Record * > & ReqFeatures)475 getReqFeatures(std::set<std::pair<bool, StringRef>> &FeaturesSet,
476                std::set<std::set<std::pair<bool, StringRef>>> &AnyOfFeatureSets,
477                const std::vector<Record *> &ReqFeatures) {
478   for (auto &R : ReqFeatures) {
479     const DagInit *D = R->getValueAsDag("AssemblerCondDag");
480     std::string CombineType = D->getOperator()->getAsString();
481     if (CombineType != "any_of" && CombineType != "all_of")
482       PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
483     if (D->getNumArgs() == 0)
484       PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
485     bool IsOr = CombineType == "any_of";
486     std::set<std::pair<bool, StringRef>> AnyOfSet;
487 
488     for (auto *Arg : D->getArgs()) {
489       bool IsNot = false;
490       if (auto *NotArg = dyn_cast<DagInit>(Arg)) {
491         if (NotArg->getOperator()->getAsString() != "not" ||
492             NotArg->getNumArgs() != 1)
493           PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
494         Arg = NotArg->getArg(0);
495         IsNot = true;
496       }
497       if (!isa<DefInit>(Arg) ||
498           !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature"))
499         PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
500       if (IsOr)
501         AnyOfSet.insert({IsNot, cast<DefInit>(Arg)->getDef()->getName()});
502       else
503         FeaturesSet.insert({IsNot, cast<DefInit>(Arg)->getDef()->getName()});
504     }
505 
506     if (IsOr)
507       AnyOfFeatureSets.insert(AnyOfSet);
508   }
509 }
510 
getPredicates(DenseMap<const Record *,unsigned> & PredicateMap,std::vector<const Record * > & Predicates,Record * Rec,StringRef Name)511 static unsigned getPredicates(DenseMap<const Record *, unsigned> &PredicateMap,
512                               std::vector<const Record *> &Predicates,
513                               Record *Rec, StringRef Name) {
514   unsigned &Entry = PredicateMap[Rec];
515   if (Entry)
516     return Entry;
517 
518   if (!Rec->isValueUnset(Name)) {
519     Predicates.push_back(Rec);
520     Entry = Predicates.size();
521     return Entry;
522   }
523 
524   PrintFatalError(Rec->getLoc(), "No " + Name +
525                                      " predicate on this operand at all: '" +
526                                      Rec->getName() + "'");
527   return 0;
528 }
529 
printPredicates(const std::vector<const Record * > & Predicates,StringRef Name,raw_ostream & o)530 static void printPredicates(const std::vector<const Record *> &Predicates,
531                             StringRef Name, raw_ostream &o) {
532   for (unsigned i = 0; i < Predicates.size(); ++i) {
533     StringRef Pred = Predicates[i]->getValueAsString(Name);
534     o << "  case " << i + 1 << ": {\n"
535       << "  // " << Predicates[i]->getName() << "\n"
536       << "  " << Pred << "\n"
537       << "  }\n";
538   }
539 }
540 
mergeCondAndCode(raw_ostream & CombinedStream,StringRef CondStr,StringRef CodeStr)541 static void mergeCondAndCode(raw_ostream &CombinedStream, StringRef CondStr,
542                              StringRef CodeStr) {
543   // Remove first indentation and last '&&'.
544   CondStr = CondStr.drop_front(6).drop_back(4);
545   CombinedStream.indent(4) << "if (" << CondStr << ") {\n";
546   CombinedStream << CodeStr;
547   CombinedStream.indent(4) << "  return true;\n";
548   CombinedStream.indent(4) << "} // if\n";
549 }
550 
emitCompressInstEmitter(raw_ostream & o,EmitterType EType)551 void RISCVCompressInstEmitter::emitCompressInstEmitter(raw_ostream &o,
552                                                        EmitterType EType) {
553   Record *AsmWriter = Target.getAsmWriter();
554   if (!AsmWriter->getValueAsInt("PassSubtarget"))
555     PrintFatalError(AsmWriter->getLoc(),
556                     "'PassSubtarget' is false. SubTargetInfo object is needed "
557                     "for target features.\n");
558 
559   StringRef Namespace = Target.getName();
560 
561   // Sort entries in CompressPatterns to handle instructions that can have more
562   // than one candidate for compression\uncompression, e.g ADD can be
563   // transformed to a C_ADD or a C_MV. When emitting 'uncompress()' function the
564   // source and destination are flipped and the sort key needs to change
565   // accordingly.
566   llvm::stable_sort(CompressPatterns, [EType](const CompressPat &LHS,
567                                               const CompressPat &RHS) {
568     if (EType == EmitterType::Compress || EType == EmitterType::CheckCompress)
569       return (LHS.Source.TheDef->getName() < RHS.Source.TheDef->getName());
570     else
571       return (LHS.Dest.TheDef->getName() < RHS.Dest.TheDef->getName());
572   });
573 
574   // A list of MCOperandPredicates for all operands in use, and the reverse map.
575   std::vector<const Record *> MCOpPredicates;
576   DenseMap<const Record *, unsigned> MCOpPredicateMap;
577   // A list of ImmLeaf Predicates for all operands in use, and the reverse map.
578   std::vector<const Record *> ImmLeafPredicates;
579   DenseMap<const Record *, unsigned> ImmLeafPredicateMap;
580 
581   std::string F;
582   std::string FH;
583   raw_string_ostream Func(F);
584   raw_string_ostream FuncH(FH);
585   bool NeedMRI = false;
586 
587   if (EType == EmitterType::Compress)
588     o << "\n#ifdef GEN_COMPRESS_INSTR\n"
589       << "#undef GEN_COMPRESS_INSTR\n\n";
590   else if (EType == EmitterType::Uncompress)
591     o << "\n#ifdef GEN_UNCOMPRESS_INSTR\n"
592       << "#undef GEN_UNCOMPRESS_INSTR\n\n";
593   else if (EType == EmitterType::CheckCompress)
594     o << "\n#ifdef GEN_CHECK_COMPRESS_INSTR\n"
595       << "#undef GEN_CHECK_COMPRESS_INSTR\n\n";
596 
597   if (EType == EmitterType::Compress) {
598     FuncH << "static bool compressInst(MCInst &OutInst,\n";
599     FuncH.indent(25) << "const MCInst &MI,\n";
600     FuncH.indent(25) << "const MCSubtargetInfo &STI,\n";
601     FuncH.indent(25) << "MCContext &Context) {\n";
602   } else if (EType == EmitterType::Uncompress){
603     FuncH << "static bool uncompressInst(MCInst &OutInst,\n";
604     FuncH.indent(27) << "const MCInst &MI,\n";
605     FuncH.indent(27) << "const MCRegisterInfo &MRI,\n";
606     FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n";
607   } else if (EType == EmitterType::CheckCompress) {
608     FuncH << "static bool isCompressibleInst(const MachineInstr &MI,\n";
609     FuncH.indent(27) << "const RISCVSubtarget *Subtarget,\n";
610     FuncH.indent(27) << "const MCRegisterInfo &MRI,\n";
611     FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n";
612   }
613 
614   if (CompressPatterns.empty()) {
615     o << FuncH.str();
616     o.indent(2) << "return false;\n}\n";
617     if (EType == EmitterType::Compress)
618       o << "\n#endif //GEN_COMPRESS_INSTR\n";
619     else if (EType == EmitterType::Uncompress)
620       o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n";
621     else if (EType == EmitterType::CheckCompress)
622       o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n";
623     return;
624   }
625 
626   std::string CaseString;
627   raw_string_ostream CaseStream(CaseString);
628   StringRef PrevOp;
629   StringRef CurOp;
630   CaseStream << "  switch (MI.getOpcode()) {\n";
631   CaseStream << "    default: return false;\n";
632 
633   bool CompressOrCheck =
634     EType == EmitterType::Compress || EType == EmitterType::CheckCompress;
635   bool CompressOrUncompress =
636     EType == EmitterType::Compress || EType == EmitterType::Uncompress;
637 
638   for (auto &CompressPat : CompressPatterns) {
639     if (EType == EmitterType::Uncompress && CompressPat.IsCompressOnly)
640       continue;
641 
642     std::string CondString;
643     std::string CodeString;
644     raw_string_ostream CondStream(CondString);
645     raw_string_ostream CodeStream(CodeString);
646     CodeGenInstruction &Source =
647         CompressOrCheck ?  CompressPat.Source : CompressPat.Dest;
648     CodeGenInstruction &Dest =
649         CompressOrCheck ? CompressPat.Dest : CompressPat.Source;
650     IndexedMap<OpData> SourceOperandMap = CompressOrCheck ?
651       CompressPat.SourceOperandMap : CompressPat.DestOperandMap;
652     IndexedMap<OpData> &DestOperandMap = CompressOrCheck ?
653       CompressPat.DestOperandMap : CompressPat.SourceOperandMap;
654 
655     CurOp = Source.TheDef->getName();
656     // Check current and previous opcode to decide to continue or end a case.
657     if (CurOp != PrevOp) {
658       if (!PrevOp.empty())
659         CaseStream.indent(6) << "break;\n    } // case " + PrevOp + "\n";
660       CaseStream.indent(4) << "case " + Namespace + "::" + CurOp + ": {\n";
661     }
662 
663     std::set<std::pair<bool, StringRef>> FeaturesSet;
664     std::set<std::set<std::pair<bool, StringRef>>> AnyOfFeatureSets;
665     // Add CompressPat required features.
666     getReqFeatures(FeaturesSet, AnyOfFeatureSets, CompressPat.PatReqFeatures);
667 
668     // Add Dest instruction required features.
669     std::vector<Record *> ReqFeatures;
670     std::vector<Record *> RF = Dest.TheDef->getValueAsListOfDefs("Predicates");
671     copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) {
672       return R->getValueAsBit("AssemblerMatcherPredicate");
673     });
674     getReqFeatures(FeaturesSet, AnyOfFeatureSets, ReqFeatures);
675 
676     // Emit checks for all required features.
677     for (auto &Op : FeaturesSet) {
678       StringRef Not = Op.first ? "!" : "";
679       CondStream.indent(6) << Not << "STI.getFeatureBits()[" << Namespace
680                            << "::" << Op.second << "]"
681                            << " &&\n";
682     }
683 
684     // Emit checks for all required feature groups.
685     for (auto &Set : AnyOfFeatureSets) {
686       CondStream.indent(6) << "(";
687       for (auto &Op : Set) {
688         bool isLast = &Op == &*Set.rbegin();
689         StringRef Not = Op.first ? "!" : "";
690         CondStream << Not << "STI.getFeatureBits()[" << Namespace
691                    << "::" << Op.second << "]";
692         if (!isLast)
693           CondStream << " || ";
694       }
695       CondStream << ") &&\n";
696     }
697 
698     // Start Source Inst operands validation.
699     unsigned OpNo = 0;
700     for (OpNo = 0; OpNo < Source.Operands.size(); ++OpNo) {
701       if (SourceOperandMap[OpNo].TiedOpIdx != -1) {
702         if (Source.Operands[OpNo].Rec->isSubClassOf("RegisterClass"))
703           CondStream.indent(6)
704               << "(MI.getOperand(" << OpNo << ").getReg() ==  MI.getOperand("
705               << SourceOperandMap[OpNo].TiedOpIdx << ").getReg()) &&\n";
706         else
707           PrintFatalError("Unexpected tied operand types!\n");
708       }
709       // Check for fixed immediates\registers in the source instruction.
710       switch (SourceOperandMap[OpNo].Kind) {
711       case OpData::Operand:
712         // We don't need to do anything for source instruction operand checks.
713         break;
714       case OpData::Imm:
715         CondStream.indent(6)
716             << "(MI.getOperand(" << OpNo << ").isImm()) &&\n"
717             << "      (MI.getOperand(" << OpNo
718             << ").getImm() == " << SourceOperandMap[OpNo].Data.Imm << ") &&\n";
719         break;
720       case OpData::Reg: {
721         Record *Reg = SourceOperandMap[OpNo].Data.Reg;
722         CondStream.indent(6)
723             << "(MI.getOperand(" << OpNo << ").getReg() == " << Namespace
724             << "::" << Reg->getName() << ") &&\n";
725         break;
726       }
727       }
728     }
729     CodeStream.indent(6) << "// " << Dest.AsmString << "\n";
730     if (CompressOrUncompress)
731       CodeStream.indent(6) << "OutInst.setOpcode(" << Namespace
732                            << "::" << Dest.TheDef->getName() << ");\n";
733     OpNo = 0;
734     for (const auto &DestOperand : Dest.Operands) {
735       CodeStream.indent(6) << "// Operand: " << DestOperand.Name << "\n";
736       switch (DestOperandMap[OpNo].Kind) {
737       case OpData::Operand: {
738         unsigned OpIdx = DestOperandMap[OpNo].Data.Operand;
739         // Check that the operand in the Source instruction fits
740         // the type for the Dest instruction.
741         if (DestOperand.Rec->isSubClassOf("RegisterClass")) {
742           NeedMRI = true;
743           // This is a register operand. Check the register class.
744           // Don't check register class if this is a tied operand, it was done
745           // for the operand its tied to.
746           if (DestOperand.getTiedRegister() == -1)
747             CondStream.indent(6) << "(MRI.getRegClass(" << Namespace
748                                  << "::" << DestOperand.Rec->getName()
749                                  << "RegClassID).contains(MI.getOperand("
750                                  << OpIdx << ").getReg())) &&\n";
751 
752           if (CompressOrUncompress)
753             CodeStream.indent(6)
754                 << "OutInst.addOperand(MI.getOperand(" << OpIdx << "));\n";
755         } else {
756           // Handling immediate operands.
757           if (CompressOrUncompress) {
758             unsigned Entry =
759                 getPredicates(MCOpPredicateMap, MCOpPredicates, DestOperand.Rec,
760                               "MCOperandPredicate");
761             CondStream.indent(6)
762                 << Namespace << "ValidateMCOperand("
763                 << "MI.getOperand(" << OpIdx << "), STI, " << Entry << ") &&\n";
764           } else {
765             unsigned Entry =
766                 getPredicates(ImmLeafPredicateMap, ImmLeafPredicates,
767                               DestOperand.Rec, "ImmediateCode");
768             CondStream.indent(6)
769                 << "MI.getOperand(" << OpIdx << ").isImm() &&\n";
770             CondStream.indent(6) << Namespace << "ValidateMachineOperand("
771                                  << "MI.getOperand(" << OpIdx
772                                  << "), Subtarget, " << Entry << ") &&\n";
773           }
774           if (CompressOrUncompress)
775             CodeStream.indent(6)
776                 << "OutInst.addOperand(MI.getOperand(" << OpIdx << "));\n";
777         }
778         break;
779       }
780       case OpData::Imm: {
781         if (CompressOrUncompress) {
782           unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates,
783                                          DestOperand.Rec, "MCOperandPredicate");
784           CondStream.indent(6)
785               << Namespace << "ValidateMCOperand("
786               << "MCOperand::createImm(" << DestOperandMap[OpNo].Data.Imm
787               << "), STI, " << Entry << ") &&\n";
788         } else {
789           unsigned Entry = getPredicates(ImmLeafPredicateMap, ImmLeafPredicates,
790                                          DestOperand.Rec, "ImmediateCode");
791           CondStream.indent(6)
792               << Namespace
793               << "ValidateMachineOperand(MachineOperand::CreateImm("
794               << DestOperandMap[OpNo].Data.Imm << "), SubTarget, " << Entry
795               << ") &&\n";
796         }
797         if (CompressOrUncompress)
798           CodeStream.indent(6) << "OutInst.addOperand(MCOperand::createImm("
799                                << DestOperandMap[OpNo].Data.Imm << "));\n";
800       } break;
801       case OpData::Reg: {
802         if (CompressOrUncompress) {
803           // Fixed register has been validated at pattern validation time.
804           Record *Reg = DestOperandMap[OpNo].Data.Reg;
805           CodeStream.indent(6)
806               << "OutInst.addOperand(MCOperand::createReg(" << Namespace
807               << "::" << Reg->getName() << "));\n";
808         }
809       } break;
810       }
811       ++OpNo;
812     }
813     if (CompressOrUncompress)
814       CodeStream.indent(6) << "OutInst.setLoc(MI.getLoc());\n";
815     mergeCondAndCode(CaseStream, CondStream.str(), CodeStream.str());
816     PrevOp = CurOp;
817   }
818   Func << CaseStream.str() << "\n";
819   // Close brace for the last case.
820   Func.indent(4) << "} // case " << CurOp << "\n";
821   Func.indent(2) << "} // switch\n";
822   Func.indent(2) << "return false;\n}\n";
823 
824   if (!MCOpPredicates.empty()) {
825     o << "static bool " << Namespace
826       << "ValidateMCOperand(const MCOperand &MCOp,\n"
827       << "                  const MCSubtargetInfo &STI,\n"
828       << "                  unsigned PredicateIndex) {\n"
829       << "  switch (PredicateIndex) {\n"
830       << "  default:\n"
831       << "    llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
832       << "    break;\n";
833 
834     printPredicates(MCOpPredicates, "MCOperandPredicate", o);
835 
836     o << "  }\n"
837       << "}\n\n";
838   }
839 
840   if (!ImmLeafPredicates.empty()) {
841     o << "static bool " << Namespace
842       << "ValidateMachineOperand(const MachineOperand &MO,\n"
843       << "                  const RISCVSubtarget *Subtarget,\n"
844       << "                  unsigned PredicateIndex) {\n"
845       << "  int64_t Imm = MO.getImm();\n"
846       << "  switch (PredicateIndex) {\n"
847       << "  default:\n"
848       << "    llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n"
849       << "    break;\n";
850 
851     printPredicates(ImmLeafPredicates, "ImmediateCode", o);
852 
853     o << "  }\n"
854       << "}\n\n";
855   }
856 
857   o << FuncH.str();
858   if (NeedMRI && EType == EmitterType::Compress)
859     o.indent(2) << "const MCRegisterInfo &MRI = *Context.getRegisterInfo();\n";
860   o << Func.str();
861 
862   if (EType == EmitterType::Compress)
863     o << "\n#endif //GEN_COMPRESS_INSTR\n";
864   else if (EType == EmitterType::Uncompress)
865     o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n";
866   else if (EType == EmitterType::CheckCompress)
867     o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n";
868 }
869 
run(raw_ostream & o)870 void RISCVCompressInstEmitter::run(raw_ostream &o) {
871   std::vector<Record *> Insts = Records.getAllDerivedDefinitions("CompressPat");
872 
873   // Process the CompressPat definitions, validating them as we do so.
874   for (unsigned i = 0, e = Insts.size(); i != e; ++i)
875     evaluateCompressPat(Insts[i]);
876 
877   // Emit file header.
878   emitSourceFileHeader("Compress instruction Source Fragment", o);
879   // Generate compressInst() function.
880   emitCompressInstEmitter(o, EmitterType::Compress);
881   // Generate uncompressInst() function.
882   emitCompressInstEmitter(o, EmitterType::Uncompress);
883   // Generate isCompressibleInst() function.
884   emitCompressInstEmitter(o, EmitterType::CheckCompress);
885 }
886 
887 namespace llvm {
888 
EmitCompressInst(RecordKeeper & RK,raw_ostream & OS)889 void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS) {
890   RISCVCompressInstEmitter(RK).run(OS);
891 }
892 
893 } // namespace llvm
894