1 //===- utils/TableGen/X86EVEX2VEXTablesEmitter.cpp - X86 backend-*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This tablegen backend is responsible for emitting the X86 backend EVEX2VEX
11 /// compression tables.
13 //===----------------------------------------------------------------------===//
15 #include "CodeGenDAGPatterns.h"
16 #include "CodeGenTarget.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/TableGenBackend.h"
24 class X86EVEX2VEXTablesEmitter {
27 // Hold all non-masked & non-broadcasted EVEX encoded instructions
28 std::vector<const CodeGenInstruction *> EVEXInsts;
29 // Hold all VEX encoded instructions. Divided into groups with same opcodes
30 // to make the search more efficient
31 std::map<uint64_t, std::vector<const CodeGenInstruction *>> VEXInsts;
33 typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *> Entry;
35 // Represent both compress tables
36 std::vector<Entry> EVEX2VEX128;
37 std::vector<Entry> EVEX2VEX256;
39 // Represents a manually added entry to the tables
41 const char *EVEXInstStr;
42 const char *VEXInstStr;
47 X86EVEX2VEXTablesEmitter(RecordKeeper &R) : Target(R) {}
49 // run - Output X86 EVEX2VEX tables.
50 void run(raw_ostream &OS);
53 // Prints the given table as a C++ array of type
54 // X86EvexToVexCompressTableEntry
55 void printTable(const std::vector<Entry> &Table, raw_ostream &OS);
57 bool inExceptionList(const CodeGenInstruction *Inst) {
58 // List of EVEX instructions that match VEX instructions by the encoding
59 // but do not perform the same operation.
60 static constexpr const char *ExceptionList[] = {
73 // Instruction's name starts with one of the entries in the exception list
74 for (StringRef InstStr : ExceptionList) {
75 if (Inst->TheDef->getName().startswith(InstStr))
83 void X86EVEX2VEXTablesEmitter::printTable(const std::vector<Entry> &Table,
85 std::string Size = (Table == EVEX2VEX128) ? "128" : "256";
87 OS << "// X86 EVEX encoded instructions that have a VEX " << Size
89 << "// (table format: <EVEX opcode, VEX-" << Size << " opcode>).\n"
90 << "static const X86EvexToVexCompressTableEntry X86EvexToVex" << Size
91 << "CompressTable[] = {\n"
92 << " // EVEX scalar with corresponding VEX.\n";
94 // Print all entries added to the table
95 for (auto Pair : Table) {
96 OS << " { X86::" << Pair.first->TheDef->getName()
97 << ", X86::" << Pair.second->TheDef->getName() << " },\n";
100 // Some VEX instructions were duplicated to multiple EVEX versions due the
101 // introduction of mask variants, and thus some of the EVEX versions have
102 // different encoding than the VEX instruction. In order to maximize the
103 // compression we add these entries manually.
104 static constexpr ManualEntry ManuallyAddedEntries[] = {
105 // EVEX-Inst VEX-Inst Is128-bit
106 {"VMOVDQU8Z128mr", "VMOVDQUmr", true},
107 {"VMOVDQU8Z128rm", "VMOVDQUrm", true},
108 {"VMOVDQU8Z128rr", "VMOVDQUrr", true},
109 {"VMOVDQU8Z128rr_REV", "VMOVDQUrr_REV", true},
110 {"VMOVDQU16Z128mr", "VMOVDQUmr", true},
111 {"VMOVDQU16Z128rm", "VMOVDQUrm", true},
112 {"VMOVDQU16Z128rr", "VMOVDQUrr", true},
113 {"VMOVDQU16Z128rr_REV", "VMOVDQUrr_REV", true},
114 {"VMOVDQU8Z256mr", "VMOVDQUYmr", false},
115 {"VMOVDQU8Z256rm", "VMOVDQUYrm", false},
116 {"VMOVDQU8Z256rr", "VMOVDQUYrr", false},
117 {"VMOVDQU8Z256rr_REV", "VMOVDQUYrr_REV", false},
118 {"VMOVDQU16Z256mr", "VMOVDQUYmr", false},
119 {"VMOVDQU16Z256rm", "VMOVDQUYrm", false},
120 {"VMOVDQU16Z256rr", "VMOVDQUYrr", false},
121 {"VMOVDQU16Z256rr_REV", "VMOVDQUYrr_REV", false},
123 {"VPERMILPDZ128mi", "VPERMILPDmi", true},
124 {"VPERMILPDZ128ri", "VPERMILPDri", true},
125 {"VPERMILPDZ128rm", "VPERMILPDrm", true},
126 {"VPERMILPDZ128rr", "VPERMILPDrr", true},
127 {"VPERMILPDZ256mi", "VPERMILPDYmi", false},
128 {"VPERMILPDZ256ri", "VPERMILPDYri", false},
129 {"VPERMILPDZ256rm", "VPERMILPDYrm", false},
130 {"VPERMILPDZ256rr", "VPERMILPDYrr", false},
132 {"VPBROADCASTQZ128m", "VPBROADCASTQrm", true},
133 {"VPBROADCASTQZ128r", "VPBROADCASTQrr", true},
134 {"VPBROADCASTQZ256m", "VPBROADCASTQYrm", false},
135 {"VPBROADCASTQZ256r", "VPBROADCASTQYrr", false},
137 {"VBROADCASTSDZ256m", "VBROADCASTSDYrm", false},
138 {"VBROADCASTSDZ256r", "VBROADCASTSDYrr", false},
140 {"VEXTRACTF64x2Z256mr", "VEXTRACTF128mr", false},
141 {"VEXTRACTF64x2Z256rr", "VEXTRACTF128rr", false},
142 {"VEXTRACTI64x2Z256mr", "VEXTRACTI128mr", false},
143 {"VEXTRACTI64x2Z256rr", "VEXTRACTI128rr", false},
145 {"VINSERTF64x2Z256rm", "VINSERTF128rm", false},
146 {"VINSERTF64x2Z256rr", "VINSERTF128rr", false},
147 {"VINSERTI64x2Z256rm", "VINSERTI128rm", false},
148 {"VINSERTI64x2Z256rr", "VINSERTI128rr", false}
151 // Print the manually added entries
152 for (const ManualEntry &Entry : ManuallyAddedEntries) {
153 if ((Table == EVEX2VEX128 && Entry.Is128Bit) ||
154 (Table == EVEX2VEX256 && !Entry.Is128Bit)) {
155 OS << " { X86::" << Entry.EVEXInstStr << ", X86::" << Entry.VEXInstStr
163 // Return true if the 2 BitsInits are equal
164 static inline bool equalBitsInits(const BitsInit *B1, const BitsInit *B2) {
165 if (B1->getNumBits() != B2->getNumBits())
166 PrintFatalError("Comparing two BitsInits with different sizes!");
168 for (unsigned i = 0, e = B1->getNumBits(); i != e; ++i) {
169 if (BitInit *Bit1 = dyn_cast<BitInit>(B1->getBit(i))) {
170 if (BitInit *Bit2 = dyn_cast<BitInit>(B2->getBit(i))) {
171 if (Bit1->getValue() != Bit2->getValue())
174 PrintFatalError("Invalid BitsInit bit");
176 PrintFatalError("Invalid BitsInit bit");
181 // Calculates the integer value residing BitsInit object
182 static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
184 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
185 if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
186 Value |= uint64_t(Bit->getValue()) << i;
188 PrintFatalError("Invalid VectSize bit");
193 // Function object - Operator() returns true if the given VEX instruction
194 // matches the EVEX instruction of this object.
196 const CodeGenInstruction *Inst;
199 IsMatch(const CodeGenInstruction *Inst) : Inst(Inst) {}
201 bool operator()(const CodeGenInstruction *Inst2) {
202 Record *Rec1 = Inst->TheDef;
203 Record *Rec2 = Inst2->TheDef;
205 getValueFromBitsInit(Rec1->getValueAsBitsInit("VEX_WPrefix"));
207 getValueFromBitsInit(Rec2->getValueAsBitsInit("VEX_WPrefix"));
209 if (Rec2->getValueAsDef("OpEnc")->getName().str() != "EncVEX" ||
211 Rec2->getValueAsDef("OpPrefix") != Rec1->getValueAsDef("OpPrefix") ||
212 Rec2->getValueAsDef("OpMap") != Rec1->getValueAsDef("OpMap") ||
213 Rec2->getValueAsBit("hasVEX_4V") != Rec1->getValueAsBit("hasVEX_4V") ||
214 !equalBitsInits(Rec2->getValueAsBitsInit("EVEX_LL"),
215 Rec1->getValueAsBitsInit("EVEX_LL")) ||
216 (Rec1WVEX != 2 && Rec2WVEX != 2 && Rec1WVEX != Rec2WVEX) ||
217 // Instruction's format
218 Rec2->getValueAsDef("Form") != Rec1->getValueAsDef("Form") ||
219 Rec2->getValueAsBit("isAsmParserOnly") !=
220 Rec1->getValueAsBit("isAsmParserOnly"))
223 // This is needed for instructions with intrinsic version (_Int).
224 // Where the only difference is the size of the operands.
225 // For example: VUCOMISDZrm and Int_VUCOMISDrm
226 // Also for instructions that their EVEX version was upgraded to work with
227 // k-registers. For example VPCMPEQBrm (xmm output register) and
228 // VPCMPEQBZ128rm (k register output register).
229 for (unsigned i = 0; i < Inst->Operands.size(); i++) {
230 Record *OpRec1 = Inst->Operands[i].Rec;
231 Record *OpRec2 = Inst2->Operands[i].Rec;
233 if (OpRec1 == OpRec2)
236 if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
237 if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
239 } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
241 } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
242 if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type"))
252 static inline bool isRegisterOperand(const Record *Rec) {
253 return Rec->isSubClassOf("RegisterClass") ||
254 Rec->isSubClassOf("RegisterOperand");
257 static inline bool isMemoryOperand(const Record *Rec) {
258 return Rec->isSubClassOf("Operand") &&
259 Rec->getValueAsString("OperandType") == "OPERAND_MEMORY";
262 static inline bool isImmediateOperand(const Record *Rec) {
263 return Rec->isSubClassOf("Operand") &&
264 Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE";
267 static inline unsigned int getRegOperandSize(const Record *RegRec) {
268 if (RegRec->isSubClassOf("RegisterClass"))
269 return RegRec->getValueAsInt("Alignment");
270 if (RegRec->isSubClassOf("RegisterOperand"))
271 return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment");
273 llvm_unreachable("Register operand's size not known!");
277 void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) {
278 emitSourceFileHeader("X86 EVEX2VEX tables", OS);
280 ArrayRef<const CodeGenInstruction *> NumberedInstructions =
281 Target.getInstructionsByEnumValue();
283 for (const CodeGenInstruction *Inst : NumberedInstructions) {
284 // Filter non-X86 instructions.
285 if (!Inst->TheDef->isSubClassOf("X86Inst"))
288 // Add VEX encoded instructions to one of VEXInsts vectors according to
290 if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") {
291 uint64_t Opcode = getValueFromBitsInit(Inst->TheDef->
292 getValueAsBitsInit("Opcode"));
293 VEXInsts[Opcode].push_back(Inst);
295 // Add relevant EVEX encoded instructions to EVEXInsts
296 else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" &&
297 !Inst->TheDef->getValueAsBit("hasEVEX_K") &&
298 !Inst->TheDef->getValueAsBit("hasEVEX_B") &&
299 getValueFromBitsInit(Inst->TheDef->
300 getValueAsBitsInit("EVEX_LL")) != 2 &&
301 !inExceptionList(Inst))
302 EVEXInsts.push_back(Inst);
305 for (const CodeGenInstruction *EVEXInst : EVEXInsts) {
306 uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef->
307 getValueAsBitsInit("Opcode"));
308 // For each EVEX instruction look for a VEX match in the appropriate vector
309 // (instructions with the same opcode) using function object IsMatch.
310 auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst));
311 if (Match != VEXInsts[Opcode].end()) {
312 const CodeGenInstruction *VEXInst = *Match;
314 // In case a match is found add new entry to the appropriate table
315 switch (getValueFromBitsInit(
316 EVEXInst->TheDef->getValueAsBitsInit("EVEX_LL"))) {
318 EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0}
321 EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1}
324 llvm_unreachable("Instruction's size not fit for the mapping!");
330 printTable(EVEX2VEX128, OS);
331 printTable(EVEX2VEX256, OS);
336 void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) {
337 X86EVEX2VEXTablesEmitter(RK).run(OS);