blob: 195fa20a2c9038076a8f50c6f5c6420f2e728797 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/CodeGen/LivePhysRegs.h"
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Ulrich Weigand49506d72015-05-05 19:28:34 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000020#include "llvm/CodeGen/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
Mehdi Amini117296c2016-10-01 02:56:57 +000032 StringRef 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;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000038 MachineFunctionProperties getRequiredProperties() const override {
39 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000040 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000041 }
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000042
Richard Sandifordc2312692014-03-06 10:38:30 +000043private:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000044 bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH);
Ulrich Weigand49506d72015-05-05 19:28:34 +000045 bool shortenOn0(MachineInstr &MI, unsigned Opcode);
46 bool shortenOn01(MachineInstr &MI, unsigned Opcode);
47 bool shortenOn001(MachineInstr &MI, unsigned Opcode);
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000048 bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode);
Ulrich Weigand49506d72015-05-05 19:28:34 +000049 bool shortenFPConv(MachineInstr &MI, unsigned Opcode);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000050
Richard Sandifordc2312692014-03-06 10:38:30 +000051 const SystemZInstrInfo *TII;
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000052 const TargetRegisterInfo *TRI;
53 LivePhysRegs LiveRegs;
Richard Sandifordc2312692014-03-06 10:38:30 +000054};
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000055
Richard Sandifordc2312692014-03-06 10:38:30 +000056char SystemZShortenInst::ID = 0;
57} // end anonymous namespace
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000058
59FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) {
60 return new SystemZShortenInst(TM);
61}
62
63SystemZShortenInst::SystemZShortenInst(const SystemZTargetMachine &tm)
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000064 : MachineFunctionPass(ID), TII(nullptr) {}
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000065
Jonas Paulssondab74072015-10-26 15:03:07 +000066// Tie operands if MI has become a two-address instruction.
67static void tieOpsIfNeeded(MachineInstr &MI) {
68 if (MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
69 !MI.getOperand(0).isTied())
70 MI.tieOperands(0, 1);
71}
72
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000073// MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH
74// are the halfword immediate loads for the same word. Try to use one of them
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +000075// instead of IIxF.
76bool SystemZShortenInst::shortenIIF(MachineInstr &MI, unsigned LLIxL,
77 unsigned LLIxH) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000078 unsigned Reg = MI.getOperand(0).getReg();
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000079 // The new opcode will clear the other half of the GR64 reg, so
80 // cancel if that is live.
NAKAMURA Takumife1202c2016-06-20 00:37:41 +000081 unsigned thisSubRegIdx =
82 (SystemZ::GRH32BitRegClass.contains(Reg) ? SystemZ::subreg_h32
83 : SystemZ::subreg_l32);
84 unsigned otherSubRegIdx =
85 (thisSubRegIdx == SystemZ::subreg_l32 ? SystemZ::subreg_h32
86 : SystemZ::subreg_l32);
87 unsigned GR64BitReg =
88 TRI->getMatchingSuperReg(Reg, thisSubRegIdx, &SystemZ::GR64BitRegClass);
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000089 unsigned OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
90 if (LiveRegs.contains(OtherReg))
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000091 return false;
92
93 uint64_t Imm = MI.getOperand(1).getImm();
94 if (SystemZ::isImmLL(Imm)) {
95 MI.setDesc(TII->get(LLIxL));
96 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
97 return true;
98 }
99 if (SystemZ::isImmLH(Imm)) {
100 MI.setDesc(TII->get(LLIxH));
101 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
102 MI.getOperand(1).setImm(Imm >> 16);
103 return true;
104 }
105 return false;
106}
107
Ulrich Weigand49506d72015-05-05 19:28:34 +0000108// Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
109bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
110 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) {
111 MI.setDesc(TII->get(Opcode));
112 return true;
113 }
114 return false;
115}
116
117// Change MI's opcode to Opcode if register operands 0 and 1 have a
118// 4-bit encoding.
119bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) {
120 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
121 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
122 MI.setDesc(TII->get(Opcode));
123 return true;
124 }
125 return false;
126}
127
128// Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
Jonas Paulssondab74072015-10-26 15:03:07 +0000129// 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0
130// with op 1, if MI becomes 2-address.
Ulrich Weigand49506d72015-05-05 19:28:34 +0000131bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) {
132 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
133 MI.getOperand(1).getReg() == MI.getOperand(0).getReg() &&
134 SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) {
135 MI.setDesc(TII->get(Opcode));
Jonas Paulssondab74072015-10-26 15:03:07 +0000136 tieOpsIfNeeded(MI);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000137 return true;
138 }
139 return false;
140}
141
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000142// Calls shortenOn001 if CCLive is false. CC def operand is added in
143// case of success.
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000144bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI, unsigned Opcode) {
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000145 if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) {
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000146 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000147 .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000148 return true;
149 }
150 return false;
151}
152
Ulrich Weigand49506d72015-05-05 19:28:34 +0000153// MI is a vector-style conversion instruction with the operand order:
154// destination, source, exact-suppress, rounding-mode. If both registers
155// have a 4-bit encoding then change it to Opcode, which has operand order:
156// destination, rouding-mode, source, exact-suppress.
157bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) {
158 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
159 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
160 MachineOperand Dest(MI.getOperand(0));
161 MachineOperand Src(MI.getOperand(1));
162 MachineOperand Suppress(MI.getOperand(2));
163 MachineOperand Mode(MI.getOperand(3));
164 MI.RemoveOperand(3);
165 MI.RemoveOperand(2);
166 MI.RemoveOperand(1);
167 MI.RemoveOperand(0);
168 MI.setDesc(TII->get(Opcode));
169 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
Diana Picus116bbab2017-01-13 09:58:52 +0000170 .add(Dest)
171 .add(Mode)
172 .add(Src)
173 .add(Suppress);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000174 return true;
175 }
176 return false;
177}
178
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000179// Process all instructions in MBB. Return true if something changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000180bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000181 bool Changed = false;
182
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000183 // Set up the set of live registers at the end of MBB (live out)
184 LiveRegs.clear();
Matthias Braund1aabb22016-05-03 00:24:32 +0000185 LiveRegs.addLiveOuts(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000186
187 // Iterate backwards through the block looking for instructions to change.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000188 for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000189 MachineInstr &MI = *MBBI;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000190 switch (MI.getOpcode()) {
191 case SystemZ::IILF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000192 Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000193 break;
194
195 case SystemZ::IIHF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000196 Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000197 break;
198
199 case SystemZ::WFADB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000200 Changed |= shortenOn001AddCC(MI, SystemZ::ADBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000201 break;
202
Ulrich Weigand33435c42017-07-17 17:42:48 +0000203 case SystemZ::WFASB:
204 Changed |= shortenOn001AddCC(MI, SystemZ::AEBR);
205 break;
206
Ulrich Weigand49506d72015-05-05 19:28:34 +0000207 case SystemZ::WFDDB:
208 Changed |= shortenOn001(MI, SystemZ::DDBR);
209 break;
210
Ulrich Weigand33435c42017-07-17 17:42:48 +0000211 case SystemZ::WFDSB:
212 Changed |= shortenOn001(MI, SystemZ::DEBR);
213 break;
214
Ulrich Weigand49506d72015-05-05 19:28:34 +0000215 case SystemZ::WFIDB:
216 Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
217 break;
218
Ulrich Weigand33435c42017-07-17 17:42:48 +0000219 case SystemZ::WFISB:
220 Changed |= shortenFPConv(MI, SystemZ::FIEBRA);
221 break;
222
Ulrich Weigand49506d72015-05-05 19:28:34 +0000223 case SystemZ::WLDEB:
224 Changed |= shortenOn01(MI, SystemZ::LDEBR);
225 break;
226
227 case SystemZ::WLEDB:
228 Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
229 break;
230
231 case SystemZ::WFMDB:
232 Changed |= shortenOn001(MI, SystemZ::MDBR);
233 break;
234
Ulrich Weigand33435c42017-07-17 17:42:48 +0000235 case SystemZ::WFMSB:
236 Changed |= shortenOn001(MI, SystemZ::MEEBR);
237 break;
238
Ulrich Weigand49506d72015-05-05 19:28:34 +0000239 case SystemZ::WFLCDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000240 Changed |= shortenOn01(MI, SystemZ::LCDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000241 break;
242
Ulrich Weigand33435c42017-07-17 17:42:48 +0000243 case SystemZ::WFLCSB:
244 Changed |= shortenOn01(MI, SystemZ::LCDFR_32);
245 break;
246
Ulrich Weigand49506d72015-05-05 19:28:34 +0000247 case SystemZ::WFLNDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000248 Changed |= shortenOn01(MI, SystemZ::LNDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000249 break;
250
Ulrich Weigand33435c42017-07-17 17:42:48 +0000251 case SystemZ::WFLNSB:
252 Changed |= shortenOn01(MI, SystemZ::LNDFR_32);
253 break;
254
Ulrich Weigand49506d72015-05-05 19:28:34 +0000255 case SystemZ::WFLPDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000256 Changed |= shortenOn01(MI, SystemZ::LPDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000257 break;
258
Ulrich Weigand33435c42017-07-17 17:42:48 +0000259 case SystemZ::WFLPSB:
260 Changed |= shortenOn01(MI, SystemZ::LPDFR_32);
261 break;
262
Ulrich Weigand49506d72015-05-05 19:28:34 +0000263 case SystemZ::WFSQDB:
264 Changed |= shortenOn01(MI, SystemZ::SQDBR);
265 break;
266
Ulrich Weigand33435c42017-07-17 17:42:48 +0000267 case SystemZ::WFSQSB:
268 Changed |= shortenOn01(MI, SystemZ::SQEBR);
269 break;
270
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000271 case SystemZ::WFSDB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000272 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000273 break;
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000274
Ulrich Weigand33435c42017-07-17 17:42:48 +0000275 case SystemZ::WFSSB:
276 Changed |= shortenOn001AddCC(MI, SystemZ::SEBR);
277 break;
278
Ulrich Weigand49506d72015-05-05 19:28:34 +0000279 case SystemZ::WFCDB:
280 Changed |= shortenOn01(MI, SystemZ::CDBR);
281 break;
282
Ulrich Weigand33435c42017-07-17 17:42:48 +0000283 case SystemZ::WFCSB:
284 Changed |= shortenOn01(MI, SystemZ::CEBR);
285 break;
286
Ulrich Weigand49506d72015-05-05 19:28:34 +0000287 case SystemZ::VL32:
288 // For z13 we prefer LDE over LE to avoid partial register dependencies.
289 Changed |= shortenOn0(MI, SystemZ::LDE32);
290 break;
291
292 case SystemZ::VST32:
293 Changed |= shortenOn0(MI, SystemZ::STE);
294 break;
295
296 case SystemZ::VL64:
297 Changed |= shortenOn0(MI, SystemZ::LD);
298 break;
299
300 case SystemZ::VST64:
301 Changed |= shortenOn0(MI, SystemZ::STD);
302 break;
303 }
304
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000305 LiveRegs.stepBackward(MI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000306 }
307
308 return Changed;
309}
310
311bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000312 if (skipFunction(F.getFunction()))
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000313 return false;
314
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000315 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
316 TII = ST.getInstrInfo();
317 TRI = ST.getRegisterInfo();
Matthias Braun0c989a82016-12-08 00:15:51 +0000318 LiveRegs.init(*TRI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000319
320 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000321 for (auto &MBB : F)
322 Changed |= processBlock(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000323
324 return Changed;
325}