Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 1 | //===-- 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 Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 16 | #include "SystemZTargetMachine.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LivePhysRegs.h" |
| 20 | #include "llvm/Target/TargetRegisterInfo.h" |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "systemz-shorten-inst" |
| 25 | |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 26 | namespace { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 27 | class SystemZShortenInst : public MachineFunctionPass { |
| 28 | public: |
| 29 | static char ID; |
| 30 | SystemZShortenInst(const SystemZTargetMachine &tm); |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 31 | |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 32 | const char *getPassName() const override { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 33 | return "SystemZ Instruction Shortening"; |
| 34 | } |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 35 | |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 36 | bool processBlock(MachineBasicBlock &MBB); |
Craig Topper | 9d74a5a | 2014-04-29 07:58:41 +0000 | [diff] [blame] | 37 | bool runOnMachineFunction(MachineFunction &F) override; |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 38 | MachineFunctionProperties getRequiredProperties() const override { |
| 39 | return MachineFunctionProperties().set( |
| 40 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 41 | } |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 42 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 43 | private: |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 44 | bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 45 | bool shortenOn0(MachineInstr &MI, unsigned Opcode); |
| 46 | bool shortenOn01(MachineInstr &MI, unsigned Opcode); |
| 47 | bool shortenOn001(MachineInstr &MI, unsigned Opcode); |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 48 | bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 49 | bool shortenFPConv(MachineInstr &MI, unsigned Opcode); |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 50 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 51 | const SystemZInstrInfo *TII; |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 52 | const TargetRegisterInfo *TRI; |
| 53 | LivePhysRegs LiveRegs; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 54 | }; |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 55 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 56 | char SystemZShortenInst::ID = 0; |
| 57 | } // end anonymous namespace |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 58 | |
| 59 | FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) { |
| 60 | return new SystemZShortenInst(TM); |
| 61 | } |
| 62 | |
| 63 | SystemZShortenInst::SystemZShortenInst(const SystemZTargetMachine &tm) |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 64 | : MachineFunctionPass(ID), TII(nullptr) {} |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 65 | |
Jonas Paulsson | dab7407 | 2015-10-26 15:03:07 +0000 | [diff] [blame] | 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()) |
| 70 | MI.tieOperands(0, 1); |
| 71 | } |
| 72 | |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 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 |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 75 | // instead of IIxF. |
| 76 | bool SystemZShortenInst::shortenIIF(MachineInstr &MI, |
| 77 | unsigned LLIxL, unsigned LLIxH) { |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 78 | unsigned Reg = MI.getOperand(0).getReg(); |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 79 | // 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 Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 89 | 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 Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 106 | // Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding. |
| 107 | bool 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. |
| 117 | bool 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 Paulsson | dab7407 | 2015-10-26 15:03:07 +0000 | [diff] [blame] | 127 | // 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 Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 129 | bool 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 Paulsson | dab7407 | 2015-10-26 15:03:07 +0000 | [diff] [blame] | 134 | tieOpsIfNeeded(MI); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
Jonas Paulsson | 29d9d8d | 2015-10-08 07:40:19 +0000 | [diff] [blame] | 140 | // Calls shortenOn001 if CCLive is false. CC def operand is added in |
| 141 | // case of success. |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 142 | bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI, |
| 143 | unsigned Opcode) { |
| 144 | if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) { |
Jonas Paulsson | 29d9d8d | 2015-10-08 07:40:19 +0000 | [diff] [blame] | 145 | MachineInstrBuilder(*MI.getParent()->getParent(), &MI) |
| 146 | .addReg(SystemZ::CC, RegState::ImplicitDefine); |
| 147 | return true; |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 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)); |
| 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 Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 178 | // Process all instructions in MBB. Return true if something changed. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 179 | bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) { |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 180 | bool Changed = false; |
| 181 | |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 182 | // Set up the set of live registers at the end of MBB (live out) |
| 183 | LiveRegs.clear(); |
| 184 | LiveRegs.addLiveOuts(&MBB); |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 185 | |
| 186 | // Iterate backwards through the block looking for instructions to change. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 187 | for (auto MBBI = MBB.rbegin(), MBBE = MBB.rend(); MBBI != MBBE; ++MBBI) { |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 188 | MachineInstr &MI = *MBBI; |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 189 | switch (MI.getOpcode()) { |
| 190 | case SystemZ::IILF: |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 191 | Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 192 | break; |
| 193 | |
| 194 | case SystemZ::IIHF: |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 195 | Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | case SystemZ::WFADB: |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 199 | Changed |= shortenOn001AddCC(MI, SystemZ::ADBR); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 200 | 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 Paulsson | 1262932 | 2015-10-01 18:12:28 +0000 | [diff] [blame] | 223 | Changed |= shortenOn01(MI, SystemZ::LCDFR); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | case SystemZ::WFLNDB: |
Jonas Paulsson | 1262932 | 2015-10-01 18:12:28 +0000 | [diff] [blame] | 227 | Changed |= shortenOn01(MI, SystemZ::LNDFR); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 228 | break; |
| 229 | |
| 230 | case SystemZ::WFLPDB: |
Jonas Paulsson | 1262932 | 2015-10-01 18:12:28 +0000 | [diff] [blame] | 231 | Changed |= shortenOn01(MI, SystemZ::LPDFR); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 232 | break; |
| 233 | |
| 234 | case SystemZ::WFSQDB: |
| 235 | Changed |= shortenOn01(MI, SystemZ::SQDBR); |
| 236 | break; |
| 237 | |
Jonas Paulsson | 5b3bab4 | 2015-10-09 07:19:20 +0000 | [diff] [blame] | 238 | case SystemZ::WFSDB: |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 239 | Changed |= shortenOn001AddCC(MI, SystemZ::SDBR); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 240 | break; |
Jonas Paulsson | 5b3bab4 | 2015-10-09 07:19:20 +0000 | [diff] [blame] | 241 | |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 242 | 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 Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 264 | LiveRegs.stepBackward(MI); |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | return Changed; |
| 268 | } |
| 269 | |
| 270 | bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) { |
Andrew Kaylor | d9974cc | 2016-04-26 23:49:41 +0000 | [diff] [blame^] | 271 | if (skipFunction(*F.getFunction())) |
| 272 | return false; |
| 273 | |
Jonas Paulsson | 4b29f6f | 2015-10-20 15:05:58 +0000 | [diff] [blame] | 274 | const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>(); |
| 275 | TII = ST.getInstrInfo(); |
| 276 | TRI = ST.getRegisterInfo(); |
| 277 | LiveRegs.init(TRI); |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 278 | |
| 279 | bool Changed = false; |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 280 | for (auto &MBB : F) |
| 281 | Changed |= processBlock(MBB); |
Richard Sandiford | 35ec4e356 | 2013-09-25 10:11:07 +0000 | [diff] [blame] | 282 | |
| 283 | return Changed; |
| 284 | } |