blob: d63414735b95a3b7b83110e769ca9d9353ec04c5 [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"
Tom Stellard6596ba72014-11-21 22:06:37 +000016#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000020#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000021#include "llvm/Support/raw_ostream.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000022#include "llvm/Target/TargetMachine.h"
23
24#define DEBUG_TYPE "si-fold-operands"
25using namespace llvm;
26
27namespace {
28
Tom Stellardbb763e62015-01-07 17:42:16 +000029struct FoldCandidate {
30 MachineInstr *UseMI;
Matt Arsenault2bc198a2016-09-14 15:51:33 +000031 union {
32 MachineOperand *OpToFold;
33 uint64_t ImmToFold;
34 int FrameIndexToFold;
35 };
36 unsigned char UseOpNo;
37 MachineOperand::MachineOperandType Kind;
Tom Stellardbb763e62015-01-07 17:42:16 +000038
39 FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp) :
Matt Arsenault2bc198a2016-09-14 15:51:33 +000040 UseMI(MI), OpToFold(nullptr), UseOpNo(OpNo), Kind(FoldOp->getType()) {
Tom Stellard05992972015-01-07 22:44:19 +000041 if (FoldOp->isImm()) {
Tom Stellard05992972015-01-07 22:44:19 +000042 ImmToFold = FoldOp->getImm();
Matt Arsenault2bc198a2016-09-14 15:51:33 +000043 } else if (FoldOp->isFI()) {
44 FrameIndexToFold = FoldOp->getIndex();
Tom Stellard05992972015-01-07 22:44:19 +000045 } else {
46 assert(FoldOp->isReg());
47 OpToFold = FoldOp;
48 }
49 }
Tom Stellardbb763e62015-01-07 17:42:16 +000050
Matt Arsenault2bc198a2016-09-14 15:51:33 +000051 bool isFI() const {
52 return Kind == MachineOperand::MO_FrameIndex;
53 }
54
Tom Stellardbb763e62015-01-07 17:42:16 +000055 bool isImm() const {
Matt Arsenault2bc198a2016-09-14 15:51:33 +000056 return Kind == MachineOperand::MO_Immediate;
57 }
58
59 bool isReg() const {
60 return Kind == MachineOperand::MO_Register;
Tom Stellardbb763e62015-01-07 17:42:16 +000061 }
62};
63
Matt Arsenault51818c12017-01-10 23:32:04 +000064class SIFoldOperands : public MachineFunctionPass {
65public:
66 static char ID;
67 MachineRegisterInfo *MRI;
68 const SIInstrInfo *TII;
69 const SIRegisterInfo *TRI;
Matt Arsenaultd5c65152017-02-22 23:27:53 +000070 const SISubtarget *ST;
Matt Arsenault51818c12017-01-10 23:32:04 +000071
72 void foldOperand(MachineOperand &OpToFold,
73 MachineInstr *UseMI,
74 unsigned UseOpIdx,
75 SmallVectorImpl<FoldCandidate> &FoldList,
76 SmallVectorImpl<MachineInstr *> &CopiesToReplace) const;
77
78 void foldInstOperand(MachineInstr &MI, MachineOperand &OpToFold) const;
79
Matt Arsenaultd5c65152017-02-22 23:27:53 +000080 const MachineOperand *isClamp(const MachineInstr &MI) const;
81 bool tryFoldClamp(MachineInstr &MI);
82
Matt Arsenault3cb39042017-02-27 19:35:42 +000083 std::pair<const MachineOperand *, int> isOMod(const MachineInstr &MI) const;
84 bool tryFoldOMod(MachineInstr &MI);
85
Matt Arsenault51818c12017-01-10 23:32:04 +000086public:
87 SIFoldOperands() : MachineFunctionPass(ID) {
88 initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
89 }
90
91 bool runOnMachineFunction(MachineFunction &MF) override;
92
93 StringRef getPassName() const override { return "SI Fold Operands"; }
94
95 void getAnalysisUsage(AnalysisUsage &AU) const override {
96 AU.setPreservesCFG();
97 MachineFunctionPass::getAnalysisUsage(AU);
98 }
99};
100
Tom Stellard6596ba72014-11-21 22:06:37 +0000101} // End anonymous namespace.
102
Matt Arsenault427c5482016-02-11 06:15:34 +0000103INITIALIZE_PASS(SIFoldOperands, DEBUG_TYPE,
104 "SI Fold Operands", false, false)
Tom Stellard6596ba72014-11-21 22:06:37 +0000105
106char SIFoldOperands::ID = 0;
107
108char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
109
Matt Arsenault69e30012017-01-11 22:00:02 +0000110// Wrapper around isInlineConstant that understands special cases when
111// instruction types are replaced during operand folding.
112static bool isInlineConstantIfFolded(const SIInstrInfo *TII,
113 const MachineInstr &UseMI,
114 unsigned OpNo,
115 const MachineOperand &OpToFold) {
116 if (TII->isInlineConstant(UseMI, OpNo, OpToFold))
117 return true;
118
119 unsigned Opc = UseMI.getOpcode();
120 switch (Opc) {
121 case AMDGPU::V_MAC_F32_e64:
122 case AMDGPU::V_MAC_F16_e64: {
123 // Special case for mac. Since this is replaced with mad when folded into
124 // src2, we need to check the legality for the final instruction.
125 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
126 if (static_cast<int>(OpNo) == Src2Idx) {
127 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
128 const MCInstrDesc &MadDesc
129 = TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
130 return TII->isInlineConstant(OpToFold, MadDesc.OpInfo[OpNo].OperandType);
131 }
132 }
133 default:
134 return false;
135 }
136}
137
Tom Stellard6596ba72014-11-21 22:06:37 +0000138FunctionPass *llvm::createSIFoldOperandsPass() {
139 return new SIFoldOperands();
140}
141
Tom Stellardbb763e62015-01-07 17:42:16 +0000142static bool updateOperand(FoldCandidate &Fold,
Tom Stellard6596ba72014-11-21 22:06:37 +0000143 const TargetRegisterInfo &TRI) {
Tom Stellardbb763e62015-01-07 17:42:16 +0000144 MachineInstr *MI = Fold.UseMI;
145 MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
Tom Stellard6596ba72014-11-21 22:06:37 +0000146 assert(Old.isReg());
147
Tom Stellardbb763e62015-01-07 17:42:16 +0000148 if (Fold.isImm()) {
149 Old.ChangeToImmediate(Fold.ImmToFold);
Tom Stellard6596ba72014-11-21 22:06:37 +0000150 return true;
151 }
152
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000153 if (Fold.isFI()) {
154 Old.ChangeToFrameIndex(Fold.FrameIndexToFold);
155 return true;
156 }
157
Tom Stellardbb763e62015-01-07 17:42:16 +0000158 MachineOperand *New = Fold.OpToFold;
159 if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
160 TargetRegisterInfo::isVirtualRegister(New->getReg())) {
161 Old.substVirtReg(New->getReg(), New->getSubReg(), TRI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000162 return true;
163 }
164
Tom Stellard6596ba72014-11-21 22:06:37 +0000165 // FIXME: Handle physical registers.
166
167 return false;
168}
169
Matt Arsenault51818c12017-01-10 23:32:04 +0000170static bool isUseMIInFoldList(ArrayRef<FoldCandidate> FoldList,
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000171 const MachineInstr *MI) {
172 for (auto Candidate : FoldList) {
173 if (Candidate.UseMI == MI)
174 return true;
175 }
176 return false;
177}
178
Matt Arsenault51818c12017-01-10 23:32:04 +0000179static bool tryAddToFoldList(SmallVectorImpl<FoldCandidate> &FoldList,
Tom Stellard05992972015-01-07 22:44:19 +0000180 MachineInstr *MI, unsigned OpNo,
181 MachineOperand *OpToFold,
182 const SIInstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000183 if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000184
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000185 // Special case for v_mac_{f16, f32}_e64 if we are trying to fold into src2
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000186 unsigned Opc = MI->getOpcode();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000187 if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64) &&
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000188 (int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
Matt Arsenault69e30012017-01-11 22:00:02 +0000189 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000190
191 // Check if changing this to a v_mad_{f16, f32} instruction will allow us
192 // to fold the operand.
193 MI->setDesc(TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16));
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000194 bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
195 if (FoldAsMAD) {
196 MI->untieRegOperand(OpNo);
197 return true;
198 }
199 MI->setDesc(TII->get(Opc));
200 }
201
Tom Stellard8485fa02016-12-07 02:42:15 +0000202 // Special case for s_setreg_b32
203 if (Opc == AMDGPU::S_SETREG_B32 && OpToFold->isImm()) {
204 MI->setDesc(TII->get(AMDGPU::S_SETREG_IMM32_B32));
205 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
206 return true;
207 }
208
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000209 // If we are already folding into another operand of MI, then
210 // we can't commute the instruction, otherwise we risk making the
211 // other fold illegal.
212 if (isUseMIInFoldList(FoldList, MI))
213 return false;
214
Tom Stellard05992972015-01-07 22:44:19 +0000215 // Operand is not legal, so try to commute the instruction to
216 // see if this makes it possible to fold.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000217 unsigned CommuteIdx0 = TargetInstrInfo::CommuteAnyOperandIndex;
218 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000219 bool CanCommute = TII->findCommutedOpIndices(*MI, CommuteIdx0, CommuteIdx1);
Tom Stellard05992972015-01-07 22:44:19 +0000220
221 if (CanCommute) {
222 if (CommuteIdx0 == OpNo)
223 OpNo = CommuteIdx1;
224 else if (CommuteIdx1 == OpNo)
225 OpNo = CommuteIdx0;
226 }
227
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000228 // One of operands might be an Imm operand, and OpNo may refer to it after
229 // the call of commuteInstruction() below. Such situations are avoided
230 // here explicitly as OpNo must be a register operand to be a candidate
231 // for memory folding.
232 if (CanCommute && (!MI->getOperand(CommuteIdx0).isReg() ||
233 !MI->getOperand(CommuteIdx1).isReg()))
234 return false;
235
236 if (!CanCommute ||
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000237 !TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1))
Tom Stellard05992972015-01-07 22:44:19 +0000238 return false;
239
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240 if (!TII->isOperandLegal(*MI, OpNo, OpToFold))
Tom Stellard05992972015-01-07 22:44:19 +0000241 return false;
242 }
243
244 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
245 return true;
246}
247
Matt Arsenault5e63a042016-10-06 18:12:13 +0000248// If the use operand doesn't care about the value, this may be an operand only
249// used for register indexing, in which case it is unsafe to fold.
250static bool isUseSafeToFold(const MachineInstr &MI,
251 const MachineOperand &UseMO) {
252 return !UseMO.isUndef();
253 //return !MI.hasRegisterImplicitUseOperand(UseMO.getReg());
254}
255
Matt Arsenault51818c12017-01-10 23:32:04 +0000256void SIFoldOperands::foldOperand(
257 MachineOperand &OpToFold,
258 MachineInstr *UseMI,
259 unsigned UseOpIdx,
260 SmallVectorImpl<FoldCandidate> &FoldList,
261 SmallVectorImpl<MachineInstr *> &CopiesToReplace) const {
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000262 const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
263
Matt Arsenault5e63a042016-10-06 18:12:13 +0000264 if (!isUseSafeToFold(*UseMI, UseOp))
265 return;
266
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000267 // FIXME: Fold operands with subregs.
Matt Arsenault3661e902016-08-15 16:18:36 +0000268 if (UseOp.isReg() && OpToFold.isReg()) {
269 if (UseOp.isImplicit() || UseOp.getSubReg() != AMDGPU::NoSubRegister)
270 return;
271
272 // Don't fold subregister extracts into tied operands, only if it is a full
273 // copy since a subregister use tied to a full register def doesn't really
274 // make sense. e.g. don't fold:
275 //
276 // %vreg1 = COPY %vreg0:sub1
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000277 // %vreg2<tied3> = V_MAC_{F16, F32} %vreg3, %vreg4, %vreg1<tied0>
Matt Arsenault3661e902016-08-15 16:18:36 +0000278 //
279 // into
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000280 // %vreg2<tied3> = V_MAC_{F16, F32} %vreg3, %vreg4, %vreg0:sub1<tied0>
Matt Arsenault3661e902016-08-15 16:18:36 +0000281 if (UseOp.isTied() && OpToFold.getSubReg() != AMDGPU::NoSubRegister)
282 return;
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000283 }
284
Tom Stellard9a197672015-09-09 15:43:26 +0000285 // Special case for REG_SEQUENCE: We can't fold literals into
286 // REG_SEQUENCE instructions, so we have to fold them into the
287 // uses of REG_SEQUENCE.
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000288 if (UseMI->isRegSequence()) {
Tom Stellard9a197672015-09-09 15:43:26 +0000289 unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
290 unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
291
292 for (MachineRegisterInfo::use_iterator
Matt Arsenault51818c12017-01-10 23:32:04 +0000293 RSUse = MRI->use_begin(RegSeqDstReg), RSE = MRI->use_end();
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000294 RSUse != RSE; ++RSUse) {
Tom Stellard9a197672015-09-09 15:43:26 +0000295
296 MachineInstr *RSUseMI = RSUse->getParent();
297 if (RSUse->getSubReg() != RegSeqDstSubReg)
298 continue;
299
300 foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
Matt Arsenault51818c12017-01-10 23:32:04 +0000301 CopiesToReplace);
Tom Stellard9a197672015-09-09 15:43:26 +0000302 }
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000303
Tom Stellard9a197672015-09-09 15:43:26 +0000304 return;
305 }
306
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000307
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000308 bool FoldingImm = OpToFold.isImm();
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000309
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000310 // In order to fold immediates into copies, we need to change the
311 // copy to a MOV.
312 if (FoldingImm && UseMI->isCopy()) {
313 unsigned DestReg = UseMI->getOperand(0).getReg();
314 const TargetRegisterClass *DestRC
315 = TargetRegisterInfo::isVirtualRegister(DestReg) ?
Matt Arsenault51818c12017-01-10 23:32:04 +0000316 MRI->getRegClass(DestReg) :
317 TRI->getPhysRegClass(DestReg);
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000318
319 unsigned MovOp = TII->getMovOpcode(DestRC);
320 if (MovOp == AMDGPU::COPY)
321 return;
322
323 UseMI->setDesc(TII->get(MovOp));
324 CopiesToReplace.push_back(UseMI);
325 } else {
326 const MCInstrDesc &UseDesc = UseMI->getDesc();
327
328 // Don't fold into target independent nodes. Target independent opcodes
329 // don't have defined register classes.
330 if (UseDesc.isVariadic() ||
331 UseDesc.OpInfo[UseOpIdx].RegClass == -1)
332 return;
333 }
334
335 if (!FoldingImm) {
336 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
337
338 // FIXME: We could try to change the instruction from 64-bit to 32-bit
339 // to enable more folding opportunites. The shrink operands pass
340 // already does this.
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000341 return;
342 }
343
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000344
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000345 const MCInstrDesc &FoldDesc = OpToFold.getParent()->getDesc();
346 const TargetRegisterClass *FoldRC =
Matt Arsenault51818c12017-01-10 23:32:04 +0000347 TRI->getRegClass(FoldDesc.OpInfo[0].RegClass);
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000348
Matt Arsenault4bd72362016-12-10 00:39:12 +0000349
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000350 // Split 64-bit constants into 32-bits for folding.
351 if (UseOp.getSubReg() && AMDGPU::getRegBitWidth(FoldRC->getID()) == 64) {
352 unsigned UseReg = UseOp.getReg();
353 const TargetRegisterClass *UseRC
354 = TargetRegisterInfo::isVirtualRegister(UseReg) ?
Matt Arsenault51818c12017-01-10 23:32:04 +0000355 MRI->getRegClass(UseReg) :
356 TRI->getPhysRegClass(UseReg);
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000357
358 if (AMDGPU::getRegBitWidth(UseRC->getID()) != 64)
359 return;
360
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000361 APInt Imm(64, OpToFold.getImm());
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000362 if (UseOp.getSubReg() == AMDGPU::sub0) {
363 Imm = Imm.getLoBits(32);
364 } else {
365 assert(UseOp.getSubReg() == AMDGPU::sub1);
366 Imm = Imm.getHiBits(32);
367 }
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000368
369 MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
370 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
371 return;
Matt Arsenaulta24d84b2016-11-23 21:51:07 +0000372 }
373
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000374
375
376 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000377}
378
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000379static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result,
Matt Arsenault51818c12017-01-10 23:32:04 +0000380 uint32_t LHS, uint32_t RHS) {
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000381 switch (Opcode) {
382 case AMDGPU::V_AND_B32_e64:
Matt Arsenault51818c12017-01-10 23:32:04 +0000383 case AMDGPU::V_AND_B32_e32:
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000384 case AMDGPU::S_AND_B32:
385 Result = LHS & RHS;
386 return true;
387 case AMDGPU::V_OR_B32_e64:
Matt Arsenault51818c12017-01-10 23:32:04 +0000388 case AMDGPU::V_OR_B32_e32:
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000389 case AMDGPU::S_OR_B32:
390 Result = LHS | RHS;
391 return true;
392 case AMDGPU::V_XOR_B32_e64:
Matt Arsenault51818c12017-01-10 23:32:04 +0000393 case AMDGPU::V_XOR_B32_e32:
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000394 case AMDGPU::S_XOR_B32:
395 Result = LHS ^ RHS;
396 return true;
Matt Arsenault51818c12017-01-10 23:32:04 +0000397 case AMDGPU::V_LSHL_B32_e64:
398 case AMDGPU::V_LSHL_B32_e32:
399 case AMDGPU::S_LSHL_B32:
400 // The instruction ignores the high bits for out of bounds shifts.
401 Result = LHS << (RHS & 31);
402 return true;
403 case AMDGPU::V_LSHLREV_B32_e64:
404 case AMDGPU::V_LSHLREV_B32_e32:
405 Result = RHS << (LHS & 31);
406 return true;
407 case AMDGPU::V_LSHR_B32_e64:
408 case AMDGPU::V_LSHR_B32_e32:
409 case AMDGPU::S_LSHR_B32:
410 Result = LHS >> (RHS & 31);
411 return true;
412 case AMDGPU::V_LSHRREV_B32_e64:
413 case AMDGPU::V_LSHRREV_B32_e32:
414 Result = RHS >> (LHS & 31);
415 return true;
416 case AMDGPU::V_ASHR_I32_e64:
417 case AMDGPU::V_ASHR_I32_e32:
418 case AMDGPU::S_ASHR_I32:
419 Result = static_cast<int32_t>(LHS) >> (RHS & 31);
420 return true;
421 case AMDGPU::V_ASHRREV_I32_e64:
422 case AMDGPU::V_ASHRREV_I32_e32:
423 Result = static_cast<int32_t>(RHS) >> (LHS & 31);
424 return true;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000425 default:
426 return false;
427 }
428}
429
430static unsigned getMovOpc(bool IsScalar) {
431 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
432}
433
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000434/// Remove any leftover implicit operands from mutating the instruction. e.g.
435/// if we replace an s_and_b32 with a copy, we don't need the implicit scc def
436/// anymore.
437static void stripExtraCopyOperands(MachineInstr &MI) {
438 const MCInstrDesc &Desc = MI.getDesc();
439 unsigned NumOps = Desc.getNumOperands() +
440 Desc.getNumImplicitUses() +
441 Desc.getNumImplicitDefs();
442
443 for (unsigned I = MI.getNumOperands() - 1; I >= NumOps; --I)
444 MI.RemoveOperand(I);
445}
446
447static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc) {
448 MI.setDesc(NewDesc);
449 stripExtraCopyOperands(MI);
450}
451
Matt Arsenault51818c12017-01-10 23:32:04 +0000452static MachineOperand *getImmOrMaterializedImm(MachineRegisterInfo &MRI,
453 MachineOperand &Op) {
454 if (Op.isReg()) {
455 // If this has a subregister, it obviously is a register source.
456 if (Op.getSubReg() != AMDGPU::NoSubRegister)
457 return &Op;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000458
Matt Arsenault51818c12017-01-10 23:32:04 +0000459 MachineInstr *Def = MRI.getVRegDef(Op.getReg());
460 if (Def->isMoveImmediate()) {
461 MachineOperand &ImmSrc = Def->getOperand(1);
462 if (ImmSrc.isImm())
463 return &ImmSrc;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000464 }
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000465 }
466
Matt Arsenault51818c12017-01-10 23:32:04 +0000467 return &Op;
468}
469
470// Try to simplify operations with a constant that may appear after instruction
471// selection.
472// TODO: See if a frame index with a fixed offset can fold.
473static bool tryConstantFoldOp(MachineRegisterInfo &MRI,
474 const SIInstrInfo *TII,
475 MachineInstr *MI,
476 MachineOperand *ImmOp) {
477 unsigned Opc = MI->getOpcode();
478 if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
479 Opc == AMDGPU::S_NOT_B32) {
480 MI->getOperand(1).ChangeToImmediate(~ImmOp->getImm());
481 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_NOT_B32)));
482 return true;
483 }
484
485 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
486 if (Src1Idx == -1)
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000487 return false;
488
489 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
Matt Arsenault51818c12017-01-10 23:32:04 +0000490 MachineOperand *Src0 = getImmOrMaterializedImm(MRI, MI->getOperand(Src0Idx));
491 MachineOperand *Src1 = getImmOrMaterializedImm(MRI, MI->getOperand(Src1Idx));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000492
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000493 if (!Src0->isImm() && !Src1->isImm())
494 return false;
495
496 // and k0, k1 -> v_mov_b32 (k0 & k1)
497 // or k0, k1 -> v_mov_b32 (k0 | k1)
498 // xor k0, k1 -> v_mov_b32 (k0 ^ k1)
499 if (Src0->isImm() && Src1->isImm()) {
500 int32_t NewImm;
501 if (!evalBinaryInstruction(Opc, NewImm, Src0->getImm(), Src1->getImm()))
502 return false;
503
504 const SIRegisterInfo &TRI = TII->getRegisterInfo();
505 bool IsSGPR = TRI.isSGPRReg(MRI, MI->getOperand(0).getReg());
506
Matt Arsenault51818c12017-01-10 23:32:04 +0000507 // Be careful to change the right operand, src0 may belong to a different
508 // instruction.
509 MI->getOperand(Src0Idx).ChangeToImmediate(NewImm);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000510 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000511 mutateCopyOp(*MI, TII->get(getMovOpc(IsSGPR)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000512 return true;
513 }
514
Matt Arsenault51818c12017-01-10 23:32:04 +0000515 if (!MI->isCommutable())
516 return false;
517
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000518 if (Src0->isImm() && !Src1->isImm()) {
519 std::swap(Src0, Src1);
520 std::swap(Src0Idx, Src1Idx);
521 }
522
523 int32_t Src1Val = static_cast<int32_t>(Src1->getImm());
Matt Arsenault51818c12017-01-10 23:32:04 +0000524 if (Opc == AMDGPU::V_OR_B32_e64 ||
525 Opc == AMDGPU::V_OR_B32_e32 ||
526 Opc == AMDGPU::S_OR_B32) {
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000527 if (Src1Val == 0) {
528 // y = or x, 0 => y = copy x
529 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000530 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000531 } else if (Src1Val == -1) {
532 // y = or x, -1 => y = v_mov_b32 -1
533 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000534 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_OR_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000535 } else
536 return false;
537
538 return true;
539 }
540
541 if (MI->getOpcode() == AMDGPU::V_AND_B32_e64 ||
Matt Arsenault51818c12017-01-10 23:32:04 +0000542 MI->getOpcode() == AMDGPU::V_AND_B32_e32 ||
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000543 MI->getOpcode() == AMDGPU::S_AND_B32) {
544 if (Src1Val == 0) {
545 // y = and x, 0 => y = v_mov_b32 0
546 MI->RemoveOperand(Src0Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000547 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_AND_B32)));
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000548 } else if (Src1Val == -1) {
549 // y = and x, -1 => y = copy x
550 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000551 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
552 stripExtraCopyOperands(*MI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000553 } else
554 return false;
555
556 return true;
557 }
558
559 if (MI->getOpcode() == AMDGPU::V_XOR_B32_e64 ||
Matt Arsenault51818c12017-01-10 23:32:04 +0000560 MI->getOpcode() == AMDGPU::V_XOR_B32_e32 ||
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000561 MI->getOpcode() == AMDGPU::S_XOR_B32) {
562 if (Src1Val == 0) {
563 // y = xor x, 0 => y = copy x
564 MI->RemoveOperand(Src1Idx);
Matt Arsenaultc2ee42c2016-10-06 17:54:30 +0000565 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
Matt Arsenault51818c12017-01-10 23:32:04 +0000566 return true;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000567 }
568 }
569
570 return false;
571}
572
Stanislav Mekhanoshin70603dc2017-03-24 18:55:20 +0000573// Try to fold an instruction into a simpler one
574static bool tryFoldInst(const SIInstrInfo *TII,
575 MachineInstr *MI) {
576 unsigned Opc = MI->getOpcode();
577
578 if (Opc == AMDGPU::V_CNDMASK_B32_e32 ||
579 Opc == AMDGPU::V_CNDMASK_B32_e64 ||
580 Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) {
581 const MachineOperand *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0);
582 const MachineOperand *Src1 = TII->getNamedOperand(*MI, AMDGPU::OpName::src1);
583 if (Src1->isIdenticalTo(*Src0)) {
584 DEBUG(dbgs() << "Folded " << *MI << " into ");
585 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
586 if (Src2Idx != -1)
587 MI->RemoveOperand(Src2Idx);
588 MI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1));
589 mutateCopyOp(*MI, TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY
590 : getMovOpc(false)));
591 DEBUG(dbgs() << *MI << '\n');
592 return true;
593 }
594 }
595
596 return false;
597}
598
Matt Arsenault51818c12017-01-10 23:32:04 +0000599void SIFoldOperands::foldInstOperand(MachineInstr &MI,
600 MachineOperand &OpToFold) const {
601 // We need mutate the operands of new mov instructions to add implicit
602 // uses of EXEC, but adding them invalidates the use_iterator, so defer
603 // this.
604 SmallVector<MachineInstr *, 4> CopiesToReplace;
605 SmallVector<FoldCandidate, 4> FoldList;
606 MachineOperand &Dst = MI.getOperand(0);
607
608 bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
609 if (FoldingImm) {
610 unsigned NumLiteralUses = 0;
611 MachineOperand *NonInlineUse = nullptr;
612 int NonInlineUseOpNo = -1;
613
614 MachineRegisterInfo::use_iterator NextUse, NextInstUse;
615 for (MachineRegisterInfo::use_iterator
616 Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
617 Use != E; Use = NextUse) {
618 NextUse = std::next(Use);
619 MachineInstr *UseMI = Use->getParent();
620 unsigned OpNo = Use.getOperandNo();
621
622 // Folding the immediate may reveal operations that can be constant
623 // folded or replaced with a copy. This can happen for example after
624 // frame indices are lowered to constants or from splitting 64-bit
625 // constants.
626 //
627 // We may also encounter cases where one or both operands are
628 // immediates materialized into a register, which would ordinarily not
629 // be folded due to multiple uses or operand constraints.
630
631 if (OpToFold.isImm() && tryConstantFoldOp(*MRI, TII, UseMI, &OpToFold)) {
632 DEBUG(dbgs() << "Constant folded " << *UseMI <<'\n');
633
634 // Some constant folding cases change the same immediate's use to a new
635 // instruction, e.g. and x, 0 -> 0. Make sure we re-visit the user
636 // again. The same constant folded instruction could also have a second
637 // use operand.
638 NextUse = MRI->use_begin(Dst.getReg());
639 continue;
640 }
641
642 // Try to fold any inline immediate uses, and then only fold other
643 // constants if they have one use.
644 //
645 // The legality of the inline immediate must be checked based on the use
646 // operand, not the defining instruction, because 32-bit instructions
647 // with 32-bit inline immediate sources may be used to materialize
648 // constants used in 16-bit operands.
649 //
650 // e.g. it is unsafe to fold:
651 // s_mov_b32 s0, 1.0 // materializes 0x3f800000
652 // v_add_f16 v0, v1, s0 // 1.0 f16 inline immediate sees 0x00003c00
653
654 // Folding immediates with more than one use will increase program size.
655 // FIXME: This will also reduce register usage, which may be better
656 // in some cases. A better heuristic is needed.
Matt Arsenault69e30012017-01-11 22:00:02 +0000657 if (isInlineConstantIfFolded(TII, *UseMI, OpNo, OpToFold)) {
Matt Arsenault51818c12017-01-10 23:32:04 +0000658 foldOperand(OpToFold, UseMI, OpNo, FoldList, CopiesToReplace);
659 } else {
660 if (++NumLiteralUses == 1) {
661 NonInlineUse = &*Use;
662 NonInlineUseOpNo = OpNo;
663 }
664 }
665 }
666
667 if (NumLiteralUses == 1) {
668 MachineInstr *UseMI = NonInlineUse->getParent();
669 foldOperand(OpToFold, UseMI, NonInlineUseOpNo, FoldList, CopiesToReplace);
670 }
671 } else {
672 // Folding register.
673 for (MachineRegisterInfo::use_iterator
674 Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
675 Use != E; ++Use) {
676 MachineInstr *UseMI = Use->getParent();
677
678 foldOperand(OpToFold, UseMI, Use.getOperandNo(),
679 FoldList, CopiesToReplace);
680 }
681 }
682
683 MachineFunction *MF = MI.getParent()->getParent();
684 // Make sure we add EXEC uses to any new v_mov instructions created.
685 for (MachineInstr *Copy : CopiesToReplace)
686 Copy->addImplicitDefUseOperands(*MF);
687
688 for (FoldCandidate &Fold : FoldList) {
689 if (updateOperand(Fold, *TRI)) {
690 // Clear kill flags.
691 if (Fold.isReg()) {
692 assert(Fold.OpToFold && Fold.OpToFold->isReg());
693 // FIXME: Probably shouldn't bother trying to fold if not an
694 // SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
695 // copies.
696 MRI->clearKillFlags(Fold.OpToFold->getReg());
697 }
698 DEBUG(dbgs() << "Folded source from " << MI << " into OpNo " <<
699 static_cast<int>(Fold.UseOpNo) << " of " << *Fold.UseMI << '\n');
Stanislav Mekhanoshin70603dc2017-03-24 18:55:20 +0000700 tryFoldInst(TII, Fold.UseMI);
Matt Arsenault51818c12017-01-10 23:32:04 +0000701 }
702 }
703}
704
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000705const MachineOperand *SIFoldOperands::isClamp(const MachineInstr &MI) const {
706 unsigned Op = MI.getOpcode();
707 switch (Op) {
708 case AMDGPU::V_MAX_F32_e64:
Matt Arsenault79a45db2017-02-22 23:53:37 +0000709 case AMDGPU::V_MAX_F16_e64:
710 case AMDGPU::V_MAX_F64: {
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000711 if (!TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
712 return nullptr;
713
714 // Make sure sources are identical.
715 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
716 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
717 if (!Src0->isReg() || Src0->getSubReg() != Src1->getSubReg() ||
718 Src0->getSubReg() != AMDGPU::NoSubRegister)
719 return nullptr;
720
721 // Can't fold up if we have modifiers.
722 if (TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
723 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
724 TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
725 return nullptr;
726 return Src0;
727 }
728 default:
729 return nullptr;
730 }
731}
732
733// We obviously have multiple uses in a clamp since the register is used twice
734// in the same instruction.
735static bool hasOneNonDBGUseInst(const MachineRegisterInfo &MRI, unsigned Reg) {
736 int Count = 0;
737 for (auto I = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
738 I != E; ++I) {
739 if (++Count > 1)
740 return false;
741 }
742
743 return true;
744}
745
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000746bool SIFoldOperands::tryFoldClamp(MachineInstr &MI) {
747 const MachineOperand *ClampSrc = isClamp(MI);
748 if (!ClampSrc || !hasOneNonDBGUseInst(*MRI, ClampSrc->getReg()))
749 return false;
750
751 MachineInstr *Def = MRI->getVRegDef(ClampSrc->getReg());
752 if (!TII->hasFPClamp(*Def))
753 return false;
754 MachineOperand *DefClamp = TII->getNamedOperand(*Def, AMDGPU::OpName::clamp);
755 if (!DefClamp)
756 return false;
757
758 DEBUG(dbgs() << "Folding clamp " << *DefClamp << " into " << *Def << '\n');
759
760 // Clamp is applied after omod, so it is OK if omod is set.
761 DefClamp->setImm(1);
762 MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
763 MI.eraseFromParent();
764 return true;
765}
766
Matt Arsenault3cb39042017-02-27 19:35:42 +0000767static int getOModValue(unsigned Opc, int64_t Val) {
768 switch (Opc) {
769 case AMDGPU::V_MUL_F32_e64: {
770 switch (static_cast<uint32_t>(Val)) {
771 case 0x3f000000: // 0.5
772 return SIOutMods::DIV2;
773 case 0x40000000: // 2.0
774 return SIOutMods::MUL2;
775 case 0x40800000: // 4.0
776 return SIOutMods::MUL4;
777 default:
778 return SIOutMods::NONE;
779 }
780 }
781 case AMDGPU::V_MUL_F16_e64: {
782 switch (static_cast<uint16_t>(Val)) {
783 case 0x3800: // 0.5
784 return SIOutMods::DIV2;
785 case 0x4000: // 2.0
786 return SIOutMods::MUL2;
787 case 0x4400: // 4.0
788 return SIOutMods::MUL4;
789 default:
790 return SIOutMods::NONE;
791 }
792 }
793 default:
794 llvm_unreachable("invalid mul opcode");
795 }
796}
797
798// FIXME: Does this really not support denormals with f16?
799// FIXME: Does this need to check IEEE mode bit? SNaNs are generally not
800// handled, so will anything other than that break?
801std::pair<const MachineOperand *, int>
802SIFoldOperands::isOMod(const MachineInstr &MI) const {
803 unsigned Op = MI.getOpcode();
804 switch (Op) {
805 case AMDGPU::V_MUL_F32_e64:
806 case AMDGPU::V_MUL_F16_e64: {
807 // If output denormals are enabled, omod is ignored.
808 if ((Op == AMDGPU::V_MUL_F32_e64 && ST->hasFP32Denormals()) ||
809 (Op == AMDGPU::V_MUL_F16_e64 && ST->hasFP16Denormals()))
810 return std::make_pair(nullptr, SIOutMods::NONE);
811
812 const MachineOperand *RegOp = nullptr;
813 const MachineOperand *ImmOp = nullptr;
814 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
815 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
816 if (Src0->isImm()) {
817 ImmOp = Src0;
818 RegOp = Src1;
819 } else if (Src1->isImm()) {
820 ImmOp = Src1;
821 RegOp = Src0;
822 } else
823 return std::make_pair(nullptr, SIOutMods::NONE);
824
825 int OMod = getOModValue(Op, ImmOp->getImm());
826 if (OMod == SIOutMods::NONE ||
827 TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
828 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
829 TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
830 TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
831 return std::make_pair(nullptr, SIOutMods::NONE);
832
833 return std::make_pair(RegOp, OMod);
834 }
835 case AMDGPU::V_ADD_F32_e64:
836 case AMDGPU::V_ADD_F16_e64: {
837 // If output denormals are enabled, omod is ignored.
838 if ((Op == AMDGPU::V_ADD_F32_e64 && ST->hasFP32Denormals()) ||
839 (Op == AMDGPU::V_ADD_F16_e64 && ST->hasFP16Denormals()))
840 return std::make_pair(nullptr, SIOutMods::NONE);
841
842 // Look through the DAGCombiner canonicalization fmul x, 2 -> fadd x, x
843 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
844 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
845
846 if (Src0->isReg() && Src1->isReg() && Src0->getReg() == Src1->getReg() &&
847 Src0->getSubReg() == Src1->getSubReg() &&
848 !TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
849 !TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
850 !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
851 !TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
852 return std::make_pair(Src0, SIOutMods::MUL2);
853
854 return std::make_pair(nullptr, SIOutMods::NONE);
855 }
856 default:
857 return std::make_pair(nullptr, SIOutMods::NONE);
858 }
859}
860
861// FIXME: Does this need to check IEEE bit on function?
862bool SIFoldOperands::tryFoldOMod(MachineInstr &MI) {
863 const MachineOperand *RegOp;
864 int OMod;
865 std::tie(RegOp, OMod) = isOMod(MI);
866 if (OMod == SIOutMods::NONE || !RegOp->isReg() ||
867 RegOp->getSubReg() != AMDGPU::NoSubRegister ||
868 !hasOneNonDBGUseInst(*MRI, RegOp->getReg()))
869 return false;
870
871 MachineInstr *Def = MRI->getVRegDef(RegOp->getReg());
872 MachineOperand *DefOMod = TII->getNamedOperand(*Def, AMDGPU::OpName::omod);
873 if (!DefOMod || DefOMod->getImm() != SIOutMods::NONE)
874 return false;
875
876 // Clamp is applied after omod. If the source already has clamp set, don't
877 // fold it.
878 if (TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
879 return false;
880
881 DEBUG(dbgs() << "Folding omod " << MI << " into " << *Def << '\n');
882
883 DefOMod->setImm(OMod);
884 MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
885 MI.eraseFromParent();
886 return true;
887}
888
Tom Stellard6596ba72014-11-21 22:06:37 +0000889bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor7de74af2016-04-25 22:23:44 +0000890 if (skipFunction(*MF.getFunction()))
891 return false;
892
Matt Arsenault51818c12017-01-10 23:32:04 +0000893 MRI = &MF.getRegInfo();
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000894 ST = &MF.getSubtarget<SISubtarget>();
895 TII = ST->getInstrInfo();
Matt Arsenault51818c12017-01-10 23:32:04 +0000896 TRI = &TII->getRegisterInfo();
Tom Stellard6596ba72014-11-21 22:06:37 +0000897
Matt Arsenault3cb39042017-02-27 19:35:42 +0000898 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
899
900 // omod is ignored by hardware if IEEE bit is enabled. omod also does not
901 // correctly handle signed zeros.
902 //
903 // TODO: Check nsz on instructions when fast math flags are preserved to MI
904 // level.
905 bool IsIEEEMode = ST->enableIEEEBit(MF) || !MFI->hasNoSignedZerosFPMath();
906
Tom Stellard6596ba72014-11-21 22:06:37 +0000907 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
Matt Arsenault51818c12017-01-10 23:32:04 +0000908 BI != BE; ++BI) {
Tom Stellard6596ba72014-11-21 22:06:37 +0000909
910 MachineBasicBlock &MBB = *BI;
911 MachineBasicBlock::iterator I, Next;
912 for (I = MBB.begin(); I != MBB.end(); I = Next) {
913 Next = std::next(I);
914 MachineInstr &MI = *I;
915
Stanislav Mekhanoshin70603dc2017-03-24 18:55:20 +0000916 tryFoldInst(TII, &MI);
917
Sam Kolton27e0f8b2017-03-31 11:42:43 +0000918 if (!TII->isFoldableCopy(MI)) {
Matt Arsenault3cb39042017-02-27 19:35:42 +0000919 if (IsIEEEMode || !tryFoldOMod(MI))
920 tryFoldClamp(MI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000921 continue;
Matt Arsenaultd5c65152017-02-22 23:27:53 +0000922 }
Tom Stellard6596ba72014-11-21 22:06:37 +0000923
924 MachineOperand &OpToFold = MI.getOperand(1);
Matt Arsenault2bc198a2016-09-14 15:51:33 +0000925 bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
Tom Stellard26cc18d2015-01-07 22:18:27 +0000926
Matt Arsenault51818c12017-01-10 23:32:04 +0000927 // FIXME: We could also be folding things like TargetIndexes.
Tom Stellard05992972015-01-07 22:44:19 +0000928 if (!FoldingImm && !OpToFold.isReg())
929 continue;
930
Tom Stellard6596ba72014-11-21 22:06:37 +0000931 if (OpToFold.isReg() &&
Nicolai Haehnle82fc9622016-01-07 17:10:29 +0000932 !TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()))
Tom Stellard6596ba72014-11-21 22:06:37 +0000933 continue;
934
Marek Olsak926c56f2016-01-13 11:44:29 +0000935 // Prevent folding operands backwards in the function. For example,
936 // the COPY opcode must not be replaced by 1 in this example:
937 //
938 // %vreg3<def> = COPY %VGPR0; VGPR_32:%vreg3
939 // ...
940 // %VGPR0<def> = V_MOV_B32_e32 1, %EXEC<imp-use>
941 MachineOperand &Dst = MI.getOperand(0);
942 if (Dst.isReg() &&
943 !TargetRegisterInfo::isVirtualRegister(Dst.getReg()))
944 continue;
945
Matt Arsenault51818c12017-01-10 23:32:04 +0000946 foldInstOperand(MI, OpToFold);
Tom Stellard6596ba72014-11-21 22:06:37 +0000947 }
948 }
949 return false;
950}