blob: 846edd51341a748e9a6824ef02c2a51c285b1aba [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
Jonas Paulssondab74072015-10-26 15:03:07 +000062// Tie operands if MI has become a two-address instruction.
63static void tieOpsIfNeeded(MachineInstr &MI) {
64 if (MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
65 !MI.getOperand(0).isTied())
66 MI.tieOperands(0, 1);
67}
68
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000069// MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH
70// are the halfword immediate loads for the same word. Try to use one of them
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000071// instead of IIxF.
72bool SystemZShortenInst::shortenIIF(MachineInstr &MI,
73 unsigned LLIxL, unsigned LLIxH) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000074 unsigned Reg = MI.getOperand(0).getReg();
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000075 // The new opcode will clear the other half of the GR64 reg, so
76 // cancel if that is live.
77 unsigned thisSubRegIdx = (SystemZ::GRH32BitRegClass.contains(Reg) ?
78 SystemZ::subreg_h32 : SystemZ::subreg_l32);
79 unsigned otherSubRegIdx = (thisSubRegIdx == SystemZ::subreg_l32 ?
80 SystemZ::subreg_h32 : SystemZ::subreg_l32);
81 unsigned GR64BitReg = TRI->getMatchingSuperReg(Reg, thisSubRegIdx,
82 &SystemZ::GR64BitRegClass);
83 unsigned OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
84 if (LiveRegs.contains(OtherReg))
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000085 return false;
86
87 uint64_t Imm = MI.getOperand(1).getImm();
88 if (SystemZ::isImmLL(Imm)) {
89 MI.setDesc(TII->get(LLIxL));
90 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
91 return true;
92 }
93 if (SystemZ::isImmLH(Imm)) {
94 MI.setDesc(TII->get(LLIxH));
95 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
96 MI.getOperand(1).setImm(Imm >> 16);
97 return true;
98 }
99 return false;
100}
101
Ulrich Weigand49506d72015-05-05 19:28:34 +0000102// Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
103bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
104 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) {
105 MI.setDesc(TII->get(Opcode));
106 return true;
107 }
108 return false;
109}
110
111// Change MI's opcode to Opcode if register operands 0 and 1 have a
112// 4-bit encoding.
113bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) {
114 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
115 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
116 MI.setDesc(TII->get(Opcode));
117 return true;
118 }
119 return false;
120}
121
122// Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
Jonas Paulssondab74072015-10-26 15:03:07 +0000123// 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0
124// with op 1, if MI becomes 2-address.
Ulrich Weigand49506d72015-05-05 19:28:34 +0000125bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) {
126 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
127 MI.getOperand(1).getReg() == MI.getOperand(0).getReg() &&
128 SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) {
129 MI.setDesc(TII->get(Opcode));
Jonas Paulssondab74072015-10-26 15:03:07 +0000130 tieOpsIfNeeded(MI);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000131 return true;
132 }
133 return false;
134}
135
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000136// Calls shortenOn001 if CCLive is false. CC def operand is added in
137// case of success.
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000138bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI,
139 unsigned Opcode) {
140 if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) {
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000141 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
142 .addReg(SystemZ::CC, RegState::ImplicitDefine);
143 return true;
144 }
145 return false;
146}
147
Ulrich Weigand49506d72015-05-05 19:28:34 +0000148// MI is a vector-style conversion instruction with the operand order:
149// destination, source, exact-suppress, rounding-mode. If both registers
150// have a 4-bit encoding then change it to Opcode, which has operand order:
151// destination, rouding-mode, source, exact-suppress.
152bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) {
153 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
154 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
155 MachineOperand Dest(MI.getOperand(0));
156 MachineOperand Src(MI.getOperand(1));
157 MachineOperand Suppress(MI.getOperand(2));
158 MachineOperand Mode(MI.getOperand(3));
159 MI.RemoveOperand(3);
160 MI.RemoveOperand(2);
161 MI.RemoveOperand(1);
162 MI.RemoveOperand(0);
163 MI.setDesc(TII->get(Opcode));
164 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
165 .addOperand(Dest)
166 .addOperand(Mode)
167 .addOperand(Src)
168 .addOperand(Suppress);
169 return true;
170 }
171 return false;
172}
173
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000174// Process all instructions in MBB. Return true if something changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000175bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000176 bool Changed = false;
177
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000178 // Set up the set of live registers at the end of MBB (live out)
179 LiveRegs.clear();
180 LiveRegs.addLiveOuts(&MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000181
182 // Iterate backwards through the block looking for instructions to change.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000183 for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000184 MachineInstr &MI = *MBBI;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000185 switch (MI.getOpcode()) {
186 case SystemZ::IILF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000187 Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000188 break;
189
190 case SystemZ::IIHF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000191 Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000192 break;
193
194 case SystemZ::WFADB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000195 Changed |= shortenOn001AddCC(MI, SystemZ::ADBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000196 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:
Jonas Paulsson12629322015-10-01 18:12:28 +0000219 Changed |= shortenOn01(MI, SystemZ::LCDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000220 break;
221
222 case SystemZ::WFLNDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000223 Changed |= shortenOn01(MI, SystemZ::LNDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000224 break;
225
226 case SystemZ::WFLPDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000227 Changed |= shortenOn01(MI, SystemZ::LPDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000228 break;
229
230 case SystemZ::WFSQDB:
231 Changed |= shortenOn01(MI, SystemZ::SQDBR);
232 break;
233
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000234 case SystemZ::WFSDB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000235 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000236 break;
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000237
Ulrich Weigand49506d72015-05-05 19:28:34 +0000238 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
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000260 LiveRegs.stepBackward(MI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000261 }
262
263 return Changed;
264}
265
266bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000267 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
268 TII = ST.getInstrInfo();
269 TRI = ST.getRegisterInfo();
270 LiveRegs.init(TRI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000271
272 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000273 for (auto &MBB : F)
274 Changed |= processBlock(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000275
276 return Changed;
277}