blob: d17dbed81b76873f9320548c9463be559b21ddce [file] [log] [blame]
Ayman Musa850fc972017-03-07 08:11:19 +00001//===- utils/TableGen/X86EVEX2VEXTablesEmitter.cpp - X86 backend-*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Ayman Musa850fc972017-03-07 08:11:19 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// This tablegen backend is responsible for emitting the X86 backend EVEX2VEX
10/// compression tables.
11///
12//===----------------------------------------------------------------------===//
13
Ayman Musa850fc972017-03-07 08:11:19 +000014#include "CodeGenTarget.h"
15#include "llvm/TableGen/Error.h"
16#include "llvm/TableGen/TableGenBackend.h"
17
18using namespace llvm;
19
20namespace {
21
22class X86EVEX2VEXTablesEmitter {
Craig Topperc2965212018-06-19 04:24:44 +000023 RecordKeeper &Records;
Ayman Musa850fc972017-03-07 08:11:19 +000024 CodeGenTarget Target;
25
26 // Hold all non-masked & non-broadcasted EVEX encoded instructions
27 std::vector<const CodeGenInstruction *> EVEXInsts;
28 // Hold all VEX encoded instructions. Divided into groups with same opcodes
29 // to make the search more efficient
30 std::map<uint64_t, std::vector<const CodeGenInstruction *>> VEXInsts;
31
32 typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *> Entry;
33
34 // Represent both compress tables
35 std::vector<Entry> EVEX2VEX128;
36 std::vector<Entry> EVEX2VEX256;
37
Ayman Musa850fc972017-03-07 08:11:19 +000038public:
Craig Topperc2965212018-06-19 04:24:44 +000039 X86EVEX2VEXTablesEmitter(RecordKeeper &R) : Records(R), Target(R) {}
Ayman Musa850fc972017-03-07 08:11:19 +000040
41 // run - Output X86 EVEX2VEX tables.
42 void run(raw_ostream &OS);
43
44private:
45 // Prints the given table as a C++ array of type
46 // X86EvexToVexCompressTableEntry
47 void printTable(const std::vector<Entry> &Table, raw_ostream &OS);
Ayman Musa850fc972017-03-07 08:11:19 +000048};
49
50void X86EVEX2VEXTablesEmitter::printTable(const std::vector<Entry> &Table,
51 raw_ostream &OS) {
Craig Topper17bd84c2018-06-18 18:47:07 +000052 StringRef Size = (Table == EVEX2VEX128) ? "128" : "256";
Ayman Musa850fc972017-03-07 08:11:19 +000053
54 OS << "// X86 EVEX encoded instructions that have a VEX " << Size
55 << " encoding\n"
56 << "// (table format: <EVEX opcode, VEX-" << Size << " opcode>).\n"
57 << "static const X86EvexToVexCompressTableEntry X86EvexToVex" << Size
58 << "CompressTable[] = {\n"
59 << " // EVEX scalar with corresponding VEX.\n";
60
61 // Print all entries added to the table
62 for (auto Pair : Table) {
Craig Topper9fc41352017-03-13 00:36:46 +000063 OS << " { X86::" << Pair.first->TheDef->getName()
Ayman Musa850fc972017-03-07 08:11:19 +000064 << ", X86::" << Pair.second->TheDef->getName() << " },\n";
65 }
66
Ayman Musa850fc972017-03-07 08:11:19 +000067 OS << "};\n\n";
68}
69
70// Return true if the 2 BitsInits are equal
71static inline bool equalBitsInits(const BitsInit *B1, const BitsInit *B2) {
72 if (B1->getNumBits() != B2->getNumBits())
73 PrintFatalError("Comparing two BitsInits with different sizes!");
74
75 for (unsigned i = 0, e = B1->getNumBits(); i != e; ++i) {
76 if (BitInit *Bit1 = dyn_cast<BitInit>(B1->getBit(i))) {
77 if (BitInit *Bit2 = dyn_cast<BitInit>(B2->getBit(i))) {
78 if (Bit1->getValue() != Bit2->getValue())
79 return false;
80 } else
81 PrintFatalError("Invalid BitsInit bit");
82 } else
83 PrintFatalError("Invalid BitsInit bit");
84 }
85 return true;
86}
87
88// Calculates the integer value residing BitsInit object
89static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
90 uint64_t Value = 0;
91 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
92 if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
93 Value |= uint64_t(Bit->getValue()) << i;
94 else
95 PrintFatalError("Invalid VectSize bit");
96 }
97 return Value;
98}
99
100// Function object - Operator() returns true if the given VEX instruction
101// matches the EVEX instruction of this object.
102class IsMatch {
Craig Topper0a5e90c2018-06-19 04:24:42 +0000103 const CodeGenInstruction *EVEXInst;
Ayman Musa850fc972017-03-07 08:11:19 +0000104
105public:
Craig Topper0a5e90c2018-06-19 04:24:42 +0000106 IsMatch(const CodeGenInstruction *EVEXInst) : EVEXInst(EVEXInst) {}
Ayman Musa850fc972017-03-07 08:11:19 +0000107
Craig Topper0a5e90c2018-06-19 04:24:42 +0000108 bool operator()(const CodeGenInstruction *VEXInst) {
109 Record *RecE = EVEXInst->TheDef;
110 Record *RecV = VEXInst->TheDef;
Craig Topper2f9c1732019-04-09 07:40:06 +0000111 bool EVEX_W = RecE->getValueAsBit("HasVEX_W");
112 bool VEX_W = RecV->getValueAsBit("HasVEX_W");
113 bool VEX_WIG = RecV->getValueAsBit("IgnoresVEX_W");
114 bool EVEX_WIG = RecE->getValueAsBit("IgnoresVEX_W");
115 bool EVEX_W1_VEX_W0 = RecE->getValueAsBit("EVEX_W1_VEX_W0");
Ayman Musa850fc972017-03-07 08:11:19 +0000116
Craig Topper0a5e90c2018-06-19 04:24:42 +0000117 if (RecV->getValueAsDef("OpEnc")->getName().str() != "EncVEX" ||
Ayman Musa850fc972017-03-07 08:11:19 +0000118 // VEX/EVEX fields
Craig Topper0a5e90c2018-06-19 04:24:42 +0000119 RecV->getValueAsDef("OpPrefix") != RecE->getValueAsDef("OpPrefix") ||
120 RecV->getValueAsDef("OpMap") != RecE->getValueAsDef("OpMap") ||
121 RecV->getValueAsBit("hasVEX_4V") != RecE->getValueAsBit("hasVEX_4V") ||
122 !equalBitsInits(RecV->getValueAsBitsInit("EVEX_LL"),
123 RecE->getValueAsBitsInit("EVEX_LL")) ||
124 // Match is allowed if either is VEX_WIG, or they match, or EVEX
125 // is VEX_W1X and VEX is VEX_W0.
Craig Topper2f9c1732019-04-09 07:40:06 +0000126 (!(VEX_WIG || EVEX_WIG || EVEX_W == VEX_W ||
127 (EVEX_W1_VEX_W0 && EVEX_W && !VEX_W))) ||
Ayman Musa850fc972017-03-07 08:11:19 +0000128 // Instruction's format
Craig Topper0a5e90c2018-06-19 04:24:42 +0000129 RecV->getValueAsDef("Form") != RecE->getValueAsDef("Form") ||
130 RecV->getValueAsBit("isAsmParserOnly") !=
131 RecE->getValueAsBit("isAsmParserOnly"))
Ayman Musa850fc972017-03-07 08:11:19 +0000132 return false;
133
134 // This is needed for instructions with intrinsic version (_Int).
135 // Where the only difference is the size of the operands.
136 // For example: VUCOMISDZrm and Int_VUCOMISDrm
137 // Also for instructions that their EVEX version was upgraded to work with
138 // k-registers. For example VPCMPEQBrm (xmm output register) and
139 // VPCMPEQBZ128rm (k register output register).
Craig Topper0a5e90c2018-06-19 04:24:42 +0000140 for (unsigned i = 0, e = EVEXInst->Operands.size(); i < e; i++) {
141 Record *OpRec1 = EVEXInst->Operands[i].Rec;
142 Record *OpRec2 = VEXInst->Operands[i].Rec;
Ayman Musa850fc972017-03-07 08:11:19 +0000143
144 if (OpRec1 == OpRec2)
145 continue;
146
147 if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
148 if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
149 return false;
150 } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
Craig Topperbb4089d22017-03-13 05:34:03 +0000151 return false;
Ayman Musa850fc972017-03-07 08:11:19 +0000152 } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
153 if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type"))
154 return false;
155 } else
156 return false;
157 }
158
159 return true;
160 }
161
162private:
163 static inline bool isRegisterOperand(const Record *Rec) {
164 return Rec->isSubClassOf("RegisterClass") ||
165 Rec->isSubClassOf("RegisterOperand");
166 }
167
168 static inline bool isMemoryOperand(const Record *Rec) {
169 return Rec->isSubClassOf("Operand") &&
170 Rec->getValueAsString("OperandType") == "OPERAND_MEMORY";
171 }
172
173 static inline bool isImmediateOperand(const Record *Rec) {
174 return Rec->isSubClassOf("Operand") &&
175 Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE";
176 }
177
178 static inline unsigned int getRegOperandSize(const Record *RegRec) {
179 if (RegRec->isSubClassOf("RegisterClass"))
180 return RegRec->getValueAsInt("Alignment");
181 if (RegRec->isSubClassOf("RegisterOperand"))
182 return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment");
183
184 llvm_unreachable("Register operand's size not known!");
185 }
186};
187
188void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) {
189 emitSourceFileHeader("X86 EVEX2VEX tables", OS);
190
191 ArrayRef<const CodeGenInstruction *> NumberedInstructions =
192 Target.getInstructionsByEnumValue();
193
194 for (const CodeGenInstruction *Inst : NumberedInstructions) {
195 // Filter non-X86 instructions.
196 if (!Inst->TheDef->isSubClassOf("X86Inst"))
197 continue;
198
199 // Add VEX encoded instructions to one of VEXInsts vectors according to
200 // it's opcode.
201 if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") {
202 uint64_t Opcode = getValueFromBitsInit(Inst->TheDef->
203 getValueAsBitsInit("Opcode"));
204 VEXInsts[Opcode].push_back(Inst);
205 }
206 // Add relevant EVEX encoded instructions to EVEXInsts
207 else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" &&
208 !Inst->TheDef->getValueAsBit("hasEVEX_K") &&
209 !Inst->TheDef->getValueAsBit("hasEVEX_B") &&
210 getValueFromBitsInit(Inst->TheDef->
211 getValueAsBitsInit("EVEX_LL")) != 2 &&
Craig Topper17bd84c2018-06-18 18:47:07 +0000212 !Inst->TheDef->getValueAsBit("notEVEX2VEXConvertible"))
Ayman Musa850fc972017-03-07 08:11:19 +0000213 EVEXInsts.push_back(Inst);
214 }
215
216 for (const CodeGenInstruction *EVEXInst : EVEXInsts) {
217 uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef->
218 getValueAsBitsInit("Opcode"));
219 // For each EVEX instruction look for a VEX match in the appropriate vector
220 // (instructions with the same opcode) using function object IsMatch.
Craig Topperc2965212018-06-19 04:24:44 +0000221 // Allow EVEX2VEXOverride to explicitly specify a match.
222 const CodeGenInstruction *VEXInst = nullptr;
223 if (!EVEXInst->TheDef->isValueUnset("EVEX2VEXOverride")) {
224 StringRef AltInstStr =
225 EVEXInst->TheDef->getValueAsString("EVEX2VEXOverride");
226 Record *AltInstRec = Records.getDef(AltInstStr);
227 assert(AltInstRec && "EVEX2VEXOverride instruction not found!");
228 VEXInst = &Target.getInstruction(AltInstRec);
229 } else {
230 auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst));
231 if (Match != VEXInsts[Opcode].end())
232 VEXInst = *Match;
233 }
Ayman Musa850fc972017-03-07 08:11:19 +0000234
Craig Topperc2965212018-06-19 04:24:44 +0000235 if (!VEXInst)
236 continue;
237
238 // In case a match is found add new entry to the appropriate table
239 switch (getValueFromBitsInit(
240 EVEXInst->TheDef->getValueAsBitsInit("EVEX_LL"))) {
241 case 0:
242 EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0}
243 break;
244 case 1:
245 EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1}
246 break;
247 default:
248 llvm_unreachable("Instruction's size not fit for the mapping!");
Ayman Musa850fc972017-03-07 08:11:19 +0000249 }
250 }
251
252 // Print both tables
253 printTable(EVEX2VEX128, OS);
254 printTable(EVEX2VEX256, OS);
255}
256}
257
258namespace llvm {
259void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) {
260 X86EVEX2VEXTablesEmitter(RK).run(OS);
261}
262}