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 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: 134 RISCVCompressInstEmitter(RecordKeeper &R) : Records(R), Target(R) {} 135 136 void run(raw_ostream &o); 137 }; 138 } // End anonymous namespace. 139 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 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. 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. 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 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. 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. 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. 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 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 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 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 Init *Pred = Predicates[i]->getValueInit(Name); 537 if (CodeInit *SI = dyn_cast<CodeInit>(Pred)) 538 o << " case " << i + 1 << ": {\n" 539 << " // " << Predicates[i]->getName().str() << "\n" 540 << " " << SI->getValue() << "\n" 541 << " }\n"; 542 else 543 llvm_unreachable("Unexpected predicate field!"); 544 } 545 } 546 547 static std::string mergeCondAndCode(raw_string_ostream &CondStream, 548 raw_string_ostream &CodeStream) { 549 std::string S; 550 raw_string_ostream CombinedStream(S); 551 CombinedStream.indent(4) 552 << "if (" 553 << CondStream.str().substr( 554 6, CondStream.str().length() - 555 10) // remove first indentation and last '&&'. 556 << ") {\n"; 557 CombinedStream << CodeStream.str(); 558 CombinedStream.indent(4) << " return true;\n"; 559 CombinedStream.indent(4) << "} // if\n"; 560 return CombinedStream.str(); 561 } 562 563 void RISCVCompressInstEmitter::emitCompressInstEmitter(raw_ostream &o, 564 EmitterType EType) { 565 Record *AsmWriter = Target.getAsmWriter(); 566 if (!AsmWriter->getValueAsInt("PassSubtarget")) 567 PrintFatalError(AsmWriter->getLoc(), 568 "'PassSubtarget' is false. SubTargetInfo object is needed " 569 "for target features.\n"); 570 571 std::string Namespace = std::string(Target.getName()); 572 573 // Sort entries in CompressPatterns to handle instructions that can have more 574 // than one candidate for compression\uncompression, e.g ADD can be 575 // transformed to a C_ADD or a C_MV. When emitting 'uncompress()' function the 576 // source and destination are flipped and the sort key needs to change 577 // accordingly. 578 llvm::stable_sort(CompressPatterns, 579 [EType](const CompressPat &LHS, const CompressPat &RHS) { 580 if (EType == EmitterType::Compress || 581 EType == EmitterType::CheckCompress) 582 return (LHS.Source.TheDef->getName().str() < 583 RHS.Source.TheDef->getName().str()); 584 else 585 return (LHS.Dest.TheDef->getName().str() < 586 RHS.Dest.TheDef->getName().str()); 587 }); 588 589 // A list of MCOperandPredicates for all operands in use, and the reverse map. 590 std::vector<const Record *> MCOpPredicates; 591 DenseMap<const Record *, unsigned> MCOpPredicateMap; 592 // A list of ImmLeaf Predicates for all operands in use, and the reverse map. 593 std::vector<const Record *> ImmLeafPredicates; 594 DenseMap<const Record *, unsigned> ImmLeafPredicateMap; 595 596 std::string F; 597 std::string FH; 598 raw_string_ostream Func(F); 599 raw_string_ostream FuncH(FH); 600 bool NeedMRI = false; 601 602 if (EType == EmitterType::Compress) 603 o << "\n#ifdef GEN_COMPRESS_INSTR\n" 604 << "#undef GEN_COMPRESS_INSTR\n\n"; 605 else if (EType == EmitterType::Uncompress) 606 o << "\n#ifdef GEN_UNCOMPRESS_INSTR\n" 607 << "#undef GEN_UNCOMPRESS_INSTR\n\n"; 608 else if (EType == EmitterType::CheckCompress) 609 o << "\n#ifdef GEN_CHECK_COMPRESS_INSTR\n" 610 << "#undef GEN_CHECK_COMPRESS_INSTR\n\n"; 611 612 if (EType == EmitterType::Compress) { 613 FuncH << "static bool compressInst(MCInst& OutInst,\n"; 614 FuncH.indent(25) << "const MCInst &MI,\n"; 615 FuncH.indent(25) << "const MCSubtargetInfo &STI,\n"; 616 FuncH.indent(25) << "MCContext &Context) {\n"; 617 } else if (EType == EmitterType::Uncompress){ 618 FuncH << "static bool uncompressInst(MCInst& OutInst,\n"; 619 FuncH.indent(27) << "const MCInst &MI,\n"; 620 FuncH.indent(27) << "const MCRegisterInfo &MRI,\n"; 621 FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n"; 622 } else if (EType == EmitterType::CheckCompress) { 623 FuncH << "static bool isCompressibleInst(const MachineInstr& MI,\n"; 624 FuncH.indent(27) << "const RISCVSubtarget *Subtarget,\n"; 625 FuncH.indent(27) << "const MCRegisterInfo &MRI,\n"; 626 FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n"; 627 } 628 629 if (CompressPatterns.empty()) { 630 o << FuncH.str(); 631 o.indent(2) << "return false;\n}\n"; 632 if (EType == EmitterType::Compress) 633 o << "\n#endif //GEN_COMPRESS_INSTR\n"; 634 else if (EType == EmitterType::Uncompress) 635 o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n"; 636 else if (EType == EmitterType::CheckCompress) 637 o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n"; 638 return; 639 } 640 641 std::string CaseString(""); 642 raw_string_ostream CaseStream(CaseString); 643 std::string PrevOp(""); 644 std::string CurOp(""); 645 CaseStream << " switch (MI.getOpcode()) {\n"; 646 CaseStream << " default: return false;\n"; 647 648 bool CompressOrCheck = 649 EType == EmitterType::Compress || EType == EmitterType::CheckCompress; 650 bool CompressOrUncompress = 651 EType == EmitterType::Compress || EType == EmitterType::Uncompress; 652 653 for (auto &CompressPat : CompressPatterns) { 654 std::string CondString; 655 std::string CodeString; 656 raw_string_ostream CondStream(CondString); 657 raw_string_ostream CodeStream(CodeString); 658 CodeGenInstruction &Source = 659 CompressOrCheck ? CompressPat.Source : CompressPat.Dest; 660 CodeGenInstruction &Dest = 661 CompressOrCheck ? CompressPat.Dest : CompressPat.Source; 662 IndexedMap<OpData> SourceOperandMap = CompressOrCheck ? 663 CompressPat.SourceOperandMap : CompressPat.DestOperandMap; 664 IndexedMap<OpData> &DestOperandMap = CompressOrCheck ? 665 CompressPat.DestOperandMap : CompressPat.SourceOperandMap; 666 667 CurOp = Source.TheDef->getName().str(); 668 // Check current and previous opcode to decide to continue or end a case. 669 if (CurOp != PrevOp) { 670 if (PrevOp != "") 671 CaseStream.indent(6) << "break;\n } // case " + PrevOp + "\n"; 672 CaseStream.indent(4) << "case " + Namespace + "::" + CurOp + ": {\n"; 673 } 674 675 std::set<std::pair<bool, StringRef>> FeaturesSet; 676 std::set<std::set<std::pair<bool, StringRef>>> AnyOfFeatureSets; 677 // Add CompressPat required features. 678 getReqFeatures(FeaturesSet, AnyOfFeatureSets, CompressPat.PatReqFeatures); 679 680 // Add Dest instruction required features. 681 std::vector<Record *> ReqFeatures; 682 std::vector<Record *> RF = Dest.TheDef->getValueAsListOfDefs("Predicates"); 683 copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) { 684 return R->getValueAsBit("AssemblerMatcherPredicate"); 685 }); 686 getReqFeatures(FeaturesSet, AnyOfFeatureSets, ReqFeatures); 687 688 // Emit checks for all required features. 689 for (auto &Op : FeaturesSet) { 690 StringRef Not = Op.first ? "!" : ""; 691 CondStream.indent(6) 692 << Not << ("STI.getFeatureBits()[" + Namespace + "::" + Op.second + "]").str() + 693 " &&\n"; 694 } 695 696 // Emit checks for all required feature groups. 697 for (auto &Set : AnyOfFeatureSets) { 698 CondStream.indent(6) << "("; 699 for (auto &Op : Set) { 700 bool isLast = &Op == &*Set.rbegin(); 701 StringRef Not = Op.first ? "!" : ""; 702 CondStream << Not << ("STI.getFeatureBits()[" + Namespace + "::" + Op.second + 703 "]").str(); 704 if (!isLast) 705 CondStream << " || "; 706 } 707 CondStream << ") &&\n"; 708 } 709 710 // Start Source Inst operands validation. 711 unsigned OpNo = 0; 712 for (OpNo = 0; OpNo < Source.Operands.size(); ++OpNo) { 713 if (SourceOperandMap[OpNo].TiedOpIdx != -1) { 714 if (Source.Operands[OpNo].Rec->isSubClassOf("RegisterClass")) 715 CondStream.indent(6) 716 << "(MI.getOperand(" 717 << std::to_string(OpNo) + ").getReg() == MI.getOperand(" 718 << std::to_string(SourceOperandMap[OpNo].TiedOpIdx) 719 << ").getReg()) &&\n"; 720 else 721 PrintFatalError("Unexpected tied operand types!\n"); 722 } 723 // Check for fixed immediates\registers in the source instruction. 724 switch (SourceOperandMap[OpNo].Kind) { 725 case OpData::Operand: 726 // We don't need to do anything for source instruction operand checks. 727 break; 728 case OpData::Imm: 729 CondStream.indent(6) 730 << "(MI.getOperand(" + std::to_string(OpNo) + ").isImm()) &&\n" + 731 " (MI.getOperand(" + std::to_string(OpNo) + 732 ").getImm() == " + 733 std::to_string(SourceOperandMap[OpNo].Data.Imm) + ") &&\n"; 734 break; 735 case OpData::Reg: { 736 Record *Reg = SourceOperandMap[OpNo].Data.Reg; 737 CondStream.indent(6) << "(MI.getOperand(" + std::to_string(OpNo) + 738 ").getReg() == " + Namespace + 739 "::" + Reg->getName().str() + ") &&\n"; 740 break; 741 } 742 } 743 } 744 CodeStream.indent(6) << "// " + Dest.AsmString + "\n"; 745 if (CompressOrUncompress) 746 CodeStream.indent(6) << "OutInst.setOpcode(" + Namespace + 747 "::" + Dest.TheDef->getName().str() + ");\n"; 748 OpNo = 0; 749 for (const auto &DestOperand : Dest.Operands) { 750 CodeStream.indent(6) << "// Operand: " + DestOperand.Name + "\n"; 751 switch (DestOperandMap[OpNo].Kind) { 752 case OpData::Operand: { 753 unsigned OpIdx = DestOperandMap[OpNo].Data.Operand; 754 // Check that the operand in the Source instruction fits 755 // the type for the Dest instruction. 756 if (DestOperand.Rec->isSubClassOf("RegisterClass")) { 757 NeedMRI = true; 758 // This is a register operand. Check the register class. 759 // Don't check register class if this is a tied operand, it was done 760 // for the operand its tied to. 761 if (DestOperand.getTiedRegister() == -1) 762 CondStream.indent(6) 763 << "(MRI.getRegClass(" + Namespace + 764 "::" + DestOperand.Rec->getName().str() + 765 "RegClassID).contains(" + "MI.getOperand(" + 766 std::to_string(OpIdx) + ").getReg())) &&\n"; 767 768 if (CompressOrUncompress) 769 CodeStream.indent(6) << "OutInst.addOperand(MI.getOperand(" + 770 std::to_string(OpIdx) + "));\n"; 771 } else { 772 // Handling immediate operands. 773 if (CompressOrUncompress) { 774 unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates, 775 DestOperand.Rec, StringRef("MCOperandPredicate")); 776 CondStream.indent(6) << Namespace + "ValidateMCOperand(" + 777 "MI.getOperand(" + std::to_string(OpIdx) + 778 "), STI, " + std::to_string(Entry) + 779 ") &&\n"; 780 } else { 781 unsigned Entry = getPredicates(ImmLeafPredicateMap, ImmLeafPredicates, 782 DestOperand.Rec, StringRef("ImmediateCode")); 783 CondStream.indent(6) << "MI.getOperand(" + std::to_string(OpIdx) + 784 ").isImm() && \n"; 785 CondStream.indent(6) << Namespace + "ValidateMachineOperand(" + 786 "MI.getOperand(" + std::to_string(OpIdx) + 787 "), Subtarget, " + std::to_string(Entry) + 788 ") &&\n"; 789 } 790 if (CompressOrUncompress) 791 CodeStream.indent(6) << "OutInst.addOperand(MI.getOperand(" + 792 std::to_string(OpIdx) + "));\n"; 793 } 794 break; 795 } 796 case OpData::Imm: { 797 if (CompressOrUncompress) { 798 unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates, 799 DestOperand.Rec, StringRef("MCOperandPredicate")); 800 CondStream.indent(6) 801 << Namespace + "ValidateMCOperand(" + "MCOperand::createImm(" + 802 std::to_string(DestOperandMap[OpNo].Data.Imm) + "), STI, " + 803 std::to_string(Entry) + ") &&\n"; 804 } else { 805 unsigned Entry = getPredicates(ImmLeafPredicateMap, ImmLeafPredicates, 806 DestOperand.Rec, StringRef("ImmediateCode")); 807 CondStream.indent(6) 808 << Namespace + "ValidateMachineOperand(" + "MachineOperand::CreateImm(" + 809 std::to_string(DestOperandMap[OpNo].Data.Imm) + "), SubTarget, " + 810 std::to_string(Entry) + ") &&\n"; 811 } 812 if (CompressOrUncompress) 813 CodeStream.indent(6) 814 << "OutInst.addOperand(MCOperand::createImm(" + 815 std::to_string(DestOperandMap[OpNo].Data.Imm) + "));\n"; 816 } break; 817 case OpData::Reg: { 818 if (CompressOrUncompress) { 819 // Fixed register has been validated at pattern validation time. 820 Record *Reg = DestOperandMap[OpNo].Data.Reg; 821 CodeStream.indent(6) << "OutInst.addOperand(MCOperand::createReg(" + 822 Namespace + "::" + Reg->getName().str() + 823 "));\n"; 824 } 825 } break; 826 } 827 ++OpNo; 828 } 829 if (CompressOrUncompress) 830 CodeStream.indent(6) << "OutInst.setLoc(MI.getLoc());\n"; 831 CaseStream << mergeCondAndCode(CondStream, CodeStream); 832 PrevOp = CurOp; 833 } 834 Func << CaseStream.str() << "\n"; 835 // Close brace for the last case. 836 Func.indent(4) << "} // case " + CurOp + "\n"; 837 Func.indent(2) << "} // switch\n"; 838 Func.indent(2) << "return false;\n}\n"; 839 840 if (!MCOpPredicates.empty()) { 841 o << "static bool " << Namespace 842 << "ValidateMCOperand(const MCOperand &MCOp,\n" 843 << " const MCSubtargetInfo &STI,\n" 844 << " unsigned PredicateIndex) {\n" 845 << " switch (PredicateIndex) {\n" 846 << " default:\n" 847 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n" 848 << " break;\n"; 849 850 printPredicates(MCOpPredicates, "MCOperandPredicate", o); 851 852 o << " }\n" 853 << "}\n\n"; 854 } 855 856 if (!ImmLeafPredicates.empty()) { 857 o << "static bool " << Namespace 858 << "ValidateMachineOperand(const MachineOperand &MO,\n" 859 << " const RISCVSubtarget *Subtarget,\n" 860 << " unsigned PredicateIndex) {\n" 861 << " int64_t Imm = MO.getImm(); \n" 862 << " switch (PredicateIndex) {\n" 863 << " default:\n" 864 << " llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n" 865 << " break;\n"; 866 867 printPredicates(ImmLeafPredicates, "ImmediateCode", o); 868 869 o << " }\n" 870 << "}\n\n"; 871 } 872 873 o << FuncH.str(); 874 if (NeedMRI && EType == EmitterType::Compress) 875 o.indent(2) << "const MCRegisterInfo &MRI = *Context.getRegisterInfo();\n"; 876 o << Func.str(); 877 878 if (EType == EmitterType::Compress) 879 o << "\n#endif //GEN_COMPRESS_INSTR\n"; 880 else if (EType == EmitterType::Uncompress) 881 o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n"; 882 else if (EType == EmitterType::CheckCompress) 883 o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n"; 884 } 885 886 void RISCVCompressInstEmitter::run(raw_ostream &o) { 887 Record *CompressClass = Records.getClass("CompressPat"); 888 assert(CompressClass && "Compress class definition missing!"); 889 std::vector<Record *> Insts; 890 for (const auto &D : Records.getDefs()) { 891 if (D.second->isSubClassOf(CompressClass)) 892 Insts.push_back(D.second.get()); 893 } 894 895 // Process the CompressPat definitions, validating them as we do so. 896 for (unsigned i = 0, e = Insts.size(); i != e; ++i) 897 evaluateCompressPat(Insts[i]); 898 899 // Emit file header. 900 emitSourceFileHeader("Compress instruction Source Fragment", o); 901 // Generate compressInst() function. 902 emitCompressInstEmitter(o, EmitterType::Compress); 903 // Generate uncompressInst() function. 904 emitCompressInstEmitter(o, EmitterType::Uncompress); 905 // Generate isCompressibleInst() function. 906 emitCompressInstEmitter(o, EmitterType::CheckCompress); 907 } 908 909 namespace llvm { 910 911 void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS) { 912 RISCVCompressInstEmitter(RK).run(OS); 913 } 914 915 } // namespace llvm 916