blob: 7c2d4efa29b3f64085a988f1685faa6d276a3451 [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},
151 {"VINSERTI64x2Z256rr", "VINSERTI128rr", false}
152 };
153
Ayman Musa850fc972017-03-07 08:11:19 +0000154 // Print the manually added entries
155 for (const ManualEntry &Entry : ManuallyAddedEntries) {
156 if ((Table == EVEX2VEX128 && Entry.Is128Bit) ||
157 (Table == EVEX2VEX256 && !Entry.Is128Bit)) {
Craig Topper9fc41352017-03-13 00:36:46 +0000158 OS << " { X86::" << Entry.EVEXInstStr << ", X86::" << Entry.VEXInstStr
Ayman Musa850fc972017-03-07 08:11:19 +0000159 << " },\n";
160 }
161 }
162
163 OS << "};\n\n";
164}
165
166// Return true if the 2 BitsInits are equal
167static inline bool equalBitsInits(const BitsInit *B1, const BitsInit *B2) {
168 if (B1->getNumBits() != B2->getNumBits())
169 PrintFatalError("Comparing two BitsInits with different sizes!");
170
171 for (unsigned i = 0, e = B1->getNumBits(); i != e; ++i) {
172 if (BitInit *Bit1 = dyn_cast<BitInit>(B1->getBit(i))) {
173 if (BitInit *Bit2 = dyn_cast<BitInit>(B2->getBit(i))) {
174 if (Bit1->getValue() != Bit2->getValue())
175 return false;
176 } else
177 PrintFatalError("Invalid BitsInit bit");
178 } else
179 PrintFatalError("Invalid BitsInit bit");
180 }
181 return true;
182}
183
184// Calculates the integer value residing BitsInit object
185static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
186 uint64_t Value = 0;
187 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
188 if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
189 Value |= uint64_t(Bit->getValue()) << i;
190 else
191 PrintFatalError("Invalid VectSize bit");
192 }
193 return Value;
194}
195
196// Function object - Operator() returns true if the given VEX instruction
197// matches the EVEX instruction of this object.
198class IsMatch {
199 const CodeGenInstruction *Inst;
200
201public:
202 IsMatch(const CodeGenInstruction *Inst) : Inst(Inst) {}
203
204 bool operator()(const CodeGenInstruction *Inst2) {
205 Record *Rec1 = Inst->TheDef;
206 Record *Rec2 = Inst2->TheDef;
207 uint64_t Rec1WVEX =
208 getValueFromBitsInit(Rec1->getValueAsBitsInit("VEX_WPrefix"));
209 uint64_t Rec2WVEX =
210 getValueFromBitsInit(Rec2->getValueAsBitsInit("VEX_WPrefix"));
211
212 if (Rec2->getValueAsDef("OpEnc")->getName().str() != "EncVEX" ||
213 // VEX/EVEX fields
214 Rec2->getValueAsDef("OpPrefix") != Rec1->getValueAsDef("OpPrefix") ||
215 Rec2->getValueAsDef("OpMap") != Rec1->getValueAsDef("OpMap") ||
216 Rec2->getValueAsBit("hasVEX_4V") != Rec1->getValueAsBit("hasVEX_4V") ||
217 !equalBitsInits(Rec2->getValueAsBitsInit("EVEX_LL"),
218 Rec1->getValueAsBitsInit("EVEX_LL")) ||
219 (Rec1WVEX != 2 && Rec2WVEX != 2 && Rec1WVEX != Rec2WVEX) ||
220 // Instruction's format
221 Rec2->getValueAsDef("Form") != Rec1->getValueAsDef("Form") ||
222 Rec2->getValueAsBit("isAsmParserOnly") !=
223 Rec1->getValueAsBit("isAsmParserOnly"))
224 return false;
225
226 // This is needed for instructions with intrinsic version (_Int).
227 // Where the only difference is the size of the operands.
228 // For example: VUCOMISDZrm and Int_VUCOMISDrm
229 // Also for instructions that their EVEX version was upgraded to work with
230 // k-registers. For example VPCMPEQBrm (xmm output register) and
231 // VPCMPEQBZ128rm (k register output register).
232 for (unsigned i = 0; i < Inst->Operands.size(); i++) {
233 Record *OpRec1 = Inst->Operands[i].Rec;
234 Record *OpRec2 = Inst2->Operands[i].Rec;
235
236 if (OpRec1 == OpRec2)
237 continue;
238
239 if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
240 if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
241 return false;
242 } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
Craig Topperbb4089d22017-03-13 05:34:03 +0000243 return false;
Ayman Musa850fc972017-03-07 08:11:19 +0000244 } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
245 if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type"))
246 return false;
247 } else
248 return false;
249 }
250
251 return true;
252 }
253
254private:
255 static inline bool isRegisterOperand(const Record *Rec) {
256 return Rec->isSubClassOf("RegisterClass") ||
257 Rec->isSubClassOf("RegisterOperand");
258 }
259
260 static inline bool isMemoryOperand(const Record *Rec) {
261 return Rec->isSubClassOf("Operand") &&
262 Rec->getValueAsString("OperandType") == "OPERAND_MEMORY";
263 }
264
265 static inline bool isImmediateOperand(const Record *Rec) {
266 return Rec->isSubClassOf("Operand") &&
267 Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE";
268 }
269
270 static inline unsigned int getRegOperandSize(const Record *RegRec) {
271 if (RegRec->isSubClassOf("RegisterClass"))
272 return RegRec->getValueAsInt("Alignment");
273 if (RegRec->isSubClassOf("RegisterOperand"))
274 return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment");
275
276 llvm_unreachable("Register operand's size not known!");
277 }
278};
279
280void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) {
281 emitSourceFileHeader("X86 EVEX2VEX tables", OS);
282
283 ArrayRef<const CodeGenInstruction *> NumberedInstructions =
284 Target.getInstructionsByEnumValue();
285
286 for (const CodeGenInstruction *Inst : NumberedInstructions) {
287 // Filter non-X86 instructions.
288 if (!Inst->TheDef->isSubClassOf("X86Inst"))
289 continue;
290
291 // Add VEX encoded instructions to one of VEXInsts vectors according to
292 // it's opcode.
293 if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") {
294 uint64_t Opcode = getValueFromBitsInit(Inst->TheDef->
295 getValueAsBitsInit("Opcode"));
296 VEXInsts[Opcode].push_back(Inst);
297 }
298 // Add relevant EVEX encoded instructions to EVEXInsts
299 else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" &&
300 !Inst->TheDef->getValueAsBit("hasEVEX_K") &&
301 !Inst->TheDef->getValueAsBit("hasEVEX_B") &&
302 getValueFromBitsInit(Inst->TheDef->
303 getValueAsBitsInit("EVEX_LL")) != 2 &&
304 !inExceptionList(Inst))
305 EVEXInsts.push_back(Inst);
306 }
307
308 for (const CodeGenInstruction *EVEXInst : EVEXInsts) {
309 uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef->
310 getValueAsBitsInit("Opcode"));
311 // For each EVEX instruction look for a VEX match in the appropriate vector
312 // (instructions with the same opcode) using function object IsMatch.
313 auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst));
314 if (Match != VEXInsts[Opcode].end()) {
315 const CodeGenInstruction *VEXInst = *Match;
316
317 // In case a match is found add new entry to the appropriate table
318 switch (getValueFromBitsInit(
319 EVEXInst->TheDef->getValueAsBitsInit("EVEX_LL"))) {
320 case 0:
321 EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0}
322 break;
323 case 1:
324 EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1}
325 break;
326 default:
327 llvm_unreachable("Instruction's size not fit for the mapping!");
328 }
329 }
330 }
331
332 // Print both tables
333 printTable(EVEX2VEX128, OS);
334 printTable(EVEX2VEX256, OS);
335}
336}
337
338namespace llvm {
339void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) {
340 X86EVEX2VEXTablesEmitter(RK).run(OS);
341}
342}