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