blob: d36f051a1cc0e0ab9a44b4328f2ae9835b60e3ea [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(
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)
170 .addOperand(Dest)
171 .addOperand(Mode)
172 .addOperand(Src)
173 .addOperand(Suppress);
174 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
203 case SystemZ::WFDDB:
204 Changed |= shortenOn001(MI, SystemZ::DDBR);
205 break;
206
207 case SystemZ::WFIDB:
208 Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
209 break;
210
211 case SystemZ::WLDEB:
212 Changed |= shortenOn01(MI, SystemZ::LDEBR);
213 break;
214
215 case SystemZ::WLEDB:
216 Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
217 break;
218
219 case SystemZ::WFMDB:
220 Changed |= shortenOn001(MI, SystemZ::MDBR);
221 break;
222
223 case SystemZ::WFLCDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000224 Changed |= shortenOn01(MI, SystemZ::LCDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000225 break;
226
227 case SystemZ::WFLNDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000228 Changed |= shortenOn01(MI, SystemZ::LNDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000229 break;
230
231 case SystemZ::WFLPDB:
Jonas Paulsson12629322015-10-01 18:12:28 +0000232 Changed |= shortenOn01(MI, SystemZ::LPDFR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000233 break;
234
235 case SystemZ::WFSQDB:
236 Changed |= shortenOn01(MI, SystemZ::SQDBR);
237 break;
238
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000239 case SystemZ::WFSDB:
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000240 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
Ulrich Weigand49506d72015-05-05 19:28:34 +0000241 break;
Jonas Paulsson5b3bab42015-10-09 07:19:20 +0000242
Ulrich Weigand49506d72015-05-05 19:28:34 +0000243 case SystemZ::WFCDB:
244 Changed |= shortenOn01(MI, SystemZ::CDBR);
245 break;
246
247 case SystemZ::VL32:
248 // For z13 we prefer LDE over LE to avoid partial register dependencies.
249 Changed |= shortenOn0(MI, SystemZ::LDE32);
250 break;
251
252 case SystemZ::VST32:
253 Changed |= shortenOn0(MI, SystemZ::STE);
254 break;
255
256 case SystemZ::VL64:
257 Changed |= shortenOn0(MI, SystemZ::LD);
258 break;
259
260 case SystemZ::VST64:
261 Changed |= shortenOn0(MI, SystemZ::STD);
262 break;
263 }
264
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000265 LiveRegs.stepBackward(MI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000266 }
267
268 return Changed;
269}
270
271bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000272 if (skipFunction(*F.getFunction()))
273 return false;
274
Jonas Paulsson4b29f6f2015-10-20 15:05:58 +0000275 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
276 TII = ST.getInstrInfo();
277 TRI = ST.getRegisterInfo();
278 LiveRegs.init(TRI);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000279
280 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000281 for (auto &MBB : F)
282 Changed |= processBlock(MBB);
Richard Sandiford35ec4e3562013-09-25 10:11:07 +0000283
284 return Changed;
285}