Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 1 | //===-- SIWholeQuadMode.cpp - enter and suspend whole quad mode -----------===// |
| 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 |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This pass adds instructions to enable whole quad mode for pixel |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 11 | /// shaders, and whole wavefront mode for all programs. |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 12 | /// |
| 13 | /// Whole quad mode is required for derivative computations, but it interferes |
| 14 | /// with shader side effects (stores and atomics). This pass is run on the |
| 15 | /// scheduled machine IR but before register coalescing, so that machine SSA is |
| 16 | /// available for analysis. It ensures that WQM is enabled when necessary, but |
| 17 | /// disabled around stores and atomics. |
| 18 | /// |
| 19 | /// When necessary, this pass creates a function prolog |
| 20 | /// |
| 21 | /// S_MOV_B64 LiveMask, EXEC |
| 22 | /// S_WQM_B64 EXEC, EXEC |
| 23 | /// |
| 24 | /// to enter WQM at the top of the function and surrounds blocks of Exact |
| 25 | /// instructions by |
| 26 | /// |
| 27 | /// S_AND_SAVEEXEC_B64 Tmp, LiveMask |
| 28 | /// ... |
| 29 | /// S_MOV_B64 EXEC, Tmp |
| 30 | /// |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 31 | /// We also compute when a sequence of instructions requires Whole Wavefront |
| 32 | /// Mode (WWM) and insert instructions to save and restore it: |
| 33 | /// |
| 34 | /// S_OR_SAVEEXEC_B64 Tmp, -1 |
| 35 | /// ... |
| 36 | /// S_MOV_B64 EXEC, Tmp |
| 37 | /// |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 38 | /// In order to avoid excessive switching during sequences of Exact |
| 39 | /// instructions, the pass first analyzes which instructions must be run in WQM |
| 40 | /// (aka which instructions produce values that lead to derivative |
| 41 | /// computations). |
| 42 | /// |
| 43 | /// Basic blocks are always exited in WQM as long as some successor needs WQM. |
| 44 | /// |
| 45 | /// There is room for improvement given better control flow analysis: |
| 46 | /// |
| 47 | /// (1) at the top level (outside of control flow statements, and as long as |
| 48 | /// kill hasn't been used), one SGPR can be saved by recovering WQM from |
| 49 | /// the LiveMask (this is implemented for the entry block). |
| 50 | /// |
| 51 | /// (2) when entire regions (e.g. if-else blocks or entire loops) only |
| 52 | /// consist of exact and don't-care instructions, the switch only has to |
| 53 | /// be done at the entry and exit points rather than potentially in each |
| 54 | /// block of the region. |
| 55 | /// |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
| 58 | #include "AMDGPU.h" |
| 59 | #include "AMDGPUSubtarget.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 60 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 61 | #include "SIInstrInfo.h" |
| 62 | #include "SIMachineFunctionInfo.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/DenseMap.h" |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 64 | #include "llvm/ADT/PostOrderIterator.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 65 | #include "llvm/ADT/SmallVector.h" |
| 66 | #include "llvm/ADT/StringRef.h" |
| 67 | #include "llvm/CodeGen/LiveInterval.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 68 | #include "llvm/CodeGen/LiveIntervals.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 69 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 70 | #include "llvm/CodeGen/MachineFunction.h" |
| 71 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 72 | #include "llvm/CodeGen/MachineInstr.h" |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 73 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 74 | #include "llvm/CodeGen/MachineOperand.h" |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 75 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 76 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 77 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 78 | #include "llvm/IR/CallingConv.h" |
| 79 | #include "llvm/IR/DebugLoc.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 80 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 81 | #include "llvm/MC/MCRegisterInfo.h" |
| 82 | #include "llvm/Pass.h" |
| 83 | #include "llvm/Support/Debug.h" |
| 84 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 85 | #include <cassert> |
| 86 | #include <vector> |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 87 | |
| 88 | using namespace llvm; |
| 89 | |
| 90 | #define DEBUG_TYPE "si-wqm" |
| 91 | |
| 92 | namespace { |
| 93 | |
| 94 | enum { |
| 95 | StateWQM = 0x1, |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 96 | StateWWM = 0x2, |
| 97 | StateExact = 0x4, |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 100 | struct PrintState { |
| 101 | public: |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 102 | int State; |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 103 | |
| 104 | explicit PrintState(int State) : State(State) {} |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Eric Christopher | 3148a1b | 2017-11-16 03:18:15 +0000 | [diff] [blame] | 107 | #ifndef NDEBUG |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 108 | static raw_ostream &operator<<(raw_ostream &OS, const PrintState &PS) { |
| 109 | if (PS.State & StateWQM) |
| 110 | OS << "WQM"; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 111 | if (PS.State & StateWWM) { |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 112 | if (PS.State & StateWQM) |
| 113 | OS << '|'; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 114 | OS << "WWM"; |
| 115 | } |
| 116 | if (PS.State & StateExact) { |
| 117 | if (PS.State & (StateWQM | StateWWM)) |
| 118 | OS << '|'; |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 119 | OS << "Exact"; |
| 120 | } |
| 121 | |
| 122 | return OS; |
| 123 | } |
Eric Christopher | 3148a1b | 2017-11-16 03:18:15 +0000 | [diff] [blame] | 124 | #endif |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 125 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 126 | struct InstrInfo { |
| 127 | char Needs = 0; |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 128 | char Disabled = 0; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 129 | char OutNeeds = 0; |
| 130 | }; |
| 131 | |
| 132 | struct BlockInfo { |
| 133 | char Needs = 0; |
| 134 | char InNeeds = 0; |
| 135 | char OutNeeds = 0; |
| 136 | }; |
| 137 | |
| 138 | struct WorkItem { |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 139 | MachineBasicBlock *MBB = nullptr; |
| 140 | MachineInstr *MI = nullptr; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 141 | |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 142 | WorkItem() = default; |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 143 | WorkItem(MachineBasicBlock *MBB) : MBB(MBB) {} |
| 144 | WorkItem(MachineInstr *MI) : MI(MI) {} |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | class SIWholeQuadMode : public MachineFunctionPass { |
| 148 | private: |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 149 | CallingConv::ID CallingConv; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 150 | const SIInstrInfo *TII; |
| 151 | const SIRegisterInfo *TRI; |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 152 | const GCNSubtarget *ST; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 153 | MachineRegisterInfo *MRI; |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 154 | LiveIntervals *LIS; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 155 | |
| 156 | DenseMap<const MachineInstr *, InstrInfo> Instructions; |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 157 | DenseMap<MachineBasicBlock *, BlockInfo> Blocks; |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 158 | SmallVector<MachineInstr *, 1> LiveMaskQueries; |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 159 | SmallVector<MachineInstr *, 4> LowerToCopyInstrs; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 160 | |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 161 | void printInfo(); |
| 162 | |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 163 | void markInstruction(MachineInstr &MI, char Flag, |
| 164 | std::vector<WorkItem> &Worklist); |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 165 | void markInstructionUses(const MachineInstr &MI, char Flag, |
| 166 | std::vector<WorkItem> &Worklist); |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 167 | char scanInstructions(MachineFunction &MF, std::vector<WorkItem> &Worklist); |
| 168 | void propagateInstruction(MachineInstr &MI, std::vector<WorkItem> &Worklist); |
| 169 | void propagateBlock(MachineBasicBlock &MBB, std::vector<WorkItem> &Worklist); |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 170 | char analyzeFunction(MachineFunction &MF); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 171 | |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 172 | bool requiresCorrectState(const MachineInstr &MI) const; |
| 173 | |
| 174 | MachineBasicBlock::iterator saveSCC(MachineBasicBlock &MBB, |
| 175 | MachineBasicBlock::iterator Before); |
| 176 | MachineBasicBlock::iterator |
| 177 | prepareInsertion(MachineBasicBlock &MBB, MachineBasicBlock::iterator First, |
| 178 | MachineBasicBlock::iterator Last, bool PreferLast, |
| 179 | bool SaveSCC); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 180 | void toExact(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before, |
| 181 | unsigned SaveWQM, unsigned LiveMaskReg); |
| 182 | void toWQM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before, |
| 183 | unsigned SavedWQM); |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 184 | void toWWM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before, |
| 185 | unsigned SaveOrig); |
| 186 | void fromWWM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before, |
| 187 | unsigned SavedOrig); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 188 | void processBlock(MachineBasicBlock &MBB, unsigned LiveMaskReg, bool isEntry); |
| 189 | |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 190 | void lowerLiveMaskQueries(unsigned LiveMaskReg); |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 191 | void lowerCopyInstrs(); |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 192 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 193 | public: |
| 194 | static char ID; |
| 195 | |
| 196 | SIWholeQuadMode() : |
| 197 | MachineFunctionPass(ID) { } |
| 198 | |
| 199 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 200 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 201 | StringRef getPassName() const override { return "SI Whole Quad Mode"; } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 202 | |
| 203 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 204 | AU.addRequired<LiveIntervals>(); |
Matt Arsenault | fa28455 | 2019-03-25 16:47:42 +0000 | [diff] [blame] | 205 | AU.addPreserved<SlotIndexes>(); |
| 206 | AU.addPreserved<LiveIntervals>(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 207 | AU.setPreservesCFG(); |
| 208 | MachineFunctionPass::getAnalysisUsage(AU); |
| 209 | } |
| 210 | }; |
| 211 | |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 212 | } // end anonymous namespace |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 213 | |
| 214 | char SIWholeQuadMode::ID = 0; |
| 215 | |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 216 | INITIALIZE_PASS_BEGIN(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false, |
| 217 | false) |
| 218 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 219 | INITIALIZE_PASS_END(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false, |
| 220 | false) |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 221 | |
| 222 | char &llvm::SIWholeQuadModeID = SIWholeQuadMode::ID; |
| 223 | |
| 224 | FunctionPass *llvm::createSIWholeQuadModePass() { |
| 225 | return new SIWholeQuadMode; |
| 226 | } |
| 227 | |
Eric Christopher | 3148a1b | 2017-11-16 03:18:15 +0000 | [diff] [blame] | 228 | #ifndef NDEBUG |
Eric Christopher | 6348188 | 2017-11-16 03:25:02 +0000 | [diff] [blame] | 229 | LLVM_DUMP_METHOD void SIWholeQuadMode::printInfo() { |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 230 | for (const auto &BII : Blocks) { |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 231 | dbgs() << "\n" |
| 232 | << printMBBReference(*BII.first) << ":\n" |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 233 | << " InNeeds = " << PrintState(BII.second.InNeeds) |
| 234 | << ", Needs = " << PrintState(BII.second.Needs) |
| 235 | << ", OutNeeds = " << PrintState(BII.second.OutNeeds) << "\n\n"; |
| 236 | |
| 237 | for (const MachineInstr &MI : *BII.first) { |
| 238 | auto III = Instructions.find(&MI); |
| 239 | if (III == Instructions.end()) |
| 240 | continue; |
| 241 | |
| 242 | dbgs() << " " << MI << " Needs = " << PrintState(III->second.Needs) |
| 243 | << ", OutNeeds = " << PrintState(III->second.OutNeeds) << '\n'; |
| 244 | } |
| 245 | } |
| 246 | } |
Eric Christopher | 3148a1b | 2017-11-16 03:18:15 +0000 | [diff] [blame] | 247 | #endif |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 248 | |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 249 | void SIWholeQuadMode::markInstruction(MachineInstr &MI, char Flag, |
| 250 | std::vector<WorkItem> &Worklist) { |
| 251 | InstrInfo &II = Instructions[&MI]; |
| 252 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 253 | assert(!(Flag & StateExact) && Flag != 0); |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 254 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 255 | // Remove any disabled states from the flag. The user that required it gets |
| 256 | // an undefined value in the helper lanes. For example, this can happen if |
| 257 | // the result of an atomic is used by instruction that requires WQM, where |
| 258 | // ignoring the request for WQM is correct as per the relevant specs. |
| 259 | Flag &= ~II.Disabled; |
| 260 | |
| 261 | // Ignore if the flag is already encompassed by the existing needs, or we |
| 262 | // just disabled everything. |
| 263 | if ((II.Needs & Flag) == Flag) |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 264 | return; |
| 265 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 266 | II.Needs |= Flag; |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 267 | Worklist.push_back(&MI); |
| 268 | } |
| 269 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 270 | /// Mark all instructions defining the uses in \p MI with \p Flag. |
| 271 | void SIWholeQuadMode::markInstructionUses(const MachineInstr &MI, char Flag, |
| 272 | std::vector<WorkItem> &Worklist) { |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 273 | for (const MachineOperand &Use : MI.uses()) { |
| 274 | if (!Use.isReg() || !Use.isUse()) |
| 275 | continue; |
| 276 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 277 | Register Reg = Use.getReg(); |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 278 | |
| 279 | // Handle physical registers that we need to track; this is mostly relevant |
| 280 | // for VCC, which can appear as the (implicit) input of a uniform branch, |
| 281 | // e.g. when a loop counter is stored in a VGPR. |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 282 | if (!Register::isVirtualRegister(Reg)) { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 283 | if (Reg == AMDGPU::EXEC || Reg == AMDGPU::EXEC_LO) |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 284 | continue; |
| 285 | |
| 286 | for (MCRegUnitIterator RegUnit(Reg, TRI); RegUnit.isValid(); ++RegUnit) { |
| 287 | LiveRange &LR = LIS->getRegUnit(*RegUnit); |
| 288 | const VNInfo *Value = LR.Query(LIS->getInstructionIndex(MI)).valueIn(); |
| 289 | if (!Value) |
| 290 | continue; |
| 291 | |
| 292 | // Since we're in machine SSA, we do not need to track physical |
| 293 | // registers across basic blocks. |
| 294 | if (Value->isPHIDef()) |
| 295 | continue; |
| 296 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 297 | markInstruction(*LIS->getInstructionFromIndex(Value->def), Flag, |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 298 | Worklist); |
| 299 | } |
| 300 | |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | for (MachineInstr &DefMI : MRI->def_instructions(Use.getReg())) |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 305 | markInstruction(DefMI, Flag, Worklist); |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 309 | // Scan instructions to determine which ones require an Exact execmask and |
| 310 | // which ones seed WQM requirements. |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 311 | char SIWholeQuadMode::scanInstructions(MachineFunction &MF, |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 312 | std::vector<WorkItem> &Worklist) { |
| 313 | char GlobalFlags = 0; |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 314 | bool WQMOutputs = MF.getFunction().hasFnAttribute("amdgpu-ps-wqm-outputs"); |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 315 | SmallVector<MachineInstr *, 4> SetInactiveInstrs; |
Carl Ritson | 00e89b4 | 2019-07-26 09:54:12 +0000 | [diff] [blame] | 316 | SmallVector<MachineInstr *, 4> SoftWQMInstrs; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 317 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 318 | // We need to visit the basic blocks in reverse post-order so that we visit |
| 319 | // defs before uses, in particular so that we don't accidentally mark an |
| 320 | // instruction as needing e.g. WQM before visiting it and realizing it needs |
| 321 | // WQM disabled. |
| 322 | ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); |
| 323 | for (auto BI = RPOT.begin(), BE = RPOT.end(); BI != BE; ++BI) { |
| 324 | MachineBasicBlock &MBB = **BI; |
| 325 | BlockInfo &BBI = Blocks[&MBB]; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 326 | |
| 327 | for (auto II = MBB.begin(), IE = MBB.end(); II != IE; ++II) { |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 328 | MachineInstr &MI = *II; |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 329 | InstrInfo &III = Instructions[&MI]; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 330 | unsigned Opcode = MI.getOpcode(); |
Nicolai Haehnle | c00e03b | 2016-06-07 21:37:17 +0000 | [diff] [blame] | 331 | char Flags = 0; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 332 | |
Tim Renouf | 18a1e9d | 2018-05-07 13:21:26 +0000 | [diff] [blame] | 333 | if (TII->isWQM(Opcode)) { |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 334 | // Sampling instructions don't need to produce results for all pixels |
| 335 | // in a quad, they just require all inputs of a quad to have been |
| 336 | // computed for derivatives. |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 337 | markInstructionUses(MI, StateWQM, Worklist); |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 338 | GlobalFlags |= StateWQM; |
| 339 | continue; |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 340 | } else if (Opcode == AMDGPU::WQM) { |
| 341 | // The WQM intrinsic requires its output to have all the helper lanes |
| 342 | // correct, so we need it to be in WQM. |
| 343 | Flags = StateWQM; |
| 344 | LowerToCopyInstrs.push_back(&MI); |
Carl Ritson | 00e89b4 | 2019-07-26 09:54:12 +0000 | [diff] [blame] | 345 | } else if (Opcode == AMDGPU::SOFT_WQM) { |
| 346 | LowerToCopyInstrs.push_back(&MI); |
| 347 | SoftWQMInstrs.push_back(&MI); |
| 348 | continue; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 349 | } else if (Opcode == AMDGPU::WWM) { |
| 350 | // The WWM intrinsic doesn't make the same guarantee, and plus it needs |
| 351 | // to be executed in WQM or Exact so that its copy doesn't clobber |
| 352 | // inactive lanes. |
| 353 | markInstructionUses(MI, StateWWM, Worklist); |
| 354 | GlobalFlags |= StateWWM; |
| 355 | LowerToCopyInstrs.push_back(&MI); |
| 356 | continue; |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 357 | } else if (Opcode == AMDGPU::V_SET_INACTIVE_B32 || |
| 358 | Opcode == AMDGPU::V_SET_INACTIVE_B64) { |
| 359 | III.Disabled = StateWWM; |
| 360 | MachineOperand &Inactive = MI.getOperand(2); |
| 361 | if (Inactive.isReg()) { |
| 362 | if (Inactive.isUndef()) { |
| 363 | LowerToCopyInstrs.push_back(&MI); |
| 364 | } else { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 365 | Register Reg = Inactive.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 366 | if (Register::isVirtualRegister(Reg)) { |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 367 | for (MachineInstr &DefMI : MRI->def_instructions(Reg)) |
| 368 | markInstruction(DefMI, StateWWM, Worklist); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | SetInactiveInstrs.push_back(&MI); |
| 373 | continue; |
Nicolai Haehnle | 8a482b3 | 2016-08-02 19:31:14 +0000 | [diff] [blame] | 374 | } else if (TII->isDisableWQM(MI)) { |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 375 | BBI.Needs |= StateExact; |
| 376 | if (!(BBI.InNeeds & StateExact)) { |
| 377 | BBI.InNeeds |= StateExact; |
| 378 | Worklist.push_back(&MBB); |
| 379 | } |
| 380 | GlobalFlags |= StateExact; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 381 | III.Disabled = StateWQM | StateWWM; |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 382 | continue; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 383 | } else { |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 384 | if (Opcode == AMDGPU::SI_PS_LIVE) { |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 385 | LiveMaskQueries.push_back(&MI); |
Nicolai Haehnle | c00e03b | 2016-06-07 21:37:17 +0000 | [diff] [blame] | 386 | } else if (WQMOutputs) { |
| 387 | // The function is in machine SSA form, which means that physical |
| 388 | // VGPRs correspond to shader inputs and outputs. Inputs are |
| 389 | // only used, outputs are only defined. |
| 390 | for (const MachineOperand &MO : MI.defs()) { |
| 391 | if (!MO.isReg()) |
| 392 | continue; |
| 393 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 394 | Register Reg = MO.getReg(); |
Nicolai Haehnle | c00e03b | 2016-06-07 21:37:17 +0000 | [diff] [blame] | 395 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 396 | if (!Register::isVirtualRegister(Reg) && |
Stanislav Mekhanoshin | e67cc38 | 2019-07-11 21:19:33 +0000 | [diff] [blame] | 397 | TRI->hasVectorRegisters(TRI->getPhysRegClass(Reg))) { |
Nicolai Haehnle | c00e03b | 2016-06-07 21:37:17 +0000 | [diff] [blame] | 398 | Flags = StateWQM; |
| 399 | break; |
| 400 | } |
| 401 | } |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Nicolai Haehnle | c00e03b | 2016-06-07 21:37:17 +0000 | [diff] [blame] | 404 | if (!Flags) |
| 405 | continue; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 408 | markInstruction(MI, Flags, Worklist); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 409 | GlobalFlags |= Flags; |
| 410 | } |
| 411 | } |
| 412 | |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 413 | // Mark sure that any SET_INACTIVE instructions are computed in WQM if WQM is |
| 414 | // ever used anywhere in the function. This implements the corresponding |
| 415 | // semantics of @llvm.amdgcn.set.inactive. |
Carl Ritson | 00e89b4 | 2019-07-26 09:54:12 +0000 | [diff] [blame] | 416 | // Similarly for SOFT_WQM instructions, implementing @llvm.amdgcn.softwqm. |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 417 | if (GlobalFlags & StateWQM) { |
| 418 | for (MachineInstr *MI : SetInactiveInstrs) |
| 419 | markInstruction(*MI, StateWQM, Worklist); |
Carl Ritson | 00e89b4 | 2019-07-26 09:54:12 +0000 | [diff] [blame] | 420 | for (MachineInstr *MI : SoftWQMInstrs) |
| 421 | markInstruction(*MI, StateWQM, Worklist); |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 424 | return GlobalFlags; |
| 425 | } |
| 426 | |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 427 | void SIWholeQuadMode::propagateInstruction(MachineInstr &MI, |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 428 | std::vector<WorkItem>& Worklist) { |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 429 | MachineBasicBlock *MBB = MI.getParent(); |
Nicolai Haehnle | 0a33abd | 2016-03-21 22:54:02 +0000 | [diff] [blame] | 430 | InstrInfo II = Instructions[&MI]; // take a copy to prevent dangling references |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 431 | BlockInfo &BI = Blocks[MBB]; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 432 | |
Nicolai Haehnle | 8a482b3 | 2016-08-02 19:31:14 +0000 | [diff] [blame] | 433 | // Control flow-type instructions and stores to temporary memory that are |
| 434 | // followed by WQM computations must themselves be in WQM. |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 435 | if ((II.OutNeeds & StateWQM) && !(II.Disabled & StateWQM) && |
Nicolai Haehnle | 8a482b3 | 2016-08-02 19:31:14 +0000 | [diff] [blame] | 436 | (MI.isTerminator() || (TII->usesVM_CNT(MI) && MI.mayStore()))) { |
Nicolai Haehnle | 0a33abd | 2016-03-21 22:54:02 +0000 | [diff] [blame] | 437 | Instructions[&MI].Needs = StateWQM; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 438 | II.Needs = StateWQM; |
Nicolai Haehnle | 0a33abd | 2016-03-21 22:54:02 +0000 | [diff] [blame] | 439 | } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 440 | |
| 441 | // Propagate to block level |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 442 | if (II.Needs & StateWQM) { |
| 443 | BI.Needs |= StateWQM; |
| 444 | if (!(BI.InNeeds & StateWQM)) { |
| 445 | BI.InNeeds |= StateWQM; |
| 446 | Worklist.push_back(MBB); |
| 447 | } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Propagate backwards within block |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 451 | if (MachineInstr *PrevMI = MI.getPrevNode()) { |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 452 | char InNeeds = (II.Needs & ~StateWWM) | II.OutNeeds; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 453 | if (!PrevMI->isPHI()) { |
| 454 | InstrInfo &PrevII = Instructions[PrevMI]; |
| 455 | if ((PrevII.OutNeeds | InNeeds) != PrevII.OutNeeds) { |
| 456 | PrevII.OutNeeds |= InNeeds; |
| 457 | Worklist.push_back(PrevMI); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Propagate WQM flag to instruction inputs |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 463 | assert(!(II.Needs & StateExact)); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 464 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 465 | if (II.Needs != 0) |
| 466 | markInstructionUses(MI, II.Needs, Worklist); |
Tim Renouf | 364edcd | 2018-05-27 17:26:11 +0000 | [diff] [blame] | 467 | |
| 468 | // Ensure we process a block containing WWM, even if it does not require any |
| 469 | // WQM transitions. |
| 470 | if (II.Needs & StateWWM) |
| 471 | BI.Needs |= StateWWM; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 474 | void SIWholeQuadMode::propagateBlock(MachineBasicBlock &MBB, |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 475 | std::vector<WorkItem>& Worklist) { |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 476 | BlockInfo BI = Blocks[&MBB]; // Make a copy to prevent dangling references. |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 477 | |
| 478 | // Propagate through instructions |
| 479 | if (!MBB.empty()) { |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 480 | MachineInstr *LastMI = &*MBB.rbegin(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 481 | InstrInfo &LastII = Instructions[LastMI]; |
| 482 | if ((LastII.OutNeeds | BI.OutNeeds) != LastII.OutNeeds) { |
| 483 | LastII.OutNeeds |= BI.OutNeeds; |
| 484 | Worklist.push_back(LastMI); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Predecessor blocks must provide for our WQM/Exact needs. |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 489 | for (MachineBasicBlock *Pred : MBB.predecessors()) { |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 490 | BlockInfo &PredBI = Blocks[Pred]; |
| 491 | if ((PredBI.OutNeeds | BI.InNeeds) == PredBI.OutNeeds) |
| 492 | continue; |
| 493 | |
| 494 | PredBI.OutNeeds |= BI.InNeeds; |
| 495 | PredBI.InNeeds |= BI.InNeeds; |
| 496 | Worklist.push_back(Pred); |
| 497 | } |
| 498 | |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 499 | // All successors must be prepared to accept the same set of WQM/Exact data. |
| 500 | for (MachineBasicBlock *Succ : MBB.successors()) { |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 501 | BlockInfo &SuccBI = Blocks[Succ]; |
| 502 | if ((SuccBI.InNeeds | BI.OutNeeds) == SuccBI.InNeeds) |
| 503 | continue; |
| 504 | |
| 505 | SuccBI.InNeeds |= BI.OutNeeds; |
| 506 | Worklist.push_back(Succ); |
| 507 | } |
| 508 | } |
| 509 | |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 510 | char SIWholeQuadMode::analyzeFunction(MachineFunction &MF) { |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 511 | std::vector<WorkItem> Worklist; |
| 512 | char GlobalFlags = scanInstructions(MF, Worklist); |
| 513 | |
| 514 | while (!Worklist.empty()) { |
| 515 | WorkItem WI = Worklist.back(); |
| 516 | Worklist.pop_back(); |
| 517 | |
| 518 | if (WI.MI) |
| 519 | propagateInstruction(*WI.MI, Worklist); |
| 520 | else |
| 521 | propagateBlock(*WI.MBB, Worklist); |
| 522 | } |
| 523 | |
| 524 | return GlobalFlags; |
| 525 | } |
| 526 | |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 527 | /// Whether \p MI really requires the exec state computed during analysis. |
| 528 | /// |
| 529 | /// Scalar instructions must occasionally be marked WQM for correct propagation |
| 530 | /// (e.g. thread masks leading up to branches), but when it comes to actual |
| 531 | /// execution, they don't care about EXEC. |
| 532 | bool SIWholeQuadMode::requiresCorrectState(const MachineInstr &MI) const { |
| 533 | if (MI.isTerminator()) |
| 534 | return true; |
| 535 | |
| 536 | // Skip instructions that are not affected by EXEC |
| 537 | if (TII->isScalarUnit(MI)) |
| 538 | return false; |
| 539 | |
| 540 | // Generic instructions such as COPY will either disappear by register |
| 541 | // coalescing or be lowered to SALU or VALU instructions. |
| 542 | if (MI.isTransient()) { |
| 543 | if (MI.getNumExplicitOperands() >= 1) { |
| 544 | const MachineOperand &Op = MI.getOperand(0); |
| 545 | if (Op.isReg()) { |
| 546 | if (TRI->isSGPRReg(*MRI, Op.getReg())) { |
| 547 | // SGPR instructions are not affected by EXEC |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | MachineBasicBlock::iterator |
| 558 | SIWholeQuadMode::saveSCC(MachineBasicBlock &MBB, |
| 559 | MachineBasicBlock::iterator Before) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 560 | Register SaveReg = MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 561 | |
| 562 | MachineInstr *Save = |
| 563 | BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), SaveReg) |
| 564 | .addReg(AMDGPU::SCC); |
| 565 | MachineInstr *Restore = |
| 566 | BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), AMDGPU::SCC) |
| 567 | .addReg(SaveReg); |
| 568 | |
| 569 | LIS->InsertMachineInstrInMaps(*Save); |
| 570 | LIS->InsertMachineInstrInMaps(*Restore); |
| 571 | LIS->createAndComputeVirtRegInterval(SaveReg); |
| 572 | |
| 573 | return Restore; |
| 574 | } |
| 575 | |
| 576 | // Return an iterator in the (inclusive) range [First, Last] at which |
| 577 | // instructions can be safely inserted, keeping in mind that some of the |
| 578 | // instructions we want to add necessarily clobber SCC. |
| 579 | MachineBasicBlock::iterator SIWholeQuadMode::prepareInsertion( |
| 580 | MachineBasicBlock &MBB, MachineBasicBlock::iterator First, |
| 581 | MachineBasicBlock::iterator Last, bool PreferLast, bool SaveSCC) { |
| 582 | if (!SaveSCC) |
| 583 | return PreferLast ? Last : First; |
| 584 | |
| 585 | LiveRange &LR = LIS->getRegUnit(*MCRegUnitIterator(AMDGPU::SCC, TRI)); |
| 586 | auto MBBE = MBB.end(); |
| 587 | SlotIndex FirstIdx = First != MBBE ? LIS->getInstructionIndex(*First) |
| 588 | : LIS->getMBBEndIdx(&MBB); |
| 589 | SlotIndex LastIdx = |
| 590 | Last != MBBE ? LIS->getInstructionIndex(*Last) : LIS->getMBBEndIdx(&MBB); |
| 591 | SlotIndex Idx = PreferLast ? LastIdx : FirstIdx; |
| 592 | const LiveRange::Segment *S; |
| 593 | |
| 594 | for (;;) { |
| 595 | S = LR.getSegmentContaining(Idx); |
| 596 | if (!S) |
| 597 | break; |
| 598 | |
| 599 | if (PreferLast) { |
| 600 | SlotIndex Next = S->start.getBaseIndex(); |
| 601 | if (Next < FirstIdx) |
| 602 | break; |
| 603 | Idx = Next; |
| 604 | } else { |
| 605 | SlotIndex Next = S->end.getNextIndex().getBaseIndex(); |
| 606 | if (Next > LastIdx) |
| 607 | break; |
| 608 | Idx = Next; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | MachineBasicBlock::iterator MBBI; |
| 613 | |
| 614 | if (MachineInstr *MI = LIS->getInstructionFromIndex(Idx)) |
| 615 | MBBI = MI; |
| 616 | else { |
| 617 | assert(Idx == LIS->getMBBEndIdx(&MBB)); |
| 618 | MBBI = MBB.end(); |
| 619 | } |
| 620 | |
| 621 | if (S) |
| 622 | MBBI = saveSCC(MBB, MBBI); |
| 623 | |
| 624 | return MBBI; |
| 625 | } |
| 626 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 627 | void SIWholeQuadMode::toExact(MachineBasicBlock &MBB, |
| 628 | MachineBasicBlock::iterator Before, |
Nicolai Haehnle | a56e6b6 | 2016-03-21 20:39:24 +0000 | [diff] [blame] | 629 | unsigned SaveWQM, unsigned LiveMaskReg) { |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 630 | MachineInstr *MI; |
| 631 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 632 | if (SaveWQM) { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 633 | MI = BuildMI(MBB, Before, DebugLoc(), TII->get(ST->isWave32() ? |
| 634 | AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64), |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 635 | SaveWQM) |
| 636 | .addReg(LiveMaskReg); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 637 | } else { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 638 | unsigned Exec = ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; |
| 639 | MI = BuildMI(MBB, Before, DebugLoc(), TII->get(ST->isWave32() ? |
| 640 | AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64), |
| 641 | Exec) |
| 642 | .addReg(Exec) |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 643 | .addReg(LiveMaskReg); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 644 | } |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 645 | |
| 646 | LIS->InsertMachineInstrInMaps(*MI); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void SIWholeQuadMode::toWQM(MachineBasicBlock &MBB, |
| 650 | MachineBasicBlock::iterator Before, |
Nicolai Haehnle | a56e6b6 | 2016-03-21 20:39:24 +0000 | [diff] [blame] | 651 | unsigned SavedWQM) { |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 652 | MachineInstr *MI; |
| 653 | |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 654 | unsigned Exec = ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 655 | if (SavedWQM) { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 656 | MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), Exec) |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 657 | .addReg(SavedWQM); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 658 | } else { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 659 | MI = BuildMI(MBB, Before, DebugLoc(), TII->get(ST->isWave32() ? |
| 660 | AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64), |
| 661 | Exec) |
| 662 | .addReg(Exec); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 663 | } |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 664 | |
| 665 | LIS->InsertMachineInstrInMaps(*MI); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 668 | void SIWholeQuadMode::toWWM(MachineBasicBlock &MBB, |
| 669 | MachineBasicBlock::iterator Before, |
| 670 | unsigned SaveOrig) { |
| 671 | MachineInstr *MI; |
| 672 | |
| 673 | assert(SaveOrig); |
Neil Henning | 0a30f33 | 2019-04-01 15:19:52 +0000 | [diff] [blame] | 674 | MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::ENTER_WWM), SaveOrig) |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 675 | .addImm(-1); |
| 676 | LIS->InsertMachineInstrInMaps(*MI); |
| 677 | } |
| 678 | |
| 679 | void SIWholeQuadMode::fromWWM(MachineBasicBlock &MBB, |
| 680 | MachineBasicBlock::iterator Before, |
| 681 | unsigned SavedOrig) { |
| 682 | MachineInstr *MI; |
| 683 | |
| 684 | assert(SavedOrig); |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 685 | MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::EXIT_WWM), |
| 686 | ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC) |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 687 | .addReg(SavedOrig); |
| 688 | LIS->InsertMachineInstrInMaps(*MI); |
| 689 | } |
| 690 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 691 | void SIWholeQuadMode::processBlock(MachineBasicBlock &MBB, unsigned LiveMaskReg, |
| 692 | bool isEntry) { |
| 693 | auto BII = Blocks.find(&MBB); |
| 694 | if (BII == Blocks.end()) |
| 695 | return; |
| 696 | |
| 697 | const BlockInfo &BI = BII->second; |
| 698 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 699 | // This is a non-entry block that is WQM throughout, so no need to do |
| 700 | // anything. |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 701 | if (!isEntry && BI.Needs == StateWQM && BI.OutNeeds != StateExact) |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 702 | return; |
| 703 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 704 | LLVM_DEBUG(dbgs() << "\nProcessing block " << printMBBReference(MBB) |
| 705 | << ":\n"); |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 706 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 707 | unsigned SavedWQMReg = 0; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 708 | unsigned SavedNonWWMReg = 0; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 709 | bool WQMFromExec = isEntry; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 710 | char State = (isEntry || !(BI.InNeeds & StateWQM)) ? StateExact : StateWQM; |
| 711 | char NonWWMState = 0; |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 712 | const TargetRegisterClass *BoolRC = TRI->getBoolRC(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 713 | |
| 714 | auto II = MBB.getFirstNonPHI(), IE = MBB.end(); |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 715 | if (isEntry) |
| 716 | ++II; // Skip the instruction that saves LiveMask |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 717 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 718 | // This stores the first instruction where it's safe to switch from WQM to |
| 719 | // Exact or vice versa. |
| 720 | MachineBasicBlock::iterator FirstWQM = IE; |
| 721 | |
| 722 | // This stores the first instruction where it's safe to switch from WWM to |
| 723 | // Exact/WQM or to switch to WWM. It must always be the same as, or after, |
| 724 | // FirstWQM since if it's safe to switch to/from WWM, it must be safe to |
| 725 | // switch to/from WQM as well. |
| 726 | MachineBasicBlock::iterator FirstWWM = IE; |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 727 | for (;;) { |
| 728 | MachineBasicBlock::iterator Next = II; |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 729 | char Needs = StateExact | StateWQM; // WWM is disabled by default |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 730 | char OutNeeds = 0; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 731 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 732 | if (FirstWQM == IE) |
| 733 | FirstWQM = II; |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 734 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 735 | if (FirstWWM == IE) |
| 736 | FirstWWM = II; |
| 737 | |
| 738 | // First, figure out the allowed states (Needs) based on the propagated |
| 739 | // flags. |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 740 | if (II != IE) { |
| 741 | MachineInstr &MI = *II; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 742 | |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 743 | if (requiresCorrectState(MI)) { |
| 744 | auto III = Instructions.find(&MI); |
| 745 | if (III != Instructions.end()) { |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 746 | if (III->second.Needs & StateWWM) |
| 747 | Needs = StateWWM; |
| 748 | else if (III->second.Needs & StateWQM) |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 749 | Needs = StateWQM; |
| 750 | else |
| 751 | Needs &= ~III->second.Disabled; |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 752 | OutNeeds = III->second.OutNeeds; |
| 753 | } |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 754 | } else { |
| 755 | // If the instruction doesn't actually need a correct EXEC, then we can |
| 756 | // safely leave WWM enabled. |
| 757 | Needs = StateExact | StateWQM | StateWWM; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 760 | if (MI.isTerminator() && OutNeeds == StateExact) |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 761 | Needs = StateExact; |
| 762 | |
| 763 | if (MI.getOpcode() == AMDGPU::SI_ELSE && BI.OutNeeds == StateExact) |
| 764 | MI.getOperand(3).setImm(1); |
| 765 | |
| 766 | ++Next; |
| 767 | } else { |
| 768 | // End of basic block |
| 769 | if (BI.OutNeeds & StateWQM) |
| 770 | Needs = StateWQM; |
| 771 | else if (BI.OutNeeds == StateExact) |
| 772 | Needs = StateExact; |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 773 | else |
| 774 | Needs = StateWQM | StateExact; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 775 | } |
Nicolai Haehnle | 3b57200 | 2016-07-28 11:39:24 +0000 | [diff] [blame] | 776 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 777 | // Now, transition if necessary. |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 778 | if (!(Needs & State)) { |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 779 | MachineBasicBlock::iterator First; |
| 780 | if (State == StateWWM || Needs == StateWWM) { |
| 781 | // We must switch to or from WWM |
| 782 | First = FirstWWM; |
| 783 | } else { |
| 784 | // We only need to switch to/from WQM, so we can use FirstWQM |
| 785 | First = FirstWQM; |
| 786 | } |
| 787 | |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 788 | MachineBasicBlock::iterator Before = |
| 789 | prepareInsertion(MBB, First, II, Needs == StateWQM, |
| 790 | Needs == StateExact || WQMFromExec); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 791 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 792 | if (State == StateWWM) { |
| 793 | assert(SavedNonWWMReg); |
| 794 | fromWWM(MBB, Before, SavedNonWWMReg); |
| 795 | State = NonWWMState; |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 798 | if (Needs == StateWWM) { |
| 799 | NonWWMState = State; |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 800 | SavedNonWWMReg = MRI->createVirtualRegister(BoolRC); |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 801 | toWWM(MBB, Before, SavedNonWWMReg); |
| 802 | State = StateWWM; |
| 803 | } else { |
| 804 | if (State == StateWQM && (Needs & StateExact) && !(Needs & StateWQM)) { |
| 805 | if (!WQMFromExec && (OutNeeds & StateWQM)) |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 806 | SavedWQMReg = MRI->createVirtualRegister(BoolRC); |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 807 | |
| 808 | toExact(MBB, Before, SavedWQMReg, LiveMaskReg); |
| 809 | State = StateExact; |
| 810 | } else if (State == StateExact && (Needs & StateWQM) && |
| 811 | !(Needs & StateExact)) { |
| 812 | assert(WQMFromExec == (SavedWQMReg == 0)); |
| 813 | |
| 814 | toWQM(MBB, Before, SavedWQMReg); |
| 815 | |
| 816 | if (SavedWQMReg) { |
| 817 | LIS->createAndComputeVirtRegInterval(SavedWQMReg); |
| 818 | SavedWQMReg = 0; |
| 819 | } |
| 820 | State = StateWQM; |
| 821 | } else { |
| 822 | // We can get here if we transitioned from WWM to a non-WWM state that |
| 823 | // already matches our needs, but we shouldn't need to do anything. |
| 824 | assert(Needs & State); |
| 825 | } |
| 826 | } |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 829 | if (Needs != (StateExact | StateWQM | StateWWM)) { |
| 830 | if (Needs != (StateExact | StateWQM)) |
| 831 | FirstWQM = IE; |
| 832 | FirstWWM = IE; |
| 833 | } |
Connor Abbott | de068fe | 2017-08-04 18:36:50 +0000 | [diff] [blame] | 834 | |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 835 | if (II == IE) |
| 836 | break; |
| 837 | II = Next; |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 841 | void SIWholeQuadMode::lowerLiveMaskQueries(unsigned LiveMaskReg) { |
| 842 | for (MachineInstr *MI : LiveMaskQueries) { |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 843 | const DebugLoc &DL = MI->getDebugLoc(); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 844 | Register Dest = MI->getOperand(0).getReg(); |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 845 | MachineInstr *Copy = |
| 846 | BuildMI(*MI->getParent(), MI, DL, TII->get(AMDGPU::COPY), Dest) |
| 847 | .addReg(LiveMaskReg); |
| 848 | |
| 849 | LIS->ReplaceMachineInstrInMaps(*MI, *Copy); |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 850 | MI->eraseFromParent(); |
| 851 | } |
| 852 | } |
| 853 | |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 854 | void SIWholeQuadMode::lowerCopyInstrs() { |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 855 | for (MachineInstr *MI : LowerToCopyInstrs) { |
| 856 | for (unsigned i = MI->getNumExplicitOperands() - 1; i > 1; i--) |
| 857 | MI->RemoveOperand(i); |
Neil Henning | 0a30f33 | 2019-04-01 15:19:52 +0000 | [diff] [blame] | 858 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 859 | const Register Reg = MI->getOperand(0).getReg(); |
Neil Henning | 0a30f33 | 2019-04-01 15:19:52 +0000 | [diff] [blame] | 860 | |
| 861 | if (TRI->isVGPR(*MRI, Reg)) { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 862 | const TargetRegisterClass *regClass = Register::isVirtualRegister(Reg) |
| 863 | ? MRI->getRegClass(Reg) |
| 864 | : TRI->getPhysRegClass(Reg); |
Neil Henning | 0a30f33 | 2019-04-01 15:19:52 +0000 | [diff] [blame] | 865 | |
| 866 | const unsigned MovOp = TII->getMovOpcode(regClass); |
| 867 | MI->setDesc(TII->get(MovOp)); |
| 868 | |
| 869 | // And make it implicitly depend on exec (like all VALU movs should do). |
| 870 | MI->addOperand(MachineOperand::CreateReg(AMDGPU::EXEC, false, true)); |
| 871 | } else { |
| 872 | MI->setDesc(TII->get(AMDGPU::COPY)); |
| 873 | } |
Connor Abbott | 66b9bd6 | 2017-08-04 18:36:54 +0000 | [diff] [blame] | 874 | } |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 877 | bool SIWholeQuadMode::runOnMachineFunction(MachineFunction &MF) { |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 878 | Instructions.clear(); |
| 879 | Blocks.clear(); |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 880 | LiveMaskQueries.clear(); |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 881 | LowerToCopyInstrs.clear(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 882 | CallingConv = MF.getFunction().getCallingConv(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 883 | |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 884 | ST = &MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 885 | |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 886 | TII = ST->getInstrInfo(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 887 | TRI = &TII->getRegisterInfo(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 888 | MRI = &MF.getRegInfo(); |
Nicolai Haehnle | bef0e90 | 2016-08-02 19:17:37 +0000 | [diff] [blame] | 889 | LIS = &getAnalysis<LiveIntervals>(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 890 | |
| 891 | char GlobalFlags = analyzeFunction(MF); |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 892 | unsigned LiveMaskReg = 0; |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 893 | unsigned Exec = ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 894 | if (!(GlobalFlags & StateWQM)) { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 895 | lowerLiveMaskQueries(Exec); |
Carl Ritson | 00e89b4 | 2019-07-26 09:54:12 +0000 | [diff] [blame] | 896 | if (!(GlobalFlags & StateWWM) && LowerToCopyInstrs.empty()) |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 897 | return !LiveMaskQueries.empty(); |
| 898 | } else { |
| 899 | // Store a copy of the original live mask when required |
Duncan P. N. Exon Smith | 4d29511 | 2016-07-08 19:16:05 +0000 | [diff] [blame] | 900 | MachineBasicBlock &Entry = MF.front(); |
| 901 | MachineBasicBlock::iterator EntryMI = Entry.getFirstNonPHI(); |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 902 | |
Duncan P. N. Exon Smith | 4d29511 | 2016-07-08 19:16:05 +0000 | [diff] [blame] | 903 | if (GlobalFlags & StateExact || !LiveMaskQueries.empty()) { |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 904 | LiveMaskReg = MRI->createVirtualRegister(TRI->getBoolRC()); |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 905 | MachineInstr *MI = BuildMI(Entry, EntryMI, DebugLoc(), |
| 906 | TII->get(AMDGPU::COPY), LiveMaskReg) |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 907 | .addReg(Exec); |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 908 | LIS->InsertMachineInstrInMaps(*MI); |
Duncan P. N. Exon Smith | 4d29511 | 2016-07-08 19:16:05 +0000 | [diff] [blame] | 909 | } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 910 | |
Connor Abbott | 92638ab | 2017-08-04 18:36:52 +0000 | [diff] [blame] | 911 | lowerLiveMaskQueries(LiveMaskReg); |
| 912 | |
Duncan P. N. Exon Smith | 4d29511 | 2016-07-08 19:16:05 +0000 | [diff] [blame] | 913 | if (GlobalFlags == StateWQM) { |
| 914 | // For a shader that needs only WQM, we can just set it once. |
Stanislav Mekhanoshin | 5250021 | 2019-06-16 17:13:09 +0000 | [diff] [blame] | 915 | BuildMI(Entry, EntryMI, DebugLoc(), TII->get(ST->isWave32() ? |
| 916 | AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64), |
| 917 | Exec) |
| 918 | .addReg(Exec); |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 919 | |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 920 | lowerCopyInstrs(); |
Duncan P. N. Exon Smith | 4d29511 | 2016-07-08 19:16:05 +0000 | [diff] [blame] | 921 | // EntryMI may become invalid here |
| 922 | return true; |
| 923 | } |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 926 | LLVM_DEBUG(printInfo()); |
Nicolai Haehnle | 3bba6a8 | 2016-09-03 12:26:38 +0000 | [diff] [blame] | 927 | |
Connor Abbott | 8c217d0 | 2017-08-04 18:36:49 +0000 | [diff] [blame] | 928 | lowerCopyInstrs(); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 929 | |
Nicolai Haehnle | b0c9748 | 2016-04-22 04:04:08 +0000 | [diff] [blame] | 930 | // Handle the general case |
Matt Arsenault | 8dff86d | 2016-07-13 05:55:15 +0000 | [diff] [blame] | 931 | for (auto BII : Blocks) |
| 932 | processBlock(*BII.first, LiveMaskReg, BII.first == &*MF.begin()); |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 933 | |
Nicolai Haehnle | e58e0e3 | 2016-09-12 16:25:20 +0000 | [diff] [blame] | 934 | // Physical registers like SCC aren't tracked by default anyway, so just |
| 935 | // removing the ranges we computed is the simplest option for maintaining |
| 936 | // the analysis results. |
| 937 | LIS->removeRegUnit(*MCRegUnitIterator(AMDGPU::SCC, TRI)); |
| 938 | |
Nicolai Haehnle | 213e87f | 2016-03-21 20:28:33 +0000 | [diff] [blame] | 939 | return true; |
| 940 | } |