X86EVEX2VEXTablesEmitter.cpp 9.1 KB

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