135ec4e35SRichard Sandiford //===-- SystemZShortenInst.cpp - Instruction-shortening pass --------------===//
235ec4e35SRichard Sandiford //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
635ec4e35SRichard Sandiford //
735ec4e35SRichard Sandiford //===----------------------------------------------------------------------===//
835ec4e35SRichard Sandiford //
935ec4e35SRichard Sandiford // This pass tries to replace instructions with shorter forms.  For example,
1035ec4e35SRichard Sandiford // IILF can be replaced with LLILL or LLILH if the constant fits and if the
1135ec4e35SRichard Sandiford // other 32 bits of the GR64 destination are not live.
1235ec4e35SRichard Sandiford //
1335ec4e35SRichard Sandiford //===----------------------------------------------------------------------===//
1435ec4e35SRichard Sandiford 
1535ec4e35SRichard Sandiford #include "SystemZTargetMachine.h"
166bda14b3SChandler Carruth #include "llvm/CodeGen/LivePhysRegs.h"
1735ec4e35SRichard Sandiford #include "llvm/CodeGen/MachineFunctionPass.h"
1849506d78SUlrich Weigand #include "llvm/CodeGen/MachineInstrBuilder.h"
19b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetRegisterInfo.h"
2035ec4e35SRichard Sandiford 
2135ec4e35SRichard Sandiford using namespace llvm;
2235ec4e35SRichard Sandiford 
2384e68b29SChandler Carruth #define DEBUG_TYPE "systemz-shorten-inst"
2484e68b29SChandler Carruth 
2535ec4e35SRichard Sandiford namespace {
2635ec4e35SRichard Sandiford class SystemZShortenInst : public MachineFunctionPass {
2735ec4e35SRichard Sandiford public:
2835ec4e35SRichard Sandiford   static char ID;
29d5ae039eSKai Nacke   SystemZShortenInst();
3035ec4e35SRichard Sandiford 
3128c111ecSRichard Sandiford   bool processBlock(MachineBasicBlock &MBB);
329d74a5a5SCraig Topper   bool runOnMachineFunction(MachineFunction &F) override;
getRequiredProperties() const331dbf7a57SDerek Schuff   MachineFunctionProperties getRequiredProperties() const override {
341dbf7a57SDerek Schuff     return MachineFunctionProperties().set(
351eb47368SMatthias Braun         MachineFunctionProperties::Property::NoVRegs);
361dbf7a57SDerek Schuff   }
3735ec4e35SRichard Sandiford 
3835ec4e35SRichard Sandiford private:
394b29f6f7SJonas Paulsson   bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH);
4049506d78SUlrich Weigand   bool shortenOn0(MachineInstr &MI, unsigned Opcode);
4149506d78SUlrich Weigand   bool shortenOn01(MachineInstr &MI, unsigned Opcode);
4249506d78SUlrich Weigand   bool shortenOn001(MachineInstr &MI, unsigned Opcode);
434b29f6f7SJonas Paulsson   bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode);
4449506d78SUlrich Weigand   bool shortenFPConv(MachineInstr &MI, unsigned Opcode);
459adc7fc3SJonas Paulsson   bool shortenFusedFPOp(MachineInstr &MI, unsigned Opcode);
4635ec4e35SRichard Sandiford 
4735ec4e35SRichard Sandiford   const SystemZInstrInfo *TII;
484b29f6f7SJonas Paulsson   const TargetRegisterInfo *TRI;
494b29f6f7SJonas Paulsson   LivePhysRegs LiveRegs;
5035ec4e35SRichard Sandiford };
5135ec4e35SRichard Sandiford 
5235ec4e35SRichard Sandiford char SystemZShortenInst::ID = 0;
53c231269fSRichard Sandiford } // end anonymous namespace
5435ec4e35SRichard Sandiford 
55d5ae039eSKai Nacke INITIALIZE_PASS(SystemZShortenInst, DEBUG_TYPE,
56d5ae039eSKai Nacke                 "SystemZ Instruction Shortening", false, false)
57d5ae039eSKai Nacke 
createSystemZShortenInstPass(SystemZTargetMachine & TM)5835ec4e35SRichard Sandiford FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) {
59d5ae039eSKai Nacke   return new SystemZShortenInst();
6035ec4e35SRichard Sandiford }
6135ec4e35SRichard Sandiford 
SystemZShortenInst()62d5ae039eSKai Nacke SystemZShortenInst::SystemZShortenInst()
63d5ae039eSKai Nacke     : MachineFunctionPass(ID), TII(nullptr) {
64d5ae039eSKai Nacke   initializeSystemZShortenInstPass(*PassRegistry::getPassRegistry());
65d5ae039eSKai Nacke }
6635ec4e35SRichard Sandiford 
67dab74072SJonas Paulsson // Tie operands if MI has become a two-address instruction.
tieOpsIfNeeded(MachineInstr & MI)68dab74072SJonas Paulsson static void tieOpsIfNeeded(MachineInstr &MI) {
698bf9e317SJonas Paulsson   if (MI.getDesc().getOperandConstraint(1, MCOI::TIED_TO) == 0 &&
70dab74072SJonas Paulsson       !MI.getOperand(0).isTied())
71dab74072SJonas Paulsson     MI.tieOperands(0, 1);
72dab74072SJonas Paulsson }
73dab74072SJonas Paulsson 
7435ec4e35SRichard Sandiford // MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH
7535ec4e35SRichard Sandiford // are the halfword immediate loads for the same word.  Try to use one of them
764b29f6f7SJonas Paulsson // instead of IIxF.
shortenIIF(MachineInstr & MI,unsigned LLIxL,unsigned LLIxH)77ae7c97d3SNAKAMURA Takumi bool SystemZShortenInst::shortenIIF(MachineInstr &MI, unsigned LLIxL,
78ae7c97d3SNAKAMURA Takumi                                     unsigned LLIxH) {
790c476111SDaniel Sanders   Register Reg = MI.getOperand(0).getReg();
804b29f6f7SJonas Paulsson   // The new opcode will clear the other half of the GR64 reg, so
814b29f6f7SJonas Paulsson   // cancel if that is live.
82fe1202c4SNAKAMURA Takumi   unsigned thisSubRegIdx =
83fe1202c4SNAKAMURA Takumi       (SystemZ::GRH32BitRegClass.contains(Reg) ? SystemZ::subreg_h32
84fe1202c4SNAKAMURA Takumi                                                : SystemZ::subreg_l32);
85fe1202c4SNAKAMURA Takumi   unsigned otherSubRegIdx =
86fe1202c4SNAKAMURA Takumi       (thisSubRegIdx == SystemZ::subreg_l32 ? SystemZ::subreg_h32
87fe1202c4SNAKAMURA Takumi                                             : SystemZ::subreg_l32);
88fe1202c4SNAKAMURA Takumi   unsigned GR64BitReg =
89fe1202c4SNAKAMURA Takumi       TRI->getMatchingSuperReg(Reg, thisSubRegIdx, &SystemZ::GR64BitRegClass);
900c476111SDaniel Sanders   Register OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
914b29f6f7SJonas Paulsson   if (LiveRegs.contains(OtherReg))
9235ec4e35SRichard Sandiford     return false;
9335ec4e35SRichard Sandiford 
9435ec4e35SRichard Sandiford   uint64_t Imm = MI.getOperand(1).getImm();
9535ec4e35SRichard Sandiford   if (SystemZ::isImmLL(Imm)) {
9635ec4e35SRichard Sandiford     MI.setDesc(TII->get(LLIxL));
9735ec4e35SRichard Sandiford     MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
9835ec4e35SRichard Sandiford     return true;
9935ec4e35SRichard Sandiford   }
10035ec4e35SRichard Sandiford   if (SystemZ::isImmLH(Imm)) {
10135ec4e35SRichard Sandiford     MI.setDesc(TII->get(LLIxH));
10235ec4e35SRichard Sandiford     MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
10335ec4e35SRichard Sandiford     MI.getOperand(1).setImm(Imm >> 16);
10435ec4e35SRichard Sandiford     return true;
10535ec4e35SRichard Sandiford   }
10635ec4e35SRichard Sandiford   return false;
10735ec4e35SRichard Sandiford }
10835ec4e35SRichard Sandiford 
10949506d78SUlrich Weigand // Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding.
shortenOn0(MachineInstr & MI,unsigned Opcode)11049506d78SUlrich Weigand bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) {
11149506d78SUlrich Weigand   if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) {
11249506d78SUlrich Weigand     MI.setDesc(TII->get(Opcode));
11349506d78SUlrich Weigand     return true;
11449506d78SUlrich Weigand   }
11549506d78SUlrich Weigand   return false;
11649506d78SUlrich Weigand }
11749506d78SUlrich Weigand 
11849506d78SUlrich Weigand // Change MI's opcode to Opcode if register operands 0 and 1 have a
11949506d78SUlrich Weigand // 4-bit encoding.
shortenOn01(MachineInstr & MI,unsigned Opcode)12049506d78SUlrich Weigand bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) {
12149506d78SUlrich Weigand   if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
12249506d78SUlrich Weigand       SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
12349506d78SUlrich Weigand     MI.setDesc(TII->get(Opcode));
12449506d78SUlrich Weigand     return true;
12549506d78SUlrich Weigand   }
12649506d78SUlrich Weigand   return false;
12749506d78SUlrich Weigand }
12849506d78SUlrich Weigand 
12949506d78SUlrich Weigand // Change MI's opcode to Opcode if register operands 0, 1 and 2 have a
130dab74072SJonas Paulsson // 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0
131dab74072SJonas Paulsson // with op 1, if MI becomes 2-address.
shortenOn001(MachineInstr & MI,unsigned Opcode)13249506d78SUlrich Weigand bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) {
13349506d78SUlrich Weigand   if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
13449506d78SUlrich Weigand       MI.getOperand(1).getReg() == MI.getOperand(0).getReg() &&
13549506d78SUlrich Weigand       SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) {
13649506d78SUlrich Weigand     MI.setDesc(TII->get(Opcode));
137dab74072SJonas Paulsson     tieOpsIfNeeded(MI);
13849506d78SUlrich Weigand     return true;
13949506d78SUlrich Weigand   }
14049506d78SUlrich Weigand   return false;
14149506d78SUlrich Weigand }
14249506d78SUlrich Weigand 
14329d9d8d9SJonas Paulsson // Calls shortenOn001 if CCLive is false. CC def operand is added in
14429d9d8d9SJonas Paulsson // case of success.
shortenOn001AddCC(MachineInstr & MI,unsigned Opcode)145fe1202c4SNAKAMURA Takumi bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI, unsigned Opcode) {
1464b29f6f7SJonas Paulsson   if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) {
14729d9d8d9SJonas Paulsson     MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
1489028acf0SJonas Paulsson       .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
14929d9d8d9SJonas Paulsson     return true;
15029d9d8d9SJonas Paulsson   }
15129d9d8d9SJonas Paulsson   return false;
15229d9d8d9SJonas Paulsson }
15329d9d8d9SJonas Paulsson 
15449506d78SUlrich Weigand // MI is a vector-style conversion instruction with the operand order:
15549506d78SUlrich Weigand // destination, source, exact-suppress, rounding-mode.  If both registers
15649506d78SUlrich Weigand // have a 4-bit encoding then change it to Opcode, which has operand order:
15749506d78SUlrich Weigand // destination, rouding-mode, source, exact-suppress.
shortenFPConv(MachineInstr & MI,unsigned Opcode)15849506d78SUlrich Weigand bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) {
15949506d78SUlrich Weigand   if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 &&
16049506d78SUlrich Weigand       SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
16149506d78SUlrich Weigand     MachineOperand Dest(MI.getOperand(0));
16249506d78SUlrich Weigand     MachineOperand Src(MI.getOperand(1));
16349506d78SUlrich Weigand     MachineOperand Suppress(MI.getOperand(2));
16449506d78SUlrich Weigand     MachineOperand Mode(MI.getOperand(3));
165*37b37838SShengchen Kan     MI.removeOperand(3);
166*37b37838SShengchen Kan     MI.removeOperand(2);
167*37b37838SShengchen Kan     MI.removeOperand(1);
168*37b37838SShengchen Kan     MI.removeOperand(0);
16949506d78SUlrich Weigand     MI.setDesc(TII->get(Opcode));
17049506d78SUlrich Weigand     MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
171116bbab4SDiana Picus         .add(Dest)
172116bbab4SDiana Picus         .add(Mode)
173116bbab4SDiana Picus         .add(Src)
174116bbab4SDiana Picus         .add(Suppress);
17549506d78SUlrich Weigand     return true;
17649506d78SUlrich Weigand   }
17749506d78SUlrich Weigand   return false;
17849506d78SUlrich Weigand }
17949506d78SUlrich Weigand 
shortenFusedFPOp(MachineInstr & MI,unsigned Opcode)1809adc7fc3SJonas Paulsson bool SystemZShortenInst::shortenFusedFPOp(MachineInstr &MI, unsigned Opcode) {
1819adc7fc3SJonas Paulsson   MachineOperand &DstMO = MI.getOperand(0);
1829adc7fc3SJonas Paulsson   MachineOperand &LHSMO = MI.getOperand(1);
1839adc7fc3SJonas Paulsson   MachineOperand &RHSMO = MI.getOperand(2);
1849adc7fc3SJonas Paulsson   MachineOperand &AccMO = MI.getOperand(3);
1859adc7fc3SJonas Paulsson   if (SystemZMC::getFirstReg(DstMO.getReg()) < 16 &&
1869adc7fc3SJonas Paulsson       SystemZMC::getFirstReg(LHSMO.getReg()) < 16 &&
1879adc7fc3SJonas Paulsson       SystemZMC::getFirstReg(RHSMO.getReg()) < 16 &&
1889adc7fc3SJonas Paulsson       SystemZMC::getFirstReg(AccMO.getReg()) < 16 &&
1899adc7fc3SJonas Paulsson       DstMO.getReg() == AccMO.getReg()) {
1909adc7fc3SJonas Paulsson     MachineOperand Lhs(LHSMO);
1919adc7fc3SJonas Paulsson     MachineOperand Rhs(RHSMO);
1929adc7fc3SJonas Paulsson     MachineOperand Src(AccMO);
193*37b37838SShengchen Kan     MI.removeOperand(3);
194*37b37838SShengchen Kan     MI.removeOperand(2);
195*37b37838SShengchen Kan     MI.removeOperand(1);
1969adc7fc3SJonas Paulsson     MI.setDesc(TII->get(Opcode));
1979adc7fc3SJonas Paulsson     MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
1989adc7fc3SJonas Paulsson         .add(Src)
1999adc7fc3SJonas Paulsson         .add(Lhs)
2009adc7fc3SJonas Paulsson         .add(Rhs);
2019adc7fc3SJonas Paulsson     return true;
2029adc7fc3SJonas Paulsson   }
2039adc7fc3SJonas Paulsson   return false;
2049adc7fc3SJonas Paulsson }
2059adc7fc3SJonas Paulsson 
20635ec4e35SRichard Sandiford // Process all instructions in MBB.  Return true if something changed.
processBlock(MachineBasicBlock & MBB)20728c111ecSRichard Sandiford bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) {
20835ec4e35SRichard Sandiford   bool Changed = false;
20935ec4e35SRichard Sandiford 
2104b29f6f7SJonas Paulsson   // Set up the set of live registers at the end of MBB (live out)
2114b29f6f7SJonas Paulsson   LiveRegs.clear();
212d1aabb28SMatthias Braun   LiveRegs.addLiveOuts(MBB);
21335ec4e35SRichard Sandiford 
21435ec4e35SRichard Sandiford   // Iterate backwards through the block looking for instructions to change.
21514d656b3SKazu Hirata   for (MachineInstr &MI : llvm::reverse(MBB)) {
21649506d78SUlrich Weigand     switch (MI.getOpcode()) {
21749506d78SUlrich Weigand     case SystemZ::IILF:
2184b29f6f7SJonas Paulsson       Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH);
21949506d78SUlrich Weigand       break;
22049506d78SUlrich Weigand 
22149506d78SUlrich Weigand     case SystemZ::IIHF:
2224b29f6f7SJonas Paulsson       Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH);
22349506d78SUlrich Weigand       break;
22449506d78SUlrich Weigand 
22549506d78SUlrich Weigand     case SystemZ::WFADB:
2264b29f6f7SJonas Paulsson       Changed |= shortenOn001AddCC(MI, SystemZ::ADBR);
22749506d78SUlrich Weigand       break;
22849506d78SUlrich Weigand 
22933435c4cSUlrich Weigand     case SystemZ::WFASB:
23033435c4cSUlrich Weigand       Changed |= shortenOn001AddCC(MI, SystemZ::AEBR);
23133435c4cSUlrich Weigand       break;
23233435c4cSUlrich Weigand 
23349506d78SUlrich Weigand     case SystemZ::WFDDB:
23449506d78SUlrich Weigand       Changed |= shortenOn001(MI, SystemZ::DDBR);
23549506d78SUlrich Weigand       break;
23649506d78SUlrich Weigand 
23733435c4cSUlrich Weigand     case SystemZ::WFDSB:
23833435c4cSUlrich Weigand       Changed |= shortenOn001(MI, SystemZ::DEBR);
23933435c4cSUlrich Weigand       break;
24033435c4cSUlrich Weigand 
24149506d78SUlrich Weigand     case SystemZ::WFIDB:
24249506d78SUlrich Weigand       Changed |= shortenFPConv(MI, SystemZ::FIDBRA);
24349506d78SUlrich Weigand       break;
24449506d78SUlrich Weigand 
24533435c4cSUlrich Weigand     case SystemZ::WFISB:
24633435c4cSUlrich Weigand       Changed |= shortenFPConv(MI, SystemZ::FIEBRA);
24733435c4cSUlrich Weigand       break;
24833435c4cSUlrich Weigand 
24949506d78SUlrich Weigand     case SystemZ::WLDEB:
25049506d78SUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::LDEBR);
25149506d78SUlrich Weigand       break;
25249506d78SUlrich Weigand 
25349506d78SUlrich Weigand     case SystemZ::WLEDB:
25449506d78SUlrich Weigand       Changed |= shortenFPConv(MI, SystemZ::LEDBRA);
25549506d78SUlrich Weigand       break;
25649506d78SUlrich Weigand 
25749506d78SUlrich Weigand     case SystemZ::WFMDB:
25849506d78SUlrich Weigand       Changed |= shortenOn001(MI, SystemZ::MDBR);
25949506d78SUlrich Weigand       break;
26049506d78SUlrich Weigand 
26133435c4cSUlrich Weigand     case SystemZ::WFMSB:
26233435c4cSUlrich Weigand       Changed |= shortenOn001(MI, SystemZ::MEEBR);
26333435c4cSUlrich Weigand       break;
26433435c4cSUlrich Weigand 
2659adc7fc3SJonas Paulsson     case SystemZ::WFMADB:
2669adc7fc3SJonas Paulsson       Changed |= shortenFusedFPOp(MI, SystemZ::MADBR);
2679adc7fc3SJonas Paulsson       break;
2689adc7fc3SJonas Paulsson 
2699adc7fc3SJonas Paulsson     case SystemZ::WFMASB:
2709adc7fc3SJonas Paulsson       Changed |= shortenFusedFPOp(MI, SystemZ::MAEBR);
2719adc7fc3SJonas Paulsson       break;
2729adc7fc3SJonas Paulsson 
2739adc7fc3SJonas Paulsson     case SystemZ::WFMSDB:
2749adc7fc3SJonas Paulsson       Changed |= shortenFusedFPOp(MI, SystemZ::MSDBR);
2759adc7fc3SJonas Paulsson       break;
2769adc7fc3SJonas Paulsson 
2779adc7fc3SJonas Paulsson     case SystemZ::WFMSSB:
2789adc7fc3SJonas Paulsson       Changed |= shortenFusedFPOp(MI, SystemZ::MSEBR);
2799adc7fc3SJonas Paulsson       break;
2809adc7fc3SJonas Paulsson 
28149506d78SUlrich Weigand     case SystemZ::WFLCDB:
28212629324SJonas Paulsson       Changed |= shortenOn01(MI, SystemZ::LCDFR);
28349506d78SUlrich Weigand       break;
28449506d78SUlrich Weigand 
28533435c4cSUlrich Weigand     case SystemZ::WFLCSB:
28633435c4cSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::LCDFR_32);
28733435c4cSUlrich Weigand       break;
28833435c4cSUlrich Weigand 
28949506d78SUlrich Weigand     case SystemZ::WFLNDB:
29012629324SJonas Paulsson       Changed |= shortenOn01(MI, SystemZ::LNDFR);
29149506d78SUlrich Weigand       break;
29249506d78SUlrich Weigand 
29333435c4cSUlrich Weigand     case SystemZ::WFLNSB:
29433435c4cSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::LNDFR_32);
29533435c4cSUlrich Weigand       break;
29633435c4cSUlrich Weigand 
29749506d78SUlrich Weigand     case SystemZ::WFLPDB:
29812629324SJonas Paulsson       Changed |= shortenOn01(MI, SystemZ::LPDFR);
29949506d78SUlrich Weigand       break;
30049506d78SUlrich Weigand 
30133435c4cSUlrich Weigand     case SystemZ::WFLPSB:
30233435c4cSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::LPDFR_32);
30333435c4cSUlrich Weigand       break;
30433435c4cSUlrich Weigand 
30549506d78SUlrich Weigand     case SystemZ::WFSQDB:
30649506d78SUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::SQDBR);
30749506d78SUlrich Weigand       break;
30849506d78SUlrich Weigand 
30933435c4cSUlrich Weigand     case SystemZ::WFSQSB:
31033435c4cSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::SQEBR);
31133435c4cSUlrich Weigand       break;
31233435c4cSUlrich Weigand 
3135b3bab40SJonas Paulsson     case SystemZ::WFSDB:
3144b29f6f7SJonas Paulsson       Changed |= shortenOn001AddCC(MI, SystemZ::SDBR);
31549506d78SUlrich Weigand       break;
3165b3bab40SJonas Paulsson 
31733435c4cSUlrich Weigand     case SystemZ::WFSSB:
31833435c4cSUlrich Weigand       Changed |= shortenOn001AddCC(MI, SystemZ::SEBR);
31933435c4cSUlrich Weigand       break;
32033435c4cSUlrich Weigand 
32149506d78SUlrich Weigand     case SystemZ::WFCDB:
32249506d78SUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::CDBR);
32349506d78SUlrich Weigand       break;
32449506d78SUlrich Weigand 
32533435c4cSUlrich Weigand     case SystemZ::WFCSB:
32633435c4cSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::CEBR);
32733435c4cSUlrich Weigand       break;
32833435c4cSUlrich Weigand 
3299db13b5aSUlrich Weigand     case SystemZ::WFKDB:
3309db13b5aSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::KDBR);
3319db13b5aSUlrich Weigand       break;
3329db13b5aSUlrich Weigand 
3339db13b5aSUlrich Weigand     case SystemZ::WFKSB:
3349db13b5aSUlrich Weigand       Changed |= shortenOn01(MI, SystemZ::KEBR);
3359db13b5aSUlrich Weigand       break;
3369db13b5aSUlrich Weigand 
33749506d78SUlrich Weigand     case SystemZ::VL32:
33849506d78SUlrich Weigand       // For z13 we prefer LDE over LE to avoid partial register dependencies.
33949506d78SUlrich Weigand       Changed |= shortenOn0(MI, SystemZ::LDE32);
34049506d78SUlrich Weigand       break;
34149506d78SUlrich Weigand 
34249506d78SUlrich Weigand     case SystemZ::VST32:
34349506d78SUlrich Weigand       Changed |= shortenOn0(MI, SystemZ::STE);
34449506d78SUlrich Weigand       break;
34549506d78SUlrich Weigand 
34649506d78SUlrich Weigand     case SystemZ::VL64:
34749506d78SUlrich Weigand       Changed |= shortenOn0(MI, SystemZ::LD);
34849506d78SUlrich Weigand       break;
34949506d78SUlrich Weigand 
35049506d78SUlrich Weigand     case SystemZ::VST64:
35149506d78SUlrich Weigand       Changed |= shortenOn0(MI, SystemZ::STD);
35249506d78SUlrich Weigand       break;
353fdc4ea34SJonas Paulsson 
354fdc4ea34SJonas Paulsson     default: {
355fdc4ea34SJonas Paulsson       int TwoOperandOpcode = SystemZ::getTwoOperandOpcode(MI.getOpcode());
356fdc4ea34SJonas Paulsson       if (TwoOperandOpcode == -1)
357fdc4ea34SJonas Paulsson         break;
358fdc4ea34SJonas Paulsson 
359fdc4ea34SJonas Paulsson       if ((MI.getOperand(0).getReg() != MI.getOperand(1).getReg()) &&
360fdc4ea34SJonas Paulsson           (!MI.isCommutable() ||
361fdc4ea34SJonas Paulsson            MI.getOperand(0).getReg() != MI.getOperand(2).getReg() ||
362fdc4ea34SJonas Paulsson            !TII->commuteInstruction(MI, false, 1, 2)))
363fdc4ea34SJonas Paulsson           break;
364fdc4ea34SJonas Paulsson 
365fdc4ea34SJonas Paulsson       MI.setDesc(TII->get(TwoOperandOpcode));
366fdc4ea34SJonas Paulsson       MI.tieOperands(0, 1);
367fdc4ea34SJonas Paulsson       if (TwoOperandOpcode == SystemZ::SLL ||
368fdc4ea34SJonas Paulsson           TwoOperandOpcode == SystemZ::SLA ||
369fdc4ea34SJonas Paulsson           TwoOperandOpcode == SystemZ::SRL ||
370fdc4ea34SJonas Paulsson           TwoOperandOpcode == SystemZ::SRA) {
371fdc4ea34SJonas Paulsson         // These shifts only use the low 6 bits of the shift count.
372fdc4ea34SJonas Paulsson         MachineOperand &ImmMO = MI.getOperand(3);
373fdc4ea34SJonas Paulsson         ImmMO.setImm(ImmMO.getImm() & 0xfff);
374fdc4ea34SJonas Paulsson       }
375fdc4ea34SJonas Paulsson       Changed = true;
376fdc4ea34SJonas Paulsson       break;
377fdc4ea34SJonas Paulsson     }
37849506d78SUlrich Weigand     }
37949506d78SUlrich Weigand 
3804b29f6f7SJonas Paulsson     LiveRegs.stepBackward(MI);
38135ec4e35SRichard Sandiford   }
38235ec4e35SRichard Sandiford 
38335ec4e35SRichard Sandiford   return Changed;
38435ec4e35SRichard Sandiford }
38535ec4e35SRichard Sandiford 
runOnMachineFunction(MachineFunction & F)38635ec4e35SRichard Sandiford bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) {
387f1caa283SMatthias Braun   if (skipFunction(F.getFunction()))
388d9974cc9SAndrew Kaylor     return false;
389d9974cc9SAndrew Kaylor 
3904b29f6f7SJonas Paulsson   const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
3914b29f6f7SJonas Paulsson   TII = ST.getInstrInfo();
3924b29f6f7SJonas Paulsson   TRI = ST.getRegisterInfo();
3930c989a89SMatthias Braun   LiveRegs.init(*TRI);
39435ec4e35SRichard Sandiford 
39535ec4e35SRichard Sandiford   bool Changed = false;
39628c111ecSRichard Sandiford   for (auto &MBB : F)
39728c111ecSRichard Sandiford     Changed |= processBlock(MBB);
39835ec4e35SRichard Sandiford 
39935ec4e35SRichard Sandiford   return Changed;
40035ec4e35SRichard Sandiford }
401