blob: 41f989ad32280ccc89c97acbd3454be88fef0879 [file] [log] [blame]
Tom Stellard1aaad692014-07-21 16:55:33 +00001//===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8/// The pass tries to use the 32-bit encoding for instructions when possible.
9//===----------------------------------------------------------------------===//
10//
11
12#include "AMDGPU.h"
Marek Olsaka93603d2015-01-15 18:42:51 +000013#include "AMDGPUMCInstLower.h"
Eric Christopherd9134482014-08-04 21:25:23 +000014#include "AMDGPUSubtarget.h"
Tom Stellard1aaad692014-07-21 16:55:33 +000015#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"
Tom Stellard6407e1e2014-08-01 00:32:33 +000020#include "llvm/IR/Constants.h"
Tom Stellard1aaad692014-07-21 16:55:33 +000021#include "llvm/IR/Function.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000022#include "llvm/IR/LLVMContext.h"
Tom Stellard1aaad692014-07-21 16:55:33 +000023#include "llvm/Support/Debug.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000024#include "llvm/Support/raw_ostream.h"
Tom Stellard1aaad692014-07-21 16:55:33 +000025#include "llvm/Target/TargetMachine.h"
26
27#define DEBUG_TYPE "si-shrink-instructions"
28
29STATISTIC(NumInstructionsShrunk,
30 "Number of 64-bit instruction reduced to 32-bit.");
Tom Stellard6407e1e2014-08-01 00:32:33 +000031STATISTIC(NumLiteralConstantsFolded,
32 "Number of literal constants folded into 32-bit instructions.");
Tom Stellard1aaad692014-07-21 16:55:33 +000033
Tom Stellard1aaad692014-07-21 16:55:33 +000034using namespace llvm;
35
36namespace {
37
38class SIShrinkInstructions : public MachineFunctionPass {
39public:
40 static char ID;
41
42public:
43 SIShrinkInstructions() : MachineFunctionPass(ID) {
44 }
45
Craig Topperfd38cbe2014-08-30 16:48:34 +000046 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard1aaad692014-07-21 16:55:33 +000047
Mehdi Amini117296c2016-10-01 02:56:57 +000048 StringRef getPassName() const override { return "SI Shrink Instructions"; }
Tom Stellard1aaad692014-07-21 16:55:33 +000049
Craig Topperfd38cbe2014-08-30 16:48:34 +000050 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard1aaad692014-07-21 16:55:33 +000051 AU.setPreservesCFG();
52 MachineFunctionPass::getAnalysisUsage(AU);
53 }
54};
55
56} // End anonymous namespace.
57
Matt Arsenaultc3a01ec2016-06-09 23:18:47 +000058INITIALIZE_PASS(SIShrinkInstructions, DEBUG_TYPE,
59 "SI Shrink Instructions", false, false)
Tom Stellard1aaad692014-07-21 16:55:33 +000060
61char SIShrinkInstructions::ID = 0;
62
63FunctionPass *llvm::createSIShrinkInstructionsPass() {
64 return new SIShrinkInstructions();
65}
66
67static bool isVGPR(const MachineOperand *MO, const SIRegisterInfo &TRI,
68 const MachineRegisterInfo &MRI) {
69 if (!MO->isReg())
70 return false;
71
72 if (TargetRegisterInfo::isVirtualRegister(MO->getReg()))
73 return TRI.hasVGPRs(MRI.getRegClass(MO->getReg()));
74
75 return TRI.hasVGPRs(TRI.getPhysRegClass(MO->getReg()));
76}
77
78static bool canShrink(MachineInstr &MI, const SIInstrInfo *TII,
79 const SIRegisterInfo &TRI,
80 const MachineRegisterInfo &MRI) {
81
82 const MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
83 // Can't shrink instruction with three operands.
Tom Stellard5224df32015-03-10 16:16:44 +000084 // FIXME: v_cndmask_b32 has 3 operands and is shrinkable, but we need to add
85 // a special case for it. It can only be shrunk if the third operand
86 // is vcc. We should handle this the same way we handle vopc, by addding
Matt Arsenault28bd4cb2017-01-11 22:35:17 +000087 // a register allocation hint pre-regalloc and then do the shrinking
Tom Stellard5224df32015-03-10 16:16:44 +000088 // post-regalloc.
Tom Stellarddb5a11f2015-07-13 15:47:57 +000089 if (Src2) {
Tom Stellarde48fe2a2015-07-14 14:15:03 +000090 switch (MI.getOpcode()) {
91 default: return false;
Tom Stellarddb5a11f2015-07-13 15:47:57 +000092
Matt Arsenault24a12732017-01-11 22:58:12 +000093 case AMDGPU::V_ADDC_U32_e64:
94 case AMDGPU::V_SUBB_U32_e64:
Stanislav Mekhanoshina9d846c2017-06-20 20:33:44 +000095 if (TII->getNamedOperand(MI, AMDGPU::OpName::src1)->isImm())
96 return false;
Matt Arsenault24a12732017-01-11 22:58:12 +000097 // Additional verification is needed for sdst/src2.
98 return true;
99
Tom Stellarde48fe2a2015-07-14 14:15:03 +0000100 case AMDGPU::V_MAC_F32_e64:
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000101 case AMDGPU::V_MAC_F16_e64:
Tom Stellarde48fe2a2015-07-14 14:15:03 +0000102 if (!isVGPR(Src2, TRI, MRI) ||
103 TII->hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers))
104 return false;
105 break;
106
107 case AMDGPU::V_CNDMASK_B32_e64:
108 break;
109 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000110 }
Tom Stellard1aaad692014-07-21 16:55:33 +0000111
112 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
Matt Arsenaulta81198d2017-07-06 20:56:59 +0000113 if (Src1 && (!isVGPR(Src1, TRI, MRI) ||
114 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers)))
Tom Stellard1aaad692014-07-21 16:55:33 +0000115 return false;
116
Matt Arsenault8943d242014-10-17 18:00:45 +0000117 // We don't need to check src0, all input types are legal, so just make sure
118 // src0 isn't using any modifiers.
119 if (TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
Tom Stellard1aaad692014-07-21 16:55:33 +0000120 return false;
121
122 // Check output modifiers
Matt Arsenaulta81198d2017-07-06 20:56:59 +0000123 return !TII->hasModifiersSet(MI, AMDGPU::OpName::omod) &&
124 !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp);
Tom Stellard1aaad692014-07-21 16:55:33 +0000125}
126
Tom Stellard6407e1e2014-08-01 00:32:33 +0000127/// \brief This function checks \p MI for operands defined by a move immediate
128/// instruction and then folds the literal constant into the instruction if it
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000129/// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instructions.
130static bool foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
Tom Stellard6407e1e2014-08-01 00:32:33 +0000131 MachineRegisterInfo &MRI, bool TryToCommute = true) {
Matt Arsenault3add6432015-10-20 04:35:43 +0000132 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
Tom Stellard6407e1e2014-08-01 00:32:33 +0000133
Matt Arsenault11a4d672015-02-13 19:05:03 +0000134 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
Tom Stellard6407e1e2014-08-01 00:32:33 +0000135
Tom Stellard6407e1e2014-08-01 00:32:33 +0000136 // Try to fold Src0
Matt Arsenault4bd72362016-12-10 00:39:12 +0000137 MachineOperand &Src0 = MI.getOperand(Src0Idx);
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000138 if (Src0.isReg()) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000139 unsigned Reg = Src0.getReg();
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000140 if (TargetRegisterInfo::isVirtualRegister(Reg) && MRI.hasOneUse(Reg)) {
141 MachineInstr *Def = MRI.getUniqueVRegDef(Reg);
142 if (Def && Def->isMoveImmediate()) {
143 MachineOperand &MovSrc = Def->getOperand(1);
144 bool ConstantFolded = false;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000145
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000146 if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) ||
147 isUInt<32>(MovSrc.getImm()))) {
148 // It's possible to have only one component of a super-reg defined by
149 // a single mov, so we need to clear any subregister flag.
150 Src0.setSubReg(0);
151 Src0.ChangeToImmediate(MovSrc.getImm());
152 ConstantFolded = true;
Matt Arsenault9cff06f2017-07-10 20:04:35 +0000153 } else if (MovSrc.isFI()) {
154 Src0.setSubReg(0);
155 Src0.ChangeToFrameIndex(MovSrc.getIndex());
156 ConstantFolded = true;
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000157 }
158
159 if (ConstantFolded) {
160 assert(MRI.use_empty(Reg));
Tom Stellard6407e1e2014-08-01 00:32:33 +0000161 Def->eraseFromParent();
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000162 ++NumLiteralConstantsFolded;
163 return true;
164 }
Tom Stellard6407e1e2014-08-01 00:32:33 +0000165 }
166 }
167 }
168
169 // We have failed to fold src0, so commute the instruction and try again.
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000170 if (TryToCommute && MI.isCommutable()) {
171 if (TII->commuteInstruction(MI)) {
172 if (foldImmediates(MI, TII, MRI, false))
173 return true;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000174
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000175 // Commute back.
176 TII->commuteInstruction(MI);
177 }
178 }
179
180 return false;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000181}
182
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000183// Copy MachineOperand with all flags except setting it as implicit.
Matt Arsenault22096252016-06-20 18:34:00 +0000184static void copyFlagsToImplicitVCC(MachineInstr &MI,
185 const MachineOperand &Orig) {
186
187 for (MachineOperand &Use : MI.implicit_operands()) {
Matt Arsenault24a12732017-01-11 22:58:12 +0000188 if (Use.isUse() && Use.getReg() == AMDGPU::VCC) {
Matt Arsenault22096252016-06-20 18:34:00 +0000189 Use.setIsUndef(Orig.isUndef());
190 Use.setIsKill(Orig.isKill());
191 return;
192 }
193 }
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000194}
195
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000196static bool isKImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000197 return isInt<16>(Src.getImm()) &&
198 !TII->isInlineConstant(*Src.getParent(),
199 Src.getParent()->getOperandNo(&Src));
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000200}
201
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000202static bool isKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000203 return isUInt<16>(Src.getImm()) &&
204 !TII->isInlineConstant(*Src.getParent(),
205 Src.getParent()->getOperandNo(&Src));
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000206}
207
208static bool isKImmOrKUImmOperand(const SIInstrInfo *TII,
209 const MachineOperand &Src,
210 bool &IsUnsigned) {
211 if (isInt<16>(Src.getImm())) {
212 IsUnsigned = false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000213 return !TII->isInlineConstant(Src);
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000214 }
215
216 if (isUInt<16>(Src.getImm())) {
217 IsUnsigned = true;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000218 return !TII->isInlineConstant(Src);
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000219 }
220
221 return false;
222}
223
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000224/// \returns true if the constant in \p Src should be replaced with a bitreverse
225/// of an inline immediate.
226static bool isReverseInlineImm(const SIInstrInfo *TII,
227 const MachineOperand &Src,
228 int32_t &ReverseImm) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000229 if (!isInt<32>(Src.getImm()) || TII->isInlineConstant(Src))
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000230 return false;
231
232 ReverseImm = reverseBits<int32_t>(static_cast<int32_t>(Src.getImm()));
233 return ReverseImm >= -16 && ReverseImm <= 64;
234}
235
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000236/// Copy implicit register operands from specified instruction to this
237/// instruction that are not part of the instruction definition.
238static void copyExtraImplicitOps(MachineInstr &NewMI, MachineFunction &MF,
239 const MachineInstr &MI) {
240 for (unsigned i = MI.getDesc().getNumOperands() +
241 MI.getDesc().getNumImplicitUses() +
242 MI.getDesc().getNumImplicitDefs(), e = MI.getNumOperands();
243 i != e; ++i) {
244 const MachineOperand &MO = MI.getOperand(i);
245 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
246 NewMI.addOperand(MF, MO);
247 }
248}
249
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000250static void shrinkScalarCompare(const SIInstrInfo *TII, MachineInstr &MI) {
251 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
252 // get constants on the RHS.
253 if (!MI.getOperand(0).isReg())
254 TII->commuteInstruction(MI, false, 0, 1);
255
256 const MachineOperand &Src1 = MI.getOperand(1);
257 if (!Src1.isImm())
258 return;
259
260 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
261 if (SOPKOpc == -1)
262 return;
263
264 // eq/ne is special because the imm16 can be treated as signed or unsigned,
Matt Arsenault5d8eb252016-09-30 01:50:20 +0000265 // and initially selectd to the unsigned versions.
266 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000267 bool HasUImm;
268 if (isKImmOrKUImmOperand(TII, Src1, HasUImm)) {
Matt Arsenault5d8eb252016-09-30 01:50:20 +0000269 if (!HasUImm) {
270 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
271 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000272 }
273
274 MI.setDesc(TII->get(SOPKOpc));
275 }
276
277 return;
278 }
279
280 const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
281
282 if ((TII->sopkIsZext(SOPKOpc) && isKUImmOperand(TII, Src1)) ||
283 (!TII->sopkIsZext(SOPKOpc) && isKImmOperand(TII, Src1))) {
284 MI.setDesc(NewDesc);
285 }
286}
287
Tom Stellard1aaad692014-07-21 16:55:33 +0000288bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000289 if (skipFunction(MF.getFunction()))
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000290 return false;
291
Tom Stellard1aaad692014-07-21 16:55:33 +0000292 MachineRegisterInfo &MRI = MF.getRegInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000293 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
294 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard1aaad692014-07-21 16:55:33 +0000295 const SIRegisterInfo &TRI = TII->getRegisterInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000296
Tom Stellard1aaad692014-07-21 16:55:33 +0000297 std::vector<unsigned> I1Defs;
298
299 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
300 BI != BE; ++BI) {
301
302 MachineBasicBlock &MBB = *BI;
303 MachineBasicBlock::iterator I, Next;
304 for (I = MBB.begin(); I != MBB.end(); I = Next) {
305 Next = std::next(I);
306 MachineInstr &MI = *I;
307
Matt Arsenault9a19c242016-03-11 07:42:49 +0000308 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
309 // If this has a literal constant source that is the same as the
310 // reversed bits of an inline immediate, replace with a bitreverse of
311 // that constant. This saves 4 bytes in the common case of materializing
312 // sign bits.
313
314 // Test if we are after regalloc. We only want to do this after any
315 // optimizations happen because this will confuse them.
316 // XXX - not exactly a check for post-regalloc run.
317 MachineOperand &Src = MI.getOperand(1);
318 if (Src.isImm() &&
319 TargetRegisterInfo::isPhysicalRegister(MI.getOperand(0).getReg())) {
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000320 int32_t ReverseImm;
321 if (isReverseInlineImm(TII, Src, ReverseImm)) {
322 MI.setDesc(TII->get(AMDGPU::V_BFREV_B32_e32));
323 Src.setImm(ReverseImm);
324 continue;
Matt Arsenault9a19c242016-03-11 07:42:49 +0000325 }
326 }
327 }
328
Matt Arsenault074ea282016-04-25 19:53:22 +0000329 // Combine adjacent s_nops to use the immediate operand encoding how long
330 // to wait.
331 //
332 // s_nop N
333 // s_nop M
334 // =>
335 // s_nop (N + M)
336 if (MI.getOpcode() == AMDGPU::S_NOP &&
337 Next != MBB.end() &&
338 (*Next).getOpcode() == AMDGPU::S_NOP) {
339
340 MachineInstr &NextMI = *Next;
341 // The instruction encodes the amount to wait with an offset of 1,
342 // i.e. 0 is wait 1 cycle. Convert both to cycles and then convert back
343 // after adding.
344 uint8_t Nop0 = MI.getOperand(0).getImm() + 1;
345 uint8_t Nop1 = NextMI.getOperand(0).getImm() + 1;
346
347 // Make sure we don't overflow the bounds.
348 if (Nop0 + Nop1 <= 8) {
349 NextMI.getOperand(0).setImm(Nop0 + Nop1 - 1);
350 MI.eraseFromParent();
351 }
352
353 continue;
354 }
355
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000356 // FIXME: We also need to consider movs of constant operands since
357 // immediate operands are not folded if they have more than one use, and
358 // the operand folding pass is unaware if the immediate will be free since
359 // it won't know if the src == dest constraint will end up being
360 // satisfied.
361 if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
362 MI.getOpcode() == AMDGPU::S_MUL_I32) {
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000363 const MachineOperand *Dest = &MI.getOperand(0);
364 MachineOperand *Src0 = &MI.getOperand(1);
365 MachineOperand *Src1 = &MI.getOperand(2);
366
367 if (!Src0->isReg() && Src1->isReg()) {
368 if (TII->commuteInstruction(MI, false, 1, 2))
369 std::swap(Src0, Src1);
370 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000371
372 // FIXME: This could work better if hints worked with subregisters. If
373 // we have a vector add of a constant, we usually don't get the correct
374 // allocation due to the subregister usage.
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000375 if (TargetRegisterInfo::isVirtualRegister(Dest->getReg()) &&
376 Src0->isReg()) {
377 MRI.setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
378 MRI.setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000379 continue;
380 }
381
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000382 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
383 if (Src1->isImm() && isKImmOperand(TII, *Src1)) {
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000384 unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ?
385 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
386
387 MI.setDesc(TII->get(Opc));
388 MI.tieOperands(0, 1);
389 }
390 }
391 }
392
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000393 // Try to use s_cmpk_*
394 if (MI.isCompare() && TII->isSOPC(MI)) {
395 shrinkScalarCompare(TII, MI);
396 continue;
397 }
398
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000399 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
400 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000401 const MachineOperand &Dst = MI.getOperand(0);
402 MachineOperand &Src = MI.getOperand(1);
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000403
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000404 if (Src.isImm() &&
405 TargetRegisterInfo::isPhysicalRegister(Dst.getReg())) {
406 int32_t ReverseImm;
407 if (isKImmOperand(TII, Src))
408 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
409 else if (isReverseInlineImm(TII, Src, ReverseImm)) {
410 MI.setDesc(TII->get(AMDGPU::S_BREV_B32));
411 Src.setImm(ReverseImm);
412 }
413 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000414
415 continue;
416 }
417
Tom Stellard86d12eb2014-08-01 00:32:28 +0000418 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard1aaad692014-07-21 16:55:33 +0000419 continue;
420
421 if (!canShrink(MI, TII, TRI, MRI)) {
Matt Arsenault66524032014-09-16 18:00:23 +0000422 // Try commuting the instruction and see if that enables us to shrink
Tom Stellard1aaad692014-07-21 16:55:33 +0000423 // it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000424 if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
Tom Stellard1aaad692014-07-21 16:55:33 +0000425 !canShrink(MI, TII, TRI, MRI))
426 continue;
427 }
428
Marek Olsaka93603d2015-01-15 18:42:51 +0000429 // getVOPe32 could be -1 here if we started with an instruction that had
Tom Stellard86d12eb2014-08-01 00:32:28 +0000430 // a 32-bit encoding and then commuted it to an instruction that did not.
Marek Olsaka93603d2015-01-15 18:42:51 +0000431 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard86d12eb2014-08-01 00:32:28 +0000432 continue;
433
Marek Olsaka93603d2015-01-15 18:42:51 +0000434 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
435
Tom Stellard1aaad692014-07-21 16:55:33 +0000436 if (TII->isVOPC(Op32)) {
437 unsigned DstReg = MI.getOperand(0).getReg();
438 if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000439 // VOPC instructions can only write to the VCC register. We can't
440 // force them to use VCC here, because this is only one register and
441 // cannot deal with sequences which would require multiple copies of
442 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
Tom Stellard1aaad692014-07-21 16:55:33 +0000443 //
Matt Arsenaulta9627ae2014-09-21 17:27:32 +0000444 // So, instead of forcing the instruction to write to VCC, we provide
445 // a hint to the register allocator to use VCC and then we we will run
446 // this pass again after RA and shrink it if it outputs to VCC.
Tom Stellard1aaad692014-07-21 16:55:33 +0000447 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
448 continue;
449 }
450 if (DstReg != AMDGPU::VCC)
451 continue;
452 }
453
Tom Stellarde48fe2a2015-07-14 14:15:03 +0000454 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
455 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
456 // instructions.
457 const MachineOperand *Src2 =
458 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
459 if (!Src2->isReg())
460 continue;
461 unsigned SReg = Src2->getReg();
462 if (TargetRegisterInfo::isVirtualRegister(SReg)) {
463 MRI.setRegAllocationHint(SReg, 0, AMDGPU::VCC);
464 continue;
465 }
466 if (SReg != AMDGPU::VCC)
467 continue;
468 }
469
Matt Arsenault28bd4cb2017-01-11 22:35:17 +0000470 // Check for the bool flag output for instructions like V_ADD_I32_e64.
471 const MachineOperand *SDst = TII->getNamedOperand(MI,
472 AMDGPU::OpName::sdst);
Matt Arsenault28bd4cb2017-01-11 22:35:17 +0000473
Matt Arsenault24a12732017-01-11 22:58:12 +0000474 // Check the carry-in operand for v_addc_u32_e64.
475 const MachineOperand *Src2 = TII->getNamedOperand(MI,
476 AMDGPU::OpName::src2);
477
478 if (SDst) {
479 if (SDst->getReg() != AMDGPU::VCC) {
480 if (TargetRegisterInfo::isVirtualRegister(SDst->getReg()))
481 MRI.setRegAllocationHint(SDst->getReg(), 0, AMDGPU::VCC);
482 continue;
483 }
484
485 // All of the instructions with carry outs also have an SGPR input in
486 // src2.
487 if (Src2 && Src2->getReg() != AMDGPU::VCC) {
488 if (TargetRegisterInfo::isVirtualRegister(Src2->getReg()))
489 MRI.setRegAllocationHint(Src2->getReg(), 0, AMDGPU::VCC);
490
491 continue;
492 }
Matt Arsenault28bd4cb2017-01-11 22:35:17 +0000493 }
494
Tom Stellard1aaad692014-07-21 16:55:33 +0000495 // We can shrink this instruction
Matt Arsenaulte0b44042015-09-10 21:51:19 +0000496 DEBUG(dbgs() << "Shrinking " << MI);
Tom Stellard1aaad692014-07-21 16:55:33 +0000497
Tom Stellard6407e1e2014-08-01 00:32:33 +0000498 MachineInstrBuilder Inst32 =
Tom Stellard1aaad692014-07-21 16:55:33 +0000499 BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
500
Tom Stellardcc4c8712016-02-16 18:14:56 +0000501 // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
Matt Arsenault46359152015-08-08 00:41:48 +0000502 // For VOPC instructions, this is replaced by an implicit def of vcc.
Tom Stellardcc4c8712016-02-16 18:14:56 +0000503 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst);
Matt Arsenault46359152015-08-08 00:41:48 +0000504 if (Op32DstIdx != -1) {
505 // dst
Diana Picus116bbab2017-01-13 09:58:52 +0000506 Inst32.add(MI.getOperand(0));
Matt Arsenault46359152015-08-08 00:41:48 +0000507 } else {
508 assert(MI.getOperand(0).getReg() == AMDGPU::VCC &&
509 "Unexpected case");
510 }
511
Tom Stellard1aaad692014-07-21 16:55:33 +0000512
Diana Picus116bbab2017-01-13 09:58:52 +0000513 Inst32.add(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
Tom Stellard1aaad692014-07-21 16:55:33 +0000514
515 const MachineOperand *Src1 =
516 TII->getNamedOperand(MI, AMDGPU::OpName::src1);
517 if (Src1)
Diana Picus116bbab2017-01-13 09:58:52 +0000518 Inst32.add(*Src1);
Tom Stellard1aaad692014-07-21 16:55:33 +0000519
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000520 if (Src2) {
521 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
522 if (Op32Src2Idx != -1) {
Diana Picus116bbab2017-01-13 09:58:52 +0000523 Inst32.add(*Src2);
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000524 } else {
525 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
Matt Arsenault22096252016-06-20 18:34:00 +0000526 // replaced with an implicit read of vcc. This was already added
527 // during the initial BuildMI, so find it to preserve the flags.
528 copyFlagsToImplicitVCC(*Inst32, *Src2);
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000529 }
530 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000531
Tom Stellard1aaad692014-07-21 16:55:33 +0000532 ++NumInstructionsShrunk;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000533
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000534 // Copy extra operands not present in the instruction definition.
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000535 copyExtraImplicitOps(*Inst32, MF, MI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000536
537 MI.eraseFromParent();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000538 foldImmediates(*Inst32, TII, MRI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000539
Tom Stellard6407e1e2014-08-01 00:32:33 +0000540 DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
541
542
Tom Stellard1aaad692014-07-21 16:55:33 +0000543 }
544 }
545 return false;
546}