Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 1 | //===- SIFixSGPRCopies.cpp - Remove potential VGPR => SGPR copies ---------===// |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 6 | // |
| 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 Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 16 | /// %0 <sgpr> = SCALAR_INST |
| 17 | /// %1 <vsrc> = COPY %0 <sgpr> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 18 | /// ... |
| 19 | /// BRANCH %cond BB1, BB2 |
| 20 | /// BB1: |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 21 | /// %2 <vgpr> = VECTOR_INST |
| 22 | /// %3 <vsrc> = COPY %2 <vgpr> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 23 | /// BB2: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 24 | /// %4 <vsrc> = PHI %1 <vsrc>, <%bb.0>, %3 <vrsc>, <%bb.1> |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 25 | /// %5 <vgpr> = VECTOR_INST %4 <vsrc> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 26 | /// |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 27 | /// |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 28 | /// The coalescer will begin at BB0 and eliminate its copy, then the resulting |
| 29 | /// code will look like this: |
| 30 | /// |
| 31 | /// BB0: |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 32 | /// %0 <sgpr> = SCALAR_INST |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 33 | /// ... |
| 34 | /// BRANCH %cond BB1, BB2 |
| 35 | /// BB1: |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 36 | /// %2 <vgpr> = VECTOR_INST |
| 37 | /// %3 <vsrc> = COPY %2 <vgpr> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 38 | /// BB2: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 39 | /// %4 <sgpr> = PHI %0 <sgpr>, <%bb.0>, %3 <vsrc>, <%bb.1> |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 40 | /// %5 <vgpr> = VECTOR_INST %4 <sgpr> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 41 | /// |
| 42 | /// Now that the result of the PHI instruction is an SGPR, the register |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 43 | /// allocator is now forced to constrain the register class of %3 to |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 44 | /// <sgpr> so we end up with final code like this: |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 45 | /// |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 46 | /// BB0: |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 47 | /// %0 <sgpr> = SCALAR_INST |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 48 | /// ... |
| 49 | /// BRANCH %cond BB1, BB2 |
| 50 | /// BB1: |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 51 | /// %2 <vgpr> = VECTOR_INST |
| 52 | /// %3 <sgpr> = COPY %2 <vgpr> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 53 | /// BB2: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 54 | /// %4 <sgpr> = PHI %0 <sgpr>, <%bb.0>, %3 <sgpr>, <%bb.1> |
Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 55 | /// %5 <vgpr> = VECTOR_INST %4 <sgpr> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 56 | /// |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 57 | /// Now this code contains an illegal copy from a VGPR to an SGPR. |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 58 | /// |
| 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 Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 68 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 69 | #include "SIInstrInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 70 | #include "SIRegisterInfo.h" |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 71 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 72 | #include "llvm/ADT/DenseSet.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 73 | #include "llvm/ADT/STLExtras.h" |
| 74 | #include "llvm/ADT/SmallSet.h" |
| 75 | #include "llvm/ADT/SmallVector.h" |
| 76 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 77 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 78 | #include "llvm/CodeGen/MachineFunction.h" |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 79 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 80 | #include "llvm/CodeGen/MachineInstr.h" |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 81 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 82 | #include "llvm/CodeGen/MachineOperand.h" |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 83 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 84 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 85 | #include "llvm/Pass.h" |
| 86 | #include "llvm/Support/CodeGen.h" |
| 87 | #include "llvm/Support/CommandLine.h" |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 88 | #include "llvm/Support/Debug.h" |
Hans Wennborg | a74fd70 | 2013-11-14 23:24:09 +0000 | [diff] [blame] | 89 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 90 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 91 | #include <cassert> |
| 92 | #include <cstdint> |
| 93 | #include <iterator> |
| 94 | #include <list> |
| 95 | #include <map> |
| 96 | #include <tuple> |
| 97 | #include <utility> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 98 | |
| 99 | using namespace llvm; |
| 100 | |
Matt Arsenault | 98f8394 | 2016-04-21 18:21:54 +0000 | [diff] [blame] | 101 | #define DEBUG_TYPE "si-fix-sgpr-copies" |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 102 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 103 | static cl::opt<bool> EnableM0Merge( |
| 104 | "amdgpu-enable-merge-m0", |
| 105 | cl::desc("Merge and hoist M0 initializations"), |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 106 | cl::init(true)); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 107 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 108 | namespace { |
| 109 | |
| 110 | class SIFixSGPRCopies : public MachineFunctionPass { |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 111 | MachineDominatorTree *MDT; |
Alexander Timofeev | b934728 | 2018-04-25 12:32:46 +0000 | [diff] [blame] | 112 | |
Matt Arsenault | 782c03b | 2015-11-03 22:30:13 +0000 | [diff] [blame] | 113 | public: |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 114 | static char ID; |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 115 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 116 | SIFixSGPRCopies() : MachineFunctionPass(ID) {} |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 117 | |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 118 | bool runOnMachineFunction(MachineFunction &MF) override; |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 119 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 120 | StringRef getPassName() const override { return "SI Fix SGPR copies"; } |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 121 | |
Matt Arsenault | 0cb8517 | 2015-09-25 17:21:28 +0000 | [diff] [blame] | 122 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 123 | AU.addRequired<MachineDominatorTree>(); |
| 124 | AU.addPreserved<MachineDominatorTree>(); |
Matt Arsenault | 0cb8517 | 2015-09-25 17:21:28 +0000 | [diff] [blame] | 125 | AU.setPreservesCFG(); |
| 126 | MachineFunctionPass::getAnalysisUsage(AU); |
| 127 | } |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 130 | } // end anonymous namespace |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 131 | |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 132 | INITIALIZE_PASS_BEGIN(SIFixSGPRCopies, DEBUG_TYPE, |
| 133 | "SI Fix SGPR copies", false, false) |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 134 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 135 | INITIALIZE_PASS_END(SIFixSGPRCopies, DEBUG_TYPE, |
| 136 | "SI Fix SGPR copies", false, false) |
| 137 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 138 | char SIFixSGPRCopies::ID = 0; |
| 139 | |
Matt Arsenault | 782c03b | 2015-11-03 22:30:13 +0000 | [diff] [blame] | 140 | char &llvm::SIFixSGPRCopiesID = SIFixSGPRCopies::ID; |
| 141 | |
| 142 | FunctionPass *llvm::createSIFixSGPRCopiesPass() { |
| 143 | return new SIFixSGPRCopies(); |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 146 | static bool hasVectorOperands(const MachineInstr &MI, |
| 147 | const SIRegisterInfo *TRI) { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 148 | const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
| 149 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 150 | if (!MI.getOperand(i).isReg() || |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 151 | !Register::isVirtualRegister(MI.getOperand(i).getReg())) |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 152 | continue; |
| 153 | |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 154 | if (TRI->hasVectorRegisters(MRI.getRegClass(MI.getOperand(i).getReg()))) |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 160 | static std::pair<const TargetRegisterClass *, const TargetRegisterClass *> |
| 161 | getCopyRegClasses(const MachineInstr &Copy, |
| 162 | const SIRegisterInfo &TRI, |
| 163 | const MachineRegisterInfo &MRI) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 164 | Register DstReg = Copy.getOperand(0).getReg(); |
| 165 | Register SrcReg = Copy.getOperand(1).getReg(); |
Matt Arsenault | 120a0c9 | 2014-12-03 05:22:39 +0000 | [diff] [blame] | 166 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 167 | const TargetRegisterClass *SrcRC = Register::isVirtualRegister(SrcReg) |
| 168 | ? MRI.getRegClass(SrcReg) |
| 169 | : TRI.getPhysRegClass(SrcReg); |
Tom Stellard | d33d7f1 | 2015-05-12 14:18:11 +0000 | [diff] [blame] | 170 | |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 171 | // We don't really care about the subregister here. |
| 172 | // SrcRC = TRI.getSubRegClass(SrcRC, Copy.getOperand(1).getSubReg()); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 173 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 174 | const TargetRegisterClass *DstRC = Register::isVirtualRegister(DstReg) |
| 175 | ? MRI.getRegClass(DstReg) |
| 176 | : TRI.getPhysRegClass(DstReg); |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 177 | |
| 178 | return std::make_pair(SrcRC, DstRC); |
| 179 | } |
| 180 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 181 | static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC, |
| 182 | const TargetRegisterClass *DstRC, |
| 183 | const SIRegisterInfo &TRI) { |
Nicolai Haehnle | 814abb5 | 2018-10-31 13:27:08 +0000 | [diff] [blame] | 184 | return SrcRC != &AMDGPU::VReg_1RegClass && TRI.isSGPRClass(DstRC) && |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 185 | TRI.hasVectorRegisters(SrcRC); |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 188 | static bool isSGPRToVGPRCopy(const TargetRegisterClass *SrcRC, |
| 189 | const TargetRegisterClass *DstRC, |
| 190 | const SIRegisterInfo &TRI) { |
Nicolai Haehnle | 814abb5 | 2018-10-31 13:27:08 +0000 | [diff] [blame] | 191 | return DstRC != &AMDGPU::VReg_1RegClass && TRI.isSGPRClass(SrcRC) && |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 192 | TRI.hasVectorRegisters(DstRC); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 195 | static bool tryChangeVGPRtoSGPRinCopy(MachineInstr &MI, |
| 196 | const SIRegisterInfo *TRI, |
| 197 | const SIInstrInfo *TII) { |
| 198 | MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
| 199 | auto &Src = MI.getOperand(1); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 200 | Register DstReg = MI.getOperand(0).getReg(); |
| 201 | Register SrcReg = Src.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 202 | if (!Register::isVirtualRegister(SrcReg) || |
| 203 | !Register::isVirtualRegister(DstReg)) |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 204 | return false; |
| 205 | |
| 206 | for (const auto &MO : MRI.reg_nodbg_operands(DstReg)) { |
| 207 | const auto *UseMI = MO.getParent(); |
| 208 | if (UseMI == &MI) |
| 209 | continue; |
| 210 | if (MO.isDef() || UseMI->getParent() != MI.getParent() || |
| 211 | UseMI->getOpcode() <= TargetOpcode::GENERIC_OP_END || |
| 212 | !TII->isOperandLegal(*UseMI, UseMI->getOperandNo(&MO), &Src)) |
| 213 | return false; |
| 214 | } |
| 215 | // Change VGPR to SGPR destination. |
| 216 | MRI.setRegClass(DstReg, TRI->getEquivalentSGPRClass(MRI.getRegClass(DstReg))); |
| 217 | return true; |
| 218 | } |
| 219 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 220 | // Distribute an SGPR->VGPR copy of a REG_SEQUENCE into a VGPR REG_SEQUENCE. |
| 221 | // |
| 222 | // SGPRx = ... |
| 223 | // SGPRy = REG_SEQUENCE SGPRx, sub0 ... |
| 224 | // VGPRz = COPY SGPRy |
| 225 | // |
| 226 | // ==> |
| 227 | // |
| 228 | // VGPRx = COPY SGPRx |
| 229 | // VGPRz = REG_SEQUENCE VGPRx, sub0 |
| 230 | // |
| 231 | // This exposes immediate folding opportunities when materializing 64-bit |
| 232 | // immediates. |
| 233 | static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI, |
| 234 | const SIRegisterInfo *TRI, |
| 235 | const SIInstrInfo *TII, |
| 236 | MachineRegisterInfo &MRI) { |
| 237 | assert(MI.isRegSequence()); |
| 238 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 239 | Register DstReg = MI.getOperand(0).getReg(); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 240 | if (!TRI->isSGPRClass(MRI.getRegClass(DstReg))) |
| 241 | return false; |
| 242 | |
| 243 | if (!MRI.hasOneUse(DstReg)) |
| 244 | return false; |
| 245 | |
| 246 | MachineInstr &CopyUse = *MRI.use_instr_begin(DstReg); |
| 247 | if (!CopyUse.isCopy()) |
| 248 | return false; |
| 249 | |
Matt Arsenault | fe78ffb | 2017-04-11 22:29:19 +0000 | [diff] [blame] | 250 | // It is illegal to have vreg inputs to a physreg defining reg_sequence. |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 251 | if (Register::isPhysicalRegister(CopyUse.getOperand(0).getReg())) |
Matt Arsenault | fe78ffb | 2017-04-11 22:29:19 +0000 | [diff] [blame] | 252 | return false; |
| 253 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 254 | const TargetRegisterClass *SrcRC, *DstRC; |
| 255 | std::tie(SrcRC, DstRC) = getCopyRegClasses(CopyUse, *TRI, MRI); |
| 256 | |
| 257 | if (!isSGPRToVGPRCopy(SrcRC, DstRC, *TRI)) |
| 258 | return false; |
| 259 | |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 260 | if (tryChangeVGPRtoSGPRinCopy(CopyUse, TRI, TII)) |
| 261 | return true; |
| 262 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 263 | // TODO: Could have multiple extracts? |
| 264 | unsigned SubReg = CopyUse.getOperand(1).getSubReg(); |
| 265 | if (SubReg != AMDGPU::NoSubRegister) |
| 266 | return false; |
| 267 | |
| 268 | MRI.setRegClass(DstReg, DstRC); |
| 269 | |
| 270 | // SGPRx = ... |
| 271 | // SGPRy = REG_SEQUENCE SGPRx, sub0 ... |
| 272 | // VGPRz = COPY SGPRy |
| 273 | |
| 274 | // => |
| 275 | // VGPRx = COPY SGPRx |
| 276 | // VGPRz = REG_SEQUENCE VGPRx, sub0 |
| 277 | |
| 278 | MI.getOperand(0).setReg(CopyUse.getOperand(0).getReg()); |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 279 | bool IsAGPR = TRI->hasAGPRs(DstRC); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 280 | |
| 281 | for (unsigned I = 1, N = MI.getNumOperands(); I != N; I += 2) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 282 | Register SrcReg = MI.getOperand(I).getReg(); |
Nicolai Haehnle | 82fc962 | 2016-01-07 17:10:29 +0000 | [diff] [blame] | 283 | unsigned SrcSubReg = MI.getOperand(I).getSubReg(); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 284 | |
| 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 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 292 | Register TmpReg = MRI.createVirtualRegister(NewSrcRC); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 293 | |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 294 | BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(AMDGPU::COPY), |
| 295 | TmpReg) |
| 296 | .add(MI.getOperand(I)); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 297 | |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 298 | if (IsAGPR) { |
| 299 | const TargetRegisterClass *NewSrcRC = TRI->getEquivalentAGPRClass(SrcRC); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 300 | Register TmpAReg = MRI.createVirtualRegister(NewSrcRC); |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 301 | unsigned Opc = NewSrcRC == &AMDGPU::AGPR_32RegClass ? |
| 302 | AMDGPU::V_ACCVGPR_WRITE_B32 : AMDGPU::COPY; |
| 303 | BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(Opc), |
| 304 | TmpAReg) |
| 305 | .addReg(TmpReg, RegState::Kill); |
| 306 | TmpReg = TmpAReg; |
| 307 | } |
| 308 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 309 | MI.getOperand(I).setReg(TmpReg); |
| 310 | } |
| 311 | |
| 312 | CopyUse.eraseFromParent(); |
| 313 | return true; |
| 314 | } |
| 315 | |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 316 | static bool phiHasVGPROperands(const MachineInstr &PHI, |
| 317 | const MachineRegisterInfo &MRI, |
| 318 | const SIRegisterInfo *TRI, |
| 319 | const SIInstrInfo *TII) { |
| 320 | for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 321 | Register Reg = PHI.getOperand(i).getReg(); |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 322 | if (TRI->hasVGPRs(MRI.getRegClass(Reg))) |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | static bool phiHasBreakDef(const MachineInstr &PHI, |
| 329 | const MachineRegisterInfo &MRI, |
| 330 | SmallSet<unsigned, 8> &Visited) { |
| 331 | for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 332 | Register Reg = PHI.getOperand(i).getReg(); |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 333 | if (Visited.count(Reg)) |
| 334 | continue; |
| 335 | |
| 336 | Visited.insert(Reg); |
| 337 | |
| 338 | MachineInstr *DefInstr = MRI.getVRegDef(Reg); |
| 339 | switch (DefInstr->getOpcode()) { |
| 340 | default: |
| 341 | break; |
| 342 | case AMDGPU::SI_IF_BREAK: |
| 343 | return true; |
| 344 | case AMDGPU::PHI: |
| 345 | if (phiHasBreakDef(*DefInstr, MRI, Visited)) |
| 346 | return true; |
| 347 | } |
| 348 | } |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | static bool hasTerminatorThatModifiesExec(const MachineBasicBlock &MBB, |
| 353 | const TargetRegisterInfo &TRI) { |
| 354 | for (MachineBasicBlock::const_iterator I = MBB.getFirstTerminator(), |
| 355 | E = MBB.end(); I != E; ++I) { |
| 356 | if (I->modifiesRegister(AMDGPU::EXEC, &TRI)) |
| 357 | return true; |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
Tom Stellard | 00cfa74 | 2016-12-06 21:13:30 +0000 | [diff] [blame] | 362 | static bool isSafeToFoldImmIntoCopy(const MachineInstr *Copy, |
| 363 | const MachineInstr *MoveImm, |
| 364 | const SIInstrInfo *TII, |
| 365 | unsigned &SMovOp, |
| 366 | int64_t &Imm) { |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 367 | if (Copy->getOpcode() != AMDGPU::COPY) |
| 368 | return false; |
| 369 | |
Tom Stellard | 00cfa74 | 2016-12-06 21:13:30 +0000 | [diff] [blame] | 370 | if (!MoveImm->isMoveImmediate()) |
| 371 | return false; |
| 372 | |
| 373 | const MachineOperand *ImmOp = |
| 374 | TII->getNamedOperand(*MoveImm, AMDGPU::OpName::src0); |
| 375 | if (!ImmOp->isImm()) |
| 376 | return false; |
| 377 | |
| 378 | // FIXME: Handle copies with sub-regs. |
| 379 | if (Copy->getOperand(0).getSubReg()) |
| 380 | return false; |
| 381 | |
| 382 | switch (MoveImm->getOpcode()) { |
| 383 | default: |
| 384 | return false; |
| 385 | case AMDGPU::V_MOV_B32_e32: |
| 386 | SMovOp = AMDGPU::S_MOV_B32; |
| 387 | break; |
| 388 | case AMDGPU::V_MOV_B64_PSEUDO: |
| 389 | SMovOp = AMDGPU::S_MOV_B64; |
| 390 | break; |
| 391 | } |
| 392 | Imm = ImmOp->getImm(); |
| 393 | return true; |
| 394 | } |
| 395 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 396 | template <class UnaryPredicate> |
| 397 | bool searchPredecessors(const MachineBasicBlock *MBB, |
| 398 | const MachineBasicBlock *CutOff, |
| 399 | UnaryPredicate Predicate) { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 400 | if (MBB == CutOff) |
| 401 | return false; |
| 402 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 403 | DenseSet<const MachineBasicBlock *> Visited; |
| 404 | SmallVector<MachineBasicBlock *, 4> Worklist(MBB->pred_begin(), |
| 405 | MBB->pred_end()); |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 406 | |
| 407 | while (!Worklist.empty()) { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 408 | MachineBasicBlock *MBB = Worklist.pop_back_val(); |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 409 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 410 | if (!Visited.insert(MBB).second) |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 411 | continue; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 412 | if (MBB == CutOff) |
| 413 | continue; |
| 414 | if (Predicate(MBB)) |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 415 | return true; |
| 416 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 417 | Worklist.append(MBB->pred_begin(), MBB->pred_end()); |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return false; |
| 421 | } |
| 422 | |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 423 | static bool predsHasDivergentTerminator(MachineBasicBlock *MBB, |
| 424 | const TargetRegisterInfo *TRI) { |
| 425 | return searchPredecessors(MBB, nullptr, [TRI](MachineBasicBlock *MBB) { |
| 426 | return hasTerminatorThatModifiesExec(*MBB, *TRI); }); |
| 427 | } |
| 428 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 429 | // Checks if there is potential path From instruction To instruction. |
| 430 | // If CutOff is specified and it sits in between of that path we ignore |
| 431 | // a higher portion of the path and report it is not reachable. |
| 432 | static bool isReachable(const MachineInstr *From, |
| 433 | const MachineInstr *To, |
| 434 | const MachineBasicBlock *CutOff, |
| 435 | MachineDominatorTree &MDT) { |
| 436 | // If either From block dominates To block or instructions are in the same |
| 437 | // block and From is higher. |
| 438 | if (MDT.dominates(From, To)) |
| 439 | return true; |
| 440 | |
| 441 | const MachineBasicBlock *MBBFrom = From->getParent(); |
| 442 | const MachineBasicBlock *MBBTo = To->getParent(); |
| 443 | if (MBBFrom == MBBTo) |
| 444 | return false; |
| 445 | |
| 446 | // Instructions are in different blocks, do predecessor search. |
| 447 | // We should almost never get here since we do not usually produce M0 stores |
| 448 | // other than -1. |
| 449 | return searchPredecessors(MBBTo, CutOff, [MBBFrom] |
| 450 | (const MachineBasicBlock *MBB) { return MBB == MBBFrom; }); |
| 451 | } |
| 452 | |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 453 | // Return the first non-prologue instruction in the block. |
| 454 | static MachineBasicBlock::iterator |
| 455 | getFirstNonPrologue(MachineBasicBlock *MBB, const TargetInstrInfo *TII) { |
| 456 | MachineBasicBlock::iterator I = MBB->getFirstNonPHI(); |
| 457 | while (I != MBB->end() && TII->isBasicBlockPrologue(*I)) |
| 458 | ++I; |
| 459 | |
| 460 | return I; |
| 461 | } |
| 462 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 463 | // Hoist and merge identical SGPR initializations into a common predecessor. |
| 464 | // This is intended to combine M0 initializations, but can work with any |
| 465 | // SGPR. A VGPR cannot be processed since we cannot guarantee vector |
| 466 | // executioon. |
| 467 | static bool hoistAndMergeSGPRInits(unsigned Reg, |
| 468 | const MachineRegisterInfo &MRI, |
Austin Kerbow | 666af67 | 2019-09-11 21:28:41 +0000 | [diff] [blame] | 469 | const TargetRegisterInfo *TRI, |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 470 | MachineDominatorTree &MDT, |
| 471 | const TargetInstrInfo *TII) { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 472 | // List of inits by immediate value. |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 473 | using InitListMap = std::map<unsigned, std::list<MachineInstr *>>; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 474 | InitListMap Inits; |
| 475 | // List of clobbering instructions. |
| 476 | SmallVector<MachineInstr*, 8> Clobbers; |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 477 | // List of instructions marked for deletion. |
| 478 | SmallSet<MachineInstr*, 8> MergedInstrs; |
| 479 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 480 | bool Changed = false; |
| 481 | |
| 482 | for (auto &MI : MRI.def_instructions(Reg)) { |
| 483 | MachineOperand *Imm = nullptr; |
Austin Kerbow | 666af67 | 2019-09-11 21:28:41 +0000 | [diff] [blame] | 484 | for (auto &MO : MI.operands()) { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 485 | if ((MO.isReg() && ((MO.isDef() && MO.getReg() != Reg) || !MO.isDef())) || |
| 486 | (!MO.isImm() && !MO.isReg()) || (MO.isImm() && Imm)) { |
| 487 | Imm = nullptr; |
| 488 | break; |
| 489 | } else if (MO.isImm()) |
| 490 | Imm = &MO; |
| 491 | } |
| 492 | if (Imm) |
| 493 | Inits[Imm->getImm()].push_front(&MI); |
| 494 | else |
| 495 | Clobbers.push_back(&MI); |
| 496 | } |
| 497 | |
| 498 | for (auto &Init : Inits) { |
| 499 | auto &Defs = Init.second; |
| 500 | |
| 501 | for (auto I1 = Defs.begin(), E = Defs.end(); I1 != E; ) { |
| 502 | MachineInstr *MI1 = *I1; |
| 503 | |
| 504 | for (auto I2 = std::next(I1); I2 != E; ) { |
| 505 | MachineInstr *MI2 = *I2; |
| 506 | |
| 507 | // Check any possible interference |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 508 | auto interferes = [&](MachineBasicBlock::iterator From, |
| 509 | MachineBasicBlock::iterator To) -> bool { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 510 | |
| 511 | assert(MDT.dominates(&*To, &*From)); |
| 512 | |
| 513 | auto interferes = [&MDT, From, To](MachineInstr* &Clobber) -> bool { |
| 514 | const MachineBasicBlock *MBBFrom = From->getParent(); |
| 515 | const MachineBasicBlock *MBBTo = To->getParent(); |
| 516 | bool MayClobberFrom = isReachable(Clobber, &*From, MBBTo, MDT); |
| 517 | bool MayClobberTo = isReachable(Clobber, &*To, MBBTo, MDT); |
| 518 | if (!MayClobberFrom && !MayClobberTo) |
| 519 | return false; |
| 520 | if ((MayClobberFrom && !MayClobberTo) || |
| 521 | (!MayClobberFrom && MayClobberTo)) |
| 522 | return true; |
| 523 | // Both can clobber, this is not an interference only if both are |
| 524 | // dominated by Clobber and belong to the same block or if Clobber |
| 525 | // properly dominates To, given that To >> From, so it dominates |
| 526 | // both and located in a common dominator. |
| 527 | return !((MBBFrom == MBBTo && |
| 528 | MDT.dominates(Clobber, &*From) && |
| 529 | MDT.dominates(Clobber, &*To)) || |
| 530 | MDT.properlyDominates(Clobber->getParent(), MBBTo)); |
| 531 | }; |
| 532 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 533 | return (llvm::any_of(Clobbers, interferes)) || |
| 534 | (llvm::any_of(Inits, [&](InitListMap::value_type &C) { |
| 535 | return C.first != Init.first && |
| 536 | llvm::any_of(C.second, interferes); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 537 | })); |
| 538 | }; |
| 539 | |
| 540 | if (MDT.dominates(MI1, MI2)) { |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 541 | if (!interferes(MI2, MI1)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 542 | LLVM_DEBUG(dbgs() |
| 543 | << "Erasing from " |
| 544 | << printMBBReference(*MI2->getParent()) << " " << *MI2); |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 545 | MergedInstrs.insert(MI2); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 546 | Changed = true; |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 547 | ++I2; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 548 | continue; |
| 549 | } |
| 550 | } else if (MDT.dominates(MI2, MI1)) { |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 551 | if (!interferes(MI1, MI2)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 552 | LLVM_DEBUG(dbgs() |
| 553 | << "Erasing from " |
| 554 | << printMBBReference(*MI1->getParent()) << " " << *MI1); |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 555 | MergedInstrs.insert(MI1); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 556 | Changed = true; |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 557 | ++I1; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | } else { |
| 561 | auto *MBB = MDT.findNearestCommonDominator(MI1->getParent(), |
| 562 | MI2->getParent()); |
| 563 | if (!MBB) { |
| 564 | ++I2; |
| 565 | continue; |
| 566 | } |
| 567 | |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 568 | MachineBasicBlock::iterator I = getFirstNonPrologue(MBB, TII); |
| 569 | if (!interferes(MI1, I) && !interferes(MI2, I)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 570 | LLVM_DEBUG(dbgs() |
| 571 | << "Erasing from " |
| 572 | << printMBBReference(*MI1->getParent()) << " " << *MI1 |
| 573 | << "and moving from " |
| 574 | << printMBBReference(*MI2->getParent()) << " to " |
| 575 | << printMBBReference(*I->getParent()) << " " << *MI2); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 576 | I->getParent()->splice(I, MI2->getParent(), MI2); |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 577 | MergedInstrs.insert(MI1); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 578 | Changed = true; |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 579 | ++I1; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | } |
| 583 | ++I2; |
| 584 | } |
| 585 | ++I1; |
| 586 | } |
| 587 | } |
| 588 | |
Austin Kerbow | 666af67 | 2019-09-11 21:28:41 +0000 | [diff] [blame] | 589 | // Remove initializations that were merged into another. |
| 590 | for (auto &Init : Inits) { |
| 591 | auto &Defs = Init.second; |
Austin Kerbow | cf321f4 | 2019-09-12 19:12:21 +0000 | [diff] [blame] | 592 | auto I = Defs.begin(); |
| 593 | while (I != Defs.end()) { |
Austin Kerbow | 666af67 | 2019-09-11 21:28:41 +0000 | [diff] [blame] | 594 | if (MergedInstrs.count(*I)) { |
| 595 | (*I)->eraseFromParent(); |
| 596 | I = Defs.erase(I); |
Austin Kerbow | cf321f4 | 2019-09-12 19:12:21 +0000 | [diff] [blame] | 597 | } else |
| 598 | ++I; |
| 599 | } |
Austin Kerbow | 666af67 | 2019-09-11 21:28:41 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // Try to schedule SGPR initializations as early as possible in the MBB. |
| 603 | for (auto &Init : Inits) { |
| 604 | auto &Defs = Init.second; |
| 605 | for (auto MI : Defs) { |
| 606 | auto MBB = MI->getParent(); |
| 607 | MachineInstr &BoundaryMI = *getFirstNonPrologue(MBB, TII); |
| 608 | MachineBasicBlock::reverse_iterator B(BoundaryMI); |
| 609 | // Check if B should actually be a bondary. If not set the previous |
| 610 | // instruction as the boundary instead. |
| 611 | if (!TII->isBasicBlockPrologue(*B)) |
| 612 | B++; |
| 613 | |
| 614 | auto R = std::next(MI->getReverseIterator()); |
| 615 | const unsigned Threshold = 50; |
| 616 | // Search until B or Threashold for a place to insert the initialization. |
| 617 | for (unsigned I = 0; R != B && I < Threshold; ++R, ++I) |
| 618 | if (R->readsRegister(Reg, TRI) || R->definesRegister(Reg, TRI) || |
| 619 | TII->isSchedulingBoundary(*R, MBB, *MBB->getParent())) |
| 620 | break; |
| 621 | |
| 622 | // Move to directly after R. |
| 623 | if (&*--R != MI) |
| 624 | MBB->splice(*R, MBB, MI); |
| 625 | } |
| 626 | } |
Austin Kerbow | 423b4a1 | 2019-07-15 22:07:05 +0000 | [diff] [blame] | 627 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 628 | if (Changed) |
| 629 | MRI.clearKillFlags(Reg); |
| 630 | |
| 631 | return Changed; |
| 632 | } |
| 633 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 634 | bool SIFixSGPRCopies::runOnMachineFunction(MachineFunction &MF) { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 635 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 636 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 637 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 638 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 639 | MDT = &getAnalysis<MachineDominatorTree>(); |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 640 | |
| 641 | SmallVector<MachineInstr *, 16> Worklist; |
| 642 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 643 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 644 | BI != BE; ++BI) { |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 645 | MachineBasicBlock &MBB = *BI; |
| 646 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 647 | I != E; ++I) { |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 648 | MachineInstr &MI = *I; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 649 | |
| 650 | switch (MI.getOpcode()) { |
Matt Arsenault | 85441dd | 2015-09-21 16:27:22 +0000 | [diff] [blame] | 651 | default: |
| 652 | continue; |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 653 | case AMDGPU::COPY: |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 654 | case AMDGPU::WQM: |
Carl Ritson | 00e89b4 | 2019-07-26 09:54:12 +0000 | [diff] [blame] | 655 | case AMDGPU::SOFT_WQM: |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 656 | case AMDGPU::WWM: { |
Matt Arsenault | aff2995 | 2019-08-01 18:27:11 +0000 | [diff] [blame] | 657 | Register DstReg = MI.getOperand(0).getReg(); |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 658 | |
| 659 | const TargetRegisterClass *SrcRC, *DstRC; |
| 660 | std::tie(SrcRC, DstRC) = getCopyRegClasses(MI, *TRI, MRI); |
Matt Arsenault | aff2995 | 2019-08-01 18:27:11 +0000 | [diff] [blame] | 661 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 662 | if (!Register::isVirtualRegister(DstReg)) { |
Matt Arsenault | aff2995 | 2019-08-01 18:27:11 +0000 | [diff] [blame] | 663 | // If the destination register is a physical register there isn't |
| 664 | // really much we can do to fix this. |
| 665 | // Some special instructions use M0 as an input. Some even only use |
| 666 | // the first lane. Insert a readfirstlane and hope for the best. |
| 667 | if (DstReg == AMDGPU::M0 && TRI->hasVectorRegisters(SrcRC)) { |
| 668 | Register TmpReg |
| 669 | = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); |
| 670 | |
| 671 | BuildMI(MBB, MI, MI.getDebugLoc(), |
| 672 | TII->get(AMDGPU::V_READFIRSTLANE_B32), TmpReg) |
| 673 | .add(MI.getOperand(1)); |
| 674 | MI.getOperand(1).setReg(TmpReg); |
| 675 | } |
| 676 | |
| 677 | continue; |
| 678 | } |
| 679 | |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 680 | if (isVGPRToSGPRCopy(SrcRC, DstRC, *TRI)) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 681 | Register SrcReg = MI.getOperand(1).getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 682 | if (!Register::isVirtualRegister(SrcReg)) { |
Scott Linder | 823549a | 2018-10-08 18:47:01 +0000 | [diff] [blame] | 683 | TII->moveToVALU(MI, MDT); |
Matt Arsenault | 2a80369 | 2017-04-29 01:26:34 +0000 | [diff] [blame] | 684 | break; |
| 685 | } |
| 686 | |
| 687 | MachineInstr *DefMI = MRI.getVRegDef(SrcReg); |
Tom Stellard | 00cfa74 | 2016-12-06 21:13:30 +0000 | [diff] [blame] | 688 | unsigned SMovOp; |
| 689 | int64_t Imm; |
| 690 | // If we are just copying an immediate, we can replace the copy with |
| 691 | // s_mov_b32. |
| 692 | if (isSafeToFoldImmIntoCopy(&MI, DefMI, TII, SMovOp, Imm)) { |
| 693 | MI.getOperand(1).ChangeToImmediate(Imm); |
| 694 | MI.addImplicitDefUseOperands(MF); |
| 695 | MI.setDesc(TII->get(SMovOp)); |
| 696 | break; |
| 697 | } |
Scott Linder | 823549a | 2018-10-08 18:47:01 +0000 | [diff] [blame] | 698 | TII->moveToVALU(MI, MDT); |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 699 | } else if (isSGPRToVGPRCopy(SrcRC, DstRC, *TRI)) { |
| 700 | tryChangeVGPRtoSGPRinCopy(MI, TRI, TII); |
Matt Arsenault | 85441dd | 2015-09-21 16:27:22 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | break; |
| 704 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 705 | case AMDGPU::PHI: { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 706 | Register Reg = MI.getOperand(0).getReg(); |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 707 | if (!TRI->isSGPRClass(MRI.getRegClass(Reg))) |
| 708 | break; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 709 | |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 710 | // We don't need to fix the PHI if the common dominator of the |
| 711 | // two incoming blocks terminates with a uniform branch. |
| 712 | bool HasVGPROperand = phiHasVGPROperands(MI, MRI, TRI, TII); |
| 713 | if (MI.getNumExplicitOperands() == 5 && !HasVGPROperand) { |
| 714 | MachineBasicBlock *MBB0 = MI.getOperand(2).getMBB(); |
| 715 | MachineBasicBlock *MBB1 = MI.getOperand(4).getMBB(); |
Alexander Timofeev | b934728 | 2018-04-25 12:32:46 +0000 | [diff] [blame] | 716 | |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 717 | if (!predsHasDivergentTerminator(MBB0, TRI) && |
| 718 | !predsHasDivergentTerminator(MBB1, TRI)) { |
| 719 | LLVM_DEBUG(dbgs() |
| 720 | << "Not fixing PHI for uniform branch: " << MI << '\n'); |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | } |
| 724 | |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 725 | // If a PHI node defines an SGPR and any of its operands are VGPRs, |
| 726 | // then we need to move it to the VALU. |
| 727 | // |
| 728 | // Also, if a PHI node defines an SGPR and has all SGPR operands |
| 729 | // we must move it to the VALU, because the SGPR operands will |
| 730 | // all end up being assigned the same register, which means |
| 731 | // there is a potential for a conflict if different threads take |
| 732 | // different control flow paths. |
| 733 | // |
| 734 | // For Example: |
| 735 | // |
| 736 | // sgpr0 = def; |
| 737 | // ... |
| 738 | // sgpr1 = def; |
| 739 | // ... |
| 740 | // sgpr2 = PHI sgpr0, sgpr1 |
| 741 | // use sgpr2; |
| 742 | // |
| 743 | // Will Become: |
| 744 | // |
| 745 | // sgpr2 = def; |
| 746 | // ... |
| 747 | // sgpr2 = def; |
| 748 | // ... |
| 749 | // use sgpr2 |
| 750 | // |
| 751 | // The one exception to this rule is when one of the operands |
| 752 | // is defined by a SI_BREAK, SI_IF_BREAK, or SI_ELSE_BREAK |
| 753 | // instruction. In this case, there we know the program will |
| 754 | // never enter the second block (the loop) without entering |
| 755 | // the first block (where the condition is computed), so there |
| 756 | // is no chance for values to be over-written. |
| 757 | |
| 758 | SmallSet<unsigned, 8> Visited; |
| 759 | if (HasVGPROperand || !phiHasBreakDef(MI, MRI, Visited)) { |
Michael Liao | 7166843 | 2019-05-28 16:29:39 +0000 | [diff] [blame] | 760 | LLVM_DEBUG(dbgs() << "Fixing PHI: " << MI); |
Alexander Timofeev | 37bd9bd | 2019-06-06 21:13:02 +0000 | [diff] [blame] | 761 | TII->moveToVALU(MI, MDT); |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 762 | } |
Alexander Timofeev | ba447ba | 2019-05-26 20:33:26 +0000 | [diff] [blame] | 763 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 764 | break; |
| 765 | } |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 766 | case AMDGPU::REG_SEQUENCE: |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 767 | if (TRI->hasVectorRegisters(TII->getOpRegClass(MI, 0)) || |
| 768 | !hasVectorOperands(MI, TRI)) { |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 769 | foldVGPRCopyIntoRegSequence(MI, TRI, TII, MRI); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 770 | continue; |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 771 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 772 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 773 | LLVM_DEBUG(dbgs() << "Fixing REG_SEQUENCE: " << MI); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 774 | |
Scott Linder | 823549a | 2018-10-08 18:47:01 +0000 | [diff] [blame] | 775 | TII->moveToVALU(MI, MDT); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 776 | break; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 777 | case AMDGPU::INSERT_SUBREG: { |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 778 | const TargetRegisterClass *DstRC, *Src0RC, *Src1RC; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 779 | DstRC = MRI.getRegClass(MI.getOperand(0).getReg()); |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 780 | Src0RC = MRI.getRegClass(MI.getOperand(1).getReg()); |
| 781 | Src1RC = MRI.getRegClass(MI.getOperand(2).getReg()); |
| 782 | if (TRI->isSGPRClass(DstRC) && |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 783 | (TRI->hasVectorRegisters(Src0RC) || |
| 784 | TRI->hasVectorRegisters(Src1RC))) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 785 | LLVM_DEBUG(dbgs() << " Fixing INSERT_SUBREG: " << MI); |
Scott Linder | 823549a | 2018-10-08 18:47:01 +0000 | [diff] [blame] | 786 | TII->moveToVALU(MI, MDT); |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 787 | } |
| 788 | break; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 789 | } |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | } |
Matt Arsenault | 6f67978 | 2014-11-17 21:11:34 +0000 | [diff] [blame] | 793 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 794 | if (MF.getTarget().getOptLevel() > CodeGenOpt::None && EnableM0Merge) |
Austin Kerbow | 666af67 | 2019-09-11 21:28:41 +0000 | [diff] [blame] | 795 | hoistAndMergeSGPRInits(AMDGPU::M0, MRI, TRI, *MDT, TII); |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 796 | |
Matt Arsenault | 6f67978 | 2014-11-17 21:11:34 +0000 | [diff] [blame] | 797 | return true; |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 798 | } |