blob: 848e59c079039c9aceadb43482223111b4579dd4 [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",
70 "VRNDSCALE",
71 "VSCALEFPS"
72 };
Ayman Musa850fc972017-03-07 08:11:19 +000073 // Instruction's name starts with one of the entries in the exception list
Benjamin Kramerc06d6722017-03-24 14:11:47 +000074 for (StringRef InstStr : ExceptionList) {
Ayman Musa850fc972017-03-07 08:11:19 +000075 if (Inst->TheDef->getName().startswith(InstStr))
76 return true;
77 }
78 return false;
79 }
80
Ayman Musa850fc972017-03-07 08:11:19 +000081};
82
83void X86EVEX2VEXTablesEmitter::printTable(const std::vector<Entry> &Table,
84 raw_ostream &OS) {
85 std::string Size = (Table == EVEX2VEX128) ? "128" : "256";
86
87 OS << "// X86 EVEX encoded instructions that have a VEX " << Size
88 << " encoding\n"
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";
93
94 // Print all entries added to the table
95 for (auto Pair : Table) {
Craig Topper9fc41352017-03-13 00:36:46 +000096 OS << " { X86::" << Pair.first->TheDef->getName()
Ayman Musa850fc972017-03-07 08:11:19 +000097 << ", X86::" << Pair.second->TheDef->getName() << " },\n";
98 }
99
Benjamin Kramerc06d6722017-03-24 14:11:47 +0000100 // 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},
122
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},
131
132 {"VPBROADCASTQZ128m", "VPBROADCASTQrm", true},
133 {"VPBROADCASTQZ128r", "VPBROADCASTQrr", true},
134 {"VPBROADCASTQZ256m", "VPBROADCASTQYrm", false},
135 {"VPBROADCASTQZ256r", "VPBROADCASTQYrr", false},
136
137 {"VBROADCASTSDZ256m", "VBROADCASTSDYrm", false},
138 {"VBROADCASTSDZ256r", "VBROADCASTSDYrr", false},
139
Craig Topper485cca12017-08-21 05:03:28 +0000140 {"VBROADCASTF64X2Z128rm", "VBROADCASTF128", false},
141 {"VBROADCASTI64X2Z128rm", "VBROADCASTI128", false},
142
Benjamin Kramerc06d6722017-03-24 14:11:47 +0000143 {"VEXTRACTF64x2Z256mr", "VEXTRACTF128mr", false},
144 {"VEXTRACTF64x2Z256rr", "VEXTRACTF128rr", false},
145 {"VEXTRACTI64x2Z256mr", "VEXTRACTI128mr", false},
146 {"VEXTRACTI64x2Z256rr", "VEXTRACTI128rr", false},
147
148 {"VINSERTF64x2Z256rm", "VINSERTF128rm", false},
149 {"VINSERTF64x2Z256rr", "VINSERTF128rr", false},
150 {"VINSERTI64x2Z256rm", "VINSERTI128rm", false},
Craig Topper4e56ba22017-11-01 21:00:59 +0000151 {"VINSERTI64x2Z256rr", "VINSERTI128rr", false},
152
153 // These will require some custom adjustment in the conversion pass.
154 {"VALIGNDZ128rri", "VPALIGNRrri", true},
155 {"VALIGNQZ128rri", "VPALIGNRrri", true},
156 {"VALIGNDZ128rmi", "VPALIGNRrmi", true},
157 {"VALIGNQZ128rmi", "VPALIGNRrmi", true},
Benjamin Kramerc06d6722017-03-24 14:11:47 +0000158 };
159
Ayman Musa850fc972017-03-07 08:11:19 +0000160 // Print the manually added entries
161 for (const ManualEntry &Entry : ManuallyAddedEntries) {
162 if ((Table == EVEX2VEX128 && Entry.Is128Bit) ||
163 (Table == EVEX2VEX256 && !Entry.Is128Bit)) {
Craig Topper9fc41352017-03-13 00:36:46 +0000164 OS << " { X86::" << Entry.EVEXInstStr << ", X86::" << Entry.VEXInstStr
Ayman Musa850fc972017-03-07 08:11:19 +0000165 << " },\n";
166 }
167 }
168
169 OS << "};\n\n";
170}
171
172// Return true if the 2 BitsInits are equal
173static inline bool equalBitsInits(const BitsInit *B1, const BitsInit *B2) {
174 if (B1->getNumBits() != B2->getNumBits())
175 PrintFatalError("Comparing two BitsInits with different sizes!");
176
177 for (unsigned i = 0, e = B1->getNumBits(); i != e; ++i) {
178 if (BitInit *Bit1 = dyn_cast<BitInit>(B1->getBit(i))) {
179 if (BitInit *Bit2 = dyn_cast<BitInit>(B2->getBit(i))) {
180 if (Bit1->getValue() != Bit2->getValue())
181 return false;
182 } else
183 PrintFatalError("Invalid BitsInit bit");
184 } else
185 PrintFatalError("Invalid BitsInit bit");
186 }
187 return true;
188}
189
190// Calculates the integer value residing BitsInit object
191static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
192 uint64_t Value = 0;
193 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
194 if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
195 Value |= uint64_t(Bit->getValue()) << i;
196 else
197 PrintFatalError("Invalid VectSize bit");
198 }
199 return Value;
200}
201
202// Function object - Operator() returns true if the given VEX instruction
203// matches the EVEX instruction of this object.
204class IsMatch {
205 const CodeGenInstruction *Inst;
206
207public:
208 IsMatch(const CodeGenInstruction *Inst) : Inst(Inst) {}
209
210 bool operator()(const CodeGenInstruction *Inst2) {
211 Record *Rec1 = Inst->TheDef;
212 Record *Rec2 = Inst2->TheDef;
213 uint64_t Rec1WVEX =
214 getValueFromBitsInit(Rec1->getValueAsBitsInit("VEX_WPrefix"));
215 uint64_t Rec2WVEX =
216 getValueFromBitsInit(Rec2->getValueAsBitsInit("VEX_WPrefix"));
217
218 if (Rec2->getValueAsDef("OpEnc")->getName().str() != "EncVEX" ||
219 // VEX/EVEX fields
220 Rec2->getValueAsDef("OpPrefix") != Rec1->getValueAsDef("OpPrefix") ||
221 Rec2->getValueAsDef("OpMap") != Rec1->getValueAsDef("OpMap") ||
222 Rec2->getValueAsBit("hasVEX_4V") != Rec1->getValueAsBit("hasVEX_4V") ||
223 !equalBitsInits(Rec2->getValueAsBitsInit("EVEX_LL"),
224 Rec1->getValueAsBitsInit("EVEX_LL")) ||
225 (Rec1WVEX != 2 && Rec2WVEX != 2 && Rec1WVEX != Rec2WVEX) ||
226 // Instruction's format
227 Rec2->getValueAsDef("Form") != Rec1->getValueAsDef("Form") ||
228 Rec2->getValueAsBit("isAsmParserOnly") !=
229 Rec1->getValueAsBit("isAsmParserOnly"))
230 return false;
231
232 // This is needed for instructions with intrinsic version (_Int).
233 // Where the only difference is the size of the operands.
234 // For example: VUCOMISDZrm and Int_VUCOMISDrm
235 // Also for instructions that their EVEX version was upgraded to work with
236 // k-registers. For example VPCMPEQBrm (xmm output register) and
237 // VPCMPEQBZ128rm (k register output register).
238 for (unsigned i = 0; i < Inst->Operands.size(); i++) {
239 Record *OpRec1 = Inst->Operands[i].Rec;
240 Record *OpRec2 = Inst2->Operands[i].Rec;
241
242 if (OpRec1 == OpRec2)
243 continue;
244
245 if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
246 if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
247 return false;
248 } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
Craig Topperbb4089d22017-03-13 05:34:03 +0000249 return false;
Ayman Musa850fc972017-03-07 08:11:19 +0000250 } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
251 if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type"))
252 return false;
253 } else
254 return false;
255 }
256
257 return true;
258 }
259
260private:
261 static inline bool isRegisterOperand(const Record *Rec) {
262 return Rec->isSubClassOf("RegisterClass") ||
263 Rec->isSubClassOf("RegisterOperand");
264 }
265
266 static inline bool isMemoryOperand(const Record *Rec) {
267 return Rec->isSubClassOf("Operand") &&
268 Rec->getValueAsString("OperandType") == "OPERAND_MEMORY";
269 }
270
271 static inline bool isImmediateOperand(const Record *Rec) {
272 return Rec->isSubClassOf("Operand") &&
273 Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE";
274 }
275
276 static inline unsigned int getRegOperandSize(const Record *RegRec) {
277 if (RegRec->isSubClassOf("RegisterClass"))
278 return RegRec->getValueAsInt("Alignment");
279 if (RegRec->isSubClassOf("RegisterOperand"))
280 return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment");
281
282 llvm_unreachable("Register operand's size not known!");
283 }
284};
285
286void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) {
287 emitSourceFileHeader("X86 EVEX2VEX tables", OS);
288
289 ArrayRef<const CodeGenInstruction *> NumberedInstructions =
290 Target.getInstructionsByEnumValue();
291
292 for (const CodeGenInstruction *Inst : NumberedInstructions) {
293 // Filter non-X86 instructions.
294 if (!Inst->TheDef->isSubClassOf("X86Inst"))
295 continue;
296
297 // Add VEX encoded instructions to one of VEXInsts vectors according to
298 // it's opcode.
299 if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") {
300 uint64_t Opcode = getValueFromBitsInit(Inst->TheDef->
301 getValueAsBitsInit("Opcode"));
302 VEXInsts[Opcode].push_back(Inst);
303 }
304 // Add relevant EVEX encoded instructions to EVEXInsts
305 else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" &&
306 !Inst->TheDef->getValueAsBit("hasEVEX_K") &&
307 !Inst->TheDef->getValueAsBit("hasEVEX_B") &&
308 getValueFromBitsInit(Inst->TheDef->
309 getValueAsBitsInit("EVEX_LL")) != 2 &&
310 !inExceptionList(Inst))
311 EVEXInsts.push_back(Inst);
312 }
313
314 for (const CodeGenInstruction *EVEXInst : EVEXInsts) {
315 uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef->
316 getValueAsBitsInit("Opcode"));
317 // For each EVEX instruction look for a VEX match in the appropriate vector
318 // (instructions with the same opcode) using function object IsMatch.
319 auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst));
320 if (Match != VEXInsts[Opcode].end()) {
321 const CodeGenInstruction *VEXInst = *Match;
322
323 // In case a match is found add new entry to the appropriate table
324 switch (getValueFromBitsInit(
325 EVEXInst->TheDef->getValueAsBitsInit("EVEX_LL"))) {
326 case 0:
327 EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0}
328 break;
329 case 1:
330 EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1}
331 break;
332 default:
333 llvm_unreachable("Instruction's size not fit for the mapping!");
334 }
335 }
336 }
337
338 // Print both tables
339 printTable(EVEX2VEX128, OS);
340 printTable(EVEX2VEX256, OS);
341}
342}
343
344namespace llvm {
345void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) {
346 X86EVEX2VEXTablesEmitter(RK).run(OS);
347}
348}