1349cc55cSDimitry Andric //===-------- CompressInstEmitter.cpp - Generator for Compression ---------===// 2349cc55cSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6349cc55cSDimitry Andric // 7349cc55cSDimitry Andric // CompressInstEmitter implements a tablegen-driven CompressPat based 8349cc55cSDimitry Andric // Instruction Compression mechanism. 9349cc55cSDimitry Andric // 10349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 11349cc55cSDimitry Andric // 12349cc55cSDimitry Andric // CompressInstEmitter implements a tablegen-driven CompressPat Instruction 13349cc55cSDimitry Andric // Compression mechanism for generating compressed instructions from the 14349cc55cSDimitry Andric // expanded instruction form. 15349cc55cSDimitry Andric 16349cc55cSDimitry Andric // This tablegen backend processes CompressPat declarations in a 17349cc55cSDimitry Andric // td file and generates all the required checks to validate the pattern 18349cc55cSDimitry Andric // declarations; validate the input and output operands to generate the correct 19349cc55cSDimitry Andric // compressed instructions. The checks include validating different types of 20349cc55cSDimitry Andric // operands; register operands, immediate operands, fixed register and fixed 21349cc55cSDimitry Andric // immediate inputs. 22349cc55cSDimitry Andric // 23349cc55cSDimitry Andric // Example: 24349cc55cSDimitry Andric // /// Defines a Pat match between compressed and uncompressed instruction. 25349cc55cSDimitry Andric // /// The relationship and helper function generation are handled by 26349cc55cSDimitry Andric // /// CompressInstEmitter backend. 27349cc55cSDimitry Andric // class CompressPat<dag input, dag output, list<Predicate> predicates = []> { 28349cc55cSDimitry Andric // /// Uncompressed instruction description. 29349cc55cSDimitry Andric // dag Input = input; 30349cc55cSDimitry Andric // /// Compressed instruction description. 31349cc55cSDimitry Andric // dag Output = output; 32349cc55cSDimitry Andric // /// Predicates that must be true for this to match. 33349cc55cSDimitry Andric // list<Predicate> Predicates = predicates; 34349cc55cSDimitry Andric // /// Duplicate match when tied operand is just different. 35349cc55cSDimitry Andric // bit isCompressOnly = false; 36349cc55cSDimitry Andric // } 37349cc55cSDimitry Andric // 38349cc55cSDimitry Andric // let Predicates = [HasStdExtC] in { 39349cc55cSDimitry Andric // def : CompressPat<(ADD GPRNoX0:$rs1, GPRNoX0:$rs1, GPRNoX0:$rs2), 40349cc55cSDimitry Andric // (C_ADD GPRNoX0:$rs1, GPRNoX0:$rs2)>; 41349cc55cSDimitry Andric // } 42349cc55cSDimitry Andric // 43349cc55cSDimitry Andric // The <TargetName>GenCompressInstEmitter.inc is an auto-generated header 44349cc55cSDimitry Andric // file which exports two functions for compressing/uncompressing MCInst 45349cc55cSDimitry Andric // instructions, plus some helper functions: 46349cc55cSDimitry Andric // 47349cc55cSDimitry Andric // bool compressInst(MCInst &OutInst, const MCInst &MI, 48bdd1243dSDimitry Andric // const MCSubtargetInfo &STI); 49349cc55cSDimitry Andric // 50349cc55cSDimitry Andric // bool uncompressInst(MCInst &OutInst, const MCInst &MI, 51349cc55cSDimitry Andric // const MCSubtargetInfo &STI); 52349cc55cSDimitry Andric // 53349cc55cSDimitry Andric // In addition, it exports a function for checking whether 54349cc55cSDimitry Andric // an instruction is compressable: 55349cc55cSDimitry Andric // 56349cc55cSDimitry Andric // bool isCompressibleInst(const MachineInstr& MI, 57bdd1243dSDimitry Andric // const <TargetName>Subtarget &STI); 58349cc55cSDimitry Andric // 59349cc55cSDimitry Andric // The clients that include this auto-generated header file and 60349cc55cSDimitry Andric // invoke these functions can compress an instruction before emitting 61349cc55cSDimitry Andric // it in the target-specific ASM or ELF streamer or can uncompress 62349cc55cSDimitry Andric // an instruction before printing it when the expanded instruction 63349cc55cSDimitry Andric // format aliases is favored. 64349cc55cSDimitry Andric 65349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 66349cc55cSDimitry Andric 67349cc55cSDimitry Andric #include "CodeGenInstruction.h" 68*06c3fb27SDimitry Andric #include "CodeGenRegisters.h" 69349cc55cSDimitry Andric #include "CodeGenTarget.h" 70349cc55cSDimitry Andric #include "llvm/ADT/IndexedMap.h" 71349cc55cSDimitry Andric #include "llvm/ADT/SmallVector.h" 72349cc55cSDimitry Andric #include "llvm/ADT/StringMap.h" 73349cc55cSDimitry Andric #include "llvm/Support/Debug.h" 74349cc55cSDimitry Andric #include "llvm/Support/ErrorHandling.h" 75349cc55cSDimitry Andric #include "llvm/TableGen/Error.h" 76349cc55cSDimitry Andric #include "llvm/TableGen/Record.h" 77349cc55cSDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 78349cc55cSDimitry Andric #include <set> 79349cc55cSDimitry Andric #include <vector> 80349cc55cSDimitry Andric using namespace llvm; 81349cc55cSDimitry Andric 82349cc55cSDimitry Andric #define DEBUG_TYPE "compress-inst-emitter" 83349cc55cSDimitry Andric 84349cc55cSDimitry Andric namespace { 85349cc55cSDimitry Andric class CompressInstEmitter { 86349cc55cSDimitry Andric struct OpData { 87349cc55cSDimitry Andric enum MapKind { Operand, Imm, Reg }; 88349cc55cSDimitry Andric MapKind Kind; 89349cc55cSDimitry Andric union { 90349cc55cSDimitry Andric // Operand number mapped to. 91349cc55cSDimitry Andric unsigned Operand; 92349cc55cSDimitry Andric // Integer immediate value. 93349cc55cSDimitry Andric int64_t Imm; 94349cc55cSDimitry Andric // Physical register. 95349cc55cSDimitry Andric Record *Reg; 96349cc55cSDimitry Andric } Data; 97349cc55cSDimitry Andric // Tied operand index within the instruction. 98349cc55cSDimitry Andric int TiedOpIdx = -1; 99349cc55cSDimitry Andric }; 100349cc55cSDimitry Andric struct CompressPat { 101349cc55cSDimitry Andric // The source instruction definition. 102349cc55cSDimitry Andric CodeGenInstruction Source; 103349cc55cSDimitry Andric // The destination instruction to transform to. 104349cc55cSDimitry Andric CodeGenInstruction Dest; 105349cc55cSDimitry Andric // Required target features to enable pattern. 106349cc55cSDimitry Andric std::vector<Record *> PatReqFeatures; 107349cc55cSDimitry Andric // Maps operands in the Source Instruction to 108349cc55cSDimitry Andric IndexedMap<OpData> SourceOperandMap; 109349cc55cSDimitry Andric // the corresponding Dest instruction operand. 110349cc55cSDimitry Andric // Maps operands in the Dest Instruction 111349cc55cSDimitry Andric // to the corresponding Source instruction operand. 112349cc55cSDimitry Andric IndexedMap<OpData> DestOperandMap; 113349cc55cSDimitry Andric 114349cc55cSDimitry Andric bool IsCompressOnly; 115349cc55cSDimitry Andric CompressPat(CodeGenInstruction &S, CodeGenInstruction &D, 116349cc55cSDimitry Andric std::vector<Record *> RF, IndexedMap<OpData> &SourceMap, 117349cc55cSDimitry Andric IndexedMap<OpData> &DestMap, bool IsCompressOnly) 118349cc55cSDimitry Andric : Source(S), Dest(D), PatReqFeatures(RF), SourceOperandMap(SourceMap), 119349cc55cSDimitry Andric DestOperandMap(DestMap), IsCompressOnly(IsCompressOnly) {} 120349cc55cSDimitry Andric }; 121349cc55cSDimitry Andric enum EmitterType { Compress, Uncompress, CheckCompress }; 122349cc55cSDimitry Andric RecordKeeper &Records; 123349cc55cSDimitry Andric CodeGenTarget Target; 124349cc55cSDimitry Andric SmallVector<CompressPat, 4> CompressPatterns; 125349cc55cSDimitry Andric 126349cc55cSDimitry Andric void addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Inst, 127349cc55cSDimitry Andric IndexedMap<OpData> &OperandMap, bool IsSourceInst); 128349cc55cSDimitry Andric void evaluateCompressPat(Record *Compress); 129349cc55cSDimitry Andric void emitCompressInstEmitter(raw_ostream &o, EmitterType EType); 130349cc55cSDimitry Andric bool validateTypes(Record *SubType, Record *Type, bool IsSourceInst); 131349cc55cSDimitry Andric bool validateRegister(Record *Reg, Record *RegClass); 132349cc55cSDimitry Andric void createDagOperandMapping(Record *Rec, StringMap<unsigned> &SourceOperands, 133349cc55cSDimitry Andric StringMap<unsigned> &DestOperands, 134349cc55cSDimitry Andric DagInit *SourceDag, DagInit *DestDag, 135349cc55cSDimitry Andric IndexedMap<OpData> &SourceOperandMap); 136349cc55cSDimitry Andric 137349cc55cSDimitry Andric void createInstOperandMapping(Record *Rec, DagInit *SourceDag, 138349cc55cSDimitry Andric DagInit *DestDag, 139349cc55cSDimitry Andric IndexedMap<OpData> &SourceOperandMap, 140349cc55cSDimitry Andric IndexedMap<OpData> &DestOperandMap, 141349cc55cSDimitry Andric StringMap<unsigned> &SourceOperands, 142349cc55cSDimitry Andric CodeGenInstruction &DestInst); 143349cc55cSDimitry Andric 144349cc55cSDimitry Andric public: 145349cc55cSDimitry Andric CompressInstEmitter(RecordKeeper &R) : Records(R), Target(R) {} 146349cc55cSDimitry Andric 147349cc55cSDimitry Andric void run(raw_ostream &o); 148349cc55cSDimitry Andric }; 149349cc55cSDimitry Andric } // End anonymous namespace. 150349cc55cSDimitry Andric 151349cc55cSDimitry Andric bool CompressInstEmitter::validateRegister(Record *Reg, Record *RegClass) { 152349cc55cSDimitry Andric assert(Reg->isSubClassOf("Register") && "Reg record should be a Register"); 153349cc55cSDimitry Andric assert(RegClass->isSubClassOf("RegisterClass") && 154349cc55cSDimitry Andric "RegClass record should be a RegisterClass"); 155349cc55cSDimitry Andric const CodeGenRegisterClass &RC = Target.getRegisterClass(RegClass); 156349cc55cSDimitry Andric const CodeGenRegister *R = Target.getRegisterByName(Reg->getName().lower()); 157349cc55cSDimitry Andric assert((R != nullptr) && "Register not defined!!"); 158349cc55cSDimitry Andric return RC.contains(R); 159349cc55cSDimitry Andric } 160349cc55cSDimitry Andric 161349cc55cSDimitry Andric bool CompressInstEmitter::validateTypes(Record *DagOpType, Record *InstOpType, 162349cc55cSDimitry Andric bool IsSourceInst) { 163349cc55cSDimitry Andric if (DagOpType == InstOpType) 164349cc55cSDimitry Andric return true; 165349cc55cSDimitry Andric // Only source instruction operands are allowed to not match Input Dag 166349cc55cSDimitry Andric // operands. 167349cc55cSDimitry Andric if (!IsSourceInst) 168349cc55cSDimitry Andric return false; 169349cc55cSDimitry Andric 170349cc55cSDimitry Andric if (DagOpType->isSubClassOf("RegisterClass") && 171349cc55cSDimitry Andric InstOpType->isSubClassOf("RegisterClass")) { 172349cc55cSDimitry Andric const CodeGenRegisterClass &RC = Target.getRegisterClass(InstOpType); 173349cc55cSDimitry Andric const CodeGenRegisterClass &SubRC = Target.getRegisterClass(DagOpType); 174349cc55cSDimitry Andric return RC.hasSubClass(&SubRC); 175349cc55cSDimitry Andric } 176349cc55cSDimitry Andric 177349cc55cSDimitry Andric // At this point either or both types are not registers, reject the pattern. 178349cc55cSDimitry Andric if (DagOpType->isSubClassOf("RegisterClass") || 179349cc55cSDimitry Andric InstOpType->isSubClassOf("RegisterClass")) 180349cc55cSDimitry Andric return false; 181349cc55cSDimitry Andric 182349cc55cSDimitry Andric // Let further validation happen when compress()/uncompress() functions are 183349cc55cSDimitry Andric // invoked. 184349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << (IsSourceInst ? "Input" : "Output") 185349cc55cSDimitry Andric << " Dag Operand Type: '" << DagOpType->getName() 186349cc55cSDimitry Andric << "' and " 187349cc55cSDimitry Andric << "Instruction Operand Type: '" << InstOpType->getName() 188349cc55cSDimitry Andric << "' can't be checked at pattern validation time!\n"); 189349cc55cSDimitry Andric return true; 190349cc55cSDimitry Andric } 191349cc55cSDimitry Andric 192349cc55cSDimitry Andric /// The patterns in the Dag contain different types of operands: 193349cc55cSDimitry Andric /// Register operands, e.g.: GPRC:$rs1; Fixed registers, e.g: X1; Immediate 194349cc55cSDimitry Andric /// operands, e.g.: simm6:$imm; Fixed immediate operands, e.g.: 0. This function 195349cc55cSDimitry Andric /// maps Dag operands to its corresponding instruction operands. For register 196349cc55cSDimitry Andric /// operands and fixed registers it expects the Dag operand type to be contained 197349cc55cSDimitry Andric /// in the instantiated instruction operand type. For immediate operands and 198349cc55cSDimitry Andric /// immediates no validation checks are enforced at pattern validation time. 199349cc55cSDimitry Andric void CompressInstEmitter::addDagOperandMapping(Record *Rec, DagInit *Dag, 200349cc55cSDimitry Andric CodeGenInstruction &Inst, 201349cc55cSDimitry Andric IndexedMap<OpData> &OperandMap, 202349cc55cSDimitry Andric bool IsSourceInst) { 203349cc55cSDimitry Andric // TiedCount keeps track of the number of operands skipped in Inst 204349cc55cSDimitry Andric // operands list to get to the corresponding Dag operand. This is 205349cc55cSDimitry Andric // necessary because the number of operands in Inst might be greater 206349cc55cSDimitry Andric // than number of operands in the Dag due to how tied operands 207349cc55cSDimitry Andric // are represented. 208349cc55cSDimitry Andric unsigned TiedCount = 0; 209349cc55cSDimitry Andric for (unsigned i = 0, e = Inst.Operands.size(); i != e; ++i) { 210349cc55cSDimitry Andric int TiedOpIdx = Inst.Operands[i].getTiedRegister(); 211349cc55cSDimitry Andric if (-1 != TiedOpIdx) { 212349cc55cSDimitry Andric // Set the entry in OperandMap for the tied operand we're skipping. 213349cc55cSDimitry Andric OperandMap[i].Kind = OperandMap[TiedOpIdx].Kind; 214349cc55cSDimitry Andric OperandMap[i].Data = OperandMap[TiedOpIdx].Data; 215349cc55cSDimitry Andric TiedCount++; 216349cc55cSDimitry Andric continue; 217349cc55cSDimitry Andric } 218349cc55cSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i - TiedCount))) { 219349cc55cSDimitry Andric if (DI->getDef()->isSubClassOf("Register")) { 220349cc55cSDimitry Andric // Check if the fixed register belongs to the Register class. 221349cc55cSDimitry Andric if (!validateRegister(DI->getDef(), Inst.Operands[i].Rec)) 222349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), 223349cc55cSDimitry Andric "Error in Dag '" + Dag->getAsString() + 224349cc55cSDimitry Andric "'Register: '" + DI->getDef()->getName() + 225349cc55cSDimitry Andric "' is not in register class '" + 226349cc55cSDimitry Andric Inst.Operands[i].Rec->getName() + "'"); 227349cc55cSDimitry Andric OperandMap[i].Kind = OpData::Reg; 228349cc55cSDimitry Andric OperandMap[i].Data.Reg = DI->getDef(); 229349cc55cSDimitry Andric continue; 230349cc55cSDimitry Andric } 231349cc55cSDimitry Andric // Validate that Dag operand type matches the type defined in the 232349cc55cSDimitry Andric // corresponding instruction. Operands in the input Dag pattern are 233349cc55cSDimitry Andric // allowed to be a subclass of the type specified in corresponding 234349cc55cSDimitry Andric // instruction operand instead of being an exact match. 235349cc55cSDimitry Andric if (!validateTypes(DI->getDef(), Inst.Operands[i].Rec, IsSourceInst)) 236349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), 237349cc55cSDimitry Andric "Error in Dag '" + Dag->getAsString() + "'. Operand '" + 238349cc55cSDimitry Andric Dag->getArgNameStr(i - TiedCount) + "' has type '" + 239349cc55cSDimitry Andric DI->getDef()->getName() + 240349cc55cSDimitry Andric "' which does not match the type '" + 241349cc55cSDimitry Andric Inst.Operands[i].Rec->getName() + 242349cc55cSDimitry Andric "' in the corresponding instruction operand!"); 243349cc55cSDimitry Andric 244349cc55cSDimitry Andric OperandMap[i].Kind = OpData::Operand; 245349cc55cSDimitry Andric } else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i - TiedCount))) { 246349cc55cSDimitry Andric // Validate that corresponding instruction operand expects an immediate. 247349cc55cSDimitry Andric if (Inst.Operands[i].Rec->isSubClassOf("RegisterClass")) 248349cc55cSDimitry Andric PrintFatalError( 249349cc55cSDimitry Andric Rec->getLoc(), 250349cc55cSDimitry Andric "Error in Dag '" + Dag->getAsString() + "' Found immediate: '" + 251349cc55cSDimitry Andric II->getAsString() + 252349cc55cSDimitry Andric "' but corresponding instruction operand expected a register!"); 253349cc55cSDimitry Andric // No pattern validation check possible for values of fixed immediate. 254349cc55cSDimitry Andric OperandMap[i].Kind = OpData::Imm; 255349cc55cSDimitry Andric OperandMap[i].Data.Imm = II->getValue(); 256349cc55cSDimitry Andric LLVM_DEBUG( 257349cc55cSDimitry Andric dbgs() << " Found immediate '" << II->getValue() << "' at " 258349cc55cSDimitry Andric << (IsSourceInst ? "input " : "output ") 259349cc55cSDimitry Andric << "Dag. No validation time check possible for values of " 260349cc55cSDimitry Andric "fixed immediate.\n"); 261349cc55cSDimitry Andric } else 262349cc55cSDimitry Andric llvm_unreachable("Unhandled CompressPat argument type!"); 263349cc55cSDimitry Andric } 264349cc55cSDimitry Andric } 265349cc55cSDimitry Andric 266349cc55cSDimitry Andric // Verify the Dag operand count is enough to build an instruction. 267349cc55cSDimitry Andric static bool verifyDagOpCount(CodeGenInstruction &Inst, DagInit *Dag, 268349cc55cSDimitry Andric bool IsSource) { 269349cc55cSDimitry Andric if (Dag->getNumArgs() == Inst.Operands.size()) 270349cc55cSDimitry Andric return true; 271349cc55cSDimitry Andric // Source instructions are non compressed instructions and don't have tied 272349cc55cSDimitry Andric // operands. 273349cc55cSDimitry Andric if (IsSource) 274349cc55cSDimitry Andric PrintFatalError(Inst.TheDef->getLoc(), 275349cc55cSDimitry Andric "Input operands for Inst '" + Inst.TheDef->getName() + 276349cc55cSDimitry Andric "' and input Dag operand count mismatch"); 277349cc55cSDimitry Andric // The Dag can't have more arguments than the Instruction. 278349cc55cSDimitry Andric if (Dag->getNumArgs() > Inst.Operands.size()) 279349cc55cSDimitry Andric PrintFatalError(Inst.TheDef->getLoc(), 280349cc55cSDimitry Andric "Inst '" + Inst.TheDef->getName() + 281349cc55cSDimitry Andric "' and Dag operand count mismatch"); 282349cc55cSDimitry Andric 283349cc55cSDimitry Andric // The Instruction might have tied operands so the Dag might have 284349cc55cSDimitry Andric // a fewer operand count. 285349cc55cSDimitry Andric unsigned RealCount = Inst.Operands.size(); 286349cc55cSDimitry Andric for (const auto &Operand : Inst.Operands) 287349cc55cSDimitry Andric if (Operand.getTiedRegister() != -1) 288349cc55cSDimitry Andric --RealCount; 289349cc55cSDimitry Andric 290349cc55cSDimitry Andric if (Dag->getNumArgs() != RealCount) 291349cc55cSDimitry Andric PrintFatalError(Inst.TheDef->getLoc(), 292349cc55cSDimitry Andric "Inst '" + Inst.TheDef->getName() + 293349cc55cSDimitry Andric "' and Dag operand count mismatch"); 294349cc55cSDimitry Andric return true; 295349cc55cSDimitry Andric } 296349cc55cSDimitry Andric 297349cc55cSDimitry Andric static bool validateArgsTypes(Init *Arg1, Init *Arg2) { 298349cc55cSDimitry Andric return cast<DefInit>(Arg1)->getDef() == cast<DefInit>(Arg2)->getDef(); 299349cc55cSDimitry Andric } 300349cc55cSDimitry Andric 301349cc55cSDimitry Andric // Creates a mapping between the operand name in the Dag (e.g. $rs1) and 302349cc55cSDimitry Andric // its index in the list of Dag operands and checks that operands with the same 303349cc55cSDimitry Andric // name have the same types. For example in 'C_ADD $rs1, $rs2' we generate the 304349cc55cSDimitry Andric // mapping $rs1 --> 0, $rs2 ---> 1. If the operand appears twice in the (tied) 305349cc55cSDimitry Andric // same Dag we use the last occurrence for indexing. 306349cc55cSDimitry Andric void CompressInstEmitter::createDagOperandMapping( 307349cc55cSDimitry Andric Record *Rec, StringMap<unsigned> &SourceOperands, 308349cc55cSDimitry Andric StringMap<unsigned> &DestOperands, DagInit *SourceDag, DagInit *DestDag, 309349cc55cSDimitry Andric IndexedMap<OpData> &SourceOperandMap) { 310349cc55cSDimitry Andric for (unsigned i = 0; i < DestDag->getNumArgs(); ++i) { 311349cc55cSDimitry Andric // Skip fixed immediates and registers, they were handled in 312349cc55cSDimitry Andric // addDagOperandMapping. 313349cc55cSDimitry Andric if ("" == DestDag->getArgNameStr(i)) 314349cc55cSDimitry Andric continue; 315349cc55cSDimitry Andric DestOperands[DestDag->getArgNameStr(i)] = i; 316349cc55cSDimitry Andric } 317349cc55cSDimitry Andric 318349cc55cSDimitry Andric for (unsigned i = 0; i < SourceDag->getNumArgs(); ++i) { 319349cc55cSDimitry Andric // Skip fixed immediates and registers, they were handled in 320349cc55cSDimitry Andric // addDagOperandMapping. 321349cc55cSDimitry Andric if ("" == SourceDag->getArgNameStr(i)) 322349cc55cSDimitry Andric continue; 323349cc55cSDimitry Andric 324349cc55cSDimitry Andric StringMap<unsigned>::iterator it = 325349cc55cSDimitry Andric SourceOperands.find(SourceDag->getArgNameStr(i)); 326349cc55cSDimitry Andric if (it != SourceOperands.end()) { 327349cc55cSDimitry Andric // Operand sharing the same name in the Dag should be mapped as tied. 328349cc55cSDimitry Andric SourceOperandMap[i].TiedOpIdx = it->getValue(); 329349cc55cSDimitry Andric if (!validateArgsTypes(SourceDag->getArg(it->getValue()), 330349cc55cSDimitry Andric SourceDag->getArg(i))) 331349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), 332349cc55cSDimitry Andric "Input Operand '" + SourceDag->getArgNameStr(i) + 333349cc55cSDimitry Andric "' has a mismatched tied operand!\n"); 334349cc55cSDimitry Andric } 335349cc55cSDimitry Andric it = DestOperands.find(SourceDag->getArgNameStr(i)); 336349cc55cSDimitry Andric if (it == DestOperands.end()) 337349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), "Operand " + SourceDag->getArgNameStr(i) + 338349cc55cSDimitry Andric " defined in Input Dag but not used in" 339349cc55cSDimitry Andric " Output Dag!\n"); 340349cc55cSDimitry Andric // Input Dag operand types must match output Dag operand type. 341349cc55cSDimitry Andric if (!validateArgsTypes(DestDag->getArg(it->getValue()), 342349cc55cSDimitry Andric SourceDag->getArg(i))) 343349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), "Type mismatch between Input and " 344349cc55cSDimitry Andric "Output Dag operand '" + 345349cc55cSDimitry Andric SourceDag->getArgNameStr(i) + "'!"); 346349cc55cSDimitry Andric SourceOperands[SourceDag->getArgNameStr(i)] = i; 347349cc55cSDimitry Andric } 348349cc55cSDimitry Andric } 349349cc55cSDimitry Andric 350349cc55cSDimitry Andric /// Map operand names in the Dag to their index in both corresponding input and 351349cc55cSDimitry Andric /// output instructions. Validate that operands defined in the input are 352349cc55cSDimitry Andric /// used in the output pattern while populating the maps. 353349cc55cSDimitry Andric void CompressInstEmitter::createInstOperandMapping( 354349cc55cSDimitry Andric Record *Rec, DagInit *SourceDag, DagInit *DestDag, 355349cc55cSDimitry Andric IndexedMap<OpData> &SourceOperandMap, IndexedMap<OpData> &DestOperandMap, 356349cc55cSDimitry Andric StringMap<unsigned> &SourceOperands, CodeGenInstruction &DestInst) { 357349cc55cSDimitry Andric // TiedCount keeps track of the number of operands skipped in Inst 358349cc55cSDimitry Andric // operands list to get to the corresponding Dag operand. 359349cc55cSDimitry Andric unsigned TiedCount = 0; 360349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << " Operand mapping:\n Source Dest\n"); 361349cc55cSDimitry Andric for (unsigned i = 0, e = DestInst.Operands.size(); i != e; ++i) { 362349cc55cSDimitry Andric int TiedInstOpIdx = DestInst.Operands[i].getTiedRegister(); 363349cc55cSDimitry Andric if (TiedInstOpIdx != -1) { 364349cc55cSDimitry Andric ++TiedCount; 365349cc55cSDimitry Andric DestOperandMap[i].Data = DestOperandMap[TiedInstOpIdx].Data; 366349cc55cSDimitry Andric DestOperandMap[i].Kind = DestOperandMap[TiedInstOpIdx].Kind; 367349cc55cSDimitry Andric if (DestOperandMap[i].Kind == OpData::Operand) 368349cc55cSDimitry Andric // No need to fill the SourceOperandMap here since it was mapped to 369349cc55cSDimitry Andric // destination operand 'TiedInstOpIdx' in a previous iteration. 370349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << " " << DestOperandMap[i].Data.Operand 371349cc55cSDimitry Andric << " ====> " << i 372349cc55cSDimitry Andric << " Dest operand tied with operand '" 373349cc55cSDimitry Andric << TiedInstOpIdx << "'\n"); 374349cc55cSDimitry Andric continue; 375349cc55cSDimitry Andric } 376349cc55cSDimitry Andric // Skip fixed immediates and registers, they were handled in 377349cc55cSDimitry Andric // addDagOperandMapping. 378349cc55cSDimitry Andric if (DestOperandMap[i].Kind != OpData::Operand) 379349cc55cSDimitry Andric continue; 380349cc55cSDimitry Andric 381349cc55cSDimitry Andric unsigned DagArgIdx = i - TiedCount; 382349cc55cSDimitry Andric StringMap<unsigned>::iterator SourceOp = 383349cc55cSDimitry Andric SourceOperands.find(DestDag->getArgNameStr(DagArgIdx)); 384349cc55cSDimitry Andric if (SourceOp == SourceOperands.end()) 385349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), 386349cc55cSDimitry Andric "Output Dag operand '" + 387349cc55cSDimitry Andric DestDag->getArgNameStr(DagArgIdx) + 388349cc55cSDimitry Andric "' has no matching input Dag operand."); 389349cc55cSDimitry Andric 390349cc55cSDimitry Andric assert(DestDag->getArgNameStr(DagArgIdx) == 391349cc55cSDimitry Andric SourceDag->getArgNameStr(SourceOp->getValue()) && 392349cc55cSDimitry Andric "Incorrect operand mapping detected!\n"); 393349cc55cSDimitry Andric DestOperandMap[i].Data.Operand = SourceOp->getValue(); 394349cc55cSDimitry Andric SourceOperandMap[SourceOp->getValue()].Data.Operand = i; 395349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << " " << SourceOp->getValue() << " ====> " << i 396349cc55cSDimitry Andric << "\n"); 397349cc55cSDimitry Andric } 398349cc55cSDimitry Andric } 399349cc55cSDimitry Andric 400349cc55cSDimitry Andric /// Validates the CompressPattern and create operand mapping. 401349cc55cSDimitry Andric /// These are the checks to validate a CompressPat pattern declarations. 402349cc55cSDimitry Andric /// Error out with message under these conditions: 403349cc55cSDimitry Andric /// - Dag Input opcode is an expanded instruction and Dag Output opcode is a 404349cc55cSDimitry Andric /// compressed instruction. 405349cc55cSDimitry Andric /// - Operands in Dag Input must be all used in Dag Output. 406349cc55cSDimitry Andric /// Register Operand type in Dag Input Type must be contained in the 407349cc55cSDimitry Andric /// corresponding Source Instruction type. 408349cc55cSDimitry Andric /// - Register Operand type in Dag Input must be the same as in Dag Ouput. 409349cc55cSDimitry Andric /// - Register Operand type in Dag Output must be the same as the 410349cc55cSDimitry Andric /// corresponding Destination Inst type. 411349cc55cSDimitry Andric /// - Immediate Operand type in Dag Input must be the same as in Dag Ouput. 412349cc55cSDimitry Andric /// - Immediate Operand type in Dag Ouput must be the same as the corresponding 413349cc55cSDimitry Andric /// Destination Instruction type. 414349cc55cSDimitry Andric /// - Fixed register must be contained in the corresponding Source Instruction 415349cc55cSDimitry Andric /// type. 416349cc55cSDimitry Andric /// - Fixed register must be contained in the corresponding Destination 417349cc55cSDimitry Andric /// Instruction type. Warning message printed under these conditions: 418349cc55cSDimitry Andric /// - Fixed immediate in Dag Input or Dag Ouput cannot be checked at this time 419349cc55cSDimitry Andric /// and generate warning. 420349cc55cSDimitry Andric /// - Immediate operand type in Dag Input differs from the corresponding Source 421349cc55cSDimitry Andric /// Instruction type and generate a warning. 422349cc55cSDimitry Andric void CompressInstEmitter::evaluateCompressPat(Record *Rec) { 423349cc55cSDimitry Andric // Validate input Dag operands. 424349cc55cSDimitry Andric DagInit *SourceDag = Rec->getValueAsDag("Input"); 425349cc55cSDimitry Andric assert(SourceDag && "Missing 'Input' in compress pattern!"); 426349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Input: " << *SourceDag << "\n"); 427349cc55cSDimitry Andric 428349cc55cSDimitry Andric // Checking we are transforming from compressed to uncompressed instructions. 429349cc55cSDimitry Andric Record *Operator = SourceDag->getOperatorAsDef(Rec->getLoc()); 430349cc55cSDimitry Andric CodeGenInstruction SourceInst(Operator); 431349cc55cSDimitry Andric verifyDagOpCount(SourceInst, SourceDag, true); 432349cc55cSDimitry Andric 433349cc55cSDimitry Andric // Validate output Dag operands. 434349cc55cSDimitry Andric DagInit *DestDag = Rec->getValueAsDag("Output"); 435349cc55cSDimitry Andric assert(DestDag && "Missing 'Output' in compress pattern!"); 436349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Output: " << *DestDag << "\n"); 437349cc55cSDimitry Andric 438349cc55cSDimitry Andric Record *DestOperator = DestDag->getOperatorAsDef(Rec->getLoc()); 439349cc55cSDimitry Andric CodeGenInstruction DestInst(DestOperator); 440349cc55cSDimitry Andric verifyDagOpCount(DestInst, DestDag, false); 441349cc55cSDimitry Andric 442349cc55cSDimitry Andric if (Operator->getValueAsInt("Size") <= DestOperator->getValueAsInt("Size")) 443349cc55cSDimitry Andric PrintFatalError( 444349cc55cSDimitry Andric Rec->getLoc(), 445349cc55cSDimitry Andric "Compressed instruction '" + DestOperator->getName() + 446349cc55cSDimitry Andric "'is not strictly smaller than the uncompressed instruction '" + 447349cc55cSDimitry Andric Operator->getName() + "' !"); 448349cc55cSDimitry Andric 449349cc55cSDimitry Andric // Fill the mapping from the source to destination instructions. 450349cc55cSDimitry Andric 451349cc55cSDimitry Andric IndexedMap<OpData> SourceOperandMap; 452349cc55cSDimitry Andric SourceOperandMap.grow(SourceInst.Operands.size()); 453349cc55cSDimitry Andric // Create a mapping between source Dag operands and source Inst operands. 454349cc55cSDimitry Andric addDagOperandMapping(Rec, SourceDag, SourceInst, SourceOperandMap, 455349cc55cSDimitry Andric /*IsSourceInst*/ true); 456349cc55cSDimitry Andric 457349cc55cSDimitry Andric IndexedMap<OpData> DestOperandMap; 458349cc55cSDimitry Andric DestOperandMap.grow(DestInst.Operands.size()); 459349cc55cSDimitry Andric // Create a mapping between destination Dag operands and destination Inst 460349cc55cSDimitry Andric // operands. 461349cc55cSDimitry Andric addDagOperandMapping(Rec, DestDag, DestInst, DestOperandMap, 462349cc55cSDimitry Andric /*IsSourceInst*/ false); 463349cc55cSDimitry Andric 464349cc55cSDimitry Andric StringMap<unsigned> SourceOperands; 465349cc55cSDimitry Andric StringMap<unsigned> DestOperands; 466349cc55cSDimitry Andric createDagOperandMapping(Rec, SourceOperands, DestOperands, SourceDag, DestDag, 467349cc55cSDimitry Andric SourceOperandMap); 468349cc55cSDimitry Andric // Create operand mapping between the source and destination instructions. 469349cc55cSDimitry Andric createInstOperandMapping(Rec, SourceDag, DestDag, SourceOperandMap, 470349cc55cSDimitry Andric DestOperandMap, SourceOperands, DestInst); 471349cc55cSDimitry Andric 472349cc55cSDimitry Andric // Get the target features for the CompressPat. 473349cc55cSDimitry Andric std::vector<Record *> PatReqFeatures; 474349cc55cSDimitry Andric std::vector<Record *> RF = Rec->getValueAsListOfDefs("Predicates"); 475349cc55cSDimitry Andric copy_if(RF, std::back_inserter(PatReqFeatures), [](Record *R) { 476349cc55cSDimitry Andric return R->getValueAsBit("AssemblerMatcherPredicate"); 477349cc55cSDimitry Andric }); 478349cc55cSDimitry Andric 479349cc55cSDimitry Andric CompressPatterns.push_back(CompressPat(SourceInst, DestInst, PatReqFeatures, 480349cc55cSDimitry Andric SourceOperandMap, DestOperandMap, 481349cc55cSDimitry Andric Rec->getValueAsBit("isCompressOnly"))); 482349cc55cSDimitry Andric } 483349cc55cSDimitry Andric 484349cc55cSDimitry Andric static void 485349cc55cSDimitry Andric getReqFeatures(std::set<std::pair<bool, StringRef>> &FeaturesSet, 486349cc55cSDimitry Andric std::set<std::set<std::pair<bool, StringRef>>> &AnyOfFeatureSets, 487349cc55cSDimitry Andric const std::vector<Record *> &ReqFeatures) { 488349cc55cSDimitry Andric for (auto &R : ReqFeatures) { 489349cc55cSDimitry Andric const DagInit *D = R->getValueAsDag("AssemblerCondDag"); 490349cc55cSDimitry Andric std::string CombineType = D->getOperator()->getAsString(); 491349cc55cSDimitry Andric if (CombineType != "any_of" && CombineType != "all_of") 492349cc55cSDimitry Andric PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!"); 493349cc55cSDimitry Andric if (D->getNumArgs() == 0) 494349cc55cSDimitry Andric PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!"); 495349cc55cSDimitry Andric bool IsOr = CombineType == "any_of"; 496349cc55cSDimitry Andric std::set<std::pair<bool, StringRef>> AnyOfSet; 497349cc55cSDimitry Andric 498349cc55cSDimitry Andric for (auto *Arg : D->getArgs()) { 499349cc55cSDimitry Andric bool IsNot = false; 500349cc55cSDimitry Andric if (auto *NotArg = dyn_cast<DagInit>(Arg)) { 501349cc55cSDimitry Andric if (NotArg->getOperator()->getAsString() != "not" || 502349cc55cSDimitry Andric NotArg->getNumArgs() != 1) 503349cc55cSDimitry Andric PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!"); 504349cc55cSDimitry Andric Arg = NotArg->getArg(0); 505349cc55cSDimitry Andric IsNot = true; 506349cc55cSDimitry Andric } 507349cc55cSDimitry Andric if (!isa<DefInit>(Arg) || 508349cc55cSDimitry Andric !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature")) 509349cc55cSDimitry Andric PrintFatalError(R->getLoc(), "Invalid AssemblerCondDag!"); 510349cc55cSDimitry Andric if (IsOr) 511349cc55cSDimitry Andric AnyOfSet.insert({IsNot, cast<DefInit>(Arg)->getDef()->getName()}); 512349cc55cSDimitry Andric else 513349cc55cSDimitry Andric FeaturesSet.insert({IsNot, cast<DefInit>(Arg)->getDef()->getName()}); 514349cc55cSDimitry Andric } 515349cc55cSDimitry Andric 516349cc55cSDimitry Andric if (IsOr) 517349cc55cSDimitry Andric AnyOfFeatureSets.insert(AnyOfSet); 518349cc55cSDimitry Andric } 519349cc55cSDimitry Andric } 520349cc55cSDimitry Andric 521349cc55cSDimitry Andric static unsigned getPredicates(DenseMap<const Record *, unsigned> &PredicateMap, 522349cc55cSDimitry Andric std::vector<const Record *> &Predicates, 523349cc55cSDimitry Andric Record *Rec, StringRef Name) { 524349cc55cSDimitry Andric unsigned &Entry = PredicateMap[Rec]; 525349cc55cSDimitry Andric if (Entry) 526349cc55cSDimitry Andric return Entry; 527349cc55cSDimitry Andric 528349cc55cSDimitry Andric if (!Rec->isValueUnset(Name)) { 529349cc55cSDimitry Andric Predicates.push_back(Rec); 530349cc55cSDimitry Andric Entry = Predicates.size(); 531349cc55cSDimitry Andric return Entry; 532349cc55cSDimitry Andric } 533349cc55cSDimitry Andric 534349cc55cSDimitry Andric PrintFatalError(Rec->getLoc(), "No " + Name + 535349cc55cSDimitry Andric " predicate on this operand at all: '" + 536349cc55cSDimitry Andric Rec->getName() + "'"); 537349cc55cSDimitry Andric return 0; 538349cc55cSDimitry Andric } 539349cc55cSDimitry Andric 540349cc55cSDimitry Andric static void printPredicates(const std::vector<const Record *> &Predicates, 541349cc55cSDimitry Andric StringRef Name, raw_ostream &o) { 542349cc55cSDimitry Andric for (unsigned i = 0; i < Predicates.size(); ++i) { 543349cc55cSDimitry Andric StringRef Pred = Predicates[i]->getValueAsString(Name); 544349cc55cSDimitry Andric o << " case " << i + 1 << ": {\n" 545349cc55cSDimitry Andric << " // " << Predicates[i]->getName() << "\n" 546349cc55cSDimitry Andric << " " << Pred << "\n" 547349cc55cSDimitry Andric << " }\n"; 548349cc55cSDimitry Andric } 549349cc55cSDimitry Andric } 550349cc55cSDimitry Andric 551349cc55cSDimitry Andric static void mergeCondAndCode(raw_ostream &CombinedStream, StringRef CondStr, 552349cc55cSDimitry Andric StringRef CodeStr) { 553349cc55cSDimitry Andric // Remove first indentation and last '&&'. 554349cc55cSDimitry Andric CondStr = CondStr.drop_front(6).drop_back(4); 555349cc55cSDimitry Andric CombinedStream.indent(4) << "if (" << CondStr << ") {\n"; 556349cc55cSDimitry Andric CombinedStream << CodeStr; 557349cc55cSDimitry Andric CombinedStream.indent(4) << " return true;\n"; 558349cc55cSDimitry Andric CombinedStream.indent(4) << "} // if\n"; 559349cc55cSDimitry Andric } 560349cc55cSDimitry Andric 561349cc55cSDimitry Andric void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &o, 562349cc55cSDimitry Andric EmitterType EType) { 563349cc55cSDimitry Andric Record *AsmWriter = Target.getAsmWriter(); 564349cc55cSDimitry Andric if (!AsmWriter->getValueAsInt("PassSubtarget")) 565349cc55cSDimitry Andric PrintFatalError(AsmWriter->getLoc(), 566349cc55cSDimitry Andric "'PassSubtarget' is false. SubTargetInfo object is needed " 567349cc55cSDimitry Andric "for target features.\n"); 568349cc55cSDimitry Andric 569349cc55cSDimitry Andric StringRef TargetName = Target.getName(); 570349cc55cSDimitry Andric 571349cc55cSDimitry Andric // Sort entries in CompressPatterns to handle instructions that can have more 572349cc55cSDimitry Andric // than one candidate for compression\uncompression, e.g ADD can be 573349cc55cSDimitry Andric // transformed to a C_ADD or a C_MV. When emitting 'uncompress()' function the 574349cc55cSDimitry Andric // source and destination are flipped and the sort key needs to change 575349cc55cSDimitry Andric // accordingly. 576349cc55cSDimitry Andric llvm::stable_sort(CompressPatterns, [EType](const CompressPat &LHS, 577349cc55cSDimitry Andric const CompressPat &RHS) { 578349cc55cSDimitry Andric if (EType == EmitterType::Compress || EType == EmitterType::CheckCompress) 579349cc55cSDimitry Andric return (LHS.Source.TheDef->getName() < RHS.Source.TheDef->getName()); 580349cc55cSDimitry Andric else 581349cc55cSDimitry Andric return (LHS.Dest.TheDef->getName() < RHS.Dest.TheDef->getName()); 582349cc55cSDimitry Andric }); 583349cc55cSDimitry Andric 584349cc55cSDimitry Andric // A list of MCOperandPredicates for all operands in use, and the reverse map. 585349cc55cSDimitry Andric std::vector<const Record *> MCOpPredicates; 586349cc55cSDimitry Andric DenseMap<const Record *, unsigned> MCOpPredicateMap; 587349cc55cSDimitry Andric // A list of ImmLeaf Predicates for all operands in use, and the reverse map. 588349cc55cSDimitry Andric std::vector<const Record *> ImmLeafPredicates; 589349cc55cSDimitry Andric DenseMap<const Record *, unsigned> ImmLeafPredicateMap; 590349cc55cSDimitry Andric 591349cc55cSDimitry Andric std::string F; 592349cc55cSDimitry Andric std::string FH; 593349cc55cSDimitry Andric raw_string_ostream Func(F); 594349cc55cSDimitry Andric raw_string_ostream FuncH(FH); 595349cc55cSDimitry Andric 596349cc55cSDimitry Andric if (EType == EmitterType::Compress) 597349cc55cSDimitry Andric o << "\n#ifdef GEN_COMPRESS_INSTR\n" 598349cc55cSDimitry Andric << "#undef GEN_COMPRESS_INSTR\n\n"; 599349cc55cSDimitry Andric else if (EType == EmitterType::Uncompress) 600349cc55cSDimitry Andric o << "\n#ifdef GEN_UNCOMPRESS_INSTR\n" 601349cc55cSDimitry Andric << "#undef GEN_UNCOMPRESS_INSTR\n\n"; 602349cc55cSDimitry Andric else if (EType == EmitterType::CheckCompress) 603349cc55cSDimitry Andric o << "\n#ifdef GEN_CHECK_COMPRESS_INSTR\n" 604349cc55cSDimitry Andric << "#undef GEN_CHECK_COMPRESS_INSTR\n\n"; 605349cc55cSDimitry Andric 606349cc55cSDimitry Andric if (EType == EmitterType::Compress) { 607349cc55cSDimitry Andric FuncH << "static bool compressInst(MCInst &OutInst,\n"; 608349cc55cSDimitry Andric FuncH.indent(25) << "const MCInst &MI,\n"; 609bdd1243dSDimitry Andric FuncH.indent(25) << "const MCSubtargetInfo &STI) {\n"; 610349cc55cSDimitry Andric } else if (EType == EmitterType::Uncompress) { 611349cc55cSDimitry Andric FuncH << "static bool uncompressInst(MCInst &OutInst,\n"; 612349cc55cSDimitry Andric FuncH.indent(27) << "const MCInst &MI,\n"; 613349cc55cSDimitry Andric FuncH.indent(27) << "const MCSubtargetInfo &STI) {\n"; 614349cc55cSDimitry Andric } else if (EType == EmitterType::CheckCompress) { 615349cc55cSDimitry Andric FuncH << "static bool isCompressibleInst(const MachineInstr &MI,\n"; 616bdd1243dSDimitry Andric FuncH.indent(31) << "const " << TargetName << "Subtarget &STI) {\n"; 617349cc55cSDimitry Andric } 618349cc55cSDimitry Andric 619349cc55cSDimitry Andric if (CompressPatterns.empty()) { 620349cc55cSDimitry Andric o << FuncH.str(); 621349cc55cSDimitry Andric o.indent(2) << "return false;\n}\n"; 622349cc55cSDimitry Andric if (EType == EmitterType::Compress) 623349cc55cSDimitry Andric o << "\n#endif //GEN_COMPRESS_INSTR\n"; 624349cc55cSDimitry Andric else if (EType == EmitterType::Uncompress) 625349cc55cSDimitry Andric o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n"; 626349cc55cSDimitry Andric else if (EType == EmitterType::CheckCompress) 627349cc55cSDimitry Andric o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n"; 628349cc55cSDimitry Andric return; 629349cc55cSDimitry Andric } 630349cc55cSDimitry Andric 631349cc55cSDimitry Andric std::string CaseString; 632349cc55cSDimitry Andric raw_string_ostream CaseStream(CaseString); 633349cc55cSDimitry Andric StringRef PrevOp; 634349cc55cSDimitry Andric StringRef CurOp; 635349cc55cSDimitry Andric CaseStream << " switch (MI.getOpcode()) {\n"; 636349cc55cSDimitry Andric CaseStream << " default: return false;\n"; 637349cc55cSDimitry Andric 638349cc55cSDimitry Andric bool CompressOrCheck = 639349cc55cSDimitry Andric EType == EmitterType::Compress || EType == EmitterType::CheckCompress; 640349cc55cSDimitry Andric bool CompressOrUncompress = 641349cc55cSDimitry Andric EType == EmitterType::Compress || EType == EmitterType::Uncompress; 642bdd1243dSDimitry Andric std::string ValidatorName = 643bdd1243dSDimitry Andric CompressOrUncompress 644bdd1243dSDimitry Andric ? (TargetName + "ValidateMCOperandFor" + 645bdd1243dSDimitry Andric (EType == EmitterType::Compress ? "Compress" : "Uncompress")) 646bdd1243dSDimitry Andric .str() 647bdd1243dSDimitry Andric : ""; 648349cc55cSDimitry Andric 649349cc55cSDimitry Andric for (auto &CompressPat : CompressPatterns) { 650349cc55cSDimitry Andric if (EType == EmitterType::Uncompress && CompressPat.IsCompressOnly) 651349cc55cSDimitry Andric continue; 652349cc55cSDimitry Andric 653349cc55cSDimitry Andric std::string CondString; 654349cc55cSDimitry Andric std::string CodeString; 655349cc55cSDimitry Andric raw_string_ostream CondStream(CondString); 656349cc55cSDimitry Andric raw_string_ostream CodeStream(CodeString); 657349cc55cSDimitry Andric CodeGenInstruction &Source = 658349cc55cSDimitry Andric CompressOrCheck ? CompressPat.Source : CompressPat.Dest; 659349cc55cSDimitry Andric CodeGenInstruction &Dest = 660349cc55cSDimitry Andric CompressOrCheck ? CompressPat.Dest : CompressPat.Source; 661349cc55cSDimitry Andric IndexedMap<OpData> SourceOperandMap = CompressOrCheck 662349cc55cSDimitry Andric ? CompressPat.SourceOperandMap 663349cc55cSDimitry Andric : CompressPat.DestOperandMap; 664349cc55cSDimitry Andric IndexedMap<OpData> &DestOperandMap = CompressOrCheck 665349cc55cSDimitry Andric ? CompressPat.DestOperandMap 666349cc55cSDimitry Andric : CompressPat.SourceOperandMap; 667349cc55cSDimitry Andric 668349cc55cSDimitry Andric CurOp = Source.TheDef->getName(); 669349cc55cSDimitry Andric // Check current and previous opcode to decide to continue or end a case. 670349cc55cSDimitry Andric if (CurOp != PrevOp) { 671349cc55cSDimitry Andric if (!PrevOp.empty()) 672349cc55cSDimitry Andric CaseStream.indent(6) << "break;\n } // case " + PrevOp + "\n"; 673349cc55cSDimitry Andric CaseStream.indent(4) << "case " + TargetName + "::" + CurOp + ": {\n"; 674349cc55cSDimitry Andric } 675349cc55cSDimitry Andric 676349cc55cSDimitry Andric std::set<std::pair<bool, StringRef>> FeaturesSet; 677349cc55cSDimitry Andric std::set<std::set<std::pair<bool, StringRef>>> AnyOfFeatureSets; 678349cc55cSDimitry Andric // Add CompressPat required features. 679349cc55cSDimitry Andric getReqFeatures(FeaturesSet, AnyOfFeatureSets, CompressPat.PatReqFeatures); 680349cc55cSDimitry Andric 681349cc55cSDimitry Andric // Add Dest instruction required features. 682349cc55cSDimitry Andric std::vector<Record *> ReqFeatures; 683349cc55cSDimitry Andric std::vector<Record *> RF = Dest.TheDef->getValueAsListOfDefs("Predicates"); 684349cc55cSDimitry Andric copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) { 685349cc55cSDimitry Andric return R->getValueAsBit("AssemblerMatcherPredicate"); 686349cc55cSDimitry Andric }); 687349cc55cSDimitry Andric getReqFeatures(FeaturesSet, AnyOfFeatureSets, ReqFeatures); 688349cc55cSDimitry Andric 689349cc55cSDimitry Andric // Emit checks for all required features. 690349cc55cSDimitry Andric for (auto &Op : FeaturesSet) { 691349cc55cSDimitry Andric StringRef Not = Op.first ? "!" : ""; 692349cc55cSDimitry Andric CondStream.indent(6) << Not << "STI.getFeatureBits()[" << TargetName 693349cc55cSDimitry Andric << "::" << Op.second << "]" 694349cc55cSDimitry Andric << " &&\n"; 695349cc55cSDimitry Andric } 696349cc55cSDimitry Andric 697349cc55cSDimitry Andric // Emit checks for all required feature groups. 698349cc55cSDimitry Andric for (auto &Set : AnyOfFeatureSets) { 699349cc55cSDimitry Andric CondStream.indent(6) << "("; 700349cc55cSDimitry Andric for (auto &Op : Set) { 701349cc55cSDimitry Andric bool isLast = &Op == &*Set.rbegin(); 702349cc55cSDimitry Andric StringRef Not = Op.first ? "!" : ""; 703349cc55cSDimitry Andric CondStream << Not << "STI.getFeatureBits()[" << TargetName 704349cc55cSDimitry Andric << "::" << Op.second << "]"; 705349cc55cSDimitry Andric if (!isLast) 706349cc55cSDimitry Andric CondStream << " || "; 707349cc55cSDimitry Andric } 708349cc55cSDimitry Andric CondStream << ") &&\n"; 709349cc55cSDimitry Andric } 710349cc55cSDimitry Andric 711349cc55cSDimitry Andric // Start Source Inst operands validation. 712349cc55cSDimitry Andric unsigned OpNo = 0; 713349cc55cSDimitry Andric for (OpNo = 0; OpNo < Source.Operands.size(); ++OpNo) { 714349cc55cSDimitry Andric if (SourceOperandMap[OpNo].TiedOpIdx != -1) { 715349cc55cSDimitry Andric if (Source.Operands[OpNo].Rec->isSubClassOf("RegisterClass")) 716349cc55cSDimitry Andric CondStream.indent(6) 717bdd1243dSDimitry Andric << "(MI.getOperand(" << OpNo << ").isReg()) && (MI.getOperand(" 718bdd1243dSDimitry Andric << SourceOperandMap[OpNo].TiedOpIdx << ").isReg()) &&\n" 719bdd1243dSDimitry Andric << " (MI.getOperand(" << OpNo 720bdd1243dSDimitry Andric << ").getReg() == MI.getOperand(" 721349cc55cSDimitry Andric << SourceOperandMap[OpNo].TiedOpIdx << ").getReg()) &&\n"; 722349cc55cSDimitry Andric else 723349cc55cSDimitry Andric PrintFatalError("Unexpected tied operand types!\n"); 724349cc55cSDimitry Andric } 725349cc55cSDimitry Andric // Check for fixed immediates\registers in the source instruction. 726349cc55cSDimitry Andric switch (SourceOperandMap[OpNo].Kind) { 727349cc55cSDimitry Andric case OpData::Operand: 728349cc55cSDimitry Andric // We don't need to do anything for source instruction operand checks. 729349cc55cSDimitry Andric break; 730349cc55cSDimitry Andric case OpData::Imm: 731349cc55cSDimitry Andric CondStream.indent(6) 732349cc55cSDimitry Andric << "(MI.getOperand(" << OpNo << ").isImm()) &&\n" 733349cc55cSDimitry Andric << " (MI.getOperand(" << OpNo 734349cc55cSDimitry Andric << ").getImm() == " << SourceOperandMap[OpNo].Data.Imm << ") &&\n"; 735349cc55cSDimitry Andric break; 736349cc55cSDimitry Andric case OpData::Reg: { 737349cc55cSDimitry Andric Record *Reg = SourceOperandMap[OpNo].Data.Reg; 738349cc55cSDimitry Andric CondStream.indent(6) 739bdd1243dSDimitry Andric << "(MI.getOperand(" << OpNo << ").isReg()) &&\n" 740349cc55cSDimitry Andric << " (MI.getOperand(" << OpNo << ").getReg() == " << TargetName 741349cc55cSDimitry Andric << "::" << Reg->getName() << ") &&\n"; 742349cc55cSDimitry Andric break; 743349cc55cSDimitry Andric } 744349cc55cSDimitry Andric } 745349cc55cSDimitry Andric } 746349cc55cSDimitry Andric CodeStream.indent(6) << "// " << Dest.AsmString << "\n"; 747349cc55cSDimitry Andric if (CompressOrUncompress) 748349cc55cSDimitry Andric CodeStream.indent(6) << "OutInst.setOpcode(" << TargetName 749349cc55cSDimitry Andric << "::" << Dest.TheDef->getName() << ");\n"; 750349cc55cSDimitry Andric OpNo = 0; 751349cc55cSDimitry Andric for (const auto &DestOperand : Dest.Operands) { 752349cc55cSDimitry Andric CodeStream.indent(6) << "// Operand: " << DestOperand.Name << "\n"; 753349cc55cSDimitry Andric switch (DestOperandMap[OpNo].Kind) { 754349cc55cSDimitry Andric case OpData::Operand: { 755349cc55cSDimitry Andric unsigned OpIdx = DestOperandMap[OpNo].Data.Operand; 756349cc55cSDimitry Andric // Check that the operand in the Source instruction fits 757349cc55cSDimitry Andric // the type for the Dest instruction. 758bdd1243dSDimitry Andric if (DestOperand.Rec->isSubClassOf("RegisterClass") || 759bdd1243dSDimitry Andric DestOperand.Rec->isSubClassOf("RegisterOperand")) { 760bdd1243dSDimitry Andric auto *ClassRec = DestOperand.Rec->isSubClassOf("RegisterClass") 761bdd1243dSDimitry Andric ? DestOperand.Rec 762bdd1243dSDimitry Andric : DestOperand.Rec->getValueAsDef("RegClass"); 763349cc55cSDimitry Andric // This is a register operand. Check the register class. 764349cc55cSDimitry Andric // Don't check register class if this is a tied operand, it was done 765349cc55cSDimitry Andric // for the operand its tied to. 766349cc55cSDimitry Andric if (DestOperand.getTiedRegister() == -1) 767bdd1243dSDimitry Andric CondStream.indent(6) 768bdd1243dSDimitry Andric << "(MI.getOperand(" << OpIdx << ").isReg()) &&\n" 769bdd1243dSDimitry Andric << " (" << TargetName << "MCRegisterClasses[" 770bdd1243dSDimitry Andric << TargetName << "::" << ClassRec->getName() 771bdd1243dSDimitry Andric << "RegClassID].contains(MI.getOperand(" << OpIdx 772bdd1243dSDimitry Andric << ").getReg())) &&\n"; 773349cc55cSDimitry Andric 774349cc55cSDimitry Andric if (CompressOrUncompress) 775349cc55cSDimitry Andric CodeStream.indent(6) 776349cc55cSDimitry Andric << "OutInst.addOperand(MI.getOperand(" << OpIdx << "));\n"; 777349cc55cSDimitry Andric } else { 778349cc55cSDimitry Andric // Handling immediate operands. 779349cc55cSDimitry Andric if (CompressOrUncompress) { 780349cc55cSDimitry Andric unsigned Entry = 781349cc55cSDimitry Andric getPredicates(MCOpPredicateMap, MCOpPredicates, DestOperand.Rec, 782349cc55cSDimitry Andric "MCOperandPredicate"); 783349cc55cSDimitry Andric CondStream.indent(6) 784bdd1243dSDimitry Andric << ValidatorName << "(" 785349cc55cSDimitry Andric << "MI.getOperand(" << OpIdx << "), STI, " << Entry << ") &&\n"; 786349cc55cSDimitry Andric } else { 787349cc55cSDimitry Andric unsigned Entry = 788349cc55cSDimitry Andric getPredicates(ImmLeafPredicateMap, ImmLeafPredicates, 789349cc55cSDimitry Andric DestOperand.Rec, "ImmediateCode"); 790349cc55cSDimitry Andric CondStream.indent(6) 791349cc55cSDimitry Andric << "MI.getOperand(" << OpIdx << ").isImm() &&\n"; 792349cc55cSDimitry Andric CondStream.indent(6) << TargetName << "ValidateMachineOperand(" 793349cc55cSDimitry Andric << "MI.getOperand(" << OpIdx 794bdd1243dSDimitry Andric << "), &STI, " << Entry << ") &&\n"; 795349cc55cSDimitry Andric } 796349cc55cSDimitry Andric if (CompressOrUncompress) 797349cc55cSDimitry Andric CodeStream.indent(6) 798349cc55cSDimitry Andric << "OutInst.addOperand(MI.getOperand(" << OpIdx << "));\n"; 799349cc55cSDimitry Andric } 800349cc55cSDimitry Andric break; 801349cc55cSDimitry Andric } 802349cc55cSDimitry Andric case OpData::Imm: { 803349cc55cSDimitry Andric if (CompressOrUncompress) { 804349cc55cSDimitry Andric unsigned Entry = getPredicates(MCOpPredicateMap, MCOpPredicates, 805349cc55cSDimitry Andric DestOperand.Rec, "MCOperandPredicate"); 806349cc55cSDimitry Andric CondStream.indent(6) 807bdd1243dSDimitry Andric << ValidatorName << "(" 808349cc55cSDimitry Andric << "MCOperand::createImm(" << DestOperandMap[OpNo].Data.Imm 809349cc55cSDimitry Andric << "), STI, " << Entry << ") &&\n"; 810349cc55cSDimitry Andric } else { 811349cc55cSDimitry Andric unsigned Entry = getPredicates(ImmLeafPredicateMap, ImmLeafPredicates, 812349cc55cSDimitry Andric DestOperand.Rec, "ImmediateCode"); 813349cc55cSDimitry Andric CondStream.indent(6) 814349cc55cSDimitry Andric << TargetName 815349cc55cSDimitry Andric << "ValidateMachineOperand(MachineOperand::CreateImm(" 816bdd1243dSDimitry Andric << DestOperandMap[OpNo].Data.Imm << "), &STI, " << Entry 817349cc55cSDimitry Andric << ") &&\n"; 818349cc55cSDimitry Andric } 819349cc55cSDimitry Andric if (CompressOrUncompress) 820349cc55cSDimitry Andric CodeStream.indent(6) << "OutInst.addOperand(MCOperand::createImm(" 821349cc55cSDimitry Andric << DestOperandMap[OpNo].Data.Imm << "));\n"; 822349cc55cSDimitry Andric } break; 823349cc55cSDimitry Andric case OpData::Reg: { 824349cc55cSDimitry Andric if (CompressOrUncompress) { 825349cc55cSDimitry Andric // Fixed register has been validated at pattern validation time. 826349cc55cSDimitry Andric Record *Reg = DestOperandMap[OpNo].Data.Reg; 827349cc55cSDimitry Andric CodeStream.indent(6) 828349cc55cSDimitry Andric << "OutInst.addOperand(MCOperand::createReg(" << TargetName 829349cc55cSDimitry Andric << "::" << Reg->getName() << "));\n"; 830349cc55cSDimitry Andric } 831349cc55cSDimitry Andric } break; 832349cc55cSDimitry Andric } 833349cc55cSDimitry Andric ++OpNo; 834349cc55cSDimitry Andric } 835349cc55cSDimitry Andric if (CompressOrUncompress) 836349cc55cSDimitry Andric CodeStream.indent(6) << "OutInst.setLoc(MI.getLoc());\n"; 837349cc55cSDimitry Andric mergeCondAndCode(CaseStream, CondStream.str(), CodeStream.str()); 838349cc55cSDimitry Andric PrevOp = CurOp; 839349cc55cSDimitry Andric } 840349cc55cSDimitry Andric Func << CaseStream.str() << "\n"; 841349cc55cSDimitry Andric // Close brace for the last case. 842349cc55cSDimitry Andric Func.indent(4) << "} // case " << CurOp << "\n"; 843349cc55cSDimitry Andric Func.indent(2) << "} // switch\n"; 844349cc55cSDimitry Andric Func.indent(2) << "return false;\n}\n"; 845349cc55cSDimitry Andric 846349cc55cSDimitry Andric if (!MCOpPredicates.empty()) { 847bdd1243dSDimitry Andric o << "static bool " << ValidatorName << "(const MCOperand &MCOp,\n" 848349cc55cSDimitry Andric << " const MCSubtargetInfo &STI,\n" 849349cc55cSDimitry Andric << " unsigned PredicateIndex) {\n" 850349cc55cSDimitry Andric << " switch (PredicateIndex) {\n" 851349cc55cSDimitry Andric << " default:\n" 852349cc55cSDimitry Andric << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n" 853349cc55cSDimitry Andric << " break;\n"; 854349cc55cSDimitry Andric 855349cc55cSDimitry Andric printPredicates(MCOpPredicates, "MCOperandPredicate", o); 856349cc55cSDimitry Andric 857349cc55cSDimitry Andric o << " }\n" 858349cc55cSDimitry Andric << "}\n\n"; 859349cc55cSDimitry Andric } 860349cc55cSDimitry Andric 861349cc55cSDimitry Andric if (!ImmLeafPredicates.empty()) { 862349cc55cSDimitry Andric o << "static bool " << TargetName 863349cc55cSDimitry Andric << "ValidateMachineOperand(const MachineOperand &MO,\n" 864349cc55cSDimitry Andric << " const " << TargetName << "Subtarget *Subtarget,\n" 865349cc55cSDimitry Andric << " unsigned PredicateIndex) {\n" 866349cc55cSDimitry Andric << " int64_t Imm = MO.getImm();\n" 867349cc55cSDimitry Andric << " switch (PredicateIndex) {\n" 868349cc55cSDimitry Andric << " default:\n" 869349cc55cSDimitry Andric << " llvm_unreachable(\"Unknown ImmLeaf Predicate kind\");\n" 870349cc55cSDimitry Andric << " break;\n"; 871349cc55cSDimitry Andric 872349cc55cSDimitry Andric printPredicates(ImmLeafPredicates, "ImmediateCode", o); 873349cc55cSDimitry Andric 874349cc55cSDimitry Andric o << " }\n" 875349cc55cSDimitry Andric << "}\n\n"; 876349cc55cSDimitry Andric } 877349cc55cSDimitry Andric 878349cc55cSDimitry Andric o << FuncH.str(); 879349cc55cSDimitry Andric o << Func.str(); 880349cc55cSDimitry Andric 881349cc55cSDimitry Andric if (EType == EmitterType::Compress) 882349cc55cSDimitry Andric o << "\n#endif //GEN_COMPRESS_INSTR\n"; 883349cc55cSDimitry Andric else if (EType == EmitterType::Uncompress) 884349cc55cSDimitry Andric o << "\n#endif //GEN_UNCOMPRESS_INSTR\n\n"; 885349cc55cSDimitry Andric else if (EType == EmitterType::CheckCompress) 886349cc55cSDimitry Andric o << "\n#endif //GEN_CHECK_COMPRESS_INSTR\n\n"; 887349cc55cSDimitry Andric } 888349cc55cSDimitry Andric 889349cc55cSDimitry Andric void CompressInstEmitter::run(raw_ostream &o) { 890349cc55cSDimitry Andric std::vector<Record *> Insts = Records.getAllDerivedDefinitions("CompressPat"); 891349cc55cSDimitry Andric 892349cc55cSDimitry Andric // Process the CompressPat definitions, validating them as we do so. 893349cc55cSDimitry Andric for (unsigned i = 0, e = Insts.size(); i != e; ++i) 894349cc55cSDimitry Andric evaluateCompressPat(Insts[i]); 895349cc55cSDimitry Andric 896349cc55cSDimitry Andric // Emit file header. 897349cc55cSDimitry Andric emitSourceFileHeader("Compress instruction Source Fragment", o); 898349cc55cSDimitry Andric // Generate compressInst() function. 899349cc55cSDimitry Andric emitCompressInstEmitter(o, EmitterType::Compress); 900349cc55cSDimitry Andric // Generate uncompressInst() function. 901349cc55cSDimitry Andric emitCompressInstEmitter(o, EmitterType::Uncompress); 902349cc55cSDimitry Andric // Generate isCompressibleInst() function. 903349cc55cSDimitry Andric emitCompressInstEmitter(o, EmitterType::CheckCompress); 904349cc55cSDimitry Andric } 905349cc55cSDimitry Andric 906*06c3fb27SDimitry Andric static TableGen::Emitter::OptClass<CompressInstEmitter> 907*06c3fb27SDimitry Andric X("gen-compress-inst-emitter", "Generate compressed instructions."); 908