blob: 600b80d83004b091c28ef486fe3f4eeea9643e29 [file] [log] [blame]
Nicolai Haehnle213e87f2016-03-21 20:28:33 +00001//===-- SIWholeQuadMode.cpp - enter and suspend whole quad mode -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Nicolai Haehnle213e87f2016-03-21 20:28:33 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This pass adds instructions to enable whole quad mode for pixel
Connor Abbott92638ab2017-08-04 18:36:52 +000011/// shaders, and whole wavefront mode for all programs.
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000012///
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 Abbott92638ab2017-08-04 18:36:52 +000031/// 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 Haehnle213e87f2016-03-21 20:28:33 +000038/// 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 Kleckner05da2fe2019-11-13 13:15:01 -080060#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000061#include "SIInstrInfo.h"
62#include "SIMachineFunctionInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000063#include "llvm/ADT/DenseMap.h"
Connor Abbottde068fe2017-08-04 18:36:50 +000064#include "llvm/ADT/PostOrderIterator.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000065#include "llvm/ADT/SmallVector.h"
66#include "llvm/ADT/StringRef.h"
67#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunf8422972017-12-13 02:51:04 +000068#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000069#include "llvm/CodeGen/MachineBasicBlock.h"
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000070#include "llvm/CodeGen/MachineFunction.h"
71#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000072#include "llvm/CodeGen/MachineInstr.h"
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000073#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000074#include "llvm/CodeGen/MachineOperand.h"
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000075#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000076#include "llvm/CodeGen/SlotIndexes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000077#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000078#include "llvm/IR/CallingConv.h"
79#include "llvm/IR/DebugLoc.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080080#include "llvm/InitializePasses.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000081#include "llvm/MC/MCRegisterInfo.h"
82#include "llvm/Pass.h"
83#include "llvm/Support/Debug.h"
84#include "llvm/Support/raw_ostream.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000085#include <cassert>
86#include <vector>
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000087
88using namespace llvm;
89
90#define DEBUG_TYPE "si-wqm"
91
92namespace {
93
94enum {
95 StateWQM = 0x1,
Connor Abbott92638ab2017-08-04 18:36:52 +000096 StateWWM = 0x2,
97 StateExact = 0x4,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000098};
99
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000100struct PrintState {
101public:
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000102 int State;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000103
104 explicit PrintState(int State) : State(State) {}
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000105};
106
Eric Christopher3148a1b2017-11-16 03:18:15 +0000107#ifndef NDEBUG
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000108static raw_ostream &operator<<(raw_ostream &OS, const PrintState &PS) {
109 if (PS.State & StateWQM)
110 OS << "WQM";
Connor Abbott92638ab2017-08-04 18:36:52 +0000111 if (PS.State & StateWWM) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000112 if (PS.State & StateWQM)
113 OS << '|';
Connor Abbott92638ab2017-08-04 18:36:52 +0000114 OS << "WWM";
115 }
116 if (PS.State & StateExact) {
117 if (PS.State & (StateWQM | StateWWM))
118 OS << '|';
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000119 OS << "Exact";
120 }
121
122 return OS;
123}
Eric Christopher3148a1b2017-11-16 03:18:15 +0000124#endif
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000125
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000126struct InstrInfo {
127 char Needs = 0;
Connor Abbottde068fe2017-08-04 18:36:50 +0000128 char Disabled = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000129 char OutNeeds = 0;
130};
131
132struct BlockInfo {
133 char Needs = 0;
134 char InNeeds = 0;
135 char OutNeeds = 0;
136};
137
138struct WorkItem {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000139 MachineBasicBlock *MBB = nullptr;
140 MachineInstr *MI = nullptr;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000141
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000142 WorkItem() = default;
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000143 WorkItem(MachineBasicBlock *MBB) : MBB(MBB) {}
144 WorkItem(MachineInstr *MI) : MI(MI) {}
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000145};
146
147class SIWholeQuadMode : public MachineFunctionPass {
148private:
Connor Abbott92638ab2017-08-04 18:36:52 +0000149 CallingConv::ID CallingConv;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000150 const SIInstrInfo *TII;
151 const SIRegisterInfo *TRI;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000152 const GCNSubtarget *ST;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000153 MachineRegisterInfo *MRI;
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000154 LiveIntervals *LIS;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000155
156 DenseMap<const MachineInstr *, InstrInfo> Instructions;
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000157 DenseMap<MachineBasicBlock *, BlockInfo> Blocks;
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000158 SmallVector<MachineInstr *, 1> LiveMaskQueries;
Connor Abbott8c217d02017-08-04 18:36:49 +0000159 SmallVector<MachineInstr *, 4> LowerToCopyInstrs;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000160
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000161 void printInfo();
162
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000163 void markInstruction(MachineInstr &MI, char Flag,
164 std::vector<WorkItem> &Worklist);
Connor Abbottde068fe2017-08-04 18:36:50 +0000165 void markInstructionUses(const MachineInstr &MI, char Flag,
166 std::vector<WorkItem> &Worklist);
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000167 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 Haehnleb0c97482016-04-22 04:04:08 +0000170 char analyzeFunction(MachineFunction &MF);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000171
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000172 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 Haehnle213e87f2016-03-21 20:28:33 +0000180 void toExact(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
181 unsigned SaveWQM, unsigned LiveMaskReg);
182 void toWQM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
183 unsigned SavedWQM);
Connor Abbott92638ab2017-08-04 18:36:52 +0000184 void toWWM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
185 unsigned SaveOrig);
186 void fromWWM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
187 unsigned SavedOrig);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000188 void processBlock(MachineBasicBlock &MBB, unsigned LiveMaskReg, bool isEntry);
189
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000190 void lowerLiveMaskQueries(unsigned LiveMaskReg);
Connor Abbott8c217d02017-08-04 18:36:49 +0000191 void lowerCopyInstrs();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000192
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000193public:
194 static char ID;
195
196 SIWholeQuadMode() :
197 MachineFunctionPass(ID) { }
198
199 bool runOnMachineFunction(MachineFunction &MF) override;
200
Mehdi Amini117296c2016-10-01 02:56:57 +0000201 StringRef getPassName() const override { return "SI Whole Quad Mode"; }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000202
203 void getAnalysisUsage(AnalysisUsage &AU) const override {
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000204 AU.addRequired<LiveIntervals>();
Matt Arsenaultfa284552019-03-25 16:47:42 +0000205 AU.addPreserved<SlotIndexes>();
206 AU.addPreserved<LiveIntervals>();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000207 AU.setPreservesCFG();
208 MachineFunctionPass::getAnalysisUsage(AU);
209 }
210};
211
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000212} // end anonymous namespace
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000213
214char SIWholeQuadMode::ID = 0;
215
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000216INITIALIZE_PASS_BEGIN(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false,
217 false)
218INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
219INITIALIZE_PASS_END(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false,
220 false)
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000221
222char &llvm::SIWholeQuadModeID = SIWholeQuadMode::ID;
223
224FunctionPass *llvm::createSIWholeQuadModePass() {
225 return new SIWholeQuadMode;
226}
227
Eric Christopher3148a1b2017-11-16 03:18:15 +0000228#ifndef NDEBUG
Eric Christopher63481882017-11-16 03:25:02 +0000229LLVM_DUMP_METHOD void SIWholeQuadMode::printInfo() {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000230 for (const auto &BII : Blocks) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000231 dbgs() << "\n"
232 << printMBBReference(*BII.first) << ":\n"
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000233 << " 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 Christopher3148a1b2017-11-16 03:18:15 +0000247#endif
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000248
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000249void SIWholeQuadMode::markInstruction(MachineInstr &MI, char Flag,
250 std::vector<WorkItem> &Worklist) {
251 InstrInfo &II = Instructions[&MI];
252
Connor Abbott92638ab2017-08-04 18:36:52 +0000253 assert(!(Flag & StateExact) && Flag != 0);
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000254
Connor Abbottde068fe2017-08-04 18:36:50 +0000255 // 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 Haehnlebef0e902016-08-02 19:17:37 +0000264 return;
265
Connor Abbottde068fe2017-08-04 18:36:50 +0000266 II.Needs |= Flag;
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000267 Worklist.push_back(&MI);
268}
269
Connor Abbottde068fe2017-08-04 18:36:50 +0000270/// Mark all instructions defining the uses in \p MI with \p Flag.
271void SIWholeQuadMode::markInstructionUses(const MachineInstr &MI, char Flag,
272 std::vector<WorkItem> &Worklist) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000273 for (const MachineOperand &Use : MI.uses()) {
274 if (!Use.isReg() || !Use.isUse())
275 continue;
276
Daniel Sanders0c476112019-08-15 19:22:08 +0000277 Register Reg = Use.getReg();
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000278
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 Sanders2bea69b2019-08-01 23:27:28 +0000282 if (!Register::isVirtualRegister(Reg)) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000283 if (Reg == AMDGPU::EXEC || Reg == AMDGPU::EXEC_LO)
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000284 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 Abbottde068fe2017-08-04 18:36:50 +0000297 markInstruction(*LIS->getInstructionFromIndex(Value->def), Flag,
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000298 Worklist);
299 }
300
301 continue;
302 }
303
304 for (MachineInstr &DefMI : MRI->def_instructions(Use.getReg()))
Connor Abbottde068fe2017-08-04 18:36:50 +0000305 markInstruction(DefMI, Flag, Worklist);
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000306 }
307}
308
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000309// Scan instructions to determine which ones require an Exact execmask and
310// which ones seed WQM requirements.
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000311char SIWholeQuadMode::scanInstructions(MachineFunction &MF,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000312 std::vector<WorkItem> &Worklist) {
313 char GlobalFlags = 0;
Matthias Braunf1caa282017-12-15 22:22:58 +0000314 bool WQMOutputs = MF.getFunction().hasFnAttribute("amdgpu-ps-wqm-outputs");
Connor Abbott66b9bd62017-08-04 18:36:54 +0000315 SmallVector<MachineInstr *, 4> SetInactiveInstrs;
Carl Ritson00e89b42019-07-26 09:54:12 +0000316 SmallVector<MachineInstr *, 4> SoftWQMInstrs;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000317
Connor Abbottde068fe2017-08-04 18:36:50 +0000318 // 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 Haehnle213e87f2016-03-21 20:28:33 +0000326
327 for (auto II = MBB.begin(), IE = MBB.end(); II != IE; ++II) {
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000328 MachineInstr &MI = *II;
Connor Abbottde068fe2017-08-04 18:36:50 +0000329 InstrInfo &III = Instructions[&MI];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000330 unsigned Opcode = MI.getOpcode();
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000331 char Flags = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000332
Tim Renouf18a1e9d2018-05-07 13:21:26 +0000333 if (TII->isWQM(Opcode)) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000334 // 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 Abbottde068fe2017-08-04 18:36:50 +0000337 markInstructionUses(MI, StateWQM, Worklist);
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000338 GlobalFlags |= StateWQM;
339 continue;
Connor Abbott8c217d02017-08-04 18:36:49 +0000340 } 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 Ritson00e89b42019-07-26 09:54:12 +0000345 } else if (Opcode == AMDGPU::SOFT_WQM) {
346 LowerToCopyInstrs.push_back(&MI);
347 SoftWQMInstrs.push_back(&MI);
348 continue;
Connor Abbott92638ab2017-08-04 18:36:52 +0000349 } 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 Abbott66b9bd62017-08-04 18:36:54 +0000357 } 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 Sanders0c476112019-08-15 19:22:08 +0000365 Register Reg = Inactive.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000366 if (Register::isVirtualRegister(Reg)) {
Connor Abbott66b9bd62017-08-04 18:36:54 +0000367 for (MachineInstr &DefMI : MRI->def_instructions(Reg))
368 markInstruction(DefMI, StateWWM, Worklist);
369 }
370 }
371 }
372 SetInactiveInstrs.push_back(&MI);
373 continue;
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000374 } else if (TII->isDisableWQM(MI)) {
Connor Abbottde068fe2017-08-04 18:36:50 +0000375 BBI.Needs |= StateExact;
376 if (!(BBI.InNeeds & StateExact)) {
377 BBI.InNeeds |= StateExact;
378 Worklist.push_back(&MBB);
379 }
380 GlobalFlags |= StateExact;
Connor Abbott92638ab2017-08-04 18:36:52 +0000381 III.Disabled = StateWQM | StateWWM;
Connor Abbottde068fe2017-08-04 18:36:50 +0000382 continue;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000383 } else {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000384 if (Opcode == AMDGPU::SI_PS_LIVE) {
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000385 LiveMaskQueries.push_back(&MI);
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000386 } 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 Sanders0c476112019-08-15 19:22:08 +0000394 Register Reg = MO.getReg();
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000395
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000396 if (!Register::isVirtualRegister(Reg) &&
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000397 TRI->hasVectorRegisters(TRI->getPhysRegClass(Reg))) {
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000398 Flags = StateWQM;
399 break;
400 }
401 }
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000402 }
403
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000404 if (!Flags)
405 continue;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000406 }
407
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000408 markInstruction(MI, Flags, Worklist);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000409 GlobalFlags |= Flags;
410 }
411 }
412
Connor Abbott66b9bd62017-08-04 18:36:54 +0000413 // 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 Ritson00e89b42019-07-26 09:54:12 +0000416 // Similarly for SOFT_WQM instructions, implementing @llvm.amdgcn.softwqm.
Connor Abbott66b9bd62017-08-04 18:36:54 +0000417 if (GlobalFlags & StateWQM) {
418 for (MachineInstr *MI : SetInactiveInstrs)
419 markInstruction(*MI, StateWQM, Worklist);
Carl Ritson00e89b42019-07-26 09:54:12 +0000420 for (MachineInstr *MI : SoftWQMInstrs)
421 markInstruction(*MI, StateWQM, Worklist);
Connor Abbott66b9bd62017-08-04 18:36:54 +0000422 }
423
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000424 return GlobalFlags;
425}
426
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000427void SIWholeQuadMode::propagateInstruction(MachineInstr &MI,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000428 std::vector<WorkItem>& Worklist) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000429 MachineBasicBlock *MBB = MI.getParent();
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000430 InstrInfo II = Instructions[&MI]; // take a copy to prevent dangling references
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000431 BlockInfo &BI = Blocks[MBB];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000432
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000433 // Control flow-type instructions and stores to temporary memory that are
434 // followed by WQM computations must themselves be in WQM.
Connor Abbottde068fe2017-08-04 18:36:50 +0000435 if ((II.OutNeeds & StateWQM) && !(II.Disabled & StateWQM) &&
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000436 (MI.isTerminator() || (TII->usesVM_CNT(MI) && MI.mayStore()))) {
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000437 Instructions[&MI].Needs = StateWQM;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000438 II.Needs = StateWQM;
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000439 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000440
441 // Propagate to block level
Connor Abbottde068fe2017-08-04 18:36:50 +0000442 if (II.Needs & StateWQM) {
443 BI.Needs |= StateWQM;
444 if (!(BI.InNeeds & StateWQM)) {
445 BI.InNeeds |= StateWQM;
446 Worklist.push_back(MBB);
447 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000448 }
449
450 // Propagate backwards within block
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000451 if (MachineInstr *PrevMI = MI.getPrevNode()) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000452 char InNeeds = (II.Needs & ~StateWWM) | II.OutNeeds;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000453 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 Abbottde068fe2017-08-04 18:36:50 +0000463 assert(!(II.Needs & StateExact));
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000464
Connor Abbottde068fe2017-08-04 18:36:50 +0000465 if (II.Needs != 0)
466 markInstructionUses(MI, II.Needs, Worklist);
Tim Renouf364edcd2018-05-27 17:26:11 +0000467
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 Haehnle213e87f2016-03-21 20:28:33 +0000472}
473
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000474void SIWholeQuadMode::propagateBlock(MachineBasicBlock &MBB,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000475 std::vector<WorkItem>& Worklist) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000476 BlockInfo BI = Blocks[&MBB]; // Make a copy to prevent dangling references.
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000477
478 // Propagate through instructions
479 if (!MBB.empty()) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000480 MachineInstr *LastMI = &*MBB.rbegin();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000481 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 Arsenault8dff86d2016-07-13 05:55:15 +0000489 for (MachineBasicBlock *Pred : MBB.predecessors()) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000490 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 Arsenault8dff86d2016-07-13 05:55:15 +0000499 // All successors must be prepared to accept the same set of WQM/Exact data.
500 for (MachineBasicBlock *Succ : MBB.successors()) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000501 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 Haehnleb0c97482016-04-22 04:04:08 +0000510char SIWholeQuadMode::analyzeFunction(MachineFunction &MF) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000511 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 Haehnlee58e0e32016-09-12 16:25:20 +0000527/// 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.
532bool 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
557MachineBasicBlock::iterator
558SIWholeQuadMode::saveSCC(MachineBasicBlock &MBB,
559 MachineBasicBlock::iterator Before) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000560 Register SaveReg = MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000561
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.
579MachineBasicBlock::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 Haehnle213e87f2016-03-21 20:28:33 +0000627void SIWholeQuadMode::toExact(MachineBasicBlock &MBB,
628 MachineBasicBlock::iterator Before,
Nicolai Haehnlea56e6b62016-03-21 20:39:24 +0000629 unsigned SaveWQM, unsigned LiveMaskReg) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000630 MachineInstr *MI;
631
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000632 if (SaveWQM) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000633 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(ST->isWave32() ?
634 AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64),
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000635 SaveWQM)
636 .addReg(LiveMaskReg);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000637 } else {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000638 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 Haehnlee58e0e32016-09-12 16:25:20 +0000643 .addReg(LiveMaskReg);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000644 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000645
646 LIS->InsertMachineInstrInMaps(*MI);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000647}
648
649void SIWholeQuadMode::toWQM(MachineBasicBlock &MBB,
650 MachineBasicBlock::iterator Before,
Nicolai Haehnlea56e6b62016-03-21 20:39:24 +0000651 unsigned SavedWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000652 MachineInstr *MI;
653
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000654 unsigned Exec = ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000655 if (SavedWQM) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000656 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), Exec)
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000657 .addReg(SavedWQM);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000658 } else {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000659 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(ST->isWave32() ?
660 AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64),
661 Exec)
662 .addReg(Exec);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000663 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000664
665 LIS->InsertMachineInstrInMaps(*MI);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000666}
667
Connor Abbott92638ab2017-08-04 18:36:52 +0000668void SIWholeQuadMode::toWWM(MachineBasicBlock &MBB,
669 MachineBasicBlock::iterator Before,
670 unsigned SaveOrig) {
671 MachineInstr *MI;
672
673 assert(SaveOrig);
Neil Henning0a30f332019-04-01 15:19:52 +0000674 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::ENTER_WWM), SaveOrig)
Connor Abbott92638ab2017-08-04 18:36:52 +0000675 .addImm(-1);
676 LIS->InsertMachineInstrInMaps(*MI);
677}
678
679void SIWholeQuadMode::fromWWM(MachineBasicBlock &MBB,
680 MachineBasicBlock::iterator Before,
681 unsigned SavedOrig) {
682 MachineInstr *MI;
683
684 assert(SavedOrig);
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000685 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::EXIT_WWM),
686 ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC)
Connor Abbott92638ab2017-08-04 18:36:52 +0000687 .addReg(SavedOrig);
688 LIS->InsertMachineInstrInMaps(*MI);
689}
690
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000691void 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 Haehnle213e87f2016-03-21 20:28:33 +0000699 // This is a non-entry block that is WQM throughout, so no need to do
700 // anything.
Connor Abbott92638ab2017-08-04 18:36:52 +0000701 if (!isEntry && BI.Needs == StateWQM && BI.OutNeeds != StateExact)
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000702 return;
703
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000704 LLVM_DEBUG(dbgs() << "\nProcessing block " << printMBBReference(MBB)
705 << ":\n");
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000706
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000707 unsigned SavedWQMReg = 0;
Connor Abbott92638ab2017-08-04 18:36:52 +0000708 unsigned SavedNonWWMReg = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000709 bool WQMFromExec = isEntry;
Connor Abbott92638ab2017-08-04 18:36:52 +0000710 char State = (isEntry || !(BI.InNeeds & StateWQM)) ? StateExact : StateWQM;
711 char NonWWMState = 0;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000712 const TargetRegisterClass *BoolRC = TRI->getBoolRC();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000713
714 auto II = MBB.getFirstNonPHI(), IE = MBB.end();
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000715 if (isEntry)
716 ++II; // Skip the instruction that saves LiveMask
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000717
Connor Abbott92638ab2017-08-04 18:36:52 +0000718 // 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 Haehnlee58e0e32016-09-12 16:25:20 +0000727 for (;;) {
728 MachineBasicBlock::iterator Next = II;
Connor Abbott92638ab2017-08-04 18:36:52 +0000729 char Needs = StateExact | StateWQM; // WWM is disabled by default
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000730 char OutNeeds = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000731
Connor Abbott92638ab2017-08-04 18:36:52 +0000732 if (FirstWQM == IE)
733 FirstWQM = II;
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000734
Connor Abbott92638ab2017-08-04 18:36:52 +0000735 if (FirstWWM == IE)
736 FirstWWM = II;
737
738 // First, figure out the allowed states (Needs) based on the propagated
739 // flags.
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000740 if (II != IE) {
741 MachineInstr &MI = *II;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000742
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000743 if (requiresCorrectState(MI)) {
744 auto III = Instructions.find(&MI);
745 if (III != Instructions.end()) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000746 if (III->second.Needs & StateWWM)
747 Needs = StateWWM;
748 else if (III->second.Needs & StateWQM)
Connor Abbottde068fe2017-08-04 18:36:50 +0000749 Needs = StateWQM;
750 else
751 Needs &= ~III->second.Disabled;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000752 OutNeeds = III->second.OutNeeds;
753 }
Connor Abbott92638ab2017-08-04 18:36:52 +0000754 } 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 Haehnle213e87f2016-03-21 20:28:33 +0000758 }
759
Connor Abbottde068fe2017-08-04 18:36:50 +0000760 if (MI.isTerminator() && OutNeeds == StateExact)
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000761 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 Abbottde068fe2017-08-04 18:36:50 +0000773 else
774 Needs = StateWQM | StateExact;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000775 }
Nicolai Haehnle3b572002016-07-28 11:39:24 +0000776
Connor Abbott92638ab2017-08-04 18:36:52 +0000777 // Now, transition if necessary.
Connor Abbottde068fe2017-08-04 18:36:50 +0000778 if (!(Needs & State)) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000779 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 Abbottde068fe2017-08-04 18:36:50 +0000788 MachineBasicBlock::iterator Before =
789 prepareInsertion(MBB, First, II, Needs == StateWQM,
790 Needs == StateExact || WQMFromExec);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000791
Connor Abbott92638ab2017-08-04 18:36:52 +0000792 if (State == StateWWM) {
793 assert(SavedNonWWMReg);
794 fromWWM(MBB, Before, SavedNonWWMReg);
795 State = NonWWMState;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000796 }
797
Connor Abbott92638ab2017-08-04 18:36:52 +0000798 if (Needs == StateWWM) {
799 NonWWMState = State;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000800 SavedNonWWMReg = MRI->createVirtualRegister(BoolRC);
Connor Abbott92638ab2017-08-04 18:36:52 +0000801 toWWM(MBB, Before, SavedNonWWMReg);
802 State = StateWWM;
803 } else {
804 if (State == StateWQM && (Needs & StateExact) && !(Needs & StateWQM)) {
805 if (!WQMFromExec && (OutNeeds & StateWQM))
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000806 SavedWQMReg = MRI->createVirtualRegister(BoolRC);
Connor Abbott92638ab2017-08-04 18:36:52 +0000807
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 Haehnlee58e0e32016-09-12 16:25:20 +0000827 }
828
Connor Abbott92638ab2017-08-04 18:36:52 +0000829 if (Needs != (StateExact | StateWQM | StateWWM)) {
830 if (Needs != (StateExact | StateWQM))
831 FirstWQM = IE;
832 FirstWWM = IE;
833 }
Connor Abbottde068fe2017-08-04 18:36:50 +0000834
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000835 if (II == IE)
836 break;
837 II = Next;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000838 }
839}
840
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000841void SIWholeQuadMode::lowerLiveMaskQueries(unsigned LiveMaskReg) {
842 for (MachineInstr *MI : LiveMaskQueries) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000843 const DebugLoc &DL = MI->getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +0000844 Register Dest = MI->getOperand(0).getReg();
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000845 MachineInstr *Copy =
846 BuildMI(*MI->getParent(), MI, DL, TII->get(AMDGPU::COPY), Dest)
847 .addReg(LiveMaskReg);
848
849 LIS->ReplaceMachineInstrInMaps(*MI, *Copy);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000850 MI->eraseFromParent();
851 }
852}
853
Connor Abbott8c217d02017-08-04 18:36:49 +0000854void SIWholeQuadMode::lowerCopyInstrs() {
Connor Abbott66b9bd62017-08-04 18:36:54 +0000855 for (MachineInstr *MI : LowerToCopyInstrs) {
856 for (unsigned i = MI->getNumExplicitOperands() - 1; i > 1; i--)
857 MI->RemoveOperand(i);
Neil Henning0a30f332019-04-01 15:19:52 +0000858
Daniel Sanders0c476112019-08-15 19:22:08 +0000859 const Register Reg = MI->getOperand(0).getReg();
Neil Henning0a30f332019-04-01 15:19:52 +0000860
861 if (TRI->isVGPR(*MRI, Reg)) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000862 const TargetRegisterClass *regClass = Register::isVirtualRegister(Reg)
863 ? MRI->getRegClass(Reg)
864 : TRI->getPhysRegClass(Reg);
Neil Henning0a30f332019-04-01 15:19:52 +0000865
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 Abbott66b9bd62017-08-04 18:36:54 +0000874 }
Connor Abbott8c217d02017-08-04 18:36:49 +0000875}
876
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000877bool SIWholeQuadMode::runOnMachineFunction(MachineFunction &MF) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000878 Instructions.clear();
879 Blocks.clear();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000880 LiveMaskQueries.clear();
Connor Abbott8c217d02017-08-04 18:36:49 +0000881 LowerToCopyInstrs.clear();
Matthias Braunf1caa282017-12-15 22:22:58 +0000882 CallingConv = MF.getFunction().getCallingConv();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000883
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000884 ST = &MF.getSubtarget<GCNSubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000885
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000886 TII = ST->getInstrInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000887 TRI = &TII->getRegisterInfo();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000888 MRI = &MF.getRegInfo();
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000889 LIS = &getAnalysis<LiveIntervals>();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000890
891 char GlobalFlags = analyzeFunction(MF);
Connor Abbott92638ab2017-08-04 18:36:52 +0000892 unsigned LiveMaskReg = 0;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000893 unsigned Exec = ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000894 if (!(GlobalFlags & StateWQM)) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000895 lowerLiveMaskQueries(Exec);
Carl Ritson00e89b42019-07-26 09:54:12 +0000896 if (!(GlobalFlags & StateWWM) && LowerToCopyInstrs.empty())
Connor Abbott92638ab2017-08-04 18:36:52 +0000897 return !LiveMaskQueries.empty();
898 } else {
899 // Store a copy of the original live mask when required
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000900 MachineBasicBlock &Entry = MF.front();
901 MachineBasicBlock::iterator EntryMI = Entry.getFirstNonPHI();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000902
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000903 if (GlobalFlags & StateExact || !LiveMaskQueries.empty()) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000904 LiveMaskReg = MRI->createVirtualRegister(TRI->getBoolRC());
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000905 MachineInstr *MI = BuildMI(Entry, EntryMI, DebugLoc(),
906 TII->get(AMDGPU::COPY), LiveMaskReg)
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000907 .addReg(Exec);
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000908 LIS->InsertMachineInstrInMaps(*MI);
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000909 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000910
Connor Abbott92638ab2017-08-04 18:36:52 +0000911 lowerLiveMaskQueries(LiveMaskReg);
912
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000913 if (GlobalFlags == StateWQM) {
914 // For a shader that needs only WQM, we can just set it once.
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +0000915 BuildMI(Entry, EntryMI, DebugLoc(), TII->get(ST->isWave32() ?
916 AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64),
917 Exec)
918 .addReg(Exec);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000919
Connor Abbott8c217d02017-08-04 18:36:49 +0000920 lowerCopyInstrs();
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000921 // EntryMI may become invalid here
922 return true;
923 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000924 }
925
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000926 LLVM_DEBUG(printInfo());
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000927
Connor Abbott8c217d02017-08-04 18:36:49 +0000928 lowerCopyInstrs();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000929
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000930 // Handle the general case
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000931 for (auto BII : Blocks)
932 processBlock(*BII.first, LiveMaskReg, BII.first == &*MF.begin());
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000933
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000934 // 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 Haehnle213e87f2016-03-21 20:28:33 +0000939 return true;
940}