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