1 //===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 /// The pass tries to use the 32-bit encoding for instructions when possible.
9 //===----------------------------------------------------------------------===//
13 #include "AMDGPUMCInstLower.h"
14 #include "AMDGPUSubtarget.h"
15 #include "SIInstrInfo.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetMachine.h"
27 #define DEBUG_TYPE "si-shrink-instructions"
29 STATISTIC(NumInstructionsShrunk,
30 "Number of 64-bit instruction reduced to 32-bit.");
31 STATISTIC(NumLiteralConstantsFolded,
32 "Number of literal constants folded into 32-bit instructions.");
35 void initializeSIShrinkInstructionsPass(PassRegistry&);
42 class SIShrinkInstructions : public MachineFunctionPass {
47 SIShrinkInstructions() : MachineFunctionPass(ID) {
50 bool runOnMachineFunction(MachineFunction &MF) override;
52 const char *getPassName() const override {
53 return "SI Shrink Instructions";
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
58 MachineFunctionPass::getAnalysisUsage(AU);
62 } // End anonymous namespace.
64 INITIALIZE_PASS_BEGIN(SIShrinkInstructions, DEBUG_TYPE,
65 "SI Lower il Copies", false, false)
66 INITIALIZE_PASS_END(SIShrinkInstructions, DEBUG_TYPE,
67 "SI Lower il Copies", false, false)
69 char SIShrinkInstructions::ID = 0;
71 FunctionPass *llvm::createSIShrinkInstructionsPass() {
72 return new SIShrinkInstructions();
75 static bool isVGPR(const MachineOperand *MO, const SIRegisterInfo &TRI,
76 const MachineRegisterInfo &MRI) {
80 if (TargetRegisterInfo::isVirtualRegister(MO->getReg()))
81 return TRI.hasVGPRs(MRI.getRegClass(MO->getReg()));
83 return TRI.hasVGPRs(TRI.getPhysRegClass(MO->getReg()));
86 static bool canShrink(MachineInstr &MI, const SIInstrInfo *TII,
87 const SIRegisterInfo &TRI,
88 const MachineRegisterInfo &MRI) {
90 const MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
91 // Can't shrink instruction with three operands.
92 // FIXME: v_cndmask_b32 has 3 operands and is shrinkable, but we need to add
93 // a special case for it. It can only be shrunk if the third operand
94 // is vcc. We should handle this the same way we handle vopc, by addding
95 // a register allocation hint pre-regalloc and then do the shrining
98 if (MI.getOpcode() != AMDGPU::V_MAC_F32_e64)
101 const MachineOperand *Src2Mod =
102 TII->getNamedOperand(MI, AMDGPU::OpName::src2_modifiers);
103 if (!isVGPR(Src2, TRI, MRI) || (Src2Mod && Src2Mod->getImm() != 0))
107 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
108 const MachineOperand *Src1Mod =
109 TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
111 if (Src1 && (!isVGPR(Src1, TRI, MRI) || (Src1Mod && Src1Mod->getImm() != 0)))
114 // We don't need to check src0, all input types are legal, so just make sure
115 // src0 isn't using any modifiers.
116 if (TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
119 // Check output modifiers
120 if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
123 if (TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
129 /// \brief This function checks \p MI for operands defined by a move immediate
130 /// instruction and then folds the literal constant into the instruction if it
131 /// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instruction
132 /// and will only fold literal constants if we are still in SSA.
133 static void foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
134 MachineRegisterInfo &MRI, bool TryToCommute = true) {
139 assert(TII->isVOP1(MI.getOpcode()) || TII->isVOP2(MI.getOpcode()) ||
140 TII->isVOPC(MI.getOpcode()));
142 const SIRegisterInfo &TRI = TII->getRegisterInfo();
143 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
144 MachineOperand &Src0 = MI.getOperand(Src0Idx);
146 // Only one literal constant is allowed per instruction, so if src0 is a
147 // literal constant then we can't do any folding.
149 TII->isLiteralConstant(Src0, TII->getOpSize(MI, Src0Idx)))
152 // Literal constants and SGPRs can only be used in Src0, so if Src0 is an
153 // SGPR, we cannot commute the instruction, so we can't fold any literal
155 if (Src0.isReg() && !isVGPR(&Src0, TRI, MRI))
159 if (Src0.isReg() && MRI.hasOneUse(Src0.getReg())) {
160 unsigned Reg = Src0.getReg();
161 MachineInstr *Def = MRI.getUniqueVRegDef(Reg);
162 if (Def && Def->isMoveImmediate()) {
163 MachineOperand &MovSrc = Def->getOperand(1);
164 bool ConstantFolded = false;
166 if (MovSrc.isImm() && isUInt<32>(MovSrc.getImm())) {
167 Src0.ChangeToImmediate(MovSrc.getImm());
168 ConstantFolded = true;
170 if (ConstantFolded) {
171 if (MRI.use_empty(Reg))
172 Def->eraseFromParent();
173 ++NumLiteralConstantsFolded;
179 // We have failed to fold src0, so commute the instruction and try again.
180 if (TryToCommute && MI.isCommutable() && TII->commuteInstruction(&MI))
181 foldImmediates(MI, TII, MRI, false);
185 bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
186 MachineRegisterInfo &MRI = MF.getRegInfo();
187 const SIInstrInfo *TII =
188 static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
189 const SIRegisterInfo &TRI = TII->getRegisterInfo();
190 std::vector<unsigned> I1Defs;
192 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
195 MachineBasicBlock &MBB = *BI;
196 MachineBasicBlock::iterator I, Next;
197 for (I = MBB.begin(); I != MBB.end(); I = Next) {
199 MachineInstr &MI = *I;
201 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
202 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
203 const MachineOperand &Src = MI.getOperand(1);
206 if (isInt<16>(Src.getImm()) && !TII->isInlineConstant(Src, 4))
207 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
213 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
216 if (!canShrink(MI, TII, TRI, MRI)) {
217 // Try commuting the instruction and see if that enables us to shrink
219 if (!MI.isCommutable() || !TII->commuteInstruction(&MI) ||
220 !canShrink(MI, TII, TRI, MRI))
224 // getVOPe32 could be -1 here if we started with an instruction that had
225 // a 32-bit encoding and then commuted it to an instruction that did not.
226 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
229 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
231 if (TII->isVOPC(Op32)) {
232 unsigned DstReg = MI.getOperand(0).getReg();
233 if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
234 // VOPC instructions can only write to the VCC register. We can't
235 // force them to use VCC here, because the register allocator has
236 // trouble with sequences like this, which cause the allocator to run
237 // out of registers if vreg0 and vreg1 belong to the VCCReg register
241 // S_AND_B64 vreg0, vreg1
243 // So, instead of forcing the instruction to write to VCC, we provide
244 // a hint to the register allocator to use VCC and then we we will run
245 // this pass again after RA and shrink it if it outputs to VCC.
246 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
249 if (DstReg != AMDGPU::VCC)
253 // We can shrink this instruction
254 DEBUG(dbgs() << "Shrinking "; MI.dump(); dbgs() << '\n';);
256 MachineInstrBuilder Inst32 =
257 BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
260 Inst32.addOperand(MI.getOperand(0));
262 Inst32.addOperand(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
264 const MachineOperand *Src1 =
265 TII->getNamedOperand(MI, AMDGPU::OpName::src1);
267 Inst32.addOperand(*Src1);
269 const MachineOperand *Src2 =
270 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
272 Inst32.addOperand(*Src2);
274 ++NumInstructionsShrunk;
275 MI.eraseFromParent();
277 foldImmediates(*Inst32, TII, MRI);
278 DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');