blob: 1a0f7d41a1fd89b1b9583532af52861a7f6c5813 [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
Craig Topperfd38cbe2014-08-30 16:48:34 +000048 const char *getPassName() const override {
Tom Stellard1aaad692014-07-21 16:55:33 +000049 return "SI Shrink Instructions";
50 }
51
Craig Topperfd38cbe2014-08-30 16:48:34 +000052 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard1aaad692014-07-21 16:55:33 +000053 AU.setPreservesCFG();
54 MachineFunctionPass::getAnalysisUsage(AU);
55 }
56};
57
58} // End anonymous namespace.
59
Matt Arsenaultc3a01ec2016-06-09 23:18:47 +000060INITIALIZE_PASS(SIShrinkInstructions, DEBUG_TYPE,
61 "SI Shrink Instructions", false, false)
Tom Stellard1aaad692014-07-21 16:55:33 +000062
63char SIShrinkInstructions::ID = 0;
64
65FunctionPass *llvm::createSIShrinkInstructionsPass() {
66 return new SIShrinkInstructions();
67}
68
69static bool isVGPR(const MachineOperand *MO, const SIRegisterInfo &TRI,
70 const MachineRegisterInfo &MRI) {
71 if (!MO->isReg())
72 return false;
73
74 if (TargetRegisterInfo::isVirtualRegister(MO->getReg()))
75 return TRI.hasVGPRs(MRI.getRegClass(MO->getReg()));
76
77 return TRI.hasVGPRs(TRI.getPhysRegClass(MO->getReg()));
78}
79
80static bool canShrink(MachineInstr &MI, const SIInstrInfo *TII,
81 const SIRegisterInfo &TRI,
82 const MachineRegisterInfo &MRI) {
83
84 const MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
85 // Can't shrink instruction with three operands.
Tom Stellard5224df32015-03-10 16:16:44 +000086 // FIXME: v_cndmask_b32 has 3 operands and is shrinkable, but we need to add
87 // a special case for it. It can only be shrunk if the third operand
88 // is vcc. We should handle this the same way we handle vopc, by addding
89 // a register allocation hint pre-regalloc and then do the shrining
90 // post-regalloc.
Tom Stellarddb5a11f2015-07-13 15:47:57 +000091 if (Src2) {
Tom Stellarde48fe2a2015-07-14 14:15:03 +000092 switch (MI.getOpcode()) {
93 default: return false;
Tom Stellarddb5a11f2015-07-13 15:47:57 +000094
Tom Stellarde48fe2a2015-07-14 14:15:03 +000095 case AMDGPU::V_MAC_F32_e64:
96 if (!isVGPR(Src2, TRI, MRI) ||
97 TII->hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers))
98 return false;
99 break;
100
101 case AMDGPU::V_CNDMASK_B32_e64:
102 break;
103 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000104 }
Tom Stellard1aaad692014-07-21 16:55:33 +0000105
106 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
107 const MachineOperand *Src1Mod =
108 TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
109
Tom Stellardb4a313a2014-08-01 00:32:39 +0000110 if (Src1 && (!isVGPR(Src1, TRI, MRI) || (Src1Mod && Src1Mod->getImm() != 0)))
Tom Stellard1aaad692014-07-21 16:55:33 +0000111 return false;
112
Matt Arsenault8943d242014-10-17 18:00:45 +0000113 // We don't need to check src0, all input types are legal, so just make sure
114 // src0 isn't using any modifiers.
115 if (TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
Tom Stellard1aaad692014-07-21 16:55:33 +0000116 return false;
117
118 // Check output modifiers
Matt Arsenault8943d242014-10-17 18:00:45 +0000119 if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
Tom Stellard1aaad692014-07-21 16:55:33 +0000120 return false;
121
Matt Arsenault8226fc42016-03-02 23:00:21 +0000122 return !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp);
Tom Stellard1aaad692014-07-21 16:55:33 +0000123}
124
Tom Stellard6407e1e2014-08-01 00:32:33 +0000125/// \brief This function checks \p MI for operands defined by a move immediate
126/// instruction and then folds the literal constant into the instruction if it
127/// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instruction
128/// and will only fold literal constants if we are still in SSA.
129static void foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
130 MachineRegisterInfo &MRI, bool TryToCommute = true) {
131
132 if (!MRI.isSSA())
133 return;
134
Matt Arsenault3add6432015-10-20 04:35:43 +0000135 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
Tom Stellard6407e1e2014-08-01 00:32:33 +0000136
Matt Arsenault11a4d672015-02-13 19:05:03 +0000137 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
138 MachineOperand &Src0 = MI.getOperand(Src0Idx);
Tom Stellard6407e1e2014-08-01 00:32:33 +0000139
140 // Only one literal constant is allowed per instruction, so if src0 is a
141 // literal constant then we can't do any folding.
Matt Arsenault11a4d672015-02-13 19:05:03 +0000142 if (Src0.isImm() &&
143 TII->isLiteralConstant(Src0, TII->getOpSize(MI, Src0Idx)))
Tom Stellard6407e1e2014-08-01 00:32:33 +0000144 return;
145
Tom Stellard6407e1e2014-08-01 00:32:33 +0000146 // Try to fold Src0
Tom Stellardab6e9c02015-07-09 16:30:36 +0000147 if (Src0.isReg() && MRI.hasOneUse(Src0.getReg())) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000148 unsigned Reg = Src0.getReg();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000149 MachineInstr *Def = MRI.getUniqueVRegDef(Reg);
150 if (Def && Def->isMoveImmediate()) {
151 MachineOperand &MovSrc = Def->getOperand(1);
152 bool ConstantFolded = false;
153
Matt Arsenault124384f2016-09-09 23:32:53 +0000154 if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) ||
155 isUInt<32>(MovSrc.getImm()))) {
Matt Arsenault11a4d672015-02-13 19:05:03 +0000156 Src0.ChangeToImmediate(MovSrc.getImm());
Tom Stellard6407e1e2014-08-01 00:32:33 +0000157 ConstantFolded = true;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000158 }
159 if (ConstantFolded) {
Tom Stellard6407e1e2014-08-01 00:32:33 +0000160 if (MRI.use_empty(Reg))
161 Def->eraseFromParent();
162 ++NumLiteralConstantsFolded;
163 return;
164 }
165 }
166 }
167
168 // We have failed to fold src0, so commute the instruction and try again.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 if (TryToCommute && MI.isCommutable() && TII->commuteInstruction(MI))
Tom Stellard6407e1e2014-08-01 00:32:33 +0000170 foldImmediates(MI, TII, MRI, false);
171
172}
173
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000174// Copy MachineOperand with all flags except setting it as implicit.
Matt Arsenault22096252016-06-20 18:34:00 +0000175static void copyFlagsToImplicitVCC(MachineInstr &MI,
176 const MachineOperand &Orig) {
177
178 for (MachineOperand &Use : MI.implicit_operands()) {
179 if (Use.getReg() == AMDGPU::VCC) {
180 Use.setIsUndef(Orig.isUndef());
181 Use.setIsKill(Orig.isKill());
182 return;
183 }
184 }
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000185}
186
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000187static bool isKImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
188 return isInt<16>(Src.getImm()) && !TII->isInlineConstant(Src, 4);
189}
190
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000191static bool isKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) {
192 return isUInt<16>(Src.getImm()) && !TII->isInlineConstant(Src, 4);
193}
194
195static bool isKImmOrKUImmOperand(const SIInstrInfo *TII,
196 const MachineOperand &Src,
197 bool &IsUnsigned) {
198 if (isInt<16>(Src.getImm())) {
199 IsUnsigned = false;
200 return !TII->isInlineConstant(Src, 4);
201 }
202
203 if (isUInt<16>(Src.getImm())) {
204 IsUnsigned = true;
205 return !TII->isInlineConstant(Src, 4);
206 }
207
208 return false;
209}
210
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000211/// Copy implicit register operands from specified instruction to this
212/// instruction that are not part of the instruction definition.
213static void copyExtraImplicitOps(MachineInstr &NewMI, MachineFunction &MF,
214 const MachineInstr &MI) {
215 for (unsigned i = MI.getDesc().getNumOperands() +
216 MI.getDesc().getNumImplicitUses() +
217 MI.getDesc().getNumImplicitDefs(), e = MI.getNumOperands();
218 i != e; ++i) {
219 const MachineOperand &MO = MI.getOperand(i);
220 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
221 NewMI.addOperand(MF, MO);
222 }
223}
224
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000225static void shrinkScalarCompare(const SIInstrInfo *TII, MachineInstr &MI) {
226 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
227 // get constants on the RHS.
228 if (!MI.getOperand(0).isReg())
229 TII->commuteInstruction(MI, false, 0, 1);
230
231 const MachineOperand &Src1 = MI.getOperand(1);
232 if (!Src1.isImm())
233 return;
234
235 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
236 if (SOPKOpc == -1)
237 return;
238
239 // eq/ne is special because the imm16 can be treated as signed or unsigned,
240 // and initially selectd to the signed versions.
241 if (SOPKOpc == AMDGPU::S_CMPK_EQ_I32 || SOPKOpc == AMDGPU::S_CMPK_LG_I32) {
242 bool HasUImm;
243 if (isKImmOrKUImmOperand(TII, Src1, HasUImm)) {
244 if (HasUImm) {
245 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_I32) ?
246 AMDGPU::S_CMPK_EQ_U32 : AMDGPU::S_CMPK_LG_U32;
247 }
248
249 MI.setDesc(TII->get(SOPKOpc));
250 }
251
252 return;
253 }
254
255 const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
256
257 if ((TII->sopkIsZext(SOPKOpc) && isKUImmOperand(TII, Src1)) ||
258 (!TII->sopkIsZext(SOPKOpc) && isKImmOperand(TII, Src1))) {
259 MI.setDesc(NewDesc);
260 }
261}
262
Tom Stellard1aaad692014-07-21 16:55:33 +0000263bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000264 if (skipFunction(*MF.getFunction()))
265 return false;
266
Tom Stellard1aaad692014-07-21 16:55:33 +0000267 MachineRegisterInfo &MRI = MF.getRegInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000268 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
269 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard1aaad692014-07-21 16:55:33 +0000270 const SIRegisterInfo &TRI = TII->getRegisterInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000271
Tom Stellard1aaad692014-07-21 16:55:33 +0000272 std::vector<unsigned> I1Defs;
273
274 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
275 BI != BE; ++BI) {
276
277 MachineBasicBlock &MBB = *BI;
278 MachineBasicBlock::iterator I, Next;
279 for (I = MBB.begin(); I != MBB.end(); I = Next) {
280 Next = std::next(I);
281 MachineInstr &MI = *I;
282
Matt Arsenault9a19c242016-03-11 07:42:49 +0000283 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
284 // If this has a literal constant source that is the same as the
285 // reversed bits of an inline immediate, replace with a bitreverse of
286 // that constant. This saves 4 bytes in the common case of materializing
287 // sign bits.
288
289 // Test if we are after regalloc. We only want to do this after any
290 // optimizations happen because this will confuse them.
291 // XXX - not exactly a check for post-regalloc run.
292 MachineOperand &Src = MI.getOperand(1);
293 if (Src.isImm() &&
294 TargetRegisterInfo::isPhysicalRegister(MI.getOperand(0).getReg())) {
295 int64_t Imm = Src.getImm();
296 if (isInt<32>(Imm) && !TII->isInlineConstant(Src, 4)) {
297 int32_t ReverseImm = reverseBits<int32_t>(static_cast<int32_t>(Imm));
298 if (ReverseImm >= -16 && ReverseImm <= 64) {
299 MI.setDesc(TII->get(AMDGPU::V_BFREV_B32_e32));
300 Src.setImm(ReverseImm);
301 continue;
302 }
303 }
304 }
305 }
306
Matt Arsenault074ea282016-04-25 19:53:22 +0000307 // Combine adjacent s_nops to use the immediate operand encoding how long
308 // to wait.
309 //
310 // s_nop N
311 // s_nop M
312 // =>
313 // s_nop (N + M)
314 if (MI.getOpcode() == AMDGPU::S_NOP &&
315 Next != MBB.end() &&
316 (*Next).getOpcode() == AMDGPU::S_NOP) {
317
318 MachineInstr &NextMI = *Next;
319 // The instruction encodes the amount to wait with an offset of 1,
320 // i.e. 0 is wait 1 cycle. Convert both to cycles and then convert back
321 // after adding.
322 uint8_t Nop0 = MI.getOperand(0).getImm() + 1;
323 uint8_t Nop1 = NextMI.getOperand(0).getImm() + 1;
324
325 // Make sure we don't overflow the bounds.
326 if (Nop0 + Nop1 <= 8) {
327 NextMI.getOperand(0).setImm(Nop0 + Nop1 - 1);
328 MI.eraseFromParent();
329 }
330
331 continue;
332 }
333
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000334 // FIXME: We also need to consider movs of constant operands since
335 // immediate operands are not folded if they have more than one use, and
336 // the operand folding pass is unaware if the immediate will be free since
337 // it won't know if the src == dest constraint will end up being
338 // satisfied.
339 if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
340 MI.getOpcode() == AMDGPU::S_MUL_I32) {
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000341 const MachineOperand *Dest = &MI.getOperand(0);
342 MachineOperand *Src0 = &MI.getOperand(1);
343 MachineOperand *Src1 = &MI.getOperand(2);
344
345 if (!Src0->isReg() && Src1->isReg()) {
346 if (TII->commuteInstruction(MI, false, 1, 2))
347 std::swap(Src0, Src1);
348 }
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000349
350 // FIXME: This could work better if hints worked with subregisters. If
351 // we have a vector add of a constant, we usually don't get the correct
352 // allocation due to the subregister usage.
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000353 if (TargetRegisterInfo::isVirtualRegister(Dest->getReg()) &&
354 Src0->isReg()) {
355 MRI.setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
356 MRI.setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000357 continue;
358 }
359
Matt Arsenaultbe90f702016-09-08 17:35:41 +0000360 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
361 if (Src1->isImm() && isKImmOperand(TII, *Src1)) {
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000362 unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ?
363 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
364
365 MI.setDesc(TII->get(Opc));
366 MI.tieOperands(0, 1);
367 }
368 }
369 }
370
Matt Arsenault7ccf6cd2016-09-16 21:41:16 +0000371 // Try to use s_cmpk_*
372 if (MI.isCompare() && TII->isSOPC(MI)) {
373 shrinkScalarCompare(TII, MI);
374 continue;
375 }
376
Matt Arsenaultb6be2022016-04-16 01:46:49 +0000377 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
378 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
379 const MachineOperand &Src = MI.getOperand(1);
380
381 if (Src.isImm() && isKImmOperand(TII, Src))
382 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
383
384 continue;
385 }
386
Tom Stellard86d12eb2014-08-01 00:32:28 +0000387 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard1aaad692014-07-21 16:55:33 +0000388 continue;
389
390 if (!canShrink(MI, TII, TRI, MRI)) {
Matt Arsenault66524032014-09-16 18:00:23 +0000391 // Try commuting the instruction and see if that enables us to shrink
Tom Stellard1aaad692014-07-21 16:55:33 +0000392 // it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000393 if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
Tom Stellard1aaad692014-07-21 16:55:33 +0000394 !canShrink(MI, TII, TRI, MRI))
395 continue;
396 }
397
Marek Olsaka93603d2015-01-15 18:42:51 +0000398 // getVOPe32 could be -1 here if we started with an instruction that had
Tom Stellard86d12eb2014-08-01 00:32:28 +0000399 // a 32-bit encoding and then commuted it to an instruction that did not.
Marek Olsaka93603d2015-01-15 18:42:51 +0000400 if (!TII->hasVALU32BitEncoding(MI.getOpcode()))
Tom Stellard86d12eb2014-08-01 00:32:28 +0000401 continue;
402
Marek Olsaka93603d2015-01-15 18:42:51 +0000403 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
404
Tom Stellard1aaad692014-07-21 16:55:33 +0000405 if (TII->isVOPC(Op32)) {
406 unsigned DstReg = MI.getOperand(0).getReg();
407 if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000408 // VOPC instructions can only write to the VCC register. We can't
409 // force them to use VCC here, because this is only one register and
410 // cannot deal with sequences which would require multiple copies of
411 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
Tom Stellard1aaad692014-07-21 16:55:33 +0000412 //
Matt Arsenaulta9627ae2014-09-21 17:27:32 +0000413 // So, instead of forcing the instruction to write to VCC, we provide
414 // a hint to the register allocator to use VCC and then we we will run
415 // this pass again after RA and shrink it if it outputs to VCC.
Tom Stellard1aaad692014-07-21 16:55:33 +0000416 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, AMDGPU::VCC);
417 continue;
418 }
419 if (DstReg != AMDGPU::VCC)
420 continue;
421 }
422
Tom Stellarde48fe2a2015-07-14 14:15:03 +0000423 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
424 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
425 // instructions.
426 const MachineOperand *Src2 =
427 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
428 if (!Src2->isReg())
429 continue;
430 unsigned SReg = Src2->getReg();
431 if (TargetRegisterInfo::isVirtualRegister(SReg)) {
432 MRI.setRegAllocationHint(SReg, 0, AMDGPU::VCC);
433 continue;
434 }
435 if (SReg != AMDGPU::VCC)
436 continue;
437 }
438
Tom Stellard1aaad692014-07-21 16:55:33 +0000439 // We can shrink this instruction
Matt Arsenaulte0b44042015-09-10 21:51:19 +0000440 DEBUG(dbgs() << "Shrinking " << MI);
Tom Stellard1aaad692014-07-21 16:55:33 +0000441
Tom Stellard6407e1e2014-08-01 00:32:33 +0000442 MachineInstrBuilder Inst32 =
Tom Stellard1aaad692014-07-21 16:55:33 +0000443 BuildMI(MBB, I, MI.getDebugLoc(), TII->get(Op32));
444
Tom Stellardcc4c8712016-02-16 18:14:56 +0000445 // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
Matt Arsenault46359152015-08-08 00:41:48 +0000446 // For VOPC instructions, this is replaced by an implicit def of vcc.
Tom Stellardcc4c8712016-02-16 18:14:56 +0000447 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst);
Matt Arsenault46359152015-08-08 00:41:48 +0000448 if (Op32DstIdx != -1) {
449 // dst
450 Inst32.addOperand(MI.getOperand(0));
451 } else {
452 assert(MI.getOperand(0).getReg() == AMDGPU::VCC &&
453 "Unexpected case");
454 }
455
Tom Stellard1aaad692014-07-21 16:55:33 +0000456
Tom Stellard6407e1e2014-08-01 00:32:33 +0000457 Inst32.addOperand(*TII->getNamedOperand(MI, AMDGPU::OpName::src0));
Tom Stellard1aaad692014-07-21 16:55:33 +0000458
459 const MachineOperand *Src1 =
460 TII->getNamedOperand(MI, AMDGPU::OpName::src1);
461 if (Src1)
Tom Stellard6407e1e2014-08-01 00:32:33 +0000462 Inst32.addOperand(*Src1);
Tom Stellard1aaad692014-07-21 16:55:33 +0000463
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000464 const MachineOperand *Src2 =
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000465 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
466 if (Src2) {
467 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
468 if (Op32Src2Idx != -1) {
469 Inst32.addOperand(*Src2);
470 } else {
471 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
Matt Arsenault22096252016-06-20 18:34:00 +0000472 // replaced with an implicit read of vcc. This was already added
473 // during the initial BuildMI, so find it to preserve the flags.
474 copyFlagsToImplicitVCC(*Inst32, *Src2);
Matt Arsenault6942d1a2015-08-08 00:41:45 +0000475 }
476 }
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000477
Tom Stellard1aaad692014-07-21 16:55:33 +0000478 ++NumInstructionsShrunk;
Tom Stellard6407e1e2014-08-01 00:32:33 +0000479
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000480 // Copy extra operands not present in the instruction definition.
Matt Arsenault5ffe3e12016-09-03 17:25:39 +0000481 copyExtraImplicitOps(*Inst32, MF, MI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000482
483 MI.eraseFromParent();
Tom Stellard6407e1e2014-08-01 00:32:33 +0000484 foldImmediates(*Inst32, TII, MRI);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000485
Tom Stellard6407e1e2014-08-01 00:32:33 +0000486 DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
487
488
Tom Stellard1aaad692014-07-21 16:55:33 +0000489 }
490 }
491 return false;
492}