X86EVEX2VEXTablesEmitter.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //===- utils/TableGen/X86EVEX2VEXTablesEmitter.cpp - X86 backend-*- C++ -*-===//
  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. //===----------------------------------------------------------------------===//
  8. ///
  9. /// This tablegen backend is responsible for emitting the X86 backend EVEX2VEX
  10. /// compression tables.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenTarget.h"
  14. #include "llvm/TableGen/Error.h"
  15. #include "llvm/TableGen/TableGenBackend.h"
  16. using namespace llvm;
  17. namespace {
  18. class X86EVEX2VEXTablesEmitter {
  19. RecordKeeper &Records;
  20. CodeGenTarget Target;
  21. // Hold all non-masked & non-broadcasted EVEX encoded instructions
  22. std::vector<const CodeGenInstruction *> EVEXInsts;
  23. // Hold all VEX encoded instructions. Divided into groups with same opcodes
  24. // to make the search more efficient
  25. std::map<uint64_t, std::vector<const CodeGenInstruction *>> VEXInsts;
  26. typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *> Entry;
  27. // Represent both compress tables
  28. std::vector<Entry> EVEX2VEX128;
  29. std::vector<Entry> EVEX2VEX256;
  30. public:
  31. X86EVEX2VEXTablesEmitter(RecordKeeper &R) : Records(R), Target(R) {}
  32. // run - Output X86 EVEX2VEX tables.
  33. void run(raw_ostream &OS);
  34. private:
  35. // Prints the given table as a C++ array of type
  36. // X86EvexToVexCompressTableEntry
  37. void printTable(const std::vector<Entry> &Table, raw_ostream &OS);
  38. };
  39. void X86EVEX2VEXTablesEmitter::printTable(const std::vector<Entry> &Table,
  40. raw_ostream &OS) {
  41. StringRef Size = (Table == EVEX2VEX128) ? "128" : "256";
  42. OS << "// X86 EVEX encoded instructions that have a VEX " << Size
  43. << " encoding\n"
  44. << "// (table format: <EVEX opcode, VEX-" << Size << " opcode>).\n"
  45. << "static const X86EvexToVexCompressTableEntry X86EvexToVex" << Size
  46. << "CompressTable[] = {\n"
  47. << " // EVEX scalar with corresponding VEX.\n";
  48. // Print all entries added to the table
  49. for (auto Pair : Table) {
  50. OS << " { X86::" << Pair.first->TheDef->getName()
  51. << ", X86::" << Pair.second->TheDef->getName() << " },\n";
  52. }
  53. OS << "};\n\n";
  54. }
  55. // Return true if the 2 BitsInits are equal
  56. // Calculates the integer value residing BitsInit object
  57. static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
  58. uint64_t Value = 0;
  59. for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
  60. if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
  61. Value |= uint64_t(Bit->getValue()) << i;
  62. else
  63. PrintFatalError("Invalid VectSize bit");
  64. }
  65. return Value;
  66. }
  67. // Function object - Operator() returns true if the given VEX instruction
  68. // matches the EVEX instruction of this object.
  69. class IsMatch {
  70. const CodeGenInstruction *EVEXInst;
  71. public:
  72. IsMatch(const CodeGenInstruction *EVEXInst) : EVEXInst(EVEXInst) {}
  73. bool operator()(const CodeGenInstruction *VEXInst) {
  74. Record *RecE = EVEXInst->TheDef;
  75. Record *RecV = VEXInst->TheDef;
  76. bool EVEX_W = RecE->getValueAsBit("HasVEX_W");
  77. bool VEX_W = RecV->getValueAsBit("HasVEX_W");
  78. bool VEX_WIG = RecV->getValueAsBit("IgnoresVEX_W");
  79. bool EVEX_WIG = RecE->getValueAsBit("IgnoresVEX_W");
  80. bool EVEX_W1_VEX_W0 = RecE->getValueAsBit("EVEX_W1_VEX_W0");
  81. if (RecV->getValueAsDef("OpEnc")->getName().str() != "EncVEX" ||
  82. RecV->getValueAsBit("isCodeGenOnly") != RecE->getValueAsBit("isCodeGenOnly") ||
  83. // VEX/EVEX fields
  84. RecV->getValueAsDef("OpPrefix") != RecE->getValueAsDef("OpPrefix") ||
  85. RecV->getValueAsDef("OpMap") != RecE->getValueAsDef("OpMap") ||
  86. RecV->getValueAsBit("hasVEX_4V") != RecE->getValueAsBit("hasVEX_4V") ||
  87. RecV->getValueAsBit("hasEVEX_L2") != RecE->getValueAsBit("hasEVEX_L2") ||
  88. RecV->getValueAsBit("hasVEX_L") != RecE->getValueAsBit("hasVEX_L") ||
  89. // Match is allowed if either is VEX_WIG, or they match, or EVEX
  90. // is VEX_W1X and VEX is VEX_W0.
  91. (!(VEX_WIG || (!EVEX_WIG && EVEX_W == VEX_W) ||
  92. (EVEX_W1_VEX_W0 && EVEX_W && !VEX_W))) ||
  93. // Instruction's format
  94. RecV->getValueAsDef("Form") != RecE->getValueAsDef("Form"))
  95. return false;
  96. // This is needed for instructions with intrinsic version (_Int).
  97. // Where the only difference is the size of the operands.
  98. // For example: VUCOMISDZrm and Int_VUCOMISDrm
  99. // Also for instructions that their EVEX version was upgraded to work with
  100. // k-registers. For example VPCMPEQBrm (xmm output register) and
  101. // VPCMPEQBZ128rm (k register output register).
  102. for (unsigned i = 0, e = EVEXInst->Operands.size(); i < e; i++) {
  103. Record *OpRec1 = EVEXInst->Operands[i].Rec;
  104. Record *OpRec2 = VEXInst->Operands[i].Rec;
  105. if (OpRec1 == OpRec2)
  106. continue;
  107. if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
  108. if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
  109. return false;
  110. } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
  111. return false;
  112. } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
  113. if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type")) {
  114. return false;
  115. }
  116. } else
  117. return false;
  118. }
  119. return true;
  120. }
  121. private:
  122. static inline bool isRegisterOperand(const Record *Rec) {
  123. return Rec->isSubClassOf("RegisterClass") ||
  124. Rec->isSubClassOf("RegisterOperand");
  125. }
  126. static inline bool isMemoryOperand(const Record *Rec) {
  127. return Rec->isSubClassOf("Operand") &&
  128. Rec->getValueAsString("OperandType") == "OPERAND_MEMORY";
  129. }
  130. static inline bool isImmediateOperand(const Record *Rec) {
  131. return Rec->isSubClassOf("Operand") &&
  132. Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE";
  133. }
  134. static inline unsigned int getRegOperandSize(const Record *RegRec) {
  135. if (RegRec->isSubClassOf("RegisterClass"))
  136. return RegRec->getValueAsInt("Alignment");
  137. if (RegRec->isSubClassOf("RegisterOperand"))
  138. return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment");
  139. llvm_unreachable("Register operand's size not known!");
  140. }
  141. };
  142. void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) {
  143. emitSourceFileHeader("X86 EVEX2VEX tables", OS);
  144. ArrayRef<const CodeGenInstruction *> NumberedInstructions =
  145. Target.getInstructionsByEnumValue();
  146. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  147. // Filter non-X86 instructions.
  148. if (!Inst->TheDef->isSubClassOf("X86Inst"))
  149. continue;
  150. // Add VEX encoded instructions to one of VEXInsts vectors according to
  151. // it's opcode.
  152. if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") {
  153. uint64_t Opcode = getValueFromBitsInit(Inst->TheDef->
  154. getValueAsBitsInit("Opcode"));
  155. VEXInsts[Opcode].push_back(Inst);
  156. }
  157. // Add relevant EVEX encoded instructions to EVEXInsts
  158. else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" &&
  159. !Inst->TheDef->getValueAsBit("hasEVEX_K") &&
  160. !Inst->TheDef->getValueAsBit("hasEVEX_B") &&
  161. !Inst->TheDef->getValueAsBit("hasEVEX_L2") &&
  162. !Inst->TheDef->getValueAsBit("notEVEX2VEXConvertible"))
  163. EVEXInsts.push_back(Inst);
  164. }
  165. for (const CodeGenInstruction *EVEXInst : EVEXInsts) {
  166. uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef->
  167. getValueAsBitsInit("Opcode"));
  168. // For each EVEX instruction look for a VEX match in the appropriate vector
  169. // (instructions with the same opcode) using function object IsMatch.
  170. // Allow EVEX2VEXOverride to explicitly specify a match.
  171. const CodeGenInstruction *VEXInst = nullptr;
  172. if (!EVEXInst->TheDef->isValueUnset("EVEX2VEXOverride")) {
  173. StringRef AltInstStr =
  174. EVEXInst->TheDef->getValueAsString("EVEX2VEXOverride");
  175. Record *AltInstRec = Records.getDef(AltInstStr);
  176. assert(AltInstRec && "EVEX2VEXOverride instruction not found!");
  177. VEXInst = &Target.getInstruction(AltInstRec);
  178. } else {
  179. auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst));
  180. if (Match != VEXInsts[Opcode].end())
  181. VEXInst = *Match;
  182. }
  183. if (!VEXInst)
  184. continue;
  185. // In case a match is found add new entry to the appropriate table
  186. if (EVEXInst->TheDef->getValueAsBit("hasVEX_L"))
  187. EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1}
  188. else
  189. EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0}
  190. }
  191. // Print both tables
  192. printTable(EVEX2VEX128, OS);
  193. printTable(EVEX2VEX256, OS);
  194. }
  195. }
  196. namespace llvm {
  197. void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) {
  198. X86EVEX2VEXTablesEmitter(RK).run(OS);
  199. }
  200. }