blob: a54b628520cce7924b7bb89545451f29696d7943 [file] [log] [blame]
Tom Stellard6596ba72014-11-21 22:06:37 +00001//===-- SIFoldOperands.cpp - Fold operands --- ----------------------------===//
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/// \file
9//===----------------------------------------------------------------------===//
10//
11
12#include "AMDGPU.h"
13#include "AMDGPUSubtarget.h"
14#include "SIInstrInfo.h"
15#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000019#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000020#include "llvm/Support/raw_ostream.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000021#include "llvm/Target/TargetMachine.h"
22
23#define DEBUG_TYPE "si-fold-operands"
24using namespace llvm;
25
26namespace {
27
28class SIFoldOperands : public MachineFunctionPass {
29public:
30 static char ID;
31
32public:
33 SIFoldOperands() : MachineFunctionPass(ID) {
34 initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
35 }
36
37 bool runOnMachineFunction(MachineFunction &MF) override;
38
Mehdi Amini117296c2016-10-01 02:56:57 +000039 StringRef getPassName() const override { return "SI Fold Operands"; }
Tom Stellard6596ba72014-11-21 22:06:37 +000040
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard6596ba72014-11-21 22:06:37 +000042 AU.setPreservesCFG();
43 MachineFunctionPass::getAnalysisUsage(AU);
44 }
45};
46
Tom Stellardbb763e62015-01-07 17:42:16 +000047struct FoldCandidate {
48 MachineInstr *UseMI;
Matt Arsenault2bc198a2016-09-14 15:51:33 +000049 union {
50 MachineOperand *OpToFold;
51 uint64_t ImmToFold;
52 int FrameIndexToFold;
53 };
54 unsigned char UseOpNo;
55 MachineOperand::MachineOperandType Kind;
Tom Stellardbb763e62015-01-07 17:42:16 +000056
57 FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp) :
Matt Arsenault2bc198a2016-09-14 15:51:33 +000058 UseMI(MI), OpToFold(nullptr), UseOpNo(OpNo), Kind(FoldOp->getType()) {
Tom Stellard05992972015-01-07 22:44:19 +000059 if (FoldOp->isImm()) {
Tom Stellard05992972015-01-07 22:44:19 +000060 ImmToFold = FoldOp->getImm();
Matt Arsenault2bc198a2016-09-14 15:51:33 +000061 } else if (FoldOp->isFI()) {
62 FrameIndexToFold = FoldOp->getIndex();
Tom Stellard05992972015-01-07 22:44:19 +000063 } else {
64 assert(FoldOp->isReg());
65 OpToFold = FoldOp;
66 }
67 }
Tom Stellardbb763e62015-01-07 17:42:16 +000068
Matt Arsenault2bc198a2016-09-14 15:51:33 +000069 bool isFI() const {
70 return Kind == MachineOperand::MO_FrameIndex;
71 }
72
Tom Stellardbb763e62015-01-07 17:42:16 +000073 bool isImm() const {
Matt Arsenault2bc198a2016-09-14 15:51:33 +000074 return Kind == MachineOperand::MO_Immediate;
75 }
76
77 bool isReg() const {
78 return Kind == MachineOperand::MO_Register;
Tom Stellardbb763e62015-01-07 17:42:16 +000079 }
80};
81
Tom Stellard6596ba72014-11-21 22:06:37 +000082} // End anonymous namespace.
83
Matt Arsenault427c5482016-02-11 06:15:34 +000084INITIALIZE_PASS(SIFoldOperands, DEBUG_TYPE,
85 "SI Fold Operands", false, false)
Tom Stellard6596ba72014-11-21 22:06:37 +000086
87char SIFoldOperands::ID = 0;
88
89char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
90
91FunctionPass *llvm::createSIFoldOperandsPass() {
92 return new SIFoldOperands();
93}
94
Matt Arsenault5e63a042016-10-06 18:12:13 +000095static bool isSafeToFold(const MachineInstr &MI) {
96 switch (MI.getOpcode()) {
Tom Stellard6596ba72014-11-21 22:06:37 +000097 case AMDGPU::V_MOV_B32_e32:
98 case AMDGPU::V_MOV_B32_e64:
Matt Arsenault5e63a042016-10-06 18:12:13 +000099 case AMDGPU::V_MOV_B64_PSEUDO: {
100 // If there are additional implicit register operands, this may be used for
101 // register indexing so the source register operand isn't simply copied.
102 unsigned NumOps = MI.getDesc().getNumOperands() +
103 MI.getDesc().getNumImplicitUses();
104
105 return MI.getNumOperands() == NumOps;
106 }
Tom Stellard6596ba72014-11-21 22:06:37 +0000107 case AMDGPU::S_MOV_B32:
108 case AMDGPU::S_MOV_B64:
109 case AMDGPU::COPY:
110 return true;
111 default:
112 return false;
113 }
114}
115
Tom Stellardbb763e62015-01-07 17:42:16 +0000116static bool updateOperand(FoldCandidate &Fold,
Tom Stellard6596ba72014-11-21 22:06:37 +0000117 const TargetRegisterInfo &TRI) {
Tom Stellardbb763e62015-01-07 17:42:16 +0000118 MachineInstr *MI = Fold.UseMI;
119 MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
Tom Stellard6596ba72014-11-21 22:06:37 +0000120 assert(Old.isReg());
121
Tom Stellardbb763e62015-01-07 17:42:16 +0000122 if (Fold.isImm()) {
123 Old.ChangeToImmediate(Fold.ImmToFold);
Tom Stellard6596ba72014-11-21 22:06:37 +0000124 return true;
125 }
126
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000127 if (Fold.isFI()) {
128 Old.ChangeToFrameIndex(Fold.FrameIndexToFold);
129 return true;
130 }
131
Tom Stellardbb763e62015-01-07 17:42:16 +0000132 MachineOperand *New = Fold.OpToFold;
133 if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
134 TargetRegisterInfo::isVirtualRegister(New->getReg())) {
135 Old.substVirtReg(New->getReg(), New->getSubReg(), TRI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000136 return true;
137 }
138
Tom Stellard6596ba72014-11-21 22:06:37 +0000139 // FIXME: Handle physical registers.
140
141 return false;
142}
143
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000144static bool isUseMIInFoldList(const std::vector<FoldCandidate> &FoldList,
145 const MachineInstr *MI) {
146 for (auto Candidate : FoldList) {
147 if (Candidate.UseMI == MI)
148 return true;
149 }
150 return false;
151}
152
Tom Stellard05992972015-01-07 22:44:19 +0000153static bool tryAddToFoldList(std::vector<FoldCandidate> &FoldList,
154 MachineInstr *MI, unsigned OpNo,
155 MachineOperand *OpToFold,
156 const SIInstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000157 if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000158
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000159 // Special case for v_mac_{f16, f32}_e64 if we are trying to fold into src2
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000160 unsigned Opc = MI->getOpcode();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000161 if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64) &&
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000162 (int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000163 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
164
165 // Check if changing this to a v_mad_{f16, f32} instruction will allow us
166 // to fold the operand.
167 MI->setDesc(TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16));
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000168 bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
169 if (FoldAsMAD) {
170 MI->untieRegOperand(OpNo);
171 return true;
172 }
173 MI->setDesc(TII->get(Opc));
174 }
175
176 // If we are already folding into another operand of MI, then
177 // we can't commute the instruction, otherwise we risk making the
178 // other fold illegal.
179 if (isUseMIInFoldList(FoldList, MI))
180 return false;
181
Tom Stellard05992972015-01-07 22:44:19 +0000182 // Operand is not legal, so try to commute the instruction to
183 // see if this makes it possible to fold.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000184 unsigned CommuteIdx0 = TargetInstrInfo::CommuteAnyOperandIndex;
185 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000186 bool CanCommute = TII->findCommutedOpIndices(*MI, CommuteIdx0, CommuteIdx1);
Tom Stellard05992972015-01-07 22:44:19 +0000187
188 if (CanCommute) {
189 if (CommuteIdx0 == OpNo)
190 OpNo = CommuteIdx1;
191 else if (CommuteIdx1 == OpNo)
192 OpNo = CommuteIdx0;
193 }
194
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000195 // One of operands might be an Imm operand, and OpNo may refer to it after
196 // the call of commuteInstruction() below. Such situations are avoided
197 // here explicitly as OpNo must be a register operand to be a candidate
198 // for memory folding.
199 if (CanCommute && (!MI->getOperand(CommuteIdx0).isReg() ||
200 !MI->getOperand(CommuteIdx1).isReg()))
201 return false;
202
203 if (!CanCommute ||
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000204 !TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1))
Tom Stellard05992972015-01-07 22:44:19 +0000205 return false;
206
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000207 if (!TII->isOperandLegal(*MI, OpNo, OpToFold))
Tom Stellard05992972015-01-07 22:44:19 +0000208 return false;
209 }
210
211 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
212 return true;
213}
214
Matt Arsenault5e63a042016-10-06 18:12:13 +0000215// If the use operand doesn't care about the value, this may be an operand only
216// used for register indexing, in which case it is unsafe to fold.
217static bool isUseSafeToFold(const MachineInstr &MI,
218 const MachineOperand &UseMO) {
219 return !UseMO.isUndef();
220 //return !MI.hasRegisterImplicitUseOperand(UseMO.getReg());
221}
222
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000223static void foldOperand(MachineOperand &OpToFold, MachineInstr *UseMI,
224 unsigned UseOpIdx,
225 std::vector<FoldCandidate> &FoldList,
Matt Arsenaultad46e0c2015-09-10 01:06:06 +0000226 SmallVectorImpl<MachineInstr *> &CopiesToReplace,
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000227 const SIInstrInfo *TII, const SIRegisterInfo &TRI,
228 MachineRegisterInfo &MRI) {
229 const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
230
Matt Arsenault5e63a042016-10-06 18:12:13 +0000231 if (!isUseSafeToFold(*UseMI, UseOp))
232 return;
233
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000234 // FIXME: Fold operands with subregs.
Matt Arsenault3661e902016-08-15 16:18:36 +0000235 if (UseOp.isReg() && OpToFold.isReg()) {
236 if (UseOp.isImplicit() || UseOp.getSubReg() != AMDGPU::NoSubRegister)
237 return;
238
239 // Don't fold subregister extracts into tied operands, only if it is a full
240 // copy since a subregister use tied to a full register def doesn't really
241 // make sense. e.g. don't fold:
242 //
243 // %vreg1 = COPY %vreg0:sub1
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000244 // %vreg2<tied3> = V_MAC_{F16, F32} %vreg3, %vreg4, %vreg1<tied0>
Matt Arsenault3661e902016-08-15 16:18:36 +0000245 //
246 // into
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000247 // %vreg2<tied3> = V_MAC_{F16, F32} %vreg3, %vreg4, %vreg0:sub1<tied0>
Matt Arsenault3661e902016-08-15 16:18:36 +0000248 if (UseOp.isTied() && OpToFold.getSubReg() != AMDGPU::NoSubRegister)
249 return;
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000250 }
251
252 bool FoldingImm = OpToFold.isImm();
253 APInt Imm;
254
255 if (FoldingImm) {
256 unsigned UseReg = UseOp.getReg();
257 const TargetRegisterClass *UseRC
258 = TargetRegisterInfo::isVirtualRegister(UseReg) ?
259 MRI.getRegClass(UseReg) :
260 TRI.getPhysRegClass(UseReg);
261
262 Imm = APInt(64, OpToFold.getImm());
263
Tom Stellardeea72cc2015-08-29 01:58:21 +0000264 const MCInstrDesc &FoldDesc = TII->get(OpToFold.getParent()->getOpcode());
265 const TargetRegisterClass *FoldRC =
266 TRI.getRegClass(FoldDesc.OpInfo[0].RegClass);
267
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000268 // Split 64-bit constants into 32-bits for folding.
Tom Stellardeea72cc2015-08-29 01:58:21 +0000269 if (FoldRC->getSize() == 8 && UseOp.getSubReg()) {
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000270 if (UseRC->getSize() != 8)
271 return;
272
273 if (UseOp.getSubReg() == AMDGPU::sub0) {
274 Imm = Imm.getLoBits(32);
275 } else {
276 assert(UseOp.getSubReg() == AMDGPU::sub1);
277 Imm = Imm.getHiBits(32);
278 }
279 }
280
281 // In order to fold immediates into copies, we need to change the
282 // copy to a MOV.
283 if (UseMI->getOpcode() == AMDGPU::COPY) {
284 unsigned DestReg = UseMI->getOperand(0).getReg();
285 const TargetRegisterClass *DestRC
286 = TargetRegisterInfo::isVirtualRegister(DestReg) ?
287 MRI.getRegClass(DestReg) :
288 TRI.getPhysRegClass(DestReg);
289
290 unsigned MovOp = TII->getMovOpcode(DestRC);
291 if (MovOp == AMDGPU::COPY)
292 return;
293
294 UseMI->setDesc(TII->get(MovOp));
Matt Arsenaultad46e0c2015-09-10 01:06:06 +0000295 CopiesToReplace.push_back(UseMI);
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000296 }
297 }
298
Tom Stellard9a197672015-09-09 15:43:26 +0000299 // Special case for REG_SEQUENCE: We can't fold literals into
300 // REG_SEQUENCE instructions, so we have to fold them into the
301 // uses of REG_SEQUENCE.
302 if (UseMI->getOpcode() == AMDGPU::REG_SEQUENCE) {
303 unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
304 unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
305
306 for (MachineRegisterInfo::use_iterator
307 RSUse = MRI.use_begin(RegSeqDstReg),
308 RSE = MRI.use_end(); RSUse != RSE; ++RSUse) {
309
310 MachineInstr *RSUseMI = RSUse->getParent();
311 if (RSUse->getSubReg() != RegSeqDstSubReg)
312 continue;
313
314 foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
Matt Arsenaultad46e0c2015-09-10 01:06:06 +0000315 CopiesToReplace, TII, TRI, MRI);
Tom Stellard9a197672015-09-09 15:43:26 +0000316 }
317 return;
318 }
319
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000320 const MCInstrDesc &UseDesc = UseMI->getDesc();
321
322 // Don't fold into target independent nodes. Target independent opcodes
323 // don't have defined register classes.
324 if (UseDesc.isVariadic() ||
325 UseDesc.OpInfo[UseOpIdx].RegClass == -1)
326 return;
327
328 if (FoldingImm) {
329 MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
330 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
331 return;
332 }
333
334 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
335
336 // FIXME: We could try to change the instruction from 64-bit to 32-bit
337 // to enable more folding opportunites. The shrink operands pass
338 // already does this.
339 return;
340}
341
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000342static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result,
343 int32_t LHS, int32_t RHS) {
344 switch (Opcode) {
345 case AMDGPU::V_AND_B32_e64:
346 case AMDGPU::S_AND_B32:
347 Result = LHS & RHS;
348 return true;
349 case AMDGPU::V_OR_B32_e64:
350 case AMDGPU::S_OR_B32:
351 Result = LHS | RHS;
352 return true;
353 case AMDGPU::V_XOR_B32_e64:
354 case AMDGPU::S_XOR_B32:
355 Result = LHS ^ RHS;
356 return true;
357 default:
358 return false;
359 }
360}
361
362static unsigned getMovOpc(bool IsScalar) {
363 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
364}
365
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000366/// Remove any leftover implicit operands from mutating the instruction. e.g.
367/// if we replace an s_and_b32 with a copy, we don't need the implicit scc def
368/// anymore.
369static void stripExtraCopyOperands(MachineInstr &MI) {
370 const MCInstrDesc &Desc = MI.getDesc();
371 unsigned NumOps = Desc.getNumOperands() +
372 Desc.getNumImplicitUses() +
373 Desc.getNumImplicitDefs();
374
375 for (unsigned I = MI.getNumOperands() - 1; I >= NumOps; --I)
376 MI.RemoveOperand(I);
377}
378
379static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc) {
380 MI.setDesc(NewDesc);
381 stripExtraCopyOperands(MI);
382}
383
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000384// Try to simplify operations with a constant that may appear after instruction
385// selection.
386static bool tryConstantFoldOp(MachineRegisterInfo &MRI,
387 const SIInstrInfo *TII,
388 MachineInstr *MI) {
389 unsigned Opc = MI->getOpcode();
390
391 if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
392 Opc == AMDGPU::S_NOT_B32) {
393 MachineOperand &Src0 = MI->getOperand(1);
394 if (Src0.isImm()) {
395 Src0.setImm(~Src0.getImm());
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000396 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_NOT_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000397 return true;
398 }
399
400 return false;
401 }
402
403 if (!MI->isCommutable())
404 return false;
405
406 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
407 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
408
409 MachineOperand *Src0 = &MI->getOperand(Src0Idx);
410 MachineOperand *Src1 = &MI->getOperand(Src1Idx);
411 if (!Src0->isImm() && !Src1->isImm())
412 return false;
413
414 // and k0, k1 -> v_mov_b32 (k0 & k1)
415 // or k0, k1 -> v_mov_b32 (k0 | k1)
416 // xor k0, k1 -> v_mov_b32 (k0 ^ k1)
417 if (Src0->isImm() && Src1->isImm()) {
418 int32_t NewImm;
419 if (!evalBinaryInstruction(Opc, NewImm, Src0->getImm(), Src1->getImm()))
420 return false;
421
422 const SIRegisterInfo &TRI = TII->getRegisterInfo();
423 bool IsSGPR = TRI.isSGPRReg(MRI, MI->getOperand(0).getReg());
424
425 Src0->setImm(NewImm);
426 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000427 mutateCopyOp(*MI, TII->get(getMovOpc(IsSGPR)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000428 return true;
429 }
430
431 if (Src0->isImm() && !Src1->isImm()) {
432 std::swap(Src0, Src1);
433 std::swap(Src0Idx, Src1Idx);
434 }
435
436 int32_t Src1Val = static_cast<int32_t>(Src1->getImm());
437 if (Opc == AMDGPU::V_OR_B32_e64 || Opc == AMDGPU::S_OR_B32) {
438 if (Src1Val == 0) {
439 // y = or x, 0 => y = copy x
440 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000441 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000442 } else if (Src1Val == -1) {
443 // y = or x, -1 => y = v_mov_b32 -1
444 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000445 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_OR_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000446 } else
447 return false;
448
449 return true;
450 }
451
452 if (MI->getOpcode() == AMDGPU::V_AND_B32_e64 ||
453 MI->getOpcode() == AMDGPU::S_AND_B32) {
454 if (Src1Val == 0) {
455 // y = and x, 0 => y = v_mov_b32 0
456 MI->RemoveOperand(Src0Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000457 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_AND_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000458 } else if (Src1Val == -1) {
459 // y = and x, -1 => y = copy x
460 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000461 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
462 stripExtraCopyOperands(*MI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000463 } else
464 return false;
465
466 return true;
467 }
468
469 if (MI->getOpcode() == AMDGPU::V_XOR_B32_e64 ||
470 MI->getOpcode() == AMDGPU::S_XOR_B32) {
471 if (Src1Val == 0) {
472 // y = xor x, 0 => y = copy x
473 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000474 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000475 }
476 }
477
478 return false;
479}
480
Tom Stellard6596ba72014-11-21 22:06:37 +0000481bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000482 if (skipFunction(*MF.getFunction()))
483 return false;
484
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000485 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
486
Tom Stellard6596ba72014-11-21 22:06:37 +0000487 MachineRegisterInfo &MRI = MF.getRegInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000488 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard6596ba72014-11-21 22:06:37 +0000489 const SIRegisterInfo &TRI = TII->getRegisterInfo();
490
491 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
492 BI != BE; ++BI) {
493
494 MachineBasicBlock &MBB = *BI;
495 MachineBasicBlock::iterator I, Next;
496 for (I = MBB.begin(); I != MBB.end(); I = Next) {
497 Next = std::next(I);
498 MachineInstr &MI = *I;
499
Matt Arsenault5e63a042016-10-06 18:12:13 +0000500 if (!isSafeToFold(MI))
Tom Stellard6596ba72014-11-21 22:06:37 +0000501 continue;
502
Matt Arsenault11a4d672015-02-13 19:05:03 +0000503 unsigned OpSize = TII->getOpSize(MI, 1);
Tom Stellard6596ba72014-11-21 22:06:37 +0000504 MachineOperand &OpToFold = MI.getOperand(1);
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000505 bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
Tom Stellard26cc18d2015-01-07 22:18:27 +0000506
Tom Stellard05992972015-01-07 22:44:19 +0000507 // FIXME: We could also be folding things like FrameIndexes and
508 // TargetIndexes.
509 if (!FoldingImm && !OpToFold.isReg())
510 continue;
511
Matt Arsenault25f61a62015-01-31 23:37:27 +0000512 // Folding immediates with more than one use will increase program size.
Tom Stellard26cc18d2015-01-07 22:18:27 +0000513 // FIXME: This will also reduce register usage, which may be better
514 // in some cases. A better heuristic is needed.
Matt Arsenault11a4d672015-02-13 19:05:03 +0000515 if (FoldingImm && !TII->isInlineConstant(OpToFold, OpSize) &&
Tom Stellard26cc18d2015-01-07 22:18:27 +0000516 !MRI.hasOneUse(MI.getOperand(0).getReg()))
517 continue;
Tom Stellard6596ba72014-11-21 22:06:37 +0000518
Tom Stellard6596ba72014-11-21 22:06:37 +0000519 if (OpToFold.isReg() &&
Nicolai Haehnle82fc9622016-01-07 17:10:29 +0000520 !TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()))
Tom Stellard6596ba72014-11-21 22:06:37 +0000521 continue;
522
Marek Olsak926c56f2016-01-13 11:44:29 +0000523 // Prevent folding operands backwards in the function. For example,
524 // the COPY opcode must not be replaced by 1 in this example:
525 //
526 // %vreg3<def> = COPY %VGPR0; VGPR_32:%vreg3
527 // ...
528 // %VGPR0<def> = V_MOV_B32_e32 1, %EXEC<imp-use>
529 MachineOperand &Dst = MI.getOperand(0);
530 if (Dst.isReg() &&
531 !TargetRegisterInfo::isVirtualRegister(Dst.getReg()))
532 continue;
533
Matt Arsenaultad46e0c2015-09-10 01:06:06 +0000534 // We need mutate the operands of new mov instructions to add implicit
535 // uses of EXEC, but adding them invalidates the use_iterator, so defer
536 // this.
537 SmallVector<MachineInstr *, 4> CopiesToReplace;
538
Tom Stellardbb763e62015-01-07 17:42:16 +0000539 std::vector<FoldCandidate> FoldList;
Tom Stellard6596ba72014-11-21 22:06:37 +0000540 for (MachineRegisterInfo::use_iterator
541 Use = MRI.use_begin(MI.getOperand(0).getReg()), E = MRI.use_end();
542 Use != E; ++Use) {
543
544 MachineInstr *UseMI = Use->getParent();
Tom Stellard6596ba72014-11-21 22:06:37 +0000545
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000546 foldOperand(OpToFold, UseMI, Use.getOperandNo(), FoldList,
Matt Arsenaultad46e0c2015-09-10 01:06:06 +0000547 CopiesToReplace, TII, TRI, MRI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000548 }
549
Matt Arsenaultad46e0c2015-09-10 01:06:06 +0000550 // Make sure we add EXEC uses to any new v_mov instructions created.
551 for (MachineInstr *Copy : CopiesToReplace)
552 Copy->addImplicitDefUseOperands(MF);
553
Tom Stellardbb763e62015-01-07 17:42:16 +0000554 for (FoldCandidate &Fold : FoldList) {
555 if (updateOperand(Fold, TRI)) {
Tom Stellard6596ba72014-11-21 22:06:37 +0000556 // Clear kill flags.
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000557 if (Fold.isReg()) {
Tom Stellardbb763e62015-01-07 17:42:16 +0000558 assert(Fold.OpToFold && Fold.OpToFold->isReg());
Matt Arsenaulte8c08912015-10-21 22:37:50 +0000559 // FIXME: Probably shouldn't bother trying to fold if not an
560 // SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
561 // copies.
562 MRI.clearKillFlags(Fold.OpToFold->getReg());
Tom Stellardbb763e62015-01-07 17:42:16 +0000563 }
Tom Stellard6596ba72014-11-21 22:06:37 +0000564 DEBUG(dbgs() << "Folded source from " << MI << " into OpNo " <<
Matt Arsenault391c3ea2016-11-23 21:51:05 +0000565 static_cast<int>(Fold.UseOpNo) << " of " << *Fold.UseMI << '\n');
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000566
567 // Folding the immediate may reveal operations that can be constant
568 // folded or replaced with a copy. This can happen for example after
569 // frame indices are lowered to constants or from splitting 64-bit
570 // constants.
571 tryConstantFoldOp(MRI, TII, Fold.UseMI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000572 }
573 }
574 }
575 }
576 return false;
577}