blob: d1a17c5500d65c70be0fa8859953b366f4e100f8 [file] [log] [blame]
Richard Sandiford35ec4e3562013-09-25 10:11:07 +00001//===-- SystemZShortenInst.cpp - Instruction-shortening pass --------------===//
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 pass tries to replace instructions with shorter forms. For example,
11// IILF can be replaced with LLILL or LLILH if the constant fits and if the
12// other 32 bits of the GR64 destination are not live.
13//
14//===----------------------------------------------------------------------===//
15
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000016#include "SystemZTargetMachine.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
Ulrich Weigand49506d72015-05-05 19:28:34 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000019
20using namespace llvm;
21
Chandler Carruth84e68b22014-04-22 02:41:26 +000022#define DEBUG_TYPE "systemz-shorten-inst"
23
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000024namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +000025class SystemZShortenInst : public MachineFunctionPass {
26public:
27 static char ID;
28 SystemZShortenInst(const SystemZTargetMachine &tm);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000029
Richard Sandifordb4d67b52014-03-06 12:03:36 +000030 const char *getPassName() const override {
Richard Sandifordc2312692014-03-06 10:38:30 +000031 return "SystemZ Instruction Shortening";
32 }
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000033
Richard Sandiford28c111e2014-03-06 11:00:15 +000034 bool processBlock(MachineBasicBlock &MBB);
Craig Topper9d74a5a2014-04-29 07:58:41 +000035 bool runOnMachineFunction(MachineFunction &F) override;
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000036
Richard Sandifordc2312692014-03-06 10:38:30 +000037private:
38 bool shortenIIF(MachineInstr &MI, unsigned *GPRMap, unsigned LiveOther,
39 unsigned LLIxL, unsigned LLIxH);
Ulrich Weigand49506d72015-05-05 19:28:34 +000040 bool shortenOn0(MachineInstr &MI, unsigned Opcode);
41 bool shortenOn01(MachineInstr &MI, unsigned Opcode);
42 bool shortenOn001(MachineInstr &MI, unsigned Opcode);
43 bool shortenFPConv(MachineInstr &MI, unsigned Opcode);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000044
Richard Sandifordc2312692014-03-06 10:38:30 +000045 const SystemZInstrInfo *TII;
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000046
Richard Sandifordc2312692014-03-06 10:38:30 +000047 // LowGPRs[I] has bit N set if LLVM register I includes the low
48 // word of GPR N. HighGPRs is the same for the high word.
49 unsigned LowGPRs[SystemZ::NUM_TARGET_REGS];
50 unsigned HighGPRs[SystemZ::NUM_TARGET_REGS];
51};
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000052
Richard Sandifordc2312692014-03-06 10:38:30 +000053char SystemZShortenInst::ID = 0;
54} // end anonymous namespace
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000055
56FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) {
57 return new SystemZShortenInst(TM);
58}
59
60SystemZShortenInst::SystemZShortenInst(const SystemZTargetMachine &tm)
Craig Topper062a2ba2014-04-25 05:30:21 +000061 : MachineFunctionPass(ID), TII(nullptr), LowGPRs(), HighGPRs() {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000062 // Set up LowGPRs and HighGPRs.
63 for (unsigned I = 0; I < 16; ++I) {
64 LowGPRs[SystemZMC::GR32Regs[I]] |= 1 << I;
65 LowGPRs[SystemZMC::GR64Regs[I]] |= 1 << I;
Richard Sandiford01240232013-10-01 13:02:28 +000066 HighGPRs[SystemZMC::GRH32Regs[I]] |= 1 << I;
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000067 HighGPRs[SystemZMC::GR64Regs[I]] |= 1 << I;
68 if (unsigned GR128 = SystemZMC::GR128Regs[I]) {
69 LowGPRs[GR128] |= 3 << I;
70 HighGPRs[GR128] |= 3 << I;
71 }
72 }
73}
74
75// MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH
76// are the halfword immediate loads for the same word. Try to use one of them
77// instead of IIxF. If MI loads the high word, GPRMap[X] is the set of high
78// words referenced by LLVM register X while LiveOther is the mask of low
79// words that are currently live, and vice versa.
80bool SystemZShortenInst::shortenIIF(MachineInstr &MI, unsigned *GPRMap,
81 unsigned LiveOther, unsigned LLIxL,
82 unsigned LLIxH) {
83 unsigned Reg = MI.getOperand(0).getReg();
84 assert(Reg < SystemZ::NUM_TARGET_REGS && "Invalid register number");
85 unsigned GPRs = GPRMap[Reg];
86 assert(GPRs != 0 && "Register must be a GPR");
87 if (GPRs & LiveOther)
88 return false;
89
90 uint64_t Imm = MI.getOperand(1).getImm();
91 if (SystemZ::isImmLL(Imm)) {
92 MI.setDesc(TII->get(LLIxL));
93 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
94 return true;
95 }
96 if (SystemZ::isImmLH(Imm)) {
97 MI.setDesc(TII->get(LLIxH));
98 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
99 MI.getOperand(1).setImm(Imm >> 16);
100 return true;
101 }
102 return false;
103}
104
Ulrich Weigand49506d72015-05-05 19:28:34 +0000105// Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
106bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
107 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) {
108 MI.setDesc(TII->get(Opcode));
109 return true;
110 }
111 return false;
112}
113
114// Change MI's opcode to Opcode if register operands 0 and 1 have a
115// 4-bit encoding.
116bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) {
117 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
118 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
119 MI.setDesc(TII->get(Opcode));
120 return true;
121 }
122 return false;
123}
124
125// Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
126// 4-bit encoding and if operands 0 and 1 are tied.
127bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) {
128 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
129 MI.getOperand(1).getReg() == MI.getOperand(0).getReg() &&
130 SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) {
131 MI.setDesc(TII->get(Opcode));
132 return true;
133 }
134 return false;
135}
136
137// MI is a vector-style conversion instruction with the operand order:
138// destination, source, exact-suppress, rounding-mode. If both registers
139// have a 4-bit encoding then change it to Opcode, which has operand order:
140// destination, rouding-mode, source, exact-suppress.
141bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) {
142 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
143 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
144 MachineOperand Dest(MI.getOperand(0));
145 MachineOperand Src(MI.getOperand(1));
146 MachineOperand Suppress(MI.getOperand(2));
147 MachineOperand Mode(MI.getOperand(3));
148 MI.RemoveOperand(3);
149 MI.RemoveOperand(2);
150 MI.RemoveOperand(1);
151 MI.RemoveOperand(0);
152 MI.setDesc(TII->get(Opcode));
153 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
154 .addOperand(Dest)
155 .addOperand(Mode)
156 .addOperand(Src)
157 .addOperand(Suppress);
158 return true;
159 }
160 return false;
161}
162
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000163// Process all instructions in MBB. Return true if something changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000164bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000165 bool Changed = false;
166
167 // Work out which words are live on exit from the block.
168 unsigned LiveLow = 0;
169 unsigned LiveHigh = 0;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000170 for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI) {
171 for (auto LI = (*SI)->livein_begin(), LE = (*SI)->livein_end();
172 LI != LE; ++LI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000173 unsigned Reg = *LI;
174 assert(Reg < SystemZ::NUM_TARGET_REGS && "Invalid register number");
175 LiveLow |= LowGPRs[Reg];
176 LiveHigh |= HighGPRs[Reg];
177 }
178 }
179
180 // Iterate backwards through the block looking for instructions to change.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000181 for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000182 MachineInstr &MI = *MBBI;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000183 switch (MI.getOpcode()) {
184 case SystemZ::IILF:
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000185 Changed |= shortenIIF(MI, LowGPRs, LiveHigh, SystemZ::LLILL,
186 SystemZ::LLILH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000187 break;
188
189 case SystemZ::IIHF:
Richard Sandiford01240232013-10-01 13:02:28 +0000190 Changed |= shortenIIF(MI, HighGPRs, LiveLow, SystemZ::LLIHL,
191 SystemZ::LLIHH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000192 break;
193
194 case SystemZ::WFADB:
195 Changed |= shortenOn001(MI, SystemZ::ADBR);
196 break;
197
198 case SystemZ::WFDDB:
199 Changed |= shortenOn001(MI, SystemZ::DDBR);
200 break;
201
202 case SystemZ::WFIDB:
203 Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
204 break;
205
206 case SystemZ::WLDEB:
207 Changed |= shortenOn01(MI, SystemZ::LDEBR);
208 break;
209
210 case SystemZ::WLEDB:
211 Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
212 break;
213
214 case SystemZ::WFMDB:
215 Changed |= shortenOn001(MI, SystemZ::MDBR);
216 break;
217
218 case SystemZ::WFLCDB:
219 Changed |= shortenOn01(MI, SystemZ::LCDBR);
220 break;
221
222 case SystemZ::WFLNDB:
223 Changed |= shortenOn01(MI, SystemZ::LNDBR);
224 break;
225
226 case SystemZ::WFLPDB:
227 Changed |= shortenOn01(MI, SystemZ::LPDBR);
228 break;
229
230 case SystemZ::WFSQDB:
231 Changed |= shortenOn01(MI, SystemZ::SQDBR);
232 break;
233
234 case SystemZ::WFSDB:
235 Changed |= shortenOn001(MI, SystemZ::SDBR);
236 break;
237
238 case SystemZ::WFCDB:
239 Changed |= shortenOn01(MI, SystemZ::CDBR);
240 break;
241
242 case SystemZ::VL32:
243 // For z13 we prefer LDE over LE to avoid partial register dependencies.
244 Changed |= shortenOn0(MI, SystemZ::LDE32);
245 break;
246
247 case SystemZ::VST32:
248 Changed |= shortenOn0(MI, SystemZ::STE);
249 break;
250
251 case SystemZ::VL64:
252 Changed |= shortenOn0(MI, SystemZ::LD);
253 break;
254
255 case SystemZ::VST64:
256 Changed |= shortenOn0(MI, SystemZ::STD);
257 break;
258 }
259
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000260 unsigned UsedLow = 0;
261 unsigned UsedHigh = 0;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000262 for (auto MOI = MI.operands_begin(), MOE = MI.operands_end();
263 MOI != MOE; ++MOI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000264 MachineOperand &MO = *MOI;
265 if (MO.isReg()) {
266 if (unsigned Reg = MO.getReg()) {
267 assert(Reg < SystemZ::NUM_TARGET_REGS && "Invalid register number");
268 if (MO.isDef()) {
269 LiveLow &= ~LowGPRs[Reg];
270 LiveHigh &= ~HighGPRs[Reg];
271 } else if (!MO.isUndef()) {
272 UsedLow |= LowGPRs[Reg];
273 UsedHigh |= HighGPRs[Reg];
274 }
275 }
276 }
277 }
278 LiveLow |= UsedLow;
279 LiveHigh |= UsedHigh;
280 }
281
282 return Changed;
283}
284
285bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000286 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000287
288 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000289 for (auto &MBB : F)
290 Changed |= processBlock(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000291
292 return Changed;
293}