1 //===-- SystemZShortenInst.cpp - Instruction-shortening pass --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "SystemZTargetMachine.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/LivePhysRegs.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
24 #define DEBUG_TYPE "systemz-shorten-inst"
27 class SystemZShortenInst : public MachineFunctionPass {
30 SystemZShortenInst(const SystemZTargetMachine &tm);
32 StringRef getPassName() const override {
33 return "SystemZ Instruction Shortening";
36 bool processBlock(MachineBasicBlock &MBB);
37 bool runOnMachineFunction(MachineFunction &F) override;
38 MachineFunctionProperties getRequiredProperties() const override {
39 return MachineFunctionProperties().set(
40 MachineFunctionProperties::Property::NoVRegs);
44 bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH);
45 bool shortenOn0(MachineInstr &MI, unsigned Opcode);
46 bool shortenOn01(MachineInstr &MI, unsigned Opcode);
47 bool shortenOn001(MachineInstr &MI, unsigned Opcode);
48 bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode);
49 bool shortenFPConv(MachineInstr &MI, unsigned Opcode);
51 const SystemZInstrInfo *TII;
52 const TargetRegisterInfo *TRI;
53 LivePhysRegs LiveRegs;
56 char SystemZShortenInst::ID = 0;
57 } // end anonymous namespace
59 FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) {
60 return new SystemZShortenInst(TM);
63 SystemZShortenInst::SystemZShortenInst(const SystemZTargetMachine &tm)
64 : MachineFunctionPass(ID), TII(nullptr) {}
66 // Tie operands if MI has become a two-address instruction.
67 static void tieOpsIfNeeded(MachineInstr &MI) {
68 if (MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
69 !MI.getOperand(0).isTied())
73 // 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
76 bool SystemZShortenInst::shortenIIF(MachineInstr &MI, unsigned LLIxL,
78 unsigned Reg = MI.getOperand(0).getReg();
79 // The new opcode will clear the other half of the GR64 reg, so
80 // cancel if that is live.
81 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);
88 TRI->getMatchingSuperReg(Reg, thisSubRegIdx, &SystemZ::GR64BitRegClass);
89 unsigned OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
90 if (LiveRegs.contains(OtherReg))
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));
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);
108 // Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
109 bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
110 if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) {
111 MI.setDesc(TII->get(Opcode));
117 // Change MI's opcode to Opcode if register operands 0 and 1 have a
119 bool 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));
128 // Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
129 // 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0
130 // with op 1, if MI becomes 2-address.
131 bool 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));
142 // Calls shortenOn001 if CCLive is false. CC def operand is added in
144 bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI, unsigned Opcode) {
145 if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) {
146 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
147 .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
153 // 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.
157 bool 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));
168 MI.setDesc(TII->get(Opcode));
169 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
179 // Process all instructions in MBB. Return true if something changed.
180 bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
181 bool Changed = false;
183 // Set up the set of live registers at the end of MBB (live out)
185 LiveRegs.addLiveOuts(MBB);
187 // Iterate backwards through the block looking for instructions to change.
188 for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) {
189 MachineInstr &MI = *MBBI;
190 switch (MI.getOpcode()) {
192 Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH);
196 Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH);
200 Changed |= shortenOn001AddCC(MI, SystemZ::ADBR);
204 Changed |= shortenOn001(MI, SystemZ::DDBR);
208 Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
212 Changed |= shortenOn01(MI, SystemZ::LDEBR);
216 Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
220 Changed |= shortenOn001(MI, SystemZ::MDBR);
223 case SystemZ::WFLCDB:
224 Changed |= shortenOn01(MI, SystemZ::LCDFR);
227 case SystemZ::WFLNDB:
228 Changed |= shortenOn01(MI, SystemZ::LNDFR);
231 case SystemZ::WFLPDB:
232 Changed |= shortenOn01(MI, SystemZ::LPDFR);
235 case SystemZ::WFSQDB:
236 Changed |= shortenOn01(MI, SystemZ::SQDBR);
240 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
244 Changed |= shortenOn01(MI, SystemZ::CDBR);
248 // For z13 we prefer LDE over LE to avoid partial register dependencies.
249 Changed |= shortenOn0(MI, SystemZ::LDE32);
253 Changed |= shortenOn0(MI, SystemZ::STE);
257 Changed |= shortenOn0(MI, SystemZ::LD);
261 Changed |= shortenOn0(MI, SystemZ::STD);
265 LiveRegs.stepBackward(MI);
271 bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
272 if (skipFunction(*F.getFunction()))
275 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
276 TII = ST.getInstrInfo();
277 TRI = ST.getRegisterInfo();
280 bool Changed = false;
282 Changed |= processBlock(MBB);