Ayman Musa | 850fc97 | 2017-03-07 08:11:19 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class X86EVEX2VEXTablesEmitter { |
Ayman Musa | 850fc97 | 2017-03-07 08:11:19 +0000 | [diff] [blame] | 25 | 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 |
| 40 | class ManualEntry { |
| 41 | public: |
| 42 | std::string EVEXInstStr; |
| 43 | std::string VEXInstStr; |
| 44 | bool Is128Bit; |
| 45 | |
| 46 | ManualEntry(std::string EVEXInstStr, std::string VEXInstStr, bool Is128Bit) |
| 47 | : EVEXInstStr(EVEXInstStr), VEXInstStr(VEXInstStr), Is128Bit(Is128Bit) { |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | public: |
Ayman Musa | 63cfb16 | 2017-03-07 08:56:27 +0000 | [diff] [blame^] | 52 | X86EVEX2VEXTablesEmitter(RecordKeeper &R) : Target(R) {} |
Ayman Musa | 850fc97 | 2017-03-07 08:11:19 +0000 | [diff] [blame] | 53 | |
| 54 | // run - Output X86 EVEX2VEX tables. |
| 55 | void run(raw_ostream &OS); |
| 56 | |
| 57 | private: |
| 58 | // Prints the given table as a C++ array of type |
| 59 | // X86EvexToVexCompressTableEntry |
| 60 | void printTable(const std::vector<Entry> &Table, raw_ostream &OS); |
| 61 | |
| 62 | // List of EVEX instructions that match VEX instructions by the encoding |
| 63 | // but do not perform the same operation. |
| 64 | const std::vector<std::string> ExceptionList = { |
| 65 | "VCVTQQ2PD", |
| 66 | "VCVTQQ2PS", |
| 67 | "VPMAXSQ", |
| 68 | "VPMAXUQ", |
| 69 | "VPMINSQ", |
| 70 | "VPMINUQ", |
| 71 | "VPMULLQ", |
| 72 | "VPSRAQ", |
| 73 | "VDBPSADBW", |
| 74 | "VRNDSCALE", |
| 75 | "VSCALEFPS" |
| 76 | }; |
| 77 | |
| 78 | bool inExceptionList(const CodeGenInstruction *Inst) { |
| 79 | // Instruction's name starts with one of the entries in the exception list |
| 80 | for (const std::string& InstStr : ExceptionList) { |
| 81 | if (Inst->TheDef->getName().startswith(InstStr)) |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // Some VEX instructions were duplicated to multiple EVEX versions due the |
| 88 | // introduction of mask variants, and thus some of the EVEX versions have |
| 89 | // different encoding than the VEX instruction. In order to maximize the |
| 90 | // compression we add these entries manually. |
| 91 | const std::vector<ManualEntry> ManuallyAddedEntries = { |
| 92 | // EVEX-Inst VEX-Inst Is128-bit |
| 93 | {"VMOVDQU8Z128mr", "VMOVDQUmr", true}, |
| 94 | {"VMOVDQU8Z128rm", "VMOVDQUrm", true}, |
| 95 | {"VMOVDQU8Z128rr", "VMOVDQUrr", true}, |
| 96 | {"VMOVDQU8Z128rr_REV", "VMOVDQUrr_REV", true}, |
| 97 | {"VMOVDQU16Z128mr", "VMOVDQUmr", true}, |
| 98 | {"VMOVDQU16Z128rm", "VMOVDQUrm", true}, |
| 99 | {"VMOVDQU16Z128rr", "VMOVDQUrr", true}, |
| 100 | {"VMOVDQU16Z128rr_REV", "VMOVDQUrr_REV", true}, |
| 101 | {"VMOVDQU8Z256mr", "VMOVDQUYmr", false}, |
| 102 | {"VMOVDQU8Z256rm", "VMOVDQUYrm", false}, |
| 103 | {"VMOVDQU8Z256rr", "VMOVDQUYrr", false}, |
| 104 | {"VMOVDQU8Z256rr_REV", "VMOVDQUYrr_REV", false}, |
| 105 | {"VMOVDQU16Z256mr", "VMOVDQUYmr", false}, |
| 106 | {"VMOVDQU16Z256rm", "VMOVDQUYrm", false}, |
| 107 | {"VMOVDQU16Z256rr", "VMOVDQUYrr", false}, |
| 108 | {"VMOVDQU16Z256rr_REV", "VMOVDQUYrr_REV", false}, |
| 109 | |
| 110 | {"VPERMILPDZ128mi", "VPERMILPDmi", true}, |
| 111 | {"VPERMILPDZ128ri", "VPERMILPDri", true}, |
| 112 | {"VPERMILPDZ128rm", "VPERMILPDrm", true}, |
| 113 | {"VPERMILPDZ128rr", "VPERMILPDrr", true}, |
| 114 | {"VPERMILPDZ256mi", "VPERMILPDYmi", false}, |
| 115 | {"VPERMILPDZ256ri", "VPERMILPDYri", false}, |
| 116 | {"VPERMILPDZ256rm", "VPERMILPDYrm", false}, |
| 117 | {"VPERMILPDZ256rr", "VPERMILPDYrr", false}, |
| 118 | |
| 119 | {"VPBROADCASTQZ128m", "VPBROADCASTQrm", true}, |
| 120 | {"VPBROADCASTQZ128r", "VPBROADCASTQrr", true}, |
| 121 | {"VPBROADCASTQZ256m", "VPBROADCASTQYrm", false}, |
| 122 | {"VPBROADCASTQZ256r", "VPBROADCASTQYrr", false}, |
| 123 | |
| 124 | {"VBROADCASTSDZ256m", "VBROADCASTSDYrm", false}, |
| 125 | {"VBROADCASTSDZ256r", "VBROADCASTSDYrr", false}, |
| 126 | |
| 127 | {"VEXTRACTF64x2Z256mr", "VEXTRACTF128mr", false}, |
| 128 | {"VEXTRACTF64x2Z256rr", "VEXTRACTF128rr", false}, |
| 129 | {"VEXTRACTI64x2Z256mr", "VEXTRACTI128mr", false}, |
| 130 | {"VEXTRACTI64x2Z256rr", "VEXTRACTI128rr", false}, |
| 131 | |
| 132 | {"VINSERTF64x2Z256rm", "VINSERTF128rm", false}, |
| 133 | {"VINSERTF64x2Z256rr", "VINSERTF128rr", false}, |
| 134 | {"VINSERTI64x2Z256rm", "VINSERTI128rm", false}, |
| 135 | {"VINSERTI64x2Z256rr", "VINSERTI128rr", false} |
| 136 | }; |
| 137 | }; |
| 138 | |
| 139 | void X86EVEX2VEXTablesEmitter::printTable(const std::vector<Entry> &Table, |
| 140 | raw_ostream &OS) { |
| 141 | std::string Size = (Table == EVEX2VEX128) ? "128" : "256"; |
| 142 | |
| 143 | OS << "// X86 EVEX encoded instructions that have a VEX " << Size |
| 144 | << " encoding\n" |
| 145 | << "// (table format: <EVEX opcode, VEX-" << Size << " opcode>).\n" |
| 146 | << "static const X86EvexToVexCompressTableEntry X86EvexToVex" << Size |
| 147 | << "CompressTable[] = {\n" |
| 148 | << " // EVEX scalar with corresponding VEX.\n"; |
| 149 | |
| 150 | // Print all entries added to the table |
| 151 | for (auto Pair : Table) { |
| 152 | OS << "{ X86::" << Pair.first->TheDef->getName() |
| 153 | << ", X86::" << Pair.second->TheDef->getName() << " },\n"; |
| 154 | } |
| 155 | |
| 156 | // Print the manually added entries |
| 157 | for (const ManualEntry &Entry : ManuallyAddedEntries) { |
| 158 | if ((Table == EVEX2VEX128 && Entry.Is128Bit) || |
| 159 | (Table == EVEX2VEX256 && !Entry.Is128Bit)) { |
| 160 | OS << "{ X86::" << Entry.EVEXInstStr << ", X86::" << Entry.VEXInstStr |
| 161 | << " },\n"; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | OS << "};\n\n"; |
| 166 | } |
| 167 | |
| 168 | // Return true if the 2 BitsInits are equal |
| 169 | static inline bool equalBitsInits(const BitsInit *B1, const BitsInit *B2) { |
| 170 | if (B1->getNumBits() != B2->getNumBits()) |
| 171 | PrintFatalError("Comparing two BitsInits with different sizes!"); |
| 172 | |
| 173 | for (unsigned i = 0, e = B1->getNumBits(); i != e; ++i) { |
| 174 | if (BitInit *Bit1 = dyn_cast<BitInit>(B1->getBit(i))) { |
| 175 | if (BitInit *Bit2 = dyn_cast<BitInit>(B2->getBit(i))) { |
| 176 | if (Bit1->getValue() != Bit2->getValue()) |
| 177 | return false; |
| 178 | } else |
| 179 | PrintFatalError("Invalid BitsInit bit"); |
| 180 | } else |
| 181 | PrintFatalError("Invalid BitsInit bit"); |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | // Calculates the integer value residing BitsInit object |
| 187 | static inline uint64_t getValueFromBitsInit(const BitsInit *B) { |
| 188 | uint64_t Value = 0; |
| 189 | for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) { |
| 190 | if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i))) |
| 191 | Value |= uint64_t(Bit->getValue()) << i; |
| 192 | else |
| 193 | PrintFatalError("Invalid VectSize bit"); |
| 194 | } |
| 195 | return Value; |
| 196 | } |
| 197 | |
| 198 | // Function object - Operator() returns true if the given VEX instruction |
| 199 | // matches the EVEX instruction of this object. |
| 200 | class IsMatch { |
| 201 | const CodeGenInstruction *Inst; |
| 202 | |
| 203 | public: |
| 204 | IsMatch(const CodeGenInstruction *Inst) : Inst(Inst) {} |
| 205 | |
| 206 | bool operator()(const CodeGenInstruction *Inst2) { |
| 207 | Record *Rec1 = Inst->TheDef; |
| 208 | Record *Rec2 = Inst2->TheDef; |
| 209 | uint64_t Rec1WVEX = |
| 210 | getValueFromBitsInit(Rec1->getValueAsBitsInit("VEX_WPrefix")); |
| 211 | uint64_t Rec2WVEX = |
| 212 | getValueFromBitsInit(Rec2->getValueAsBitsInit("VEX_WPrefix")); |
| 213 | |
| 214 | if (Rec2->getValueAsDef("OpEnc")->getName().str() != "EncVEX" || |
| 215 | // VEX/EVEX fields |
| 216 | Rec2->getValueAsDef("OpPrefix") != Rec1->getValueAsDef("OpPrefix") || |
| 217 | Rec2->getValueAsDef("OpMap") != Rec1->getValueAsDef("OpMap") || |
| 218 | Rec2->getValueAsBit("hasVEX_4V") != Rec1->getValueAsBit("hasVEX_4V") || |
| 219 | !equalBitsInits(Rec2->getValueAsBitsInit("EVEX_LL"), |
| 220 | Rec1->getValueAsBitsInit("EVEX_LL")) || |
| 221 | (Rec1WVEX != 2 && Rec2WVEX != 2 && Rec1WVEX != Rec2WVEX) || |
| 222 | // Instruction's format |
| 223 | Rec2->getValueAsDef("Form") != Rec1->getValueAsDef("Form") || |
| 224 | Rec2->getValueAsBit("isAsmParserOnly") != |
| 225 | Rec1->getValueAsBit("isAsmParserOnly")) |
| 226 | return false; |
| 227 | |
| 228 | // This is needed for instructions with intrinsic version (_Int). |
| 229 | // Where the only difference is the size of the operands. |
| 230 | // For example: VUCOMISDZrm and Int_VUCOMISDrm |
| 231 | // Also for instructions that their EVEX version was upgraded to work with |
| 232 | // k-registers. For example VPCMPEQBrm (xmm output register) and |
| 233 | // VPCMPEQBZ128rm (k register output register). |
| 234 | for (unsigned i = 0; i < Inst->Operands.size(); i++) { |
| 235 | Record *OpRec1 = Inst->Operands[i].Rec; |
| 236 | Record *OpRec2 = Inst2->Operands[i].Rec; |
| 237 | |
| 238 | if (OpRec1 == OpRec2) |
| 239 | continue; |
| 240 | |
| 241 | if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) { |
| 242 | if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2)) |
| 243 | return false; |
| 244 | } else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) { |
| 245 | return false; |
| 246 | } else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) { |
| 247 | if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type")) |
| 248 | return false; |
| 249 | } else |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | private: |
| 257 | static inline bool isRegisterOperand(const Record *Rec) { |
| 258 | return Rec->isSubClassOf("RegisterClass") || |
| 259 | Rec->isSubClassOf("RegisterOperand"); |
| 260 | } |
| 261 | |
| 262 | static inline bool isMemoryOperand(const Record *Rec) { |
| 263 | return Rec->isSubClassOf("Operand") && |
| 264 | Rec->getValueAsString("OperandType") == "OPERAND_MEMORY"; |
| 265 | } |
| 266 | |
| 267 | static inline bool isImmediateOperand(const Record *Rec) { |
| 268 | return Rec->isSubClassOf("Operand") && |
| 269 | Rec->getValueAsString("OperandType") == "OPERAND_IMMEDIATE"; |
| 270 | } |
| 271 | |
| 272 | static inline unsigned int getRegOperandSize(const Record *RegRec) { |
| 273 | if (RegRec->isSubClassOf("RegisterClass")) |
| 274 | return RegRec->getValueAsInt("Alignment"); |
| 275 | if (RegRec->isSubClassOf("RegisterOperand")) |
| 276 | return RegRec->getValueAsDef("RegClass")->getValueAsInt("Alignment"); |
| 277 | |
| 278 | llvm_unreachable("Register operand's size not known!"); |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | void X86EVEX2VEXTablesEmitter::run(raw_ostream &OS) { |
| 283 | emitSourceFileHeader("X86 EVEX2VEX tables", OS); |
| 284 | |
| 285 | ArrayRef<const CodeGenInstruction *> NumberedInstructions = |
| 286 | Target.getInstructionsByEnumValue(); |
| 287 | |
| 288 | for (const CodeGenInstruction *Inst : NumberedInstructions) { |
| 289 | // Filter non-X86 instructions. |
| 290 | if (!Inst->TheDef->isSubClassOf("X86Inst")) |
| 291 | continue; |
| 292 | |
| 293 | // Add VEX encoded instructions to one of VEXInsts vectors according to |
| 294 | // it's opcode. |
| 295 | if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncVEX") { |
| 296 | uint64_t Opcode = getValueFromBitsInit(Inst->TheDef-> |
| 297 | getValueAsBitsInit("Opcode")); |
| 298 | VEXInsts[Opcode].push_back(Inst); |
| 299 | } |
| 300 | // Add relevant EVEX encoded instructions to EVEXInsts |
| 301 | else if (Inst->TheDef->getValueAsDef("OpEnc")->getName() == "EncEVEX" && |
| 302 | !Inst->TheDef->getValueAsBit("hasEVEX_K") && |
| 303 | !Inst->TheDef->getValueAsBit("hasEVEX_B") && |
| 304 | getValueFromBitsInit(Inst->TheDef-> |
| 305 | getValueAsBitsInit("EVEX_LL")) != 2 && |
| 306 | !inExceptionList(Inst)) |
| 307 | EVEXInsts.push_back(Inst); |
| 308 | } |
| 309 | |
| 310 | for (const CodeGenInstruction *EVEXInst : EVEXInsts) { |
| 311 | uint64_t Opcode = getValueFromBitsInit(EVEXInst->TheDef-> |
| 312 | getValueAsBitsInit("Opcode")); |
| 313 | // For each EVEX instruction look for a VEX match in the appropriate vector |
| 314 | // (instructions with the same opcode) using function object IsMatch. |
| 315 | auto Match = llvm::find_if(VEXInsts[Opcode], IsMatch(EVEXInst)); |
| 316 | if (Match != VEXInsts[Opcode].end()) { |
| 317 | const CodeGenInstruction *VEXInst = *Match; |
| 318 | |
| 319 | // In case a match is found add new entry to the appropriate table |
| 320 | switch (getValueFromBitsInit( |
| 321 | EVEXInst->TheDef->getValueAsBitsInit("EVEX_LL"))) { |
| 322 | case 0: |
| 323 | EVEX2VEX128.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,0} |
| 324 | break; |
| 325 | case 1: |
| 326 | EVEX2VEX256.push_back(std::make_pair(EVEXInst, VEXInst)); // {0,1} |
| 327 | break; |
| 328 | default: |
| 329 | llvm_unreachable("Instruction's size not fit for the mapping!"); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Print both tables |
| 335 | printTable(EVEX2VEX128, OS); |
| 336 | printTable(EVEX2VEX256, OS); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | namespace llvm { |
| 341 | void EmitX86EVEX2VEXTables(RecordKeeper &RK, raw_ostream &OS) { |
| 342 | X86EVEX2VEXTablesEmitter(RK).run(OS); |
| 343 | } |
| 344 | } |