blob: 9fc38aeefaa21b2d744dd0189c1ea65a7160deb7 [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"
Matt Arsenault3cb39042017-02-27 19:35:42 +000015#include "SIMachineFunctionInfo.h"
Matt Arsenaultff3f9122017-06-20 18:56:32 +000016#include "llvm/ADT/DepthFirstIterator.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000021#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000022#include "llvm/Support/raw_ostream.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000023#include "llvm/Target/TargetMachine.h"
24
25#define DEBUG_TYPE "si-fold-operands"
26using namespace llvm;
27
28namespace {
29
Tom Stellardbb763e62015-01-07 17:42:16 +000030struct FoldCandidate {
31 MachineInstr *UseMI;
Matt Arsenault2bc198a2016-09-14 15:51:33 +000032 union {
33 MachineOperand *OpToFold;
34 uint64_t ImmToFold;
35 int FrameIndexToFold;
36 };
37 unsigned char UseOpNo;
38 MachineOperand::MachineOperandType Kind;
Stanislav Mekhanoshinf154b4f2017-06-03 00:41:52 +000039 bool Commuted;
Tom Stellardbb763e62015-01-07 17:42:16 +000040
Stanislav Mekhanoshinf154b4f2017-06-03 00:41:52 +000041 FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp,
42 bool Commuted_ = false) :
43 UseMI(MI), OpToFold(nullptr), UseOpNo(OpNo), Kind(FoldOp->getType()),
44 Commuted(Commuted_) {
Tom Stellard05992972015-01-07 22:44:19 +000045 if (FoldOp->isImm()) {
Tom Stellard05992972015-01-07 22:44:19 +000046 ImmToFold = FoldOp->getImm();
Matt Arsenault2bc198a2016-09-14 15:51:33 +000047 } else if (FoldOp->isFI()) {
48 FrameIndexToFold = FoldOp->getIndex();
Tom Stellard05992972015-01-07 22:44:19 +000049 } else {
50 assert(FoldOp->isReg());
51 OpToFold = FoldOp;
52 }
53 }
Tom Stellardbb763e62015-01-07 17:42:16 +000054
Matt Arsenault2bc198a2016-09-14 15:51:33 +000055 bool isFI() const {
56 return Kind == MachineOperand::MO_FrameIndex;
57 }
58
Tom Stellardbb763e62015-01-07 17:42:16 +000059 bool isImm() const {
Matt Arsenault2bc198a2016-09-14 15:51:33 +000060 return Kind == MachineOperand::MO_Immediate;
61 }
62
63 bool isReg() const {
64 return Kind == MachineOperand::MO_Register;
Tom Stellardbb763e62015-01-07 17:42:16 +000065 }
Stanislav Mekhanoshinf154b4f2017-06-03 00:41:52 +000066
67 bool isCommuted() const {
68 return Commuted;
69 }
Tom Stellardbb763e62015-01-07 17:42:16 +000070};
71
Matt Arsenault51818c12017-01-10 23:32:04 +000072class SIFoldOperands : public MachineFunctionPass {
73public:
74 static char ID;
75 MachineRegisterInfo *MRI;
76 const SIInstrInfo *TII;
77 const SIRegisterInfo *TRI;
Matt Arsenaultd5c65152017-02-22 23:27:53 +000078 const SISubtarget *ST;
Matt Arsenault51818c12017-01-10 23:32:04 +000079
80 void foldOperand(MachineOperand &OpToFold,
81 MachineInstr *UseMI,
82 unsigned UseOpIdx,
83 SmallVectorImpl<FoldCandidate> &FoldList,
84 SmallVectorImpl<MachineInstr *> &CopiesToReplace) const;
85
86 void foldInstOperand(MachineInstr &MI, MachineOperand &OpToFold) const;
87
Matt Arsenaultd5c65152017-02-22 23:27:53 +000088 const MachineOperand *isClamp(const MachineInstr &MI) const;
89 bool tryFoldClamp(MachineInstr &MI);
90
Matt Arsenault3cb39042017-02-27 19:35:42 +000091 std::pair<const MachineOperand *, int> isOMod(const MachineInstr &MI) const;
92 bool tryFoldOMod(MachineInstr &MI);
93
Matt Arsenault51818c12017-01-10 23:32:04 +000094public:
95 SIFoldOperands() : MachineFunctionPass(ID) {
96 initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
97 }
98
99 bool runOnMachineFunction(MachineFunction &MF) override;
100
101 StringRef getPassName() const override { return "SI Fold Operands"; }
102
103 void getAnalysisUsage(AnalysisUsage &AU) const override {
104 AU.setPreservesCFG();
105 MachineFunctionPass::getAnalysisUsage(AU);
106 }
107};
108
Tom Stellard6596ba72014-11-21 22:06:37 +0000109} // End anonymous namespace.
110
Matt Arsenault427c5482016-02-11 06:15:34 +0000111INITIALIZE_PASS(SIFoldOperands, DEBUG_TYPE,
112 "SI Fold Operands", false, false)
Tom Stellard6596ba72014-11-21 22:06:37 +0000113
114char SIFoldOperands::ID = 0;
115
116char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
117
Matt Arsenault69e30012017-01-11 22:00:02 +0000118// Wrapper around isInlineConstant that understands special cases when
119// instruction types are replaced during operand folding.
120static bool isInlineConstantIfFolded(const SIInstrInfo *TII,
121 const MachineInstr &UseMI,
122 unsigned OpNo,
123 const MachineOperand &OpToFold) {
124 if (TII->isInlineConstant(UseMI, OpNo, OpToFold))
125 return true;
126
127 unsigned Opc = UseMI.getOpcode();
128 switch (Opc) {
129 case AMDGPU::V_MAC_F32_e64:
130 case AMDGPU::V_MAC_F16_e64: {
131 // Special case for mac. Since this is replaced with mad when folded into
132 // src2, we need to check the legality for the final instruction.
133 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
134 if (static_cast<int>(OpNo) == Src2Idx) {
135 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
136 const MCInstrDesc &MadDesc
137 = TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
138 return TII->isInlineConstant(OpToFold, MadDesc.OpInfo[OpNo].OperandType);
139 }
Simon Pilgrim0f5b3502017-07-07 10:18:57 +0000140 return false;
Matt Arsenault69e30012017-01-11 22:00:02 +0000141 }
142 default:
143 return false;
144 }
145}
146
Tom Stellard6596ba72014-11-21 22:06:37 +0000147FunctionPass *llvm::createSIFoldOperandsPass() {
148 return new SIFoldOperands();
149}
150
Tom Stellardbb763e62015-01-07 17:42:16 +0000151static bool updateOperand(FoldCandidate &Fold,
Tom Stellard6596ba72014-11-21 22:06:37 +0000152 const TargetRegisterInfo &TRI) {
Tom Stellardbb763e62015-01-07 17:42:16 +0000153 MachineInstr *MI = Fold.UseMI;
154 MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
Tom Stellard6596ba72014-11-21 22:06:37 +0000155 assert(Old.isReg());
156
Tom Stellardbb763e62015-01-07 17:42:16 +0000157 if (Fold.isImm()) {
158 Old.ChangeToImmediate(Fold.ImmToFold);
Tom Stellard6596ba72014-11-21 22:06:37 +0000159 return true;
160 }
161
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000162 if (Fold.isFI()) {
163 Old.ChangeToFrameIndex(Fold.FrameIndexToFold);
164 return true;
165 }
166
Tom Stellardbb763e62015-01-07 17:42:16 +0000167 MachineOperand *New = Fold.OpToFold;
168 if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
169 TargetRegisterInfo::isVirtualRegister(New->getReg())) {
170 Old.substVirtReg(New->getReg(), New->getSubReg(), TRI);
Matt Arsenault76858f52017-06-20 18:41:31 +0000171
172 Old.setIsUndef(New->isUndef());
Tom Stellard6596ba72014-11-21 22:06:37 +0000173 return true;
174 }
175
Tom Stellard6596ba72014-11-21 22:06:37 +0000176 // FIXME: Handle physical registers.
177
178 return false;
179}
180
Matt Arsenault51818c12017-01-10 23:32:04 +0000181static bool isUseMIInFoldList(ArrayRef<FoldCandidate> FoldList,
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000182 const MachineInstr *MI) {
183 for (auto Candidate : FoldList) {
184 if (Candidate.UseMI == MI)
185 return true;
186 }
187 return false;
188}
189
Matt Arsenault51818c12017-01-10 23:32:04 +0000190static bool tryAddToFoldList(SmallVectorImpl<FoldCandidate> &FoldList,
Tom Stellard05992972015-01-07 22:44:19 +0000191 MachineInstr *MI, unsigned OpNo,
192 MachineOperand *OpToFold,
193 const SIInstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000194 if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000195
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000196 // Special case for v_mac_{f16, f32}_e64 if we are trying to fold into src2
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000197 unsigned Opc = MI->getOpcode();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000198 if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64) &&
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000199 (int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
Matt Arsenault69e30012017-01-11 22:00:02 +0000200 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000201
202 // Check if changing this to a v_mad_{f16, f32} instruction will allow us
203 // to fold the operand.
204 MI->setDesc(TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16));
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000205 bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
206 if (FoldAsMAD) {
207 MI->untieRegOperand(OpNo);
208 return true;
209 }
210 MI->setDesc(TII->get(Opc));
211 }
212
Tom Stellard8485fa02016-12-07 02:42:15 +0000213 // Special case for s_setreg_b32
214 if (Opc == AMDGPU::S_SETREG_B32 && OpToFold->isImm()) {
215 MI->setDesc(TII->get(AMDGPU::S_SETREG_IMM32_B32));
216 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
217 return true;
218 }
219
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000220 // If we are already folding into another operand of MI, then
221 // we can't commute the instruction, otherwise we risk making the
222 // other fold illegal.
223 if (isUseMIInFoldList(FoldList, MI))
224 return false;
225
Tom Stellard05992972015-01-07 22:44:19 +0000226 // Operand is not legal, so try to commute the instruction to
227 // see if this makes it possible to fold.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000228 unsigned CommuteIdx0 = TargetInstrInfo::CommuteAnyOperandIndex;
229 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230 bool CanCommute = TII->findCommutedOpIndices(*MI, CommuteIdx0, CommuteIdx1);
Tom Stellard05992972015-01-07 22:44:19 +0000231
232 if (CanCommute) {
233 if (CommuteIdx0 == OpNo)
234 OpNo = CommuteIdx1;
235 else if (CommuteIdx1 == OpNo)
236 OpNo = CommuteIdx0;
237 }
238
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000239 // One of operands might be an Imm operand, and OpNo may refer to it after
240 // the call of commuteInstruction() below. Such situations are avoided
241 // here explicitly as OpNo must be a register operand to be a candidate
242 // for memory folding.
243 if (CanCommute && (!MI->getOperand(CommuteIdx0).isReg() ||
244 !MI->getOperand(CommuteIdx1).isReg()))
245 return false;
246
247 if (!CanCommute ||
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248 !TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1))
Tom Stellard05992972015-01-07 22:44:19 +0000249 return false;
250
Stanislav Mekhanoshinf154b4f2017-06-03 00:41:52 +0000251 if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
252 TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1);
Tom Stellard05992972015-01-07 22:44:19 +0000253 return false;
Stanislav Mekhanoshinf154b4f2017-06-03 00:41:52 +0000254 }
255
256 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold, true));
257 return true;
Tom Stellard05992972015-01-07 22:44:19 +0000258 }
259
260 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
261 return true;
262}
263
Matt Arsenault5e63a042016-10-06 18:12:13 +0000264// If the use operand doesn't care about the value, this may be an operand only
265// used for register indexing, in which case it is unsafe to fold.
Stanislav Mekhanoshin56ea4882017-05-30 16:49:24 +0000266static bool isUseSafeToFold(const SIInstrInfo *TII,
267 const MachineInstr &MI,
Matt Arsenault5e63a042016-10-06 18:12:13 +0000268 const MachineOperand &UseMO) {
Stanislav Mekhanoshin56ea4882017-05-30 16:49:24 +0000269 return !UseMO.isUndef() && !TII->isSDWA(MI);
Matt Arsenault5e63a042016-10-06 18:12:13 +0000270 //return !MI.hasRegisterImplicitUseOperand(UseMO.getReg());
271}
272
Matt Arsenault51818c12017-01-10 23:32:04 +0000273void SIFoldOperands::foldOperand(
274 MachineOperand &OpToFold,
275 MachineInstr *UseMI,
276 unsigned UseOpIdx,
277 SmallVectorImpl<FoldCandidate> &FoldList,
278 SmallVectorImpl<MachineInstr *> &CopiesToReplace) const {
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000279 const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
280
Stanislav Mekhanoshin56ea4882017-05-30 16:49:24 +0000281 if (!isUseSafeToFold(TII, *UseMI, UseOp))
Matt Arsenault5e63a042016-10-06 18:12:13 +0000282 return;
283
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000284 // FIXME: Fold operands with subregs.
Matt Arsenault3661e902016-08-15 16:18:36 +0000285 if (UseOp.isReg() && OpToFold.isReg()) {
286 if (UseOp.isImplicit() || UseOp.getSubReg() != AMDGPU::NoSubRegister)
287 return;
288
289 // Don't fold subregister extracts into tied operands, only if it is a full
290 // copy since a subregister use tied to a full register def doesn't really
291 // make sense. e.g. don't fold:
292 //
293 // %vreg1 = COPY %vreg0:sub1
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000294 // %vreg2<tied3> = V_MAC_{F16, F32} %vreg3, %vreg4, %vreg1<tied0>
Matt Arsenault3661e902016-08-15 16:18:36 +0000295 //
296 // into
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000297 // %vreg2<tied3> = V_MAC_{F16, F32} %vreg3, %vreg4, %vreg0:sub1<tied0>
Matt Arsenault3661e902016-08-15 16:18:36 +0000298 if (UseOp.isTied() && OpToFold.getSubReg() != AMDGPU::NoSubRegister)
299 return;
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000300 }
301
Tom Stellard9a197672015-09-09 15:43:26 +0000302 // Special case for REG_SEQUENCE: We can't fold literals into
303 // REG_SEQUENCE instructions, so we have to fold them into the
304 // uses of REG_SEQUENCE.
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000305 if (UseMI->isRegSequence()) {
Tom Stellard9a197672015-09-09 15:43:26 +0000306 unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
307 unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
308
309 for (MachineRegisterInfo::use_iterator
Matt Arsenault51818c12017-01-10 23:32:04 +0000310 RSUse = MRI->use_begin(RegSeqDstReg), RSE = MRI->use_end();
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000311 RSUse != RSE; ++RSUse) {
Tom Stellard9a197672015-09-09 15:43:26 +0000312
313 MachineInstr *RSUseMI = RSUse->getParent();
314 if (RSUse->getSubReg() != RegSeqDstSubReg)
315 continue;
316
317 foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
Matt Arsenault51818c12017-01-10 23:32:04 +0000318 CopiesToReplace);
Tom Stellard9a197672015-09-09 15:43:26 +0000319 }
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000320
Tom Stellard9a197672015-09-09 15:43:26 +0000321 return;
322 }
323
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000324
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000325 bool FoldingImm = OpToFold.isImm();
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000326
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000327 // In order to fold immediates into copies, we need to change the
328 // copy to a MOV.
329 if (FoldingImm && UseMI->isCopy()) {
330 unsigned DestReg = UseMI->getOperand(0).getReg();
331 const TargetRegisterClass *DestRC
332 = TargetRegisterInfo::isVirtualRegister(DestReg) ?
Matt Arsenault51818c12017-01-10 23:32:04 +0000333 MRI->getRegClass(DestReg) :
334 TRI->getPhysRegClass(DestReg);
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000335
336 unsigned MovOp = TII->getMovOpcode(DestRC);
337 if (MovOp == AMDGPU::COPY)
338 return;
339
340 UseMI->setDesc(TII->get(MovOp));
341 CopiesToReplace.push_back(UseMI);
342 } else {
343 const MCInstrDesc &UseDesc = UseMI->getDesc();
344
345 // Don't fold into target independent nodes. Target independent opcodes
346 // don't have defined register classes.
347 if (UseDesc.isVariadic() ||
348 UseDesc.OpInfo[UseOpIdx].RegClass == -1)
349 return;
350 }
351
352 if (!FoldingImm) {
353 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
354
355 // FIXME: We could try to change the instruction from 64-bit to 32-bit
356 // to enable more folding opportunites. The shrink operands pass
357 // already does this.
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000358 return;
359 }
360
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000361
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000362 const MCInstrDesc &FoldDesc = OpToFold.getParent()->getDesc();
363 const TargetRegisterClass *FoldRC =
Matt Arsenault51818c12017-01-10 23:32:04 +0000364 TRI->getRegClass(FoldDesc.OpInfo[0].RegClass);
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000365
Matt Arsenault4bd72362016-12-10 00:39:12 +0000366
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000367 // Split 64-bit constants into 32-bits for folding.
368 if (UseOp.getSubReg() && AMDGPU::getRegBitWidth(FoldRC->getID()) == 64) {
369 unsigned UseReg = UseOp.getReg();
370 const TargetRegisterClass *UseRC
371 = TargetRegisterInfo::isVirtualRegister(UseReg) ?
Matt Arsenault51818c12017-01-10 23:32:04 +0000372 MRI->getRegClass(UseReg) :
373 TRI->getPhysRegClass(UseReg);
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000374
375 if (AMDGPU::getRegBitWidth(UseRC->getID()) != 64)
376 return;
377
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000378 APInt Imm(64, OpToFold.getImm());
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000379 if (UseOp.getSubReg() == AMDGPU::sub0) {
380 Imm = Imm.getLoBits(32);
381 } else {
382 assert(UseOp.getSubReg() == AMDGPU::sub1);
383 Imm = Imm.getHiBits(32);
384 }
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000385
386 MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
387 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
388 return;
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000389 }
390
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000391
392
393 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000394}
395
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000396static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result,
Matt Arsenault51818c12017-01-10 23:32:04 +0000397 uint32_t LHS, uint32_t RHS) {
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000398 switch (Opcode) {
399 case AMDGPU::V_AND_B32_e64:
Matt Arsenault51818c12017-01-10 23:32:04 +0000400 case AMDGPU::V_AND_B32_e32:
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000401 case AMDGPU::S_AND_B32:
402 Result = LHS & RHS;
403 return true;
404 case AMDGPU::V_OR_B32_e64:
Matt Arsenault51818c12017-01-10 23:32:04 +0000405 case AMDGPU::V_OR_B32_e32:
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000406 case AMDGPU::S_OR_B32:
407 Result = LHS | RHS;
408 return true;
409 case AMDGPU::V_XOR_B32_e64:
Matt Arsenault51818c12017-01-10 23:32:04 +0000410 case AMDGPU::V_XOR_B32_e32:
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000411 case AMDGPU::S_XOR_B32:
412 Result = LHS ^ RHS;
413 return true;
Matt Arsenault51818c12017-01-10 23:32:04 +0000414 case AMDGPU::V_LSHL_B32_e64:
415 case AMDGPU::V_LSHL_B32_e32:
416 case AMDGPU::S_LSHL_B32:
417 // The instruction ignores the high bits for out of bounds shifts.
418 Result = LHS << (RHS & 31);
419 return true;
420 case AMDGPU::V_LSHLREV_B32_e64:
421 case AMDGPU::V_LSHLREV_B32_e32:
422 Result = RHS << (LHS & 31);
423 return true;
424 case AMDGPU::V_LSHR_B32_e64:
425 case AMDGPU::V_LSHR_B32_e32:
426 case AMDGPU::S_LSHR_B32:
427 Result = LHS >> (RHS & 31);
428 return true;
429 case AMDGPU::V_LSHRREV_B32_e64:
430 case AMDGPU::V_LSHRREV_B32_e32:
431 Result = RHS >> (LHS & 31);
432 return true;
433 case AMDGPU::V_ASHR_I32_e64:
434 case AMDGPU::V_ASHR_I32_e32:
435 case AMDGPU::S_ASHR_I32:
436 Result = static_cast<int32_t>(LHS) >> (RHS & 31);
437 return true;
438 case AMDGPU::V_ASHRREV_I32_e64:
439 case AMDGPU::V_ASHRREV_I32_e32:
440 Result = static_cast<int32_t>(RHS) >> (LHS & 31);
441 return true;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000442 default:
443 return false;
444 }
445}
446
447static unsigned getMovOpc(bool IsScalar) {
448 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
449}
450
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000451/// Remove any leftover implicit operands from mutating the instruction. e.g.
452/// if we replace an s_and_b32 with a copy, we don't need the implicit scc def
453/// anymore.
454static void stripExtraCopyOperands(MachineInstr &MI) {
455 const MCInstrDesc &Desc = MI.getDesc();
456 unsigned NumOps = Desc.getNumOperands() +
457 Desc.getNumImplicitUses() +
458 Desc.getNumImplicitDefs();
459
460 for (unsigned I = MI.getNumOperands() - 1; I >= NumOps; --I)
461 MI.RemoveOperand(I);
462}
463
464static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc) {
465 MI.setDesc(NewDesc);
466 stripExtraCopyOperands(MI);
467}
468
Matt Arsenault51818c12017-01-10 23:32:04 +0000469static MachineOperand *getImmOrMaterializedImm(MachineRegisterInfo &MRI,
470 MachineOperand &Op) {
471 if (Op.isReg()) {
472 // If this has a subregister, it obviously is a register source.
473 if (Op.getSubReg() != AMDGPU::NoSubRegister)
474 return &Op;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000475
Matt Arsenault51818c12017-01-10 23:32:04 +0000476 MachineInstr *Def = MRI.getVRegDef(Op.getReg());
Matt Arsenault7f67b352017-06-20 18:28:02 +0000477 if (Def && Def->isMoveImmediate()) {
Matt Arsenault51818c12017-01-10 23:32:04 +0000478 MachineOperand &ImmSrc = Def->getOperand(1);
479 if (ImmSrc.isImm())
480 return &ImmSrc;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000481 }
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000482 }
483
Matt Arsenault51818c12017-01-10 23:32:04 +0000484 return &Op;
485}
486
487// Try to simplify operations with a constant that may appear after instruction
488// selection.
489// TODO: See if a frame index with a fixed offset can fold.
490static bool tryConstantFoldOp(MachineRegisterInfo &MRI,
491 const SIInstrInfo *TII,
492 MachineInstr *MI,
493 MachineOperand *ImmOp) {
494 unsigned Opc = MI->getOpcode();
495 if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
496 Opc == AMDGPU::S_NOT_B32) {
497 MI->getOperand(1).ChangeToImmediate(~ImmOp->getImm());
498 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_NOT_B32)));
499 return true;
500 }
501
502 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
503 if (Src1Idx == -1)
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000504 return false;
505
506 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
Matt Arsenault51818c12017-01-10 23:32:04 +0000507 MachineOperand *Src0 = getImmOrMaterializedImm(MRI, MI->getOperand(Src0Idx));
508 MachineOperand *Src1 = getImmOrMaterializedImm(MRI, MI->getOperand(Src1Idx));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000509
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000510 if (!Src0->isImm() && !Src1->isImm())
511 return false;
512
513 // and k0, k1 -> v_mov_b32 (k0 & k1)
514 // or k0, k1 -> v_mov_b32 (k0 | k1)
515 // xor k0, k1 -> v_mov_b32 (k0 ^ k1)
516 if (Src0->isImm() && Src1->isImm()) {
517 int32_t NewImm;
518 if (!evalBinaryInstruction(Opc, NewImm, Src0->getImm(), Src1->getImm()))
519 return false;
520
521 const SIRegisterInfo &TRI = TII->getRegisterInfo();
522 bool IsSGPR = TRI.isSGPRReg(MRI, MI->getOperand(0).getReg());
523
Matt Arsenault51818c12017-01-10 23:32:04 +0000524 // Be careful to change the right operand, src0 may belong to a different
525 // instruction.
526 MI->getOperand(Src0Idx).ChangeToImmediate(NewImm);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000527 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000528 mutateCopyOp(*MI, TII->get(getMovOpc(IsSGPR)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000529 return true;
530 }
531
Matt Arsenault51818c12017-01-10 23:32:04 +0000532 if (!MI->isCommutable())
533 return false;
534
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000535 if (Src0->isImm() && !Src1->isImm()) {
536 std::swap(Src0, Src1);
537 std::swap(Src0Idx, Src1Idx);
538 }
539
540 int32_t Src1Val = static_cast<int32_t>(Src1->getImm());
Matt Arsenault51818c12017-01-10 23:32:04 +0000541 if (Opc == AMDGPU::V_OR_B32_e64 ||
542 Opc == AMDGPU::V_OR_B32_e32 ||
543 Opc == AMDGPU::S_OR_B32) {
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000544 if (Src1Val == 0) {
545 // y = or x, 0 => y = copy x
546 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000547 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000548 } else if (Src1Val == -1) {
549 // y = or x, -1 => y = v_mov_b32 -1
550 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000551 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_OR_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000552 } else
553 return false;
554
555 return true;
556 }
557
558 if (MI->getOpcode() == AMDGPU::V_AND_B32_e64 ||
Matt Arsenault51818c12017-01-10 23:32:04 +0000559 MI->getOpcode() == AMDGPU::V_AND_B32_e32 ||
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000560 MI->getOpcode() == AMDGPU::S_AND_B32) {
561 if (Src1Val == 0) {
562 // y = and x, 0 => y = v_mov_b32 0
563 MI->RemoveOperand(Src0Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000564 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_AND_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000565 } else if (Src1Val == -1) {
566 // y = and x, -1 => y = copy x
567 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000568 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
569 stripExtraCopyOperands(*MI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000570 } else
571 return false;
572
573 return true;
574 }
575
576 if (MI->getOpcode() == AMDGPU::V_XOR_B32_e64 ||
Matt Arsenault51818c12017-01-10 23:32:04 +0000577 MI->getOpcode() == AMDGPU::V_XOR_B32_e32 ||
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000578 MI->getOpcode() == AMDGPU::S_XOR_B32) {
579 if (Src1Val == 0) {
580 // y = xor x, 0 => y = copy x
581 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000582 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
Matt Arsenault51818c12017-01-10 23:32:04 +0000583 return true;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000584 }
585 }
586
587 return false;
588}
589
Stanislav Mekhanoshin70603dc2017-03-24 18:55:20 +0000590// Try to fold an instruction into a simpler one
591static bool tryFoldInst(const SIInstrInfo *TII,
592 MachineInstr *MI) {
593 unsigned Opc = MI->getOpcode();
594
595 if (Opc == AMDGPU::V_CNDMASK_B32_e32 ||
596 Opc == AMDGPU::V_CNDMASK_B32_e64 ||
597 Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) {
598 const MachineOperand *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0);
599 const MachineOperand *Src1 = TII->getNamedOperand(*MI, AMDGPU::OpName::src1);
600 if (Src1->isIdenticalTo(*Src0)) {
601 DEBUG(dbgs() << "Folded " << *MI << " into ");
602 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
603 if (Src2Idx != -1)
604 MI->RemoveOperand(Src2Idx);
605 MI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1));
606 mutateCopyOp(*MI, TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY
607 : getMovOpc(false)));
608 DEBUG(dbgs() << *MI << '\n');
609 return true;
610 }
611 }
612
613 return false;
614}
615
Matt Arsenault51818c12017-01-10 23:32:04 +0000616void SIFoldOperands::foldInstOperand(MachineInstr &MI,
617 MachineOperand &OpToFold) const {
618 // We need mutate the operands of new mov instructions to add implicit
619 // uses of EXEC, but adding them invalidates the use_iterator, so defer
620 // this.
621 SmallVector<MachineInstr *, 4> CopiesToReplace;
622 SmallVector<FoldCandidate, 4> FoldList;
623 MachineOperand &Dst = MI.getOperand(0);
624
625 bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
626 if (FoldingImm) {
627 unsigned NumLiteralUses = 0;
628 MachineOperand *NonInlineUse = nullptr;
629 int NonInlineUseOpNo = -1;
630
631 MachineRegisterInfo::use_iterator NextUse, NextInstUse;
632 for (MachineRegisterInfo::use_iterator
633 Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
634 Use != E; Use = NextUse) {
635 NextUse = std::next(Use);
636 MachineInstr *UseMI = Use->getParent();
637 unsigned OpNo = Use.getOperandNo();
638
639 // Folding the immediate may reveal operations that can be constant
640 // folded or replaced with a copy. This can happen for example after
641 // frame indices are lowered to constants or from splitting 64-bit
642 // constants.
643 //
644 // We may also encounter cases where one or both operands are
645 // immediates materialized into a register, which would ordinarily not
646 // be folded due to multiple uses or operand constraints.
647
648 if (OpToFold.isImm() && tryConstantFoldOp(*MRI, TII, UseMI, &OpToFold)) {
649 DEBUG(dbgs() << "Constant folded " << *UseMI <<'\n');
650
651 // Some constant folding cases change the same immediate's use to a new
652 // instruction, e.g. and x, 0 -> 0. Make sure we re-visit the user
653 // again. The same constant folded instruction could also have a second
654 // use operand.
655 NextUse = MRI->use_begin(Dst.getReg());
Nicolai Haehnlea253e4c2017-07-18 14:54:41 +0000656 FoldList.clear();
Matt Arsenault51818c12017-01-10 23:32:04 +0000657 continue;
658 }
659
660 // Try to fold any inline immediate uses, and then only fold other
661 // constants if they have one use.
662 //
663 // The legality of the inline immediate must be checked based on the use
664 // operand, not the defining instruction, because 32-bit instructions
665 // with 32-bit inline immediate sources may be used to materialize
666 // constants used in 16-bit operands.
667 //
668 // e.g. it is unsafe to fold:
669 // s_mov_b32 s0, 1.0 // materializes 0x3f800000
670 // v_add_f16 v0, v1, s0 // 1.0 f16 inline immediate sees 0x00003c00
671
672 // Folding immediates with more than one use will increase program size.
673 // FIXME: This will also reduce register usage, which may be better
674 // in some cases. A better heuristic is needed.
Matt Arsenault69e30012017-01-11 22:00:02 +0000675 if (isInlineConstantIfFolded(TII, *UseMI, OpNo, OpToFold)) {
Matt Arsenault51818c12017-01-10 23:32:04 +0000676 foldOperand(OpToFold, UseMI, OpNo, FoldList, CopiesToReplace);
677 } else {
678 if (++NumLiteralUses == 1) {
679 NonInlineUse = &*Use;
680 NonInlineUseOpNo = OpNo;
681 }
682 }
683 }
684
685 if (NumLiteralUses == 1) {
686 MachineInstr *UseMI = NonInlineUse->getParent();
687 foldOperand(OpToFold, UseMI, NonInlineUseOpNo, FoldList, CopiesToReplace);
688 }
689 } else {
690 // Folding register.
691 for (MachineRegisterInfo::use_iterator
692 Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
693 Use != E; ++Use) {
694 MachineInstr *UseMI = Use->getParent();
695
696 foldOperand(OpToFold, UseMI, Use.getOperandNo(),
697 FoldList, CopiesToReplace);
698 }
699 }
700
701 MachineFunction *MF = MI.getParent()->getParent();
702 // Make sure we add EXEC uses to any new v_mov instructions created.
703 for (MachineInstr *Copy : CopiesToReplace)
704 Copy->addImplicitDefUseOperands(*MF);
705
706 for (FoldCandidate &Fold : FoldList) {
707 if (updateOperand(Fold, *TRI)) {
708 // Clear kill flags.
709 if (Fold.isReg()) {
710 assert(Fold.OpToFold && Fold.OpToFold->isReg());
711 // FIXME: Probably shouldn't bother trying to fold if not an
712 // SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
713 // copies.
714 MRI->clearKillFlags(Fold.OpToFold->getReg());
715 }
716 DEBUG(dbgs() << "Folded source from " << MI << " into OpNo " <<
717 static_cast<int>(Fold.UseOpNo) << " of " << *Fold.UseMI << '\n');
Stanislav Mekhanoshin70603dc2017-03-24 18:55:20 +0000718 tryFoldInst(TII, Fold.UseMI);
Stanislav Mekhanoshinf154b4f2017-06-03 00:41:52 +0000719 } else if (Fold.isCommuted()) {
720 // Restoring instruction's original operand order if fold has failed.
721 TII->commuteInstruction(*Fold.UseMI, false);
Matt Arsenault51818c12017-01-10 23:32:04 +0000722 }
723 }
724}
725
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000726const MachineOperand *SIFoldOperands::isClamp(const MachineInstr &MI) const {
727 unsigned Op = MI.getOpcode();
728 switch (Op) {
729 case AMDGPU::V_MAX_F32_e64:
Matt Arsenault79a45db2017-02-22 23:53:37 +0000730 case AMDGPU::V_MAX_F16_e64:
Matt Arsenaultab4a5cd2017-08-31 23:53:50 +0000731 case AMDGPU::V_MAX_F64:
732 case AMDGPU::V_PK_MAX_F16: {
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000733 if (!TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
734 return nullptr;
735
736 // Make sure sources are identical.
737 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
738 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
Stanislav Mekhanoshin286a4222017-06-05 01:03:04 +0000739 if (!Src0->isReg() || !Src1->isReg() ||
740 Src0->getSubReg() != Src1->getSubReg() ||
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000741 Src0->getSubReg() != AMDGPU::NoSubRegister)
742 return nullptr;
743
744 // Can't fold up if we have modifiers.
Matt Arsenaultab4a5cd2017-08-31 23:53:50 +0000745 if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
746 return nullptr;
747
748 unsigned Src0Mods
749 = TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm();
750 unsigned Src1Mods
751 = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm();
752
753 // Having a 0 op_sel_hi would require swizzling the output in the source
754 // instruction, which we can't do.
755 unsigned UnsetMods = (Op == AMDGPU::V_PK_MAX_F16) ? SISrcMods::OP_SEL_1 : 0;
756 if (Src0Mods != UnsetMods && Src1Mods != UnsetMods)
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000757 return nullptr;
758 return Src0;
759 }
760 default:
761 return nullptr;
762 }
763}
764
765// We obviously have multiple uses in a clamp since the register is used twice
766// in the same instruction.
767static bool hasOneNonDBGUseInst(const MachineRegisterInfo &MRI, unsigned Reg) {
768 int Count = 0;
769 for (auto I = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
770 I != E; ++I) {
771 if (++Count > 1)
772 return false;
773 }
774
775 return true;
776}
777
Matt Arsenault8cbb4882017-09-20 21:01:24 +0000778// FIXME: Clamp for v_mad_mixhi_f16 handled during isel.
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000779bool SIFoldOperands::tryFoldClamp(MachineInstr &MI) {
780 const MachineOperand *ClampSrc = isClamp(MI);
781 if (!ClampSrc || !hasOneNonDBGUseInst(*MRI, ClampSrc->getReg()))
782 return false;
783
784 MachineInstr *Def = MRI->getVRegDef(ClampSrc->getReg());
Matt Arsenaultab4a5cd2017-08-31 23:53:50 +0000785
786 // The type of clamp must be compatible.
787 if (TII->getClampMask(*Def) != TII->getClampMask(MI))
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000788 return false;
Matt Arsenaultab4a5cd2017-08-31 23:53:50 +0000789
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000790 MachineOperand *DefClamp = TII->getNamedOperand(*Def, AMDGPU::OpName::clamp);
791 if (!DefClamp)
792 return false;
793
794 DEBUG(dbgs() << "Folding clamp " << *DefClamp << " into " << *Def << '\n');
795
796 // Clamp is applied after omod, so it is OK if omod is set.
797 DefClamp->setImm(1);
798 MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
799 MI.eraseFromParent();
800 return true;
801}
802
Matt Arsenault3cb39042017-02-27 19:35:42 +0000803static int getOModValue(unsigned Opc, int64_t Val) {
804 switch (Opc) {
805 case AMDGPU::V_MUL_F32_e64: {
806 switch (static_cast<uint32_t>(Val)) {
807 case 0x3f000000: // 0.5
808 return SIOutMods::DIV2;
809 case 0x40000000: // 2.0
810 return SIOutMods::MUL2;
811 case 0x40800000: // 4.0
812 return SIOutMods::MUL4;
813 default:
814 return SIOutMods::NONE;
815 }
816 }
817 case AMDGPU::V_MUL_F16_e64: {
818 switch (static_cast<uint16_t>(Val)) {
819 case 0x3800: // 0.5
820 return SIOutMods::DIV2;
821 case 0x4000: // 2.0
822 return SIOutMods::MUL2;
823 case 0x4400: // 4.0
824 return SIOutMods::MUL4;
825 default:
826 return SIOutMods::NONE;
827 }
828 }
829 default:
830 llvm_unreachable("invalid mul opcode");
831 }
832}
833
834// FIXME: Does this really not support denormals with f16?
835// FIXME: Does this need to check IEEE mode bit? SNaNs are generally not
836// handled, so will anything other than that break?
837std::pair<const MachineOperand *, int>
838SIFoldOperands::isOMod(const MachineInstr &MI) const {
839 unsigned Op = MI.getOpcode();
840 switch (Op) {
841 case AMDGPU::V_MUL_F32_e64:
842 case AMDGPU::V_MUL_F16_e64: {
843 // If output denormals are enabled, omod is ignored.
844 if ((Op == AMDGPU::V_MUL_F32_e64 && ST->hasFP32Denormals()) ||
845 (Op == AMDGPU::V_MUL_F16_e64 && ST->hasFP16Denormals()))
846 return std::make_pair(nullptr, SIOutMods::NONE);
847
848 const MachineOperand *RegOp = nullptr;
849 const MachineOperand *ImmOp = nullptr;
850 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
851 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
852 if (Src0->isImm()) {
853 ImmOp = Src0;
854 RegOp = Src1;
855 } else if (Src1->isImm()) {
856 ImmOp = Src1;
857 RegOp = Src0;
858 } else
859 return std::make_pair(nullptr, SIOutMods::NONE);
860
861 int OMod = getOModValue(Op, ImmOp->getImm());
862 if (OMod == SIOutMods::NONE ||
863 TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
864 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
865 TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
866 TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
867 return std::make_pair(nullptr, SIOutMods::NONE);
868
869 return std::make_pair(RegOp, OMod);
870 }
871 case AMDGPU::V_ADD_F32_e64:
872 case AMDGPU::V_ADD_F16_e64: {
873 // If output denormals are enabled, omod is ignored.
874 if ((Op == AMDGPU::V_ADD_F32_e64 && ST->hasFP32Denormals()) ||
875 (Op == AMDGPU::V_ADD_F16_e64 && ST->hasFP16Denormals()))
876 return std::make_pair(nullptr, SIOutMods::NONE);
877
878 // Look through the DAGCombiner canonicalization fmul x, 2 -> fadd x, x
879 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
880 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
881
882 if (Src0->isReg() && Src1->isReg() && Src0->getReg() == Src1->getReg() &&
883 Src0->getSubReg() == Src1->getSubReg() &&
884 !TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
885 !TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
886 !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
887 !TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
888 return std::make_pair(Src0, SIOutMods::MUL2);
889
890 return std::make_pair(nullptr, SIOutMods::NONE);
891 }
892 default:
893 return std::make_pair(nullptr, SIOutMods::NONE);
894 }
895}
896
897// FIXME: Does this need to check IEEE bit on function?
898bool SIFoldOperands::tryFoldOMod(MachineInstr &MI) {
899 const MachineOperand *RegOp;
900 int OMod;
901 std::tie(RegOp, OMod) = isOMod(MI);
902 if (OMod == SIOutMods::NONE || !RegOp->isReg() ||
903 RegOp->getSubReg() != AMDGPU::NoSubRegister ||
904 !hasOneNonDBGUseInst(*MRI, RegOp->getReg()))
905 return false;
906
907 MachineInstr *Def = MRI->getVRegDef(RegOp->getReg());
908 MachineOperand *DefOMod = TII->getNamedOperand(*Def, AMDGPU::OpName::omod);
909 if (!DefOMod || DefOMod->getImm() != SIOutMods::NONE)
910 return false;
911
912 // Clamp is applied after omod. If the source already has clamp set, don't
913 // fold it.
914 if (TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
915 return false;
916
917 DEBUG(dbgs() << "Folding omod " << MI << " into " << *Def << '\n');
918
919 DefOMod->setImm(OMod);
920 MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
921 MI.eraseFromParent();
922 return true;
923}
924
Tom Stellard6596ba72014-11-21 22:06:37 +0000925bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000926 if (skipFunction(*MF.getFunction()))
927 return false;
928
Matt Arsenault51818c12017-01-10 23:32:04 +0000929 MRI = &MF.getRegInfo();
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000930 ST = &MF.getSubtarget<SISubtarget>();
931 TII = ST->getInstrInfo();
Matt Arsenault51818c12017-01-10 23:32:04 +0000932 TRI = &TII->getRegisterInfo();
Tom Stellard6596ba72014-11-21 22:06:37 +0000933
Matt Arsenault3cb39042017-02-27 19:35:42 +0000934 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
935
936 // omod is ignored by hardware if IEEE bit is enabled. omod also does not
937 // correctly handle signed zeros.
938 //
939 // TODO: Check nsz on instructions when fast math flags are preserved to MI
940 // level.
941 bool IsIEEEMode = ST->enableIEEEBit(MF) || !MFI->hasNoSignedZerosFPMath();
942
Matt Arsenaultff3f9122017-06-20 18:56:32 +0000943 for (MachineBasicBlock *MBB : depth_first(&MF)) {
Tom Stellard6596ba72014-11-21 22:06:37 +0000944 MachineBasicBlock::iterator I, Next;
Matt Arsenaultff3f9122017-06-20 18:56:32 +0000945 for (I = MBB->begin(); I != MBB->end(); I = Next) {
Tom Stellard6596ba72014-11-21 22:06:37 +0000946 Next = std::next(I);
947 MachineInstr &MI = *I;
948
Stanislav Mekhanoshin70603dc2017-03-24 18:55:20 +0000949 tryFoldInst(TII, &MI);
950
Sam Kolton27e0f8b2017-03-31 11:42:43 +0000951 if (!TII->isFoldableCopy(MI)) {
Matt Arsenault3cb39042017-02-27 19:35:42 +0000952 if (IsIEEEMode || !tryFoldOMod(MI))
953 tryFoldClamp(MI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000954 continue;
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000955 }
Tom Stellard6596ba72014-11-21 22:06:37 +0000956
957 MachineOperand &OpToFold = MI.getOperand(1);
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000958 bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
Tom Stellard26cc18d2015-01-07 22:18:27 +0000959
Matt Arsenault51818c12017-01-10 23:32:04 +0000960 // FIXME: We could also be folding things like TargetIndexes.
Tom Stellard05992972015-01-07 22:44:19 +0000961 if (!FoldingImm && !OpToFold.isReg())
962 continue;
963
Tom Stellard6596ba72014-11-21 22:06:37 +0000964 if (OpToFold.isReg() &&
Nicolai Haehnle82fc9622016-01-07 17:10:29 +0000965 !TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()))
Tom Stellard6596ba72014-11-21 22:06:37 +0000966 continue;
967
Marek Olsak926c56f2016-01-13 11:44:29 +0000968 // Prevent folding operands backwards in the function. For example,
969 // the COPY opcode must not be replaced by 1 in this example:
970 //
971 // %vreg3<def> = COPY %VGPR0; VGPR_32:%vreg3
972 // ...
973 // %VGPR0<def> = V_MOV_B32_e32 1, %EXEC<imp-use>
974 MachineOperand &Dst = MI.getOperand(0);
975 if (Dst.isReg() &&
976 !TargetRegisterInfo::isVirtualRegister(Dst.getReg()))
977 continue;
978
Matt Arsenault51818c12017-01-10 23:32:04 +0000979 foldInstOperand(MI, OpToFold);
Tom Stellard6596ba72014-11-21 22:06:37 +0000980 }
981 }
982 return false;
983}