CompressInstEmitter.cpp 39 KB

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