blob: fe11385d0cd77aef20aa7af8fc57680d0467b7e0 [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"
16#include "llvm/CodeGen/MachineDominators.h"
17#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/IR/Function.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000021#include "llvm/IR/LLVMContext.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000022#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000023#include "llvm/Support/raw_ostream.h"
Tom Stellard6596ba72014-11-21 22:06:37 +000024#include "llvm/Target/TargetMachine.h"
25
26#define DEBUG_TYPE "si-fold-operands"
27using namespace llvm;
28
29namespace {
30
31class SIFoldOperands : public MachineFunctionPass {
32public:
33 static char ID;
34
35public:
36 SIFoldOperands() : MachineFunctionPass(ID) {
37 initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
38 }
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42 const char *getPassName() const override {
43 return "SI Fold Operands";
44 }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.addRequired<MachineDominatorTree>();
48 AU.setPreservesCFG();
49 MachineFunctionPass::getAnalysisUsage(AU);
50 }
51};
52
Tom Stellardbb763e62015-01-07 17:42:16 +000053struct FoldCandidate {
54 MachineInstr *UseMI;
55 unsigned UseOpNo;
56 MachineOperand *OpToFold;
57 uint64_t ImmToFold;
58
59 FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp) :
Tom Stellard05992972015-01-07 22:44:19 +000060 UseMI(MI), UseOpNo(OpNo) {
Tom Stellardbb763e62015-01-07 17:42:16 +000061
Tom Stellard05992972015-01-07 22:44:19 +000062 if (FoldOp->isImm()) {
63 OpToFold = nullptr;
64 ImmToFold = FoldOp->getImm();
65 } else {
66 assert(FoldOp->isReg());
67 OpToFold = FoldOp;
68 }
69 }
Tom Stellardbb763e62015-01-07 17:42:16 +000070
71 bool isImm() const {
72 return !OpToFold;
73 }
74};
75
Tom Stellard6596ba72014-11-21 22:06:37 +000076} // End anonymous namespace.
77
78INITIALIZE_PASS_BEGIN(SIFoldOperands, DEBUG_TYPE,
79 "SI Fold Operands", false, false)
80INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
81INITIALIZE_PASS_END(SIFoldOperands, DEBUG_TYPE,
82 "SI Fold Operands", false, false)
83
84char SIFoldOperands::ID = 0;
85
86char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
87
88FunctionPass *llvm::createSIFoldOperandsPass() {
89 return new SIFoldOperands();
90}
91
92static bool isSafeToFold(unsigned Opcode) {
93 switch(Opcode) {
94 case AMDGPU::V_MOV_B32_e32:
95 case AMDGPU::V_MOV_B32_e64:
Tom Stellard4842c052015-01-07 20:27:25 +000096 case AMDGPU::V_MOV_B64_PSEUDO:
Tom Stellard6596ba72014-11-21 22:06:37 +000097 case AMDGPU::S_MOV_B32:
98 case AMDGPU::S_MOV_B64:
99 case AMDGPU::COPY:
100 return true;
101 default:
102 return false;
103 }
104}
105
Tom Stellardbb763e62015-01-07 17:42:16 +0000106static bool updateOperand(FoldCandidate &Fold,
Tom Stellard6596ba72014-11-21 22:06:37 +0000107 const TargetRegisterInfo &TRI) {
Tom Stellardbb763e62015-01-07 17:42:16 +0000108 MachineInstr *MI = Fold.UseMI;
109 MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
Tom Stellard6596ba72014-11-21 22:06:37 +0000110 assert(Old.isReg());
111
Tom Stellardbb763e62015-01-07 17:42:16 +0000112 if (Fold.isImm()) {
113 Old.ChangeToImmediate(Fold.ImmToFold);
Tom Stellard6596ba72014-11-21 22:06:37 +0000114 return true;
115 }
116
Tom Stellardbb763e62015-01-07 17:42:16 +0000117 MachineOperand *New = Fold.OpToFold;
118 if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
119 TargetRegisterInfo::isVirtualRegister(New->getReg())) {
120 Old.substVirtReg(New->getReg(), New->getSubReg(), TRI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000121 return true;
122 }
123
Tom Stellard6596ba72014-11-21 22:06:37 +0000124 // FIXME: Handle physical registers.
125
126 return false;
127}
128
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000129static bool isUseMIInFoldList(const std::vector<FoldCandidate> &FoldList,
130 const MachineInstr *MI) {
131 for (auto Candidate : FoldList) {
132 if (Candidate.UseMI == MI)
133 return true;
134 }
135 return false;
136}
137
Tom Stellard05992972015-01-07 22:44:19 +0000138static bool tryAddToFoldList(std::vector<FoldCandidate> &FoldList,
139 MachineInstr *MI, unsigned OpNo,
140 MachineOperand *OpToFold,
141 const SIInstrInfo *TII) {
142 if (!TII->isOperandLegal(MI, OpNo, OpToFold)) {
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000143
144 // Special case for v_mac_f32_e64 if we are trying to fold into src2
145 unsigned Opc = MI->getOpcode();
146 if (Opc == AMDGPU::V_MAC_F32_e64 &&
147 (int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
148 // Check if changing this to a v_mad_f32 instruction will allow us to
149 // fold the operand.
150 MI->setDesc(TII->get(AMDGPU::V_MAD_F32));
151 bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
152 if (FoldAsMAD) {
153 MI->untieRegOperand(OpNo);
154 return true;
155 }
156 MI->setDesc(TII->get(Opc));
157 }
158
159 // If we are already folding into another operand of MI, then
160 // we can't commute the instruction, otherwise we risk making the
161 // other fold illegal.
162 if (isUseMIInFoldList(FoldList, MI))
163 return false;
164
Tom Stellard05992972015-01-07 22:44:19 +0000165 // Operand is not legal, so try to commute the instruction to
166 // see if this makes it possible to fold.
167 unsigned CommuteIdx0;
168 unsigned CommuteIdx1;
169 bool CanCommute = TII->findCommutedOpIndices(MI, CommuteIdx0, CommuteIdx1);
170
171 if (CanCommute) {
172 if (CommuteIdx0 == OpNo)
173 OpNo = CommuteIdx1;
174 else if (CommuteIdx1 == OpNo)
175 OpNo = CommuteIdx0;
176 }
177
178 if (!CanCommute || !TII->commuteInstruction(MI))
179 return false;
180
181 if (!TII->isOperandLegal(MI, OpNo, OpToFold))
182 return false;
183 }
184
185 FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
186 return true;
187}
188
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000189static void foldOperand(MachineOperand &OpToFold, MachineInstr *UseMI,
190 unsigned UseOpIdx,
191 std::vector<FoldCandidate> &FoldList,
192 const SIInstrInfo *TII, const SIRegisterInfo &TRI,
193 MachineRegisterInfo &MRI) {
194 const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
195
196 // FIXME: Fold operands with subregs.
197 if (UseOp.isReg() && ((UseOp.getSubReg() && OpToFold.isReg()) ||
198 UseOp.isImplicit())) {
199 return;
200 }
201
202 bool FoldingImm = OpToFold.isImm();
203 APInt Imm;
204
205 if (FoldingImm) {
206 unsigned UseReg = UseOp.getReg();
207 const TargetRegisterClass *UseRC
208 = TargetRegisterInfo::isVirtualRegister(UseReg) ?
209 MRI.getRegClass(UseReg) :
210 TRI.getPhysRegClass(UseReg);
211
212 Imm = APInt(64, OpToFold.getImm());
213
Tom Stellardeea72cc2015-08-29 01:58:21 +0000214 const MCInstrDesc &FoldDesc = TII->get(OpToFold.getParent()->getOpcode());
215 const TargetRegisterClass *FoldRC =
216 TRI.getRegClass(FoldDesc.OpInfo[0].RegClass);
217
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000218 // Split 64-bit constants into 32-bits for folding.
Tom Stellardeea72cc2015-08-29 01:58:21 +0000219 if (FoldRC->getSize() == 8 && UseOp.getSubReg()) {
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000220 if (UseRC->getSize() != 8)
221 return;
222
223 if (UseOp.getSubReg() == AMDGPU::sub0) {
224 Imm = Imm.getLoBits(32);
225 } else {
226 assert(UseOp.getSubReg() == AMDGPU::sub1);
227 Imm = Imm.getHiBits(32);
228 }
229 }
230
231 // In order to fold immediates into copies, we need to change the
232 // copy to a MOV.
233 if (UseMI->getOpcode() == AMDGPU::COPY) {
234 unsigned DestReg = UseMI->getOperand(0).getReg();
235 const TargetRegisterClass *DestRC
236 = TargetRegisterInfo::isVirtualRegister(DestReg) ?
237 MRI.getRegClass(DestReg) :
238 TRI.getPhysRegClass(DestReg);
239
240 unsigned MovOp = TII->getMovOpcode(DestRC);
241 if (MovOp == AMDGPU::COPY)
242 return;
243
244 UseMI->setDesc(TII->get(MovOp));
245 }
246 }
247
Tom Stellard9a197672015-09-09 15:43:26 +0000248 // Special case for REG_SEQUENCE: We can't fold literals into
249 // REG_SEQUENCE instructions, so we have to fold them into the
250 // uses of REG_SEQUENCE.
251 if (UseMI->getOpcode() == AMDGPU::REG_SEQUENCE) {
252 unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
253 unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
254
255 for (MachineRegisterInfo::use_iterator
256 RSUse = MRI.use_begin(RegSeqDstReg),
257 RSE = MRI.use_end(); RSUse != RSE; ++RSUse) {
258
259 MachineInstr *RSUseMI = RSUse->getParent();
260 if (RSUse->getSubReg() != RegSeqDstSubReg)
261 continue;
262
263 foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
264 TII, TRI, MRI);
265 }
266 return;
267 }
268
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000269 const MCInstrDesc &UseDesc = UseMI->getDesc();
270
271 // Don't fold into target independent nodes. Target independent opcodes
272 // don't have defined register classes.
273 if (UseDesc.isVariadic() ||
274 UseDesc.OpInfo[UseOpIdx].RegClass == -1)
275 return;
276
277 if (FoldingImm) {
278 MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
279 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
280 return;
281 }
282
283 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
284
285 // FIXME: We could try to change the instruction from 64-bit to 32-bit
286 // to enable more folding opportunites. The shrink operands pass
287 // already does this.
288 return;
289}
290
Tom Stellard6596ba72014-11-21 22:06:37 +0000291bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
292 MachineRegisterInfo &MRI = MF.getRegInfo();
293 const SIInstrInfo *TII =
294 static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
295 const SIRegisterInfo &TRI = TII->getRegisterInfo();
296
297 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
298 BI != BE; ++BI) {
299
300 MachineBasicBlock &MBB = *BI;
301 MachineBasicBlock::iterator I, Next;
302 for (I = MBB.begin(); I != MBB.end(); I = Next) {
303 Next = std::next(I);
304 MachineInstr &MI = *I;
305
306 if (!isSafeToFold(MI.getOpcode()))
307 continue;
308
Matt Arsenault11a4d672015-02-13 19:05:03 +0000309 unsigned OpSize = TII->getOpSize(MI, 1);
Tom Stellard6596ba72014-11-21 22:06:37 +0000310 MachineOperand &OpToFold = MI.getOperand(1);
Tom Stellardfb77f002015-01-13 22:59:41 +0000311 bool FoldingImm = OpToFold.isImm();
Tom Stellard26cc18d2015-01-07 22:18:27 +0000312
Tom Stellard05992972015-01-07 22:44:19 +0000313 // FIXME: We could also be folding things like FrameIndexes and
314 // TargetIndexes.
315 if (!FoldingImm && !OpToFold.isReg())
316 continue;
317
Matt Arsenault25f61a62015-01-31 23:37:27 +0000318 // Folding immediates with more than one use will increase program size.
Tom Stellard26cc18d2015-01-07 22:18:27 +0000319 // FIXME: This will also reduce register usage, which may be better
320 // in some cases. A better heuristic is needed.
Matt Arsenault11a4d672015-02-13 19:05:03 +0000321 if (FoldingImm && !TII->isInlineConstant(OpToFold, OpSize) &&
Tom Stellard26cc18d2015-01-07 22:18:27 +0000322 !MRI.hasOneUse(MI.getOperand(0).getReg()))
323 continue;
Tom Stellard6596ba72014-11-21 22:06:37 +0000324
325 // FIXME: Fold operands with subregs.
326 if (OpToFold.isReg() &&
327 (!TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()) ||
328 OpToFold.getSubReg()))
329 continue;
330
Tom Stellardbb763e62015-01-07 17:42:16 +0000331 std::vector<FoldCandidate> FoldList;
Tom Stellard6596ba72014-11-21 22:06:37 +0000332 for (MachineRegisterInfo::use_iterator
333 Use = MRI.use_begin(MI.getOperand(0).getReg()), E = MRI.use_end();
334 Use != E; ++Use) {
335
336 MachineInstr *UseMI = Use->getParent();
Tom Stellard6596ba72014-11-21 22:06:37 +0000337
Tom Stellardb8ce14c2015-08-28 23:45:19 +0000338 foldOperand(OpToFold, UseMI, Use.getOperandNo(), FoldList,
339 TII, TRI, MRI);
Tom Stellard6596ba72014-11-21 22:06:37 +0000340 }
341
Tom Stellardbb763e62015-01-07 17:42:16 +0000342 for (FoldCandidate &Fold : FoldList) {
343 if (updateOperand(Fold, TRI)) {
Tom Stellard6596ba72014-11-21 22:06:37 +0000344 // Clear kill flags.
Tom Stellardbb763e62015-01-07 17:42:16 +0000345 if (!Fold.isImm()) {
346 assert(Fold.OpToFold && Fold.OpToFold->isReg());
347 Fold.OpToFold->setIsKill(false);
348 }
Tom Stellard6596ba72014-11-21 22:06:37 +0000349 DEBUG(dbgs() << "Folded source from " << MI << " into OpNo " <<
Tom Stellardbb763e62015-01-07 17:42:16 +0000350 Fold.UseOpNo << " of " << *Fold.UseMI << '\n');
Tom Stellard6596ba72014-11-21 22:06:37 +0000351 }
352 }
353 }
354 }
355 return false;
356}