blob: c4dbbee4f136ddd6fda01b095ef1ac602d3cd87f [file] [log] [blame]
Gadi Haber19c4fc52016-12-28 10:12:48 +00001//===----------------------- X86EvexToVex.cpp ----------------------------===//
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//
9//===---------------------------------------------------------------------===//
10/// \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
16/// use the xmm or the mask registers or xmm/ymm registers wuith indexes
17/// higher than 15.
18/// The pass applies code reduction on the generated code for AVX-512 instrs.
19///
20//===---------------------------------------------------------------------===//
21
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"
26#include "X86InstrTablesInfo.h"
Gadi Haber19c4fc52016-12-28 10:12:48 +000027#include "X86Subtarget.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000028#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 Haber19c4fc52016-12-28 10:12:48 +000038
39using namespace llvm;
40
41#define EVEX2VEX_DESC "Compressing EVEX instrs to VEX encoding when possible"
42#define EVEX2VEX_NAME "x86-evex-to-vex-compress"
43
44#define DEBUG_TYPE EVEX2VEX_NAME
45
46namespace {
47
48class EvexToVexInstPass : public MachineFunctionPass {
49
50 /// X86EvexToVexCompressTable - Evex to Vex encoding opcode map.
51 typedef DenseMap<unsigned, uint16_t> EvexToVexTableType;
52 EvexToVexTableType EvexToVex128Table;
53 EvexToVexTableType EvexToVex256Table;
54
55 /// For EVEX instructions that can be encoded using VEX encoding, replace
56 /// them by the VEX encoding in order to reduce size.
57 bool CompressEvexToVexImpl(MachineInstr &MI) const;
58
59 /// For initializing the hash map tables of all AVX-512 EVEX
60 /// corresponding to AVX/AVX2 opcodes.
61 void AddTableEntry(EvexToVexTableType &EvexToVexTable, uint16_t EvexOp,
62 uint16_t VexOp);
63
64public:
65 static char ID;
66
Gadi Haber19c4fc52016-12-28 10:12:48 +000067 EvexToVexInstPass() : MachineFunctionPass(ID) {
68 initializeEvexToVexInstPassPass(*PassRegistry::getPassRegistry());
69
70 // Initialize the EVEX to VEX 128 table map.
71 for (X86EvexToVexCompressTableEntry Entry : X86EvexToVex128CompressTable) {
72 AddTableEntry(EvexToVex128Table, Entry.EvexOpcode, Entry.VexOpcode);
73 }
74
75 // Initialize the EVEX to VEX 256 table map.
76 for (X86EvexToVexCompressTableEntry Entry : X86EvexToVex256CompressTable) {
77 AddTableEntry(EvexToVex256Table, Entry.EvexOpcode, Entry.VexOpcode);
78 }
79 }
80
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000081 StringRef getPassName() const override { return EVEX2VEX_DESC; }
82
Gadi Haber19c4fc52016-12-28 10:12:48 +000083 /// Loop over all of the basic blocks, replacing EVEX instructions
84 /// by equivalent VEX instructions when possible for reducing code size.
85 bool runOnMachineFunction(MachineFunction &MF) override;
86
87 // This pass runs after regalloc and doesn't support VReg operands.
88 MachineFunctionProperties getRequiredProperties() const override {
89 return MachineFunctionProperties().set(
90 MachineFunctionProperties::Property::NoVRegs);
91 }
92
93private:
94 /// Machine instruction info used throughout the class.
95 const X86InstrInfo *TII;
96};
97
98char EvexToVexInstPass::ID = 0;
Gadi Haber19c4fc52016-12-28 10:12:48 +000099
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000100} // end anonymous namespace
Gadi Haber19c4fc52016-12-28 10:12:48 +0000101
102bool EvexToVexInstPass::runOnMachineFunction(MachineFunction &MF) {
103 TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
104
105 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
106 if (!ST.hasAVX512())
107 return false;
108
109 bool Changed = false;
110
111 /// Go over all basic blocks in function and replace
112 /// EVEX encoded instrs by VEX encoding when possible.
113 for (MachineBasicBlock &MBB : MF) {
114
115 // Traverse the basic block.
116 for (MachineInstr &MI : MBB)
117 Changed |= CompressEvexToVexImpl(MI);
118 }
119
120 return Changed;
121}
122
123void EvexToVexInstPass::AddTableEntry(EvexToVexTableType &EvexToVexTable,
124 uint16_t EvexOp, uint16_t VexOp) {
125 EvexToVexTable[EvexOp] = VexOp;
126}
127
128// For EVEX instructions that can be encoded using VEX encoding
129// replace them by the VEX encoding in order to reduce size.
130bool EvexToVexInstPass::CompressEvexToVexImpl(MachineInstr &MI) const {
Gadi Haber19c4fc52016-12-28 10:12:48 +0000131 // VEX format.
132 // # of bytes: 0,2,3 1 1 0,1 0,1,2,4 0,1
133 // [Prefixes] [VEX] OPCODE ModR/M [SIB] [DISP] [IMM]
134 //
135 // EVEX format.
136 // # of bytes: 4 1 1 1 4 / 1 1
137 // [Prefixes] EVEX Opcode ModR/M [SIB] [Disp32] / [Disp8*N] [Immediate]
138
139 const MCInstrDesc &Desc = MI.getDesc();
140
141 // Check for EVEX instructions only.
142 if ((Desc.TSFlags & X86II::EncodingMask) != X86II::EVEX)
143 return false;
144
145 // Check for EVEX instructions with mask or broadcast as in these cases
146 // the EVEX prefix is needed in order to carry this information
147 // thus preventing the transformation to VEX encoding.
148 if (Desc.TSFlags & (X86II::EVEX_K | X86II::EVEX_B))
149 return false;
150
151 // Check for non EVEX_V512 instrs only.
152 // EVEX_V512 instr: bit EVEX_L2 = 1; bit VEX_L = 0.
153 if ((Desc.TSFlags & X86II::EVEX_L2) && !(Desc.TSFlags & X86II::VEX_L))
154 return false;
155
156 // EVEX_V128 instr: bit EVEX_L2 = 0, bit VEX_L = 0.
157 bool IsEVEX_V128 =
158 (!(Desc.TSFlags & X86II::EVEX_L2) && !(Desc.TSFlags & X86II::VEX_L));
159
160 // EVEX_V256 instr: bit EVEX_L2 = 0, bit VEX_L = 1.
161 bool IsEVEX_V256 =
162 (!(Desc.TSFlags & X86II::EVEX_L2) && (Desc.TSFlags & X86II::VEX_L));
163
164 unsigned NewOpc = 0;
165
166 // Check for EVEX_V256 instructions.
167 if (IsEVEX_V256) {
168 // Search for opcode in the EvexToVex256 table.
169 auto It = EvexToVex256Table.find(MI.getOpcode());
170 if (It != EvexToVex256Table.end())
171 NewOpc = It->second;
172 }
173
174 // Check for EVEX_V128 or Scalar instructions.
175 else if (IsEVEX_V128) {
176 // Search for opcode in the EvexToVex128 table.
177 auto It = EvexToVex128Table.find(MI.getOpcode());
178 if (It != EvexToVex128Table.end())
179 NewOpc = It->second;
180 }
181
182 if (!NewOpc)
183 return false;
184
185 auto isHiRegIdx = [](unsigned Reg) {
186 // Check for XMM register with indexes between 16 - 31.
187 if (Reg >= X86::XMM16 && Reg <= X86::XMM31)
188 return true;
189
190 // Check for YMM register with indexes between 16 - 31.
191 if (Reg >= X86::YMM16 && Reg <= X86::YMM31)
192 return true;
193
194 return false;
195 };
196
197 // Check that operands are not ZMM regs or
198 // XMM/YMM regs with hi indexes between 16 - 31.
199 for (const MachineOperand &MO : MI.explicit_operands()) {
200 if (!MO.isReg())
201 continue;
202
203 unsigned Reg = MO.getReg();
204
205 assert (!(Reg >= X86::ZMM0 && Reg <= X86::ZMM31));
206
207 if (isHiRegIdx(Reg))
208 return false;
209 }
210
211 const MCInstrDesc &MCID = TII->get(NewOpc);
212 MI.setDesc(MCID);
213 MI.setAsmPrinterFlag(AC_EVEX_2_VEX);
214 return true;
215}
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000216
217INITIALIZE_PASS(EvexToVexInstPass, EVEX2VEX_NAME, EVEX2VEX_DESC, false, false)
218
219FunctionPass *llvm::createX86EvexToVexInsts() {
220 return new EvexToVexInstPass();
221}