blob: 41ef3b340e2982bce25eed44706dc5d51f8155d5 [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;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000038 MachineFunctionProperties getRequiredProperties() const override {
39 return MachineFunctionProperties().set(
40 MachineFunctionProperties::Property::AllVRegsAllocated);
41 }
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
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +000075// instead of IIxF.
76bool SystemZShortenInst::shortenIIF(MachineInstr &MI,
77 unsigned LLIxL, 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.
81 unsigned thisSubRegIdx = (SystemZ::GRH32BitRegClass.contains(Reg) ?
82 SystemZ::subreg_h32 : SystemZ::subreg_l32);
83 unsigned otherSubRegIdx = (thisSubRegIdx == SystemZ::subreg_l32 ?
84 SystemZ::subreg_h32 : SystemZ::subreg_l32);
85 unsigned GR64BitReg = TRI->getMatchingSuperReg(Reg, thisSubRegIdx,
86 &SystemZ::GR64BitRegClass);
87 unsigned OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
88 if (LiveRegs.contains(OtherReg))
Richard Sandiford35ec4e3562013-09-25 10:11:07 +000089 return false;
90
91 uint64_t Imm = MI.getOperand(1).getImm();
92 if (SystemZ::isImmLL(Imm)) {
93 MI.setDesc(TII->get(LLIxL));
94 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
95 return true;
96 }
97 if (SystemZ::isImmLH(Imm)) {
98 MI.setDesc(TII->get(LLIxH));
99 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
100 MI.getOperand(1).setImm(Imm >> 16);
101 return true;
102 }
103 return false;
104}
105
Ulrich Weigand49506d72015-05-05 19:28:34 +0000106// Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
107bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
108 if (SystemZMC::getFirstReg(MI.getOperand(0).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 and 1 have a
116// 4-bit encoding.
117bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) {
118 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
119 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
120 MI.setDesc(TII->get(Opcode));
121 return true;
122 }
123 return false;
124}
125
126// Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
Jonas Paulssondab74072015-10-26 15:03:07 +0000127// 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0
128// with op 1, if MI becomes 2-address.
Ulrich Weigand49506d72015-05-05 19:28:34 +0000129bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) {
130 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
131 MI.getOperand(1).getReg() == MI.getOperand(0).getReg() &&
132 SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) {
133 MI.setDesc(TII->get(Opcode));
Jonas Paulssondab74072015-10-26 15:03:07 +0000134 tieOpsIfNeeded(MI);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000135 return true;
136 }
137 return false;
138}
139
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000140// Calls shortenOn001 if CCLive is false. CC def operand is added in
141// case of success.
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000142bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI,
143 unsigned Opcode) {
144 if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) {
Jonas Paulsson29d9d8d2015-10-08 07:40:19 +0000145 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
146 .addReg(SystemZ::CC, RegState::ImplicitDefine);
147 return true;
148 }
149 return false;
150}
151
Ulrich Weigand49506d72015-05-05 19:28:34 +0000152// MI is a vector-style conversion instruction with the operand order:
153// destination, source, exact-suppress, rounding-mode. If both registers
154// have a 4-bit encoding then change it to Opcode, which has operand order:
155// destination, rouding-mode, source, exact-suppress.
156bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) {
157 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
158 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
159 MachineOperand Dest(MI.getOperand(0));
160 MachineOperand Src(MI.getOperand(1));
161 MachineOperand Suppress(MI.getOperand(2));
162 MachineOperand Mode(MI.getOperand(3));
163 MI.RemoveOperand(3);
164 MI.RemoveOperand(2);
165 MI.RemoveOperand(1);
166 MI.RemoveOperand(0);
167 MI.setDesc(TII->get(Opcode));
168 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
169 .addOperand(Dest)
170 .addOperand(Mode)
171 .addOperand(Src)
172 .addOperand(Suppress);
173 return true;
174 }
175 return false;
176}
177
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000178// Process all instructions in MBB. Return true if something changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000179bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000180 bool Changed = false;
181
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000182 // Set up the set of live registers at the end of MBB (live out)
183 LiveRegs.clear();
184 LiveRegs.addLiveOuts(&MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000185
186 // Iterate backwards through the block looking for instructions to change.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000187 for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) {
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000188 MachineInstr &MI = *MBBI;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000189 switch (MI.getOpcode()) {
190 case SystemZ::IILF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000191 Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000192 break;
193
194 case SystemZ::IIHF:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000195 Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000196 break;
197
198 case SystemZ::WFADB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000199 Changed |= shortenOn001AddCC(MI, SystemZ::ADBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000200 break;
201
202 case SystemZ::WFDDB:
203 Changed |= shortenOn001(MI, SystemZ::DDBR);
204 break;
205
206 case SystemZ::WFIDB:
207 Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
208 break;
209
210 case SystemZ::WLDEB:
211 Changed |= shortenOn01(MI, SystemZ::LDEBR);
212 break;
213
214 case SystemZ::WLEDB:
215 Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
216 break;
217
218 case SystemZ::WFMDB:
219 Changed |= shortenOn001(MI, SystemZ::MDBR);
220 break;
221
222 case SystemZ::WFLCDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000223 Changed |= shortenOn01(MI, SystemZ::LCDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000224 break;
225
226 case SystemZ::WFLNDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000227 Changed |= shortenOn01(MI, SystemZ::LNDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000228 break;
229
230 case SystemZ::WFLPDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000231 Changed |= shortenOn01(MI, SystemZ::LPDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000232 break;
233
234 case SystemZ::WFSQDB:
235 Changed |= shortenOn01(MI, SystemZ::SQDBR);
236 break;
237
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000238 case SystemZ::WFSDB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000239 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000240 break;
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000241
Ulrich Weigand49506d72015-05-05 19:28:34 +0000242 case SystemZ::WFCDB:
243 Changed |= shortenOn01(MI, SystemZ::CDBR);
244 break;
245
246 case SystemZ::VL32:
247 // For z13 we prefer LDE over LE to avoid partial register dependencies.
248 Changed |= shortenOn0(MI, SystemZ::LDE32);
249 break;
250
251 case SystemZ::VST32:
252 Changed |= shortenOn0(MI, SystemZ::STE);
253 break;
254
255 case SystemZ::VL64:
256 Changed |= shortenOn0(MI, SystemZ::LD);
257 break;
258
259 case SystemZ::VST64:
260 Changed |= shortenOn0(MI, SystemZ::STD);
261 break;
262 }
263
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000264 LiveRegs.stepBackward(MI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000265 }
266
267 return Changed;
268}
269
270bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000271 if (skipFunction(*F.getFunction()))
272 return false;
273
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000274 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
275 TII = ST.getInstrInfo();
276 TRI = ST.getRegisterInfo();
277 LiveRegs.init(TRI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000278
279 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000280 for (auto &MBB : F)
281 Changed |= processBlock(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000282
283 return Changed;
284}