blob: 94b1e636c7b1534bfb8a55804c0183665bc12f4a [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"
Tom Stellard2f7cdda2013-08-06 23:08:28 +000069#include "SIInstrInfo.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000070#include "SIRegisterInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000071#include "MCTargetDesc/AMDGPUMCTargetDesc.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"
Eugene Zelenko59e12822017-08-08 00:47:13 +000085#include "llvm/Pass.h"
86#include "llvm/Support/CodeGen.h"
87#include "llvm/Support/CommandLine.h"
Tom Stellard82166022013-11-13 23:36:37 +000088#include "llvm/Support/Debug.h"
Hans Wennborga74fd702013-11-14 23:24:09 +000089#include "llvm/Support/raw_ostream.h"
Tom Stellard2f7cdda2013-08-06 23:08:28 +000090#include "llvm/Target/TargetMachine.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000091#include <cassert>
92#include <cstdint>
93#include <iterator>
94#include <list>
95#include <map>
96#include <tuple>
97#include <utility>
Tom Stellard2f7cdda2013-08-06 23:08:28 +000098
99using namespace llvm;
100
Matt Arsenault98f83942016-04-21 18:21:54 +0000101#define DEBUG_TYPE "si-fix-sgpr-copies"
Chandler Carruth84e68b22014-04-22 02:41:26 +0000102
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000103static cl::opt<bool> EnableM0Merge(
104 "amdgpu-enable-merge-m0",
105 cl::desc("Merge and hoist M0 initializations"),
106 cl::init(false));
107
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000108namespace {
109
110class SIFixSGPRCopies : public MachineFunctionPass {
Tom Stellard0bc68812016-11-29 00:46:46 +0000111 MachineDominatorTree *MDT;
Alexander Timofeevb9347282018-04-25 12:32:46 +0000112
Matt Arsenault782c03b2015-11-03 22:30:13 +0000113public:
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000114 static char ID;
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000115
Eugene Zelenko59e12822017-08-08 00:47:13 +0000116 SIFixSGPRCopies() : MachineFunctionPass(ID) {}
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000117
Craig Topper5656db42014-04-29 07:57:24 +0000118 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000119
Mehdi Amini117296c2016-10-01 02:56:57 +0000120 StringRef getPassName() const override { return "SI Fix SGPR copies"; }
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000121
Matt Arsenault0cb85172015-09-25 17:21:28 +0000122 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard0bc68812016-11-29 00:46:46 +0000123 AU.addRequired<MachineDominatorTree>();
124 AU.addPreserved<MachineDominatorTree>();
Matt Arsenault0cb85172015-09-25 17:21:28 +0000125 AU.setPreservesCFG();
126 MachineFunctionPass::getAnalysisUsage(AU);
127 }
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000128};
129
Eugene Zelenko59e12822017-08-08 00:47:13 +0000130} // end anonymous namespace
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000131
Tom Stellard0bc68812016-11-29 00:46:46 +0000132INITIALIZE_PASS_BEGIN(SIFixSGPRCopies, DEBUG_TYPE,
133 "SI Fix SGPR copies", false, false)
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000134INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Tom Stellard0bc68812016-11-29 00:46:46 +0000135INITIALIZE_PASS_END(SIFixSGPRCopies, DEBUG_TYPE,
136 "SI Fix SGPR copies", false, false)
137
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000138char SIFixSGPRCopies::ID = 0;
139
Matt Arsenault782c03b2015-11-03 22:30:13 +0000140char &llvm::SIFixSGPRCopiesID = SIFixSGPRCopies::ID;
141
142FunctionPass *llvm::createSIFixSGPRCopiesPass() {
143 return new SIFixSGPRCopies();
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000144}
145
Tom Stellard82166022013-11-13 23:36:37 +0000146static bool hasVGPROperands(const MachineInstr &MI, const SIRegisterInfo *TRI) {
147 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
148 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
149 if (!MI.getOperand(i).isReg() ||
150 !TargetRegisterInfo::isVirtualRegister(MI.getOperand(i).getReg()))
151 continue;
152
153 if (TRI->hasVGPRs(MRI.getRegClass(MI.getOperand(i).getReg())))
154 return true;
155 }
156 return false;
157}
158
Matt Arsenault0de924b2015-11-02 23:15:42 +0000159static std::pair<const TargetRegisterClass *, const TargetRegisterClass *>
160getCopyRegClasses(const MachineInstr &Copy,
161 const SIRegisterInfo &TRI,
162 const MachineRegisterInfo &MRI) {
Tom Stellard82166022013-11-13 23:36:37 +0000163 unsigned DstReg = Copy.getOperand(0).getReg();
164 unsigned SrcReg = Copy.getOperand(1).getReg();
Matt Arsenault120a0c92014-12-03 05:22:39 +0000165
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000166 const TargetRegisterClass *SrcRC =
167 TargetRegisterInfo::isVirtualRegister(SrcReg) ?
168 MRI.getRegClass(SrcReg) :
169 TRI.getPhysRegClass(SrcReg);
Tom Stellardd33d7f12015-05-12 14:18:11 +0000170
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000171 // We don't really care about the subregister here.
172 // SrcRC = TRI.getSubRegClass(SrcRC, Copy.getOperand(1).getSubReg());
Tom Stellard82166022013-11-13 23:36:37 +0000173
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000174 const TargetRegisterClass *DstRC =
175 TargetRegisterInfo::isVirtualRegister(DstReg) ?
176 MRI.getRegClass(DstReg) :
177 TRI.getPhysRegClass(DstReg);
178
179 return std::make_pair(SrcRC, DstRC);
180}
181
Matt Arsenault0de924b2015-11-02 23:15:42 +0000182static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC,
183 const TargetRegisterClass *DstRC,
184 const SIRegisterInfo &TRI) {
Nicolai Haehnle814abb52018-10-31 13:27:08 +0000185 return SrcRC != &AMDGPU::VReg_1RegClass && TRI.isSGPRClass(DstRC) &&
186 TRI.hasVGPRs(SrcRC);
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000187}
188
Matt Arsenault0de924b2015-11-02 23:15:42 +0000189static bool isSGPRToVGPRCopy(const TargetRegisterClass *SrcRC,
190 const TargetRegisterClass *DstRC,
191 const SIRegisterInfo &TRI) {
Nicolai Haehnle814abb52018-10-31 13:27:08 +0000192 return DstRC != &AMDGPU::VReg_1RegClass && TRI.isSGPRClass(SrcRC) &&
193 TRI.hasVGPRs(DstRC);
Tom Stellard82166022013-11-13 23:36:37 +0000194}
195
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000196static bool tryChangeVGPRtoSGPRinCopy(MachineInstr &MI,
197 const SIRegisterInfo *TRI,
198 const SIInstrInfo *TII) {
199 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
200 auto &Src = MI.getOperand(1);
201 unsigned DstReg = MI.getOperand(0).getReg();
202 unsigned SrcReg = Src.getReg();
203 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
204 !TargetRegisterInfo::isVirtualRegister(DstReg))
205 return false;
206
207 for (const auto &MO : MRI.reg_nodbg_operands(DstReg)) {
208 const auto *UseMI = MO.getParent();
209 if (UseMI == &MI)
210 continue;
211 if (MO.isDef() || UseMI->getParent() != MI.getParent() ||
212 UseMI->getOpcode() <= TargetOpcode::GENERIC_OP_END ||
213 !TII->isOperandLegal(*UseMI, UseMI->getOperandNo(&MO), &Src))
214 return false;
215 }
216 // Change VGPR to SGPR destination.
217 MRI.setRegClass(DstReg, TRI->getEquivalentSGPRClass(MRI.getRegClass(DstReg)));
218 return true;
219}
220
Matt Arsenault0de924b2015-11-02 23:15:42 +0000221// Distribute an SGPR->VGPR copy of a REG_SEQUENCE into a VGPR REG_SEQUENCE.
222//
223// SGPRx = ...
224// SGPRy = REG_SEQUENCE SGPRx, sub0 ...
225// VGPRz = COPY SGPRy
226//
227// ==>
228//
229// VGPRx = COPY SGPRx
230// VGPRz = REG_SEQUENCE VGPRx, sub0
231//
232// This exposes immediate folding opportunities when materializing 64-bit
233// immediates.
234static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
235 const SIRegisterInfo *TRI,
236 const SIInstrInfo *TII,
237 MachineRegisterInfo &MRI) {
238 assert(MI.isRegSequence());
239
240 unsigned DstReg = MI.getOperand(0).getReg();
241 if (!TRI->isSGPRClass(MRI.getRegClass(DstReg)))
242 return false;
243
244 if (!MRI.hasOneUse(DstReg))
245 return false;
246
247 MachineInstr &CopyUse = *MRI.use_instr_begin(DstReg);
248 if (!CopyUse.isCopy())
249 return false;
250
Matt Arsenaultfe78ffb2017-04-11 22:29:19 +0000251 // It is illegal to have vreg inputs to a physreg defining reg_sequence.
252 if (TargetRegisterInfo::isPhysicalRegister(CopyUse.getOperand(0).getReg()))
253 return false;
254
Matt Arsenault0de924b2015-11-02 23:15:42 +0000255 const TargetRegisterClass *SrcRC, *DstRC;
256 std::tie(SrcRC, DstRC) = getCopyRegClasses(CopyUse, *TRI, MRI);
257
258 if (!isSGPRToVGPRCopy(SrcRC, DstRC, *TRI))
259 return false;
260
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000261 if (tryChangeVGPRtoSGPRinCopy(CopyUse, TRI, TII))
262 return true;
263
Matt Arsenault0de924b2015-11-02 23:15:42 +0000264 // TODO: Could have multiple extracts?
265 unsigned SubReg = CopyUse.getOperand(1).getSubReg();
266 if (SubReg != AMDGPU::NoSubRegister)
267 return false;
268
269 MRI.setRegClass(DstReg, DstRC);
270
271 // SGPRx = ...
272 // SGPRy = REG_SEQUENCE SGPRx, sub0 ...
273 // VGPRz = COPY SGPRy
274
275 // =>
276 // VGPRx = COPY SGPRx
277 // VGPRz = REG_SEQUENCE VGPRx, sub0
278
279 MI.getOperand(0).setReg(CopyUse.getOperand(0).getReg());
280
281 for (unsigned I = 1, N = MI.getNumOperands(); I != N; I += 2) {
282 unsigned SrcReg = MI.getOperand(I).getReg();
Nicolai Haehnle82fc9622016-01-07 17:10:29 +0000283 unsigned SrcSubReg = MI.getOperand(I).getSubReg();
Matt Arsenault0de924b2015-11-02 23:15:42 +0000284
285 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
286 assert(TRI->isSGPRClass(SrcRC) &&
287 "Expected SGPR REG_SEQUENCE to only have SGPR inputs");
288
289 SrcRC = TRI->getSubRegClass(SrcRC, SrcSubReg);
290 const TargetRegisterClass *NewSrcRC = TRI->getEquivalentVGPRClass(SrcRC);
291
292 unsigned TmpReg = MRI.createVirtualRegister(NewSrcRC);
293
Diana Picus116bbab2017-01-13 09:58:52 +0000294 BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(AMDGPU::COPY),
295 TmpReg)
296 .add(MI.getOperand(I));
Matt Arsenault0de924b2015-11-02 23:15:42 +0000297
298 MI.getOperand(I).setReg(TmpReg);
299 }
300
301 CopyUse.eraseFromParent();
302 return true;
303}
304
Tom Stellard9fdbec82016-11-11 23:35:42 +0000305static bool phiHasVGPROperands(const MachineInstr &PHI,
306 const MachineRegisterInfo &MRI,
307 const SIRegisterInfo *TRI,
308 const SIInstrInfo *TII) {
Tom Stellard9fdbec82016-11-11 23:35:42 +0000309 for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) {
310 unsigned Reg = PHI.getOperand(i).getReg();
311 if (TRI->hasVGPRs(MRI.getRegClass(Reg)))
312 return true;
313 }
314 return false;
315}
Eugene Zelenko59e12822017-08-08 00:47:13 +0000316
Tom Stellard9fdbec82016-11-11 23:35:42 +0000317static bool phiHasBreakDef(const MachineInstr &PHI,
318 const MachineRegisterInfo &MRI,
319 SmallSet<unsigned, 8> &Visited) {
Tom Stellard9fdbec82016-11-11 23:35:42 +0000320 for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) {
321 unsigned Reg = PHI.getOperand(i).getReg();
322 if (Visited.count(Reg))
323 continue;
324
325 Visited.insert(Reg);
326
Matt Arsenault2a803692017-04-29 01:26:34 +0000327 MachineInstr *DefInstr = MRI.getVRegDef(Reg);
Tom Stellard9fdbec82016-11-11 23:35:42 +0000328 switch (DefInstr->getOpcode()) {
329 default:
330 break;
Tom Stellard9fdbec82016-11-11 23:35:42 +0000331 case AMDGPU::SI_IF_BREAK:
Tom Stellard9fdbec82016-11-11 23:35:42 +0000332 return true;
333 case AMDGPU::PHI:
334 if (phiHasBreakDef(*DefInstr, MRI, Visited))
335 return true;
336 }
337 }
338 return false;
339}
340
Tom Stellard0bc68812016-11-29 00:46:46 +0000341static bool hasTerminatorThatModifiesExec(const MachineBasicBlock &MBB,
342 const TargetRegisterInfo &TRI) {
343 for (MachineBasicBlock::const_iterator I = MBB.getFirstTerminator(),
344 E = MBB.end(); I != E; ++I) {
345 if (I->modifiesRegister(AMDGPU::EXEC, &TRI))
346 return true;
347 }
348 return false;
349}
350
Tom Stellard00cfa742016-12-06 21:13:30 +0000351static bool isSafeToFoldImmIntoCopy(const MachineInstr *Copy,
352 const MachineInstr *MoveImm,
353 const SIInstrInfo *TII,
354 unsigned &SMovOp,
355 int64_t &Imm) {
Connor Abbott8c217d02017-08-04 18:36:49 +0000356 if (Copy->getOpcode() != AMDGPU::COPY)
357 return false;
358
Tom Stellard00cfa742016-12-06 21:13:30 +0000359 if (!MoveImm->isMoveImmediate())
360 return false;
361
362 const MachineOperand *ImmOp =
363 TII->getNamedOperand(*MoveImm, AMDGPU::OpName::src0);
364 if (!ImmOp->isImm())
365 return false;
366
367 // FIXME: Handle copies with sub-regs.
368 if (Copy->getOperand(0).getSubReg())
369 return false;
370
371 switch (MoveImm->getOpcode()) {
372 default:
373 return false;
374 case AMDGPU::V_MOV_B32_e32:
375 SMovOp = AMDGPU::S_MOV_B32;
376 break;
377 case AMDGPU::V_MOV_B64_PSEUDO:
378 SMovOp = AMDGPU::S_MOV_B64;
379 break;
380 }
381 Imm = ImmOp->getImm();
382 return true;
383}
384
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000385template <class UnaryPredicate>
386bool searchPredecessors(const MachineBasicBlock *MBB,
387 const MachineBasicBlock *CutOff,
388 UnaryPredicate Predicate) {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000389 if (MBB == CutOff)
390 return false;
391
Eugene Zelenko59e12822017-08-08 00:47:13 +0000392 DenseSet<const MachineBasicBlock *> Visited;
393 SmallVector<MachineBasicBlock *, 4> Worklist(MBB->pred_begin(),
394 MBB->pred_end());
Wei Ding74da3502017-04-12 23:51:47 +0000395
396 while (!Worklist.empty()) {
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000397 MachineBasicBlock *MBB = Worklist.pop_back_val();
Wei Ding74da3502017-04-12 23:51:47 +0000398
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000399 if (!Visited.insert(MBB).second)
Wei Ding74da3502017-04-12 23:51:47 +0000400 continue;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000401 if (MBB == CutOff)
402 continue;
403 if (Predicate(MBB))
Wei Ding74da3502017-04-12 23:51:47 +0000404 return true;
405
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000406 Worklist.append(MBB->pred_begin(), MBB->pred_end());
Wei Ding74da3502017-04-12 23:51:47 +0000407 }
408
409 return false;
410}
411
Alexander Timofeevb9347282018-04-25 12:32:46 +0000412static bool predsHasDivergentTerminator(MachineBasicBlock *MBB,
413 const TargetRegisterInfo *TRI) {
414 return searchPredecessors(MBB, nullptr, [TRI](MachineBasicBlock *MBB) {
415 return hasTerminatorThatModifiesExec(*MBB, *TRI); });
416}
417
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000418// Checks if there is potential path From instruction To instruction.
419// If CutOff is specified and it sits in between of that path we ignore
420// a higher portion of the path and report it is not reachable.
421static bool isReachable(const MachineInstr *From,
422 const MachineInstr *To,
423 const MachineBasicBlock *CutOff,
424 MachineDominatorTree &MDT) {
425 // If either From block dominates To block or instructions are in the same
426 // block and From is higher.
427 if (MDT.dominates(From, To))
428 return true;
429
430 const MachineBasicBlock *MBBFrom = From->getParent();
431 const MachineBasicBlock *MBBTo = To->getParent();
432 if (MBBFrom == MBBTo)
433 return false;
434
435 // Instructions are in different blocks, do predecessor search.
436 // We should almost never get here since we do not usually produce M0 stores
437 // other than -1.
438 return searchPredecessors(MBBTo, CutOff, [MBBFrom]
439 (const MachineBasicBlock *MBB) { return MBB == MBBFrom; });
440}
441
442// Hoist and merge identical SGPR initializations into a common predecessor.
443// This is intended to combine M0 initializations, but can work with any
444// SGPR. A VGPR cannot be processed since we cannot guarantee vector
445// executioon.
446static bool hoistAndMergeSGPRInits(unsigned Reg,
447 const MachineRegisterInfo &MRI,
448 MachineDominatorTree &MDT) {
449 // List of inits by immediate value.
Eugene Zelenko59e12822017-08-08 00:47:13 +0000450 using InitListMap = std::map<unsigned, std::list<MachineInstr *>>;
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000451 InitListMap Inits;
452 // List of clobbering instructions.
453 SmallVector<MachineInstr*, 8> Clobbers;
454 bool Changed = false;
455
456 for (auto &MI : MRI.def_instructions(Reg)) {
457 MachineOperand *Imm = nullptr;
458 for (auto &MO: MI.operands()) {
459 if ((MO.isReg() && ((MO.isDef() && MO.getReg() != Reg) || !MO.isDef())) ||
460 (!MO.isImm() && !MO.isReg()) || (MO.isImm() && Imm)) {
461 Imm = nullptr;
462 break;
463 } else if (MO.isImm())
464 Imm = &MO;
465 }
466 if (Imm)
467 Inits[Imm->getImm()].push_front(&MI);
468 else
469 Clobbers.push_back(&MI);
470 }
471
472 for (auto &Init : Inits) {
473 auto &Defs = Init.second;
474
475 for (auto I1 = Defs.begin(), E = Defs.end(); I1 != E; ) {
476 MachineInstr *MI1 = *I1;
477
478 for (auto I2 = std::next(I1); I2 != E; ) {
479 MachineInstr *MI2 = *I2;
480
481 // Check any possible interference
482 auto intereferes = [&](MachineBasicBlock::iterator From,
483 MachineBasicBlock::iterator To) -> bool {
484
485 assert(MDT.dominates(&*To, &*From));
486
487 auto interferes = [&MDT, From, To](MachineInstr* &Clobber) -> bool {
488 const MachineBasicBlock *MBBFrom = From->getParent();
489 const MachineBasicBlock *MBBTo = To->getParent();
490 bool MayClobberFrom = isReachable(Clobber, &*From, MBBTo, MDT);
491 bool MayClobberTo = isReachable(Clobber, &*To, MBBTo, MDT);
492 if (!MayClobberFrom && !MayClobberTo)
493 return false;
494 if ((MayClobberFrom && !MayClobberTo) ||
495 (!MayClobberFrom && MayClobberTo))
496 return true;
497 // Both can clobber, this is not an interference only if both are
498 // dominated by Clobber and belong to the same block or if Clobber
499 // properly dominates To, given that To >> From, so it dominates
500 // both and located in a common dominator.
501 return !((MBBFrom == MBBTo &&
502 MDT.dominates(Clobber, &*From) &&
503 MDT.dominates(Clobber, &*To)) ||
504 MDT.properlyDominates(Clobber->getParent(), MBBTo));
505 };
506
Eugene Zelenko59e12822017-08-08 00:47:13 +0000507 return (llvm::any_of(Clobbers, interferes)) ||
508 (llvm::any_of(Inits, [&](InitListMap::value_type &C) {
509 return C.first != Init.first &&
510 llvm::any_of(C.second, interferes);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000511 }));
512 };
513
514 if (MDT.dominates(MI1, MI2)) {
515 if (!intereferes(MI2, MI1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000516 LLVM_DEBUG(dbgs()
517 << "Erasing from "
518 << printMBBReference(*MI2->getParent()) << " " << *MI2);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000519 MI2->eraseFromParent();
520 Defs.erase(I2++);
521 Changed = true;
522 continue;
523 }
524 } else if (MDT.dominates(MI2, MI1)) {
525 if (!intereferes(MI1, MI2)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000526 LLVM_DEBUG(dbgs()
527 << "Erasing from "
528 << printMBBReference(*MI1->getParent()) << " " << *MI1);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000529 MI1->eraseFromParent();
530 Defs.erase(I1++);
531 Changed = true;
532 break;
533 }
534 } else {
535 auto *MBB = MDT.findNearestCommonDominator(MI1->getParent(),
536 MI2->getParent());
537 if (!MBB) {
538 ++I2;
539 continue;
540 }
541
542 MachineBasicBlock::iterator I = MBB->getFirstNonPHI();
543 if (!intereferes(MI1, I) && !intereferes(MI2, I)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000544 LLVM_DEBUG(dbgs()
545 << "Erasing from "
546 << printMBBReference(*MI1->getParent()) << " " << *MI1
547 << "and moving from "
548 << printMBBReference(*MI2->getParent()) << " to "
549 << printMBBReference(*I->getParent()) << " " << *MI2);
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000550 I->getParent()->splice(I, MI2->getParent(), MI2);
551 MI1->eraseFromParent();
552 Defs.erase(I1++);
553 Changed = true;
554 break;
555 }
556 }
557 ++I2;
558 }
559 ++I1;
560 }
561 }
562
563 if (Changed)
564 MRI.clearKillFlags(Reg);
565
566 return Changed;
567}
568
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000569bool SIFixSGPRCopies::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000570 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000571 MachineRegisterInfo &MRI = MF.getRegInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000572 const SIRegisterInfo *TRI = ST.getRegisterInfo();
573 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard0bc68812016-11-29 00:46:46 +0000574 MDT = &getAnalysis<MachineDominatorTree>();
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000575
576 SmallVector<MachineInstr *, 16> Worklist;
577
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000578 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
579 BI != BE; ++BI) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000580 MachineBasicBlock &MBB = *BI;
581 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000582 I != E; ++I) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000583 MachineInstr &MI = *I;
Tom Stellard82166022013-11-13 23:36:37 +0000584
585 switch (MI.getOpcode()) {
Matt Arsenault85441dd2015-09-21 16:27:22 +0000586 default:
587 continue;
Connor Abbott8c217d02017-08-04 18:36:49 +0000588 case AMDGPU::COPY:
Connor Abbott92638ab2017-08-04 18:36:52 +0000589 case AMDGPU::WQM:
590 case AMDGPU::WWM: {
Matt Arsenaultf0d9e472015-10-13 00:07:54 +0000591 // If the destination register is a physical register there isn't really
592 // much we can do to fix this.
593 if (!TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg()))
594 continue;
595
596 const TargetRegisterClass *SrcRC, *DstRC;
597 std::tie(SrcRC, DstRC) = getCopyRegClasses(MI, *TRI, MRI);
598 if (isVGPRToSGPRCopy(SrcRC, DstRC, *TRI)) {
Matt Arsenault2a803692017-04-29 01:26:34 +0000599 unsigned SrcReg = MI.getOperand(1).getReg();
600 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Scott Linder823549a2018-10-08 18:47:01 +0000601 TII->moveToVALU(MI, MDT);
Matt Arsenault2a803692017-04-29 01:26:34 +0000602 break;
603 }
604
605 MachineInstr *DefMI = MRI.getVRegDef(SrcReg);
Tom Stellard00cfa742016-12-06 21:13:30 +0000606 unsigned SMovOp;
607 int64_t Imm;
608 // If we are just copying an immediate, we can replace the copy with
609 // s_mov_b32.
610 if (isSafeToFoldImmIntoCopy(&MI, DefMI, TII, SMovOp, Imm)) {
611 MI.getOperand(1).ChangeToImmediate(Imm);
612 MI.addImplicitDefUseOperands(MF);
613 MI.setDesc(TII->get(SMovOp));
614 break;
615 }
Scott Linder823549a2018-10-08 18:47:01 +0000616 TII->moveToVALU(MI, MDT);
Stanislav Mekhanoshin465a1ff2017-06-20 18:32:42 +0000617 } else if (isSGPRToVGPRCopy(SrcRC, DstRC, *TRI)) {
618 tryChangeVGPRtoSGPRinCopy(MI, TRI, TII);
Matt Arsenault85441dd2015-09-21 16:27:22 +0000619 }
620
621 break;
622 }
Tom Stellard82166022013-11-13 23:36:37 +0000623 case AMDGPU::PHI: {
Tom Stellard82166022013-11-13 23:36:37 +0000624 unsigned Reg = MI.getOperand(0).getReg();
Tom Stellard82166022013-11-13 23:36:37 +0000625 if (!TRI->isSGPRClass(MRI.getRegClass(Reg)))
626 break;
627
Alexander Timofeevb9347282018-04-25 12:32:46 +0000628 // We don't need to fix the PHI if the common dominator of the
629 // two incoming blocks terminates with a uniform branch.
Changpeng Fangef4dbb42017-08-03 16:37:02 +0000630 bool HasVGPROperand = phiHasVGPROperands(MI, MRI, TRI, TII);
Alexander Timofeevb9347282018-04-25 12:32:46 +0000631 if (MI.getNumExplicitOperands() == 5 && !HasVGPROperand) {
632 MachineBasicBlock *MBB0 = MI.getOperand(2).getMBB();
633 MachineBasicBlock *MBB1 = MI.getOperand(4).getMBB();
634
635 if (!predsHasDivergentTerminator(MBB0, TRI) &&
636 !predsHasDivergentTerminator(MBB1, TRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000637 LLVM_DEBUG(dbgs()
638 << "Not fixing PHI for uniform branch: " << MI << '\n');
Tom Stellard0bc68812016-11-29 00:46:46 +0000639 break;
640 }
641 }
642
Tom Stellard82166022013-11-13 23:36:37 +0000643 // If a PHI node defines an SGPR and any of its operands are VGPRs,
644 // then we need to move it to the VALU.
Tom Stellarddeb3f9e2014-09-24 01:33:26 +0000645 //
646 // Also, if a PHI node defines an SGPR and has all SGPR operands
647 // we must move it to the VALU, because the SGPR operands will
648 // all end up being assigned the same register, which means
649 // there is a potential for a conflict if different threads take
Matt Arsenaultbfaab762014-10-17 00:36:20 +0000650 // different control flow paths.
Tom Stellarddeb3f9e2014-09-24 01:33:26 +0000651 //
652 // For Example:
653 //
654 // sgpr0 = def;
655 // ...
656 // sgpr1 = def;
657 // ...
658 // sgpr2 = PHI sgpr0, sgpr1
659 // use sgpr2;
660 //
661 // Will Become:
662 //
663 // sgpr2 = def;
664 // ...
665 // sgpr2 = def;
666 // ...
667 // use sgpr2
668 //
Tom Stellarddeb3f9e2014-09-24 01:33:26 +0000669 // The one exception to this rule is when one of the operands
670 // is defined by a SI_BREAK, SI_IF_BREAK, or SI_ELSE_BREAK
671 // instruction. In this case, there we know the program will
672 // never enter the second block (the loop) without entering
673 // the first block (where the condition is computed), so there
674 // is no chance for values to be over-written.
675
Tom Stellard9fdbec82016-11-11 23:35:42 +0000676 SmallSet<unsigned, 8> Visited;
Changpeng Fangef4dbb42017-08-03 16:37:02 +0000677 if (HasVGPROperand || !phiHasBreakDef(MI, MRI, Visited)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000678 LLVM_DEBUG(dbgs() << "Fixing PHI: " << MI);
Scott Linder823549a2018-10-08 18:47:01 +0000679 TII->moveToVALU(MI, MDT);
Tom Stellard9fdbec82016-11-11 23:35:42 +0000680 }
Tom Stellard82166022013-11-13 23:36:37 +0000681 break;
682 }
Eugene Zelenko59e12822017-08-08 00:47:13 +0000683 case AMDGPU::REG_SEQUENCE:
Tom Stellard82166022013-11-13 23:36:37 +0000684 if (TRI->hasVGPRs(TII->getOpRegClass(MI, 0)) ||
Matt Arsenault0de924b2015-11-02 23:15:42 +0000685 !hasVGPROperands(MI, TRI)) {
686 foldVGPRCopyIntoRegSequence(MI, TRI, TII, MRI);
Tom Stellard82166022013-11-13 23:36:37 +0000687 continue;
Matt Arsenault0de924b2015-11-02 23:15:42 +0000688 }
Tom Stellard82166022013-11-13 23:36:37 +0000689
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000690 LLVM_DEBUG(dbgs() << "Fixing REG_SEQUENCE: " << MI);
Tom Stellard82166022013-11-13 23:36:37 +0000691
Scott Linder823549a2018-10-08 18:47:01 +0000692 TII->moveToVALU(MI, MDT);
Tom Stellard82166022013-11-13 23:36:37 +0000693 break;
Tom Stellard204e61b2014-04-07 19:45:45 +0000694 case AMDGPU::INSERT_SUBREG: {
Tom Stellarda5687382014-05-15 14:41:55 +0000695 const TargetRegisterClass *DstRC, *Src0RC, *Src1RC;
Tom Stellard204e61b2014-04-07 19:45:45 +0000696 DstRC = MRI.getRegClass(MI.getOperand(0).getReg());
Tom Stellarda5687382014-05-15 14:41:55 +0000697 Src0RC = MRI.getRegClass(MI.getOperand(1).getReg());
698 Src1RC = MRI.getRegClass(MI.getOperand(2).getReg());
699 if (TRI->isSGPRClass(DstRC) &&
700 (TRI->hasVGPRs(Src0RC) || TRI->hasVGPRs(Src1RC))) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000701 LLVM_DEBUG(dbgs() << " Fixing INSERT_SUBREG: " << MI);
Scott Linder823549a2018-10-08 18:47:01 +0000702 TII->moveToVALU(MI, MDT);
Tom Stellarda5687382014-05-15 14:41:55 +0000703 }
704 break;
Tom Stellard204e61b2014-04-07 19:45:45 +0000705 }
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000706 }
707 }
708 }
Matt Arsenault6f679782014-11-17 21:11:34 +0000709
Stanislav Mekhanoshinbd5394b2017-04-24 19:37:54 +0000710 if (MF.getTarget().getOptLevel() > CodeGenOpt::None && EnableM0Merge)
711 hoistAndMergeSGPRInits(AMDGPU::M0, MRI, *MDT);
712
Matt Arsenault6f679782014-11-17 21:11:34 +0000713 return true;
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000714}