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; |
Craig Topper | d22ad85 | 2018-06-20 04:32:04 +0000 | [diff] [blame^] | 45 | |
| 46 | bool operator<(const X86EvexToVexCompressTableEntry &RHS) const { |
| 47 | return EvexOpcode < RHS.EvexOpcode; |
| 48 | } |
| 49 | |
| 50 | friend bool operator<(const X86EvexToVexCompressTableEntry &TE, |
| 51 | unsigned Opc) { |
| 52 | return TE.EvexOpcode < Opc; |
| 53 | } |
Ayman Musa | 850fc97 | 2017-03-07 08:11:19 +0000 | [diff] [blame] | 54 | }; |
| 55 | #include "X86GenEVEX2VEXTables.inc" |
| 56 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 57 | #define EVEX2VEX_DESC "Compressing EVEX instrs to VEX encoding when possible" |
| 58 | #define EVEX2VEX_NAME "x86-evex-to-vex-compress" |
| 59 | |
| 60 | #define DEBUG_TYPE EVEX2VEX_NAME |
| 61 | |
| 62 | namespace { |
| 63 | |
| 64 | class EvexToVexInstPass : public MachineFunctionPass { |
| 65 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 66 | /// For EVEX instructions that can be encoded using VEX encoding, replace |
| 67 | /// them by the VEX encoding in order to reduce size. |
| 68 | bool CompressEvexToVexImpl(MachineInstr &MI) const; |
| 69 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 70 | public: |
| 71 | static char ID; |
| 72 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 73 | EvexToVexInstPass() : MachineFunctionPass(ID) { |
| 74 | initializeEvexToVexInstPassPass(*PassRegistry::getPassRegistry()); |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 77 | StringRef getPassName() const override { return EVEX2VEX_DESC; } |
| 78 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 79 | /// Loop over all of the basic blocks, replacing EVEX instructions |
| 80 | /// by equivalent VEX instructions when possible for reducing code size. |
| 81 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 82 | |
| 83 | // This pass runs after regalloc and doesn't support VReg operands. |
| 84 | MachineFunctionProperties getRequiredProperties() const override { |
| 85 | return MachineFunctionProperties().set( |
| 86 | MachineFunctionProperties::Property::NoVRegs); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | /// Machine instruction info used throughout the class. |
| 91 | const X86InstrInfo *TII; |
| 92 | }; |
| 93 | |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 94 | } // end anonymous namespace |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 95 | |
Eugene Zelenko | 60433b6 | 2017-10-05 00:33:50 +0000 | [diff] [blame] | 96 | char EvexToVexInstPass::ID = 0; |
| 97 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 98 | bool EvexToVexInstPass::runOnMachineFunction(MachineFunction &MF) { |
| 99 | TII = MF.getSubtarget<X86Subtarget>().getInstrInfo(); |
| 100 | |
| 101 | const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>(); |
| 102 | if (!ST.hasAVX512()) |
| 103 | return false; |
| 104 | |
| 105 | bool Changed = false; |
| 106 | |
| 107 | /// Go over all basic blocks in function and replace |
| 108 | /// EVEX encoded instrs by VEX encoding when possible. |
| 109 | for (MachineBasicBlock &MBB : MF) { |
| 110 | |
Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 111 | // Traverse the basic block. |
| 112 | for (MachineInstr &MI : MBB) |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 113 | Changed |= CompressEvexToVexImpl(MI); |
| 114 | } |
| 115 | |
| 116 | return Changed; |
| 117 | } |
| 118 | |
Craig Topper | 730414b | 2017-10-30 03:35:43 +0000 | [diff] [blame] | 119 | static bool usesExtendedRegister(const MachineInstr &MI) { |
| 120 | auto isHiRegIdx = [](unsigned Reg) { |
| 121 | // Check for XMM register with indexes between 16 - 31. |
| 122 | if (Reg >= X86::XMM16 && Reg <= X86::XMM31) |
| 123 | return true; |
| 124 | |
| 125 | // Check for YMM register with indexes between 16 - 31. |
| 126 | if (Reg >= X86::YMM16 && Reg <= X86::YMM31) |
| 127 | return true; |
| 128 | |
| 129 | return false; |
| 130 | }; |
| 131 | |
| 132 | // Check that operands are not ZMM regs or |
| 133 | // XMM/YMM regs with hi indexes between 16 - 31. |
| 134 | for (const MachineOperand &MO : MI.explicit_operands()) { |
| 135 | if (!MO.isReg()) |
| 136 | continue; |
| 137 | |
| 138 | unsigned Reg = MO.getReg(); |
| 139 | |
| 140 | assert(!(Reg >= X86::ZMM0 && Reg <= X86::ZMM31) && |
| 141 | "ZMM instructions should not be in the EVEX->VEX tables"); |
| 142 | |
| 143 | if (isHiRegIdx(Reg)) |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 150 | // Do any custom cleanup needed to finalize the conversion. |
Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 151 | static bool performCustomAdjustments(MachineInstr &MI, unsigned NewOpc) { |
Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 152 | (void)NewOpc; |
| 153 | unsigned Opc = MI.getOpcode(); |
| 154 | switch (Opc) { |
| 155 | case X86::VALIGNDZ128rri: |
| 156 | case X86::VALIGNDZ128rmi: |
| 157 | case X86::VALIGNQZ128rri: |
Craig Topper | e5d44ce | 2017-11-04 18:10:03 +0000 | [diff] [blame] | 158 | case X86::VALIGNQZ128rmi: { |
Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 159 | assert((NewOpc == X86::VPALIGNRrri || NewOpc == X86::VPALIGNRrmi) && |
| 160 | "Unexpected new opcode!"); |
| 161 | unsigned Scale = (Opc == X86::VALIGNQZ128rri || |
| 162 | Opc == X86::VALIGNQZ128rmi) ? 8 : 4; |
| 163 | MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1); |
| 164 | Imm.setImm(Imm.getImm() * Scale); |
| 165 | break; |
| 166 | } |
Craig Topper | e5d44ce | 2017-11-04 18:10:03 +0000 | [diff] [blame] | 167 | case X86::VSHUFF32X4Z256rmi: |
| 168 | case X86::VSHUFF32X4Z256rri: |
| 169 | case X86::VSHUFF64X2Z256rmi: |
| 170 | case X86::VSHUFF64X2Z256rri: |
| 171 | case X86::VSHUFI32X4Z256rmi: |
| 172 | case X86::VSHUFI32X4Z256rri: |
| 173 | case X86::VSHUFI64X2Z256rmi: |
| 174 | case X86::VSHUFI64X2Z256rri: { |
| 175 | assert((NewOpc == X86::VPERM2F128rr || NewOpc == X86::VPERM2I128rr || |
| 176 | NewOpc == X86::VPERM2F128rm || NewOpc == X86::VPERM2I128rm) && |
| 177 | "Unexpected new opcode!"); |
| 178 | MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1); |
| 179 | int64_t ImmVal = Imm.getImm(); |
| 180 | // Set bit 5, move bit 1 to bit 4, copy bit 0. |
| 181 | Imm.setImm(0x20 | ((ImmVal & 2) << 3) | (ImmVal & 1)); |
| 182 | break; |
| 183 | } |
Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 184 | case X86::VRNDSCALEPDZ128rri: |
| 185 | case X86::VRNDSCALEPDZ128rmi: |
| 186 | case X86::VRNDSCALEPSZ128rri: |
| 187 | case X86::VRNDSCALEPSZ128rmi: |
| 188 | case X86::VRNDSCALEPDZ256rri: |
| 189 | case X86::VRNDSCALEPDZ256rmi: |
| 190 | case X86::VRNDSCALEPSZ256rri: |
| 191 | case X86::VRNDSCALEPSZ256rmi: |
Craig Topper | f43807d | 2018-06-15 04:42:54 +0000 | [diff] [blame] | 192 | case X86::VRNDSCALESDZr: |
| 193 | case X86::VRNDSCALESDZm: |
| 194 | case X86::VRNDSCALESSZr: |
| 195 | case X86::VRNDSCALESSZm: |
| 196 | case X86::VRNDSCALESDZr_Int: |
| 197 | case X86::VRNDSCALESDZm_Int: |
| 198 | case X86::VRNDSCALESSZr_Int: |
| 199 | case X86::VRNDSCALESSZm_Int: |
Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 200 | const MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1); |
| 201 | int64_t ImmVal = Imm.getImm(); |
| 202 | // Ensure that only bits 3:0 of the immediate are used. |
| 203 | if ((ImmVal & 0xf) != ImmVal) |
| 204 | return false; |
| 205 | break; |
Craig Topper | e5d44ce | 2017-11-04 18:10:03 +0000 | [diff] [blame] | 206 | } |
Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 207 | |
| 208 | return true; |
Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Craig Topper | 730414b | 2017-10-30 03:35:43 +0000 | [diff] [blame] | 211 | |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 212 | // For EVEX instructions that can be encoded using VEX encoding |
| 213 | // replace them by the VEX encoding in order to reduce size. |
| 214 | bool EvexToVexInstPass::CompressEvexToVexImpl(MachineInstr &MI) const { |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 215 | // VEX format. |
| 216 | // # of bytes: 0,2,3 1 1 0,1 0,1,2,4 0,1 |
| 217 | // [Prefixes] [VEX] OPCODE ModR/M [SIB] [DISP] [IMM] |
| 218 | // |
| 219 | // EVEX format. |
| 220 | // # of bytes: 4 1 1 1 4 / 1 1 |
| 221 | // [Prefixes] EVEX Opcode ModR/M [SIB] [Disp32] / [Disp8*N] [Immediate] |
| 222 | |
| 223 | const MCInstrDesc &Desc = MI.getDesc(); |
| 224 | |
| 225 | // Check for EVEX instructions only. |
| 226 | if ((Desc.TSFlags & X86II::EncodingMask) != X86II::EVEX) |
| 227 | return false; |
Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 228 | |
| 229 | // Check for EVEX instructions with mask or broadcast as in these cases |
| 230 | // the EVEX prefix is needed in order to carry this information |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 231 | // thus preventing the transformation to VEX encoding. |
| 232 | if (Desc.TSFlags & (X86II::EVEX_K | X86II::EVEX_B)) |
| 233 | return false; |
Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 234 | |
Craig Topper | 46c0b36 | 2018-06-19 03:17:46 +0000 | [diff] [blame] | 235 | // Check for EVEX instructions with L2 set. These instructions are 512-bits |
| 236 | // and can't be converted to VEX. |
| 237 | if (Desc.TSFlags & X86II::EVEX_L2) |
Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 238 | return false; |
| 239 | |
Craig Topper | d22ad85 | 2018-06-20 04:32:04 +0000 | [diff] [blame^] | 240 | #ifndef NDEBUG |
| 241 | // Make sure the tables are sorted. |
| 242 | static bool TableChecked = false; |
| 243 | if (!TableChecked) { |
| 244 | assert(std::is_sorted(std::begin(X86EvexToVex128CompressTable), |
| 245 | std::end(X86EvexToVex128CompressTable)) && |
| 246 | "X86EvexToVex128CompressTable is not sorted!"); |
| 247 | assert(std::is_sorted(std::begin(X86EvexToVex256CompressTable), |
| 248 | std::end(X86EvexToVex256CompressTable)) && |
| 249 | "X86EvexToVex256CompressTable is not sorted!"); |
| 250 | TableChecked = true; |
| 251 | } |
| 252 | #endif |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 253 | |
Craig Topper | 46c0b36 | 2018-06-19 03:17:46 +0000 | [diff] [blame] | 254 | // Use the VEX.L bit to select the 128 or 256-bit table. |
Craig Topper | d22ad85 | 2018-06-20 04:32:04 +0000 | [diff] [blame^] | 255 | ArrayRef<X86EvexToVexCompressTableEntry> Table = |
| 256 | (Desc.TSFlags & X86II::VEX_L) ? makeArrayRef(X86EvexToVex256CompressTable) |
| 257 | : makeArrayRef(X86EvexToVex128CompressTable); |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 258 | |
Craig Topper | d22ad85 | 2018-06-20 04:32:04 +0000 | [diff] [blame^] | 259 | auto I = std::lower_bound(Table.begin(), Table.end(), MI.getOpcode()); |
| 260 | if (I == Table.end() || I->EvexOpcode != MI.getOpcode()) |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 261 | return false; |
| 262 | |
Craig Topper | d22ad85 | 2018-06-20 04:32:04 +0000 | [diff] [blame^] | 263 | unsigned NewOpc = I->VexOpcode; |
| 264 | |
Craig Topper | 730414b | 2017-10-30 03:35:43 +0000 | [diff] [blame] | 265 | if (usesExtendedRegister(MI)) |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 266 | return false; |
Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 267 | |
Craig Topper | df99baa | 2018-02-13 04:19:26 +0000 | [diff] [blame] | 268 | if (!performCustomAdjustments(MI, NewOpc)) |
| 269 | return false; |
Craig Topper | 4e56ba2 | 2017-11-01 21:00:59 +0000 | [diff] [blame] | 270 | |
Craig Topper | c8483553 | 2017-10-30 03:35:44 +0000 | [diff] [blame] | 271 | MI.setDesc(TII->get(NewOpc)); |
Craig Topper | f27016f | 2018-03-10 05:15:22 +0000 | [diff] [blame] | 272 | MI.setAsmPrinterFlag(X86::AC_EVEX_2_VEX); |
Simon Pilgrim | 60ea09e | 2017-09-05 12:32:16 +0000 | [diff] [blame] | 273 | return true; |
Gadi Haber | 19c4fc5 | 2016-12-28 10:12:48 +0000 | [diff] [blame] | 274 | } |
Eugene Zelenko | fbd13c5 | 2017-02-02 22:55:55 +0000 | [diff] [blame] | 275 | |
| 276 | INITIALIZE_PASS(EvexToVexInstPass, EVEX2VEX_NAME, EVEX2VEX_DESC, false, false) |
| 277 | |
| 278 | FunctionPass *llvm::createX86EvexToVexInsts() { |
| 279 | return new EvexToVexInstPass(); |
| 280 | } |