LLVM 24.0.0git
SystemZShortenInst.cpp
Go to the documentation of this file.
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"
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();
43 }
44};
45
46char SystemZShortenInstLegacy::ID = 0;
47} // end anonymous namespace
48
49INITIALIZE_PASS(SystemZShortenInstLegacy, DEBUG_TYPE,
50 "SystemZ Instruction Shortening", false, false)
51
54 return new SystemZShortenInstLegacy();
55}
56
57SystemZShortenInstLegacy::SystemZShortenInstLegacy()
58 : MachineFunctionPass(ID) {}
59
60// Tie operands if MI has become a two-address instruction.
62 if (MI.getDesc().getOperandConstraint(1, MCOI::TIED_TO) == 0 &&
63 !MI.getOperand(0).isTied())
64 MI.tieOperands(0, 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,
73 Register Reg = MI.getOperand(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, thisSubRegIdx, &SystemZ::GR64BitRegClass);
84 Register OtherReg = TRI->getSubReg(GR64BitReg, otherSubRegIdx);
85 if (!LiveRegs.available(OtherReg))
86 return false;
87
88 uint64_t Imm = MI.getOperand(1).getImm();
89 if (SystemZ::isImmLL(Imm)) {
90 MI.setDesc(TII->get(LLIxL));
91 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
92 return true;
93 }
94 if (SystemZ::isImmLH(Imm)) {
95 MI.setDesc(TII->get(LLIxH));
96 MI.getOperand(0).setReg(SystemZMC::getRegAsGR64(Reg));
97 MI.getOperand(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(MI.getOperand(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(MI.getOperand(0).getReg()) < 16 &&
118 SystemZMC::getFirstReg(MI.getOperand(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(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));
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,
145 if (LiveRegs.available(SystemZ::CC) && shortenOn001(MI, Opcode, TII)) {
146 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
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(MI.getOperand(0).getReg()) < 16 &&
160 SystemZMC::getFirstReg(MI.getOperand(1).getReg()) < 16) {
161 MachineOperand Dest(MI.getOperand(0));
162 MachineOperand Src(MI.getOperand(1));
163 MachineOperand Suppress(MI.getOperand(2));
164 MachineOperand Mode(MI.getOperand(3));
165 MI.removeOperand(3);
166 MI.removeOperand(2);
167 MI.removeOperand(1);
168 MI.removeOperand(0);
169 MI.setDesc(TII->get(Opcode));
170 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
171 .add(Dest)
172 .add(Mode)
173 .add(Src)
174 .add(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(0);
183 MachineOperand &LHSMO = MI.getOperand(1);
184 MachineOperand &RHSMO = MI.getOperand(2);
185 MachineOperand &AccMO = MI.getOperand(3);
186 if (SystemZMC::getFirstReg(DstMO.getReg()) < 16 &&
187 SystemZMC::getFirstReg(LHSMO.getReg()) < 16 &&
188 SystemZMC::getFirstReg(RHSMO.getReg()) < 16 &&
189 SystemZMC::getFirstReg(AccMO.getReg()) < 16 &&
190 DstMO.getReg() == AccMO.getReg()) {
191 MachineOperand Lhs(LHSMO);
192 MachineOperand Rhs(RHSMO);
193 MachineOperand Src(AccMO);
194 MI.removeOperand(3);
195 MI.removeOperand(2);
196 MI.removeOperand(1);
197 MI.setDesc(TII->get(Opcode));
198 MachineInstrBuilder(*MI.getParent()->getParent(), &MI)
199 .add(Src)
200 .add(Lhs)
201 .add(Rhs);
202 return true;
203 }
204 return false;
205}
206
207// Process all instructions in MBB. Return true if something changed.
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(MBB)) {
219 switch (MI.getOpcode()) {
220 case SystemZ::IILF:
221 Changed |=
222 shortenIIF(MI, SystemZ::LLILL, SystemZ::LLILH, TII, TRI, LiveRegs);
223 break;
224
225 case SystemZ::IIHF:
226 Changed |=
227 shortenIIF(MI, SystemZ::LLIHL, SystemZ::LLIHH, TII, TRI, LiveRegs);
228 break;
229
230 case SystemZ::WFADB:
231 Changed |= shortenOn001AddCC(MI, SystemZ::ADBR, TII, LiveRegs);
232 break;
233
234 case SystemZ::WFASB:
235 Changed |= shortenOn001AddCC(MI, SystemZ::AEBR, TII, LiveRegs);
236 break;
237
238 case SystemZ::WFDDB:
239 Changed |= shortenOn001(MI, SystemZ::DDBR, TII);
240 break;
241
242 case SystemZ::WFDSB:
243 Changed |= shortenOn001(MI, SystemZ::DEBR, TII);
244 break;
245
246 case SystemZ::WFIDB:
247 Changed |= shortenFPConv(MI, SystemZ::FIDBRA, TII);
248 break;
249
250 case SystemZ::WFISB:
251 Changed |= shortenFPConv(MI, SystemZ::FIEBRA, TII);
252 break;
253
254 case SystemZ::WLDEB:
255 Changed |= shortenOn01(MI, SystemZ::LDEBR, TII);
256 break;
257
258 case SystemZ::WLEDB:
259 Changed |= shortenFPConv(MI, SystemZ::LEDBRA, TII);
260 break;
261
262 case SystemZ::WFMDB:
263 Changed |= shortenOn001(MI, SystemZ::MDBR, TII);
264 break;
265
266 case SystemZ::WFMSB:
267 Changed |= shortenOn001(MI, SystemZ::MEEBR, TII);
268 break;
269
270 case SystemZ::WFMADB:
271 Changed |= shortenFusedFPOp(MI, SystemZ::MADBR, TII);
272 break;
273
274 case SystemZ::WFMASB:
275 Changed |= shortenFusedFPOp(MI, SystemZ::MAEBR, TII);
276 break;
277
278 case SystemZ::WFMSDB:
279 Changed |= shortenFusedFPOp(MI, SystemZ::MSDBR, TII);
280 break;
281
282 case SystemZ::WFMSSB:
283 Changed |= shortenFusedFPOp(MI, SystemZ::MSEBR, TII);
284 break;
285
286 case SystemZ::WFLCDB:
287 Changed |= shortenOn01(MI, SystemZ::LCDFR, TII);
288 break;
289
290 case SystemZ::WFLCSB:
291 Changed |= shortenOn01(MI, SystemZ::LCDFR_32, TII);
292 break;
293
294 case SystemZ::WFLNDB:
295 Changed |= shortenOn01(MI, SystemZ::LNDFR, TII);
296 break;
297
298 case SystemZ::WFLNSB:
299 Changed |= shortenOn01(MI, SystemZ::LNDFR_32, TII);
300 break;
301
302 case SystemZ::WFLPDB:
303 Changed |= shortenOn01(MI, SystemZ::LPDFR, TII);
304 break;
305
306 case SystemZ::WFLPSB:
307 Changed |= shortenOn01(MI, SystemZ::LPDFR_32, TII);
308 break;
309
310 case SystemZ::WFSQDB:
311 Changed |= shortenOn01(MI, SystemZ::SQDBR, TII);
312 break;
313
314 case SystemZ::WFSQSB:
315 Changed |= shortenOn01(MI, SystemZ::SQEBR, TII);
316 break;
317
318 case SystemZ::WFSDB:
319 Changed |= shortenOn001AddCC(MI, SystemZ::SDBR, TII, LiveRegs);
320 break;
321
322 case SystemZ::WFSSB:
323 Changed |= shortenOn001AddCC(MI, SystemZ::SEBR, TII, LiveRegs);
324 break;
325
326 case SystemZ::WFCDB:
327 Changed |= shortenOn01(MI, SystemZ::CDBR, TII);
328 break;
329
330 case SystemZ::WFCSB:
331 Changed |= shortenOn01(MI, SystemZ::CEBR, TII);
332 break;
333
334 case SystemZ::WFKDB:
335 Changed |= shortenOn01(MI, SystemZ::KDBR, TII);
336 break;
337
338 case SystemZ::WFKSB:
339 Changed |= shortenOn01(MI, 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, SystemZ::LDE32, TII);
345 break;
346
347 case SystemZ::VST32:
348 Changed |= shortenOn0(MI, SystemZ::STE, TII);
349 break;
350
351 case SystemZ::VL64:
352 Changed |= shortenOn0(MI, SystemZ::LD, TII);
353 break;
354
355 case SystemZ::VST64:
356 Changed |= shortenOn0(MI, SystemZ::STD, TII);
357 break;
358
359 default: {
360 int TwoOperandOpcode = SystemZ::getTwoOperandOpcode(MI.getOpcode());
361 if (TwoOperandOpcode == -1)
362 break;
363
364 if ((MI.getOperand(0).getReg() != MI.getOperand(1).getReg()) &&
365 (!MI.isCommutable() ||
366 MI.getOperand(0).getReg() != MI.getOperand(2).getReg() ||
367 !TII->commuteInstruction(MI, false, 1, 2)))
368 break;
369
370 MI.setDesc(TII->get(TwoOperandOpcode));
371 MI.tieOperands(0, 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(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
392 const SystemZSubtarget &ST = F.getSubtarget<SystemZSubtarget>();
393 const SystemZInstrInfo *TII = ST.getInstrInfo();
394 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
396 LiveRegs.init(*TRI);
397
398 bool Changed = false;
399 for (auto &MBB : F)
401
402 return Changed;
403}
404
405bool SystemZShortenInstLegacy::runOnMachineFunction(MachineFunction &MF) {
406 if (skipFunction(MF.getFunction()))
407 return false;
408 return shortenInstructionsInMF(MF);
409}
410
411PreservedAnalyses
uint64_t uint64_t
uint64_t Imm
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
A set of register units.
#define F(x, y, z)
Definition MD5.cpp:54
Register Reg
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static bool shortenFPConv(MachineInstr &MI, unsigned Opcode, const SystemZInstrInfo *TII)
static bool processBlock(MachineBasicBlock &MBB, LiveRegUnits &LiveRegs, const SystemZInstrInfo *TII, const TargetRegisterInfo *TRI)
static bool shortenOn01(MachineInstr &MI, unsigned Opcode, const SystemZInstrInfo *TII)
static bool shortenIIF(MachineInstr &MI, unsigned LLIxL, unsigned LLIxH, const SystemZInstrInfo *TII, const TargetRegisterInfo *TRI, LiveRegUnits &LiveRegs)
static void tieOpsIfNeeded(MachineInstr &MI)
static bool shortenFusedFPOp(MachineInstr &MI, unsigned Opcode, const SystemZInstrInfo *TII)
static bool shortenOn001(MachineInstr &MI, unsigned Opcode, const SystemZInstrInfo *TII)
static bool shortenOn0(MachineInstr &MI, unsigned Opcode, const SystemZInstrInfo *TII)
static bool shortenOn001AddCC(MachineInstr &MI, unsigned Opcode, const SystemZInstrInfo *TII, LiveRegUnits &LiveRegs)
static bool shortenInstructionsInMF(MachineFunction &F)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
A set of register units used to track register liveness.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
int64_t getImm() const
Register getReg() const
getReg - Returns the register number.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:152
Wrapper class representing virtual and physical registers.
Definition Register.h:20
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Pass manager infrastructure for declaring and invalidating analyses.
unsigned getFirstReg(unsigned Reg)
unsigned getRegAsGR64(unsigned Reg)
static bool isImmLL(uint64_t Val)
Definition SystemZ.h:167
static bool isImmLH(uint64_t Val)
Definition SystemZ.h:172
int32_t getTwoOperandOpcode(uint32_t Opcode)
This is an optimization pass for GlobalISel generic memory operations.
@ Dead
Unused definition.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
FunctionPass * createSystemZShortenInstLegacyPass(SystemZTargetMachine &TM)