blob: 87db4f46fcdb1ea4945fa4e6fed92d7cd3270b18 [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"
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000019#include "llvm/CodeGen/LivePhysRegs.h"
20#include "llvm/Target/TargetRegisterInfo.h"
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000021
22using namespace llvm;
23
Chandler Carruth84e68b22014-04-22 02:41:26 +000024#define DEBUG_TYPE "systemz-shorten-inst"
25
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000026namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +000027class SystemZShortenInst : public MachineFunctionPass {
28public:
29 static char ID;
30 SystemZShortenInst(const SystemZTargetMachine &tm);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000031
Richard Sandifordb4d67b52014-03-06 12:03:36 +000032 const char *getPassName() const override {
Richard Sandifordc2312692014-03-06 10:38:30 +000033 return "SystemZ Instruction Shortening";
34 }
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000035
Richard Sandiford28c111e2014-03-06 11:00:15 +000036 bool processBlock(MachineBasicBlock &MBB);
Craig Topper9d74a5a2014-04-29 07:58:41 +000037 bool runOnMachineFunction(MachineFunction &F) override;
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000038
Richard Sandifordc2312692014-03-06 10:38:30 +000039private:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000040 bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH);
Ulrich Weigand49506d72015-05-05 19:28:34 +000041 bool shortenOn0(MachineInstr &MI, unsigned Opcode);
42 bool shortenOn01(MachineInstr &MI, unsigned Opcode);
43 bool shortenOn001(MachineInstr &MI, unsigned Opcode);
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000044 bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode);
Ulrich Weigand49506d72015-05-05 19:28:34 +000045 bool shortenFPConv(MachineInstr &MI, unsigned Opcode);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000046
Richard Sandifordc2312692014-03-06 10:38:30 +000047 const SystemZInstrInfo *TII;
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000048 const TargetRegisterInfo *TRI;
49 LivePhysRegs LiveRegs;
Richard Sandifordc2312692014-03-06 10:38:30 +000050};
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000051
Richard Sandifordc2312692014-03-06 10:38:30 +000052char SystemZShortenInst::ID = 0;
53} // end anonymous namespace
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000054
55FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) {
56 return new SystemZShortenInst(TM);
57}
58
59SystemZShortenInst::SystemZShortenInst(const SystemZTargetMachine &tm)
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000060 : MachineFunctionPass(ID), TII(nullptr) {}
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000061
62// MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH
63// are the halfword immediate loads for the same word. Try to use one of them
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000064// instead of IIxF.
65bool SystemZShortenInst::shortenIIF(MachineInstr &MI,
66 unsigned LLIxL, unsigned LLIxH) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000067 unsigned Reg = MI.getOperand(0).getReg();
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000068 // The new opcode will clear the other half of the GR64 reg, so
69 // cancel if that is live.
70 unsigned thisSubRegIdx = (SystemZ::GRH32BitRegClass.contains(Reg) ?
71 SystemZ::subreg_h32 : SystemZ::subreg_l32);
72 unsigned otherSubRegIdx = (thisSubRegIdx == SystemZ::subreg_l32 ?
73 SystemZ::subreg_h32 : SystemZ::subreg_l32);
74 unsigned GR64BitReg = TRI->getMatchingSuperReg(Reg, thisSubRegIdx,
75 &SystemZ::GR64BitRegClass);
76 unsigned OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
77 if (LiveRegs.contains(OtherReg))
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000078 return false;
79
80 uint64_t Imm = MI.getOperand(1).getImm();
81 if (SystemZ::isImmLL(Imm)) {
82 MI.setDesc(TII->get(LLIxL));
83 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
84 return true;
85 }
86 if (SystemZ::isImmLH(Imm)) {
87 MI.setDesc(TII->get(LLIxH));
88 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
89 MI.getOperand(1).setImm(Imm >> 16);
90 return true;
91 }
92 return false;
93}
94
Ulrich Weigand49506d72015-05-05 19:28:34 +000095// Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
96bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
97 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) {
98 MI.setDesc(TII->get(Opcode));
99 return true;
100 }
101 return false;
102}
103
104// Change MI's opcode to Opcode if register operands 0 and 1 have a
105// 4-bit encoding.
106bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) {
107 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
108 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
109 MI.setDesc(TII->get(Opcode));
110 return true;
111 }
112 return false;
113}
114
115// Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
116// 4-bit encoding and if operands 0 and 1 are tied.
117bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) {
118 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
119 MI.getOperand(1).getReg() == MI.getOperand(0).getReg() &&
120 SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) {
121 MI.setDesc(TII->get(Opcode));
122 return true;
123 }
124 return false;
125}
126
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000127// Calls shortenOn001 if CCLive is false. CC def operand is added in
128// case of success.
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000129bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI,
130 unsigned Opcode) {
131 if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) {
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000132 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
133 .addReg(SystemZ::CC, RegState::ImplicitDefine);
134 return true;
135 }
136 return false;
137}
138
Ulrich Weigand49506d72015-05-05 19:28:34 +0000139// MI is a vector-style conversion instruction with the operand order:
140// destination, source, exact-suppress, rounding-mode. If both registers
141// have a 4-bit encoding then change it to Opcode, which has operand order:
142// destination, rouding-mode, source, exact-suppress.
143bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) {
144 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
145 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
146 MachineOperand Dest(MI.getOperand(0));
147 MachineOperand Src(MI.getOperand(1));
148 MachineOperand Suppress(MI.getOperand(2));
149 MachineOperand Mode(MI.getOperand(3));
150 MI.RemoveOperand(3);
151 MI.RemoveOperand(2);
152 MI.RemoveOperand(1);
153 MI.RemoveOperand(0);
154 MI.setDesc(TII->get(Opcode));
155 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
156 .addOperand(Dest)
157 .addOperand(Mode)
158 .addOperand(Src)
159 .addOperand(Suppress);
160 return true;
161 }
162 return false;
163}
164
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000165// Process all instructions in MBB. Return true if something changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000166bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000167 bool Changed = false;
168
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000169 // Set up the set of live registers at the end of MBB (live out)
170 LiveRegs.clear();
171 LiveRegs.addLiveOuts(&MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000172
173 // Iterate backwards through the block looking for instructions to change.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000174 for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000175 MachineInstr &MI = *MBBI;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000176 switch (MI.getOpcode()) {
177 case SystemZ::IILF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000178 Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000179 break;
180
181 case SystemZ::IIHF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000182 Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000183 break;
184
185 case SystemZ::WFADB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000186 Changed |= shortenOn001AddCC(MI, SystemZ::ADBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000187 break;
188
189 case SystemZ::WFDDB:
190 Changed |= shortenOn001(MI, SystemZ::DDBR);
191 break;
192
193 case SystemZ::WFIDB:
194 Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
195 break;
196
197 case SystemZ::WLDEB:
198 Changed |= shortenOn01(MI, SystemZ::LDEBR);
199 break;
200
201 case SystemZ::WLEDB:
202 Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
203 break;
204
205 case SystemZ::WFMDB:
206 Changed |= shortenOn001(MI, SystemZ::MDBR);
207 break;
208
209 case SystemZ::WFLCDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000210 Changed |= shortenOn01(MI, SystemZ::LCDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000211 break;
212
213 case SystemZ::WFLNDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000214 Changed |= shortenOn01(MI, SystemZ::LNDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000215 break;
216
217 case SystemZ::WFLPDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000218 Changed |= shortenOn01(MI, SystemZ::LPDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000219 break;
220
221 case SystemZ::WFSQDB:
222 Changed |= shortenOn01(MI, SystemZ::SQDBR);
223 break;
224
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000225 case SystemZ::WFSDB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000226 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000227 break;
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000228
Ulrich Weigand49506d72015-05-05 19:28:34 +0000229 case SystemZ::WFCDB:
230 Changed |= shortenOn01(MI, SystemZ::CDBR);
231 break;
232
233 case SystemZ::VL32:
234 // For z13 we prefer LDE over LE to avoid partial register dependencies.
235 Changed |= shortenOn0(MI, SystemZ::LDE32);
236 break;
237
238 case SystemZ::VST32:
239 Changed |= shortenOn0(MI, SystemZ::STE);
240 break;
241
242 case SystemZ::VL64:
243 Changed |= shortenOn0(MI, SystemZ::LD);
244 break;
245
246 case SystemZ::VST64:
247 Changed |= shortenOn0(MI, SystemZ::STD);
248 break;
249 }
250
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000251 LiveRegs.stepBackward(MI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000252 }
253
254 return Changed;
255}
256
257bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000258 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
259 TII = ST.getInstrInfo();
260 TRI = ST.getRegisterInfo();
261 LiveRegs.init(TRI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000262
263 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000264 for (auto &MBB : F)
265 Changed |= processBlock(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000266
267 return Changed;
268}