10b57cec5SDimitry Andric //===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric /// The pass tries to use the 32-bit encoding for instructions when possible.
80b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric //
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "AMDGPU.h"
12e8d8bef9SDimitry Andric #include "GCNSubtarget.h"
130b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
14bdd1243dSDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
150b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric #define DEBUG_TYPE "si-shrink-instructions"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric STATISTIC(NumInstructionsShrunk,
210b57cec5SDimitry Andric "Number of 64-bit instruction reduced to 32-bit.");
220b57cec5SDimitry Andric STATISTIC(NumLiteralConstantsFolded,
230b57cec5SDimitry Andric "Number of literal constants folded into 32-bit instructions.");
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric namespace {
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric class SIShrinkInstructions : public MachineFunctionPass {
30bdd1243dSDimitry Andric MachineFunction *MF;
3181ad6265SDimitry Andric MachineRegisterInfo *MRI;
3281ad6265SDimitry Andric const GCNSubtarget *ST;
3381ad6265SDimitry Andric const SIInstrInfo *TII;
3481ad6265SDimitry Andric const SIRegisterInfo *TRI;
3581ad6265SDimitry Andric
360b57cec5SDimitry Andric public:
370b57cec5SDimitry Andric static char ID;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric public:
SIShrinkInstructions()400b57cec5SDimitry Andric SIShrinkInstructions() : MachineFunctionPass(ID) {
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
4381ad6265SDimitry Andric bool foldImmediates(MachineInstr &MI, bool TryToCommute = true) const;
44bdd1243dSDimitry Andric bool shouldShrinkTrue16(MachineInstr &MI) const;
4581ad6265SDimitry Andric bool isKImmOperand(const MachineOperand &Src) const;
4681ad6265SDimitry Andric bool isKUImmOperand(const MachineOperand &Src) const;
4781ad6265SDimitry Andric bool isKImmOrKUImmOperand(const MachineOperand &Src, bool &IsUnsigned) const;
4881ad6265SDimitry Andric void copyExtraImplicitOps(MachineInstr &NewMI, MachineInstr &MI) const;
4981ad6265SDimitry Andric void shrinkScalarCompare(MachineInstr &MI) const;
5081ad6265SDimitry Andric void shrinkMIMG(MachineInstr &MI) const;
5181ad6265SDimitry Andric void shrinkMadFma(MachineInstr &MI) const;
5281ad6265SDimitry Andric bool shrinkScalarLogicOp(MachineInstr &MI) const;
5381ad6265SDimitry Andric bool tryReplaceDeadSDST(MachineInstr &MI) const;
5481ad6265SDimitry Andric bool instAccessReg(iterator_range<MachineInstr::const_mop_iterator> &&R,
5581ad6265SDimitry Andric Register Reg, unsigned SubReg) const;
5681ad6265SDimitry Andric bool instReadsReg(const MachineInstr *MI, unsigned Reg,
5781ad6265SDimitry Andric unsigned SubReg) const;
5881ad6265SDimitry Andric bool instModifiesReg(const MachineInstr *MI, unsigned Reg,
5981ad6265SDimitry Andric unsigned SubReg) const;
6081ad6265SDimitry Andric TargetInstrInfo::RegSubRegPair getSubRegForIndex(Register Reg, unsigned Sub,
6181ad6265SDimitry Andric unsigned I) const;
6281ad6265SDimitry Andric void dropInstructionKeepingImpDefs(MachineInstr &MI) const;
6381ad6265SDimitry Andric MachineInstr *matchSwap(MachineInstr &MovT) const;
6481ad6265SDimitry Andric
650b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
660b57cec5SDimitry Andric
getPassName() const670b57cec5SDimitry Andric StringRef getPassName() const override { return "SI Shrink Instructions"; }
680b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const690b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
700b57cec5SDimitry Andric AU.setPreservesCFG();
710b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric };
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric } // End anonymous namespace.
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric INITIALIZE_PASS(SIShrinkInstructions, DEBUG_TYPE,
780b57cec5SDimitry Andric "SI Shrink Instructions", false, false)
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric char SIShrinkInstructions::ID = 0;
810b57cec5SDimitry Andric
createSIShrinkInstructionsPass()820b57cec5SDimitry Andric FunctionPass *llvm::createSIShrinkInstructionsPass() {
830b57cec5SDimitry Andric return new SIShrinkInstructions();
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric /// This function checks \p MI for operands defined by a move immediate
870b57cec5SDimitry Andric /// instruction and then folds the literal constant into the instruction if it
880b57cec5SDimitry Andric /// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instructions.
foldImmediates(MachineInstr & MI,bool TryToCommute) const8981ad6265SDimitry Andric bool SIShrinkInstructions::foldImmediates(MachineInstr &MI,
9081ad6265SDimitry Andric bool TryToCommute) const {
910b57cec5SDimitry Andric assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric // Try to fold Src0
960b57cec5SDimitry Andric MachineOperand &Src0 = MI.getOperand(Src0Idx);
970b57cec5SDimitry Andric if (Src0.isReg()) {
988bcb0991SDimitry Andric Register Reg = Src0.getReg();
9981ad6265SDimitry Andric if (Reg.isVirtual()) {
10081ad6265SDimitry Andric MachineInstr *Def = MRI->getUniqueVRegDef(Reg);
1010b57cec5SDimitry Andric if (Def && Def->isMoveImmediate()) {
1020b57cec5SDimitry Andric MachineOperand &MovSrc = Def->getOperand(1);
1030b57cec5SDimitry Andric bool ConstantFolded = false;
1040b57cec5SDimitry Andric
105d409305fSDimitry Andric if (TII->isOperandLegal(MI, Src0Idx, &MovSrc)) {
1065f757f3fSDimitry Andric if (MovSrc.isImm()) {
1070b57cec5SDimitry Andric Src0.ChangeToImmediate(MovSrc.getImm());
1080b57cec5SDimitry Andric ConstantFolded = true;
1090b57cec5SDimitry Andric } else if (MovSrc.isFI()) {
1100b57cec5SDimitry Andric Src0.ChangeToFrameIndex(MovSrc.getIndex());
1110b57cec5SDimitry Andric ConstantFolded = true;
1120b57cec5SDimitry Andric } else if (MovSrc.isGlobal()) {
1130b57cec5SDimitry Andric Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(),
1140b57cec5SDimitry Andric MovSrc.getTargetFlags());
1150b57cec5SDimitry Andric ConstantFolded = true;
1160b57cec5SDimitry Andric }
117d409305fSDimitry Andric }
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric if (ConstantFolded) {
12081ad6265SDimitry Andric if (MRI->use_nodbg_empty(Reg))
1210b57cec5SDimitry Andric Def->eraseFromParent();
1220b57cec5SDimitry Andric ++NumLiteralConstantsFolded;
1230b57cec5SDimitry Andric return true;
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric // We have failed to fold src0, so commute the instruction and try again.
1300b57cec5SDimitry Andric if (TryToCommute && MI.isCommutable()) {
1310b57cec5SDimitry Andric if (TII->commuteInstruction(MI)) {
13281ad6265SDimitry Andric if (foldImmediates(MI, false))
1330b57cec5SDimitry Andric return true;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric // Commute back.
1360b57cec5SDimitry Andric TII->commuteInstruction(MI);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric return false;
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric
143bdd1243dSDimitry Andric /// Do not shrink the instruction if its registers are not expressible in the
144bdd1243dSDimitry Andric /// shrunk encoding.
shouldShrinkTrue16(MachineInstr & MI) const145bdd1243dSDimitry Andric bool SIShrinkInstructions::shouldShrinkTrue16(MachineInstr &MI) const {
146bdd1243dSDimitry Andric for (unsigned I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
147bdd1243dSDimitry Andric const MachineOperand &MO = MI.getOperand(I);
148bdd1243dSDimitry Andric if (MO.isReg()) {
149bdd1243dSDimitry Andric Register Reg = MO.getReg();
150bdd1243dSDimitry Andric assert(!Reg.isVirtual() && "Prior checks should ensure we only shrink "
151bdd1243dSDimitry Andric "True16 Instructions post-RA");
152bdd1243dSDimitry Andric if (AMDGPU::VGPR_32RegClass.contains(Reg) &&
153bdd1243dSDimitry Andric !AMDGPU::VGPR_32_Lo128RegClass.contains(Reg))
154bdd1243dSDimitry Andric return false;
155bdd1243dSDimitry Andric }
156bdd1243dSDimitry Andric }
157bdd1243dSDimitry Andric return true;
158bdd1243dSDimitry Andric }
159bdd1243dSDimitry Andric
isKImmOperand(const MachineOperand & Src) const16081ad6265SDimitry Andric bool SIShrinkInstructions::isKImmOperand(const MachineOperand &Src) const {
1615f757f3fSDimitry Andric return isInt<16>(SignExtend64(Src.getImm(), 32)) &&
16206c3fb27SDimitry Andric !TII->isInlineConstant(*Src.getParent(), Src.getOperandNo());
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric
isKUImmOperand(const MachineOperand & Src) const16581ad6265SDimitry Andric bool SIShrinkInstructions::isKUImmOperand(const MachineOperand &Src) const {
1660b57cec5SDimitry Andric return isUInt<16>(Src.getImm()) &&
16706c3fb27SDimitry Andric !TII->isInlineConstant(*Src.getParent(), Src.getOperandNo());
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric
isKImmOrKUImmOperand(const MachineOperand & Src,bool & IsUnsigned) const17081ad6265SDimitry Andric bool SIShrinkInstructions::isKImmOrKUImmOperand(const MachineOperand &Src,
17181ad6265SDimitry Andric bool &IsUnsigned) const {
1725f757f3fSDimitry Andric if (isInt<16>(SignExtend64(Src.getImm(), 32))) {
1730b57cec5SDimitry Andric IsUnsigned = false;
1740b57cec5SDimitry Andric return !TII->isInlineConstant(Src);
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric if (isUInt<16>(Src.getImm())) {
1780b57cec5SDimitry Andric IsUnsigned = true;
1790b57cec5SDimitry Andric return !TII->isInlineConstant(Src);
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric return false;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric
185*0fca6ea1SDimitry Andric /// \returns the opcode of an instruction a move immediate of the constant \p
186*0fca6ea1SDimitry Andric /// Src can be replaced with if the constant is replaced with \p ModifiedImm.
187*0fca6ea1SDimitry Andric /// i.e.
188*0fca6ea1SDimitry Andric ///
189*0fca6ea1SDimitry Andric /// If the bitreverse of a constant is an inline immediate, reverse the
190*0fca6ea1SDimitry Andric /// immediate and return the bitreverse opcode.
191*0fca6ea1SDimitry Andric ///
192*0fca6ea1SDimitry Andric /// If the bitwise negation of a constant is an inline immediate, reverse the
193*0fca6ea1SDimitry Andric /// immediate and return the bitwise not opcode.
canModifyToInlineImmOp32(const SIInstrInfo * TII,const MachineOperand & Src,int32_t & ModifiedImm,bool Scalar)194*0fca6ea1SDimitry Andric static unsigned canModifyToInlineImmOp32(const SIInstrInfo *TII,
195*0fca6ea1SDimitry Andric const MachineOperand &Src,
196*0fca6ea1SDimitry Andric int32_t &ModifiedImm, bool Scalar) {
197*0fca6ea1SDimitry Andric if (TII->isInlineConstant(Src))
198*0fca6ea1SDimitry Andric return 0;
199*0fca6ea1SDimitry Andric int32_t SrcImm = static_cast<int32_t>(Src.getImm());
2000b57cec5SDimitry Andric
201*0fca6ea1SDimitry Andric if (!Scalar) {
202*0fca6ea1SDimitry Andric // We could handle the scalar case with here, but we would need to check
203*0fca6ea1SDimitry Andric // that SCC is not live as S_NOT_B32 clobbers it. It's probably not worth
204*0fca6ea1SDimitry Andric // it, as the reasonable values are already covered by s_movk_i32.
205*0fca6ea1SDimitry Andric ModifiedImm = ~SrcImm;
206*0fca6ea1SDimitry Andric if (TII->isInlineConstant(APInt(32, ModifiedImm)))
207*0fca6ea1SDimitry Andric return AMDGPU::V_NOT_B32_e32;
208*0fca6ea1SDimitry Andric }
209*0fca6ea1SDimitry Andric
210*0fca6ea1SDimitry Andric ModifiedImm = reverseBits<int32_t>(SrcImm);
211*0fca6ea1SDimitry Andric if (TII->isInlineConstant(APInt(32, ModifiedImm)))
212*0fca6ea1SDimitry Andric return Scalar ? AMDGPU::S_BREV_B32 : AMDGPU::V_BFREV_B32_e32;
213*0fca6ea1SDimitry Andric
214*0fca6ea1SDimitry Andric return 0;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric /// Copy implicit register operands from specified instruction to this
2180b57cec5SDimitry Andric /// instruction that are not part of the instruction definition.
copyExtraImplicitOps(MachineInstr & NewMI,MachineInstr & MI) const21981ad6265SDimitry Andric void SIShrinkInstructions::copyExtraImplicitOps(MachineInstr &NewMI,
22081ad6265SDimitry Andric MachineInstr &MI) const {
22181ad6265SDimitry Andric MachineFunction &MF = *MI.getMF();
2220b57cec5SDimitry Andric for (unsigned i = MI.getDesc().getNumOperands() +
223bdd1243dSDimitry Andric MI.getDesc().implicit_uses().size() +
224bdd1243dSDimitry Andric MI.getDesc().implicit_defs().size(),
225bdd1243dSDimitry Andric e = MI.getNumOperands();
2260b57cec5SDimitry Andric i != e; ++i) {
2270b57cec5SDimitry Andric const MachineOperand &MO = MI.getOperand(i);
2280b57cec5SDimitry Andric if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
2290b57cec5SDimitry Andric NewMI.addOperand(MF, MO);
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
shrinkScalarCompare(MachineInstr & MI) const23381ad6265SDimitry Andric void SIShrinkInstructions::shrinkScalarCompare(MachineInstr &MI) const {
2345f757f3fSDimitry Andric if (!ST->hasSCmpK())
2355f757f3fSDimitry Andric return;
2365f757f3fSDimitry Andric
2370b57cec5SDimitry Andric // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
2380b57cec5SDimitry Andric // get constants on the RHS.
2390b57cec5SDimitry Andric if (!MI.getOperand(0).isReg())
2400b57cec5SDimitry Andric TII->commuteInstruction(MI, false, 0, 1);
2410b57cec5SDimitry Andric
2425ffd83dbSDimitry Andric // cmpk requires src0 to be a register
2435ffd83dbSDimitry Andric const MachineOperand &Src0 = MI.getOperand(0);
2445ffd83dbSDimitry Andric if (!Src0.isReg())
2455ffd83dbSDimitry Andric return;
2465ffd83dbSDimitry Andric
2475f757f3fSDimitry Andric MachineOperand &Src1 = MI.getOperand(1);
2480b57cec5SDimitry Andric if (!Src1.isImm())
2490b57cec5SDimitry Andric return;
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
2520b57cec5SDimitry Andric if (SOPKOpc == -1)
2530b57cec5SDimitry Andric return;
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric // eq/ne is special because the imm16 can be treated as signed or unsigned,
256349cc55cSDimitry Andric // and initially selected to the unsigned versions.
2570b57cec5SDimitry Andric if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
2580b57cec5SDimitry Andric bool HasUImm;
25981ad6265SDimitry Andric if (isKImmOrKUImmOperand(Src1, HasUImm)) {
2600b57cec5SDimitry Andric if (!HasUImm) {
2610b57cec5SDimitry Andric SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
2620b57cec5SDimitry Andric AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
2635f757f3fSDimitry Andric Src1.setImm(SignExtend32(Src1.getImm(), 32));
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric MI.setDesc(TII->get(SOPKOpc));
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric return;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
2730b57cec5SDimitry Andric
274*0fca6ea1SDimitry Andric if ((SIInstrInfo::sopkIsZext(SOPKOpc) && isKUImmOperand(Src1)) ||
275*0fca6ea1SDimitry Andric (!SIInstrInfo::sopkIsZext(SOPKOpc) && isKImmOperand(Src1))) {
276*0fca6ea1SDimitry Andric if (!SIInstrInfo::sopkIsZext(SOPKOpc))
2775f757f3fSDimitry Andric Src1.setImm(SignExtend64(Src1.getImm(), 32));
2780b57cec5SDimitry Andric MI.setDesc(NewDesc);
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andric // Shrink NSA encoded instructions with contiguous VGPRs to non-NSA encoding.
shrinkMIMG(MachineInstr & MI) const28381ad6265SDimitry Andric void SIShrinkInstructions::shrinkMIMG(MachineInstr &MI) const {
2840b57cec5SDimitry Andric const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
28581ad6265SDimitry Andric if (!Info)
2860b57cec5SDimitry Andric return;
2870b57cec5SDimitry Andric
28881ad6265SDimitry Andric uint8_t NewEncoding;
28981ad6265SDimitry Andric switch (Info->MIMGEncoding) {
29081ad6265SDimitry Andric case AMDGPU::MIMGEncGfx10NSA:
29181ad6265SDimitry Andric NewEncoding = AMDGPU::MIMGEncGfx10Default;
29281ad6265SDimitry Andric break;
29381ad6265SDimitry Andric case AMDGPU::MIMGEncGfx11NSA:
29481ad6265SDimitry Andric NewEncoding = AMDGPU::MIMGEncGfx11Default;
29581ad6265SDimitry Andric break;
29681ad6265SDimitry Andric default:
29781ad6265SDimitry Andric return;
29881ad6265SDimitry Andric }
29981ad6265SDimitry Andric
3000b57cec5SDimitry Andric int VAddr0Idx =
3010b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
3020b57cec5SDimitry Andric unsigned NewAddrDwords = Info->VAddrDwords;
3030b57cec5SDimitry Andric const TargetRegisterClass *RC;
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric if (Info->VAddrDwords == 2) {
3060b57cec5SDimitry Andric RC = &AMDGPU::VReg_64RegClass;
3070b57cec5SDimitry Andric } else if (Info->VAddrDwords == 3) {
3080b57cec5SDimitry Andric RC = &AMDGPU::VReg_96RegClass;
3090b57cec5SDimitry Andric } else if (Info->VAddrDwords == 4) {
3100b57cec5SDimitry Andric RC = &AMDGPU::VReg_128RegClass;
311fe6060f1SDimitry Andric } else if (Info->VAddrDwords == 5) {
312fe6060f1SDimitry Andric RC = &AMDGPU::VReg_160RegClass;
313fe6060f1SDimitry Andric } else if (Info->VAddrDwords == 6) {
314fe6060f1SDimitry Andric RC = &AMDGPU::VReg_192RegClass;
315fe6060f1SDimitry Andric } else if (Info->VAddrDwords == 7) {
316fe6060f1SDimitry Andric RC = &AMDGPU::VReg_224RegClass;
317fe6060f1SDimitry Andric } else if (Info->VAddrDwords == 8) {
3180b57cec5SDimitry Andric RC = &AMDGPU::VReg_256RegClass;
319bdd1243dSDimitry Andric } else if (Info->VAddrDwords == 9) {
320bdd1243dSDimitry Andric RC = &AMDGPU::VReg_288RegClass;
321bdd1243dSDimitry Andric } else if (Info->VAddrDwords == 10) {
322bdd1243dSDimitry Andric RC = &AMDGPU::VReg_320RegClass;
323bdd1243dSDimitry Andric } else if (Info->VAddrDwords == 11) {
324bdd1243dSDimitry Andric RC = &AMDGPU::VReg_352RegClass;
325bdd1243dSDimitry Andric } else if (Info->VAddrDwords == 12) {
326bdd1243dSDimitry Andric RC = &AMDGPU::VReg_384RegClass;
3270b57cec5SDimitry Andric } else {
3280b57cec5SDimitry Andric RC = &AMDGPU::VReg_512RegClass;
3290b57cec5SDimitry Andric NewAddrDwords = 16;
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric
3320b57cec5SDimitry Andric unsigned VgprBase = 0;
33381ad6265SDimitry Andric unsigned NextVgpr = 0;
3340b57cec5SDimitry Andric bool IsUndef = true;
3350b57cec5SDimitry Andric bool IsKill = NewAddrDwords == Info->VAddrDwords;
33606c3fb27SDimitry Andric const unsigned NSAMaxSize = ST->getNSAMaxSize();
33706c3fb27SDimitry Andric const bool IsPartialNSA = NewAddrDwords > NSAMaxSize;
33806c3fb27SDimitry Andric const unsigned EndVAddr = IsPartialNSA ? NSAMaxSize : Info->VAddrOperands;
33906c3fb27SDimitry Andric for (unsigned Idx = 0; Idx < EndVAddr; ++Idx) {
34081ad6265SDimitry Andric const MachineOperand &Op = MI.getOperand(VAddr0Idx + Idx);
34181ad6265SDimitry Andric unsigned Vgpr = TRI->getHWRegIndex(Op.getReg());
34281ad6265SDimitry Andric unsigned Dwords = TRI->getRegSizeInBits(Op.getReg(), *MRI) / 32;
34381ad6265SDimitry Andric assert(Dwords > 0 && "Un-implemented for less than 32 bit regs");
3440b57cec5SDimitry Andric
34581ad6265SDimitry Andric if (Idx == 0) {
3460b57cec5SDimitry Andric VgprBase = Vgpr;
34781ad6265SDimitry Andric NextVgpr = Vgpr + Dwords;
34881ad6265SDimitry Andric } else if (Vgpr == NextVgpr) {
34981ad6265SDimitry Andric NextVgpr = Vgpr + Dwords;
35081ad6265SDimitry Andric } else {
3510b57cec5SDimitry Andric return;
35281ad6265SDimitry Andric }
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric if (!Op.isUndef())
3550b57cec5SDimitry Andric IsUndef = false;
3560b57cec5SDimitry Andric if (!Op.isKill())
3570b57cec5SDimitry Andric IsKill = false;
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric if (VgprBase + NewAddrDwords > 256)
3610b57cec5SDimitry Andric return;
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric // Further check for implicit tied operands - this may be present if TFE is
3640b57cec5SDimitry Andric // enabled
3650b57cec5SDimitry Andric int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe);
3660b57cec5SDimitry Andric int LWEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::lwe);
367e8d8bef9SDimitry Andric unsigned TFEVal = (TFEIdx == -1) ? 0 : MI.getOperand(TFEIdx).getImm();
368e8d8bef9SDimitry Andric unsigned LWEVal = (LWEIdx == -1) ? 0 : MI.getOperand(LWEIdx).getImm();
3690b57cec5SDimitry Andric int ToUntie = -1;
3700b57cec5SDimitry Andric if (TFEVal || LWEVal) {
3710b57cec5SDimitry Andric // TFE/LWE is enabled so we need to deal with an implicit tied operand
3720b57cec5SDimitry Andric for (unsigned i = LWEIdx + 1, e = MI.getNumOperands(); i != e; ++i) {
3730b57cec5SDimitry Andric if (MI.getOperand(i).isReg() && MI.getOperand(i).isTied() &&
3740b57cec5SDimitry Andric MI.getOperand(i).isImplicit()) {
3750b57cec5SDimitry Andric // This is the tied operand
3760b57cec5SDimitry Andric assert(
3770b57cec5SDimitry Andric ToUntie == -1 &&
3780b57cec5SDimitry Andric "found more than one tied implicit operand when expecting only 1");
3790b57cec5SDimitry Andric ToUntie = i;
3800b57cec5SDimitry Andric MI.untieRegOperand(ToUntie);
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
38581ad6265SDimitry Andric unsigned NewOpcode = AMDGPU::getMIMGOpcode(Info->BaseOpcode, NewEncoding,
3860b57cec5SDimitry Andric Info->VDataDwords, NewAddrDwords);
3870b57cec5SDimitry Andric MI.setDesc(TII->get(NewOpcode));
3880b57cec5SDimitry Andric MI.getOperand(VAddr0Idx).setReg(RC->getRegister(VgprBase));
3890b57cec5SDimitry Andric MI.getOperand(VAddr0Idx).setIsUndef(IsUndef);
3900b57cec5SDimitry Andric MI.getOperand(VAddr0Idx).setIsKill(IsKill);
3910b57cec5SDimitry Andric
39206c3fb27SDimitry Andric for (unsigned i = 1; i < EndVAddr; ++i)
39381ad6265SDimitry Andric MI.removeOperand(VAddr0Idx + 1);
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andric if (ToUntie >= 0) {
3960b57cec5SDimitry Andric MI.tieOperands(
3970b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata),
39806c3fb27SDimitry Andric ToUntie - (EndVAddr - 1));
39981ad6265SDimitry Andric }
40081ad6265SDimitry Andric }
40181ad6265SDimitry Andric
40281ad6265SDimitry Andric // Shrink MAD to MADAK/MADMK and FMA to FMAAK/FMAMK.
shrinkMadFma(MachineInstr & MI) const40381ad6265SDimitry Andric void SIShrinkInstructions::shrinkMadFma(MachineInstr &MI) const {
404bdd1243dSDimitry Andric // Pre-GFX10 VOP3 instructions like MAD/FMA cannot take a literal operand so
405bdd1243dSDimitry Andric // there is no reason to try to shrink them.
40681ad6265SDimitry Andric if (!ST->hasVOP3Literal())
40781ad6265SDimitry Andric return;
40881ad6265SDimitry Andric
409bdd1243dSDimitry Andric // There is no advantage to doing this pre-RA.
410bdd1243dSDimitry Andric if (!MF->getProperties().hasProperty(
411bdd1243dSDimitry Andric MachineFunctionProperties::Property::NoVRegs))
412bdd1243dSDimitry Andric return;
413bdd1243dSDimitry Andric
41481ad6265SDimitry Andric if (TII->hasAnyModifiersSet(MI))
41581ad6265SDimitry Andric return;
41681ad6265SDimitry Andric
41781ad6265SDimitry Andric const unsigned Opcode = MI.getOpcode();
41881ad6265SDimitry Andric MachineOperand &Src0 = *TII->getNamedOperand(MI, AMDGPU::OpName::src0);
41981ad6265SDimitry Andric MachineOperand &Src1 = *TII->getNamedOperand(MI, AMDGPU::OpName::src1);
42081ad6265SDimitry Andric MachineOperand &Src2 = *TII->getNamedOperand(MI, AMDGPU::OpName::src2);
42181ad6265SDimitry Andric unsigned NewOpcode = AMDGPU::INSTRUCTION_LIST_END;
42281ad6265SDimitry Andric
42381ad6265SDimitry Andric bool Swap;
42481ad6265SDimitry Andric
42581ad6265SDimitry Andric // Detect "Dst = VSrc * VGPR + Imm" and convert to AK form.
42681ad6265SDimitry Andric if (Src2.isImm() && !TII->isInlineConstant(Src2)) {
42781ad6265SDimitry Andric if (Src1.isReg() && TRI->isVGPR(*MRI, Src1.getReg()))
42881ad6265SDimitry Andric Swap = false;
42981ad6265SDimitry Andric else if (Src0.isReg() && TRI->isVGPR(*MRI, Src0.getReg()))
43081ad6265SDimitry Andric Swap = true;
43181ad6265SDimitry Andric else
43281ad6265SDimitry Andric return;
43381ad6265SDimitry Andric
43481ad6265SDimitry Andric switch (Opcode) {
43581ad6265SDimitry Andric default:
43681ad6265SDimitry Andric llvm_unreachable("Unexpected mad/fma opcode!");
43781ad6265SDimitry Andric case AMDGPU::V_MAD_F32_e64:
43881ad6265SDimitry Andric NewOpcode = AMDGPU::V_MADAK_F32;
43981ad6265SDimitry Andric break;
44081ad6265SDimitry Andric case AMDGPU::V_FMA_F32_e64:
44181ad6265SDimitry Andric NewOpcode = AMDGPU::V_FMAAK_F32;
44281ad6265SDimitry Andric break;
44381ad6265SDimitry Andric case AMDGPU::V_MAD_F16_e64:
44481ad6265SDimitry Andric NewOpcode = AMDGPU::V_MADAK_F16;
44581ad6265SDimitry Andric break;
44681ad6265SDimitry Andric case AMDGPU::V_FMA_F16_e64:
447bdd1243dSDimitry Andric case AMDGPU::V_FMA_F16_gfx9_e64:
448bdd1243dSDimitry Andric NewOpcode = ST->hasTrue16BitInsts() ? AMDGPU::V_FMAAK_F16_t16
449bdd1243dSDimitry Andric : AMDGPU::V_FMAAK_F16;
45081ad6265SDimitry Andric break;
45181ad6265SDimitry Andric }
45281ad6265SDimitry Andric }
45381ad6265SDimitry Andric
45481ad6265SDimitry Andric // Detect "Dst = VSrc * Imm + VGPR" and convert to MK form.
45581ad6265SDimitry Andric if (Src2.isReg() && TRI->isVGPR(*MRI, Src2.getReg())) {
45681ad6265SDimitry Andric if (Src1.isImm() && !TII->isInlineConstant(Src1))
45781ad6265SDimitry Andric Swap = false;
45881ad6265SDimitry Andric else if (Src0.isImm() && !TII->isInlineConstant(Src0))
45981ad6265SDimitry Andric Swap = true;
46081ad6265SDimitry Andric else
46181ad6265SDimitry Andric return;
46281ad6265SDimitry Andric
46381ad6265SDimitry Andric switch (Opcode) {
46481ad6265SDimitry Andric default:
46581ad6265SDimitry Andric llvm_unreachable("Unexpected mad/fma opcode!");
46681ad6265SDimitry Andric case AMDGPU::V_MAD_F32_e64:
46781ad6265SDimitry Andric NewOpcode = AMDGPU::V_MADMK_F32;
46881ad6265SDimitry Andric break;
46981ad6265SDimitry Andric case AMDGPU::V_FMA_F32_e64:
47081ad6265SDimitry Andric NewOpcode = AMDGPU::V_FMAMK_F32;
47181ad6265SDimitry Andric break;
47281ad6265SDimitry Andric case AMDGPU::V_MAD_F16_e64:
47381ad6265SDimitry Andric NewOpcode = AMDGPU::V_MADMK_F16;
47481ad6265SDimitry Andric break;
47581ad6265SDimitry Andric case AMDGPU::V_FMA_F16_e64:
476bdd1243dSDimitry Andric case AMDGPU::V_FMA_F16_gfx9_e64:
477bdd1243dSDimitry Andric NewOpcode = ST->hasTrue16BitInsts() ? AMDGPU::V_FMAMK_F16_t16
478bdd1243dSDimitry Andric : AMDGPU::V_FMAMK_F16;
47981ad6265SDimitry Andric break;
48081ad6265SDimitry Andric }
48181ad6265SDimitry Andric }
48281ad6265SDimitry Andric
48381ad6265SDimitry Andric if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END)
48481ad6265SDimitry Andric return;
48581ad6265SDimitry Andric
486bdd1243dSDimitry Andric if (AMDGPU::isTrue16Inst(NewOpcode) && !shouldShrinkTrue16(MI))
487bdd1243dSDimitry Andric return;
488bdd1243dSDimitry Andric
48981ad6265SDimitry Andric if (Swap) {
49081ad6265SDimitry Andric // Swap Src0 and Src1 by building a new instruction.
49181ad6265SDimitry Andric BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(NewOpcode),
49281ad6265SDimitry Andric MI.getOperand(0).getReg())
49381ad6265SDimitry Andric .add(Src1)
49481ad6265SDimitry Andric .add(Src0)
49581ad6265SDimitry Andric .add(Src2)
49681ad6265SDimitry Andric .setMIFlags(MI.getFlags());
49781ad6265SDimitry Andric MI.eraseFromParent();
49881ad6265SDimitry Andric } else {
49981ad6265SDimitry Andric TII->removeModOperands(MI);
50081ad6265SDimitry Andric MI.setDesc(TII->get(NewOpcode));
5010b57cec5SDimitry Andric }
5020b57cec5SDimitry Andric }
5030b57cec5SDimitry Andric
50406c3fb27SDimitry Andric /// Attempt to shrink AND/OR/XOR operations requiring non-inlineable literals.
5050b57cec5SDimitry Andric /// For AND or OR, try using S_BITSET{0,1} to clear or set bits.
5060b57cec5SDimitry Andric /// If the inverse of the immediate is legal, use ANDN2, ORN2 or
5070b57cec5SDimitry Andric /// XNOR (as a ^ b == ~(a ^ ~b)).
5080b57cec5SDimitry Andric /// \returns true if the caller should continue the machine function iterator
shrinkScalarLogicOp(MachineInstr & MI) const50981ad6265SDimitry Andric bool SIShrinkInstructions::shrinkScalarLogicOp(MachineInstr &MI) const {
5100b57cec5SDimitry Andric unsigned Opc = MI.getOpcode();
5110b57cec5SDimitry Andric const MachineOperand *Dest = &MI.getOperand(0);
5120b57cec5SDimitry Andric MachineOperand *Src0 = &MI.getOperand(1);
5130b57cec5SDimitry Andric MachineOperand *Src1 = &MI.getOperand(2);
5140b57cec5SDimitry Andric MachineOperand *SrcReg = Src0;
5150b57cec5SDimitry Andric MachineOperand *SrcImm = Src1;
5160b57cec5SDimitry Andric
5175ffd83dbSDimitry Andric if (!SrcImm->isImm() ||
51881ad6265SDimitry Andric AMDGPU::isInlinableLiteral32(SrcImm->getImm(), ST->hasInv2PiInlineImm()))
5195ffd83dbSDimitry Andric return false;
5205ffd83dbSDimitry Andric
5210b57cec5SDimitry Andric uint32_t Imm = static_cast<uint32_t>(SrcImm->getImm());
5220b57cec5SDimitry Andric uint32_t NewImm = 0;
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andric if (Opc == AMDGPU::S_AND_B32) {
5250b57cec5SDimitry Andric if (isPowerOf2_32(~Imm)) {
52606c3fb27SDimitry Andric NewImm = llvm::countr_one(Imm);
5270b57cec5SDimitry Andric Opc = AMDGPU::S_BITSET0_B32;
52881ad6265SDimitry Andric } else if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
5290b57cec5SDimitry Andric NewImm = ~Imm;
5300b57cec5SDimitry Andric Opc = AMDGPU::S_ANDN2_B32;
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric } else if (Opc == AMDGPU::S_OR_B32) {
5330b57cec5SDimitry Andric if (isPowerOf2_32(Imm)) {
53406c3fb27SDimitry Andric NewImm = llvm::countr_zero(Imm);
5350b57cec5SDimitry Andric Opc = AMDGPU::S_BITSET1_B32;
53681ad6265SDimitry Andric } else if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
5370b57cec5SDimitry Andric NewImm = ~Imm;
5380b57cec5SDimitry Andric Opc = AMDGPU::S_ORN2_B32;
5390b57cec5SDimitry Andric }
5400b57cec5SDimitry Andric } else if (Opc == AMDGPU::S_XOR_B32) {
54181ad6265SDimitry Andric if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
5420b57cec5SDimitry Andric NewImm = ~Imm;
5430b57cec5SDimitry Andric Opc = AMDGPU::S_XNOR_B32;
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric } else {
5460b57cec5SDimitry Andric llvm_unreachable("unexpected opcode");
5470b57cec5SDimitry Andric }
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andric if (NewImm != 0) {
550e8d8bef9SDimitry Andric if (Dest->getReg().isVirtual() && SrcReg->isReg()) {
55181ad6265SDimitry Andric MRI->setRegAllocationHint(Dest->getReg(), 0, SrcReg->getReg());
55281ad6265SDimitry Andric MRI->setRegAllocationHint(SrcReg->getReg(), 0, Dest->getReg());
5530b57cec5SDimitry Andric return true;
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric if (SrcReg->isReg() && SrcReg->getReg() == Dest->getReg()) {
557e8d8bef9SDimitry Andric const bool IsUndef = SrcReg->isUndef();
558e8d8bef9SDimitry Andric const bool IsKill = SrcReg->isKill();
5590b57cec5SDimitry Andric MI.setDesc(TII->get(Opc));
5600b57cec5SDimitry Andric if (Opc == AMDGPU::S_BITSET0_B32 ||
5610b57cec5SDimitry Andric Opc == AMDGPU::S_BITSET1_B32) {
5620b57cec5SDimitry Andric Src0->ChangeToImmediate(NewImm);
5630b57cec5SDimitry Andric // Remove the immediate and add the tied input.
564e8d8bef9SDimitry Andric MI.getOperand(2).ChangeToRegister(Dest->getReg(), /*IsDef*/ false,
565e8d8bef9SDimitry Andric /*isImp*/ false, IsKill,
566e8d8bef9SDimitry Andric /*isDead*/ false, IsUndef);
5670b57cec5SDimitry Andric MI.tieOperands(0, 2);
5680b57cec5SDimitry Andric } else {
5690b57cec5SDimitry Andric SrcImm->setImm(NewImm);
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andric return false;
5750b57cec5SDimitry Andric }
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andric // This is the same as MachineInstr::readsRegister/modifiesRegister except
5780b57cec5SDimitry Andric // it takes subregs into account.
instAccessReg(iterator_range<MachineInstr::const_mop_iterator> && R,Register Reg,unsigned SubReg) const57981ad6265SDimitry Andric bool SIShrinkInstructions::instAccessReg(
58081ad6265SDimitry Andric iterator_range<MachineInstr::const_mop_iterator> &&R, Register Reg,
58181ad6265SDimitry Andric unsigned SubReg) const {
5820b57cec5SDimitry Andric for (const MachineOperand &MO : R) {
5830b57cec5SDimitry Andric if (!MO.isReg())
5840b57cec5SDimitry Andric continue;
5850b57cec5SDimitry Andric
586e8d8bef9SDimitry Andric if (Reg.isPhysical() && MO.getReg().isPhysical()) {
58781ad6265SDimitry Andric if (TRI->regsOverlap(Reg, MO.getReg()))
5880b57cec5SDimitry Andric return true;
589e8d8bef9SDimitry Andric } else if (MO.getReg() == Reg && Reg.isVirtual()) {
59081ad6265SDimitry Andric LaneBitmask Overlap = TRI->getSubRegIndexLaneMask(SubReg) &
59181ad6265SDimitry Andric TRI->getSubRegIndexLaneMask(MO.getSubReg());
5920b57cec5SDimitry Andric if (Overlap.any())
5930b57cec5SDimitry Andric return true;
5940b57cec5SDimitry Andric }
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric return false;
5970b57cec5SDimitry Andric }
5980b57cec5SDimitry Andric
instReadsReg(const MachineInstr * MI,unsigned Reg,unsigned SubReg) const59981ad6265SDimitry Andric bool SIShrinkInstructions::instReadsReg(const MachineInstr *MI, unsigned Reg,
60081ad6265SDimitry Andric unsigned SubReg) const {
60181ad6265SDimitry Andric return instAccessReg(MI->uses(), Reg, SubReg);
6020b57cec5SDimitry Andric }
6030b57cec5SDimitry Andric
instModifiesReg(const MachineInstr * MI,unsigned Reg,unsigned SubReg) const60481ad6265SDimitry Andric bool SIShrinkInstructions::instModifiesReg(const MachineInstr *MI, unsigned Reg,
60581ad6265SDimitry Andric unsigned SubReg) const {
60681ad6265SDimitry Andric return instAccessReg(MI->defs(), Reg, SubReg);
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric
60981ad6265SDimitry Andric TargetInstrInfo::RegSubRegPair
getSubRegForIndex(Register Reg,unsigned Sub,unsigned I) const61081ad6265SDimitry Andric SIShrinkInstructions::getSubRegForIndex(Register Reg, unsigned Sub,
61181ad6265SDimitry Andric unsigned I) const {
61281ad6265SDimitry Andric if (TRI->getRegSizeInBits(Reg, *MRI) != 32) {
613e8d8bef9SDimitry Andric if (Reg.isPhysical()) {
61481ad6265SDimitry Andric Reg = TRI->getSubReg(Reg, TRI->getSubRegFromChannel(I));
6150b57cec5SDimitry Andric } else {
61681ad6265SDimitry Andric Sub = TRI->getSubRegFromChannel(I + TRI->getChannelFromSubReg(Sub));
6170b57cec5SDimitry Andric }
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric return TargetInstrInfo::RegSubRegPair(Reg, Sub);
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric
dropInstructionKeepingImpDefs(MachineInstr & MI) const62281ad6265SDimitry Andric void SIShrinkInstructions::dropInstructionKeepingImpDefs(
62381ad6265SDimitry Andric MachineInstr &MI) const {
624e8d8bef9SDimitry Andric for (unsigned i = MI.getDesc().getNumOperands() +
625bdd1243dSDimitry Andric MI.getDesc().implicit_uses().size() +
626bdd1243dSDimitry Andric MI.getDesc().implicit_defs().size(),
627bdd1243dSDimitry Andric e = MI.getNumOperands();
628e8d8bef9SDimitry Andric i != e; ++i) {
629e8d8bef9SDimitry Andric const MachineOperand &Op = MI.getOperand(i);
630e8d8bef9SDimitry Andric if (!Op.isDef())
631e8d8bef9SDimitry Andric continue;
632e8d8bef9SDimitry Andric BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
633e8d8bef9SDimitry Andric TII->get(AMDGPU::IMPLICIT_DEF), Op.getReg());
634e8d8bef9SDimitry Andric }
635e8d8bef9SDimitry Andric
636e8d8bef9SDimitry Andric MI.eraseFromParent();
637e8d8bef9SDimitry Andric }
638e8d8bef9SDimitry Andric
6390b57cec5SDimitry Andric // Match:
6400b57cec5SDimitry Andric // mov t, x
6410b57cec5SDimitry Andric // mov x, y
6420b57cec5SDimitry Andric // mov y, t
6430b57cec5SDimitry Andric //
6440b57cec5SDimitry Andric // =>
6450b57cec5SDimitry Andric //
6460b57cec5SDimitry Andric // mov t, x (t is potentially dead and move eliminated)
6470b57cec5SDimitry Andric // v_swap_b32 x, y
6480b57cec5SDimitry Andric //
6490b57cec5SDimitry Andric // Returns next valid instruction pointer if was able to create v_swap_b32.
6500b57cec5SDimitry Andric //
6510b57cec5SDimitry Andric // This shall not be done too early not to prevent possible folding which may
65281ad6265SDimitry Andric // remove matched moves, and this should preferably be done before RA to
6530b57cec5SDimitry Andric // release saved registers and also possibly after RA which can insert copies
6540b57cec5SDimitry Andric // too.
6550b57cec5SDimitry Andric //
65681ad6265SDimitry Andric // This is really just a generic peephole that is not a canonical shrinking,
6570b57cec5SDimitry Andric // although requirements match the pass placement and it reduces code size too.
matchSwap(MachineInstr & MovT) const65881ad6265SDimitry Andric MachineInstr *SIShrinkInstructions::matchSwap(MachineInstr &MovT) const {
6590b57cec5SDimitry Andric assert(MovT.getOpcode() == AMDGPU::V_MOV_B32_e32 ||
6600b57cec5SDimitry Andric MovT.getOpcode() == AMDGPU::COPY);
6610b57cec5SDimitry Andric
6628bcb0991SDimitry Andric Register T = MovT.getOperand(0).getReg();
6630b57cec5SDimitry Andric unsigned Tsub = MovT.getOperand(0).getSubReg();
6640b57cec5SDimitry Andric MachineOperand &Xop = MovT.getOperand(1);
6650b57cec5SDimitry Andric
6660b57cec5SDimitry Andric if (!Xop.isReg())
6670b57cec5SDimitry Andric return nullptr;
6688bcb0991SDimitry Andric Register X = Xop.getReg();
6690b57cec5SDimitry Andric unsigned Xsub = Xop.getSubReg();
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andric unsigned Size = TII->getOpSize(MovT, 0) / 4;
6720b57cec5SDimitry Andric
67381ad6265SDimitry Andric if (!TRI->isVGPR(*MRI, X))
6740b57cec5SDimitry Andric return nullptr;
6750b57cec5SDimitry Andric
6765ffd83dbSDimitry Andric const unsigned SearchLimit = 16;
6775ffd83dbSDimitry Andric unsigned Count = 0;
678e8d8bef9SDimitry Andric bool KilledT = false;
6795ffd83dbSDimitry Andric for (auto Iter = std::next(MovT.getIterator()),
6805ffd83dbSDimitry Andric E = MovT.getParent()->instr_end();
681e8d8bef9SDimitry Andric Iter != E && Count < SearchLimit && !KilledT; ++Iter, ++Count) {
6825ffd83dbSDimitry Andric
6835ffd83dbSDimitry Andric MachineInstr *MovY = &*Iter;
68481ad6265SDimitry Andric KilledT = MovY->killsRegister(T, TRI);
685e8d8bef9SDimitry Andric
6865ffd83dbSDimitry Andric if ((MovY->getOpcode() != AMDGPU::V_MOV_B32_e32 &&
6875ffd83dbSDimitry Andric MovY->getOpcode() != AMDGPU::COPY) ||
6885ffd83dbSDimitry Andric !MovY->getOperand(1).isReg() ||
6895ffd83dbSDimitry Andric MovY->getOperand(1).getReg() != T ||
690bdd1243dSDimitry Andric MovY->getOperand(1).getSubReg() != Tsub)
6910b57cec5SDimitry Andric continue;
6920b57cec5SDimitry Andric
6935ffd83dbSDimitry Andric Register Y = MovY->getOperand(0).getReg();
6945ffd83dbSDimitry Andric unsigned Ysub = MovY->getOperand(0).getSubReg();
6950b57cec5SDimitry Andric
69681ad6265SDimitry Andric if (!TRI->isVGPR(*MRI, Y))
6970b57cec5SDimitry Andric continue;
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric MachineInstr *MovX = nullptr;
7005ffd83dbSDimitry Andric for (auto IY = MovY->getIterator(), I = std::next(MovT.getIterator());
7015ffd83dbSDimitry Andric I != IY; ++I) {
70281ad6265SDimitry Andric if (instReadsReg(&*I, X, Xsub) || instModifiesReg(&*I, Y, Ysub) ||
70381ad6265SDimitry Andric instModifiesReg(&*I, T, Tsub) ||
70481ad6265SDimitry Andric (MovX && instModifiesReg(&*I, X, Xsub))) {
7050b57cec5SDimitry Andric MovX = nullptr;
7060b57cec5SDimitry Andric break;
7070b57cec5SDimitry Andric }
70881ad6265SDimitry Andric if (!instReadsReg(&*I, Y, Ysub)) {
70981ad6265SDimitry Andric if (!MovX && instModifiesReg(&*I, X, Xsub)) {
7100b57cec5SDimitry Andric MovX = nullptr;
7110b57cec5SDimitry Andric break;
7120b57cec5SDimitry Andric }
7130b57cec5SDimitry Andric continue;
7140b57cec5SDimitry Andric }
7150b57cec5SDimitry Andric if (MovX ||
7160b57cec5SDimitry Andric (I->getOpcode() != AMDGPU::V_MOV_B32_e32 &&
7170b57cec5SDimitry Andric I->getOpcode() != AMDGPU::COPY) ||
7180b57cec5SDimitry Andric I->getOperand(0).getReg() != X ||
7190b57cec5SDimitry Andric I->getOperand(0).getSubReg() != Xsub) {
7200b57cec5SDimitry Andric MovX = nullptr;
7210b57cec5SDimitry Andric break;
7220b57cec5SDimitry Andric }
723e8d8bef9SDimitry Andric
724e8d8bef9SDimitry Andric if (Size > 1 && (I->getNumImplicitOperands() > (I->isCopy() ? 0U : 1U)))
725e8d8bef9SDimitry Andric continue;
726e8d8bef9SDimitry Andric
7270b57cec5SDimitry Andric MovX = &*I;
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric
7305ffd83dbSDimitry Andric if (!MovX)
7310b57cec5SDimitry Andric continue;
7320b57cec5SDimitry Andric
733e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Matched v_swap_b32:\n" << MovT << *MovX << *MovY);
7340b57cec5SDimitry Andric
7350b57cec5SDimitry Andric for (unsigned I = 0; I < Size; ++I) {
7360b57cec5SDimitry Andric TargetInstrInfo::RegSubRegPair X1, Y1;
73781ad6265SDimitry Andric X1 = getSubRegForIndex(X, Xsub, I);
73881ad6265SDimitry Andric Y1 = getSubRegForIndex(Y, Ysub, I);
739e8d8bef9SDimitry Andric MachineBasicBlock &MBB = *MovT.getParent();
740e8d8bef9SDimitry Andric auto MIB = BuildMI(MBB, MovX->getIterator(), MovT.getDebugLoc(),
7410b57cec5SDimitry Andric TII->get(AMDGPU::V_SWAP_B32))
7420b57cec5SDimitry Andric .addDef(X1.Reg, 0, X1.SubReg)
7430b57cec5SDimitry Andric .addDef(Y1.Reg, 0, Y1.SubReg)
7440b57cec5SDimitry Andric .addReg(Y1.Reg, 0, Y1.SubReg)
7450b57cec5SDimitry Andric .addReg(X1.Reg, 0, X1.SubReg).getInstr();
746e8d8bef9SDimitry Andric if (MovX->hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
747e8d8bef9SDimitry Andric // Drop implicit EXEC.
74881ad6265SDimitry Andric MIB->removeOperand(MIB->getNumExplicitOperands());
749e8d8bef9SDimitry Andric MIB->copyImplicitOps(*MBB.getParent(), *MovX);
750e8d8bef9SDimitry Andric }
7510b57cec5SDimitry Andric }
7520b57cec5SDimitry Andric MovX->eraseFromParent();
75381ad6265SDimitry Andric dropInstructionKeepingImpDefs(*MovY);
7540b57cec5SDimitry Andric MachineInstr *Next = &*std::next(MovT.getIterator());
755e8d8bef9SDimitry Andric
75681ad6265SDimitry Andric if (T.isVirtual() && MRI->use_nodbg_empty(T)) {
75781ad6265SDimitry Andric dropInstructionKeepingImpDefs(MovT);
758e8d8bef9SDimitry Andric } else {
7590b57cec5SDimitry Andric Xop.setIsKill(false);
760e8d8bef9SDimitry Andric for (int I = MovT.getNumImplicitOperands() - 1; I >= 0; --I ) {
761e8d8bef9SDimitry Andric unsigned OpNo = MovT.getNumExplicitOperands() + I;
762e8d8bef9SDimitry Andric const MachineOperand &Op = MovT.getOperand(OpNo);
76381ad6265SDimitry Andric if (Op.isKill() && TRI->regsOverlap(X, Op.getReg()))
76481ad6265SDimitry Andric MovT.removeOperand(OpNo);
765e8d8bef9SDimitry Andric }
766e8d8bef9SDimitry Andric }
7670b57cec5SDimitry Andric
7680b57cec5SDimitry Andric return Next;
7690b57cec5SDimitry Andric }
7700b57cec5SDimitry Andric
7710b57cec5SDimitry Andric return nullptr;
7720b57cec5SDimitry Andric }
7730b57cec5SDimitry Andric
77481ad6265SDimitry Andric // If an instruction has dead sdst replace it with NULL register on gfx1030+
tryReplaceDeadSDST(MachineInstr & MI) const77581ad6265SDimitry Andric bool SIShrinkInstructions::tryReplaceDeadSDST(MachineInstr &MI) const {
77681ad6265SDimitry Andric if (!ST->hasGFX10_3Insts())
77781ad6265SDimitry Andric return false;
77881ad6265SDimitry Andric
77981ad6265SDimitry Andric MachineOperand *Op = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
78081ad6265SDimitry Andric if (!Op)
78181ad6265SDimitry Andric return false;
78281ad6265SDimitry Andric Register SDstReg = Op->getReg();
78381ad6265SDimitry Andric if (SDstReg.isPhysical() || !MRI->use_nodbg_empty(SDstReg))
78481ad6265SDimitry Andric return false;
78581ad6265SDimitry Andric
78681ad6265SDimitry Andric Op->setReg(ST->isWave32() ? AMDGPU::SGPR_NULL : AMDGPU::SGPR_NULL64);
78781ad6265SDimitry Andric return true;
78881ad6265SDimitry Andric }
78981ad6265SDimitry Andric
runOnMachineFunction(MachineFunction & MF)7900b57cec5SDimitry Andric bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
7910b57cec5SDimitry Andric if (skipFunction(MF.getFunction()))
7920b57cec5SDimitry Andric return false;
7930b57cec5SDimitry Andric
794bdd1243dSDimitry Andric this->MF = &MF;
79581ad6265SDimitry Andric MRI = &MF.getRegInfo();
79681ad6265SDimitry Andric ST = &MF.getSubtarget<GCNSubtarget>();
79781ad6265SDimitry Andric TII = ST->getInstrInfo();
79881ad6265SDimitry Andric TRI = &TII->getRegisterInfo();
79981ad6265SDimitry Andric
80081ad6265SDimitry Andric unsigned VCCReg = ST->isWave32() ? AMDGPU::VCC_LO : AMDGPU::VCC;
8010b57cec5SDimitry Andric
8020b57cec5SDimitry Andric std::vector<unsigned> I1Defs;
8030b57cec5SDimitry Andric
8040b57cec5SDimitry Andric for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
8050b57cec5SDimitry Andric BI != BE; ++BI) {
8060b57cec5SDimitry Andric
8070b57cec5SDimitry Andric MachineBasicBlock &MBB = *BI;
8080b57cec5SDimitry Andric MachineBasicBlock::iterator I, Next;
8090b57cec5SDimitry Andric for (I = MBB.begin(); I != MBB.end(); I = Next) {
8100b57cec5SDimitry Andric Next = std::next(I);
8110b57cec5SDimitry Andric MachineInstr &MI = *I;
8120b57cec5SDimitry Andric
8130b57cec5SDimitry Andric if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
8140b57cec5SDimitry Andric // If this has a literal constant source that is the same as the
8150b57cec5SDimitry Andric // reversed bits of an inline immediate, replace with a bitreverse of
8160b57cec5SDimitry Andric // that constant. This saves 4 bytes in the common case of materializing
8170b57cec5SDimitry Andric // sign bits.
8180b57cec5SDimitry Andric
8190b57cec5SDimitry Andric // Test if we are after regalloc. We only want to do this after any
8200b57cec5SDimitry Andric // optimizations happen because this will confuse them.
8210b57cec5SDimitry Andric // XXX - not exactly a check for post-regalloc run.
8220b57cec5SDimitry Andric MachineOperand &Src = MI.getOperand(1);
823e8d8bef9SDimitry Andric if (Src.isImm() && MI.getOperand(0).getReg().isPhysical()) {
824*0fca6ea1SDimitry Andric int32_t ModImm;
825*0fca6ea1SDimitry Andric unsigned ModOpcode =
826*0fca6ea1SDimitry Andric canModifyToInlineImmOp32(TII, Src, ModImm, /*Scalar=*/false);
827*0fca6ea1SDimitry Andric if (ModOpcode != 0) {
828*0fca6ea1SDimitry Andric MI.setDesc(TII->get(ModOpcode));
829*0fca6ea1SDimitry Andric Src.setImm(static_cast<int64_t>(ModImm));
8300b57cec5SDimitry Andric continue;
8310b57cec5SDimitry Andric }
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric }
8340b57cec5SDimitry Andric
83581ad6265SDimitry Andric if (ST->hasSwap() && (MI.getOpcode() == AMDGPU::V_MOV_B32_e32 ||
8360b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::COPY)) {
83781ad6265SDimitry Andric if (auto *NextMI = matchSwap(MI)) {
8380b57cec5SDimitry Andric Next = NextMI->getIterator();
8390b57cec5SDimitry Andric continue;
8400b57cec5SDimitry Andric }
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric
84381ad6265SDimitry Andric // Try to use S_ADDK_I32 and S_MULK_I32.
8440b57cec5SDimitry Andric if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
8450b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::S_MUL_I32) {
8460b57cec5SDimitry Andric const MachineOperand *Dest = &MI.getOperand(0);
8470b57cec5SDimitry Andric MachineOperand *Src0 = &MI.getOperand(1);
8480b57cec5SDimitry Andric MachineOperand *Src1 = &MI.getOperand(2);
8490b57cec5SDimitry Andric
8500b57cec5SDimitry Andric if (!Src0->isReg() && Src1->isReg()) {
8510b57cec5SDimitry Andric if (TII->commuteInstruction(MI, false, 1, 2))
8520b57cec5SDimitry Andric std::swap(Src0, Src1);
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric
8550b57cec5SDimitry Andric // FIXME: This could work better if hints worked with subregisters. If
8560b57cec5SDimitry Andric // we have a vector add of a constant, we usually don't get the correct
8570b57cec5SDimitry Andric // allocation due to the subregister usage.
858e8d8bef9SDimitry Andric if (Dest->getReg().isVirtual() && Src0->isReg()) {
85981ad6265SDimitry Andric MRI->setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
86081ad6265SDimitry Andric MRI->setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
8610b57cec5SDimitry Andric continue;
8620b57cec5SDimitry Andric }
8630b57cec5SDimitry Andric
8640b57cec5SDimitry Andric if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
86581ad6265SDimitry Andric if (Src1->isImm() && isKImmOperand(*Src1)) {
8660b57cec5SDimitry Andric unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ?
8670b57cec5SDimitry Andric AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
8680b57cec5SDimitry Andric
8695f757f3fSDimitry Andric Src1->setImm(SignExtend64(Src1->getImm(), 32));
8700b57cec5SDimitry Andric MI.setDesc(TII->get(Opc));
8710b57cec5SDimitry Andric MI.tieOperands(0, 1);
8720b57cec5SDimitry Andric }
8730b57cec5SDimitry Andric }
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric
8760b57cec5SDimitry Andric // Try to use s_cmpk_*
8770b57cec5SDimitry Andric if (MI.isCompare() && TII->isSOPC(MI)) {
87881ad6265SDimitry Andric shrinkScalarCompare(MI);
8790b57cec5SDimitry Andric continue;
8800b57cec5SDimitry Andric }
8810b57cec5SDimitry Andric
8820b57cec5SDimitry Andric // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
8830b57cec5SDimitry Andric if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
8840b57cec5SDimitry Andric const MachineOperand &Dst = MI.getOperand(0);
8850b57cec5SDimitry Andric MachineOperand &Src = MI.getOperand(1);
8860b57cec5SDimitry Andric
887e8d8bef9SDimitry Andric if (Src.isImm() && Dst.getReg().isPhysical()) {
888*0fca6ea1SDimitry Andric unsigned ModOpc;
889*0fca6ea1SDimitry Andric int32_t ModImm;
8905f757f3fSDimitry Andric if (isKImmOperand(Src)) {
8910b57cec5SDimitry Andric MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
8925f757f3fSDimitry Andric Src.setImm(SignExtend64(Src.getImm(), 32));
893*0fca6ea1SDimitry Andric } else if ((ModOpc = canModifyToInlineImmOp32(TII, Src, ModImm,
894*0fca6ea1SDimitry Andric /*Scalar=*/true))) {
895*0fca6ea1SDimitry Andric MI.setDesc(TII->get(ModOpc));
896*0fca6ea1SDimitry Andric Src.setImm(static_cast<int64_t>(ModImm));
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric }
8990b57cec5SDimitry Andric
9000b57cec5SDimitry Andric continue;
9010b57cec5SDimitry Andric }
9020b57cec5SDimitry Andric
9030b57cec5SDimitry Andric // Shrink scalar logic operations.
9040b57cec5SDimitry Andric if (MI.getOpcode() == AMDGPU::S_AND_B32 ||
9050b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::S_OR_B32 ||
9060b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::S_XOR_B32) {
90781ad6265SDimitry Andric if (shrinkScalarLogicOp(MI))
9080b57cec5SDimitry Andric continue;
9090b57cec5SDimitry Andric }
9100b57cec5SDimitry Andric
9110b57cec5SDimitry Andric if (TII->isMIMG(MI.getOpcode()) &&
91281ad6265SDimitry Andric ST->getGeneration() >= AMDGPUSubtarget::GFX10 &&
9130b57cec5SDimitry Andric MF.getProperties().hasProperty(
9140b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs)) {
9150b57cec5SDimitry Andric shrinkMIMG(MI);
9160b57cec5SDimitry Andric continue;
9170b57cec5SDimitry Andric }
9180b57cec5SDimitry Andric
91981ad6265SDimitry Andric if (!TII->isVOP3(MI))
9200b57cec5SDimitry Andric continue;
9210b57cec5SDimitry Andric
92281ad6265SDimitry Andric if (MI.getOpcode() == AMDGPU::V_MAD_F32_e64 ||
92381ad6265SDimitry Andric MI.getOpcode() == AMDGPU::V_FMA_F32_e64 ||
92481ad6265SDimitry Andric MI.getOpcode() == AMDGPU::V_MAD_F16_e64 ||
925bdd1243dSDimitry Andric MI.getOpcode() == AMDGPU::V_FMA_F16_e64 ||
926bdd1243dSDimitry Andric MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_e64) {
92781ad6265SDimitry Andric shrinkMadFma(MI);
92881ad6265SDimitry Andric continue;
92981ad6265SDimitry Andric }
93081ad6265SDimitry Andric
93181ad6265SDimitry Andric if (!TII->hasVALU32BitEncoding(MI.getOpcode())) {
93281ad6265SDimitry Andric // If there is no chance we will shrink it and use VCC as sdst to get
93381ad6265SDimitry Andric // a 32 bit form try to replace dead sdst with NULL.
93481ad6265SDimitry Andric tryReplaceDeadSDST(MI);
93581ad6265SDimitry Andric continue;
93681ad6265SDimitry Andric }
93781ad6265SDimitry Andric
93881ad6265SDimitry Andric if (!TII->canShrink(MI, *MRI)) {
9390b57cec5SDimitry Andric // Try commuting the instruction and see if that enables us to shrink
9400b57cec5SDimitry Andric // it.
9410b57cec5SDimitry Andric if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
94281ad6265SDimitry Andric !TII->canShrink(MI, *MRI)) {
94381ad6265SDimitry Andric tryReplaceDeadSDST(MI);
9440b57cec5SDimitry Andric continue;
9450b57cec5SDimitry Andric }
94681ad6265SDimitry Andric }
9470b57cec5SDimitry Andric
9480b57cec5SDimitry Andric int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
9490b57cec5SDimitry Andric
9500b57cec5SDimitry Andric if (TII->isVOPC(Op32)) {
95181ad6265SDimitry Andric MachineOperand &Op0 = MI.getOperand(0);
95281ad6265SDimitry Andric if (Op0.isReg()) {
95381ad6265SDimitry Andric // Exclude VOPCX instructions as these don't explicitly write a
95481ad6265SDimitry Andric // dst.
95581ad6265SDimitry Andric Register DstReg = Op0.getReg();
956e8d8bef9SDimitry Andric if (DstReg.isVirtual()) {
9570b57cec5SDimitry Andric // VOPC instructions can only write to the VCC register. We can't
9580b57cec5SDimitry Andric // force them to use VCC here, because this is only one register and
9590b57cec5SDimitry Andric // cannot deal with sequences which would require multiple copies of
9600b57cec5SDimitry Andric // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
9610b57cec5SDimitry Andric //
96281ad6265SDimitry Andric // So, instead of forcing the instruction to write to VCC, we
96381ad6265SDimitry Andric // provide a hint to the register allocator to use VCC and then we
96481ad6265SDimitry Andric // will run this pass again after RA and shrink it if it outputs to
96581ad6265SDimitry Andric // VCC.
96681ad6265SDimitry Andric MRI->setRegAllocationHint(DstReg, 0, VCCReg);
9670b57cec5SDimitry Andric continue;
9680b57cec5SDimitry Andric }
9690b57cec5SDimitry Andric if (DstReg != VCCReg)
9700b57cec5SDimitry Andric continue;
9710b57cec5SDimitry Andric }
97281ad6265SDimitry Andric }
9730b57cec5SDimitry Andric
9740b57cec5SDimitry Andric if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
9750b57cec5SDimitry Andric // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
9760b57cec5SDimitry Andric // instructions.
9770b57cec5SDimitry Andric const MachineOperand *Src2 =
9780b57cec5SDimitry Andric TII->getNamedOperand(MI, AMDGPU::OpName::src2);
9790b57cec5SDimitry Andric if (!Src2->isReg())
9800b57cec5SDimitry Andric continue;
9818bcb0991SDimitry Andric Register SReg = Src2->getReg();
982e8d8bef9SDimitry Andric if (SReg.isVirtual()) {
98381ad6265SDimitry Andric MRI->setRegAllocationHint(SReg, 0, VCCReg);
9840b57cec5SDimitry Andric continue;
9850b57cec5SDimitry Andric }
9860b57cec5SDimitry Andric if (SReg != VCCReg)
9870b57cec5SDimitry Andric continue;
9880b57cec5SDimitry Andric }
9890b57cec5SDimitry Andric
9900b57cec5SDimitry Andric // Check for the bool flag output for instructions like V_ADD_I32_e64.
9910b57cec5SDimitry Andric const MachineOperand *SDst = TII->getNamedOperand(MI,
9920b57cec5SDimitry Andric AMDGPU::OpName::sdst);
9930b57cec5SDimitry Andric
9940b57cec5SDimitry Andric if (SDst) {
9950b57cec5SDimitry Andric bool Next = false;
9960b57cec5SDimitry Andric
9970b57cec5SDimitry Andric if (SDst->getReg() != VCCReg) {
998e8d8bef9SDimitry Andric if (SDst->getReg().isVirtual())
99981ad6265SDimitry Andric MRI->setRegAllocationHint(SDst->getReg(), 0, VCCReg);
10000b57cec5SDimitry Andric Next = true;
10010b57cec5SDimitry Andric }
10020b57cec5SDimitry Andric
10030b57cec5SDimitry Andric // All of the instructions with carry outs also have an SGPR input in
10040b57cec5SDimitry Andric // src2.
10050eae32dcSDimitry Andric const MachineOperand *Src2 = TII->getNamedOperand(MI,
10060eae32dcSDimitry Andric AMDGPU::OpName::src2);
10070b57cec5SDimitry Andric if (Src2 && Src2->getReg() != VCCReg) {
1008e8d8bef9SDimitry Andric if (Src2->getReg().isVirtual())
100981ad6265SDimitry Andric MRI->setRegAllocationHint(Src2->getReg(), 0, VCCReg);
10100b57cec5SDimitry Andric Next = true;
10110b57cec5SDimitry Andric }
10120b57cec5SDimitry Andric
10130b57cec5SDimitry Andric if (Next)
10140b57cec5SDimitry Andric continue;
10150b57cec5SDimitry Andric }
10160b57cec5SDimitry Andric
1017bdd1243dSDimitry Andric // Pre-GFX10, shrinking VOP3 instructions pre-RA gave us the chance to
1018bdd1243dSDimitry Andric // fold an immediate into the shrunk instruction as a literal operand. In
1019bdd1243dSDimitry Andric // GFX10 VOP3 instructions can take a literal operand anyway, so there is
1020bdd1243dSDimitry Andric // no advantage to doing this.
1021bdd1243dSDimitry Andric if (ST->hasVOP3Literal() &&
1022bdd1243dSDimitry Andric !MF.getProperties().hasProperty(
1023bdd1243dSDimitry Andric MachineFunctionProperties::Property::NoVRegs))
1024bdd1243dSDimitry Andric continue;
1025bdd1243dSDimitry Andric
1026bdd1243dSDimitry Andric if (ST->hasTrue16BitInsts() && AMDGPU::isTrue16Inst(MI.getOpcode()) &&
1027bdd1243dSDimitry Andric !shouldShrinkTrue16(MI))
1028bdd1243dSDimitry Andric continue;
1029bdd1243dSDimitry Andric
10300b57cec5SDimitry Andric // We can shrink this instruction
10310b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Shrinking " << MI);
10320b57cec5SDimitry Andric
10330b57cec5SDimitry Andric MachineInstr *Inst32 = TII->buildShrunkInst(MI, Op32);
10340b57cec5SDimitry Andric ++NumInstructionsShrunk;
10350b57cec5SDimitry Andric
10360b57cec5SDimitry Andric // Copy extra operands not present in the instruction definition.
103781ad6265SDimitry Andric copyExtraImplicitOps(*Inst32, MI);
10380b57cec5SDimitry Andric
1039349cc55cSDimitry Andric // Copy deadness from the old explicit vcc def to the new implicit def.
1040349cc55cSDimitry Andric if (SDst && SDst->isDead())
1041*0fca6ea1SDimitry Andric Inst32->findRegisterDefOperand(VCCReg, /*TRI=*/nullptr)->setIsDead();
1042349cc55cSDimitry Andric
10430b57cec5SDimitry Andric MI.eraseFromParent();
104481ad6265SDimitry Andric foldImmediates(*Inst32);
10450b57cec5SDimitry Andric
10460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
10470b57cec5SDimitry Andric }
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric return false;
10500b57cec5SDimitry Andric }
1051