blob: 7fc8cda65dad16ca66014af3483645b4688dca56 [file] [log] [blame]
Ayman Musa850fc972017-03-07 08:11:19 +00001//===- utils/TableGen/X86EVEX2VEXTablesEmitter.cpp - X86 backend-*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// This tablegen backend is responsible for emitting the X86 backend EVEX2VEX
11/// compression tables.
12///
13//===----------------------------------------------------------------------===//
14
15#include "CodeGenDAGPatterns.h"
16#include "CodeGenTarget.h"
17#include "llvm/TableGen/Error.h"
18#include "llvm/TableGen/TableGenBackend.h"
19
20using namespace llvm;
21
22namespace {
23
24class X86EVEX2VEXTablesEmitter {
Ayman Musa850fc972017-03-07 08:11:19 +000025 CodeGenTarget Target;
26
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;
32
33 typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *> Entry;
34
35 // Represent both compress tables
36 std::vector<Entry> EVEX2VEX128;
37 std::vector<Entry> EVEX2VEX256;
38
39 // Represents a manually added entry to the tables
Benjamin Kramerc06d6722017-03-24 14:11:47 +000040 struct ManualEntry {
Benjamin Kramerdd0620e2017-03-24 14:17:56 +000041 const char *EVEXInstStr;
42 const char *VEXInstStr;
Ayman Musa850fc972017-03-07 08:11:19 +000043 bool Is128Bit;
Ayman Musa850fc972017-03-07 08:11:19 +000044 };
45
46public:
Ayman Musa63cfb162017-03-07 08:56:27 +000047 X86EVEX2VEXTablesEmitter(RecordKeeper &R) : Target(R) {}
Ayman Musa850fc972017-03-07 08:11:19 +000048
49 // run - Output X86 EVEX2VEX tables.
50 void run(raw_ostream &OS);
51
52private:
53 // Prints the given table as a C++ array of type
54 // X86EvexToVexCompressTableEntry
55 void printTable(const std::vector<Entry> &Table, raw_ostream &OS);
56
Ayman Musa850fc972017-03-07 08:11:19 +000057 bool inExceptionList(const CodeGenInstruction *Inst) {
Benjamin Kramerc06d6722017-03-24 14:11:47 +000058 // List of EVEX instructions that match VEX instructions by the encoding
59 // but do not perform the same operation.
Benjamin Kramer46f5e2c2017-03-24 14:15:35 +000060 static constexpr const char *ExceptionList[] = {
Benjamin Kramerc06d6722017-03-24 14:11:47 +000061 "VCVTQQ2PD",
62 "VCVTQQ2PS",
63 "VPMAXSQ",
64 "VPMAXUQ",
65 "VPMINSQ",
66 "VPMINUQ",
67 "VPMULLQ",
68 "VPSRAQ",
69 "VDBPSADBW",
Benjamin Kramerc06d6722017-03-24 14:11:47 +000070 "VSCALEFPS"
71 };
Ayman Musa850fc972017-03-07 08:11:19 +000072 // Instruction's name starts with one of the entries in the exception list
Benjamin Kramerc06d6722017-03-24 14:11:47 +000073 for (StringRef InstStr : ExceptionList) {
Ayman Musa850fc972017-03-07 08:11:19 +000074 if (Inst->TheDef->getName().startswith(InstStr))
75 return true;
76 }
77 return false;
78 }
79
Ayman Musa850fc972017-03-07 08:11:19 +000080};
81
82void X86EVEX2VEXTablesEmitter::printTable(const std::vector<Entry> &Table,
83 raw_ostream &OS) {
84 std::string Size = (Table == EVEX2VEX128) ? "128" : "256";
85
86 OS << "// X86 EVEX encoded instructions that have a VEX " << Size
87 << " encoding\n"
88 << "// (table format: <EVEX opcode, VEX-" << Size << " opcode>).\n"
89 << "static const X86EvexToVexCompressTableEntry X86EvexToVex" << Size
90 << "CompressTable[] = {\n"
91 << " // EVEX scalar with corresponding VEX.\n";
92
93 // Print all entries added to the table
94 for (auto Pair : Table) {
Craig Topper9fc41352017-03-13 00:36:46 +000095 OS << " { X86::" << Pair.first->TheDef->getName()
Ayman Musa850fc972017-03-07 08:11:19 +000096 << ", X86::" << Pair.second->TheDef->getName() << " },\n";
97 }
98
Benjamin Kramerc06d6722017-03-24 14:11:47 +000099 // Some VEX instructions were duplicated to multiple EVEX versions due the
100 // introduction of mask variants, and thus some of the EVEX versions have
101 // different encoding than the VEX instruction. In order to maximize the
102 // compression we add these entries manually.
103 static constexpr ManualEntry ManuallyAddedEntries[] = {
104 // EVEX-Inst VEX-Inst Is128-bit
105 {"VMOVDQU8Z128mr", "VMOVDQUmr", true},
106 {"VMOVDQU8Z128rm", "VMOVDQUrm", true},
107 {"VMOVDQU8Z128rr", "VMOVDQUrr", true},
108 {"VMOVDQU8Z128rr_REV", "VMOVDQUrr_REV", true},
109 {"VMOVDQU16Z128mr", "VMOVDQUmr", true},
110 {"VMOVDQU16Z128rm", "VMOVDQUrm", true},
111 {"VMOVDQU16Z128rr", "VMOVDQUrr", true},
112 {"VMOVDQU16Z128rr_REV", "VMOVDQUrr_REV", true},
113 {"VMOVDQU8Z256mr", "VMOVDQUYmr", false},
114 {"VMOVDQU8Z256rm", "VMOVDQUYrm", false},
115 {"VMOVDQU8Z256rr", "VMOVDQUYrr", false},
116 {"VMOVDQU8Z256rr_REV", "VMOVDQUYrr_REV", false},
117 {"VMOVDQU16Z256mr", "VMOVDQUYmr", false},
118 {"VMOVDQU16Z256rm", "VMOVDQUYrm", false},
119 {"VMOVDQU16Z256rr", "VMOVDQUYrr", false},
120 {"VMOVDQU16Z256rr_REV", "VMOVDQUYrr_REV", false},
121
122 {"VPERMILPDZ128mi", "VPERMILPDmi", true},
123 {"VPERMILPDZ128ri", "VPERMILPDri", true},
124 {"VPERMILPDZ128rm", "VPERMILPDrm", true},
125 {"VPERMILPDZ128rr", "VPERMILPDrr", true},
126 {"VPERMILPDZ256mi", "VPERMILPDYmi", false},
127 {"VPERMILPDZ256ri", "VPERMILPDYri", false},
128 {"VPERMILPDZ256rm", "VPERMILPDYrm", false},
129 {"VPERMILPDZ256rr", "VPERMILPDYrr", false},
130
131 {"VPBROADCASTQZ128m", "VPBROADCASTQrm", true},
132 {"VPBROADCASTQZ128r", "VPBROADCASTQrr", true},
133 {"VPBROADCASTQZ256m", "VPBROADCASTQYrm", false},
134 {"VPBROADCASTQZ256r", "VPBROADCASTQYrr", false},
135
136 {"VBROADCASTSDZ256m", "VBROADCASTSDYrm", false},
137 {"VBROADCASTSDZ256r", "VBROADCASTSDYrr", false},
138
Craig Topper485cca12017-08-21 05:03:28 +0000139 {"VBROADCASTF64X2Z128rm", "VBROADCASTF128", false},
140 {"VBROADCASTI64X2Z128rm", "VBROADCASTI128", false},
141
Benjamin Kramerc06d6722017-03-24 14:11:47 +0000142 {"VEXTRACTF64x2Z256mr", "VEXTRACTF128mr", false},
143 {"VEXTRACTF64x2Z256rr", "VEXTRACTF128rr", false},
144 {"VEXTRACTI64x2Z256mr", "VEXTRACTI128mr", false},
145 {"VEXTRACTI64x2Z256rr", "VEXTRACTI128rr", false},
146
147 {"VINSERTF64x2Z256rm", "VINSERTF128rm", false},
148 {"VINSERTF64x2Z256rr", "VINSERTF128rr", false},
149 {"VINSERTI64x2Z256rm", "VINSERTI128rm", false},
Craig Topper4e56ba22017-11-01 21:00:59 +0000150 {"VINSERTI64x2Z256rr", "VINSERTI128rr", false},
151
152 // These will require some custom adjustment in the conversion pass.
153 {"VALIGNDZ128rri", "VPALIGNRrri", true},
154 {"VALIGNQZ128rri", "VPALIGNRrri", true},
155 {"VALIGNDZ128rmi", "VPALIGNRrmi", true},
156 {"VALIGNQZ128rmi", "VPALIGNRrmi", true},
Craig Toppere5d44ce2017-11-04 18:10:03 +0000157 {"VSHUFF32X4Z256rmi", "VPERM2F128rm", false},
158 {"VSHUFF32X4Z256rri", "VPERM2F128rr", false},
159 {"VSHUFF64X2Z256rmi", "VPERM2F128rm", false},
160 {"VSHUFF64X2Z256rri", "VPERM2F128rr", false},
161 {"VSHUFI32X4Z256rmi", "VPERM2I128rm", false},
162 {"VSHUFI32X4Z256rri", "VPERM2I128rr", false},
163 {"VSHUFI64X2Z256rmi", "VPERM2I128rm", false},
164 {"VSHUFI64X2Z256rri", "VPERM2I128rr", false},
Craig Topperdf99baa2018-02-13 04:19:26 +0000165
166 // These can be replaced if we verify the scale part of the immediate is
167 // zero.
168 {"VRNDSCALEPDZ128rri", "VROUNDPDr", true},
169 {"VRNDSCALEPDZ128rmi", "VROUNDPDm", true},
170 {"VRNDSCALEPSZ128rri", "VROUNDPSr", true},
171 {"VRNDSCALEPSZ128rmi", "VROUNDPSm", true},
Craig Topper40d3b322018-03-22 21:55:20 +0000172 {"VRNDSCALEPDZ256rri", "VROUNDPDYr", false},
173 {"VRNDSCALEPDZ256rmi", "VROUNDPDYm", false},
174 {"VRNDSCALEPSZ256rri", "VROUNDPSYr", false},
175 {"VRNDSCALEPSZ256rmi", "VROUNDPSYm", false},
Craig Topperdf99baa2018-02-13 04:19:26 +0000176 {"VRNDSCALESDr", "VROUNDSDr", true},
177 {"VRNDSCALESDm", "VROUNDSDm", true},
178 {"VRNDSCALESSr", "VROUNDSSr", true},
179 {"VRNDSCALESSm", "VROUNDSSm", true},
180 {"VRNDSCALESDr_Int", "VROUNDSDr_Int", true},
181 {"VRNDSCALESDm_Int", "VROUNDSDm_Int", true},
182 {"VRNDSCALESSr_Int", "VROUNDSSr_Int", true},
183 {"VRNDSCALESSm_Int", "VROUNDSSm_Int", true},
Benjamin Kramerc06d6722017-03-24 14:11:47 +0000184 };
185
Ayman Musa850fc972017-03-07 08:11:19 +0000186 // Print the manually added entries
187 for (const ManualEntry &Entry : ManuallyAddedEntries) {
188 if ((Table == EVEX2VEX128 && Entry.Is128Bit) ||
189 (Table == EVEX2VEX256 && !Entry.Is128Bit)) {
Craig Topper9fc41352017-03-13 00:36:46 +0000190 OS << " { X86::" << Entry.EVEXInstStr << ", X86::" << Entry.VEXInstStr
Ayman Musa850fc972017-03-07 08:11:19 +0000191 << " },\n";
192 }
193 }
194
195 OS << "};\n\n";
196}
197
198// Return true if the 2 BitsInits are equal
199static inline bool equalBitsInits(const BitsInit *B1, const BitsInit *B2) {
200 if (B1->getNumBits() != B2->getNumBits())
201 PrintFatalError("Comparing two BitsInits with different sizes!");
202
203 for (unsigned i = 0, e = B1->getNumBits(); i != e; ++i) {
204 if (BitInit *Bit1 = dyn_cast<BitInit>(B1->getBit(i))) {
205 if (BitInit *Bit2 = dyn_cast<BitInit>(B2->getBit(i))) {
206 if (Bit1->getValue() != Bit2->getValue())
207 return false;
208 } else
209 PrintFatalError("Invalid BitsInit bit");
210 } else
211 PrintFatalError("Invalid BitsInit bit");
212 }
213 return true;
214}
215
216// Calculates the integer value residing BitsInit object
217static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
218 uint64_t Value = 0;
219 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
220 if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
221 Value |= uint64_t(Bit->getValue()) << i;
222 else
223 PrintFatalError("Invalid VectSize bit");
224 }
225 return Value;
226}
227
228// Function object - Operator() returns true if the given VEX instruction
229// matches the EVEX instruction of this object.
230class IsMatch {
231 const CodeGenInstruction *Inst;
232
233public:
234 IsMatch(const CodeGenInstruction *Inst) : Inst(Inst) {}
235
236 bool operator()(const CodeGenInstruction *Inst2) {
237 Record *Rec1 = Inst->TheDef;
238 Record *Rec2 = Inst2->TheDef;
239 uint64_t Rec1WVEX =
240 getValueFromBitsInit(Rec1->getValueAsBitsInit("VEX_WPrefix"));
241 uint64_t Rec2WVEX =
242 getValueFromBitsInit(Rec2->getValueAsBitsInit("VEX_WPrefix"));
243
244 if (Rec2->getValueAsDef("OpEnc")->getName().str() != "EncVEX" ||
245 // VEX/EVEX fields
246 Rec2->getValueAsDef("OpPrefix") != Rec1->getValueAsDef("OpPrefix") ||
247 Rec2->getValueAsDef("OpMap") != Rec1->getValueAsDef("OpMap") ||
248 Rec2->getValueAsBit("hasVEX_4V") != Rec1->getValueAsBit("hasVEX_4V") ||
249 !equalBitsInits(Rec2->getValueAsBitsInit("EVEX_LL"),
250 Rec1->getValueAsBitsInit("EVEX_LL")) ||
251 (Rec1WVEX != 2 && Rec2WVEX != 2 && Rec1WVEX != Rec2WVEX) ||
252 // Instruction's format
253 Rec2->getValueAsDef("Form") != Rec1->getValueAsDef("Form") ||
254 Rec2->getValueAsBit("isAsmParserOnly") !=
255 Rec1->getValueAsBit("isAsmParserOnly"))
256 return false;
257
258 // This is needed for instructions with intrinsic version (_Int).
259 // Where the only difference is the size of the operands.
260 // For example: VUCOMISDZrm and Int_VUCOMISDrm
261 // Also for instructions that their EVEX version was upgraded to work with
262 // k-registers. For example VPCMPEQBrm (xmm output register) and
263 // VPCMPEQBZ128rm (k register output register).
264 for (unsigned i = 0; i < Inst->Operands.size(); i++) {
265 Record *OpRec1 = Inst->Operands[i].Rec;
266 Record *OpRec2 = Inst2->Operands[i].Rec;
267
268 if (OpRec1 == OpRec2)
269 continue;
270
271 if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
272 if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
273 return false;
274 } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
Craig Topperbb4089d22017-03-13 05:34:03 +0000275 return false;
Ayman Musa850fc972017-03-07 08:11:19 +0000276 } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
277 if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type"))
278 return false;
279 } else
280 return false;
281 }
282
283 return true;
284 }
285
286private:
287 static inline bool isRegisterOperand(const Record *Rec) {
288 return Rec->isSubClassOf("RegisterClass") ||
289 Rec->isSubClassOf("RegisterOperand");
290 }
291
292 static inline bool isMemoryOperand(const Record *Rec) {
293 return Rec->isSubClassOf("Operand") &&
294 Rec->getValueAsString("OperandType") == "OPERAND_MEMORY";
295 }
296
297 static inline bool isImmediateOperand(const Record *Rec) {
298 return Rec->isSubClassOf("Operand") &&
299 Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE";
300 }
301
302 static inline unsigned int getRegOperandSize(const Record *RegRec) {
303 if (RegRec->isSubClassOf("RegisterClass"))
304 return RegRec->getValueAsInt("Alignment");
305 if (RegRec->isSubClassOf("RegisterOperand"))
306 return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment");
307
308 llvm_unreachable("Register operand's size not known!");
309 }
310};
311
312void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) {
313 emitSourceFileHeader("X86 EVEX2VEX tables", OS);
314
315 ArrayRef<const CodeGenInstruction *> NumberedInstructions =
316 Target.getInstructionsByEnumValue();
317
318 for (const CodeGenInstruction *Inst : NumberedInstructions) {
319 // Filter non-X86 instructions.
320 if (!Inst->TheDef->isSubClassOf("X86Inst"))
321 continue;
322
323 // Add VEX encoded instructions to one of VEXInsts vectors according to
324 // it's opcode.
325 if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") {
326 uint64_t Opcode = getValueFromBitsInit(Inst->TheDef->
327 getValueAsBitsInit("Opcode"));
328 VEXInsts[Opcode].push_back(Inst);
329 }
330 // Add relevant EVEX encoded instructions to EVEXInsts
331 else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" &&
332 !Inst->TheDef->getValueAsBit("hasEVEX_K") &&
333 !Inst->TheDef->getValueAsBit("hasEVEX_B") &&
334 getValueFromBitsInit(Inst->TheDef->
335 getValueAsBitsInit("EVEX_LL")) != 2 &&
336 !inExceptionList(Inst))
337 EVEXInsts.push_back(Inst);
338 }
339
340 for (const CodeGenInstruction *EVEXInst : EVEXInsts) {
341 uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef->
342 getValueAsBitsInit("Opcode"));
343 // For each EVEX instruction look for a VEX match in the appropriate vector
344 // (instructions with the same opcode) using function object IsMatch.
345 auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst));
346 if (Match != VEXInsts[Opcode].end()) {
347 const CodeGenInstruction *VEXInst = *Match;
348
349 // In case a match is found add new entry to the appropriate table
350 switch (getValueFromBitsInit(
351 EVEXInst->TheDef->getValueAsBitsInit("EVEX_LL"))) {
352 case 0:
353 EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0}
354 break;
355 case 1:
356 EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1}
357 break;
358 default:
359 llvm_unreachable("Instruction's size not fit for the mapping!");
360 }
361 }
362 }
363
364 // Print both tables
365 printTable(EVEX2VEX128, OS);
366 printTable(EVEX2VEX256, OS);
367}
368}
369
370namespace llvm {
371void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) {
372 X86EVEX2VEXTablesEmitter(RK).run(OS);
373}
374}