LLVM 24.0.0git
SIShrinkInstructions.cpp
Go to the documentation of this file.
1//===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===//
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/// The pass tries to use the 32-bit encoding for instructions when possible.
8//===----------------------------------------------------------------------===//
9//
10
12#include "AMDGPU.h"
13#include "GCNSubtarget.h"
15#include "llvm/ADT/Statistic.h"
17
18#define DEBUG_TYPE "si-shrink-instructions"
19
20STATISTIC(NumInstructionsShrunk,
21 "Number of 64-bit instruction reduced to 32-bit.");
22STATISTIC(NumLiteralConstantsFolded,
23 "Number of literal constants folded into 32-bit instructions.");
24
25using namespace llvm;
26
27namespace {
28
29enum ChangeKind { None, UpdateHint, UpdateInst };
30
31class SIShrinkInstructions {
32 MachineRegisterInfo *MRI;
33 const GCNSubtarget *ST;
34 const SIInstrInfo *TII;
35 const SIRegisterInfo *TRI;
36 bool IsPostRA;
37
38 bool foldImmediates(MachineInstr &MI, bool TryToCommute = true) const;
39 bool shouldShrinkTrue16(MachineInstr &MI) const;
40 bool isKImmOperand(const MachineInstr &MI, const MachineOperand &Src) const;
41 bool isKUImmOperand(const MachineInstr &MI, const MachineOperand &Src) const;
42 bool isKImmOrKUImmOperand(const MachineOperand &Src, bool &IsUnsigned) const;
43 void copyExtraImplicitOps(MachineInstr &NewMI, MachineInstr &MI) const;
44 bool shrinkScalarCompare(MachineInstr &MI) const;
45 bool shrinkMIMG(MachineInstr &MI) const;
46 bool shrinkMadFma(MachineInstr &MI) const;
47 ChangeKind shrinkScalarLogicOp(MachineInstr &MI) const;
48 bool tryReplaceDeadSDST(MachineInstr &MI) const;
50 unsigned SubReg) const;
51 bool instReadsReg(const MachineInstr *MI, unsigned Reg,
52 unsigned SubReg) const;
53 bool instModifiesReg(const MachineInstr *MI, unsigned Reg,
54 unsigned SubReg) const;
55 TargetInstrInfo::RegSubRegPair getSubRegForIndex(Register Reg, unsigned Sub,
56 unsigned I) const;
57 void dropInstructionKeepingImpDefs(MachineInstr &MI) const;
58 MachineInstr *matchSwap(MachineInstr &MovT) const;
59
60public:
61 SIShrinkInstructions() = default;
62 bool run(MachineFunction &MF);
63};
64
65class SIShrinkInstructionsLegacy : public MachineFunctionPass {
66
67public:
68 static char ID;
69
70 SIShrinkInstructionsLegacy() : MachineFunctionPass(ID) {}
71
72 bool runOnMachineFunction(MachineFunction &MF) override;
73
74 StringRef getPassName() const override { return "SI Shrink Instructions"; }
75
76 void getAnalysisUsage(AnalysisUsage &AU) const override {
77 AU.setPreservesCFG();
79 }
80};
81
82} // End anonymous namespace.
83
84INITIALIZE_PASS(SIShrinkInstructionsLegacy, DEBUG_TYPE,
85 "SI Shrink Instructions", false, false)
86
87char SIShrinkInstructionsLegacy::ID = 0;
88
90 return new SIShrinkInstructionsLegacy();
91}
92
93/// This function checks \p MI for operands defined by a move immediate
94/// instruction and then folds the literal constant into the instruction if it
95/// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instructions.
96bool SIShrinkInstructions::foldImmediates(MachineInstr &MI,
97 bool TryToCommute) const {
98 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
99
100 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
101
102 // Try to fold Src0
103 MachineOperand &Src0 = MI.getOperand(Src0Idx);
104 if (Src0.isReg()) {
105 Register Reg = Src0.getReg();
106 if (Reg.isVirtual()) {
107 MachineInstr *Def = MRI->getUniqueVRegDef(Reg);
108 if (Def && Def->isMoveImmediate()) {
109 const MachineOperand &MovSrc = Def->getOperand(1);
110 bool ConstantFolded = false;
111
112 if (MovSrc.isImm()) {
113 if (std::optional<int64_t> Imm = SIInstrInfo::extractSubregFromImm(
114 MovSrc.getImm(), Src0.getSubReg())) {
115 MachineOperand ImmOp = MachineOperand::CreateImm(*Imm);
116 if (TII->isOperandLegal(MI, Src0Idx, &ImmOp)) {
117 Src0.ChangeToImmediate(*Imm);
118 ConstantFolded = true;
119 }
120 }
121 } else if (MovSrc.isGlobal()) {
122 if (std::optional<unsigned> Flags =
124 MovSrc.getTargetFlags(), Src0.getSubReg())) {
125 MachineOperand GAOp = MachineOperand::CreateGA(
126 MovSrc.getGlobal(), MovSrc.getOffset(), *Flags);
127 if (TII->isOperandLegal(MI, Src0Idx, &GAOp)) {
128 Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(), *Flags);
129 ConstantFolded = true;
130 }
131 }
132 } else if (MovSrc.isFI() && !Src0.getSubReg() &&
133 TII->isOperandLegal(MI, Src0Idx, &MovSrc)) {
134 Src0.ChangeToFrameIndex(MovSrc.getIndex());
135 ConstantFolded = true;
136 }
137
138 if (ConstantFolded) {
139 if (MRI->use_nodbg_empty(Reg))
140 Def->eraseFromParent();
141 ++NumLiteralConstantsFolded;
142 return true;
143 }
144 }
145 }
146 }
147
148 // We have failed to fold src0, so commute the instruction and try again.
149 if (TryToCommute && MI.isCommutable()) {
150 if (TII->commuteInstruction(MI)) {
151 if (foldImmediates(MI, false))
152 return true;
153
154 // Commute back.
155 TII->commuteInstruction(MI);
156 }
157 }
158
159 return false;
160}
161
162/// Do not shrink the instruction if its registers are not expressible in the
163/// shrunk encoding.
164bool SIShrinkInstructions::shouldShrinkTrue16(MachineInstr &MI) const {
165 for (unsigned I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
166 const MachineOperand &MO = MI.getOperand(I);
167 if (MO.isReg()) {
168 Register Reg = MO.getReg();
169 assert(!Reg.isVirtual() && "Prior checks should ensure we only shrink "
170 "True16 Instructions post-RA");
171 if (AMDGPU::VGPR_32RegClass.contains(Reg) &&
172 !AMDGPU::VGPR_32_Lo128RegClass.contains(Reg))
173 return false;
174
175 if (AMDGPU::VGPR_16RegClass.contains(Reg) &&
176 !AMDGPU::VGPR_16_Lo128RegClass.contains(Reg))
177 return false;
178 }
179 }
180 return true;
181}
182
183bool SIShrinkInstructions::isKImmOperand(const MachineInstr &MI,
184 const MachineOperand &Src) const {
185 return isInt<16>(SignExtend64(Src.getImm(), 32)) &&
186 !TII->isInlineConstant(MI, MI.getOperandNo(&Src));
187}
188
189bool SIShrinkInstructions::isKUImmOperand(const MachineInstr &MI,
190 const MachineOperand &Src) const {
191 return isUInt<16>(Src.getImm()) &&
192 !TII->isInlineConstant(MI, MI.getOperandNo(&Src));
193}
194
195bool SIShrinkInstructions::isKImmOrKUImmOperand(const MachineOperand &Src,
196 bool &IsUnsigned) const {
197 if (isInt<16>(SignExtend64(Src.getImm(), 32))) {
198 IsUnsigned = false;
199 return !TII->isInlineConstant(Src);
200 }
201
202 if (isUInt<16>(Src.getImm())) {
203 IsUnsigned = true;
204 return !TII->isInlineConstant(Src);
205 }
206
207 return false;
208}
209
210/// \returns the opcode of an instruction a move immediate of the constant \p
211/// Src can be replaced with if the constant is replaced with \p ModifiedImm.
212/// i.e.
213///
214/// If the bitreverse of a constant is an inline immediate, reverse the
215/// immediate and return the bitreverse opcode.
216///
217/// If the bitwise negation of a constant is an inline immediate, reverse the
218/// immediate and return the bitwise not opcode.
220 const MachineOperand &Src,
221 int32_t &ModifiedImm, bool Scalar) {
222 if (TII->isInlineConstant(Src))
223 return 0;
224 int32_t SrcImm = static_cast<int32_t>(Src.getImm());
225
226 if (!Scalar) {
227 // We could handle the scalar case with here, but we would need to check
228 // that SCC is not live as S_NOT_B32 clobbers it. It's probably not worth
229 // it, as the reasonable values are already covered by s_movk_i32.
230 ModifiedImm = ~SrcImm;
231 if (TII->isInlineConstant(APInt(32, ModifiedImm, true)))
232 return AMDGPU::V_NOT_B32_e32;
233 }
234
235 ModifiedImm = reverseBits<int32_t>(SrcImm);
236 if (TII->isInlineConstant(APInt(32, ModifiedImm, true)))
237 return Scalar ? AMDGPU::S_BREV_B32 : AMDGPU::V_BFREV_B32_e32;
238
239 return 0;
240}
241
242/// Copy implicit register operands from specified instruction to this
243/// instruction that are not part of the instruction definition.
244void SIShrinkInstructions::copyExtraImplicitOps(MachineInstr &NewMI,
245 MachineInstr &MI) const {
246 MachineFunction &MF = *MI.getMF();
247 for (unsigned i = MI.getDesc().getNumOperands() +
248 MI.getDesc().implicit_uses().size() +
249 MI.getDesc().implicit_defs().size(),
250 e = MI.getNumOperands();
251 i != e; ++i) {
252 const MachineOperand &MO = MI.getOperand(i);
253 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
254 NewMI.addOperand(MF, MO);
255 }
256}
257
258bool SIShrinkInstructions::shrinkScalarCompare(MachineInstr &MI) const {
259 if (!ST->hasSCmpK())
260 return false;
261
262 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
263 // get constants on the RHS.
264 bool Changed = false;
265 if (!MI.getOperand(0).isReg()) {
266 if (TII->commuteInstruction(MI, false, 0, 1))
267 Changed = true;
268 }
269
270 // cmpk requires src0 to be a register
271 const MachineOperand &Src0 = MI.getOperand(0);
272 if (!Src0.isReg())
273 return Changed;
274
275 MachineOperand &Src1 = MI.getOperand(1);
276 if (!Src1.isImm())
277 return Changed;
278
279 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
280 if (SOPKOpc == -1)
281 return Changed;
282
283 // eq/ne is special because the imm16 can be treated as signed or unsigned,
284 // and initially selected to the unsigned versions.
285 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
286 bool HasUImm;
287 if (isKImmOrKUImmOperand(Src1, HasUImm)) {
288 if (!HasUImm) {
289 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
290 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
291 Src1.setImm(SignExtend32(Src1.getImm(), 32));
292 }
293
294 MI.setDesc(TII->get(SOPKOpc));
295 Changed = true;
296 }
297
298 return Changed;
299 }
300
301 const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
302
303 if ((SIInstrInfo::sopkIsZext(SOPKOpc) && isKUImmOperand(MI, Src1)) ||
304 (!SIInstrInfo::sopkIsZext(SOPKOpc) && isKImmOperand(MI, Src1))) {
305 if (!SIInstrInfo::sopkIsZext(SOPKOpc))
306 Src1.setImm(SignExtend64(Src1.getImm(), 32));
307 MI.setDesc(NewDesc);
308 Changed = true;
309 }
310 return Changed;
311}
312
313// Shrink NSA encoded instructions with contiguous VGPRs to non-NSA encoding.
314bool SIShrinkInstructions::shrinkMIMG(MachineInstr &MI) const {
315 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
316 if (!Info)
317 return false;
318
319 uint8_t NewEncoding;
320 switch (Info->MIMGEncoding) {
321 case AMDGPU::MIMGEncGfx10NSA:
322 NewEncoding = AMDGPU::MIMGEncGfx10Default;
323 break;
324 case AMDGPU::MIMGEncGfx11NSA:
325 NewEncoding = AMDGPU::MIMGEncGfx11Default;
326 break;
327 default:
328 return false;
329 }
330
331 int VAddr0Idx =
332 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
333 unsigned NewAddrDwords = Info->VAddrDwords;
334 const TargetRegisterClass *RC;
335
336 if (Info->VAddrDwords == 2) {
337 RC = &AMDGPU::VReg_64RegClass;
338 } else if (Info->VAddrDwords == 3) {
339 RC = &AMDGPU::VReg_96RegClass;
340 } else if (Info->VAddrDwords == 4) {
341 RC = &AMDGPU::VReg_128RegClass;
342 } else if (Info->VAddrDwords == 5) {
343 RC = &AMDGPU::VReg_160RegClass;
344 } else if (Info->VAddrDwords == 6) {
345 RC = &AMDGPU::VReg_192RegClass;
346 } else if (Info->VAddrDwords == 7) {
347 RC = &AMDGPU::VReg_224RegClass;
348 } else if (Info->VAddrDwords == 8) {
349 RC = &AMDGPU::VReg_256RegClass;
350 } else if (Info->VAddrDwords == 9) {
351 RC = &AMDGPU::VReg_288RegClass;
352 } else if (Info->VAddrDwords == 10) {
353 RC = &AMDGPU::VReg_320RegClass;
354 } else if (Info->VAddrDwords == 11) {
355 RC = &AMDGPU::VReg_352RegClass;
356 } else if (Info->VAddrDwords == 12) {
357 RC = &AMDGPU::VReg_384RegClass;
358 } else {
359 RC = &AMDGPU::VReg_512RegClass;
360 NewAddrDwords = 16;
361 }
362
363 unsigned VgprBase = 0;
364 unsigned NextVgpr = 0;
365 bool IsUndef = true;
366 bool IsKill = NewAddrDwords == Info->VAddrDwords;
367 const unsigned NSAMaxSize = ST->getNSAMaxSize();
368 const bool IsPartialNSA = NewAddrDwords > NSAMaxSize;
369 const unsigned EndVAddr = IsPartialNSA ? NSAMaxSize : Info->VAddrOperands;
370 for (unsigned Idx = 0; Idx < EndVAddr; ++Idx) {
371 const MachineOperand &Op = MI.getOperand(VAddr0Idx + Idx);
372 unsigned Vgpr = TRI->getHWRegIndex(Op.getReg());
373 unsigned Dwords = TRI->getRegSizeInBits(Op.getReg(), *MRI) / 32;
374 assert(Dwords > 0 && "Un-implemented for less than 32 bit regs");
375
376 if (Idx == 0) {
377 VgprBase = Vgpr;
378 NextVgpr = Vgpr + Dwords;
379 } else if (Vgpr == NextVgpr) {
380 NextVgpr = Vgpr + Dwords;
381 } else {
382 return false;
383 }
384
385 if (!Op.isUndef())
386 IsUndef = false;
387 if (!Op.isKill())
388 IsKill = false;
389 }
390
391 if (VgprBase + NewAddrDwords > 256)
392 return false;
393
394 // Further check for implicit tied operands - this may be present if TFE is
395 // enabled
396 int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe);
397 int LWEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::lwe);
398 unsigned TFEVal = (TFEIdx == -1) ? 0 : MI.getOperand(TFEIdx).getImm();
399 unsigned LWEVal = (LWEIdx == -1) ? 0 : MI.getOperand(LWEIdx).getImm();
400 int ToUntie = -1;
401 if (TFEVal || LWEVal) {
402 // TFE/LWE is enabled so we need to deal with an implicit tied operand
403 for (unsigned i = LWEIdx + 1, e = MI.getNumOperands(); i != e; ++i) {
404 if (MI.getOperand(i).isReg() && MI.getOperand(i).isTied() &&
405 MI.getOperand(i).isImplicit()) {
406 // This is the tied operand
407 assert(
408 ToUntie == -1 &&
409 "found more than one tied implicit operand when expecting only 1");
410 ToUntie = i;
411 MI.untieRegOperand(ToUntie);
412 }
413 }
414 }
415
416 unsigned NewOpcode = AMDGPU::getMIMGOpcode(Info->BaseOpcode, NewEncoding,
417 Info->VDataDwords, NewAddrDwords);
418 MI.setDesc(TII->get(NewOpcode));
419 MI.getOperand(VAddr0Idx).setReg(RC->getRegister(VgprBase));
420 MI.getOperand(VAddr0Idx).setIsUndef(IsUndef);
421 MI.getOperand(VAddr0Idx).setIsKill(IsKill);
422
423 for (unsigned i = 1; i < EndVAddr; ++i)
424 MI.removeOperand(VAddr0Idx + 1);
425
426 if (ToUntie >= 0) {
427 MI.tieOperands(
428 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata),
429 ToUntie - (EndVAddr - 1));
430 }
431 return true;
432}
433
434// Shrink MAD to MADAK/MADMK and FMA to FMAAK/FMAMK.
435bool SIShrinkInstructions::shrinkMadFma(MachineInstr &MI) const {
436 // Pre-GFX10 VOP3 instructions like MAD/FMA cannot take a literal operand so
437 // there is no reason to try to shrink them.
438 if (!ST->hasVOP3Literal())
439 return false;
440
441 // There is no advantage to doing this pre-RA.
442 if (!IsPostRA)
443 return false;
444
445 if (TII->hasAnyModifiersSet(MI))
446 return false;
447
448 const unsigned Opcode = MI.getOpcode();
449 MachineOperand &Src0 = *TII->getNamedOperand(MI, AMDGPU::OpName::src0);
450 MachineOperand &Src1 = *TII->getNamedOperand(MI, AMDGPU::OpName::src1);
451 MachineOperand &Src2 = *TII->getNamedOperand(MI, AMDGPU::OpName::src2);
452 unsigned NewOpcode = AMDGPU::INSTRUCTION_LIST_END;
453
454 bool Swap;
455
456 // Detect "Dst = VSrc * VGPR + Imm" and convert to AK form.
457 if (Src2.isImm() && !TII->isInlineConstant(Src2)) {
458 if (Src1.isReg() && TRI->isVGPR(*MRI, Src1.getReg()))
459 Swap = false;
460 else if (Src0.isReg() && TRI->isVGPR(*MRI, Src0.getReg()))
461 Swap = true;
462 else
463 return false;
464
465 switch (Opcode) {
466 default:
467 llvm_unreachable("Unexpected mad/fma opcode!");
468 case AMDGPU::V_MAD_F32_e64:
469 NewOpcode = AMDGPU::V_MADAK_F32;
470 break;
471 case AMDGPU::V_FMA_F32_e64:
472 NewOpcode = AMDGPU::V_FMAAK_F32;
473 break;
474 case AMDGPU::V_MAD_F16_e64:
475 NewOpcode = AMDGPU::V_MADAK_F16;
476 break;
477 case AMDGPU::V_FMA_F16_e64:
478 case AMDGPU::V_FMA_F16_gfx9_e64:
479 NewOpcode = AMDGPU::V_FMAAK_F16;
480 break;
481 case AMDGPU::V_FMA_F16_gfx9_t16_e64:
482 NewOpcode = AMDGPU::V_FMAAK_F16_t16;
483 break;
484 case AMDGPU::V_FMA_F16_gfx9_fake16_e64:
485 NewOpcode = AMDGPU::V_FMAAK_F16_fake16;
486 break;
487 case AMDGPU::V_FMA_F64_e64:
488 if (ST->hasFmaakFmamkF64Insts())
489 NewOpcode = AMDGPU::V_FMAAK_F64;
490 break;
491 }
492 }
493
494 // Detect "Dst = VSrc * Imm + VGPR" and convert to MK form.
495 if (Src2.isReg() && TRI->isVGPR(*MRI, Src2.getReg())) {
496 if (Src1.isImm() && !TII->isInlineConstant(Src1))
497 Swap = false;
498 else if (Src0.isImm() && !TII->isInlineConstant(Src0))
499 Swap = true;
500 else
501 return false;
502
503 switch (Opcode) {
504 default:
505 llvm_unreachable("Unexpected mad/fma opcode!");
506 case AMDGPU::V_MAD_F32_e64:
507 NewOpcode = AMDGPU::V_MADMK_F32;
508 break;
509 case AMDGPU::V_FMA_F32_e64:
510 NewOpcode = AMDGPU::V_FMAMK_F32;
511 break;
512 case AMDGPU::V_MAD_F16_e64:
513 NewOpcode = AMDGPU::V_MADMK_F16;
514 break;
515 case AMDGPU::V_FMA_F16_e64:
516 case AMDGPU::V_FMA_F16_gfx9_e64:
517 NewOpcode = AMDGPU::V_FMAMK_F16;
518 break;
519 case AMDGPU::V_FMA_F16_gfx9_t16_e64:
520 NewOpcode = AMDGPU::V_FMAMK_F16_t16;
521 break;
522 case AMDGPU::V_FMA_F16_gfx9_fake16_e64:
523 NewOpcode = AMDGPU::V_FMAMK_F16_fake16;
524 break;
525 case AMDGPU::V_FMA_F64_e64:
526 if (ST->hasFmaakFmamkF64Insts())
527 NewOpcode = AMDGPU::V_FMAMK_F64;
528 break;
529 }
530 }
531
532 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END)
533 return false;
534
535 if (AMDGPU::isTrue16Inst(NewOpcode) && !shouldShrinkTrue16(MI))
536 return false;
537
538 if (Swap) {
539 // Swap Src0 and Src1 by building a new instruction.
540 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(NewOpcode),
541 MI.getOperand(0).getReg())
542 .add(Src1)
543 .add(Src0)
544 .add(Src2)
545 .setMIFlags(MI.getFlags());
546 MI.eraseFromParent();
547 } else {
548 TII->removeModOperands(MI);
549 MI.setDesc(TII->get(NewOpcode));
550 }
551 return true;
552}
553
554/// Attempt to shrink AND/OR/XOR operations requiring non-inlineable literals.
555/// For AND or OR, try using S_BITSET{0,1} to clear or set bits.
556/// If the inverse of the immediate is legal, use ANDN2, ORN2 or
557/// XNOR (as a ^ b == ~(a ^ ~b)).
558/// \return ChangeKind::None if no changes were made.
559/// ChangeKind::UpdateHint if regalloc hints were updated.
560/// ChangeKind::UpdateInst if the instruction was modified.
561ChangeKind SIShrinkInstructions::shrinkScalarLogicOp(MachineInstr &MI) const {
562 unsigned Opc = MI.getOpcode();
563 const MachineOperand *Dest = &MI.getOperand(0);
564 MachineOperand *Src0 = &MI.getOperand(1);
565 MachineOperand *Src1 = &MI.getOperand(2);
566 MachineOperand *SrcReg = Src0;
567 MachineOperand *SrcImm = Src1;
568
569 if (!SrcImm->isImm() ||
570 AMDGPU::isInlinableLiteral32(SrcImm->getImm(), ST->hasInv2PiInlineImm()))
571 return ChangeKind::None;
572
573 uint32_t Imm = static_cast<uint32_t>(SrcImm->getImm());
574 uint32_t NewImm = 0;
575
576 if (Opc == AMDGPU::S_AND_B32) {
577 if (isPowerOf2_32(~Imm) &&
578 MI.findRegisterDefOperand(AMDGPU::SCC, /*TRI=*/nullptr)->isDead()) {
579 NewImm = llvm::countr_one(Imm);
580 Opc = AMDGPU::S_BITSET0_B32;
581 } else if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
582 NewImm = ~Imm;
583 Opc = AMDGPU::S_ANDN2_B32;
584 }
585 } else if (Opc == AMDGPU::S_OR_B32) {
586 if (isPowerOf2_32(Imm) &&
587 MI.findRegisterDefOperand(AMDGPU::SCC, /*TRI=*/nullptr)->isDead()) {
588 NewImm = llvm::countr_zero(Imm);
589 Opc = AMDGPU::S_BITSET1_B32;
590 } else if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
591 NewImm = ~Imm;
592 Opc = AMDGPU::S_ORN2_B32;
593 }
594 } else if (Opc == AMDGPU::S_XOR_B32) {
595 if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
596 NewImm = ~Imm;
597 Opc = AMDGPU::S_XNOR_B32;
598 }
599 } else {
600 llvm_unreachable("unexpected opcode");
601 }
602
603 if (NewImm != 0) {
604 if (Dest->getReg().isVirtual() && SrcReg->isReg()) {
605 MRI->setRegAllocationHint(Dest->getReg(), 0, SrcReg->getReg());
606 MRI->setRegAllocationHint(SrcReg->getReg(), 0, Dest->getReg());
607 return ChangeKind::UpdateHint;
608 }
609
610 if (SrcReg->isReg() && SrcReg->getReg() == Dest->getReg()) {
611 const bool IsUndef = SrcReg->isUndef();
612 const bool IsKill = SrcReg->isKill();
613 TII->mutateAndCleanupImplicit(MI, TII->get(Opc));
614 if (Opc == AMDGPU::S_BITSET0_B32 ||
615 Opc == AMDGPU::S_BITSET1_B32) {
616 Src0->ChangeToImmediate(NewImm);
617 // Remove the immediate and add the tied input.
618 MI.getOperand(2).ChangeToRegister(Dest->getReg(), /*IsDef*/ false,
619 /*isImp*/ false, IsKill,
620 /*isDead*/ false, IsUndef);
621 MI.tieOperands(0, 2);
622 } else {
623 SrcImm->setImm(NewImm);
624 }
625 return ChangeKind::UpdateInst;
626 }
627 }
628
629 return ChangeKind::None;
630}
631
632// This is the same as MachineInstr::readsRegister/modifiesRegister except
633// it takes subregs into account.
634bool SIShrinkInstructions::instAccessReg(
636 unsigned SubReg) const {
637 for (const MachineOperand &MO : R) {
638 if (Reg.isPhysical() && MO.getReg().isPhysical()) {
639 if (TRI->regsOverlap(Reg, MO.getReg()))
640 return true;
641 } else if (MO.getReg() == Reg && Reg.isVirtual()) {
642 LaneBitmask Overlap = TRI->getSubRegIndexLaneMask(SubReg) &
643 TRI->getSubRegIndexLaneMask(MO.getSubReg());
644 if (Overlap.any())
645 return true;
646 }
647 }
648 return false;
649}
650
651bool SIShrinkInstructions::instReadsReg(const MachineInstr *MI, unsigned Reg,
652 unsigned SubReg) const {
653 return instAccessReg(MI->all_uses(), Reg, SubReg);
654}
655
656bool SIShrinkInstructions::instModifiesReg(const MachineInstr *MI, unsigned Reg,
657 unsigned SubReg) const {
658 return instAccessReg(MI->all_defs(), Reg, SubReg);
659}
660
661TargetInstrInfo::RegSubRegPair
662SIShrinkInstructions::getSubRegForIndex(Register Reg, unsigned Sub,
663 unsigned I) const {
664 if (TRI->getRegSizeInBits(Reg, *MRI) != 32) {
665 if (Reg.isPhysical()) {
666 Reg = TRI->getSubReg(Reg, TRI->getSubRegFromChannel(I));
667 } else {
668 Sub = TRI->getSubRegFromChannel(I + TRI->getChannelFromSubReg(Sub));
669 }
670 }
671 return TargetInstrInfo::RegSubRegPair(Reg, Sub);
672}
673
674void SIShrinkInstructions::dropInstructionKeepingImpDefs(
675 MachineInstr &MI) const {
676 for (unsigned i = MI.getDesc().getNumOperands() +
677 MI.getDesc().implicit_uses().size() +
678 MI.getDesc().implicit_defs().size(),
679 e = MI.getNumOperands();
680 i != e; ++i) {
681 const MachineOperand &Op = MI.getOperand(i);
682 if (!Op.isDef())
683 continue;
684 BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
685 TII->get(AMDGPU::IMPLICIT_DEF), Op.getReg());
686 }
687
688 MI.eraseFromParent();
689}
690
691// Match:
692// mov t, x
693// mov x, y
694// mov y, t
695//
696// =>
697//
698// mov t, x (t is potentially dead and move eliminated)
699// v_swap_b32 x, y
700//
701// Returns next valid instruction pointer if was able to create v_swap_b32.
702//
703// This shall not be done too early not to prevent possible folding which may
704// remove matched moves, and this should preferably be done before RA to
705// release saved registers and also possibly after RA which can insert copies
706// too.
707//
708// This is really just a generic peephole that is not a canonical shrinking,
709// although requirements match the pass placement and it reduces code size too.
710MachineInstr *SIShrinkInstructions::matchSwap(MachineInstr &MovT) const {
711 assert(MovT.getOpcode() == AMDGPU::V_MOV_B32_e32 ||
712 MovT.getOpcode() == AMDGPU::V_MOV_B16_t16_e32 ||
713 MovT.getOpcode() == AMDGPU::COPY);
714
715 Register T = MovT.getOperand(0).getReg();
716 unsigned Tsub = MovT.getOperand(0).getSubReg();
717 MachineOperand &Xop = MovT.getOperand(1);
718
719 if (!Xop.isReg())
720 return nullptr;
721 Register X = Xop.getReg();
722 unsigned Xsub = Xop.getSubReg();
723 Register Y;
724 unsigned Ysub;
725
726 unsigned Size = TII->getOpSize(MovT, 0);
727
728 // We can't match v_swap_b16 pre-RA, because VGPR_16_Lo128 registers
729 // are not allocatble.
730 if (Size == 2 && X.isVirtual())
731 return nullptr;
732
733 if (!TRI->isVGPR(*MRI, X))
734 return nullptr;
735
736 const unsigned SearchLimit = 16;
737 unsigned Count = 0;
738
739 MachineInstr *MovX = nullptr;
740 MachineInstr *InsertionPt = nullptr;
741 MachineInstr *MovY = nullptr;
742
743 for (auto Iter = std::next(MovT.getIterator()),
744 E = MovT.getParent()->instr_end();
745 Iter != E && Count < SearchLimit; ++Iter) {
746 if (Iter->isDebugInstr())
747 continue;
748 ++Count;
749
750 if (!MovX) {
751 // Search for mov x, y.
752 if ((Iter->getOpcode() == AMDGPU::V_MOV_B32_e32 ||
753 Iter->getOpcode() == AMDGPU::V_MOV_B16_t16_e32 ||
754 Iter->getOpcode() == AMDGPU::COPY) &&
755 Iter->getOperand(0).getReg() == X &&
756 Iter->getOperand(0).getSubReg() == Xsub &&
757 Iter->getOperand(1).isReg()) {
758 MovX = &*Iter;
759 Y = MovX->getOperand(1).getReg();
760 Ysub = MovX->getOperand(1).getSubReg();
761 } else if (instModifiesReg(&*Iter, X, Xsub)) {
762 // Writes to x are not allowed until mov x, y has been found
763 return nullptr;
764 }
765 } else {
766 // mov x, y has been found.
767 // Search for mov y, t.
768 if ((Iter->getOpcode() == AMDGPU::V_MOV_B32_e32 ||
769 Iter->getOpcode() == AMDGPU::V_MOV_B16_t16_e32 ||
770 Iter->getOpcode() == AMDGPU::COPY) &&
771 Iter->getOperand(0).getReg() == Y &&
772 Iter->getOperand(0).getSubReg() == Ysub &&
773 Iter->getOperand(1).isReg() && Iter->getOperand(1).getReg() == T &&
774 Iter->getOperand(1).getSubReg() == Tsub) {
775 MovY = &*Iter;
776 break;
777 }
778
779 // Effectively, mov x, y must be moved downward
780 // and mov y, t must be moved upward so that they can be fused into a
781 // swap. A write to y creates a barrier that prevents the two moves from
782 // being moved adjacent to each other.
783 if (instModifiesReg(&*Iter, Y, Ysub))
784 return nullptr;
785
786 // Reads or writes to x prevent mov x, y from being moved farther
787 // downward. Select this to be the insertion point.
788 if (!InsertionPt &&
789 (instReadsReg(&*Iter, X, Xsub) || instModifiesReg(&*Iter, X, Xsub))) {
790 InsertionPt = &*Iter;
791 }
792 // If the insertion point has been found, then mov y, t must be moved
793 // upward past all subsequent instructions. A read of y will block this
794 // movement.
795 if (InsertionPt) {
796 if (instReadsReg(&*Iter, Y, Ysub))
797 return nullptr;
798 }
799 }
800
801 if (instModifiesReg(&*Iter, T, Tsub))
802 return nullptr;
803 }
804 if (MovY) {
805 LLVM_DEBUG(dbgs() << "Matched v_swap:\n" << MovT << *MovX << *MovY);
806
807 MachineBasicBlock &MBB = *MovT.getParent();
808 SmallVector<MachineInstr *, 4> Swaps;
809
810 if (!InsertionPt)
811 InsertionPt = MovY;
812 if (Size == 2) {
813 auto *MIB = BuildMI(MBB, InsertionPt->getIterator(), MovT.getDebugLoc(),
814 TII->get(AMDGPU::V_SWAP_B16))
815 .addDef(X)
816 .addDef(Y)
817 .addReg(Y)
818 .addReg(X)
819 .getInstr();
820 Swaps.push_back(MIB);
821 } else {
822 assert(Size > 0 && Size % 4 == 0);
823 for (unsigned I = 0; I < Size / 4; ++I) {
824 TargetInstrInfo::RegSubRegPair X1, Y1;
825 X1 = getSubRegForIndex(X, Xsub, I);
826 Y1 = getSubRegForIndex(Y, Ysub, I);
827 auto *MIB = BuildMI(MBB, InsertionPt->getIterator(), MovT.getDebugLoc(),
828 TII->get(AMDGPU::V_SWAP_B32))
829 .addDef(X1.Reg, {}, X1.SubReg)
830 .addDef(Y1.Reg, {}, Y1.SubReg)
831 .addReg(Y1.Reg, {}, Y1.SubReg)
832 .addReg(X1.Reg, {}, X1.SubReg)
833 .getInstr();
834 Swaps.push_back(MIB);
835 }
836 }
837 // Drop implicit EXEC.
838 if (MovX->hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
839 for (MachineInstr *Swap : Swaps) {
840 Swap->removeOperand(Swap->getNumExplicitOperands());
841 Swap->copyImplicitOps(*MBB.getParent(), *MovX);
842 }
843 }
844 MovX->eraseFromParent();
845 dropInstructionKeepingImpDefs(*MovY);
846 MachineInstr *Next = &*std::next(MovT.getIterator());
847
848 if (T.isVirtual() && MRI->use_nodbg_empty(T)) {
849 dropInstructionKeepingImpDefs(MovT);
850 } else {
851 Xop.setIsKill(false);
852 for (int I = MovT.getNumImplicitOperands() - 1; I >= 0; --I ) {
853 unsigned OpNo = MovT.getNumExplicitOperands() + I;
854 const MachineOperand &Op = MovT.getOperand(OpNo);
855 if (Op.isKill() && TRI->regsOverlap(X, Op.getReg()))
856 MovT.removeOperand(OpNo);
857 }
858 }
859
860 return Next;
861 }
862 return nullptr;
863}
864
865// If an instruction has dead sdst replace it with NULL register on gfx1030+
866bool SIShrinkInstructions::tryReplaceDeadSDST(MachineInstr &MI) const {
867 if (!ST->hasGFX10_3Insts())
868 return false;
869
870 MachineOperand *Op = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
871 if (!Op)
872 return false;
873 Register SDstReg = Op->getReg();
874 if (SDstReg.isPhysical() || !MRI->use_nodbg_empty(SDstReg))
875 return false;
876
877 Op->setReg(ST->isWave32() ? AMDGPU::SGPR_NULL : AMDGPU::SGPR_NULL64);
878 return true;
879}
880
881bool SIShrinkInstructions::run(MachineFunction &MF) {
882 MRI = &MF.getRegInfo();
883 ST = &MF.getSubtarget<GCNSubtarget>();
884 TII = ST->getInstrInfo();
885 TRI = &TII->getRegisterInfo();
886 IsPostRA = MF.getProperties().hasNoVRegs();
887
888 unsigned VCCReg = ST->isWave32() ? AMDGPU::VCC_LO : AMDGPU::VCC;
889 bool Changed = false;
890
891 for (MachineBasicBlock &MBB : MF) {
893 for (I = MBB.begin(); I != MBB.end(); I = Next) {
894 Next = std::next(I);
895 MachineInstr &MI = *I;
896
897 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
898 // If this has a literal constant source that is the same as the
899 // reversed bits of an inline immediate, replace with a bitreverse of
900 // that constant. This saves 4 bytes in the common case of materializing
901 // sign bits.
902
903 // Test if we are after regalloc. We only want to do this after any
904 // optimizations happen because this will confuse them.
905 MachineOperand &Src = MI.getOperand(1);
906 if (Src.isImm() && IsPostRA) {
907 int32_t ModImm;
908 unsigned ModOpcode =
909 canModifyToInlineImmOp32(TII, Src, ModImm, /*Scalar=*/false);
910 if (ModOpcode != 0) {
911 MI.setDesc(TII->get(ModOpcode));
912 Src.setImm(static_cast<int64_t>(ModImm));
913 Changed = true;
914 continue;
915 }
916 }
917 }
918
919 if (ST->hasSwap() && (MI.getOpcode() == AMDGPU::V_MOV_B32_e32 ||
920 MI.getOpcode() == AMDGPU::V_MOV_B16_t16_e32 ||
921 MI.getOpcode() == AMDGPU::COPY)) {
922 if (auto *NextMI = matchSwap(MI)) {
923 Next = NextMI->getIterator();
924 Changed = true;
925 continue;
926 }
927 }
928
929 // Shrink scalar logic operations.
930 if (MI.getOpcode() == AMDGPU::S_AND_B32 ||
931 MI.getOpcode() == AMDGPU::S_OR_B32 ||
932 MI.getOpcode() == AMDGPU::S_XOR_B32) {
933 ChangeKind CK = shrinkScalarLogicOp(MI);
934 if (CK == ChangeKind::UpdateHint)
935 continue;
936 Changed |= (CK == ChangeKind::UpdateInst);
937 }
938
939 // Try to use S_ADDK_I32 and S_MULK_I32.
940 // S_OR_B32 sets SCC to (result != 0), but S_ADDK_I32 sets it to
941 // signed overflow, so only shrink the OR when SCC is dead.
942 if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
943 MI.getOpcode() == AMDGPU::S_MUL_I32 ||
944 (MI.getOpcode() == AMDGPU::S_OR_B32 &&
945 MI.getFlag(MachineInstr::MIFlag::Disjoint) &&
946 MI.allImplicitDefsAreDead())) {
947 const MachineOperand *Dest = &MI.getOperand(0);
948 MachineOperand *Src0 = &MI.getOperand(1);
949 MachineOperand *Src1 = &MI.getOperand(2);
950
951 if (!Src0->isReg() && Src1->isReg()) {
952 if (TII->commuteInstruction(MI, false, 1, 2)) {
953 std::swap(Src0, Src1);
954 Changed = true;
955 }
956 }
957
958 // FIXME: This could work better if hints worked with subregisters. If
959 // we have a vector add of a constant, we usually don't get the correct
960 // allocation due to the subregister usage.
961 if (Dest->getReg().isVirtual() && Src0->isReg()) {
962 MRI->setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
963 MRI->setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
964 continue;
965 }
966 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
967 if (Src1->isImm() && isKImmOperand(MI, *Src1)) {
968 unsigned Opc = (MI.getOpcode() == AMDGPU::S_MUL_I32)
969 ? AMDGPU::S_MULK_I32
970 : AMDGPU::S_ADDK_I32;
971 Src1->setImm(SignExtend64(Src1->getImm(), 32));
972 MI.setDesc(TII->get(Opc));
973 MI.tieOperands(0, 1);
974 Changed = true;
975 }
976 }
977 }
978
979 // Try to use s_cmpk_*
980 if (MI.isCompare() && TII->isSOPC(MI)) {
981 Changed |= shrinkScalarCompare(MI);
982 continue;
983 }
984
985 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
986 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
987 const MachineOperand &Dst = MI.getOperand(0);
988 MachineOperand &Src = MI.getOperand(1);
989
990 if (Src.isImm() && Dst.getReg().isPhysical()) {
991 unsigned ModOpc;
992 int32_t ModImm;
993 if (isKImmOperand(MI, Src)) {
994 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
995 Src.setImm(SignExtend64(Src.getImm(), 32));
996 Changed = true;
997 } else if ((ModOpc = canModifyToInlineImmOp32(TII, Src, ModImm,
998 /*Scalar=*/true))) {
999 MI.setDesc(TII->get(ModOpc));
1000 Src.setImm(static_cast<int64_t>(ModImm));
1001 Changed = true;
1002 }
1003 }
1004
1005 continue;
1006 }
1007
1008 if (IsPostRA && TII->isMIMG(MI.getOpcode()) &&
1009 ST->getGeneration() >= AMDGPUSubtarget::GFX10) {
1010 Changed |= shrinkMIMG(MI);
1011 continue;
1012 }
1013
1014 if (!TII->isVOP3(MI))
1015 continue;
1016
1017 if (MI.getOpcode() == AMDGPU::V_MAD_F32_e64 ||
1018 MI.getOpcode() == AMDGPU::V_FMA_F32_e64 ||
1019 MI.getOpcode() == AMDGPU::V_MAD_F16_e64 ||
1020 MI.getOpcode() == AMDGPU::V_FMA_F16_e64 ||
1021 MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_e64 ||
1022 MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_t16_e64 ||
1023 MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_fake16_e64 ||
1024 (MI.getOpcode() == AMDGPU::V_FMA_F64_e64 &&
1025 ST->hasFmaakFmamkF64Insts())) {
1026 Changed |= shrinkMadFma(MI);
1027 continue;
1028 }
1029
1030 // If there is no chance we will shrink it and use VCC as sdst to get
1031 // a 32 bit form try to replace dead sdst with NULL.
1032 Changed |= tryReplaceDeadSDST(MI);
1033 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
1034 continue;
1035
1036 if (!TII->canShrink(MI, *MRI)) {
1037 // Try commuting the instruction and see if that enables us to shrink
1038 // it.
1039 if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
1040 !TII->canShrink(MI, *MRI)) {
1041 Changed |= tryReplaceDeadSDST(MI);
1042 continue;
1043 }
1044
1045 // Operands were commuted.
1046 Changed = true;
1047 }
1048
1049 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
1050
1051 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
1052 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
1053 // instructions.
1054 const MachineOperand *Src2 =
1055 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
1056 if (!Src2->isReg())
1057 continue;
1058 Register SReg = Src2->getReg();
1059 if (SReg.isVirtual()) {
1060 MRI->setRegAllocationHint(SReg, 0, VCCReg);
1061 continue;
1062 }
1063 if (SReg != VCCReg)
1064 continue;
1065 }
1066
1067 // Check for the bool flag output for instructions like V_ADD_I32_e64.
1068 // For VOPC e64 this is also the dst operand. VOPCX (nosdst) variants
1069 // have no sdst, so they fall through to be shrunk directly.
1070 const MachineOperand *SDst =
1071 TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
1072
1073 if (SDst) {
1074 bool Next = false;
1075
1076 if (SDst->getReg() != VCCReg) {
1077 // VOPC instructions can only write to the VCC register. We can't
1078 // force them to use VCC here, because this is only one register and
1079 // cannot deal with sequences which would require multiple copies of
1080 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
1081 //
1082 // So, instead of forcing the instruction to write to VCC, we
1083 // provide a hint to the register allocator to use VCC and then we
1084 // will run this pass again after RA and shrink it if it outputs to
1085 // VCC.
1086 if (SDst->getReg().isVirtual())
1087 MRI->setRegAllocationHint(SDst->getReg(), 0, VCCReg);
1088 Next = true;
1089 }
1090
1091 // All of the instructions with carry outs also have an SGPR input in
1092 // src2.
1093 const MachineOperand *Src2 = TII->getNamedOperand(MI,
1094 AMDGPU::OpName::src2);
1095 if (Src2 && Src2->getReg() != VCCReg) {
1096 if (Src2->getReg().isVirtual())
1097 MRI->setRegAllocationHint(Src2->getReg(), 0, VCCReg);
1098 Next = true;
1099 }
1100
1101 if (Next)
1102 continue;
1103 }
1104
1105 // Pre-GFX10, shrinking VOP3 instructions pre-RA gave us the chance to
1106 // fold an immediate into the shrunk instruction as a literal operand. In
1107 // GFX10 VOP3 instructions can take a literal operand anyway, so there is
1108 // no advantage to doing this.
1109 // However, if 64-bit literals are allowed we still need to shrink it
1110 // for such literal to be able to fold.
1111 if (ST->hasVOP3Literal() &&
1112 (!ST->has64BitLiterals() || AMDGPU::isTrue16Inst(MI.getOpcode())) &&
1113 !IsPostRA)
1114 continue;
1115
1116 if (ST->hasTrue16BitInsts() && AMDGPU::isTrue16Inst(MI.getOpcode()) &&
1117 !shouldShrinkTrue16(MI))
1118 continue;
1119
1120 // We can shrink this instruction
1121 LLVM_DEBUG(dbgs() << "Shrinking " << MI);
1122
1123 MachineInstr *Inst32 = TII->buildShrunkInst(MI, Op32);
1124 ++NumInstructionsShrunk;
1125
1126 // Copy extra operands not present in the instruction definition.
1127 copyExtraImplicitOps(*Inst32, MI);
1128
1129 MI.eraseFromParent();
1130 foldImmediates(*Inst32);
1131
1132 LLVM_DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
1133 Changed = true;
1134 }
1135 }
1136 return Changed;
1137}
1138
1139bool SIShrinkInstructionsLegacy::runOnMachineFunction(MachineFunction &MF) {
1140 if (skipFunction(MF.getFunction()))
1141 return false;
1142
1143 return SIShrinkInstructions().run(MF);
1144}
1145
1146PreservedAnalyses
1149 if (MF.getFunction().hasOptNone() || !SIShrinkInstructions().run(MF))
1150 return PreservedAnalyses::all();
1151
1153 PA.preserveSet<CFGAnalyses>();
1154 return PA;
1155}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Imm
MachineBasicBlock & MBB
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
MachineRegisterInfo & MRI
static unsigned canModifyToInlineImmOp32(const SIInstrInfo *TII, const MachineOperand &Src, int32_t &ModifiedImm, bool Scalar)
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Class for arbitrary precision integers.
Definition APInt.h:78
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
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
bool hasSwap() const
bool hasFmaakFmamkF64Insts() const
const SIInstrInfo * getInstrInfo() const override
bool isWave32() const
unsigned getNSAMaxSize(bool HasSampler=false) const
bool hasSCmpK() const
Generation getGeneration() const
const HexagonRegisterInfo & getRegisterInfo() const
MCRegister getRegister(unsigned i) const
getRegister - Return the specified register in the class.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
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.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getNumImplicitOperands() const
Returns the implicit operands number.
iterator_range< filter_iterator< const_mop_iterator, bool(*)(const MachineOperand &)> > filtered_const_mop_range
const MachineBasicBlock * getParent() const
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI bool hasRegisterImplicitUseOperand(Register Reg) const
Returns true if the MachineInstr has an implicit-use operand of exactly the given register (not consi...
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const GlobalValue * getGlobal() const
void setImm(int64_t immVal)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
unsigned getTargetFlags() const
static MachineOperand CreateImm(int64_t Val)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
int64_t getOffset() const
Return the offset from the symbol in this operand.
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg)
setRegAllocationHint - Specify a register allocation hint for the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
static std::optional< unsigned > extractSubregFromGlobalAddressFlags(unsigned TargetFlags, unsigned SubRegIndex)
Return the target flags for a global address operand reading subregister SubRegIndex of a 64-bit glob...
static std::optional< int64_t > extractSubregFromImm(int64_t ImmVal, unsigned SubRegIndex)
Return the extracted immediate value in a subregister use from a constant materialized in a super reg...
static bool sopkIsZext(unsigned Opcode)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &)
void push_back(const T &Elt)
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
LLVM_READONLY int32_t getSOPKOp(uint32_t Opcode)
bool isKImmOperand(const MCInstrDesc &Desc, unsigned OpNo)
Is this a KImm operand?
int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, unsigned VDataDwords, unsigned VAddrDwords, bool IndexedRsrc, bool IndexedSamp)
bool isTrue16Inst(unsigned Opc)
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
LLVM_READONLY int32_t getVOPe32(uint32_t Opcode)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
@ Sub
Subtraction of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition MathExtras.h:554
constexpr T reverseBits(T Val)
Reverse the bits in Val.
Definition MathExtras.h:119
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
FunctionPass * createSIShrinkInstructionsLegacyPass()
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
constexpr bool any() const
Definition LaneBitmask.h:53