blob: 914d2a5ef1485e52c26c31b703bf6997c7c6bab6 [file] [log] [blame]
Eugene Zelenko59e12822017-08-08 00:47:13 +00001//===- SIFixSGPRCopies.cpp - Remove potential VGPR => SGPR copies ---------===//
Tom Stellard2f7cdda2013-08-06 23:08:28 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellard2f7cdda2013-08-06 23:08:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Copies from VGPR to SGPR registers are illegal and the register coalescer
11/// will sometimes generate these illegal copies in situations like this:
12///
13/// Register Class <vsrc> is the union of <vgpr> and <sgpr>
14///
15/// BB0:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000016/// %0 <sgpr> = SCALAR_INST
17/// %1 <vsrc> = COPY %0 <sgpr>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000018/// ...
19/// BRANCH %cond BB1, BB2
20/// BB1:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000021/// %2 <vgpr> = VECTOR_INST
22/// %3 <vsrc> = COPY %2 <vgpr>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000023/// BB2:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000024/// %4 <vsrc> = PHI %1 <vsrc>, <%bb.0>, %3 <vrsc>, <%bb.1>
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000025/// %5 <vgpr> = VECTOR_INST %4 <vsrc>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000026///
NAKAMURA Takumi78e80cd2013-11-14 04:05:22 +000027///
Tom Stellard2f7cdda2013-08-06 23:08:28 +000028/// The coalescer will begin at BB0 and eliminate its copy, then the resulting
29/// code will look like this:
30///
31/// BB0:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000032/// %0 <sgpr> = SCALAR_INST
Tom Stellard2f7cdda2013-08-06 23:08:28 +000033/// ...
34/// BRANCH %cond BB1, BB2
35/// BB1:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000036/// %2 <vgpr> = VECTOR_INST
37/// %3 <vsrc> = COPY %2 <vgpr>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000038/// BB2:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000039/// %4 <sgpr> = PHI %0 <sgpr>, <%bb.0>, %3 <vsrc>, <%bb.1>
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000040/// %5 <vgpr> = VECTOR_INST %4 <sgpr>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000041///
42/// Now that the result of the PHI instruction is an SGPR, the register
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000043/// allocator is now forced to constrain the register class of %3 to
Tom Stellard2f7cdda2013-08-06 23:08:28 +000044/// <sgpr> so we end up with final code like this:
NAKAMURA Takumi78e80cd2013-11-14 04:05:22 +000045///
Tom Stellard2f7cdda2013-08-06 23:08:28 +000046/// BB0:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000047/// %0 <sgpr> = SCALAR_INST
Tom Stellard2f7cdda2013-08-06 23:08:28 +000048/// ...
49/// BRANCH %cond BB1, BB2
50/// BB1:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000051/// %2 <vgpr> = VECTOR_INST
52/// %3 <sgpr> = COPY %2 <vgpr>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000053/// BB2:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000054/// %4 <sgpr> = PHI %0 <sgpr>, <%bb.0>, %3 <sgpr>, <%bb.1>
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000055/// %5 <vgpr> = VECTOR_INST %4 <sgpr>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000056///
NAKAMURA Takumi78e80cd2013-11-14 04:05:22 +000057/// Now this code contains an illegal copy from a VGPR to an SGPR.
Tom Stellard2f7cdda2013-08-06 23:08:28 +000058///
59/// In order to avoid this problem, this pass searches for PHI instructions
60/// which define a <vsrc> register and constrains its definition class to
61/// <vgpr> if the user of the PHI's definition register is a vector instruction.
62/// If the PHI's definition class is constrained to <vgpr> then the coalescer
63/// will be unable to perform the COPY removal from the above example which
64/// ultimately led to the creation of an illegal COPY.
65//===----------------------------------------------------------------------===//
66
67#include "AMDGPU.h"
Eric Christopherd9134482014-08-04 21:25:23 +000068#include "AMDGPUSubtarget.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080069#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard2f7cdda2013-08-06 23:08:28 +000070#include "SIInstrInfo.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000071#include "SIRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000072#include "llvm/ADT/DenseSet.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000073#include "llvm/ADT/STLExtras.h"
74#include "llvm/ADT/SmallSet.h"
75#include "llvm/ADT/SmallVector.h"
76#include "llvm/CodeGen/MachineBasicBlock.h"
Tom Stellard0bc68812016-11-29 00:46:46 +000077#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000078#include "llvm/CodeGen/MachineFunction.h"
Tom Stellard2f7cdda2013-08-06 23:08:28 +000079#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000080#include "llvm/CodeGen/MachineInstr.h"
Tom Stellard82166022013-11-13 23:36:37 +000081#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000082#include "llvm/CodeGen/MachineOperand.h"
Tom Stellard2f7cdda2013-08-06 23:08:28 +000083#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000084#include "llvm/CodeGen/TargetRegisterInfo.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080085#include "llvm/InitializePasses.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000086#include "llvm/Pass.h"
87#include "llvm/Support/CodeGen.h"
88#include "llvm/Support/CommandLine.h"
Tom Stellard82166022013-11-13 23:36:37 +000089#include "llvm/Support/Debug.h"
Hans Wennborga74fd702013-11-14 23:24:09 +000090#include "llvm/Support/raw_ostream.h"
Tom Stellard2f7cdda2013-08-06 23:08:28 +000091#include "llvm/Target/TargetMachine.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000092#include <cassert>
93#include <cstdint>
94#include <iterator>
95#include <list>
96#include <map>
97#include <tuple>
98#include <utility>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000099
100using namespace llvm;
101
Matt Arsenault98f83942016-04-21 18:21:54 +0000102#define DEBUG_TYPE "si-fix-sgpr-copies"
Chandler Carruth84e68b22014-04-22 02:41:26 +0000103
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000104static cl::opt<bool> EnableM0Merge(
105 "amdgpu-enable-merge-m0",
106 cl::desc("Merge and hoist M0 initializations"),
Austin Kerbow423b4a12019-07-15 22:07:05 +0000107 cl::init(true));
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000108
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000109namespace {
110
111class SIFixSGPRCopies : public MachineFunctionPass {
Tom Stellard0bc68812016-11-29 00:46:46 +0000112 MachineDominatorTree *MDT;
Alexander Timofeevb9347282018-04-25 12:32:46 +0000113
Matt Arsenault782c03b2015-11-03 22:30:13 +0000114public:
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000115 static char ID;
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000116
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000117 MachineRegisterInfo *MRI;
118 const SIRegisterInfo *TRI;
119 const SIInstrInfo *TII;
120
Eugene Zelenko59e12822017-08-08 00:47:13 +0000121 SIFixSGPRCopies() : MachineFunctionPass(ID) {}
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000122
Craig Topper5656db42014-04-29 07:57:24 +0000123 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000124
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000125 void processPHINode(MachineInstr &MI);
126
Mehdi Amini117296c2016-10-01 02:56:57 +0000127 StringRef getPassName() const override { return "SI Fix SGPR copies"; }
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000128
Matt Arsenault0cb85172015-09-25 17:21:28 +0000129 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0bc68812016-11-29 00:46:46 +0000130 AU.addRequired<MachineDominatorTree>();
131 AU.addPreserved<MachineDominatorTree>();
Matt Arsenault0cb85172015-09-25 17:21:28 +0000132 AU.setPreservesCFG();
133 MachineFunctionPass::getAnalysisUsage(AU);
134 }
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000135};
136
Eugene Zelenko59e12822017-08-08 00:47:13 +0000137} // end anonymous namespace
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000138
Tom Stellard0bc68812016-11-29 00:46:46 +0000139INITIALIZE_PASS_BEGIN(SIFixSGPRCopies, DEBUG_TYPE,
140 "SI Fix SGPR copies", false, false)
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000141INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Tom Stellard0bc68812016-11-29 00:46:46 +0000142INITIALIZE_PASS_END(SIFixSGPRCopies, DEBUG_TYPE,
143 "SI Fix SGPR copies", false, false)
144
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000145char SIFixSGPRCopies::ID = 0;
146
Matt Arsenault782c03b2015-11-03 22:30:13 +0000147char &llvm::SIFixSGPRCopiesID = SIFixSGPRCopies::ID;
148
149FunctionPass *llvm::createSIFixSGPRCopiesPass() {
150 return new SIFixSGPRCopies();
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000151}
152
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000153static bool hasVectorOperands(const MachineInstr &MI,
154 const SIRegisterInfo *TRI) {
Tom Stellard82166022013-11-13 23:36:37 +0000155 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
156 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
157 if (!MI.getOperand(i).isReg() ||
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000158 !Register::isVirtualRegister(MI.getOperand(i).getReg()))
Tom Stellard82166022013-11-13 23:36:37 +0000159 continue;
160
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000161 if (TRI->hasVectorRegisters(MRI.getRegClass(MI.getOperand(i).getReg())))
Tom Stellard82166022013-11-13 23:36:37 +0000162 return true;
163 }
164 return false;
165}
166
Matt Arsenault0de924b2015-11-02 23:15:42 +0000167static std::pair<const TargetRegisterClass *, const TargetRegisterClass *>
168getCopyRegClasses(const MachineInstr &Copy,
169 const SIRegisterInfo &TRI,
170 const MachineRegisterInfo &MRI) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000171 Register DstReg = Copy.getOperand(0).getReg();
172 Register SrcReg = Copy.getOperand(1).getReg();
Matt Arsenault120a0c92014-12-03 05:22:39 +0000173
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000174 const TargetRegisterClass *SrcRC = Register::isVirtualRegister(SrcReg)
175 ? MRI.getRegClass(SrcReg)
176 : TRI.getPhysRegClass(SrcReg);
Tom Stellardd33d7f12015-05-12 14:18:11 +0000177
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000178 // We don't really care about the subregister here.
179 // SrcRC = TRI.getSubRegClass(SrcRC, Copy.getOperand(1).getSubReg());
Tom Stellard82166022013-11-13 23:36:37 +0000180
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000181 const TargetRegisterClass *DstRC = Register::isVirtualRegister(DstReg)
182 ? MRI.getRegClass(DstReg)
183 : TRI.getPhysRegClass(DstReg);
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000184
185 return std::make_pair(SrcRC, DstRC);
186}
187
Matt Arsenault0de924b2015-11-02 23:15:42 +0000188static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC,
189 const TargetRegisterClass *DstRC,
190 const SIRegisterInfo &TRI) {
Nicolai Haehnle814abb52018-10-31 13:27:08 +0000191 return SrcRC != &AMDGPU::VReg_1RegClass && TRI.isSGPRClass(DstRC) &&
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000192 TRI.hasVectorRegisters(SrcRC);
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000193}
194
Matt Arsenault0de924b2015-11-02 23:15:42 +0000195static bool isSGPRToVGPRCopy(const TargetRegisterClass *SrcRC,
196 const TargetRegisterClass *DstRC,
197 const SIRegisterInfo &TRI) {
Nicolai Haehnle814abb52018-10-31 13:27:08 +0000198 return DstRC != &AMDGPU::VReg_1RegClass && TRI.isSGPRClass(SrcRC) &&
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000199 TRI.hasVectorRegisters(DstRC);
Tom Stellard82166022013-11-13 23:36:37 +0000200}
201
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000202static bool tryChangeVGPRtoSGPRinCopy(MachineInstr &MI,
203 const SIRegisterInfo *TRI,
204 const SIInstrInfo *TII) {
205 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
206 auto &Src = MI.getOperand(1);
Daniel Sanders0c476112019-08-15 19:22:08 +0000207 Register DstReg = MI.getOperand(0).getReg();
208 Register SrcReg = Src.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000209 if (!Register::isVirtualRegister(SrcReg) ||
210 !Register::isVirtualRegister(DstReg))
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000211 return false;
212
213 for (const auto &MO : MRI.reg_nodbg_operands(DstReg)) {
214 const auto *UseMI = MO.getParent();
215 if (UseMI == &MI)
216 continue;
217 if (MO.isDef() || UseMI->getParent() != MI.getParent() ||
218 UseMI->getOpcode() <= TargetOpcode::GENERIC_OP_END ||
219 !TII->isOperandLegal(*UseMI, UseMI->getOperandNo(&MO), &Src))
220 return false;
221 }
222 // Change VGPR to SGPR destination.
223 MRI.setRegClass(DstReg, TRI->getEquivalentSGPRClass(MRI.getRegClass(DstReg)));
224 return true;
225}
226
Matt Arsenault0de924b2015-11-02 23:15:42 +0000227// Distribute an SGPR->VGPR copy of a REG_SEQUENCE into a VGPR REG_SEQUENCE.
228//
229// SGPRx = ...
230// SGPRy = REG_SEQUENCE SGPRx, sub0 ...
231// VGPRz = COPY SGPRy
232//
233// ==>
234//
235// VGPRx = COPY SGPRx
236// VGPRz = REG_SEQUENCE VGPRx, sub0
237//
238// This exposes immediate folding opportunities when materializing 64-bit
239// immediates.
240static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
241 const SIRegisterInfo *TRI,
242 const SIInstrInfo *TII,
243 MachineRegisterInfo &MRI) {
244 assert(MI.isRegSequence());
245
Daniel Sanders0c476112019-08-15 19:22:08 +0000246 Register DstReg = MI.getOperand(0).getReg();
Matt Arsenault0de924b2015-11-02 23:15:42 +0000247 if (!TRI->isSGPRClass(MRI.getRegClass(DstReg)))
248 return false;
249
250 if (!MRI.hasOneUse(DstReg))
251 return false;
252
253 MachineInstr &CopyUse = *MRI.use_instr_begin(DstReg);
254 if (!CopyUse.isCopy())
255 return false;
256
Matt Arsenaultfe78ffb2017-04-11 22:29:19 +0000257 // It is illegal to have vreg inputs to a physreg defining reg_sequence.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000258 if (Register::isPhysicalRegister(CopyUse.getOperand(0).getReg()))
Matt Arsenaultfe78ffb2017-04-11 22:29:19 +0000259 return false;
260
Matt Arsenault0de924b2015-11-02 23:15:42 +0000261 const TargetRegisterClass *SrcRC, *DstRC;
262 std::tie(SrcRC, DstRC) = getCopyRegClasses(CopyUse, *TRI, MRI);
263
264 if (!isSGPRToVGPRCopy(SrcRC, DstRC, *TRI))
265 return false;
266
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000267 if (tryChangeVGPRtoSGPRinCopy(CopyUse, TRI, TII))
268 return true;
269
Matt Arsenault0de924b2015-11-02 23:15:42 +0000270 // TODO: Could have multiple extracts?
271 unsigned SubReg = CopyUse.getOperand(1).getSubReg();
272 if (SubReg != AMDGPU::NoSubRegister)
273 return false;
274
275 MRI.setRegClass(DstReg, DstRC);
276
277 // SGPRx = ...
278 // SGPRy = REG_SEQUENCE SGPRx, sub0 ...
279 // VGPRz = COPY SGPRy
280
281 // =>
282 // VGPRx = COPY SGPRx
283 // VGPRz = REG_SEQUENCE VGPRx, sub0
284
285 MI.getOperand(0).setReg(CopyUse.getOperand(0).getReg());
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000286 bool IsAGPR = TRI->hasAGPRs(DstRC);
Matt Arsenault0de924b2015-11-02 23:15:42 +0000287
288 for (unsigned I = 1, N = MI.getNumOperands(); I != N; I += 2) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000289 Register SrcReg = MI.getOperand(I).getReg();
Nicolai Haehnle82fc9622016-01-07 17:10:29 +0000290 unsigned SrcSubReg = MI.getOperand(I).getSubReg();
Matt Arsenault0de924b2015-11-02 23:15:42 +0000291
292 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
293 assert(TRI->isSGPRClass(SrcRC) &&
294 "Expected SGPR REG_SEQUENCE to only have SGPR inputs");
295
296 SrcRC = TRI->getSubRegClass(SrcRC, SrcSubReg);
297 const TargetRegisterClass *NewSrcRC = TRI->getEquivalentVGPRClass(SrcRC);
298
Daniel Sanders0c476112019-08-15 19:22:08 +0000299 Register TmpReg = MRI.createVirtualRegister(NewSrcRC);
Matt Arsenault0de924b2015-11-02 23:15:42 +0000300
Diana Picus116bbab2017-01-13 09:58:52 +0000301 BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(AMDGPU::COPY),
302 TmpReg)
303 .add(MI.getOperand(I));
Matt Arsenault0de924b2015-11-02 23:15:42 +0000304
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000305 if (IsAGPR) {
306 const TargetRegisterClass *NewSrcRC = TRI->getEquivalentAGPRClass(SrcRC);
Daniel Sanders0c476112019-08-15 19:22:08 +0000307 Register TmpAReg = MRI.createVirtualRegister(NewSrcRC);
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000308 unsigned Opc = NewSrcRC == &AMDGPU::AGPR_32RegClass ?
309 AMDGPU::V_ACCVGPR_WRITE_B32 : AMDGPU::COPY;
310 BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(Opc),
311 TmpAReg)
312 .addReg(TmpReg, RegState::Kill);
313 TmpReg = TmpAReg;
314 }
315
Matt Arsenault0de924b2015-11-02 23:15:42 +0000316 MI.getOperand(I).setReg(TmpReg);
317 }
318
319 CopyUse.eraseFromParent();
320 return true;
321}
322
Tom Stellard00cfa742016-12-06 21:13:30 +0000323static bool isSafeToFoldImmIntoCopy(const MachineInstr *Copy,
324 const MachineInstr *MoveImm,
325 const SIInstrInfo *TII,
326 unsigned &SMovOp,
327 int64_t &Imm) {
Connor Abbott8c217d02017-08-04 18:36:49 +0000328 if (Copy->getOpcode() != AMDGPU::COPY)
329 return false;
330
Tom Stellard00cfa742016-12-06 21:13:30 +0000331 if (!MoveImm->isMoveImmediate())
332 return false;
333
334 const MachineOperand *ImmOp =
335 TII->getNamedOperand(*MoveImm, AMDGPU::OpName::src0);
336 if (!ImmOp->isImm())
337 return false;
338
339 // FIXME: Handle copies with sub-regs.
340 if (Copy->getOperand(0).getSubReg())
341 return false;
342
343 switch (MoveImm->getOpcode()) {
344 default:
345 return false;
346 case AMDGPU::V_MOV_B32_e32:
347 SMovOp = AMDGPU::S_MOV_B32;
348 break;
349 case AMDGPU::V_MOV_B64_PSEUDO:
350 SMovOp = AMDGPU::S_MOV_B64;
351 break;
352 }
353 Imm = ImmOp->getImm();
354 return true;
355}
356
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000357template <class UnaryPredicate>
358bool searchPredecessors(const MachineBasicBlock *MBB,
359 const MachineBasicBlock *CutOff,
360 UnaryPredicate Predicate) {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000361 if (MBB == CutOff)
362 return false;
363
Eugene Zelenko59e12822017-08-08 00:47:13 +0000364 DenseSet<const MachineBasicBlock *> Visited;
365 SmallVector<MachineBasicBlock *, 4> Worklist(MBB->pred_begin(),
366 MBB->pred_end());
Wei Ding74da3502017-04-12 23:51:47 +0000367
368 while (!Worklist.empty()) {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000369 MachineBasicBlock *MBB = Worklist.pop_back_val();
Wei Ding74da3502017-04-12 23:51:47 +0000370
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000371 if (!Visited.insert(MBB).second)
Wei Ding74da3502017-04-12 23:51:47 +0000372 continue;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000373 if (MBB == CutOff)
374 continue;
375 if (Predicate(MBB))
Wei Ding74da3502017-04-12 23:51:47 +0000376 return true;
377
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000378 Worklist.append(MBB->pred_begin(), MBB->pred_end());
Wei Ding74da3502017-04-12 23:51:47 +0000379 }
380
381 return false;
382}
383
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000384// Checks if there is potential path From instruction To instruction.
385// If CutOff is specified and it sits in between of that path we ignore
386// a higher portion of the path and report it is not reachable.
387static bool isReachable(const MachineInstr *From,
388 const MachineInstr *To,
389 const MachineBasicBlock *CutOff,
390 MachineDominatorTree &MDT) {
391 // If either From block dominates To block or instructions are in the same
392 // block and From is higher.
393 if (MDT.dominates(From, To))
394 return true;
395
396 const MachineBasicBlock *MBBFrom = From->getParent();
397 const MachineBasicBlock *MBBTo = To->getParent();
398 if (MBBFrom == MBBTo)
399 return false;
400
401 // Instructions are in different blocks, do predecessor search.
402 // We should almost never get here since we do not usually produce M0 stores
403 // other than -1.
404 return searchPredecessors(MBBTo, CutOff, [MBBFrom]
405 (const MachineBasicBlock *MBB) { return MBB == MBBFrom; });
406}
407
Austin Kerbow423b4a12019-07-15 22:07:05 +0000408// Return the first non-prologue instruction in the block.
409static MachineBasicBlock::iterator
410getFirstNonPrologue(MachineBasicBlock *MBB, const TargetInstrInfo *TII) {
411 MachineBasicBlock::iterator I = MBB->getFirstNonPHI();
412 while (I != MBB->end() && TII->isBasicBlockPrologue(*I))
413 ++I;
414
415 return I;
416}
417
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000418// Hoist and merge identical SGPR initializations into a common predecessor.
419// This is intended to combine M0 initializations, but can work with any
420// SGPR. A VGPR cannot be processed since we cannot guarantee vector
421// executioon.
422static bool hoistAndMergeSGPRInits(unsigned Reg,
423 const MachineRegisterInfo &MRI,
Austin Kerbow666af672019-09-11 21:28:41 +0000424 const TargetRegisterInfo *TRI,
Austin Kerbow423b4a12019-07-15 22:07:05 +0000425 MachineDominatorTree &MDT,
426 const TargetInstrInfo *TII) {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000427 // List of inits by immediate value.
Eugene Zelenko59e12822017-08-08 00:47:13 +0000428 using InitListMap = std::map<unsigned, std::list<MachineInstr *>>;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000429 InitListMap Inits;
430 // List of clobbering instructions.
431 SmallVector<MachineInstr*, 8> Clobbers;
Austin Kerbow423b4a12019-07-15 22:07:05 +0000432 // List of instructions marked for deletion.
433 SmallSet<MachineInstr*, 8> MergedInstrs;
434
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000435 bool Changed = false;
436
437 for (auto &MI : MRI.def_instructions(Reg)) {
438 MachineOperand *Imm = nullptr;
Austin Kerbow666af672019-09-11 21:28:41 +0000439 for (auto &MO : MI.operands()) {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000440 if ((MO.isReg() && ((MO.isDef() && MO.getReg() != Reg) || !MO.isDef())) ||
441 (!MO.isImm() && !MO.isReg()) || (MO.isImm() && Imm)) {
442 Imm = nullptr;
443 break;
444 } else if (MO.isImm())
445 Imm = &MO;
446 }
447 if (Imm)
448 Inits[Imm->getImm()].push_front(&MI);
449 else
450 Clobbers.push_back(&MI);
451 }
452
453 for (auto &Init : Inits) {
454 auto &Defs = Init.second;
455
456 for (auto I1 = Defs.begin(), E = Defs.end(); I1 != E; ) {
457 MachineInstr *MI1 = *I1;
458
459 for (auto I2 = std::next(I1); I2 != E; ) {
460 MachineInstr *MI2 = *I2;
461
462 // Check any possible interference
Austin Kerbow423b4a12019-07-15 22:07:05 +0000463 auto interferes = [&](MachineBasicBlock::iterator From,
464 MachineBasicBlock::iterator To) -> bool {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000465
466 assert(MDT.dominates(&*To, &*From));
467
468 auto interferes = [&MDT, From, To](MachineInstr* &Clobber) -> bool {
469 const MachineBasicBlock *MBBFrom = From->getParent();
470 const MachineBasicBlock *MBBTo = To->getParent();
471 bool MayClobberFrom = isReachable(Clobber, &*From, MBBTo, MDT);
472 bool MayClobberTo = isReachable(Clobber, &*To, MBBTo, MDT);
473 if (!MayClobberFrom && !MayClobberTo)
474 return false;
475 if ((MayClobberFrom && !MayClobberTo) ||
476 (!MayClobberFrom && MayClobberTo))
477 return true;
478 // Both can clobber, this is not an interference only if both are
479 // dominated by Clobber and belong to the same block or if Clobber
480 // properly dominates To, given that To >> From, so it dominates
481 // both and located in a common dominator.
482 return !((MBBFrom == MBBTo &&
483 MDT.dominates(Clobber, &*From) &&
484 MDT.dominates(Clobber, &*To)) ||
485 MDT.properlyDominates(Clobber->getParent(), MBBTo));
486 };
487
Eugene Zelenko59e12822017-08-08 00:47:13 +0000488 return (llvm::any_of(Clobbers, interferes)) ||
489 (llvm::any_of(Inits, [&](InitListMap::value_type &C) {
490 return C.first != Init.first &&
491 llvm::any_of(C.second, interferes);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000492 }));
493 };
494
495 if (MDT.dominates(MI1, MI2)) {
Austin Kerbow423b4a12019-07-15 22:07:05 +0000496 if (!interferes(MI2, MI1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000497 LLVM_DEBUG(dbgs()
498 << "Erasing from "
499 << printMBBReference(*MI2->getParent()) << " " << *MI2);
Austin Kerbow423b4a12019-07-15 22:07:05 +0000500 MergedInstrs.insert(MI2);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000501 Changed = true;
Austin Kerbow423b4a12019-07-15 22:07:05 +0000502 ++I2;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000503 continue;
504 }
505 } else if (MDT.dominates(MI2, MI1)) {
Austin Kerbow423b4a12019-07-15 22:07:05 +0000506 if (!interferes(MI1, MI2)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000507 LLVM_DEBUG(dbgs()
508 << "Erasing from "
509 << printMBBReference(*MI1->getParent()) << " " << *MI1);
Austin Kerbow423b4a12019-07-15 22:07:05 +0000510 MergedInstrs.insert(MI1);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000511 Changed = true;
Austin Kerbow423b4a12019-07-15 22:07:05 +0000512 ++I1;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000513 break;
514 }
515 } else {
516 auto *MBB = MDT.findNearestCommonDominator(MI1->getParent(),
517 MI2->getParent());
518 if (!MBB) {
519 ++I2;
520 continue;
521 }
522
Austin Kerbow423b4a12019-07-15 22:07:05 +0000523 MachineBasicBlock::iterator I = getFirstNonPrologue(MBB, TII);
524 if (!interferes(MI1, I) && !interferes(MI2, I)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000525 LLVM_DEBUG(dbgs()
526 << "Erasing from "
527 << printMBBReference(*MI1->getParent()) << " " << *MI1
528 << "and moving from "
529 << printMBBReference(*MI2->getParent()) << " to "
530 << printMBBReference(*I->getParent()) << " " << *MI2);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000531 I->getParent()->splice(I, MI2->getParent(), MI2);
Austin Kerbow423b4a12019-07-15 22:07:05 +0000532 MergedInstrs.insert(MI1);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000533 Changed = true;
Austin Kerbow423b4a12019-07-15 22:07:05 +0000534 ++I1;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000535 break;
536 }
537 }
538 ++I2;
539 }
540 ++I1;
541 }
542 }
543
Austin Kerbow666af672019-09-11 21:28:41 +0000544 // Remove initializations that were merged into another.
545 for (auto &Init : Inits) {
546 auto &Defs = Init.second;
Austin Kerbowcf321f42019-09-12 19:12:21 +0000547 auto I = Defs.begin();
548 while (I != Defs.end()) {
Austin Kerbow666af672019-09-11 21:28:41 +0000549 if (MergedInstrs.count(*I)) {
550 (*I)->eraseFromParent();
551 I = Defs.erase(I);
Austin Kerbowcf321f42019-09-12 19:12:21 +0000552 } else
553 ++I;
554 }
Austin Kerbow666af672019-09-11 21:28:41 +0000555 }
556
557 // Try to schedule SGPR initializations as early as possible in the MBB.
558 for (auto &Init : Inits) {
559 auto &Defs = Init.second;
560 for (auto MI : Defs) {
561 auto MBB = MI->getParent();
562 MachineInstr &BoundaryMI = *getFirstNonPrologue(MBB, TII);
563 MachineBasicBlock::reverse_iterator B(BoundaryMI);
Matt Arsenaulte114be62019-10-09 22:44:47 +0000564 // Check if B should actually be a boundary. If not set the previous
Austin Kerbow666af672019-09-11 21:28:41 +0000565 // instruction as the boundary instead.
566 if (!TII->isBasicBlockPrologue(*B))
567 B++;
568
569 auto R = std::next(MI->getReverseIterator());
570 const unsigned Threshold = 50;
Matt Arsenaulte114be62019-10-09 22:44:47 +0000571 // Search until B or Threshold for a place to insert the initialization.
Austin Kerbow666af672019-09-11 21:28:41 +0000572 for (unsigned I = 0; R != B && I < Threshold; ++R, ++I)
573 if (R->readsRegister(Reg, TRI) || R->definesRegister(Reg, TRI) ||
574 TII->isSchedulingBoundary(*R, MBB, *MBB->getParent()))
575 break;
576
577 // Move to directly after R.
578 if (&*--R != MI)
579 MBB->splice(*R, MBB, MI);
580 }
581 }
Austin Kerbow423b4a12019-07-15 22:07:05 +0000582
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000583 if (Changed)
584 MRI.clearKillFlags(Reg);
585
586 return Changed;
587}
588
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000589bool SIFixSGPRCopies::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000590 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000591 MRI = &MF.getRegInfo();
592 TRI = ST.getRegisterInfo();
593 TII = ST.getInstrInfo();
Tom Stellard0bc68812016-11-29 00:46:46 +0000594 MDT = &getAnalysis<MachineDominatorTree>();
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000595
596 SmallVector<MachineInstr *, 16> Worklist;
597
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000598 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
599 BI != BE; ++BI) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000600 MachineBasicBlock &MBB = *BI;
601 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000602 I != E; ++I) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000603 MachineInstr &MI = *I;
Tom Stellard82166022013-11-13 23:36:37 +0000604
605 switch (MI.getOpcode()) {
Matt Arsenault85441dd2015-09-21 16:27:22 +0000606 default:
607 continue;
Connor Abbott8c217d02017-08-04 18:36:49 +0000608 case AMDGPU::COPY:
Connor Abbott92638ab2017-08-04 18:36:52 +0000609 case AMDGPU::WQM:
Carl Ritson00e89b42019-07-26 09:54:12 +0000610 case AMDGPU::SOFT_WQM:
Connor Abbott92638ab2017-08-04 18:36:52 +0000611 case AMDGPU::WWM: {
Matt Arsenaultaff29952019-08-01 18:27:11 +0000612 Register DstReg = MI.getOperand(0).getReg();
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000613
614 const TargetRegisterClass *SrcRC, *DstRC;
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000615 std::tie(SrcRC, DstRC) = getCopyRegClasses(MI, *TRI, *MRI);
Matt Arsenaultaff29952019-08-01 18:27:11 +0000616
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000617 if (!Register::isVirtualRegister(DstReg)) {
Matt Arsenaultaff29952019-08-01 18:27:11 +0000618 // If the destination register is a physical register there isn't
619 // really much we can do to fix this.
620 // Some special instructions use M0 as an input. Some even only use
621 // the first lane. Insert a readfirstlane and hope for the best.
622 if (DstReg == AMDGPU::M0 && TRI->hasVectorRegisters(SrcRC)) {
623 Register TmpReg
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000624 = MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
Matt Arsenaultaff29952019-08-01 18:27:11 +0000625
626 BuildMI(MBB, MI, MI.getDebugLoc(),
627 TII->get(AMDGPU::V_READFIRSTLANE_B32), TmpReg)
628 .add(MI.getOperand(1));
629 MI.getOperand(1).setReg(TmpReg);
630 }
631
632 continue;
633 }
634
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000635 if (isVGPRToSGPRCopy(SrcRC, DstRC, *TRI)) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000636 Register SrcReg = MI.getOperand(1).getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000637 if (!Register::isVirtualRegister(SrcReg)) {
Scott Linder823549a2018-10-08 18:47:01 +0000638 TII->moveToVALU(MI, MDT);
Matt Arsenault2a803692017-04-29 01:26:34 +0000639 break;
640 }
641
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000642 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
Tom Stellard00cfa742016-12-06 21:13:30 +0000643 unsigned SMovOp;
644 int64_t Imm;
645 // If we are just copying an immediate, we can replace the copy with
646 // s_mov_b32.
647 if (isSafeToFoldImmIntoCopy(&MI, DefMI, TII, SMovOp, Imm)) {
648 MI.getOperand(1).ChangeToImmediate(Imm);
649 MI.addImplicitDefUseOperands(MF);
650 MI.setDesc(TII->get(SMovOp));
651 break;
652 }
Scott Linder823549a2018-10-08 18:47:01 +0000653 TII->moveToVALU(MI, MDT);
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000654 } else if (isSGPRToVGPRCopy(SrcRC, DstRC, *TRI)) {
655 tryChangeVGPRtoSGPRinCopy(MI, TRI, TII);
Matt Arsenault85441dd2015-09-21 16:27:22 +0000656 }
657
658 break;
659 }
Tom Stellard82166022013-11-13 23:36:37 +0000660 case AMDGPU::PHI: {
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000661 processPHINode(MI);
Tom Stellard82166022013-11-13 23:36:37 +0000662 break;
663 }
Eugene Zelenko59e12822017-08-08 00:47:13 +0000664 case AMDGPU::REG_SEQUENCE:
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000665 if (TRI->hasVectorRegisters(TII->getOpRegClass(MI, 0)) ||
666 !hasVectorOperands(MI, TRI)) {
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000667 foldVGPRCopyIntoRegSequence(MI, TRI, TII, *MRI);
Tom Stellard82166022013-11-13 23:36:37 +0000668 continue;
Matt Arsenault0de924b2015-11-02 23:15:42 +0000669 }
Tom Stellard82166022013-11-13 23:36:37 +0000670
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000671 LLVM_DEBUG(dbgs() << "Fixing REG_SEQUENCE: " << MI);
Tom Stellard82166022013-11-13 23:36:37 +0000672
Scott Linder823549a2018-10-08 18:47:01 +0000673 TII->moveToVALU(MI, MDT);
Tom Stellard82166022013-11-13 23:36:37 +0000674 break;
Tom Stellard204e61b2014-04-07 19:45:45 +0000675 case AMDGPU::INSERT_SUBREG: {
Tom Stellarda5687382014-05-15 14:41:55 +0000676 const TargetRegisterClass *DstRC, *Src0RC, *Src1RC;
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000677 DstRC = MRI->getRegClass(MI.getOperand(0).getReg());
678 Src0RC = MRI->getRegClass(MI.getOperand(1).getReg());
679 Src1RC = MRI->getRegClass(MI.getOperand(2).getReg());
Tom Stellarda5687382014-05-15 14:41:55 +0000680 if (TRI->isSGPRClass(DstRC) &&
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000681 (TRI->hasVectorRegisters(Src0RC) ||
682 TRI->hasVectorRegisters(Src1RC))) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000683 LLVM_DEBUG(dbgs() << " Fixing INSERT_SUBREG: " << MI);
Scott Linder823549a2018-10-08 18:47:01 +0000684 TII->moveToVALU(MI, MDT);
Tom Stellarda5687382014-05-15 14:41:55 +0000685 }
686 break;
Tom Stellard204e61b2014-04-07 19:45:45 +0000687 }
David Stuttard2d6a2302019-10-16 14:37:39 +0000688 case AMDGPU::V_WRITELANE_B32: {
689 // Some architectures allow more than one constant bus access without
690 // SGPR restriction
691 if (ST.getConstantBusLimit(MI.getOpcode()) != 1)
692 break;
693
694 // Writelane is special in that it can use SGPR and M0 (which would
695 // normally count as using the constant bus twice - but in this case it
696 // is allowed since the lane selector doesn't count as a use of the
697 // constant bus). However, it is still required to abide by the 1 SGPR
698 // rule. Apply a fix here as we might have multiple SGPRs after
699 // legalizing VGPRs to SGPRs
700 int Src0Idx =
701 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
702 int Src1Idx =
703 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src1);
704 MachineOperand &Src0 = MI.getOperand(Src0Idx);
705 MachineOperand &Src1 = MI.getOperand(Src1Idx);
706
707 // Check to see if the instruction violates the 1 SGPR rule
708 if ((Src0.isReg() && TRI->isSGPRReg(*MRI, Src0.getReg()) &&
709 Src0.getReg() != AMDGPU::M0) &&
710 (Src1.isReg() && TRI->isSGPRReg(*MRI, Src1.getReg()) &&
711 Src1.getReg() != AMDGPU::M0)) {
712
713 // Check for trivially easy constant prop into one of the operands
714 // If this is the case then perform the operation now to resolve SGPR
715 // issue. If we don't do that here we will always insert a mov to m0
716 // that can't be resolved in later operand folding pass
717 bool Resolved = false;
718 for (MachineOperand *MO : {&Src0, &Src1}) {
719 if (Register::isVirtualRegister(MO->getReg())) {
720 MachineInstr *DefMI = MRI->getVRegDef(MO->getReg());
721 if (DefMI && TII->isFoldableCopy(*DefMI)) {
722 const MachineOperand &Def = DefMI->getOperand(0);
723 if (Def.isReg() &&
724 MO->getReg() == Def.getReg() &&
725 MO->getSubReg() == Def.getSubReg()) {
726 const MachineOperand &Copied = DefMI->getOperand(1);
727 if (Copied.isImm() &&
728 TII->isInlineConstant(APInt(64, Copied.getImm(), true))) {
729 MO->ChangeToImmediate(Copied.getImm());
730 Resolved = true;
731 break;
732 }
733 }
734 }
735 }
736 }
737
738 if (!Resolved) {
739 // Haven't managed to resolve by replacing an SGPR with an immediate
740 // Move src1 to be in M0
741 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
742 TII->get(AMDGPU::COPY), AMDGPU::M0)
743 .add(Src1);
744 Src1.ChangeToRegister(AMDGPU::M0, false);
745 }
746 }
747 break;
748 }
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000749 }
750 }
751 }
Matt Arsenault6f679782014-11-17 21:11:34 +0000752
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000753 if (MF.getTarget().getOptLevel() > CodeGenOpt::None && EnableM0Merge)
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000754 hoistAndMergeSGPRInits(AMDGPU::M0, *MRI, TRI, *MDT, TII);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000755
Matt Arsenault6f679782014-11-17 21:11:34 +0000756 return true;
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000757}
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000758
759void SIFixSGPRCopies::processPHINode(MachineInstr &MI) {
760 unsigned numVGPRUses = 0;
Stanislav Mekhanoshin0fab2202019-10-18 22:48:45 +0000761 bool AllAGPRUses = true;
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000762 SetVector<const MachineInstr *> worklist;
Austin Kerbow527e9f92019-10-15 19:59:45 +0000763 SmallSet<const MachineInstr *, 4> Visited;
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000764 worklist.insert(&MI);
Austin Kerbow527e9f92019-10-15 19:59:45 +0000765 Visited.insert(&MI);
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000766 while (!worklist.empty()) {
767 const MachineInstr *Instr = worklist.pop_back_val();
768 unsigned Reg = Instr->getOperand(0).getReg();
769 for (const auto &Use : MRI->use_operands(Reg)) {
770 const MachineInstr *UseMI = Use.getParent();
Stanislav Mekhanoshin0fab2202019-10-18 22:48:45 +0000771 AllAGPRUses &= (UseMI->isCopy() &&
772 TRI->isAGPR(*MRI, UseMI->getOperand(0).getReg())) ||
773 TRI->isAGPR(*MRI, Use.getReg());
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000774 if (UseMI->isCopy() || UseMI->isRegSequence()) {
775 if (UseMI->isCopy() &&
776 UseMI->getOperand(0).getReg().isPhysical() &&
777 !TRI->isSGPRReg(*MRI, UseMI->getOperand(0).getReg())) {
778 numVGPRUses++;
779 }
Austin Kerbow527e9f92019-10-15 19:59:45 +0000780 if (Visited.insert(UseMI).second)
781 worklist.insert(UseMI);
782
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000783 continue;
784 }
785
786 if (UseMI->isPHI()) {
787 const TargetRegisterClass *UseRC = MRI->getRegClass(Use.getReg());
788 if (!TRI->isSGPRReg(*MRI, Use.getReg()) &&
789 UseRC != &AMDGPU::VReg_1RegClass)
790 numVGPRUses++;
791 continue;
792 }
793
794 const TargetRegisterClass *OpRC =
795 TII->getOpRegClass(*UseMI, UseMI->getOperandNo(&Use));
796 if (!TRI->isSGPRClass(OpRC) && OpRC != &AMDGPU::VS_32RegClass &&
797 OpRC != &AMDGPU::VS_64RegClass) {
798 numVGPRUses++;
799 }
800 }
801 }
Stanislav Mekhanoshin0fab2202019-10-18 22:48:45 +0000802
803 Register PHIRes = MI.getOperand(0).getReg();
804 const TargetRegisterClass *RC0 = MRI->getRegClass(PHIRes);
805 if (AllAGPRUses && numVGPRUses && !TRI->hasAGPRs(RC0)) {
806 LLVM_DEBUG(dbgs() << "Moving PHI to AGPR: " << MI);
807 MRI->setRegClass(PHIRes, TRI->getEquivalentAGPRClass(RC0));
808 }
809
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000810 bool hasVGPRInput = false;
811 for (unsigned i = 1; i < MI.getNumOperands(); i += 2) {
812 unsigned InputReg = MI.getOperand(i).getReg();
813 MachineInstr *Def = MRI->getVRegDef(InputReg);
Stanislav Mekhanoshin0fab2202019-10-18 22:48:45 +0000814 if (TRI->isVectorRegister(*MRI, InputReg)) {
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000815 if (Def->isCopy()) {
816 unsigned SrcReg = Def->getOperand(1).getReg();
817 const TargetRegisterClass *RC =
818 TRI->getRegClassForReg(*MRI, SrcReg);
819 if (TRI->isSGPRClass(RC))
820 continue;
821 }
822 hasVGPRInput = true;
823 break;
824 }
825 else if (Def->isCopy() &&
Stanislav Mekhanoshin0fab2202019-10-18 22:48:45 +0000826 TRI->isVectorRegister(*MRI, Def->getOperand(1).getReg())) {
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000827 hasVGPRInput = true;
828 break;
829 }
830 }
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000831
Stanislav Mekhanoshin0fab2202019-10-18 22:48:45 +0000832 if ((!TRI->isVectorRegister(*MRI, PHIRes) &&
833 RC0 != &AMDGPU::VReg_1RegClass) &&
Alexander Timofeevc4d256a2019-10-14 12:01:10 +0000834 (hasVGPRInput || numVGPRUses > 1)) {
835 LLVM_DEBUG(dbgs() << "Fixing PHI: " << MI);
836 TII->moveToVALU(MI);
837 }
838 else {
839 LLVM_DEBUG(dbgs() << "Legalizing PHI: " << MI);
840 TII->legalizeOperands(MI, MDT);
841 }
842
843}