| Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 1 | //===- X86EvexToVex.cpp ---------------------------------------------------===// | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 2 | // Compress EVEX instructions to VEX encoding when possible to reduce code size | 
|  | 3 | // | 
|  | 4 | //                     The LLVM Compiler Infrastructure | 
|  | 5 | // | 
|  | 6 | // This file is distributed under the University of Illinois Open Source | 
|  | 7 | // License. See LICENSE.TXT for details. | 
|  | 8 | // | 
| Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 9 | //===----------------------------------------------------------------------===// | 
|  | 10 | // | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 11 | /// \file | 
|  | 12 | /// This file defines the pass that goes over all AVX-512 instructions which | 
|  | 13 | /// are encoded using the EVEX prefix and if possible replaces them by their | 
|  | 14 | /// corresponding VEX encoding which is usually shorter by 2 bytes. | 
|  | 15 | /// EVEX instructions may be encoded via the VEX prefix when the AVX-512 | 
|  | 16 | /// instruction has a corresponding AVX/AVX2 opcode and when it does not | 
| Craig Topper | fbb1985 | 2017-10-17 04:17:54 +0000 | [diff] [blame] | 17 | /// use the xmm or the mask registers or xmm/ymm registers with indexes | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 18 | /// higher than 15. | 
|  | 19 | /// The pass applies code reduction on the generated code for AVX-512 instrs. | 
| Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 20 | // | 
|  | 21 | //===----------------------------------------------------------------------===// | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 22 |  | 
|  | 23 | #include "InstPrinter/X86InstComments.h" | 
| Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 24 | #include "MCTargetDesc/X86BaseInfo.h" | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 25 | #include "X86.h" | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 26 | #include "X86InstrInfo.h" | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 27 | #include "X86Subtarget.h" | 
| Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" | 
|  | 29 | #include "llvm/ADT/StringRef.h" | 
|  | 30 | #include "llvm/CodeGen/MachineFunction.h" | 
|  | 31 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
|  | 32 | #include "llvm/CodeGen/MachineInstr.h" | 
|  | 33 | #include "llvm/CodeGen/MachineOperand.h" | 
|  | 34 | #include "llvm/MC/MCInstrDesc.h" | 
|  | 35 | #include "llvm/Pass.h" | 
|  | 36 | #include <cassert> | 
|  | 37 | #include <cstdint> | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | using namespace llvm; | 
|  | 40 |  | 
| Ayman Musa | 850fc97 | 2017-03-07 08:11:19 +0000 | [diff] [blame] | 41 | // Including the generated EVEX2VEX tables. | 
|  | 42 | struct X86EvexToVexCompressTableEntry { | 
|  | 43 | uint16_t EvexOpcode; | 
|  | 44 | uint16_t VexOpcode; | 
|  | 45 | }; | 
|  | 46 | #include "X86GenEVEX2VEXTables.inc" | 
|  | 47 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 48 | #define EVEX2VEX_DESC "Compressing EVEX instrs to VEX encoding when possible" | 
|  | 49 | #define EVEX2VEX_NAME "x86-evex-to-vex-compress" | 
|  | 50 |  | 
|  | 51 | #define DEBUG_TYPE EVEX2VEX_NAME | 
|  | 52 |  | 
|  | 53 | namespace { | 
|  | 54 |  | 
|  | 55 | class EvexToVexInstPass : public MachineFunctionPass { | 
|  | 56 |  | 
|  | 57 | /// X86EvexToVexCompressTable - Evex to Vex encoding opcode map. | 
| Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 58 | using EvexToVexTableType = DenseMap<unsigned, uint16_t>; | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 59 | EvexToVexTableType EvexToVex128Table; | 
|  | 60 | EvexToVexTableType EvexToVex256Table; | 
|  | 61 |  | 
|  | 62 | /// For EVEX instructions that can be encoded using VEX encoding, replace | 
|  | 63 | /// them by the VEX encoding in order to reduce size. | 
|  | 64 | bool CompressEvexToVexImpl(MachineInstr &MI) const; | 
|  | 65 |  | 
|  | 66 | /// For initializing the hash map tables of all AVX-512 EVEX | 
|  | 67 | /// corresponding to AVX/AVX2 opcodes. | 
|  | 68 | void AddTableEntry(EvexToVexTableType &EvexToVexTable, uint16_t EvexOp, | 
|  | 69 | uint16_t VexOp); | 
|  | 70 |  | 
|  | 71 | public: | 
|  | 72 | static char ID; | 
|  | 73 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 74 | EvexToVexInstPass() : MachineFunctionPass(ID) { | 
|  | 75 | initializeEvexToVexInstPassPass(*PassRegistry::getPassRegistry()); | 
|  | 76 |  | 
|  | 77 | // Initialize the EVEX to VEX 128 table map. | 
|  | 78 | for (X86EvexToVexCompressTableEntry Entry : X86EvexToVex128CompressTable) { | 
|  | 79 | AddTableEntry(EvexToVex128Table, Entry.EvexOpcode, Entry.VexOpcode); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | // Initialize the EVEX to VEX 256 table map. | 
|  | 83 | for (X86EvexToVexCompressTableEntry Entry : X86EvexToVex256CompressTable) { | 
|  | 84 | AddTableEntry(EvexToVex256Table, Entry.EvexOpcode, Entry.VexOpcode); | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 |  | 
| Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 88 | StringRef getPassName() const override { return EVEX2VEX_DESC; } | 
|  | 89 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 90 | /// Loop over all of the basic blocks, replacing EVEX instructions | 
|  | 91 | /// by equivalent VEX instructions when possible for reducing code size. | 
|  | 92 | bool runOnMachineFunction(MachineFunction &MF) override; | 
|  | 93 |  | 
|  | 94 | // This pass runs after regalloc and doesn't support VReg operands. | 
|  | 95 | MachineFunctionProperties getRequiredProperties() const override { | 
|  | 96 | return MachineFunctionProperties().set( | 
|  | 97 | MachineFunctionProperties::Property::NoVRegs); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | private: | 
|  | 101 | /// Machine instruction info used throughout the class. | 
|  | 102 | const X86InstrInfo *TII; | 
|  | 103 | }; | 
|  | 104 |  | 
| Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 105 | } // end anonymous namespace | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 106 |  | 
| Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 107 | char EvexToVexInstPass::ID = 0; | 
|  | 108 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 109 | bool EvexToVexInstPass::runOnMachineFunction(MachineFunction &MF) { | 
|  | 110 | TII = MF.getSubtarget<X86Subtarget>().getInstrInfo(); | 
|  | 111 |  | 
|  | 112 | const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>(); | 
|  | 113 | if (!ST.hasAVX512()) | 
|  | 114 | return false; | 
|  | 115 |  | 
|  | 116 | bool Changed = false; | 
|  | 117 |  | 
|  | 118 | /// Go over all basic blocks in function and replace | 
|  | 119 | /// EVEX encoded instrs by VEX encoding when possible. | 
|  | 120 | for (MachineBasicBlock &MBB : MF) { | 
|  | 121 |  | 
| Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 122 | // Traverse the basic block. | 
|  | 123 | for (MachineInstr &MI : MBB) | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 124 | Changed |= CompressEvexToVexImpl(MI); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | return Changed; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | void EvexToVexInstPass::AddTableEntry(EvexToVexTableType &EvexToVexTable, | 
|  | 131 | uint16_t EvexOp, uint16_t VexOp) { | 
|  | 132 | EvexToVexTable[EvexOp] = VexOp; | 
|  | 133 | } | 
|  | 134 |  | 
| Craig Topper | 730414b | 2017-10-30 03:35:43 +0000 | [diff] [blame] | 135 | static bool usesExtendedRegister(const MachineInstr &MI) { | 
|  | 136 | auto isHiRegIdx = [](unsigned Reg) { | 
|  | 137 | // Check for XMM register with indexes between 16 - 31. | 
|  | 138 | if (Reg >= X86::XMM16 && Reg <= X86::XMM31) | 
|  | 139 | return true; | 
|  | 140 |  | 
|  | 141 | // Check for YMM register with indexes between 16 - 31. | 
|  | 142 | if (Reg >= X86::YMM16 && Reg <= X86::YMM31) | 
|  | 143 | return true; | 
|  | 144 |  | 
|  | 145 | return false; | 
|  | 146 | }; | 
|  | 147 |  | 
|  | 148 | // Check that operands are not ZMM regs or | 
|  | 149 | // XMM/YMM regs with hi indexes between 16 - 31. | 
|  | 150 | for (const MachineOperand &MO : MI.explicit_operands()) { | 
|  | 151 | if (!MO.isReg()) | 
|  | 152 | continue; | 
|  | 153 |  | 
|  | 154 | unsigned Reg = MO.getReg(); | 
|  | 155 |  | 
|  | 156 | assert(!(Reg >= X86::ZMM0 && Reg <= X86::ZMM31) && | 
|  | 157 | "ZMM instructions should not be in the EVEX->VEX tables"); | 
|  | 158 |  | 
|  | 159 | if (isHiRegIdx(Reg)) | 
|  | 160 | return true; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | return false; | 
|  | 164 | } | 
|  | 165 |  | 
| Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 166 | // Do any custom cleanup needed to finalize the conversion. | 
| Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 167 | static bool performCustomAdjustments(MachineInstr &MI, unsigned NewOpc) { | 
| Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 168 | (void)NewOpc; | 
|  | 169 | unsigned Opc = MI.getOpcode(); | 
|  | 170 | switch (Opc) { | 
|  | 171 | case X86::VALIGNDZ128rri: | 
|  | 172 | case X86::VALIGNDZ128rmi: | 
|  | 173 | case X86::VALIGNQZ128rri: | 
| Craig Topper | e5d44ce | 2017-11-04 18:10:03 +0000 | [diff] [blame] | 174 | case X86::VALIGNQZ128rmi: { | 
| Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 175 | assert((NewOpc == X86::VPALIGNRrri || NewOpc == X86::VPALIGNRrmi) && | 
|  | 176 | "Unexpected new opcode!"); | 
|  | 177 | unsigned Scale = (Opc == X86::VALIGNQZ128rri || | 
|  | 178 | Opc == X86::VALIGNQZ128rmi) ? 8 : 4; | 
|  | 179 | MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1); | 
|  | 180 | Imm.setImm(Imm.getImm() * Scale); | 
|  | 181 | break; | 
|  | 182 | } | 
| Craig Topper | e5d44ce | 2017-11-04 18:10:03 +0000 | [diff] [blame] | 183 | case X86::VSHUFF32X4Z256rmi: | 
|  | 184 | case X86::VSHUFF32X4Z256rri: | 
|  | 185 | case X86::VSHUFF64X2Z256rmi: | 
|  | 186 | case X86::VSHUFF64X2Z256rri: | 
|  | 187 | case X86::VSHUFI32X4Z256rmi: | 
|  | 188 | case X86::VSHUFI32X4Z256rri: | 
|  | 189 | case X86::VSHUFI64X2Z256rmi: | 
|  | 190 | case X86::VSHUFI64X2Z256rri: { | 
|  | 191 | assert((NewOpc == X86::VPERM2F128rr || NewOpc == X86::VPERM2I128rr || | 
|  | 192 | NewOpc == X86::VPERM2F128rm || NewOpc == X86::VPERM2I128rm) && | 
|  | 193 | "Unexpected new opcode!"); | 
|  | 194 | MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1); | 
|  | 195 | int64_t ImmVal = Imm.getImm(); | 
|  | 196 | // Set bit 5, move bit 1 to bit 4, copy bit 0. | 
|  | 197 | Imm.setImm(0x20 | ((ImmVal & 2) << 3) | (ImmVal & 1)); | 
|  | 198 | break; | 
|  | 199 | } | 
| Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 200 | case X86::VRNDSCALEPDZ128rri: | 
|  | 201 | case X86::VRNDSCALEPDZ128rmi: | 
|  | 202 | case X86::VRNDSCALEPSZ128rri: | 
|  | 203 | case X86::VRNDSCALEPSZ128rmi: | 
|  | 204 | case X86::VRNDSCALEPDZ256rri: | 
|  | 205 | case X86::VRNDSCALEPDZ256rmi: | 
|  | 206 | case X86::VRNDSCALEPSZ256rri: | 
|  | 207 | case X86::VRNDSCALEPSZ256rmi: | 
|  | 208 | case X86::VRNDSCALESDr: | 
|  | 209 | case X86::VRNDSCALESDm: | 
|  | 210 | case X86::VRNDSCALESSr: | 
|  | 211 | case X86::VRNDSCALESSm: | 
|  | 212 | case X86::VRNDSCALESDr_Int: | 
|  | 213 | case X86::VRNDSCALESDm_Int: | 
|  | 214 | case X86::VRNDSCALESSr_Int: | 
|  | 215 | case X86::VRNDSCALESSm_Int: | 
|  | 216 | const MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1); | 
|  | 217 | int64_t ImmVal = Imm.getImm(); | 
|  | 218 | // Ensure that only bits 3:0 of the immediate are used. | 
|  | 219 | if ((ImmVal & 0xf) != ImmVal) | 
|  | 220 | return false; | 
|  | 221 | break; | 
| Craig Topper | e5d44ce | 2017-11-04 18:10:03 +0000 | [diff] [blame] | 222 | } | 
| Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 223 |  | 
|  | 224 | return true; | 
| Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
| Craig Topper | 730414b | 2017-10-30 03:35:43 +0000 | [diff] [blame] | 227 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 228 | // For EVEX instructions that can be encoded using VEX encoding | 
|  | 229 | // replace them by the VEX encoding in order to reduce size. | 
|  | 230 | bool EvexToVexInstPass::CompressEvexToVexImpl(MachineInstr &MI) const { | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 231 | // VEX format. | 
|  | 232 | // # of bytes: 0,2,3  1      1      0,1   0,1,2,4  0,1 | 
|  | 233 | //  [Prefixes] [VEX]  OPCODE ModR/M [SIB] [DISP]  [IMM] | 
|  | 234 | // | 
|  | 235 | // EVEX format. | 
|  | 236 | //  # of bytes: 4    1      1      1      4       / 1         1 | 
|  | 237 | //  [Prefixes]  EVEX Opcode ModR/M [SIB] [Disp32] / [Disp8*N] [Immediate] | 
|  | 238 |  | 
|  | 239 | const MCInstrDesc &Desc = MI.getDesc(); | 
|  | 240 |  | 
|  | 241 | // Check for EVEX instructions only. | 
|  | 242 | if ((Desc.TSFlags & X86II::EncodingMask) != X86II::EVEX) | 
|  | 243 | return false; | 
| Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 244 |  | 
|  | 245 | // Check for EVEX instructions with mask or broadcast as in these cases | 
|  | 246 | // the EVEX prefix is needed in order to carry this information | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 247 | // thus preventing the transformation to VEX encoding. | 
|  | 248 | if (Desc.TSFlags & (X86II::EVEX_K | X86II::EVEX_B)) | 
|  | 249 | return false; | 
| Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 250 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 251 | // Check for non EVEX_V512 instrs only. | 
|  | 252 | // EVEX_V512 instr: bit EVEX_L2 = 1; bit VEX_L = 0. | 
|  | 253 | if ((Desc.TSFlags & X86II::EVEX_L2) && !(Desc.TSFlags & X86II::VEX_L)) | 
| Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 254 | return false; | 
|  | 255 |  | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 256 | // EVEX_V128 instr: bit EVEX_L2 = 0, bit VEX_L = 0. | 
|  | 257 | bool IsEVEX_V128 = | 
|  | 258 | (!(Desc.TSFlags & X86II::EVEX_L2) && !(Desc.TSFlags & X86II::VEX_L)); | 
|  | 259 |  | 
|  | 260 | // EVEX_V256 instr: bit EVEX_L2 = 0, bit VEX_L = 1. | 
|  | 261 | bool IsEVEX_V256 = | 
|  | 262 | (!(Desc.TSFlags & X86II::EVEX_L2) && (Desc.TSFlags & X86II::VEX_L)); | 
|  | 263 |  | 
|  | 264 | unsigned NewOpc = 0; | 
|  | 265 |  | 
|  | 266 | // Check for EVEX_V256 instructions. | 
|  | 267 | if (IsEVEX_V256) { | 
|  | 268 | // Search for opcode in the EvexToVex256 table. | 
|  | 269 | auto It = EvexToVex256Table.find(MI.getOpcode()); | 
|  | 270 | if (It != EvexToVex256Table.end()) | 
|  | 271 | NewOpc = It->second; | 
|  | 272 | } | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 273 | // Check for EVEX_V128 or Scalar instructions. | 
|  | 274 | else if (IsEVEX_V128) { | 
|  | 275 | // Search for opcode in the EvexToVex128 table. | 
|  | 276 | auto It = EvexToVex128Table.find(MI.getOpcode()); | 
|  | 277 | if (It != EvexToVex128Table.end()) | 
|  | 278 | NewOpc = It->second; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | if (!NewOpc) | 
|  | 282 | return false; | 
|  | 283 |  | 
| Craig Topper | 730414b | 2017-10-30 03:35:43 +0000 | [diff] [blame] | 284 | if (usesExtendedRegister(MI)) | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 285 | return false; | 
| Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 286 |  | 
| Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 287 | if (!performCustomAdjustments(MI, NewOpc)) | 
|  | 288 | return false; | 
| Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 289 |  | 
| Craig Topper | c8483553 | 2017-10-30 03:35:44 +0000 | [diff] [blame] | 290 | MI.setDesc(TII->get(NewOpc)); | 
| Craig Topper | f27016f | 2018-03-10 05:15:22 +0000 | [diff] [blame] | 291 | MI.setAsmPrinterFlag(X86::AC_EVEX_2_VEX); | 
| Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 292 | return true; | 
| Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 293 | } | 
| Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 294 |  | 
|  | 295 | INITIALIZE_PASS(EvexToVexInstPass, EVEX2VEX_NAME, EVEX2VEX_DESC, false, false) | 
|  | 296 |  | 
|  | 297 | FunctionPass *llvm::createX86EvexToVexInsts() { | 
|  | 298 | return new EvexToVexInstPass(); | 
|  | 299 | } |