blob: 7f9e9cded638dcb90030804169c9062e354ffa95 [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
87 // a register allocation hint pre-regalloc and then do the shrining
88 // 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
Tom Stellarde48fe2a2015-07-14 14:15:03 +000093 case AMDGPU::V_MAC_F32_e64:
94 if (!isVGPR(Src2, TRI, MRI) ||
95 TII->hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers))
96 return false;
97 break;
98
99 case AMDGPU::V_CNDMASK_B32_e64:
100 break;
101 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000102 }
Tom Stellard1aaad692014-07-21 16:55:33 +0000103
104 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
105 const MachineOperand *Src1Mod =
106 TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
107
Tom Stellardb4a313a2014-08-01 00:32:39 +0000108 if (Src1 && (!isVGPR(Src1, TRI, MRI) || (Src1Mod && Src1Mod->getImm() != 0)))
Tom Stellard1aaad692014-07-21 16:55:33 +0000109 return false;
110
Matt Arsenault8943d242014-10-17 18:00:45 +0000111 // We don't need to check src0, all input types are legal, so just make sure
112 // src0 isn't using any modifiers.
113 if (TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
Tom Stellard1aaad692014-07-21 16:55:33 +0000114 return false;
115
116 // Check output modifiers
Matt Arsenault8943d242014-10-17 18:00:45 +0000117 if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
Tom Stellard1aaad692014-07-21 16:55:33 +0000118 return false;
119
Matt Arsenault8226fc42016-03-02 23:00:21 +0000120 return !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp);
Tom Stellard1aaad692014-07-21 16:55:33 +0000121}
122
Tom Stellard6407e1e2014-08-01 00:32:33 +0000123/// \brief This function checks \p MI for operands defined by a move immediate
124/// instruction and then folds the literal constant into the instruction if it
125/// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instruction
126/// and will only fold literal constants if we are still in SSA.
127static void foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
128 MachineRegisterInfo &MRI, bool TryToCommute = true) {
129
130 if (!MRI.isSSA())
131 return;
132
Matt Arsenault3add6432015-10-20 04:35:43 +0000133 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
Tom Stellard6407e1e2014-08-01 00:32:33 +0000134
Matt Arsenault11a4d672015-02-13 19:05:03 +0000135 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
136 MachineOperand &Src0 = MI.getOperand(Src0Idx);
Tom Stellard6407e1e2014-08-01 00:32:33 +0000137
138 // Only one literal constant is allowed per instruction, so if src0 is a
139 // literal constant then we can't do any folding.
Matt Arsenault11a4d672015-02-13 19:05:03 +0000140 if (Src0.isImm() &&
141 TII->isLiteralConstant(Src0, TII->getOpSize(MI, Src0Idx)))
Tom Stellard6407e1e2014-08-01 00:32:33 +0000142 return;
143
Tom Stellard6407e1e2014-08-01 00:32:33 +0000144 // Try to fold Src0
Tom Stellardab6e9c02015-07-09 16:30:36 +0000145 if (Src0.isReg() && MRI.hasOneUse(Src0.getReg())) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000146 unsigned Reg = Src0.getReg();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000147 MachineInstr *Def = MRI.getUniqueVRegDef(Reg);
148 if (Def && Def->isMoveImmediate()) {
149 MachineOperand &MovSrc = Def->getOperand(1);
150 bool ConstantFolded = false;
151
Matt Arsenault124384f2016-09-09 23:32:53 +0000152 if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) ||
153 isUInt<32>(MovSrc.getImm()))) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000154 Src0.ChangeToImmediate(MovSrc.getImm());
Tom Stellard6407e1e2014-08-01 00:32:33 +0000155 ConstantFolded = true;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000156 }
157 if (ConstantFolded) {
Tom Stellard6407e1e2014-08-01 00:32:33 +0000158 if (MRI.use_empty(Reg))
159 Def->eraseFromParent();
160 ++NumLiteralConstantsFolded;
161 return;
162 }
163 }
164 }
165
166 // We have failed to fold src0, so commute the instruction and try again.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000167 if (TryToCommute && MI.isCommutable() && TII->commuteInstruction(MI))
Tom Stellard6407e1e2014-08-01 00:32:33 +0000168 foldImmediates(MI, TII, MRI, false);
169
170}
171
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000172// Copy MachineOperand with all flags except setting it as implicit.
Matt Arsenault22096252016-06-20 18:34:00 +0000173static void copyFlagsToImplicitVCC(MachineInstr &MI,
174 const MachineOperand &Orig) {
175
176 for (MachineOperand &Use : MI.implicit_operands()) {
177 if (Use.getReg() == AMDGPU::VCC) {
178 Use.setIsUndef(Orig.isUndef());
179 Use.setIsKill(Orig.isKill());
180 return;
181 }
182 }
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000183}
184
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000185static bool isKImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
186 return isInt<16>(Src.getImm()) && !TII->isInlineConstant(Src, 4);
187}
188
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000189static bool isKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
190 return isUInt<16>(Src.getImm()) && !TII->isInlineConstant(Src, 4);
191}
192
193static bool isKImmOrKUImmOperand(const SIInstrInfo *TII,
194 const MachineOperand &Src,
195 bool &IsUnsigned) {
196 if (isInt<16>(Src.getImm())) {
197 IsUnsigned = false;
198 return !TII->isInlineConstant(Src, 4);
199 }
200
201 if (isUInt<16>(Src.getImm())) {
202 IsUnsigned = true;
203 return !TII->isInlineConstant(Src, 4);
204 }
205
206 return false;
207}
208
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000209/// \returns true if the constant in \p Src should be replaced with a bitreverse
210/// of an inline immediate.
211static bool isReverseInlineImm(const SIInstrInfo *TII,
212 const MachineOperand &Src,
213 int32_t &ReverseImm) {
214 if (!isInt<32>(Src.getImm()) || TII->isInlineConstant(Src, 4))
215 return false;
216
217 ReverseImm = reverseBits<int32_t>(static_cast<int32_t>(Src.getImm()));
218 return ReverseImm >= -16 && ReverseImm <= 64;
219}
220
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000221/// Copy implicit register operands from specified instruction to this
222/// instruction that are not part of the instruction definition.
223static void copyExtraImplicitOps(MachineInstr &NewMI, MachineFunction &MF,
224 const MachineInstr &MI) {
225 for (unsigned i = MI.getDesc().getNumOperands() +
226 MI.getDesc().getNumImplicitUses() +
227 MI.getDesc().getNumImplicitDefs(), e = MI.getNumOperands();
228 i != e; ++i) {
229 const MachineOperand &MO = MI.getOperand(i);
230 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
231 NewMI.addOperand(MF, MO);
232 }
233}
234
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000235static void shrinkScalarCompare(const SIInstrInfo *TII, MachineInstr &MI) {
236 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
237 // get constants on the RHS.
238 if (!MI.getOperand(0).isReg())
239 TII->commuteInstruction(MI, false, 0, 1);
240
241 const MachineOperand &Src1 = MI.getOperand(1);
242 if (!Src1.isImm())
243 return;
244
245 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
246 if (SOPKOpc == -1)
247 return;
248
249 // eq/ne is special because the imm16 can be treated as signed or unsigned,
Matt Arsenault5d8eb252016-09-30 01:50:20 +0000250 // and initially selectd to the unsigned versions.
251 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000252 bool HasUImm;
253 if (isKImmOrKUImmOperand(TII, Src1, HasUImm)) {
Matt Arsenault5d8eb252016-09-30 01:50:20 +0000254 if (!HasUImm) {
255 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
256 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000257 }
258
259 MI.setDesc(TII->get(SOPKOpc));
260 }
261
262 return;
263 }
264
265 const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
266
267 if ((TII->sopkIsZext(SOPKOpc) && isKUImmOperand(TII, Src1)) ||
268 (!TII->sopkIsZext(SOPKOpc) && isKImmOperand(TII, Src1))) {
269 MI.setDesc(NewDesc);
270 }
271}
272
Tom Stellard1aaad692014-07-21 16:55:33 +0000273bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000274 if (skipFunction(*MF.getFunction()))
275 return false;
276
Tom Stellard1aaad692014-07-21 16:55:33 +0000277 MachineRegisterInfo &MRI = MF.getRegInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000278 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
279 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard1aaad692014-07-21 16:55:33 +0000280 const SIRegisterInfo &TRI = TII->getRegisterInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000281
Tom Stellard1aaad692014-07-21 16:55:33 +0000282 std::vector<unsigned> I1Defs;
283
284 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
285 BI != BE; ++BI) {
286
287 MachineBasicBlock &MBB = *BI;
288 MachineBasicBlock::iterator I, Next;
289 for (I = MBB.begin(); I != MBB.end(); I = Next) {
290 Next = std::next(I);
291 MachineInstr &MI = *I;
292
Matt Arsenault9a19c242016-03-11 07:42:49 +0000293 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
294 // If this has a literal constant source that is the same as the
295 // reversed bits of an inline immediate, replace with a bitreverse of
296 // that constant. This saves 4 bytes in the common case of materializing
297 // sign bits.
298
299 // Test if we are after regalloc. We only want to do this after any
300 // optimizations happen because this will confuse them.
301 // XXX - not exactly a check for post-regalloc run.
302 MachineOperand &Src = MI.getOperand(1);
303 if (Src.isImm() &&
304 TargetRegisterInfo::isPhysicalRegister(MI.getOperand(0).getReg())) {
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000305 int32_t ReverseImm;
306 if (isReverseInlineImm(TII, Src, ReverseImm)) {
307 MI.setDesc(TII->get(AMDGPU::V_BFREV_B32_e32));
308 Src.setImm(ReverseImm);
309 continue;
Matt Arsenault9a19c242016-03-11 07:42:49 +0000310 }
311 }
312 }
313
Matt Arsenault074ea282016-04-25 19:53:22 +0000314 // Combine adjacent s_nops to use the immediate operand encoding how long
315 // to wait.
316 //
317 // s_nop N
318 // s_nop M
319 // =>
320 // s_nop (N + M)
321 if (MI.getOpcode() == AMDGPU::S_NOP &&
322 Next != MBB.end() &&
323 (*Next).getOpcode() == AMDGPU::S_NOP) {
324
325 MachineInstr &NextMI = *Next;
326 // The instruction encodes the amount to wait with an offset of 1,
327 // i.e. 0 is wait 1 cycle. Convert both to cycles and then convert back
328 // after adding.
329 uint8_t Nop0 = MI.getOperand(0).getImm() + 1;
330 uint8_t Nop1 = NextMI.getOperand(0).getImm() + 1;
331
332 // Make sure we don't overflow the bounds.
333 if (Nop0 + Nop1 <= 8) {
334 NextMI.getOperand(0).setImm(Nop0 + Nop1 - 1);
335 MI.eraseFromParent();
336 }
337
338 continue;
339 }
340
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000341 // FIXME: We also need to consider movs of constant operands since
342 // immediate operands are not folded if they have more than one use, and
343 // the operand folding pass is unaware if the immediate will be free since
344 // it won't know if the src == dest constraint will end up being
345 // satisfied.
346 if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
347 MI.getOpcode() == AMDGPU::S_MUL_I32) {
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000348 const MachineOperand *Dest = &MI.getOperand(0);
349 MachineOperand *Src0 = &MI.getOperand(1);
350 MachineOperand *Src1 = &MI.getOperand(2);
351
352 if (!Src0->isReg() && Src1->isReg()) {
353 if (TII->commuteInstruction(MI, false, 1, 2))
354 std::swap(Src0, Src1);
355 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000356
357 // FIXME: This could work better if hints worked with subregisters. If
358 // we have a vector add of a constant, we usually don't get the correct
359 // allocation due to the subregister usage.
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000360 if (TargetRegisterInfo::isVirtualRegister(Dest->getReg()) &&
361 Src0->isReg()) {
362 MRI.setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
363 MRI.setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000364 continue;
365 }
366
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000367 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
368 if (Src1->isImm() && isKImmOperand(TII, *Src1)) {
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000369 unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ?
370 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
371
372 MI.setDesc(TII->get(Opc));
373 MI.tieOperands(0, 1);
374 }
375 }
376 }
377
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000378 // Try to use s_cmpk_*
379 if (MI.isCompare() && TII->isSOPC(MI)) {
380 shrinkScalarCompare(TII, MI);
381 continue;
382 }
383
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000384 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
385 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000386 const MachineOperand &Dst = MI.getOperand(0);
387 MachineOperand &Src = MI.getOperand(1);
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000388
Matt Arsenault663ab8c2016-11-01 23:14:20 +0000389 if (Src.isImm() &&
390 TargetRegisterInfo::isPhysicalRegister(Dst.getReg())) {
391 int32_t ReverseImm;
392 if (isKImmOperand(TII, Src))
393 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
394 else if (isReverseInlineImm(TII, Src, ReverseImm)) {
395 MI.setDesc(TII->get(AMDGPU::S_BREV_B32));
396 Src.setImm(ReverseImm);
397 }
398 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000399
400 continue;
401 }
402
Tom Stellard86d12eb2014-08-01 00:32:28 +0000403 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard1aaad692014-07-21 16:55:33 +0000404 continue;
405
406 if (!canShrink(MI, TII, TRI, MRI)) {
Matt Arsenault66524032014-09-16 18:00:23 +0000407 // Try commuting the instruction and see if that enables us to shrink
Tom Stellard1aaad692014-07-21 16:55:33 +0000408 // it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000409 if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
Tom Stellard1aaad692014-07-21 16:55:33 +0000410 !canShrink(MI, TII, TRI, MRI))
411 continue;
412 }
413
Marek Olsaka93603d2015-01-15 18:42:51 +0000414 // getVOPe32 could be -1 here if we started with an instruction that had
Tom Stellard86d12eb2014-08-01 00:32:28 +0000415 // a 32-bit encoding and then commuted it to an instruction that did not.
Marek Olsaka93603d2015-01-15 18:42:51 +0000416 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard86d12eb2014-08-01 00:32:28 +0000417 continue;
418
Marek Olsaka93603d2015-01-15 18:42:51 +0000419 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
420
Tom Stellard1aaad692014-07-21 16:55:33 +0000421 if (TII->isVOPC(Op32)) {
422 unsigned DstReg = MI.getOperand(0).getReg();
423 if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000424 // VOPC instructions can only write to the VCC register. We can't
425 // force them to use VCC here, because this is only one register and
426 // cannot deal with sequences which would require multiple copies of
427 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
Tom Stellard1aaad692014-07-21 16:55:33 +0000428 //
Matt Arsenaulta9627ae2014-09-21 17:27:32 +0000429 // So, instead of forcing the instruction to write to VCC, we provide
430 // a hint to the register allocator to use VCC and then we we will run
431 // this pass again after RA and shrink it if it outputs to VCC.
Tom Stellard1aaad692014-07-21 16:55:33 +0000432 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
433 continue;
434 }
435 if (DstReg != AMDGPU::VCC)
436 continue;
437 }
438
Tom Stellarde48fe2a2015-07-14 14:15:03 +0000439 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
440 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
441 // instructions.
442 const MachineOperand *Src2 =
443 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
444 if (!Src2->isReg())
445 continue;
446 unsigned SReg = Src2->getReg();
447 if (TargetRegisterInfo::isVirtualRegister(SReg)) {
448 MRI.setRegAllocationHint(SReg, 0, AMDGPU::VCC);
449 continue;
450 }
451 if (SReg != AMDGPU::VCC)
452 continue;
453 }
454
Tom Stellard1aaad692014-07-21 16:55:33 +0000455 // We can shrink this instruction
Matt Arsenaulte0b44042015-09-10 21:51:19 +0000456 DEBUG(dbgs() << "Shrinking " << MI);
Tom Stellard1aaad692014-07-21 16:55:33 +0000457
Tom Stellard6407e1e2014-08-01 00:32:33 +0000458 MachineInstrBuilder Inst32 =
Tom Stellard1aaad692014-07-21 16:55:33 +0000459 BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
460
Tom Stellardcc4c8712016-02-16 18:14:56 +0000461 // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
Matt Arsenault46359152015-08-08 00:41:48 +0000462 // For VOPC instructions, this is replaced by an implicit def of vcc.
Tom Stellardcc4c8712016-02-16 18:14:56 +0000463 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst);
Matt Arsenault46359152015-08-08 00:41:48 +0000464 if (Op32DstIdx != -1) {
465 // dst
466 Inst32.addOperand(MI.getOperand(0));
467 } else {
468 assert(MI.getOperand(0).getReg() == AMDGPU::VCC &&
469 "Unexpected case");
470 }
471
Tom Stellard1aaad692014-07-21 16:55:33 +0000472
Tom Stellard6407e1e2014-08-01 00:32:33 +0000473 Inst32.addOperand(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
Tom Stellard1aaad692014-07-21 16:55:33 +0000474
475 const MachineOperand *Src1 =
476 TII->getNamedOperand(MI, AMDGPU::OpName::src1);
477 if (Src1)
Tom Stellard6407e1e2014-08-01 00:32:33 +0000478 Inst32.addOperand(*Src1);
Tom Stellard1aaad692014-07-21 16:55:33 +0000479
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000480 const MachineOperand *Src2 =
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000481 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
482 if (Src2) {
483 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
484 if (Op32Src2Idx != -1) {
485 Inst32.addOperand(*Src2);
486 } else {
487 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
Matt Arsenault22096252016-06-20 18:34:00 +0000488 // replaced with an implicit read of vcc. This was already added
489 // during the initial BuildMI, so find it to preserve the flags.
490 copyFlagsToImplicitVCC(*Inst32, *Src2);
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000491 }
492 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000493
Tom Stellard1aaad692014-07-21 16:55:33 +0000494 ++NumInstructionsShrunk;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000495
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000496 // Copy extra operands not present in the instruction definition.
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000497 copyExtraImplicitOps(*Inst32, MF, MI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000498
499 MI.eraseFromParent();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000500 foldImmediates(*Inst32, TII, MRI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000501
Tom Stellard6407e1e2014-08-01 00:32:33 +0000502 DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
503
504
Tom Stellard1aaad692014-07-21 16:55:33 +0000505 }
506 }
507 return false;
508}