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.
CompressPat__anonaa3be4960111::RISCVCompressInstEmitter::CompressPat104     CompressPat(CodeGenInstruction &S, CodeGenInstruction &D,
105                 std::vector<Record *> RF, IndexedMap<OpData> &SourceMap,
106                 IndexedMap<OpData> &DestMap)
107         : Source(S), Dest(D), PatReqFeatures(RF), SourceOperandMap(SourceMap),
108           DestOperandMap(DestMap) {}
109   };
110   enum EmitterType { Compress, Uncompress, CheckCompress };
111   RecordKeeper &Records;
112   CodeGenTarget Target;
113   SmallVector<CompressPat, 4> CompressPatterns;
114 
115   void addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Inst,
116                             IndexedMap<OpData> &OperandMap, bool IsSourceInst);
117   void evaluateCompressPat(Record *Compress);
118   void emitCompressInstEmitter(raw_ostream &o, EmitterType EType);
119   bool validateTypes(Record *SubType, Record *Type, bool IsSourceInst);
120   bool validateRegister(Record *Reg, Record *RegClass);
121   void createDagOperandMapping(Record *Rec, StringMap<unsigned> &SourceOperands,
122                                StringMap<unsigned> &DestOperands,
123                                DagInit *SourceDag, DagInit *DestDag,
124                                IndexedMap<OpData> &SourceOperandMap);
125 
126   void createInstOperandMapping(Record *Rec, DagInit *SourceDag,
127                                 DagInit *DestDag,
128                                 IndexedMap<OpData> &SourceOperandMap,
129                                 IndexedMap<OpData> &DestOperandMap,
130                                 StringMap<unsigned> &SourceOperands,
131                                 CodeGenInstruction &DestInst);
132 
133 public:
RISCVCompressInstEmitter(RecordKeeper & R)134   RISCVCompressInstEmitter(RecordKeeper &R) : Records(R), Target(R) {}
135 
136   void run(raw_ostream &o);
137 };
138 } // End anonymous namespace.
139 
validateRegister(Record * Reg,Record * RegClass)140 bool RISCVCompressInstEmitter::validateRegister(Record *Reg, Record *RegClass) {
141   assert(Reg->isSubClassOf("Register") && "Reg record should be a Register\n");
142   assert(RegClass->isSubClassOf("RegisterClass") && "RegClass record should be"
143                                                     " a RegisterClass\n");
144   const CodeGenRegisterClass &RC = Target.getRegisterClass(RegClass);
145   const CodeGenRegister *R = Target.getRegisterByName(Reg->getName().lower());
146   assert((R != nullptr) &&
147          ("Register" + Reg->getName().str() + " not defined!!\n").c_str());
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 (unsigned i = 0; i < Inst.Operands.size(); i++)
277     if (Inst.Operands[i].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   DefInit *Type1 = dyn_cast<DefInit>(Arg1);
289   DefInit *Type2 = dyn_cast<DefInit>(Arg2);
290   assert(Type1 && ("Arg1 type not found\n"));
291   assert(Type2 && ("Arg2 type not found\n"));
292   return Type1->getDef() == Type2->getDef();
293 }
294 
295 // Creates a mapping between the operand name in the Dag (e.g. $rs1) and
296 // its index in the list of Dag operands and checks that operands with the same
297 // name have the same types. For example in 'C_ADD $rs1, $rs2' we generate the
298 // mapping $rs1 --> 0, $rs2 ---> 1. If the operand appears twice in the (tied)
299 // 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)300 void RISCVCompressInstEmitter::createDagOperandMapping(
301     Record *Rec, StringMap<unsigned> &SourceOperands,
302     StringMap<unsigned> &DestOperands, DagInit *SourceDag, DagInit *DestDag,
303     IndexedMap<OpData> &SourceOperandMap) {
304   for (unsigned i = 0; i < DestDag->getNumArgs(); ++i) {
305     // Skip fixed immediates and registers, they were handled in
306     // addDagOperandMapping.
307     if ("" == DestDag->getArgNameStr(i))
308       continue;
309     DestOperands[DestDag->getArgNameStr(i)] = i;
310   }
311 
312   for (unsigned i = 0; i < SourceDag->getNumArgs(); ++i) {
313     // Skip fixed immediates and registers, they were handled in
314     // addDagOperandMapping.
315     if ("" == SourceDag->getArgNameStr(i))
316       continue;
317 
318     StringMap<unsigned>::iterator it =
319         SourceOperands.find(SourceDag->getArgNameStr(i));
320     if (it != SourceOperands.end()) {
321       // Operand sharing the same name in the Dag should be mapped as tied.
322       SourceOperandMap[i].TiedOpIdx = it->getValue();
323       if (!validateArgsTypes(SourceDag->getArg(it->getValue()),
324                              SourceDag->getArg(i)))
325         PrintFatalError(Rec->getLoc(),
326                         "Input Operand '" + SourceDag->getArgNameStr(i) +
327                             "' has a mismatched tied operand!\n");
328     }
329     it = DestOperands.find(SourceDag->getArgNameStr(i));
330     if (it == DestOperands.end())
331       PrintFatalError(Rec->getLoc(), "Operand " + SourceDag->getArgNameStr(i) +
332                                          " defined in Input Dag but not used in"
333                                          " Output Dag!\n");
334     // Input Dag operand types must match output Dag operand type.
335     if (!validateArgsTypes(DestDag->getArg(it->getValue()),
336                            SourceDag->getArg(i)))
337       PrintFatalError(Rec->getLoc(), "Type mismatch between Input and "
338                                      "Output Dag operand '" +
339                                          SourceDag->getArgNameStr(i) + "'!");
340     SourceOperands[SourceDag->getArgNameStr(i)] = i;
341   }
342 }
343 
344 /// Map operand names in the Dag to their index in both corresponding input and
345 /// output instructions. Validate that operands defined in the input are
346 /// 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)347 void RISCVCompressInstEmitter::createInstOperandMapping(
348     Record *Rec, DagInit *SourceDag, DagInit *DestDag,
349     IndexedMap<OpData> &SourceOperandMap, IndexedMap<OpData> &DestOperandMap,
350     StringMap<unsigned> &SourceOperands, CodeGenInstruction &DestInst) {
351   // TiedCount keeps track of the number of operands skipped in Inst
352   // operands list to get to the corresponding Dag operand.
353   unsigned TiedCount = 0;
354   LLVM_DEBUG(dbgs() << "  Operand mapping:\n  Source   Dest\n");
355   for (unsigned i = 0, e = DestInst.Operands.size(); i != e; ++i) {
356     int TiedInstOpIdx = DestInst.Operands[i].getTiedRegister();
357     if (TiedInstOpIdx != -1) {
358       ++TiedCount;
359       DestOperandMap[i].Data = DestOperandMap[TiedInstOpIdx].Data;
360       DestOperandMap[i].Kind = DestOperandMap[TiedInstOpIdx].Kind;
361       if (DestOperandMap[i].Kind == OpData::Operand)
362         // No need to fill the SourceOperandMap here since it was mapped to
363         // destination operand 'TiedInstOpIdx' in a previous iteration.
364         LLVM_DEBUG(dbgs() << "    " << DestOperandMap[i].Data.Operand
365                           << " ====> " << i
366                           << "  Dest operand tied with operand '"
367                           << TiedInstOpIdx << "'\n");
368       continue;
369     }
370     // Skip fixed immediates and registers, they were handled in
371     // addDagOperandMapping.
372     if (DestOperandMap[i].Kind != OpData::Operand)
373       continue;
374 
375     unsigned DagArgIdx = i - TiedCount;
376     StringMap<unsigned>::iterator SourceOp =
377         SourceOperands.find(DestDag->getArgNameStr(DagArgIdx));
378     if (SourceOp == SourceOperands.end())
379       PrintFatalError(Rec->getLoc(),
380                       "Output Dag operand '" +
381                           DestDag->getArgNameStr(DagArgIdx) +
382                           "' has no matching input Dag operand.");
383 
384     assert(DestDag->getArgNameStr(DagArgIdx) ==
385                SourceDag->getArgNameStr(SourceOp->getValue()) &&
386            "Incorrect operand mapping detected!\n");
387     DestOperandMap[i].Data.Operand = SourceOp->getValue();
388     SourceOperandMap[SourceOp->getValue()].Data.Operand = i;
389     LLVM_DEBUG(dbgs() << "    " << SourceOp->getValue() << " ====> " << i
390                       << "\n");
391   }
392 }
393 
394 /// Validates the CompressPattern and create operand mapping.
395 /// These are the checks to validate a CompressPat pattern declarations.
396 /// Error out with message under these conditions:
397 /// - Dag Input opcode is an expanded instruction and Dag Output opcode is a
398 ///   compressed instruction.
399 /// - Operands in Dag Input must be all used in Dag Output.
400 ///   Register Operand type in Dag Input Type  must be contained in the
401 ///   corresponding Source Instruction type.
402 /// - Register Operand type in Dag Input must be the  same as in  Dag Ouput.
403 /// - Register Operand type in  Dag Output must be the same  as the
404 ///   corresponding Destination Inst type.
405 /// - Immediate Operand type in Dag Input must be the same as in Dag Ouput.
406 /// - Immediate Operand type in Dag Ouput must be the same as the corresponding
407 ///   Destination Instruction type.
408 /// - Fixed register must be contained in the corresponding Source Instruction
409 ///   type.
410 /// - Fixed register must be contained in the corresponding Destination
411 ///   Instruction type. Warning message printed under these conditions:
412 /// - Fixed immediate in Dag Input or Dag Ouput cannot be checked at this time
413 ///   and generate warning.
414 /// - Immediate operand type in Dag Input differs from the corresponding Source
415 ///   Instruction type  and generate a warning.
evaluateCompressPat(Record * Rec)416 void RISCVCompressInstEmitter::evaluateCompressPat(Record *Rec) {
417   // Validate input Dag operands.
418   DagInit *SourceDag = Rec->getValueAsDag("Input");
419   assert(SourceDag && "Missing 'Input' in compress pattern!");
420   LLVM_DEBUG(dbgs() << "Input: " << *SourceDag << "\n");
421 
422   // Checking we are transforming from compressed to uncompressed instructions.
423   Record *Operator = SourceDag->getOperatorAsDef(Rec->getLoc());
424   if (!Operator->isSubClassOf("RVInst"))
425     PrintFatalError(Rec->getLoc(), "Input instruction '" + Operator->getName() +
426                                        "' is not a 32 bit wide instruction!");
427   CodeGenInstruction SourceInst(Operator);
428   verifyDagOpCount(SourceInst, SourceDag, true);
429 
430   // Validate output Dag operands.
431   DagInit *DestDag = Rec->getValueAsDag("Output");
432   assert(DestDag && "Missing 'Output' in compress pattern!");
433   LLVM_DEBUG(dbgs() << "Output: " << *DestDag << "\n");
434 
435   Record *DestOperator = DestDag->getOperatorAsDef(Rec->getLoc());
436   if (!DestOperator->isSubClassOf("RVInst16"))
437     PrintFatalError(Rec->getLoc(), "Output instruction  '" +
438                                        DestOperator->getName() +
439                                        "' is not a 16 bit wide instruction!");
440   CodeGenInstruction DestInst(DestOperator);
441   verifyDagOpCount(DestInst, DestDag, false);
442 
443   // Fill the mapping from the source to destination instructions.
444 
445   IndexedMap<OpData> SourceOperandMap;
446   SourceOperandMap.grow(SourceInst.Operands.size());
447   // Create a mapping between source Dag operands and source Inst operands.
448   addDagOperandMapping(Rec, SourceDag, SourceInst, SourceOperandMap,
449                        /*IsSourceInst*/ true);
450 
451   IndexedMap<OpData> DestOperandMap;
452   DestOperandMap.grow(DestInst.Operands.size());
453   // Create a mapping between destination Dag operands and destination Inst
454   // operands.
455   addDagOperandMapping(Rec, DestDag, DestInst, DestOperandMap,
456                        /*IsSourceInst*/ false);
457 
458   StringMap<unsigned> SourceOperands;
459   StringMap<unsigned> DestOperands;
460   createDagOperandMapping(Rec, SourceOperands, DestOperands, SourceDag, DestDag,
461                           SourceOperandMap);
462   // Create operand mapping between the source and destination instructions.
463   createInstOperandMapping(Rec, SourceDag, DestDag, SourceOperandMap,
464                            DestOperandMap, SourceOperands, DestInst);
465 
466   // Get the target features for the CompressPat.
467   std::vector<Record *> PatReqFeatures;
468   std::vector<Record *> RF = Rec->getValueAsListOfDefs("Predicates");
469   copy_if(RF, std::back_inserter(PatReqFeatures), [](Record *R) {
470     return R->getValueAsBit("AssemblerMatcherPredicate");
471   });
472 
473   CompressPatterns.push_back(CompressPat(SourceInst, DestInst, PatReqFeatures,
474                                          SourceOperandMap, DestOperandMap));
475 }
476 
477 static void
getReqFeatures(std::set<std::pair<bool,StringRef>> & FeaturesSet,std::set<std::set<std::pair<bool,StringRef>>> & AnyOfFeatureSets,const std::vector<Record * > & ReqFeatures)478 getReqFeatures(std::set<std::pair<bool, StringRef>> &FeaturesSet,
479                std::set<std::set<std::pair<bool, StringRef>>> &AnyOfFeatureSets,
480                const std::vector<Record *> &ReqFeatures) {
481   for (auto &R : ReqFeatures) {
482     const DagInit *D = R->getValueAsDag("AssemblerCondDag");
483     std::string CombineType = D->getOperator()->getAsString();
484     if (CombineType != "any_of" && CombineType != "all_of")
485       PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
486     if (D->getNumArgs() == 0)
487       PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
488     bool IsOr = CombineType == "any_of";
489     std::set<std::pair<bool, StringRef>> AnyOfSet;
490 
491     for (auto *Arg : D->getArgs()) {
492       bool IsNot = false;
493       if (auto *NotArg = dyn_cast<DagInit>(Arg)) {
494         if (NotArg->getOperator()->getAsString() != "not" ||
495             NotArg->getNumArgs() != 1)
496           PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
497         Arg = NotArg->getArg(0);
498         IsNot = true;
499       }
500       if (!isa<DefInit>(Arg) ||
501           !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature"))
502         PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!");
503       if (IsOr)
504         AnyOfSet.insert({IsNot, cast<DefInit>(Arg)->getDef()->getName()});
505       else
506         FeaturesSet.insert({IsNot, cast<DefInit>(Arg)->getDef()->getName()});
507     }
508 
509     if (IsOr)
510       AnyOfFeatureSets.insert(AnyOfSet);
511   }
512 }
513 
getPredicates(DenseMap<const Record *,unsigned> & PredicateMap,std::vector<const Record * > & Predicates,Record * Rec,StringRef Name)514 static unsigned getPredicates(DenseMap<const Record *, unsigned> &PredicateMap,
515                               std::vector<const Record *> &Predicates,
516                               Record *Rec, StringRef Name) {
517   unsigned Entry = PredicateMap[Rec];
518   if (Entry)
519     return Entry;
520 
521   if (!Rec->isValueUnset(Name)) {
522     Predicates.push_back(Rec);
523     Entry = Predicates.size();
524     PredicateMap[Rec] = Entry;
525     return Entry;
526   }
527 
528   PrintFatalError(Rec->getLoc(), "No " + Name +
529     " predicate on this operand at all: '" + Rec->getName().str() + "'");
530   return 0;
531 }
532 
printPredicates(std::vector<const Record * > & Predicates,StringRef Name,raw_ostream & o)533 static void printPredicates(std::vector<const Record *> &Predicates,
534                             StringRef Name, raw_ostream &o) {
535   for (unsigned i = 0; i < Predicates.size(); ++i) {
536     StringRef Pred = Predicates[i]->getValueAsString(Name);
537     o << "  case " << i + 1 << ": {\n"
538       << "  // " << Predicates[i]->getName().str() << "\n"
539       << "  " << Pred.data() << "\n"
540       << "  }\n";
541   }
542 }
543 
mergeCondAndCode(raw_string_ostream & CondStream,raw_string_ostream & CodeStream)544 static std::string mergeCondAndCode(raw_string_ostream &CondStream,
545                                     raw_string_ostream &CodeStream) {
546   std::string S;
547   raw_string_ostream CombinedStream(S);
548   CombinedStream.indent(4)
549       << "if ("
550       << CondStream.str().substr(
551              6, CondStream.str().length() -
552                     10) // remove first indentation and last '&&'.
553       << ") {\n";
554   CombinedStream << CodeStream.str();
555   CombinedStream.indent(4) << "  return true;\n";
556   CombinedStream.indent(4) << "} // if\n";
557   return CombinedStream.str();
558 }
559 
emitCompressInstEmitter(raw_ostream & o,EmitterType EType)560 void RISCVCompressInstEmitter::emitCompressInstEmitter(raw_ostream &o,
561                                                        EmitterType EType) {
562   Record *AsmWriter = Target.getAsmWriter();
563   if (!AsmWriter->getValueAsInt("PassSubtarget"))
564     PrintFatalError(AsmWriter->getLoc(),
565                     "'PassSubtarget' is false. SubTargetInfo object is needed "
566                     "for target features.\n");
567 
568   std::string Namespace = std::string(Target.getName());
569 
570   // Sort entries in CompressPatterns to handle instructions that can have more
571   // than one candidate for compression\uncompression, e.g ADD can be
572   // transformed to a C_ADD or a C_MV. When emitting 'uncompress()' function the
573   // source and destination are flipped and the sort key needs to change
574   // accordingly.
575   llvm::stable_sort(CompressPatterns,
576                     [EType](const CompressPat &LHS, const CompressPat &RHS) {
577                       if (EType == EmitterType::Compress ||
578                         EType == EmitterType::CheckCompress)
579                         return (LHS.Source.TheDef->getName().str() <
580                                 RHS.Source.TheDef->getName().str());
581                       else
582                         return (LHS.Dest.TheDef->getName().str() <
583                                 RHS.Dest.TheDef->getName().str());
584                     });
585 
586   // A list of MCOperandPredicates for all operands in use, and the reverse map.
587   std::vector<const Record *> MCOpPredicates;
588   DenseMap<const Record *, unsigned> MCOpPredicateMap;
589   // A list of ImmLeaf Predicates for all operands in use, and the reverse map.
590   std::vector<const Record *> ImmLeafPredicates;
591   DenseMap<const Record *, unsigned> ImmLeafPredicateMap;
592 
593   std::string F;
594   std::string FH;
595   raw_string_ostream Func(F);
596   raw_string_ostream FuncH(FH);
597   bool NeedMRI = false;
598 
599   if (EType == EmitterType::Compress)
600     o << "\n#ifdef GEN_COMPRESS_INSTR\n"
601       << "#undef GEN_COMPRESS_INSTR\n\n";
602   else if (EType == EmitterType::Uncompress)
603     o << "\n#ifdef GEN_UNCOMPRESS_INSTR\n"
604       << "#undef GEN_UNCOMPRESS_INSTR\n\n";
605   else if (EType == EmitterType::CheckCompress)
606     o << "\n#ifdef GEN_CHECK_COMPRESS_INSTR\n"
607       << "#undef GEN_CHECK_COMPRESS_INSTR\n\n";
608 
609   if (EType == EmitterType::Compress) {
610     FuncH << "static bool compressInst(MCInst &OutInst,\n";
611     FuncH.indent(25) << "const MCInst &MI,\n";
612     FuncH.indent(25) << "const MCSubtargetInfo &STI,\n";
613     FuncH.indent(25) << "MCContext &Context) {\n";
614   } else if (EType == EmitterType::Uncompress){
615     FuncH << "static bool uncompressInst(MCInst &OutInst,\n";
616     FuncH.indent(27) << "const MCInst &MI,\n";
617     FuncH.indent(27) << "const MCRegisterInfo &MRI,\n";
618     FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n";
619   } else if (EType == EmitterType::CheckCompress) {
620     FuncH << "static bool isCompressibleInst(const MachineInstr &MI,\n";
621     FuncH.indent(27) << "const RISCVSubtarget *Subtarget,\n";
622     FuncH.indent(27) << "const MCRegisterInfo &MRI,\n";
623     FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n";
624   }
625 
626   if (CompressPatterns.empty()) {
627     o << FuncH.str();
628     o.indent(2) << "return false;\n}\n";
629     if (EType == EmitterType::Compress)
630       o << "\n#endif //GEN_COMPRESS_INSTR\n";
631     else if (EType == EmitterType::Uncompress)
632       o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n";
633     else if (EType == EmitterType::CheckCompress)
634       o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n";
635     return;
636   }
637 
638   std::string CaseString("");
639   raw_string_ostream CaseStream(CaseString);
640   std::string PrevOp("");
641   std::string CurOp("");
642   CaseStream << "  switch (MI.getOpcode()) {\n";
643   CaseStream << "    default: return false;\n";
644 
645   bool CompressOrCheck =
646     EType == EmitterType::Compress || EType == EmitterType::CheckCompress;
647   bool CompressOrUncompress =
648     EType == EmitterType::Compress || EType == EmitterType::Uncompress;
649 
650   for (auto &CompressPat : CompressPatterns) {
651     std::string CondString;
652     std::string CodeString;
653     raw_string_ostream CondStream(CondString);
654     raw_string_ostream CodeStream(CodeString);
655     CodeGenInstruction &Source =
656         CompressOrCheck ?  CompressPat.Source : CompressPat.Dest;
657     CodeGenInstruction &Dest =
658         CompressOrCheck ? CompressPat.Dest : CompressPat.Source;
659     IndexedMap<OpData> SourceOperandMap = CompressOrCheck ?
660       CompressPat.SourceOperandMap : CompressPat.DestOperandMap;
661     IndexedMap<OpData> &DestOperandMap = CompressOrCheck ?
662       CompressPat.DestOperandMap : CompressPat.SourceOperandMap;
663 
664     CurOp = Source.TheDef->getName().str();
665     // Check current and previous opcode to decide to continue or end a case.
666     if (CurOp != PrevOp) {
667       if (PrevOp != "")
668         CaseStream.indent(6) << "break;\n    } // case " + PrevOp + "\n";
669       CaseStream.indent(4) << "case " + Namespace + "::" + CurOp + ": {\n";
670     }
671 
672     std::set<std::pair<bool, StringRef>> FeaturesSet;
673     std::set<std::set<std::pair<bool, StringRef>>> AnyOfFeatureSets;
674     // Add CompressPat required features.
675     getReqFeatures(FeaturesSet, AnyOfFeatureSets, CompressPat.PatReqFeatures);
676 
677     // Add Dest instruction required features.
678     std::vector<Record *> ReqFeatures;
679     std::vector<Record *> RF = Dest.TheDef->getValueAsListOfDefs("Predicates");
680     copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) {
681       return R->getValueAsBit("AssemblerMatcherPredicate");
682     });
683     getReqFeatures(FeaturesSet, AnyOfFeatureSets, ReqFeatures);
684 
685     // Emit checks for all required features.
686     for (auto &Op : FeaturesSet) {
687       StringRef Not = Op.first ? "!" : "";
688       CondStream.indent(6)
689           << Not << ("STI.getFeatureBits()[" + Namespace + "::" + Op.second + "]").str() +
690                   " &&\n";
691     }
692 
693     // Emit checks for all required feature groups.
694     for (auto &Set : AnyOfFeatureSets) {
695       CondStream.indent(6) << "(";
696       for (auto &Op : Set) {
697         bool isLast = &Op == &*Set.rbegin();
698         StringRef Not = Op.first ? "!" : "";
699         CondStream << Not << ("STI.getFeatureBits()[" + Namespace + "::" + Op.second +
700                           "]").str();
701         if (!isLast)
702           CondStream << " || ";
703       }
704       CondStream << ") &&\n";
705     }
706 
707     // Start Source Inst operands validation.
708     unsigned OpNo = 0;
709     for (OpNo = 0; OpNo < Source.Operands.size(); ++OpNo) {
710       if (SourceOperandMap[OpNo].TiedOpIdx != -1) {
711         if (Source.Operands[OpNo].Rec->isSubClassOf("RegisterClass"))
712           CondStream.indent(6)
713               << "(MI.getOperand("
714               << std::to_string(OpNo) + ").getReg() ==  MI.getOperand("
715               << std::to_string(SourceOperandMap[OpNo].TiedOpIdx)
716               << ").getReg()) &&\n";
717         else
718           PrintFatalError("Unexpected tied operand types!\n");
719       }
720       // Check for fixed immediates\registers in the source instruction.
721       switch (SourceOperandMap[OpNo].Kind) {
722       case OpData::Operand:
723         // We don't need to do anything for source instruction operand checks.
724         break;
725       case OpData::Imm:
726         CondStream.indent(6)
727             << "(MI.getOperand(" + std::to_string(OpNo) + ").isImm()) &&\n" +
728                    "      (MI.getOperand(" + std::to_string(OpNo) +
729                    ").getImm() == " +
730                    std::to_string(SourceOperandMap[OpNo].Data.Imm) + ") &&\n";
731         break;
732       case OpData::Reg: {
733         Record *Reg = SourceOperandMap[OpNo].Data.Reg;
734         CondStream.indent(6) << "(MI.getOperand(" + std::to_string(OpNo) +
735                                     ").getReg() == " + Namespace +
736                                     "::" + Reg->getName().str() + ") &&\n";
737         break;
738       }
739       }
740     }
741     CodeStream.indent(6) << "// " + Dest.AsmString + "\n";
742     if (CompressOrUncompress)
743       CodeStream.indent(6) << "OutInst.setOpcode(" + Namespace +
744                                 "::" + Dest.TheDef->getName().str() + ");\n";
745     OpNo = 0;
746     for (const auto &DestOperand : Dest.Operands) {
747       CodeStream.indent(6) << "// Operand: " + DestOperand.Name + "\n";
748       switch (DestOperandMap[OpNo].Kind) {
749       case OpData::Operand: {
750         unsigned OpIdx = DestOperandMap[OpNo].Data.Operand;
751         // Check that the operand in the Source instruction fits
752         // the type for the Dest instruction.
753         if (DestOperand.Rec->isSubClassOf("RegisterClass")) {
754           NeedMRI = true;
755           // This is a register operand. Check the register class.
756           // Don't check register class if this is a tied operand, it was done
757           // for the operand its tied to.
758           if (DestOperand.getTiedRegister() == -1)
759             CondStream.indent(6)
760                 << "(MRI.getRegClass(" + Namespace +
761                        "::" + DestOperand.Rec->getName().str() +
762                        "RegClassID).contains(" + "MI.getOperand(" +
763                        std::to_string(OpIdx) + ").getReg())) &&\n";
764 
765           if (CompressOrUncompress)
766             CodeStream.indent(6) << "OutInst.addOperand(MI.getOperand(" +
767                                         std::to_string(OpIdx) + "));\n";
768         } else {
769           // Handling immediate operands.
770           if (CompressOrUncompress) {
771             unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates,
772               DestOperand.Rec, StringRef("MCOperandPredicate"));
773             CondStream.indent(6) << Namespace + "ValidateMCOperand(" +
774                                         "MI.getOperand(" + std::to_string(OpIdx) +
775                                         "), STI, " + std::to_string(Entry) +
776                                         ") &&\n";
777           } else {
778             unsigned Entry = getPredicates(ImmLeafPredicateMap, ImmLeafPredicates,
779               DestOperand.Rec, StringRef("ImmediateCode"));
780             CondStream.indent(6) << "MI.getOperand(" + std::to_string(OpIdx) +
781                                     ").isImm() &&\n";
782             CondStream.indent(6) << Namespace + "ValidateMachineOperand(" +
783                                         "MI.getOperand(" + std::to_string(OpIdx) +
784                                         "), Subtarget, " + std::to_string(Entry) +
785                                         ") &&\n";
786           }
787           if (CompressOrUncompress)
788             CodeStream.indent(6) << "OutInst.addOperand(MI.getOperand(" +
789                                         std::to_string(OpIdx) + "));\n";
790         }
791         break;
792       }
793       case OpData::Imm: {
794         if (CompressOrUncompress) {
795           unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates,
796             DestOperand.Rec, StringRef("MCOperandPredicate"));
797           CondStream.indent(6)
798               << Namespace + "ValidateMCOperand(" + "MCOperand::createImm(" +
799                      std::to_string(DestOperandMap[OpNo].Data.Imm) + "), STI, " +
800                      std::to_string(Entry) + ") &&\n";
801         } else {
802           unsigned Entry = getPredicates(ImmLeafPredicateMap, ImmLeafPredicates,
803             DestOperand.Rec, StringRef("ImmediateCode"));
804           CondStream.indent(6)
805               << Namespace + "ValidateMachineOperand(" + "MachineOperand::CreateImm(" +
806                      std::to_string(DestOperandMap[OpNo].Data.Imm) + "), SubTarget, " +
807                      std::to_string(Entry) + ") &&\n";
808         }
809         if (CompressOrUncompress)
810           CodeStream.indent(6)
811               << "OutInst.addOperand(MCOperand::createImm(" +
812                      std::to_string(DestOperandMap[OpNo].Data.Imm) + "));\n";
813       } break;
814       case OpData::Reg: {
815         if (CompressOrUncompress) {
816           // Fixed register has been validated at pattern validation time.
817           Record *Reg = DestOperandMap[OpNo].Data.Reg;
818           CodeStream.indent(6) << "OutInst.addOperand(MCOperand::createReg(" +
819                                       Namespace + "::" + Reg->getName().str() +
820                                       "));\n";
821         }
822       } break;
823       }
824       ++OpNo;
825     }
826     if (CompressOrUncompress)
827       CodeStream.indent(6) << "OutInst.setLoc(MI.getLoc());\n";
828     CaseStream << mergeCondAndCode(CondStream, CodeStream);
829     PrevOp = CurOp;
830   }
831   Func << CaseStream.str() << "\n";
832   // Close brace for the last case.
833   Func.indent(4) << "} // case " + CurOp + "\n";
834   Func.indent(2) << "} // switch\n";
835   Func.indent(2) << "return false;\n}\n";
836 
837   if (!MCOpPredicates.empty()) {
838     o << "static bool " << Namespace
839       << "ValidateMCOperand(const MCOperand &MCOp,\n"
840       << "                  const MCSubtargetInfo &STI,\n"
841       << "                  unsigned PredicateIndex) {\n"
842       << "  switch (PredicateIndex) {\n"
843       << "  default:\n"
844       << "    llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
845       << "    break;\n";
846 
847     printPredicates(MCOpPredicates, "MCOperandPredicate", o);
848 
849     o << "  }\n"
850       << "}\n\n";
851   }
852 
853   if (!ImmLeafPredicates.empty()) {
854     o << "static bool " << Namespace
855       << "ValidateMachineOperand(const MachineOperand &MO,\n"
856       << "                  const RISCVSubtarget *Subtarget,\n"
857       << "                  unsigned PredicateIndex) {\n"
858       << "  int64_t Imm = MO.getImm();\n"
859       << "  switch (PredicateIndex) {\n"
860       << "  default:\n"
861       << "    llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n"
862       << "    break;\n";
863 
864     printPredicates(ImmLeafPredicates, "ImmediateCode", o);
865 
866     o << "  }\n"
867       << "}\n\n";
868   }
869 
870   o << FuncH.str();
871   if (NeedMRI && EType == EmitterType::Compress)
872     o.indent(2) << "const MCRegisterInfo &MRI = *Context.getRegisterInfo();\n";
873   o << Func.str();
874 
875   if (EType == EmitterType::Compress)
876     o << "\n#endif //GEN_COMPRESS_INSTR\n";
877   else if (EType == EmitterType::Uncompress)
878     o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n";
879   else if (EType == EmitterType::CheckCompress)
880     o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n";
881 }
882 
run(raw_ostream & o)883 void RISCVCompressInstEmitter::run(raw_ostream &o) {
884   std::vector<Record *> Insts = Records.getAllDerivedDefinitions("CompressPat");
885 
886   // Process the CompressPat definitions, validating them as we do so.
887   for (unsigned i = 0, e = Insts.size(); i != e; ++i)
888     evaluateCompressPat(Insts[i]);
889 
890   // Emit file header.
891   emitSourceFileHeader("Compress instruction Source Fragment", o);
892   // Generate compressInst() function.
893   emitCompressInstEmitter(o, EmitterType::Compress);
894   // Generate uncompressInst() function.
895   emitCompressInstEmitter(o, EmitterType::Uncompress);
896   // Generate isCompressibleInst() function.
897   emitCompressInstEmitter(o, EmitterType::CheckCompress);
898 }
899 
900 namespace llvm {
901 
EmitCompressInst(RecordKeeper & RK,raw_ostream & OS)902 void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS) {
903   RISCVCompressInstEmitter(RK).run(OS);
904 }
905 
906 } // namespace llvm
907