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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// Copies from VGPR to SGPR registers are illegal and the register coalescer |
| 12 | /// will sometimes generate these illegal copies in situations like this: |
| 13 | /// |
| 14 | /// Register Class <vsrc> is the union of <vgpr> and <sgpr> |
| 15 | /// |
| 16 | /// BB0: |
| 17 | /// %vreg0 <sgpr> = SCALAR_INST |
| 18 | /// %vreg1 <vsrc> = COPY %vreg0 <sgpr> |
| 19 | /// ... |
| 20 | /// BRANCH %cond BB1, BB2 |
| 21 | /// BB1: |
| 22 | /// %vreg2 <vgpr> = VECTOR_INST |
| 23 | /// %vreg3 <vsrc> = COPY %vreg2 <vgpr> |
| 24 | /// BB2: |
| 25 | /// %vreg4 <vsrc> = PHI %vreg1 <vsrc>, <BB#0>, %vreg3 <vrsc>, <BB#1> |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 26 | /// %vreg5 <vgpr> = VECTOR_INST %vreg4 <vsrc> |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 27 | /// |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 28 | /// |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 29 | /// The coalescer will begin at BB0 and eliminate its copy, then the resulting |
| 30 | /// code will look like this: |
| 31 | /// |
| 32 | /// BB0: |
| 33 | /// %vreg0 <sgpr> = SCALAR_INST |
| 34 | /// ... |
| 35 | /// BRANCH %cond BB1, BB2 |
| 36 | /// BB1: |
| 37 | /// %vreg2 <vgpr> = VECTOR_INST |
| 38 | /// %vreg3 <vsrc> = COPY %vreg2 <vgpr> |
| 39 | /// BB2: |
| 40 | /// %vreg4 <sgpr> = PHI %vreg0 <sgpr>, <BB#0>, %vreg3 <vsrc>, <BB#1> |
| 41 | /// %vreg5 <vgpr> = VECTOR_INST %vreg4 <sgpr> |
| 42 | /// |
| 43 | /// Now that the result of the PHI instruction is an SGPR, the register |
| 44 | /// allocator is now forced to constrain the register class of %vreg3 to |
| 45 | /// <sgpr> so we end up with final code like this: |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 46 | /// |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 47 | /// BB0: |
| 48 | /// %vreg0 <sgpr> = SCALAR_INST |
| 49 | /// ... |
| 50 | /// BRANCH %cond BB1, BB2 |
| 51 | /// BB1: |
| 52 | /// %vreg2 <vgpr> = VECTOR_INST |
| 53 | /// %vreg3 <sgpr> = COPY %vreg2 <vgpr> |
| 54 | /// BB2: |
| 55 | /// %vreg4 <sgpr> = PHI %vreg0 <sgpr>, <BB#0>, %vreg3 <sgpr>, <BB#1> |
| 56 | /// %vreg5 <vgpr> = VECTOR_INST %vreg4 <sgpr> |
| 57 | /// |
NAKAMURA Takumi | 78e80cd | 2013-11-14 04:05:22 +0000 | [diff] [blame] | 58 | /// 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] | 59 | /// |
| 60 | /// In order to avoid this problem, this pass searches for PHI instructions |
| 61 | /// which define a <vsrc> register and constrains its definition class to |
| 62 | /// <vgpr> if the user of the PHI's definition register is a vector instruction. |
| 63 | /// If the PHI's definition class is constrained to <vgpr> then the coalescer |
| 64 | /// will be unable to perform the COPY removal from the above example which |
| 65 | /// ultimately led to the creation of an illegal COPY. |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
| 68 | #include "AMDGPU.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 69 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 70 | #include "SIInstrInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 71 | #include "SIRegisterInfo.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" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 84 | #include "llvm/Pass.h" |
| 85 | #include "llvm/Support/CodeGen.h" |
| 86 | #include "llvm/Support/CommandLine.h" |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 87 | #include "llvm/Support/Debug.h" |
Hans Wennborg | a74fd70 | 2013-11-14 23:24:09 +0000 | [diff] [blame] | 88 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 89 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 90 | #include "llvm/Target/TargetRegisterInfo.h" |
| 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"), |
| 106 | cl::init(false)); |
| 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; |
| 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 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 146 | static 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 Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 159 | static std::pair<const TargetRegisterClass *, const TargetRegisterClass *> |
| 160 | getCopyRegClasses(const MachineInstr &Copy, |
| 161 | const SIRegisterInfo &TRI, |
| 162 | const MachineRegisterInfo &MRI) { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 163 | unsigned DstReg = Copy.getOperand(0).getReg(); |
| 164 | unsigned SrcReg = Copy.getOperand(1).getReg(); |
Matt Arsenault | 120a0c9 | 2014-12-03 05:22:39 +0000 | [diff] [blame] | 165 | |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 166 | const TargetRegisterClass *SrcRC = |
| 167 | TargetRegisterInfo::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 | |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 174 | 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 Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 182 | static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC, |
| 183 | const TargetRegisterClass *DstRC, |
| 184 | const SIRegisterInfo &TRI) { |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 185 | return TRI.isSGPRClass(DstRC) && TRI.hasVGPRs(SrcRC); |
| 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) { |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 191 | return TRI.isSGPRClass(SrcRC) && TRI.hasVGPRs(DstRC); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 194 | static bool tryChangeVGPRtoSGPRinCopy(MachineInstr &MI, |
| 195 | const SIRegisterInfo *TRI, |
| 196 | const SIInstrInfo *TII) { |
| 197 | MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
| 198 | auto &Src = MI.getOperand(1); |
| 199 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 200 | unsigned SrcReg = Src.getReg(); |
| 201 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg) || |
| 202 | !TargetRegisterInfo::isVirtualRegister(DstReg)) |
| 203 | return false; |
| 204 | |
| 205 | for (const auto &MO : MRI.reg_nodbg_operands(DstReg)) { |
| 206 | const auto *UseMI = MO.getParent(); |
| 207 | if (UseMI == &MI) |
| 208 | continue; |
| 209 | if (MO.isDef() || UseMI->getParent() != MI.getParent() || |
| 210 | UseMI->getOpcode() <= TargetOpcode::GENERIC_OP_END || |
| 211 | !TII->isOperandLegal(*UseMI, UseMI->getOperandNo(&MO), &Src)) |
| 212 | return false; |
| 213 | } |
| 214 | // Change VGPR to SGPR destination. |
| 215 | MRI.setRegClass(DstReg, TRI->getEquivalentSGPRClass(MRI.getRegClass(DstReg))); |
| 216 | return true; |
| 217 | } |
| 218 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 219 | // Distribute an SGPR->VGPR copy of a REG_SEQUENCE into a VGPR REG_SEQUENCE. |
| 220 | // |
| 221 | // SGPRx = ... |
| 222 | // SGPRy = REG_SEQUENCE SGPRx, sub0 ... |
| 223 | // VGPRz = COPY SGPRy |
| 224 | // |
| 225 | // ==> |
| 226 | // |
| 227 | // VGPRx = COPY SGPRx |
| 228 | // VGPRz = REG_SEQUENCE VGPRx, sub0 |
| 229 | // |
| 230 | // This exposes immediate folding opportunities when materializing 64-bit |
| 231 | // immediates. |
| 232 | static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI, |
| 233 | const SIRegisterInfo *TRI, |
| 234 | const SIInstrInfo *TII, |
| 235 | MachineRegisterInfo &MRI) { |
| 236 | assert(MI.isRegSequence()); |
| 237 | |
| 238 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 239 | if (!TRI->isSGPRClass(MRI.getRegClass(DstReg))) |
| 240 | return false; |
| 241 | |
| 242 | if (!MRI.hasOneUse(DstReg)) |
| 243 | return false; |
| 244 | |
| 245 | MachineInstr &CopyUse = *MRI.use_instr_begin(DstReg); |
| 246 | if (!CopyUse.isCopy()) |
| 247 | return false; |
| 248 | |
Matt Arsenault | fe78ffb | 2017-04-11 22:29:19 +0000 | [diff] [blame] | 249 | // It is illegal to have vreg inputs to a physreg defining reg_sequence. |
| 250 | if (TargetRegisterInfo::isPhysicalRegister(CopyUse.getOperand(0).getReg())) |
| 251 | return false; |
| 252 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 253 | const TargetRegisterClass *SrcRC, *DstRC; |
| 254 | std::tie(SrcRC, DstRC) = getCopyRegClasses(CopyUse, *TRI, MRI); |
| 255 | |
| 256 | if (!isSGPRToVGPRCopy(SrcRC, DstRC, *TRI)) |
| 257 | return false; |
| 258 | |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 259 | if (tryChangeVGPRtoSGPRinCopy(CopyUse, TRI, TII)) |
| 260 | return true; |
| 261 | |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 262 | // TODO: Could have multiple extracts? |
| 263 | unsigned SubReg = CopyUse.getOperand(1).getSubReg(); |
| 264 | if (SubReg != AMDGPU::NoSubRegister) |
| 265 | return false; |
| 266 | |
| 267 | MRI.setRegClass(DstReg, DstRC); |
| 268 | |
| 269 | // SGPRx = ... |
| 270 | // SGPRy = REG_SEQUENCE SGPRx, sub0 ... |
| 271 | // VGPRz = COPY SGPRy |
| 272 | |
| 273 | // => |
| 274 | // VGPRx = COPY SGPRx |
| 275 | // VGPRz = REG_SEQUENCE VGPRx, sub0 |
| 276 | |
| 277 | MI.getOperand(0).setReg(CopyUse.getOperand(0).getReg()); |
| 278 | |
| 279 | for (unsigned I = 1, N = MI.getNumOperands(); I != N; I += 2) { |
| 280 | unsigned SrcReg = MI.getOperand(I).getReg(); |
Nicolai Haehnle | 82fc962 | 2016-01-07 17:10:29 +0000 | [diff] [blame] | 281 | unsigned SrcSubReg = MI.getOperand(I).getSubReg(); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 282 | |
| 283 | const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg); |
| 284 | assert(TRI->isSGPRClass(SrcRC) && |
| 285 | "Expected SGPR REG_SEQUENCE to only have SGPR inputs"); |
| 286 | |
| 287 | SrcRC = TRI->getSubRegClass(SrcRC, SrcSubReg); |
| 288 | const TargetRegisterClass *NewSrcRC = TRI->getEquivalentVGPRClass(SrcRC); |
| 289 | |
| 290 | unsigned TmpReg = MRI.createVirtualRegister(NewSrcRC); |
| 291 | |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 292 | BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(AMDGPU::COPY), |
| 293 | TmpReg) |
| 294 | .add(MI.getOperand(I)); |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 295 | |
| 296 | MI.getOperand(I).setReg(TmpReg); |
| 297 | } |
| 298 | |
| 299 | CopyUse.eraseFromParent(); |
| 300 | return true; |
| 301 | } |
| 302 | |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 303 | static bool phiHasVGPROperands(const MachineInstr &PHI, |
| 304 | const MachineRegisterInfo &MRI, |
| 305 | const SIRegisterInfo *TRI, |
| 306 | const SIInstrInfo *TII) { |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 307 | for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) { |
| 308 | unsigned Reg = PHI.getOperand(i).getReg(); |
| 309 | if (TRI->hasVGPRs(MRI.getRegClass(Reg))) |
| 310 | return true; |
| 311 | } |
| 312 | return false; |
| 313 | } |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 314 | |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 315 | static bool phiHasBreakDef(const MachineInstr &PHI, |
| 316 | const MachineRegisterInfo &MRI, |
| 317 | SmallSet<unsigned, 8> &Visited) { |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 318 | for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) { |
| 319 | unsigned Reg = PHI.getOperand(i).getReg(); |
| 320 | if (Visited.count(Reg)) |
| 321 | continue; |
| 322 | |
| 323 | Visited.insert(Reg); |
| 324 | |
Matt Arsenault | 2a80369 | 2017-04-29 01:26:34 +0000 | [diff] [blame] | 325 | MachineInstr *DefInstr = MRI.getVRegDef(Reg); |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 326 | switch (DefInstr->getOpcode()) { |
| 327 | default: |
| 328 | break; |
| 329 | case AMDGPU::SI_BREAK: |
| 330 | case AMDGPU::SI_IF_BREAK: |
| 331 | case AMDGPU::SI_ELSE_BREAK: |
| 332 | return true; |
| 333 | case AMDGPU::PHI: |
| 334 | if (phiHasBreakDef(*DefInstr, MRI, Visited)) |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 341 | static 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 Stellard | 00cfa74 | 2016-12-06 21:13:30 +0000 | [diff] [blame] | 351 | static bool isSafeToFoldImmIntoCopy(const MachineInstr *Copy, |
| 352 | const MachineInstr *MoveImm, |
| 353 | const SIInstrInfo *TII, |
| 354 | unsigned &SMovOp, |
| 355 | int64_t &Imm) { |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 356 | if (Copy->getOpcode() != AMDGPU::COPY) |
| 357 | return false; |
| 358 | |
Tom Stellard | 00cfa74 | 2016-12-06 21:13:30 +0000 | [diff] [blame] | 359 | 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 Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 385 | template <class UnaryPredicate> |
| 386 | bool searchPredecessors(const MachineBasicBlock *MBB, |
| 387 | const MachineBasicBlock *CutOff, |
| 388 | UnaryPredicate Predicate) { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 389 | if (MBB == CutOff) |
| 390 | return false; |
| 391 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 392 | DenseSet<const MachineBasicBlock *> Visited; |
| 393 | SmallVector<MachineBasicBlock *, 4> Worklist(MBB->pred_begin(), |
| 394 | MBB->pred_end()); |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 395 | |
| 396 | while (!Worklist.empty()) { |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 397 | MachineBasicBlock *MBB = Worklist.pop_back_val(); |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 398 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 399 | if (!Visited.insert(MBB).second) |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 400 | continue; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 401 | if (MBB == CutOff) |
| 402 | continue; |
| 403 | if (Predicate(MBB)) |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 404 | return true; |
| 405 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 406 | Worklist.append(MBB->pred_begin(), MBB->pred_end()); |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return false; |
| 410 | } |
| 411 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 412 | static bool predsHasDivergentTerminator(MachineBasicBlock *MBB, |
| 413 | const TargetRegisterInfo *TRI) { |
| 414 | return searchPredecessors(MBB, nullptr, [TRI](MachineBasicBlock *MBB) { |
| 415 | return hasTerminatorThatModifiesExec(*MBB, *TRI); }); |
| 416 | } |
| 417 | |
| 418 | // 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. |
| 421 | static 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. |
| 446 | static bool hoistAndMergeSGPRInits(unsigned Reg, |
| 447 | const MachineRegisterInfo &MRI, |
| 448 | MachineDominatorTree &MDT) { |
| 449 | // List of inits by immediate value. |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 450 | using InitListMap = std::map<unsigned, std::list<MachineInstr *>>; |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 451 | 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 Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 507 | 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 Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 511 | })); |
| 512 | }; |
| 513 | |
| 514 | if (MDT.dominates(MI1, MI2)) { |
| 515 | if (!intereferes(MI2, MI1)) { |
| 516 | DEBUG(dbgs() << "Erasing from BB#" << MI2->getParent()->getNumber() |
| 517 | << " " << *MI2); |
| 518 | MI2->eraseFromParent(); |
| 519 | Defs.erase(I2++); |
| 520 | Changed = true; |
| 521 | continue; |
| 522 | } |
| 523 | } else if (MDT.dominates(MI2, MI1)) { |
| 524 | if (!intereferes(MI1, MI2)) { |
| 525 | DEBUG(dbgs() << "Erasing from BB#" << MI1->getParent()->getNumber() |
| 526 | << " " << *MI1); |
| 527 | MI1->eraseFromParent(); |
| 528 | Defs.erase(I1++); |
| 529 | Changed = true; |
| 530 | break; |
| 531 | } |
| 532 | } else { |
| 533 | auto *MBB = MDT.findNearestCommonDominator(MI1->getParent(), |
| 534 | MI2->getParent()); |
| 535 | if (!MBB) { |
| 536 | ++I2; |
| 537 | continue; |
| 538 | } |
| 539 | |
| 540 | MachineBasicBlock::iterator I = MBB->getFirstNonPHI(); |
| 541 | if (!intereferes(MI1, I) && !intereferes(MI2, I)) { |
| 542 | DEBUG(dbgs() << "Erasing from BB#" << MI1->getParent()->getNumber() |
| 543 | << " " << *MI1 << "and moving from BB#" |
| 544 | << MI2->getParent()->getNumber() << " to BB#" |
| 545 | << I->getParent()->getNumber() << " " << *MI2); |
| 546 | I->getParent()->splice(I, MI2->getParent(), MI2); |
| 547 | MI1->eraseFromParent(); |
| 548 | Defs.erase(I1++); |
| 549 | Changed = true; |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | ++I2; |
| 554 | } |
| 555 | ++I1; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | if (Changed) |
| 560 | MRI.clearKillFlags(Reg); |
| 561 | |
| 562 | return Changed; |
| 563 | } |
| 564 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 565 | bool SIFixSGPRCopies::runOnMachineFunction(MachineFunction &MF) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 566 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 567 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 568 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 569 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 570 | MDT = &getAnalysis<MachineDominatorTree>(); |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 571 | |
| 572 | SmallVector<MachineInstr *, 16> Worklist; |
| 573 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 574 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 575 | BI != BE; ++BI) { |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 576 | MachineBasicBlock &MBB = *BI; |
| 577 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 578 | I != E; ++I) { |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 579 | MachineInstr &MI = *I; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 580 | |
| 581 | switch (MI.getOpcode()) { |
Matt Arsenault | 85441dd | 2015-09-21 16:27:22 +0000 | [diff] [blame] | 582 | default: |
| 583 | continue; |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 584 | case AMDGPU::COPY: |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 585 | case AMDGPU::WQM: |
| 586 | case AMDGPU::WWM: { |
Matt Arsenault | f0d9e47 | 2015-10-13 00:07:54 +0000 | [diff] [blame] | 587 | // If the destination register is a physical register there isn't really |
| 588 | // much we can do to fix this. |
| 589 | if (!TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg())) |
| 590 | continue; |
| 591 | |
| 592 | const TargetRegisterClass *SrcRC, *DstRC; |
| 593 | std::tie(SrcRC, DstRC) = getCopyRegClasses(MI, *TRI, MRI); |
| 594 | if (isVGPRToSGPRCopy(SrcRC, DstRC, *TRI)) { |
Matt Arsenault | 2a80369 | 2017-04-29 01:26:34 +0000 | [diff] [blame] | 595 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 596 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 597 | TII->moveToVALU(MI); |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | MachineInstr *DefMI = MRI.getVRegDef(SrcReg); |
Tom Stellard | 00cfa74 | 2016-12-06 21:13:30 +0000 | [diff] [blame] | 602 | unsigned SMovOp; |
| 603 | int64_t Imm; |
| 604 | // If we are just copying an immediate, we can replace the copy with |
| 605 | // s_mov_b32. |
| 606 | if (isSafeToFoldImmIntoCopy(&MI, DefMI, TII, SMovOp, Imm)) { |
| 607 | MI.getOperand(1).ChangeToImmediate(Imm); |
| 608 | MI.addImplicitDefUseOperands(MF); |
| 609 | MI.setDesc(TII->get(SMovOp)); |
| 610 | break; |
| 611 | } |
Matt Arsenault | 85441dd | 2015-09-21 16:27:22 +0000 | [diff] [blame] | 612 | TII->moveToVALU(MI); |
Stanislav Mekhanoshin | 465a1ff | 2017-06-20 18:32:42 +0000 | [diff] [blame] | 613 | } else if (isSGPRToVGPRCopy(SrcRC, DstRC, *TRI)) { |
| 614 | tryChangeVGPRtoSGPRinCopy(MI, TRI, TII); |
Matt Arsenault | 85441dd | 2015-09-21 16:27:22 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | break; |
| 618 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 619 | case AMDGPU::PHI: { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 620 | unsigned Reg = MI.getOperand(0).getReg(); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 621 | if (!TRI->isSGPRClass(MRI.getRegClass(Reg))) |
| 622 | break; |
| 623 | |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 624 | // We don't need to fix the PHI if the common dominator of the |
| 625 | // two incoming blocks terminates with a uniform branch. |
Changpeng Fang | ef4dbb4 | 2017-08-03 16:37:02 +0000 | [diff] [blame] | 626 | bool HasVGPROperand = phiHasVGPROperands(MI, MRI, TRI, TII); |
| 627 | if (MI.getNumExplicitOperands() == 5 && !HasVGPROperand) { |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 628 | MachineBasicBlock *MBB0 = MI.getOperand(2).getMBB(); |
| 629 | MachineBasicBlock *MBB1 = MI.getOperand(4).getMBB(); |
| 630 | |
Wei Ding | 74da350 | 2017-04-12 23:51:47 +0000 | [diff] [blame] | 631 | if (!predsHasDivergentTerminator(MBB0, TRI) && |
| 632 | !predsHasDivergentTerminator(MBB1, TRI)) { |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 633 | DEBUG(dbgs() << "Not fixing PHI for uniform branch: " << MI << '\n'); |
| 634 | break; |
| 635 | } |
| 636 | } |
| 637 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 638 | // If a PHI node defines an SGPR and any of its operands are VGPRs, |
| 639 | // then we need to move it to the VALU. |
Tom Stellard | deb3f9e | 2014-09-24 01:33:26 +0000 | [diff] [blame] | 640 | // |
| 641 | // Also, if a PHI node defines an SGPR and has all SGPR operands |
| 642 | // we must move it to the VALU, because the SGPR operands will |
| 643 | // all end up being assigned the same register, which means |
| 644 | // there is a potential for a conflict if different threads take |
Matt Arsenault | bfaab76 | 2014-10-17 00:36:20 +0000 | [diff] [blame] | 645 | // different control flow paths. |
Tom Stellard | deb3f9e | 2014-09-24 01:33:26 +0000 | [diff] [blame] | 646 | // |
| 647 | // For Example: |
| 648 | // |
| 649 | // sgpr0 = def; |
| 650 | // ... |
| 651 | // sgpr1 = def; |
| 652 | // ... |
| 653 | // sgpr2 = PHI sgpr0, sgpr1 |
| 654 | // use sgpr2; |
| 655 | // |
| 656 | // Will Become: |
| 657 | // |
| 658 | // sgpr2 = def; |
| 659 | // ... |
| 660 | // sgpr2 = def; |
| 661 | // ... |
| 662 | // use sgpr2 |
| 663 | // |
Tom Stellard | deb3f9e | 2014-09-24 01:33:26 +0000 | [diff] [blame] | 664 | // The one exception to this rule is when one of the operands |
| 665 | // is defined by a SI_BREAK, SI_IF_BREAK, or SI_ELSE_BREAK |
| 666 | // instruction. In this case, there we know the program will |
| 667 | // never enter the second block (the loop) without entering |
| 668 | // the first block (where the condition is computed), so there |
| 669 | // is no chance for values to be over-written. |
| 670 | |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 671 | SmallSet<unsigned, 8> Visited; |
Changpeng Fang | ef4dbb4 | 2017-08-03 16:37:02 +0000 | [diff] [blame] | 672 | if (HasVGPROperand || !phiHasBreakDef(MI, MRI, Visited)) { |
Tom Stellard | 0bc6881 | 2016-11-29 00:46:46 +0000 | [diff] [blame] | 673 | DEBUG(dbgs() << "Fixing PHI: " << MI); |
Tom Stellard | deb3f9e | 2014-09-24 01:33:26 +0000 | [diff] [blame] | 674 | TII->moveToVALU(MI); |
Tom Stellard | 9fdbec8 | 2016-11-11 23:35:42 +0000 | [diff] [blame] | 675 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 676 | break; |
| 677 | } |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 678 | case AMDGPU::REG_SEQUENCE: |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 679 | if (TRI->hasVGPRs(TII->getOpRegClass(MI, 0)) || |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 680 | !hasVGPROperands(MI, TRI)) { |
| 681 | foldVGPRCopyIntoRegSequence(MI, TRI, TII, MRI); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 682 | continue; |
Matt Arsenault | 0de924b | 2015-11-02 23:15:42 +0000 | [diff] [blame] | 683 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 684 | |
Matt Arsenault | bfaab76 | 2014-10-17 00:36:20 +0000 | [diff] [blame] | 685 | DEBUG(dbgs() << "Fixing REG_SEQUENCE: " << MI); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 686 | |
| 687 | TII->moveToVALU(MI); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 688 | break; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 689 | case AMDGPU::INSERT_SUBREG: { |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 690 | const TargetRegisterClass *DstRC, *Src0RC, *Src1RC; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 691 | DstRC = MRI.getRegClass(MI.getOperand(0).getReg()); |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 692 | Src0RC = MRI.getRegClass(MI.getOperand(1).getReg()); |
| 693 | Src1RC = MRI.getRegClass(MI.getOperand(2).getReg()); |
| 694 | if (TRI->isSGPRClass(DstRC) && |
| 695 | (TRI->hasVGPRs(Src0RC) || TRI->hasVGPRs(Src1RC))) { |
Matt Arsenault | bfaab76 | 2014-10-17 00:36:20 +0000 | [diff] [blame] | 696 | DEBUG(dbgs() << " Fixing INSERT_SUBREG: " << MI); |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 697 | TII->moveToVALU(MI); |
| 698 | } |
| 699 | break; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 700 | } |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | } |
Matt Arsenault | 6f67978 | 2014-11-17 21:11:34 +0000 | [diff] [blame] | 704 | |
Stanislav Mekhanoshin | bd5394b | 2017-04-24 19:37:54 +0000 | [diff] [blame] | 705 | if (MF.getTarget().getOptLevel() > CodeGenOpt::None && EnableM0Merge) |
| 706 | hoistAndMergeSGPRInits(AMDGPU::M0, MRI, *MDT); |
| 707 | |
Matt Arsenault | 6f67978 | 2014-11-17 21:11:34 +0000 | [diff] [blame] | 708 | return true; |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 709 | } |