1 //===-- SystemZShortenInst.cpp - Instruction-shortening pass --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass tries to replace instructions with shorter forms. For example,
10 // IILF can be replaced with LLILL or LLILH if the constant fits and if the
11 // other 32 bits of the GR64 destination are not live.
13 //===----------------------------------------------------------------------===//
15 #include "SystemZTargetMachine.h"
16 #include "llvm/CodeGen/LivePhysRegs.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
23 #define DEBUG_TYPE "systemz-shorten-inst"
26 class SystemZShortenInst
: public MachineFunctionPass
{
29 SystemZShortenInst(const SystemZTargetMachine
&tm
);
31 StringRef
getPassName() const override
{
32 return "SystemZ Instruction Shortening";
35 bool processBlock(MachineBasicBlock
&MBB
);
36 bool runOnMachineFunction(MachineFunction
&F
) override
;
37 MachineFunctionProperties
getRequiredProperties() const override
{
38 return MachineFunctionProperties().set(
39 MachineFunctionProperties::Property::NoVRegs
);
43 bool shortenIIF(MachineInstr
&MI
, unsigned LLIxL
, unsigned LLIxH
);
44 bool shortenOn0(MachineInstr
&MI
, unsigned Opcode
);
45 bool shortenOn01(MachineInstr
&MI
, unsigned Opcode
);
46 bool shortenOn001(MachineInstr
&MI
, unsigned Opcode
);
47 bool shortenOn001AddCC(MachineInstr
&MI
, unsigned Opcode
);
48 bool shortenFPConv(MachineInstr
&MI
, unsigned Opcode
);
50 const SystemZInstrInfo
*TII
;
51 const TargetRegisterInfo
*TRI
;
52 LivePhysRegs LiveRegs
;
55 char SystemZShortenInst::ID
= 0;
56 } // end anonymous namespace
58 FunctionPass
*llvm::createSystemZShortenInstPass(SystemZTargetMachine
&TM
) {
59 return new SystemZShortenInst(TM
);
62 SystemZShortenInst::SystemZShortenInst(const SystemZTargetMachine
&tm
)
63 : MachineFunctionPass(ID
), TII(nullptr) {}
65 // Tie operands if MI has become a two-address instruction.
66 static void tieOpsIfNeeded(MachineInstr
&MI
) {
67 if (MI
.getDesc().getOperandConstraint(0, MCOI::TIED_TO
) &&
68 !MI
.getOperand(0).isTied())
72 // MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH
73 // are the halfword immediate loads for the same word. Try to use one of them
75 bool SystemZShortenInst::shortenIIF(MachineInstr
&MI
, unsigned LLIxL
,
77 Register Reg
= MI
.getOperand(0).getReg();
78 // The new opcode will clear the other half of the GR64 reg, so
79 // cancel if that is live.
80 unsigned thisSubRegIdx
=
81 (SystemZ::GRH32BitRegClass
.contains(Reg
) ? SystemZ::subreg_h32
82 : SystemZ::subreg_l32
);
83 unsigned otherSubRegIdx
=
84 (thisSubRegIdx
== SystemZ::subreg_l32
? SystemZ::subreg_h32
85 : SystemZ::subreg_l32
);
87 TRI
->getMatchingSuperReg(Reg
, thisSubRegIdx
, &SystemZ::GR64BitRegClass
);
88 Register OtherReg
= TRI
->getSubReg(GR64BitReg
, otherSubRegIdx
);
89 if (LiveRegs
.contains(OtherReg
))
92 uint64_t Imm
= MI
.getOperand(1).getImm();
93 if (SystemZ::isImmLL(Imm
)) {
94 MI
.setDesc(TII
->get(LLIxL
));
95 MI
.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg
));
98 if (SystemZ::isImmLH(Imm
)) {
99 MI
.setDesc(TII
->get(LLIxH
));
100 MI
.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg
));
101 MI
.getOperand(1).setImm(Imm
>> 16);
107 // Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
108 bool SystemZShortenInst::shortenOn0(MachineInstr
&MI
, unsigned Opcode
) {
109 if (SystemZMC::getFirstReg(MI
.getOperand(0).getReg()) < 16) {
110 MI
.setDesc(TII
->get(Opcode
));
116 // Change MI's opcode to Opcode if register operands 0 and 1 have a
118 bool SystemZShortenInst::shortenOn01(MachineInstr
&MI
, unsigned Opcode
) {
119 if (SystemZMC::getFirstReg(MI
.getOperand(0).getReg()) < 16 &&
120 SystemZMC::getFirstReg(MI
.getOperand(1).getReg()) < 16) {
121 MI
.setDesc(TII
->get(Opcode
));
127 // Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
128 // 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0
129 // with op 1, if MI becomes 2-address.
130 bool SystemZShortenInst::shortenOn001(MachineInstr
&MI
, unsigned Opcode
) {
131 if (SystemZMC::getFirstReg(MI
.getOperand(0).getReg()) < 16 &&
132 MI
.getOperand(1).getReg() == MI
.getOperand(0).getReg() &&
133 SystemZMC::getFirstReg(MI
.getOperand(2).getReg()) < 16) {
134 MI
.setDesc(TII
->get(Opcode
));
141 // Calls shortenOn001 if CCLive is false. CC def operand is added in
143 bool SystemZShortenInst::shortenOn001AddCC(MachineInstr
&MI
, unsigned Opcode
) {
144 if (!LiveRegs
.contains(SystemZ::CC
) && shortenOn001(MI
, Opcode
)) {
145 MachineInstrBuilder(*MI
.getParent()->getParent(), &MI
)
146 .addReg(SystemZ::CC
, RegState::ImplicitDefine
| RegState::Dead
);
152 // 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.
156 bool 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));
167 MI
.setDesc(TII
->get(Opcode
));
168 MachineInstrBuilder(*MI
.getParent()->getParent(), &MI
)
178 // Process all instructions in MBB. Return true if something changed.
179 bool SystemZShortenInst::processBlock(MachineBasicBlock
&MBB
) {
180 bool Changed
= false;
182 // Set up the set of live registers at the end of MBB (live out)
184 LiveRegs
.addLiveOuts(MBB
);
186 // Iterate backwards through the block looking for instructions to change.
187 for (auto MBBI
= MBB
.rbegin(), MBBE
= MBB
.rend(); MBBI
!= MBBE
; ++MBBI
) {
188 MachineInstr
&MI
= *MBBI
;
189 switch (MI
.getOpcode()) {
191 Changed
|= shortenIIF(MI
, SystemZ::LLILL
, SystemZ::LLILH
);
195 Changed
|= shortenIIF(MI
, SystemZ::LLIHL
, SystemZ::LLIHH
);
199 Changed
|= shortenOn001AddCC(MI
, SystemZ::ADBR
);
203 Changed
|= shortenOn001AddCC(MI
, SystemZ::AEBR
);
207 Changed
|= shortenOn001(MI
, SystemZ::DDBR
);
211 Changed
|= shortenOn001(MI
, SystemZ::DEBR
);
215 Changed
|= shortenFPConv(MI
, SystemZ::FIDBRA
);
219 Changed
|= shortenFPConv(MI
, SystemZ::FIEBRA
);
223 Changed
|= shortenOn01(MI
, SystemZ::LDEBR
);
227 Changed
|= shortenFPConv(MI
, SystemZ::LEDBRA
);
231 Changed
|= shortenOn001(MI
, SystemZ::MDBR
);
235 Changed
|= shortenOn001(MI
, SystemZ::MEEBR
);
238 case SystemZ::WFLCDB
:
239 Changed
|= shortenOn01(MI
, SystemZ::LCDFR
);
242 case SystemZ::WFLCSB
:
243 Changed
|= shortenOn01(MI
, SystemZ::LCDFR_32
);
246 case SystemZ::WFLNDB
:
247 Changed
|= shortenOn01(MI
, SystemZ::LNDFR
);
250 case SystemZ::WFLNSB
:
251 Changed
|= shortenOn01(MI
, SystemZ::LNDFR_32
);
254 case SystemZ::WFLPDB
:
255 Changed
|= shortenOn01(MI
, SystemZ::LPDFR
);
258 case SystemZ::WFLPSB
:
259 Changed
|= shortenOn01(MI
, SystemZ::LPDFR_32
);
262 case SystemZ::WFSQDB
:
263 Changed
|= shortenOn01(MI
, SystemZ::SQDBR
);
266 case SystemZ::WFSQSB
:
267 Changed
|= shortenOn01(MI
, SystemZ::SQEBR
);
271 Changed
|= shortenOn001AddCC(MI
, SystemZ::SDBR
);
275 Changed
|= shortenOn001AddCC(MI
, SystemZ::SEBR
);
279 Changed
|= shortenOn01(MI
, SystemZ::CDBR
);
283 Changed
|= shortenOn01(MI
, SystemZ::CEBR
);
287 Changed
|= shortenOn01(MI
, SystemZ::KDBR
);
291 Changed
|= shortenOn01(MI
, SystemZ::KEBR
);
295 // For z13 we prefer LDE over LE to avoid partial register dependencies.
296 Changed
|= shortenOn0(MI
, SystemZ::LDE32
);
300 Changed
|= shortenOn0(MI
, SystemZ::STE
);
304 Changed
|= shortenOn0(MI
, SystemZ::LD
);
308 Changed
|= shortenOn0(MI
, SystemZ::STD
);
312 int TwoOperandOpcode
= SystemZ::getTwoOperandOpcode(MI
.getOpcode());
313 if (TwoOperandOpcode
== -1)
316 if ((MI
.getOperand(0).getReg() != MI
.getOperand(1).getReg()) &&
317 (!MI
.isCommutable() ||
318 MI
.getOperand(0).getReg() != MI
.getOperand(2).getReg() ||
319 !TII
->commuteInstruction(MI
, false, 1, 2)))
322 MI
.setDesc(TII
->get(TwoOperandOpcode
));
323 MI
.tieOperands(0, 1);
324 if (TwoOperandOpcode
== SystemZ::SLL
||
325 TwoOperandOpcode
== SystemZ::SLA
||
326 TwoOperandOpcode
== SystemZ::SRL
||
327 TwoOperandOpcode
== SystemZ::SRA
) {
328 // These shifts only use the low 6 bits of the shift count.
329 MachineOperand
&ImmMO
= MI
.getOperand(3);
330 ImmMO
.setImm(ImmMO
.getImm() & 0xfff);
337 LiveRegs
.stepBackward(MI
);
343 bool SystemZShortenInst::runOnMachineFunction(MachineFunction
&F
) {
344 if (skipFunction(F
.getFunction()))
347 const SystemZSubtarget
&ST
= F
.getSubtarget
<SystemZSubtarget
>();
348 TII
= ST
.getInstrInfo();
349 TRI
= ST
.getRegisterInfo();
352 bool Changed
= false;
354 Changed
|= processBlock(MBB
);