blob: 327e745dee1132ce2dbab31adbdc4143d646c50a [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
129/// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instruction
130/// and will only fold literal constants if we are still in SSA.
131static void foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
132 MachineRegisterInfo &MRI, bool TryToCommute = true) {
133
134 if (!MRI.isSSA())
135 return;
136
Matt Arsenault3add6432015-10-20 04:35:43 +0000137 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
Tom Stellard6407e1e2014-08-01 00:32:33 +0000138
Matt Arsenault11a4d672015-02-13 19:05:03 +0000139 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
Tom Stellard6407e1e2014-08-01 00:32:33 +0000140
Tom Stellard6407e1e2014-08-01 00:32:33 +0000141 // Try to fold Src0
Matt Arsenault4bd72362016-12-10 00:39:12 +0000142 MachineOperand &Src0 = MI.getOperand(Src0Idx);
Tom Stellardab6e9c02015-07-09 16:30:36 +0000143 if (Src0.isReg() && MRI.hasOneUse(Src0.getReg())) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000144 unsigned Reg = Src0.getReg();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000145 MachineInstr *Def = MRI.getUniqueVRegDef(Reg);
146 if (Def && Def->isMoveImmediate()) {
147 MachineOperand &MovSrc = Def->getOperand(1);
148 bool ConstantFolded = false;
149
Matt Arsenault124384f2016-09-09 23:32:53 +0000150 if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) ||
151 isUInt<32>(MovSrc.getImm()))) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000152 Src0.ChangeToImmediate(MovSrc.getImm());
Tom Stellard6407e1e2014-08-01 00:32:33 +0000153 ConstantFolded = true;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000154 }
155 if (ConstantFolded) {
Tom Stellard6407e1e2014-08-01 00:32:33 +0000156 if (MRI.use_empty(Reg))
157 Def->eraseFromParent();
158 ++NumLiteralConstantsFolded;
159 return;
160 }
161 }
162 }
163
164 // We have failed to fold src0, so commute the instruction and try again.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000165 if (TryToCommute && MI.isCommutable() && TII->commuteInstruction(MI))
Tom Stellard6407e1e2014-08-01 00:32:33 +0000166 foldImmediates(MI, TII, MRI, false);
167
168}
169
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000170// Copy MachineOperand with all flags except setting it as implicit.
Matt Arsenault22096252016-06-20 18:34:00 +0000171static void copyFlagsToImplicitVCC(MachineInstr &MI,
172 const MachineOperand &Orig) {
173
174 for (MachineOperand &Use : MI.implicit_operands()) {
Matt Arsenault24a12732017-01-11 22:58:12 +0000175 if (Use.isUse() && Use.getReg() == AMDGPU::VCC) {
Matt Arsenault22096252016-06-20 18:34:00 +0000176 Use.setIsUndef(Orig.isUndef());
177 Use.setIsKill(Orig.isKill());
178 return;
179 }
180 }
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000181}
182
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000183static bool isKImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000184 return isInt<16>(Src.getImm()) &&
185 !TII->isInlineConstant(*Src.getParent(),
186 Src.getParent()->getOperandNo(&Src));
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000187}
188
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000189static bool isKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000190 return isUInt<16>(Src.getImm()) &&
191 !TII->isInlineConstant(*Src.getParent(),
192 Src.getParent()->getOperandNo(&Src));
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000193}
194
195static bool isKImmOrKUImmOperand(const SIInstrInfo *TII,
196 const MachineOperand &Src,
197 bool &IsUnsigned) {
198 if (isInt<16>(Src.getImm())) {
199 IsUnsigned = false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000200 return !TII->isInlineConstant(Src);
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000201 }
202
203 if (isUInt<16>(Src.getImm())) {
204 IsUnsigned = true;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000205 return !TII->isInlineConstant(Src);
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000206 }
207
208 return false;
209}
210
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000211/// \returns true if the constant in \p Src should be replaced with a bitreverse
212/// of an inline immediate.
213static bool isReverseInlineImm(const SIInstrInfo *TII,
214 const MachineOperand &Src,
215 int32_t &ReverseImm) {
Matt Arsenault4bd72362016-12-10 00:39:12 +0000216 if (!isInt<32>(Src.getImm()) || TII->isInlineConstant(Src))
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000217 return false;
218
219 ReverseImm = reverseBits<int32_t>(static_cast<int32_t>(Src.getImm()));
220 return ReverseImm >= -16 && ReverseImm <= 64;
221}
222
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000223/// Copy implicit register operands from specified instruction to this
224/// instruction that are not part of the instruction definition.
225static void copyExtraImplicitOps(MachineInstr &NewMI, MachineFunction &MF,
226 const MachineInstr &MI) {
227 for (unsigned i = MI.getDesc().getNumOperands() +
228 MI.getDesc().getNumImplicitUses() +
229 MI.getDesc().getNumImplicitDefs(), e = MI.getNumOperands();
230 i != e; ++i) {
231 const MachineOperand &MO = MI.getOperand(i);
232 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
233 NewMI.addOperand(MF, MO);
234 }
235}
236
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000237static void shrinkScalarCompare(const SIInstrInfo *TII, MachineInstr &MI) {
238 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
239 // get constants on the RHS.
240 if (!MI.getOperand(0).isReg())
241 TII->commuteInstruction(MI, false, 0, 1);
242
243 const MachineOperand &Src1 = MI.getOperand(1);
244 if (!Src1.isImm())
245 return;
246
247 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
248 if (SOPKOpc == -1)
249 return;
250
251 // eq/ne is special because the imm16 can be treated as signed or unsigned,
Matt Arsenault5d8eb252016-09-30 01:50:20 +0000252 // and initially selectd to the unsigned versions.
253 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000254 bool HasUImm;
255 if (isKImmOrKUImmOperand(TII, Src1, HasUImm)) {
Matt Arsenault5d8eb252016-09-30 01:50:20 +0000256 if (!HasUImm) {
257 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
258 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000259 }
260
261 MI.setDesc(TII->get(SOPKOpc));
262 }
263
264 return;
265 }
266
267 const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
268
269 if ((TII->sopkIsZext(SOPKOpc) && isKUImmOperand(TII, Src1)) ||
270 (!TII->sopkIsZext(SOPKOpc) && isKImmOperand(TII, Src1))) {
271 MI.setDesc(NewDesc);
272 }
273}
274
Tom Stellard1aaad692014-07-21 16:55:33 +0000275bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000276 if (skipFunction(*MF.getFunction()))
277 return false;
278
Tom Stellard1aaad692014-07-21 16:55:33 +0000279 MachineRegisterInfo &MRI = MF.getRegInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000280 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
281 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard1aaad692014-07-21 16:55:33 +0000282 const SIRegisterInfo &TRI = TII->getRegisterInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000283
Tom Stellard1aaad692014-07-21 16:55:33 +0000284 std::vector<unsigned> I1Defs;
285
286 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
287 BI != BE; ++BI) {
288
289 MachineBasicBlock &MBB = *BI;
290 MachineBasicBlock::iterator I, Next;
291 for (I = MBB.begin(); I != MBB.end(); I = Next) {
292 Next = std::next(I);
293 MachineInstr &MI = *I;
294
Matt Arsenault9a19c242016-03-11 07:42:49 +0000295 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
296 // If this has a literal constant source that is the same as the
297 // reversed bits of an inline immediate, replace with a bitreverse of
298 // that constant. This saves 4 bytes in the common case of materializing
299 // sign bits.
300
301 // Test if we are after regalloc. We only want to do this after any
302 // optimizations happen because this will confuse them.
303 // XXX - not exactly a check for post-regalloc run.
304 MachineOperand &Src = MI.getOperand(1);
305 if (Src.isImm() &&
306 TargetRegisterInfo::isPhysicalRegister(MI.getOperand(0).getReg())) {
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000307 int32_t ReverseImm;
308 if (isReverseInlineImm(TII, Src, ReverseImm)) {
309 MI.setDesc(TII->get(AMDGPU::V_BFREV_B32_e32));
310 Src.setImm(ReverseImm);
311 continue;
Matt Arsenault9a19c242016-03-11 07:42:49 +0000312 }
313 }
314 }
315
Matt Arsenault074ea282016-04-25 19:53:22 +0000316 // Combine adjacent s_nops to use the immediate operand encoding how long
317 // to wait.
318 //
319 // s_nop N
320 // s_nop M
321 // =>
322 // s_nop (N + M)
323 if (MI.getOpcode() == AMDGPU::S_NOP &&
324 Next != MBB.end() &&
325 (*Next).getOpcode() == AMDGPU::S_NOP) {
326
327 MachineInstr &NextMI = *Next;
328 // The instruction encodes the amount to wait with an offset of 1,
329 // i.e. 0 is wait 1 cycle. Convert both to cycles and then convert back
330 // after adding.
331 uint8_t Nop0 = MI.getOperand(0).getImm() + 1;
332 uint8_t Nop1 = NextMI.getOperand(0).getImm() + 1;
333
334 // Make sure we don't overflow the bounds.
335 if (Nop0 + Nop1 <= 8) {
336 NextMI.getOperand(0).setImm(Nop0 + Nop1 - 1);
337 MI.eraseFromParent();
338 }
339
340 continue;
341 }
342
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000343 // FIXME: We also need to consider movs of constant operands since
344 // immediate operands are not folded if they have more than one use, and
345 // the operand folding pass is unaware if the immediate will be free since
346 // it won't know if the src == dest constraint will end up being
347 // satisfied.
348 if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
349 MI.getOpcode() == AMDGPU::S_MUL_I32) {
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000350 const MachineOperand *Dest = &MI.getOperand(0);
351 MachineOperand *Src0 = &MI.getOperand(1);
352 MachineOperand *Src1 = &MI.getOperand(2);
353
354 if (!Src0->isReg() && Src1->isReg()) {
355 if (TII->commuteInstruction(MI, false, 1, 2))
356 std::swap(Src0, Src1);
357 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000358
359 // FIXME: This could work better if hints worked with subregisters. If
360 // we have a vector add of a constant, we usually don't get the correct
361 // allocation due to the subregister usage.
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000362 if (TargetRegisterInfo::isVirtualRegister(Dest->getReg()) &&
363 Src0->isReg()) {
364 MRI.setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
365 MRI.setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000366 continue;
367 }
368
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000369 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
370 if (Src1->isImm() && isKImmOperand(TII, *Src1)) {
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000371 unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ?
372 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
373
374 MI.setDesc(TII->get(Opc));
375 MI.tieOperands(0, 1);
376 }
377 }
378 }
379
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000380 // Try to use s_cmpk_*
381 if (MI.isCompare() && TII->isSOPC(MI)) {
382 shrinkScalarCompare(TII, MI);
383 continue;
384 }
385
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000386 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
387 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000388 const MachineOperand &Dst = MI.getOperand(0);
389 MachineOperand &Src = MI.getOperand(1);
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000390
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000391 if (Src.isImm() &&
392 TargetRegisterInfo::isPhysicalRegister(Dst.getReg())) {
393 int32_t ReverseImm;
394 if (isKImmOperand(TII, Src))
395 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
396 else if (isReverseInlineImm(TII, Src, ReverseImm)) {
397 MI.setDesc(TII->get(AMDGPU::S_BREV_B32));
398 Src.setImm(ReverseImm);
399 }
400 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000401
402 continue;
403 }
404
Tom Stellard86d12eb2014-08-01 00:32:28 +0000405 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard1aaad692014-07-21 16:55:33 +0000406 continue;
407
408 if (!canShrink(MI, TII, TRI, MRI)) {
Matt Arsenault66524032014-09-16 18:00:23 +0000409 // Try commuting the instruction and see if that enables us to shrink
Tom Stellard1aaad692014-07-21 16:55:33 +0000410 // it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000411 if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
Tom Stellard1aaad692014-07-21 16:55:33 +0000412 !canShrink(MI, TII, TRI, MRI))
413 continue;
414 }
415
Marek Olsaka93603d2015-01-15 18:42:51 +0000416 // getVOPe32 could be -1 here if we started with an instruction that had
Tom Stellard86d12eb2014-08-01 00:32:28 +0000417 // a 32-bit encoding and then commuted it to an instruction that did not.
Marek Olsaka93603d2015-01-15 18:42:51 +0000418 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard86d12eb2014-08-01 00:32:28 +0000419 continue;
420
Marek Olsaka93603d2015-01-15 18:42:51 +0000421 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
422
Tom Stellard1aaad692014-07-21 16:55:33 +0000423 if (TII->isVOPC(Op32)) {
424 unsigned DstReg = MI.getOperand(0).getReg();
425 if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000426 // VOPC instructions can only write to the VCC register. We can't
427 // force them to use VCC here, because this is only one register and
428 // cannot deal with sequences which would require multiple copies of
429 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
Tom Stellard1aaad692014-07-21 16:55:33 +0000430 //
Matt Arsenaulta9627ae2014-09-21 17:27:32 +0000431 // So, instead of forcing the instruction to write to VCC, we provide
432 // a hint to the register allocator to use VCC and then we we will run
433 // this pass again after RA and shrink it if it outputs to VCC.
Tom Stellard1aaad692014-07-21 16:55:33 +0000434 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
435 continue;
436 }
437 if (DstReg != AMDGPU::VCC)
438 continue;
439 }
440
Tom Stellarde48fe2a2015-07-14 14:15:03 +0000441 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
442 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
443 // instructions.
444 const MachineOperand *Src2 =
445 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
446 if (!Src2->isReg())
447 continue;
448 unsigned SReg = Src2->getReg();
449 if (TargetRegisterInfo::isVirtualRegister(SReg)) {
450 MRI.setRegAllocationHint(SReg, 0, AMDGPU::VCC);
451 continue;
452 }
453 if (SReg != AMDGPU::VCC)
454 continue;
455 }
456
Matt Arsenault28bd4cb2017-01-11 22:35:17 +0000457 // Check for the bool flag output for instructions like V_ADD_I32_e64.
458 const MachineOperand *SDst = TII->getNamedOperand(MI,
459 AMDGPU::OpName::sdst);
Matt Arsenault28bd4cb2017-01-11 22:35:17 +0000460
Matt Arsenault24a12732017-01-11 22:58:12 +0000461 // Check the carry-in operand for v_addc_u32_e64.
462 const MachineOperand *Src2 = TII->getNamedOperand(MI,
463 AMDGPU::OpName::src2);
464
465 if (SDst) {
466 if (SDst->getReg() != AMDGPU::VCC) {
467 if (TargetRegisterInfo::isVirtualRegister(SDst->getReg()))
468 MRI.setRegAllocationHint(SDst->getReg(), 0, AMDGPU::VCC);
469 continue;
470 }
471
472 // All of the instructions with carry outs also have an SGPR input in
473 // src2.
474 if (Src2 && Src2->getReg() != AMDGPU::VCC) {
475 if (TargetRegisterInfo::isVirtualRegister(Src2->getReg()))
476 MRI.setRegAllocationHint(Src2->getReg(), 0, AMDGPU::VCC);
477
478 continue;
479 }
Matt Arsenault28bd4cb2017-01-11 22:35:17 +0000480 }
481
Tom Stellard1aaad692014-07-21 16:55:33 +0000482 // We can shrink this instruction
Matt Arsenaulte0b44042015-09-10 21:51:19 +0000483 DEBUG(dbgs() << "Shrinking " << MI);
Tom Stellard1aaad692014-07-21 16:55:33 +0000484
Tom Stellard6407e1e2014-08-01 00:32:33 +0000485 MachineInstrBuilder Inst32 =
Tom Stellard1aaad692014-07-21 16:55:33 +0000486 BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
487
Tom Stellardcc4c8712016-02-16 18:14:56 +0000488 // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
Matt Arsenault46359152015-08-08 00:41:48 +0000489 // For VOPC instructions, this is replaced by an implicit def of vcc.
Tom Stellardcc4c8712016-02-16 18:14:56 +0000490 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst);
Matt Arsenault46359152015-08-08 00:41:48 +0000491 if (Op32DstIdx != -1) {
492 // dst
Diana Picus116bbab2017-01-13 09:58:52 +0000493 Inst32.add(MI.getOperand(0));
Matt Arsenault46359152015-08-08 00:41:48 +0000494 } else {
495 assert(MI.getOperand(0).getReg() == AMDGPU::VCC &&
496 "Unexpected case");
497 }
498
Tom Stellard1aaad692014-07-21 16:55:33 +0000499
Diana Picus116bbab2017-01-13 09:58:52 +0000500 Inst32.add(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
Tom Stellard1aaad692014-07-21 16:55:33 +0000501
502 const MachineOperand *Src1 =
503 TII->getNamedOperand(MI, AMDGPU::OpName::src1);
504 if (Src1)
Diana Picus116bbab2017-01-13 09:58:52 +0000505 Inst32.add(*Src1);
Tom Stellard1aaad692014-07-21 16:55:33 +0000506
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000507 if (Src2) {
508 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
509 if (Op32Src2Idx != -1) {
Diana Picus116bbab2017-01-13 09:58:52 +0000510 Inst32.add(*Src2);
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000511 } else {
512 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
Matt Arsenault22096252016-06-20 18:34:00 +0000513 // replaced with an implicit read of vcc. This was already added
514 // during the initial BuildMI, so find it to preserve the flags.
515 copyFlagsToImplicitVCC(*Inst32, *Src2);
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000516 }
517 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000518
Tom Stellard1aaad692014-07-21 16:55:33 +0000519 ++NumInstructionsShrunk;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000520
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000521 // Copy extra operands not present in the instruction definition.
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000522 copyExtraImplicitOps(*Inst32, MF, MI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000523
524 MI.eraseFromParent();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000525 foldImmediates(*Inst32, TII, MRI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000526
Tom Stellard6407e1e2014-08-01 00:32:33 +0000527 DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
528
529
Tom Stellard1aaad692014-07-21 16:55:33 +0000530 }
531 }
532 return false;
533}