/src/llvm-project/llvm/lib/Target/SystemZ/SystemZShortenInst.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- SystemZShortenInst.cpp - Instruction-shortening pass --------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
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. |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
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" |
20 | | |
21 | | using namespace llvm; |
22 | | |
23 | | #define DEBUG_TYPE "systemz-shorten-inst" |
24 | | |
25 | | namespace { |
26 | | class SystemZShortenInst : public MachineFunctionPass { |
27 | | public: |
28 | | static char ID; |
29 | | SystemZShortenInst(); |
30 | | |
31 | | bool processBlock(MachineBasicBlock &MBB); |
32 | | bool runOnMachineFunction(MachineFunction &F) override; |
33 | 0 | MachineFunctionProperties getRequiredProperties() const override { |
34 | 0 | return MachineFunctionProperties().set( |
35 | 0 | MachineFunctionProperties::Property::NoVRegs); |
36 | 0 | } |
37 | | |
38 | | private: |
39 | | bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH); |
40 | | bool shortenOn0(MachineInstr &MI, unsigned Opcode); |
41 | | bool shortenOn01(MachineInstr &MI, unsigned Opcode); |
42 | | bool shortenOn001(MachineInstr &MI, unsigned Opcode); |
43 | | bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode); |
44 | | bool shortenFPConv(MachineInstr &MI, unsigned Opcode); |
45 | | bool shortenFusedFPOp(MachineInstr &MI, unsigned Opcode); |
46 | | |
47 | | const SystemZInstrInfo *TII; |
48 | | const TargetRegisterInfo *TRI; |
49 | | LivePhysRegs LiveRegs; |
50 | | }; |
51 | | |
52 | | char SystemZShortenInst::ID = 0; |
53 | | } // end anonymous namespace |
54 | | |
55 | | INITIALIZE_PASS(SystemZShortenInst, DEBUG_TYPE, |
56 | | "SystemZ Instruction Shortening", false, false) |
57 | | |
58 | 0 | FunctionPass *llvm::createSystemZShortenInstPass(SystemZTargetMachine &TM) { |
59 | 0 | return new SystemZShortenInst(); |
60 | 0 | } |
61 | | |
62 | | SystemZShortenInst::SystemZShortenInst() |
63 | 0 | : MachineFunctionPass(ID), TII(nullptr) { |
64 | 0 | initializeSystemZShortenInstPass(*PassRegistry::getPassRegistry()); |
65 | 0 | } |
66 | | |
67 | | // Tie operands if MI has become a two-address instruction. |
68 | 0 | static void tieOpsIfNeeded(MachineInstr &MI) { |
69 | 0 | if (MI.getDesc().getOperandConstraint(1, MCOI::TIED_TO) == 0 && |
70 | 0 | !MI.getOperand(0).isTied()) |
71 | 0 | MI.tieOperands(0, 1); |
72 | 0 | } |
73 | | |
74 | | // MI loads one word of a GPR using an IIxF instruction and LLIxL and LLIxH |
75 | | // are the halfword immediate loads for the same word. Try to use one of them |
76 | | // instead of IIxF. |
77 | | bool SystemZShortenInst::shortenIIF(MachineInstr &MI, unsigned LLIxL, |
78 | 0 | unsigned LLIxH) { |
79 | 0 | Register Reg = MI.getOperand(0).getReg(); |
80 | | // The new opcode will clear the other half of the GR64 reg, so |
81 | | // cancel if that is live. |
82 | 0 | unsigned thisSubRegIdx = |
83 | 0 | (SystemZ::GRH32BitRegClass.contains(Reg) ? SystemZ::subreg_h32 |
84 | 0 | : SystemZ::subreg_l32); |
85 | 0 | unsigned otherSubRegIdx = |
86 | 0 | (thisSubRegIdx == SystemZ::subreg_l32 ? SystemZ::subreg_h32 |
87 | 0 | : SystemZ::subreg_l32); |
88 | 0 | unsigned GR64BitReg = |
89 | 0 | TRI->getMatchingSuperReg(Reg, thisSubRegIdx, &SystemZ::GR64BitRegClass); |
90 | 0 | Register OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx); |
91 | 0 | if (LiveRegs.contains(OtherReg)) |
92 | 0 | return false; |
93 | | |
94 | 0 | uint64_t Imm = MI.getOperand(1).getImm(); |
95 | 0 | if (SystemZ::isImmLL(Imm)) { |
96 | 0 | MI.setDesc(TII->get(LLIxL)); |
97 | 0 | MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg)); |
98 | 0 | return true; |
99 | 0 | } |
100 | 0 | if (SystemZ::isImmLH(Imm)) { |
101 | 0 | MI.setDesc(TII->get(LLIxH)); |
102 | 0 | MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg)); |
103 | 0 | MI.getOperand(1).setImm(Imm >> 16); |
104 | 0 | return true; |
105 | 0 | } |
106 | 0 | return false; |
107 | 0 | } |
108 | | |
109 | | // Change MI's opcode to Opcode if register operand 0 has a 4-bit encoding. |
110 | 0 | bool SystemZShortenInst::shortenOn0(MachineInstr &MI, unsigned Opcode) { |
111 | 0 | if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16) { |
112 | 0 | MI.setDesc(TII->get(Opcode)); |
113 | 0 | return true; |
114 | 0 | } |
115 | 0 | return false; |
116 | 0 | } |
117 | | |
118 | | // Change MI's opcode to Opcode if register operands 0 and 1 have a |
119 | | // 4-bit encoding. |
120 | 0 | bool SystemZShortenInst::shortenOn01(MachineInstr &MI, unsigned Opcode) { |
121 | 0 | if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 && |
122 | 0 | SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) { |
123 | 0 | MI.setDesc(TII->get(Opcode)); |
124 | 0 | return true; |
125 | 0 | } |
126 | 0 | return false; |
127 | 0 | } |
128 | | |
129 | | // Change MI's opcode to Opcode if register operands 0, 1 and 2 have a |
130 | | // 4-bit encoding and if operands 0 and 1 are tied. Also ties op 0 |
131 | | // with op 1, if MI becomes 2-address. |
132 | 0 | bool SystemZShortenInst::shortenOn001(MachineInstr &MI, unsigned Opcode) { |
133 | 0 | if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 && |
134 | 0 | MI.getOperand(1).getReg() == MI.getOperand(0).getReg() && |
135 | 0 | SystemZMC::getFirstReg(MI.getOperand(2).getReg()) < 16) { |
136 | 0 | MI.setDesc(TII->get(Opcode)); |
137 | 0 | tieOpsIfNeeded(MI); |
138 | 0 | return true; |
139 | 0 | } |
140 | 0 | return false; |
141 | 0 | } |
142 | | |
143 | | // Calls shortenOn001 if CCLive is false. CC def operand is added in |
144 | | // case of success. |
145 | 0 | bool SystemZShortenInst::shortenOn001AddCC(MachineInstr &MI, unsigned Opcode) { |
146 | 0 | if (!LiveRegs.contains(SystemZ::CC) && shortenOn001(MI, Opcode)) { |
147 | 0 | MachineInstrBuilder(*MI.getParent()->getParent(), &MI) |
148 | 0 | .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); |
149 | 0 | return true; |
150 | 0 | } |
151 | 0 | return false; |
152 | 0 | } |
153 | | |
154 | | // MI is a vector-style conversion instruction with the operand order: |
155 | | // destination, source, exact-suppress, rounding-mode. If both registers |
156 | | // have a 4-bit encoding then change it to Opcode, which has operand order: |
157 | | // destination, rouding-mode, source, exact-suppress. |
158 | 0 | bool SystemZShortenInst::shortenFPConv(MachineInstr &MI, unsigned Opcode) { |
159 | 0 | if (SystemZMC::getFirstReg(MI.getOperand(0).getReg()) < 16 && |
160 | 0 | SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) { |
161 | 0 | MachineOperand Dest(MI.getOperand(0)); |
162 | 0 | MachineOperand Src(MI.getOperand(1)); |
163 | 0 | MachineOperand Suppress(MI.getOperand(2)); |
164 | 0 | MachineOperand Mode(MI.getOperand(3)); |
165 | 0 | MI.removeOperand(3); |
166 | 0 | MI.removeOperand(2); |
167 | 0 | MI.removeOperand(1); |
168 | 0 | MI.removeOperand(0); |
169 | 0 | MI.setDesc(TII->get(Opcode)); |
170 | 0 | MachineInstrBuilder(*MI.getParent()->getParent(), &MI) |
171 | 0 | .add(Dest) |
172 | 0 | .add(Mode) |
173 | 0 | .add(Src) |
174 | 0 | .add(Suppress); |
175 | 0 | return true; |
176 | 0 | } |
177 | 0 | return false; |
178 | 0 | } |
179 | | |
180 | 0 | bool SystemZShortenInst::shortenFusedFPOp(MachineInstr &MI, unsigned Opcode) { |
181 | 0 | MachineOperand &DstMO = MI.getOperand(0); |
182 | 0 | MachineOperand &LHSMO = MI.getOperand(1); |
183 | 0 | MachineOperand &RHSMO = MI.getOperand(2); |
184 | 0 | MachineOperand &AccMO = MI.getOperand(3); |
185 | 0 | if (SystemZMC::getFirstReg(DstMO.getReg()) < 16 && |
186 | 0 | SystemZMC::getFirstReg(LHSMO.getReg()) < 16 && |
187 | 0 | SystemZMC::getFirstReg(RHSMO.getReg()) < 16 && |
188 | 0 | SystemZMC::getFirstReg(AccMO.getReg()) < 16 && |
189 | 0 | DstMO.getReg() == AccMO.getReg()) { |
190 | 0 | MachineOperand Lhs(LHSMO); |
191 | 0 | MachineOperand Rhs(RHSMO); |
192 | 0 | MachineOperand Src(AccMO); |
193 | 0 | MI.removeOperand(3); |
194 | 0 | MI.removeOperand(2); |
195 | 0 | MI.removeOperand(1); |
196 | 0 | MI.setDesc(TII->get(Opcode)); |
197 | 0 | MachineInstrBuilder(*MI.getParent()->getParent(), &MI) |
198 | 0 | .add(Src) |
199 | 0 | .add(Lhs) |
200 | 0 | .add(Rhs); |
201 | 0 | return true; |
202 | 0 | } |
203 | 0 | return false; |
204 | 0 | } |
205 | | |
206 | | // Process all instructions in MBB. Return true if something changed. |
207 | 0 | bool SystemZShortenInst::processBlock(MachineBasicBlock &MBB) { |
208 | 0 | bool Changed = false; |
209 | | |
210 | | // Set up the set of live registers at the end of MBB (live out) |
211 | 0 | LiveRegs.clear(); |
212 | 0 | LiveRegs.addLiveOuts(MBB); |
213 | | |
214 | | // Iterate backwards through the block looking for instructions to change. |
215 | 0 | for (MachineInstr &MI : llvm::reverse(MBB)) { |
216 | 0 | switch (MI.getOpcode()) { |
217 | 0 | case SystemZ::IILF: |
218 | 0 | Changed |= shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH); |
219 | 0 | break; |
220 | | |
221 | 0 | case SystemZ::IIHF: |
222 | 0 | Changed |= shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH); |
223 | 0 | break; |
224 | | |
225 | 0 | case SystemZ::WFADB: |
226 | 0 | Changed |= shortenOn001AddCC(MI, SystemZ::ADBR); |
227 | 0 | break; |
228 | | |
229 | 0 | case SystemZ::WFASB: |
230 | 0 | Changed |= shortenOn001AddCC(MI, SystemZ::AEBR); |
231 | 0 | break; |
232 | | |
233 | 0 | case SystemZ::WFDDB: |
234 | 0 | Changed |= shortenOn001(MI, SystemZ::DDBR); |
235 | 0 | break; |
236 | | |
237 | 0 | case SystemZ::WFDSB: |
238 | 0 | Changed |= shortenOn001(MI, SystemZ::DEBR); |
239 | 0 | break; |
240 | | |
241 | 0 | case SystemZ::WFIDB: |
242 | 0 | Changed |= shortenFPConv(MI, SystemZ::FIDBRA); |
243 | 0 | break; |
244 | | |
245 | 0 | case SystemZ::WFISB: |
246 | 0 | Changed |= shortenFPConv(MI, SystemZ::FIEBRA); |
247 | 0 | break; |
248 | | |
249 | 0 | case SystemZ::WLDEB: |
250 | 0 | Changed |= shortenOn01(MI, SystemZ::LDEBR); |
251 | 0 | break; |
252 | | |
253 | 0 | case SystemZ::WLEDB: |
254 | 0 | Changed |= shortenFPConv(MI, SystemZ::LEDBRA); |
255 | 0 | break; |
256 | | |
257 | 0 | case SystemZ::WFMDB: |
258 | 0 | Changed |= shortenOn001(MI, SystemZ::MDBR); |
259 | 0 | break; |
260 | | |
261 | 0 | case SystemZ::WFMSB: |
262 | 0 | Changed |= shortenOn001(MI, SystemZ::MEEBR); |
263 | 0 | break; |
264 | | |
265 | 0 | case SystemZ::WFMADB: |
266 | 0 | Changed |= shortenFusedFPOp(MI, SystemZ::MADBR); |
267 | 0 | break; |
268 | | |
269 | 0 | case SystemZ::WFMASB: |
270 | 0 | Changed |= shortenFusedFPOp(MI, SystemZ::MAEBR); |
271 | 0 | break; |
272 | | |
273 | 0 | case SystemZ::WFMSDB: |
274 | 0 | Changed |= shortenFusedFPOp(MI, SystemZ::MSDBR); |
275 | 0 | break; |
276 | | |
277 | 0 | case SystemZ::WFMSSB: |
278 | 0 | Changed |= shortenFusedFPOp(MI, SystemZ::MSEBR); |
279 | 0 | break; |
280 | | |
281 | 0 | case SystemZ::WFLCDB: |
282 | 0 | Changed |= shortenOn01(MI, SystemZ::LCDFR); |
283 | 0 | break; |
284 | | |
285 | 0 | case SystemZ::WFLCSB: |
286 | 0 | Changed |= shortenOn01(MI, SystemZ::LCDFR_32); |
287 | 0 | break; |
288 | | |
289 | 0 | case SystemZ::WFLNDB: |
290 | 0 | Changed |= shortenOn01(MI, SystemZ::LNDFR); |
291 | 0 | break; |
292 | | |
293 | 0 | case SystemZ::WFLNSB: |
294 | 0 | Changed |= shortenOn01(MI, SystemZ::LNDFR_32); |
295 | 0 | break; |
296 | | |
297 | 0 | case SystemZ::WFLPDB: |
298 | 0 | Changed |= shortenOn01(MI, SystemZ::LPDFR); |
299 | 0 | break; |
300 | | |
301 | 0 | case SystemZ::WFLPSB: |
302 | 0 | Changed |= shortenOn01(MI, SystemZ::LPDFR_32); |
303 | 0 | break; |
304 | | |
305 | 0 | case SystemZ::WFSQDB: |
306 | 0 | Changed |= shortenOn01(MI, SystemZ::SQDBR); |
307 | 0 | break; |
308 | | |
309 | 0 | case SystemZ::WFSQSB: |
310 | 0 | Changed |= shortenOn01(MI, SystemZ::SQEBR); |
311 | 0 | break; |
312 | | |
313 | 0 | case SystemZ::WFSDB: |
314 | 0 | Changed |= shortenOn001AddCC(MI, SystemZ::SDBR); |
315 | 0 | break; |
316 | | |
317 | 0 | case SystemZ::WFSSB: |
318 | 0 | Changed |= shortenOn001AddCC(MI, SystemZ::SEBR); |
319 | 0 | break; |
320 | | |
321 | 0 | case SystemZ::WFCDB: |
322 | 0 | Changed |= shortenOn01(MI, SystemZ::CDBR); |
323 | 0 | break; |
324 | | |
325 | 0 | case SystemZ::WFCSB: |
326 | 0 | Changed |= shortenOn01(MI, SystemZ::CEBR); |
327 | 0 | break; |
328 | | |
329 | 0 | case SystemZ::WFKDB: |
330 | 0 | Changed |= shortenOn01(MI, SystemZ::KDBR); |
331 | 0 | break; |
332 | | |
333 | 0 | case SystemZ::WFKSB: |
334 | 0 | Changed |= shortenOn01(MI, SystemZ::KEBR); |
335 | 0 | break; |
336 | | |
337 | 0 | case SystemZ::VL32: |
338 | | // For z13 we prefer LDE over LE to avoid partial register dependencies. |
339 | 0 | Changed |= shortenOn0(MI, SystemZ::LDE32); |
340 | 0 | break; |
341 | | |
342 | 0 | case SystemZ::VST32: |
343 | 0 | Changed |= shortenOn0(MI, SystemZ::STE); |
344 | 0 | break; |
345 | | |
346 | 0 | case SystemZ::VL64: |
347 | 0 | Changed |= shortenOn0(MI, SystemZ::LD); |
348 | 0 | break; |
349 | | |
350 | 0 | case SystemZ::VST64: |
351 | 0 | Changed |= shortenOn0(MI, SystemZ::STD); |
352 | 0 | break; |
353 | | |
354 | 0 | default: { |
355 | 0 | int TwoOperandOpcode = SystemZ::getTwoOperandOpcode(MI.getOpcode()); |
356 | 0 | if (TwoOperandOpcode == -1) |
357 | 0 | break; |
358 | | |
359 | 0 | if ((MI.getOperand(0).getReg() != MI.getOperand(1).getReg()) && |
360 | 0 | (!MI.isCommutable() || |
361 | 0 | MI.getOperand(0).getReg() != MI.getOperand(2).getReg() || |
362 | 0 | !TII->commuteInstruction(MI, false, 1, 2))) |
363 | 0 | break; |
364 | | |
365 | 0 | MI.setDesc(TII->get(TwoOperandOpcode)); |
366 | 0 | MI.tieOperands(0, 1); |
367 | 0 | if (TwoOperandOpcode == SystemZ::SLL || |
368 | 0 | TwoOperandOpcode == SystemZ::SLA || |
369 | 0 | TwoOperandOpcode == SystemZ::SRL || |
370 | 0 | TwoOperandOpcode == SystemZ::SRA) { |
371 | | // These shifts only use the low 6 bits of the shift count. |
372 | 0 | MachineOperand &ImmMO = MI.getOperand(3); |
373 | 0 | ImmMO.setImm(ImmMO.getImm() & 0xfff); |
374 | 0 | } |
375 | 0 | Changed = true; |
376 | 0 | break; |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | 0 | LiveRegs.stepBackward(MI); |
381 | 0 | } |
382 | | |
383 | 0 | return Changed; |
384 | 0 | } |
385 | | |
386 | 0 | bool SystemZShortenInst::runOnMachineFunction(MachineFunction &F) { |
387 | 0 | if (skipFunction(F.getFunction())) |
388 | 0 | return false; |
389 | | |
390 | 0 | const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>(); |
391 | 0 | TII = ST.getInstrInfo(); |
392 | 0 | TRI = ST.getRegisterInfo(); |
393 | 0 | LiveRegs.init(*TRI); |
394 | |
|
395 | 0 | bool Changed = false; |
396 | 0 | for (auto &MBB : F) |
397 | 0 | Changed |= processBlock(MBB); |
398 | |
|
399 | 0 | return Changed; |
400 | 0 | } |