blob: a7c5bab67f9437a855be73cf959f9e455c917535 [file] [log] [blame]
Eugene Zelenko60433b62017-10-05 00:33:50 +00001//===- X86EvexToVex.cpp ---------------------------------------------------===//
Gadi Haber19c4fc52016-12-28 10:12:48 +00002// Compress EVEX instructions to VEX encoding when possible to reduce code size
3//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Gadi Haber19c4fc52016-12-28 10:12:48 +00007//
Eugene Zelenko60433b62017-10-05 00:33:50 +00008//===----------------------------------------------------------------------===//
9//
Gadi Haber19c4fc52016-12-28 10:12:48 +000010/// \file
11/// This file defines the pass that goes over all AVX-512 instructions which
12/// are encoded using the EVEX prefix and if possible replaces them by their
13/// corresponding VEX encoding which is usually shorter by 2 bytes.
14/// EVEX instructions may be encoded via the VEX prefix when the AVX-512
15/// instruction has a corresponding AVX/AVX2 opcode and when it does not
Craig Topperfbb19852017-10-17 04:17:54 +000016/// use the xmm or the mask registers or xmm/ymm registers with indexes
Gadi Haber19c4fc52016-12-28 10:12:48 +000017/// higher than 15.
18/// The pass applies code reduction on the generated code for AVX-512 instrs.
Eugene Zelenko60433b62017-10-05 00:33:50 +000019//
20//===----------------------------------------------------------------------===//
Gadi Haber19c4fc52016-12-28 10:12:48 +000021
22#include "InstPrinter/X86InstComments.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000023#include "MCTargetDesc/X86BaseInfo.h"
Gadi Haber19c4fc52016-12-28 10:12:48 +000024#include "X86.h"
Gadi Haber19c4fc52016-12-28 10:12:48 +000025#include "X86InstrInfo.h"
Gadi Haber19c4fc52016-12-28 10:12:48 +000026#include "X86Subtarget.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000027#include "llvm/ADT/StringRef.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/CodeGen/MachineOperand.h"
32#include "llvm/MC/MCInstrDesc.h"
33#include "llvm/Pass.h"
34#include <cassert>
35#include <cstdint>
Gadi Haber19c4fc52016-12-28 10:12:48 +000036
37using namespace llvm;
38
Ayman Musa850fc972017-03-07 08:11:19 +000039// Including the generated EVEX2VEX tables.
40struct X86EvexToVexCompressTableEntry {
41 uint16_t EvexOpcode;
42 uint16_t VexOpcode;
Craig Topperd22ad852018-06-20 04:32:04 +000043
44 bool operator<(const X86EvexToVexCompressTableEntry &RHS) const {
45 return EvexOpcode < RHS.EvexOpcode;
46 }
47
48 friend bool operator<(const X86EvexToVexCompressTableEntry &TE,
49 unsigned Opc) {
50 return TE.EvexOpcode < Opc;
51 }
Ayman Musa850fc972017-03-07 08:11:19 +000052};
53#include "X86GenEVEX2VEXTables.inc"
54
Gadi Haber19c4fc52016-12-28 10:12:48 +000055#define EVEX2VEX_DESC "Compressing EVEX instrs to VEX encoding when possible"
56#define EVEX2VEX_NAME "x86-evex-to-vex-compress"
57
58#define DEBUG_TYPE EVEX2VEX_NAME
59
60namespace {
61
62class EvexToVexInstPass : public MachineFunctionPass {
63
Gadi Haber19c4fc52016-12-28 10:12:48 +000064 /// For EVEX instructions that can be encoded using VEX encoding, replace
65 /// them by the VEX encoding in order to reduce size.
66 bool CompressEvexToVexImpl(MachineInstr &MI) const;
67
Gadi Haber19c4fc52016-12-28 10:12:48 +000068public:
69 static char ID;
70
Gadi Haber19c4fc52016-12-28 10:12:48 +000071 EvexToVexInstPass() : MachineFunctionPass(ID) {
72 initializeEvexToVexInstPassPass(*PassRegistry::getPassRegistry());
Gadi Haber19c4fc52016-12-28 10:12:48 +000073 }
74
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000075 StringRef getPassName() const override { return EVEX2VEX_DESC; }
76
Gadi Haber19c4fc52016-12-28 10:12:48 +000077 /// Loop over all of the basic blocks, replacing EVEX instructions
78 /// by equivalent VEX instructions when possible for reducing code size.
79 bool runOnMachineFunction(MachineFunction &MF) override;
80
81 // This pass runs after regalloc and doesn't support VReg operands.
82 MachineFunctionProperties getRequiredProperties() const override {
83 return MachineFunctionProperties().set(
84 MachineFunctionProperties::Property::NoVRegs);
85 }
86
87private:
88 /// Machine instruction info used throughout the class.
89 const X86InstrInfo *TII;
90};
91
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000092} // end anonymous namespace
Gadi Haber19c4fc52016-12-28 10:12:48 +000093
Eugene Zelenko60433b62017-10-05 00:33:50 +000094char EvexToVexInstPass::ID = 0;
95
Gadi Haber19c4fc52016-12-28 10:12:48 +000096bool EvexToVexInstPass::runOnMachineFunction(MachineFunction &MF) {
97 TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
98
99 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
100 if (!ST.hasAVX512())
101 return false;
102
103 bool Changed = false;
104
105 /// Go over all basic blocks in function and replace
106 /// EVEX encoded instrs by VEX encoding when possible.
107 for (MachineBasicBlock &MBB : MF) {
108
Simon Pilgrim60ea09e2017-09-05 12:32:16 +0000109 // Traverse the basic block.
110 for (MachineInstr &MI : MBB)
Gadi Haber19c4fc52016-12-28 10:12:48 +0000111 Changed |= CompressEvexToVexImpl(MI);
112 }
113
114 return Changed;
115}
116
Craig Topper730414b2017-10-30 03:35:43 +0000117static bool usesExtendedRegister(const MachineInstr &MI) {
118 auto isHiRegIdx = [](unsigned Reg) {
119 // Check for XMM register with indexes between 16 - 31.
120 if (Reg >= X86::XMM16 && Reg <= X86::XMM31)
121 return true;
122
123 // Check for YMM register with indexes between 16 - 31.
124 if (Reg >= X86::YMM16 && Reg <= X86::YMM31)
125 return true;
126
127 return false;
128 };
129
130 // Check that operands are not ZMM regs or
131 // XMM/YMM regs with hi indexes between 16 - 31.
132 for (const MachineOperand &MO : MI.explicit_operands()) {
133 if (!MO.isReg())
134 continue;
135
136 unsigned Reg = MO.getReg();
137
138 assert(!(Reg >= X86::ZMM0 && Reg <= X86::ZMM31) &&
139 "ZMM instructions should not be in the EVEX->VEX tables");
140
141 if (isHiRegIdx(Reg))
142 return true;
143 }
144
145 return false;
146}
147
Craig Topper4e56ba22017-11-01 21:00:59 +0000148// Do any custom cleanup needed to finalize the conversion.
Craig Topperdf99baa2018-02-13 04:19:26 +0000149static bool performCustomAdjustments(MachineInstr &MI, unsigned NewOpc) {
Craig Topper4e56ba22017-11-01 21:00:59 +0000150 (void)NewOpc;
151 unsigned Opc = MI.getOpcode();
152 switch (Opc) {
153 case X86::VALIGNDZ128rri:
154 case X86::VALIGNDZ128rmi:
155 case X86::VALIGNQZ128rri:
Craig Toppere5d44ce2017-11-04 18:10:03 +0000156 case X86::VALIGNQZ128rmi: {
Craig Topper4e56ba22017-11-01 21:00:59 +0000157 assert((NewOpc == X86::VPALIGNRrri || NewOpc == X86::VPALIGNRrmi) &&
158 "Unexpected new opcode!");
159 unsigned Scale = (Opc == X86::VALIGNQZ128rri ||
160 Opc == X86::VALIGNQZ128rmi) ? 8 : 4;
161 MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1);
162 Imm.setImm(Imm.getImm() * Scale);
163 break;
164 }
Craig Toppere5d44ce2017-11-04 18:10:03 +0000165 case X86::VSHUFF32X4Z256rmi:
166 case X86::VSHUFF32X4Z256rri:
167 case X86::VSHUFF64X2Z256rmi:
168 case X86::VSHUFF64X2Z256rri:
169 case X86::VSHUFI32X4Z256rmi:
170 case X86::VSHUFI32X4Z256rri:
171 case X86::VSHUFI64X2Z256rmi:
172 case X86::VSHUFI64X2Z256rri: {
173 assert((NewOpc == X86::VPERM2F128rr || NewOpc == X86::VPERM2I128rr ||
174 NewOpc == X86::VPERM2F128rm || NewOpc == X86::VPERM2I128rm) &&
175 "Unexpected new opcode!");
176 MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1);
177 int64_t ImmVal = Imm.getImm();
178 // Set bit 5, move bit 1 to bit 4, copy bit 0.
179 Imm.setImm(0x20 | ((ImmVal & 2) << 3) | (ImmVal & 1));
180 break;
181 }
Craig Topperdf99baa2018-02-13 04:19:26 +0000182 case X86::VRNDSCALEPDZ128rri:
183 case X86::VRNDSCALEPDZ128rmi:
184 case X86::VRNDSCALEPSZ128rri:
185 case X86::VRNDSCALEPSZ128rmi:
186 case X86::VRNDSCALEPDZ256rri:
187 case X86::VRNDSCALEPDZ256rmi:
188 case X86::VRNDSCALEPSZ256rri:
189 case X86::VRNDSCALEPSZ256rmi:
Craig Topperf43807d2018-06-15 04:42:54 +0000190 case X86::VRNDSCALESDZr:
191 case X86::VRNDSCALESDZm:
192 case X86::VRNDSCALESSZr:
193 case X86::VRNDSCALESSZm:
194 case X86::VRNDSCALESDZr_Int:
195 case X86::VRNDSCALESDZm_Int:
196 case X86::VRNDSCALESSZr_Int:
197 case X86::VRNDSCALESSZm_Int:
Craig Topperdf99baa2018-02-13 04:19:26 +0000198 const MachineOperand &Imm = MI.getOperand(MI.getNumExplicitOperands()-1);
199 int64_t ImmVal = Imm.getImm();
200 // Ensure that only bits 3:0 of the immediate are used.
201 if ((ImmVal & 0xf) != ImmVal)
202 return false;
203 break;
Craig Toppere5d44ce2017-11-04 18:10:03 +0000204 }
Craig Topperdf99baa2018-02-13 04:19:26 +0000205
206 return true;
Craig Topper4e56ba22017-11-01 21:00:59 +0000207}
208
Craig Topper730414b2017-10-30 03:35:43 +0000209
Gadi Haber19c4fc52016-12-28 10:12:48 +0000210// For EVEX instructions that can be encoded using VEX encoding
211// replace them by the VEX encoding in order to reduce size.
212bool EvexToVexInstPass::CompressEvexToVexImpl(MachineInstr &MI) const {
Gadi Haber19c4fc52016-12-28 10:12:48 +0000213 // VEX format.
214 // # of bytes: 0,2,3 1 1 0,1 0,1,2,4 0,1
215 // [Prefixes] [VEX] OPCODE ModR/M [SIB] [DISP] [IMM]
216 //
217 // EVEX format.
218 // # of bytes: 4 1 1 1 4 / 1 1
219 // [Prefixes] EVEX Opcode ModR/M [SIB] [Disp32] / [Disp8*N] [Immediate]
220
221 const MCInstrDesc &Desc = MI.getDesc();
222
223 // Check for EVEX instructions only.
224 if ((Desc.TSFlags & X86II::EncodingMask) != X86II::EVEX)
225 return false;
Simon Pilgrim60ea09e2017-09-05 12:32:16 +0000226
227 // Check for EVEX instructions with mask or broadcast as in these cases
228 // the EVEX prefix is needed in order to carry this information
Gadi Haber19c4fc52016-12-28 10:12:48 +0000229 // thus preventing the transformation to VEX encoding.
230 if (Desc.TSFlags & (X86II::EVEX_K | X86II::EVEX_B))
231 return false;
Simon Pilgrim60ea09e2017-09-05 12:32:16 +0000232
Craig Topper46c0b362018-06-19 03:17:46 +0000233 // Check for EVEX instructions with L2 set. These instructions are 512-bits
234 // and can't be converted to VEX.
235 if (Desc.TSFlags & X86II::EVEX_L2)
Simon Pilgrim60ea09e2017-09-05 12:32:16 +0000236 return false;
237
Craig Topperd22ad852018-06-20 04:32:04 +0000238#ifndef NDEBUG
239 // Make sure the tables are sorted.
Benjamin Kramerf9613b22018-06-28 10:03:45 +0000240 static std::atomic<bool> TableChecked(false);
241 if (!TableChecked.load(std::memory_order_relaxed)) {
Craig Topperd22ad852018-06-20 04:32:04 +0000242 assert(std::is_sorted(std::begin(X86EvexToVex128CompressTable),
243 std::end(X86EvexToVex128CompressTable)) &&
244 "X86EvexToVex128CompressTable is not sorted!");
245 assert(std::is_sorted(std::begin(X86EvexToVex256CompressTable),
246 std::end(X86EvexToVex256CompressTable)) &&
247 "X86EvexToVex256CompressTable is not sorted!");
Hans Wennborga2573762018-06-28 10:24:38 +0000248 TableChecked.store(true, std::memory_order_relaxed);
Craig Topperd22ad852018-06-20 04:32:04 +0000249 }
250#endif
Gadi Haber19c4fc52016-12-28 10:12:48 +0000251
Craig Topper46c0b362018-06-19 03:17:46 +0000252 // Use the VEX.L bit to select the 128 or 256-bit table.
Craig Topperd22ad852018-06-20 04:32:04 +0000253 ArrayRef<X86EvexToVexCompressTableEntry> Table =
254 (Desc.TSFlags & X86II::VEX_L) ? makeArrayRef(X86EvexToVex256CompressTable)
255 : makeArrayRef(X86EvexToVex128CompressTable);
Gadi Haber19c4fc52016-12-28 10:12:48 +0000256
Craig Topperd22ad852018-06-20 04:32:04 +0000257 auto I = std::lower_bound(Table.begin(), Table.end(), MI.getOpcode());
258 if (I == Table.end() || I->EvexOpcode != MI.getOpcode())
Gadi Haber19c4fc52016-12-28 10:12:48 +0000259 return false;
260
Craig Topperd22ad852018-06-20 04:32:04 +0000261 unsigned NewOpc = I->VexOpcode;
262
Craig Topper730414b2017-10-30 03:35:43 +0000263 if (usesExtendedRegister(MI))
Gadi Haber19c4fc52016-12-28 10:12:48 +0000264 return false;
Simon Pilgrim60ea09e2017-09-05 12:32:16 +0000265
Craig Topperdf99baa2018-02-13 04:19:26 +0000266 if (!performCustomAdjustments(MI, NewOpc))
267 return false;
Craig Topper4e56ba22017-11-01 21:00:59 +0000268
Craig Topperc84835532017-10-30 03:35:44 +0000269 MI.setDesc(TII->get(NewOpc));
Craig Topperf27016f2018-03-10 05:15:22 +0000270 MI.setAsmPrinterFlag(X86::AC_EVEX_2_VEX);
Simon Pilgrim60ea09e2017-09-05 12:32:16 +0000271 return true;
Gadi Haber19c4fc52016-12-28 10:12:48 +0000272}
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000273
274INITIALIZE_PASS(EvexToVexInstPass, EVEX2VEX_NAME, EVEX2VEX_DESC, false, false)
275
276FunctionPass *llvm::createX86EvexToVexInsts() {
277 return new EvexToVexInstPass();
278}