blob: b7d96f0842fbd7318e15a970e02f6fd4e8c58420 [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"
60#include "SIInstrInfo.h"
61#include "SIMachineFunctionInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000062#include "MCTargetDesc/AMDGPUMCTargetDesc.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"
80#include "llvm/MC/MCRegisterInfo.h"
81#include "llvm/Pass.h"
82#include "llvm/Support/Debug.h"
83#include "llvm/Support/raw_ostream.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000084#include <cassert>
85#include <vector>
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000086
87using namespace llvm;
88
89#define DEBUG_TYPE "si-wqm"
90
91namespace {
92
93enum {
94 StateWQM = 0x1,
Connor Abbott92638ab2017-08-04 18:36:52 +000095 StateWWM = 0x2,
96 StateExact = 0x4,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000097};
98
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +000099struct PrintState {
100public:
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000101 int State;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000102
103 explicit PrintState(int State) : State(State) {}
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000104};
105
Eric Christopher3148a1b2017-11-16 03:18:15 +0000106#ifndef NDEBUG
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000107static raw_ostream &operator<<(raw_ostream &OS, const PrintState &PS) {
108 if (PS.State & StateWQM)
109 OS << "WQM";
Connor Abbott92638ab2017-08-04 18:36:52 +0000110 if (PS.State & StateWWM) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000111 if (PS.State & StateWQM)
112 OS << '|';
Connor Abbott92638ab2017-08-04 18:36:52 +0000113 OS << "WWM";
114 }
115 if (PS.State & StateExact) {
116 if (PS.State & (StateWQM | StateWWM))
117 OS << '|';
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000118 OS << "Exact";
119 }
120
121 return OS;
122}
Eric Christopher3148a1b2017-11-16 03:18:15 +0000123#endif
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000124
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000125struct InstrInfo {
126 char Needs = 0;
Connor Abbottde068fe2017-08-04 18:36:50 +0000127 char Disabled = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000128 char OutNeeds = 0;
129};
130
131struct BlockInfo {
132 char Needs = 0;
133 char InNeeds = 0;
134 char OutNeeds = 0;
135};
136
137struct WorkItem {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000138 MachineBasicBlock *MBB = nullptr;
139 MachineInstr *MI = nullptr;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000140
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000141 WorkItem() = default;
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000142 WorkItem(MachineBasicBlock *MBB) : MBB(MBB) {}
143 WorkItem(MachineInstr *MI) : MI(MI) {}
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000144};
145
146class SIWholeQuadMode : public MachineFunctionPass {
147private:
Connor Abbott92638ab2017-08-04 18:36:52 +0000148 CallingConv::ID CallingConv;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000149 const SIInstrInfo *TII;
150 const SIRegisterInfo *TRI;
151 MachineRegisterInfo *MRI;
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000152 LiveIntervals *LIS;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000153
154 DenseMap<const MachineInstr *, InstrInfo> Instructions;
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000155 DenseMap<MachineBasicBlock *, BlockInfo> Blocks;
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000156 SmallVector<MachineInstr *, 1> LiveMaskQueries;
Connor Abbott8c217d02017-08-04 18:36:49 +0000157 SmallVector<MachineInstr *, 4> LowerToCopyInstrs;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000158
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000159 void printInfo();
160
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000161 void markInstruction(MachineInstr &MI, char Flag,
162 std::vector<WorkItem> &Worklist);
Connor Abbottde068fe2017-08-04 18:36:50 +0000163 void markInstructionUses(const MachineInstr &MI, char Flag,
164 std::vector<WorkItem> &Worklist);
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000165 char scanInstructions(MachineFunction &MF, std::vector<WorkItem> &Worklist);
166 void propagateInstruction(MachineInstr &MI, std::vector<WorkItem> &Worklist);
167 void propagateBlock(MachineBasicBlock &MBB, std::vector<WorkItem> &Worklist);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000168 char analyzeFunction(MachineFunction &MF);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000169
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000170 bool requiresCorrectState(const MachineInstr &MI) const;
171
172 MachineBasicBlock::iterator saveSCC(MachineBasicBlock &MBB,
173 MachineBasicBlock::iterator Before);
174 MachineBasicBlock::iterator
175 prepareInsertion(MachineBasicBlock &MBB, MachineBasicBlock::iterator First,
176 MachineBasicBlock::iterator Last, bool PreferLast,
177 bool SaveSCC);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000178 void toExact(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
179 unsigned SaveWQM, unsigned LiveMaskReg);
180 void toWQM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
181 unsigned SavedWQM);
Connor Abbott92638ab2017-08-04 18:36:52 +0000182 void toWWM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
183 unsigned SaveOrig);
184 void fromWWM(MachineBasicBlock &MBB, MachineBasicBlock::iterator Before,
185 unsigned SavedOrig);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000186 void processBlock(MachineBasicBlock &MBB, unsigned LiveMaskReg, bool isEntry);
187
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000188 void lowerLiveMaskQueries(unsigned LiveMaskReg);
Connor Abbott8c217d02017-08-04 18:36:49 +0000189 void lowerCopyInstrs();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000190
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000191public:
192 static char ID;
193
194 SIWholeQuadMode() :
195 MachineFunctionPass(ID) { }
196
197 bool runOnMachineFunction(MachineFunction &MF) override;
198
Mehdi Amini117296c2016-10-01 02:56:57 +0000199 StringRef getPassName() const override { return "SI Whole Quad Mode"; }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000200
201 void getAnalysisUsage(AnalysisUsage &AU) const override {
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000202 AU.addRequired<LiveIntervals>();
Matt Arsenaultfa284552019-03-25 16:47:42 +0000203 AU.addPreserved<SlotIndexes>();
204 AU.addPreserved<LiveIntervals>();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000205 AU.setPreservesCFG();
206 MachineFunctionPass::getAnalysisUsage(AU);
207 }
208};
209
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000210} // end anonymous namespace
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000211
212char SIWholeQuadMode::ID = 0;
213
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000214INITIALIZE_PASS_BEGIN(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false,
215 false)
216INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
217INITIALIZE_PASS_END(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false,
218 false)
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000219
220char &llvm::SIWholeQuadModeID = SIWholeQuadMode::ID;
221
222FunctionPass *llvm::createSIWholeQuadModePass() {
223 return new SIWholeQuadMode;
224}
225
Eric Christopher3148a1b2017-11-16 03:18:15 +0000226#ifndef NDEBUG
Eric Christopher63481882017-11-16 03:25:02 +0000227LLVM_DUMP_METHOD void SIWholeQuadMode::printInfo() {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000228 for (const auto &BII : Blocks) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000229 dbgs() << "\n"
230 << printMBBReference(*BII.first) << ":\n"
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000231 << " InNeeds = " << PrintState(BII.second.InNeeds)
232 << ", Needs = " << PrintState(BII.second.Needs)
233 << ", OutNeeds = " << PrintState(BII.second.OutNeeds) << "\n\n";
234
235 for (const MachineInstr &MI : *BII.first) {
236 auto III = Instructions.find(&MI);
237 if (III == Instructions.end())
238 continue;
239
240 dbgs() << " " << MI << " Needs = " << PrintState(III->second.Needs)
241 << ", OutNeeds = " << PrintState(III->second.OutNeeds) << '\n';
242 }
243 }
244}
Eric Christopher3148a1b2017-11-16 03:18:15 +0000245#endif
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000246
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000247void SIWholeQuadMode::markInstruction(MachineInstr &MI, char Flag,
248 std::vector<WorkItem> &Worklist) {
249 InstrInfo &II = Instructions[&MI];
250
Connor Abbott92638ab2017-08-04 18:36:52 +0000251 assert(!(Flag & StateExact) && Flag != 0);
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000252
Connor Abbottde068fe2017-08-04 18:36:50 +0000253 // Remove any disabled states from the flag. The user that required it gets
254 // an undefined value in the helper lanes. For example, this can happen if
255 // the result of an atomic is used by instruction that requires WQM, where
256 // ignoring the request for WQM is correct as per the relevant specs.
257 Flag &= ~II.Disabled;
258
259 // Ignore if the flag is already encompassed by the existing needs, or we
260 // just disabled everything.
261 if ((II.Needs & Flag) == Flag)
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000262 return;
263
Connor Abbottde068fe2017-08-04 18:36:50 +0000264 II.Needs |= Flag;
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000265 Worklist.push_back(&MI);
266}
267
Connor Abbottde068fe2017-08-04 18:36:50 +0000268/// Mark all instructions defining the uses in \p MI with \p Flag.
269void SIWholeQuadMode::markInstructionUses(const MachineInstr &MI, char Flag,
270 std::vector<WorkItem> &Worklist) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000271 for (const MachineOperand &Use : MI.uses()) {
272 if (!Use.isReg() || !Use.isUse())
273 continue;
274
275 unsigned Reg = Use.getReg();
276
277 // Handle physical registers that we need to track; this is mostly relevant
278 // for VCC, which can appear as the (implicit) input of a uniform branch,
279 // e.g. when a loop counter is stored in a VGPR.
280 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
281 if (Reg == AMDGPU::EXEC)
282 continue;
283
284 for (MCRegUnitIterator RegUnit(Reg, TRI); RegUnit.isValid(); ++RegUnit) {
285 LiveRange &LR = LIS->getRegUnit(*RegUnit);
286 const VNInfo *Value = LR.Query(LIS->getInstructionIndex(MI)).valueIn();
287 if (!Value)
288 continue;
289
290 // Since we're in machine SSA, we do not need to track physical
291 // registers across basic blocks.
292 if (Value->isPHIDef())
293 continue;
294
Connor Abbottde068fe2017-08-04 18:36:50 +0000295 markInstruction(*LIS->getInstructionFromIndex(Value->def), Flag,
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000296 Worklist);
297 }
298
299 continue;
300 }
301
302 for (MachineInstr &DefMI : MRI->def_instructions(Use.getReg()))
Connor Abbottde068fe2017-08-04 18:36:50 +0000303 markInstruction(DefMI, Flag, Worklist);
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000304 }
305}
306
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000307// Scan instructions to determine which ones require an Exact execmask and
308// which ones seed WQM requirements.
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000309char SIWholeQuadMode::scanInstructions(MachineFunction &MF,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000310 std::vector<WorkItem> &Worklist) {
311 char GlobalFlags = 0;
Matthias Braunf1caa282017-12-15 22:22:58 +0000312 bool WQMOutputs = MF.getFunction().hasFnAttribute("amdgpu-ps-wqm-outputs");
Connor Abbott66b9bd62017-08-04 18:36:54 +0000313 SmallVector<MachineInstr *, 4> SetInactiveInstrs;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000314
Connor Abbottde068fe2017-08-04 18:36:50 +0000315 // We need to visit the basic blocks in reverse post-order so that we visit
316 // defs before uses, in particular so that we don't accidentally mark an
317 // instruction as needing e.g. WQM before visiting it and realizing it needs
318 // WQM disabled.
319 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
320 for (auto BI = RPOT.begin(), BE = RPOT.end(); BI != BE; ++BI) {
321 MachineBasicBlock &MBB = **BI;
322 BlockInfo &BBI = Blocks[&MBB];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000323
324 for (auto II = MBB.begin(), IE = MBB.end(); II != IE; ++II) {
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000325 MachineInstr &MI = *II;
Connor Abbottde068fe2017-08-04 18:36:50 +0000326 InstrInfo &III = Instructions[&MI];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000327 unsigned Opcode = MI.getOpcode();
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000328 char Flags = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000329
Tim Renouf18a1e9d2018-05-07 13:21:26 +0000330 if (TII->isWQM(Opcode)) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000331 // Sampling instructions don't need to produce results for all pixels
332 // in a quad, they just require all inputs of a quad to have been
333 // computed for derivatives.
Connor Abbottde068fe2017-08-04 18:36:50 +0000334 markInstructionUses(MI, StateWQM, Worklist);
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000335 GlobalFlags |= StateWQM;
336 continue;
Connor Abbott8c217d02017-08-04 18:36:49 +0000337 } else if (Opcode == AMDGPU::WQM) {
338 // The WQM intrinsic requires its output to have all the helper lanes
339 // correct, so we need it to be in WQM.
340 Flags = StateWQM;
341 LowerToCopyInstrs.push_back(&MI);
Connor Abbott92638ab2017-08-04 18:36:52 +0000342 } else if (Opcode == AMDGPU::WWM) {
343 // The WWM intrinsic doesn't make the same guarantee, and plus it needs
344 // to be executed in WQM or Exact so that its copy doesn't clobber
345 // inactive lanes.
346 markInstructionUses(MI, StateWWM, Worklist);
347 GlobalFlags |= StateWWM;
348 LowerToCopyInstrs.push_back(&MI);
349 continue;
Connor Abbott66b9bd62017-08-04 18:36:54 +0000350 } else if (Opcode == AMDGPU::V_SET_INACTIVE_B32 ||
351 Opcode == AMDGPU::V_SET_INACTIVE_B64) {
352 III.Disabled = StateWWM;
353 MachineOperand &Inactive = MI.getOperand(2);
354 if (Inactive.isReg()) {
355 if (Inactive.isUndef()) {
356 LowerToCopyInstrs.push_back(&MI);
357 } else {
358 unsigned Reg = Inactive.getReg();
359 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
360 for (MachineInstr &DefMI : MRI->def_instructions(Reg))
361 markInstruction(DefMI, StateWWM, Worklist);
362 }
363 }
364 }
365 SetInactiveInstrs.push_back(&MI);
366 continue;
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000367 } else if (TII->isDisableWQM(MI)) {
Connor Abbottde068fe2017-08-04 18:36:50 +0000368 BBI.Needs |= StateExact;
369 if (!(BBI.InNeeds & StateExact)) {
370 BBI.InNeeds |= StateExact;
371 Worklist.push_back(&MBB);
372 }
373 GlobalFlags |= StateExact;
Connor Abbott92638ab2017-08-04 18:36:52 +0000374 III.Disabled = StateWQM | StateWWM;
Connor Abbottde068fe2017-08-04 18:36:50 +0000375 continue;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000376 } else {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000377 if (Opcode == AMDGPU::SI_PS_LIVE) {
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000378 LiveMaskQueries.push_back(&MI);
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000379 } else if (WQMOutputs) {
380 // The function is in machine SSA form, which means that physical
381 // VGPRs correspond to shader inputs and outputs. Inputs are
382 // only used, outputs are only defined.
383 for (const MachineOperand &MO : MI.defs()) {
384 if (!MO.isReg())
385 continue;
386
387 unsigned Reg = MO.getReg();
388
389 if (!TRI->isVirtualRegister(Reg) &&
390 TRI->hasVGPRs(TRI->getPhysRegClass(Reg))) {
391 Flags = StateWQM;
392 break;
393 }
394 }
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000395 }
396
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000397 if (!Flags)
398 continue;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000399 }
400
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000401 markInstruction(MI, Flags, Worklist);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000402 GlobalFlags |= Flags;
403 }
404 }
405
Connor Abbott66b9bd62017-08-04 18:36:54 +0000406 // Mark sure that any SET_INACTIVE instructions are computed in WQM if WQM is
407 // ever used anywhere in the function. This implements the corresponding
408 // semantics of @llvm.amdgcn.set.inactive.
409 if (GlobalFlags & StateWQM) {
410 for (MachineInstr *MI : SetInactiveInstrs)
411 markInstruction(*MI, StateWQM, Worklist);
412 }
413
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000414 return GlobalFlags;
415}
416
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000417void SIWholeQuadMode::propagateInstruction(MachineInstr &MI,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000418 std::vector<WorkItem>& Worklist) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000419 MachineBasicBlock *MBB = MI.getParent();
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000420 InstrInfo II = Instructions[&MI]; // take a copy to prevent dangling references
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000421 BlockInfo &BI = Blocks[MBB];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000422
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000423 // Control flow-type instructions and stores to temporary memory that are
424 // followed by WQM computations must themselves be in WQM.
Connor Abbottde068fe2017-08-04 18:36:50 +0000425 if ((II.OutNeeds & StateWQM) && !(II.Disabled & StateWQM) &&
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000426 (MI.isTerminator() || (TII->usesVM_CNT(MI) && MI.mayStore()))) {
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000427 Instructions[&MI].Needs = StateWQM;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000428 II.Needs = StateWQM;
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000429 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000430
431 // Propagate to block level
Connor Abbottde068fe2017-08-04 18:36:50 +0000432 if (II.Needs & StateWQM) {
433 BI.Needs |= StateWQM;
434 if (!(BI.InNeeds & StateWQM)) {
435 BI.InNeeds |= StateWQM;
436 Worklist.push_back(MBB);
437 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000438 }
439
440 // Propagate backwards within block
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000441 if (MachineInstr *PrevMI = MI.getPrevNode()) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000442 char InNeeds = (II.Needs & ~StateWWM) | II.OutNeeds;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000443 if (!PrevMI->isPHI()) {
444 InstrInfo &PrevII = Instructions[PrevMI];
445 if ((PrevII.OutNeeds | InNeeds) != PrevII.OutNeeds) {
446 PrevII.OutNeeds |= InNeeds;
447 Worklist.push_back(PrevMI);
448 }
449 }
450 }
451
452 // Propagate WQM flag to instruction inputs
Connor Abbottde068fe2017-08-04 18:36:50 +0000453 assert(!(II.Needs & StateExact));
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000454
Connor Abbottde068fe2017-08-04 18:36:50 +0000455 if (II.Needs != 0)
456 markInstructionUses(MI, II.Needs, Worklist);
Tim Renouf364edcd2018-05-27 17:26:11 +0000457
458 // Ensure we process a block containing WWM, even if it does not require any
459 // WQM transitions.
460 if (II.Needs & StateWWM)
461 BI.Needs |= StateWWM;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000462}
463
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000464void SIWholeQuadMode::propagateBlock(MachineBasicBlock &MBB,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000465 std::vector<WorkItem>& Worklist) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000466 BlockInfo BI = Blocks[&MBB]; // Make a copy to prevent dangling references.
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000467
468 // Propagate through instructions
469 if (!MBB.empty()) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000470 MachineInstr *LastMI = &*MBB.rbegin();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000471 InstrInfo &LastII = Instructions[LastMI];
472 if ((LastII.OutNeeds | BI.OutNeeds) != LastII.OutNeeds) {
473 LastII.OutNeeds |= BI.OutNeeds;
474 Worklist.push_back(LastMI);
475 }
476 }
477
478 // Predecessor blocks must provide for our WQM/Exact needs.
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000479 for (MachineBasicBlock *Pred : MBB.predecessors()) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000480 BlockInfo &PredBI = Blocks[Pred];
481 if ((PredBI.OutNeeds | BI.InNeeds) == PredBI.OutNeeds)
482 continue;
483
484 PredBI.OutNeeds |= BI.InNeeds;
485 PredBI.InNeeds |= BI.InNeeds;
486 Worklist.push_back(Pred);
487 }
488
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000489 // All successors must be prepared to accept the same set of WQM/Exact data.
490 for (MachineBasicBlock *Succ : MBB.successors()) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000491 BlockInfo &SuccBI = Blocks[Succ];
492 if ((SuccBI.InNeeds | BI.OutNeeds) == SuccBI.InNeeds)
493 continue;
494
495 SuccBI.InNeeds |= BI.OutNeeds;
496 Worklist.push_back(Succ);
497 }
498}
499
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000500char SIWholeQuadMode::analyzeFunction(MachineFunction &MF) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000501 std::vector<WorkItem> Worklist;
502 char GlobalFlags = scanInstructions(MF, Worklist);
503
504 while (!Worklist.empty()) {
505 WorkItem WI = Worklist.back();
506 Worklist.pop_back();
507
508 if (WI.MI)
509 propagateInstruction(*WI.MI, Worklist);
510 else
511 propagateBlock(*WI.MBB, Worklist);
512 }
513
514 return GlobalFlags;
515}
516
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000517/// Whether \p MI really requires the exec state computed during analysis.
518///
519/// Scalar instructions must occasionally be marked WQM for correct propagation
520/// (e.g. thread masks leading up to branches), but when it comes to actual
521/// execution, they don't care about EXEC.
522bool SIWholeQuadMode::requiresCorrectState(const MachineInstr &MI) const {
523 if (MI.isTerminator())
524 return true;
525
526 // Skip instructions that are not affected by EXEC
527 if (TII->isScalarUnit(MI))
528 return false;
529
530 // Generic instructions such as COPY will either disappear by register
531 // coalescing or be lowered to SALU or VALU instructions.
532 if (MI.isTransient()) {
533 if (MI.getNumExplicitOperands() >= 1) {
534 const MachineOperand &Op = MI.getOperand(0);
535 if (Op.isReg()) {
536 if (TRI->isSGPRReg(*MRI, Op.getReg())) {
537 // SGPR instructions are not affected by EXEC
538 return false;
539 }
540 }
541 }
542 }
543
544 return true;
545}
546
547MachineBasicBlock::iterator
548SIWholeQuadMode::saveSCC(MachineBasicBlock &MBB,
549 MachineBasicBlock::iterator Before) {
Marek Olsak79c05872016-11-25 17:37:09 +0000550 unsigned SaveReg = MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000551
552 MachineInstr *Save =
553 BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), SaveReg)
554 .addReg(AMDGPU::SCC);
555 MachineInstr *Restore =
556 BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), AMDGPU::SCC)
557 .addReg(SaveReg);
558
559 LIS->InsertMachineInstrInMaps(*Save);
560 LIS->InsertMachineInstrInMaps(*Restore);
561 LIS->createAndComputeVirtRegInterval(SaveReg);
562
563 return Restore;
564}
565
566// Return an iterator in the (inclusive) range [First, Last] at which
567// instructions can be safely inserted, keeping in mind that some of the
568// instructions we want to add necessarily clobber SCC.
569MachineBasicBlock::iterator SIWholeQuadMode::prepareInsertion(
570 MachineBasicBlock &MBB, MachineBasicBlock::iterator First,
571 MachineBasicBlock::iterator Last, bool PreferLast, bool SaveSCC) {
572 if (!SaveSCC)
573 return PreferLast ? Last : First;
574
575 LiveRange &LR = LIS->getRegUnit(*MCRegUnitIterator(AMDGPU::SCC, TRI));
576 auto MBBE = MBB.end();
577 SlotIndex FirstIdx = First != MBBE ? LIS->getInstructionIndex(*First)
578 : LIS->getMBBEndIdx(&MBB);
579 SlotIndex LastIdx =
580 Last != MBBE ? LIS->getInstructionIndex(*Last) : LIS->getMBBEndIdx(&MBB);
581 SlotIndex Idx = PreferLast ? LastIdx : FirstIdx;
582 const LiveRange::Segment *S;
583
584 for (;;) {
585 S = LR.getSegmentContaining(Idx);
586 if (!S)
587 break;
588
589 if (PreferLast) {
590 SlotIndex Next = S->start.getBaseIndex();
591 if (Next < FirstIdx)
592 break;
593 Idx = Next;
594 } else {
595 SlotIndex Next = S->end.getNextIndex().getBaseIndex();
596 if (Next > LastIdx)
597 break;
598 Idx = Next;
599 }
600 }
601
602 MachineBasicBlock::iterator MBBI;
603
604 if (MachineInstr *MI = LIS->getInstructionFromIndex(Idx))
605 MBBI = MI;
606 else {
607 assert(Idx == LIS->getMBBEndIdx(&MBB));
608 MBBI = MBB.end();
609 }
610
611 if (S)
612 MBBI = saveSCC(MBB, MBBI);
613
614 return MBBI;
615}
616
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000617void SIWholeQuadMode::toExact(MachineBasicBlock &MBB,
618 MachineBasicBlock::iterator Before,
Nicolai Haehnlea56e6b62016-03-21 20:39:24 +0000619 unsigned SaveWQM, unsigned LiveMaskReg) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000620 MachineInstr *MI;
621
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000622 if (SaveWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000623 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_AND_SAVEEXEC_B64),
624 SaveWQM)
625 .addReg(LiveMaskReg);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000626 } else {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000627 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_AND_B64),
628 AMDGPU::EXEC)
629 .addReg(AMDGPU::EXEC)
630 .addReg(LiveMaskReg);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000631 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000632
633 LIS->InsertMachineInstrInMaps(*MI);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000634}
635
636void SIWholeQuadMode::toWQM(MachineBasicBlock &MBB,
637 MachineBasicBlock::iterator Before,
Nicolai Haehnlea56e6b62016-03-21 20:39:24 +0000638 unsigned SavedWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000639 MachineInstr *MI;
640
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000641 if (SavedWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000642 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), AMDGPU::EXEC)
643 .addReg(SavedWQM);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000644 } else {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000645 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
646 AMDGPU::EXEC)
647 .addReg(AMDGPU::EXEC);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000648 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000649
650 LIS->InsertMachineInstrInMaps(*MI);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000651}
652
Connor Abbott92638ab2017-08-04 18:36:52 +0000653void SIWholeQuadMode::toWWM(MachineBasicBlock &MBB,
654 MachineBasicBlock::iterator Before,
655 unsigned SaveOrig) {
656 MachineInstr *MI;
657
658 assert(SaveOrig);
Neil Henning0a30f332019-04-01 15:19:52 +0000659 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::ENTER_WWM), SaveOrig)
Connor Abbott92638ab2017-08-04 18:36:52 +0000660 .addImm(-1);
661 LIS->InsertMachineInstrInMaps(*MI);
662}
663
664void SIWholeQuadMode::fromWWM(MachineBasicBlock &MBB,
665 MachineBasicBlock::iterator Before,
666 unsigned SavedOrig) {
667 MachineInstr *MI;
668
669 assert(SavedOrig);
670 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::EXIT_WWM), AMDGPU::EXEC)
671 .addReg(SavedOrig);
672 LIS->InsertMachineInstrInMaps(*MI);
673}
674
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000675void SIWholeQuadMode::processBlock(MachineBasicBlock &MBB, unsigned LiveMaskReg,
676 bool isEntry) {
677 auto BII = Blocks.find(&MBB);
678 if (BII == Blocks.end())
679 return;
680
681 const BlockInfo &BI = BII->second;
682
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000683 // This is a non-entry block that is WQM throughout, so no need to do
684 // anything.
Connor Abbott92638ab2017-08-04 18:36:52 +0000685 if (!isEntry && BI.Needs == StateWQM && BI.OutNeeds != StateExact)
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000686 return;
687
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000688 LLVM_DEBUG(dbgs() << "\nProcessing block " << printMBBReference(MBB)
689 << ":\n");
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000690
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000691 unsigned SavedWQMReg = 0;
Connor Abbott92638ab2017-08-04 18:36:52 +0000692 unsigned SavedNonWWMReg = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000693 bool WQMFromExec = isEntry;
Connor Abbott92638ab2017-08-04 18:36:52 +0000694 char State = (isEntry || !(BI.InNeeds & StateWQM)) ? StateExact : StateWQM;
695 char NonWWMState = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000696
697 auto II = MBB.getFirstNonPHI(), IE = MBB.end();
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000698 if (isEntry)
699 ++II; // Skip the instruction that saves LiveMask
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000700
Connor Abbott92638ab2017-08-04 18:36:52 +0000701 // This stores the first instruction where it's safe to switch from WQM to
702 // Exact or vice versa.
703 MachineBasicBlock::iterator FirstWQM = IE;
704
705 // This stores the first instruction where it's safe to switch from WWM to
706 // Exact/WQM or to switch to WWM. It must always be the same as, or after,
707 // FirstWQM since if it's safe to switch to/from WWM, it must be safe to
708 // switch to/from WQM as well.
709 MachineBasicBlock::iterator FirstWWM = IE;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000710 for (;;) {
711 MachineBasicBlock::iterator Next = II;
Connor Abbott92638ab2017-08-04 18:36:52 +0000712 char Needs = StateExact | StateWQM; // WWM is disabled by default
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000713 char OutNeeds = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000714
Connor Abbott92638ab2017-08-04 18:36:52 +0000715 if (FirstWQM == IE)
716 FirstWQM = II;
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000717
Connor Abbott92638ab2017-08-04 18:36:52 +0000718 if (FirstWWM == IE)
719 FirstWWM = II;
720
721 // First, figure out the allowed states (Needs) based on the propagated
722 // flags.
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000723 if (II != IE) {
724 MachineInstr &MI = *II;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000725
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000726 if (requiresCorrectState(MI)) {
727 auto III = Instructions.find(&MI);
728 if (III != Instructions.end()) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000729 if (III->second.Needs & StateWWM)
730 Needs = StateWWM;
731 else if (III->second.Needs & StateWQM)
Connor Abbottde068fe2017-08-04 18:36:50 +0000732 Needs = StateWQM;
733 else
734 Needs &= ~III->second.Disabled;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000735 OutNeeds = III->second.OutNeeds;
736 }
Connor Abbott92638ab2017-08-04 18:36:52 +0000737 } else {
738 // If the instruction doesn't actually need a correct EXEC, then we can
739 // safely leave WWM enabled.
740 Needs = StateExact | StateWQM | StateWWM;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000741 }
742
Connor Abbottde068fe2017-08-04 18:36:50 +0000743 if (MI.isTerminator() && OutNeeds == StateExact)
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000744 Needs = StateExact;
745
746 if (MI.getOpcode() == AMDGPU::SI_ELSE && BI.OutNeeds == StateExact)
747 MI.getOperand(3).setImm(1);
748
749 ++Next;
750 } else {
751 // End of basic block
752 if (BI.OutNeeds & StateWQM)
753 Needs = StateWQM;
754 else if (BI.OutNeeds == StateExact)
755 Needs = StateExact;
Connor Abbottde068fe2017-08-04 18:36:50 +0000756 else
757 Needs = StateWQM | StateExact;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000758 }
Nicolai Haehnle3b572002016-07-28 11:39:24 +0000759
Connor Abbott92638ab2017-08-04 18:36:52 +0000760 // Now, transition if necessary.
Connor Abbottde068fe2017-08-04 18:36:50 +0000761 if (!(Needs & State)) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000762 MachineBasicBlock::iterator First;
763 if (State == StateWWM || Needs == StateWWM) {
764 // We must switch to or from WWM
765 First = FirstWWM;
766 } else {
767 // We only need to switch to/from WQM, so we can use FirstWQM
768 First = FirstWQM;
769 }
770
Connor Abbottde068fe2017-08-04 18:36:50 +0000771 MachineBasicBlock::iterator Before =
772 prepareInsertion(MBB, First, II, Needs == StateWQM,
773 Needs == StateExact || WQMFromExec);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000774
Connor Abbott92638ab2017-08-04 18:36:52 +0000775 if (State == StateWWM) {
776 assert(SavedNonWWMReg);
777 fromWWM(MBB, Before, SavedNonWWMReg);
778 State = NonWWMState;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000779 }
780
Connor Abbott92638ab2017-08-04 18:36:52 +0000781 if (Needs == StateWWM) {
782 NonWWMState = State;
783 SavedNonWWMReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
784 toWWM(MBB, Before, SavedNonWWMReg);
785 State = StateWWM;
786 } else {
787 if (State == StateWQM && (Needs & StateExact) && !(Needs & StateWQM)) {
788 if (!WQMFromExec && (OutNeeds & StateWQM))
789 SavedWQMReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
790
791 toExact(MBB, Before, SavedWQMReg, LiveMaskReg);
792 State = StateExact;
793 } else if (State == StateExact && (Needs & StateWQM) &&
794 !(Needs & StateExact)) {
795 assert(WQMFromExec == (SavedWQMReg == 0));
796
797 toWQM(MBB, Before, SavedWQMReg);
798
799 if (SavedWQMReg) {
800 LIS->createAndComputeVirtRegInterval(SavedWQMReg);
801 SavedWQMReg = 0;
802 }
803 State = StateWQM;
804 } else {
805 // We can get here if we transitioned from WWM to a non-WWM state that
806 // already matches our needs, but we shouldn't need to do anything.
807 assert(Needs & State);
808 }
809 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000810 }
811
Connor Abbott92638ab2017-08-04 18:36:52 +0000812 if (Needs != (StateExact | StateWQM | StateWWM)) {
813 if (Needs != (StateExact | StateWQM))
814 FirstWQM = IE;
815 FirstWWM = IE;
816 }
Connor Abbottde068fe2017-08-04 18:36:50 +0000817
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000818 if (II == IE)
819 break;
820 II = Next;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000821 }
822}
823
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000824void SIWholeQuadMode::lowerLiveMaskQueries(unsigned LiveMaskReg) {
825 for (MachineInstr *MI : LiveMaskQueries) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000826 const DebugLoc &DL = MI->getDebugLoc();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000827 unsigned Dest = MI->getOperand(0).getReg();
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000828 MachineInstr *Copy =
829 BuildMI(*MI->getParent(), MI, DL, TII->get(AMDGPU::COPY), Dest)
830 .addReg(LiveMaskReg);
831
832 LIS->ReplaceMachineInstrInMaps(*MI, *Copy);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000833 MI->eraseFromParent();
834 }
835}
836
Connor Abbott8c217d02017-08-04 18:36:49 +0000837void SIWholeQuadMode::lowerCopyInstrs() {
Connor Abbott66b9bd62017-08-04 18:36:54 +0000838 for (MachineInstr *MI : LowerToCopyInstrs) {
839 for (unsigned i = MI->getNumExplicitOperands() - 1; i > 1; i--)
840 MI->RemoveOperand(i);
Neil Henning0a30f332019-04-01 15:19:52 +0000841
842 const unsigned Reg = MI->getOperand(0).getReg();
843
844 if (TRI->isVGPR(*MRI, Reg)) {
845 const TargetRegisterClass *regClass =
846 TargetRegisterInfo::isVirtualRegister(Reg)
847 ? MRI->getRegClass(Reg)
848 : TRI->getPhysRegClass(Reg);
849
850 const unsigned MovOp = TII->getMovOpcode(regClass);
851 MI->setDesc(TII->get(MovOp));
852
853 // And make it implicitly depend on exec (like all VALU movs should do).
854 MI->addOperand(MachineOperand::CreateReg(AMDGPU::EXEC, false, true));
855 } else {
856 MI->setDesc(TII->get(AMDGPU::COPY));
857 }
Connor Abbott66b9bd62017-08-04 18:36:54 +0000858 }
Connor Abbott8c217d02017-08-04 18:36:49 +0000859}
860
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000861bool SIWholeQuadMode::runOnMachineFunction(MachineFunction &MF) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000862 Instructions.clear();
863 Blocks.clear();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000864 LiveMaskQueries.clear();
Connor Abbott8c217d02017-08-04 18:36:49 +0000865 LowerToCopyInstrs.clear();
Matthias Braunf1caa282017-12-15 22:22:58 +0000866 CallingConv = MF.getFunction().getCallingConv();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000867
Tom Stellard5bfbae52018-07-11 20:59:01 +0000868 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000869
870 TII = ST.getInstrInfo();
871 TRI = &TII->getRegisterInfo();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000872 MRI = &MF.getRegInfo();
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000873 LIS = &getAnalysis<LiveIntervals>();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000874
875 char GlobalFlags = analyzeFunction(MF);
Connor Abbott92638ab2017-08-04 18:36:52 +0000876 unsigned LiveMaskReg = 0;
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000877 if (!(GlobalFlags & StateWQM)) {
878 lowerLiveMaskQueries(AMDGPU::EXEC);
Connor Abbott92638ab2017-08-04 18:36:52 +0000879 if (!(GlobalFlags & StateWWM))
880 return !LiveMaskQueries.empty();
881 } else {
882 // Store a copy of the original live mask when required
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000883 MachineBasicBlock &Entry = MF.front();
884 MachineBasicBlock::iterator EntryMI = Entry.getFirstNonPHI();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000885
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000886 if (GlobalFlags & StateExact || !LiveMaskQueries.empty()) {
887 LiveMaskReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000888 MachineInstr *MI = BuildMI(Entry, EntryMI, DebugLoc(),
889 TII->get(AMDGPU::COPY), LiveMaskReg)
890 .addReg(AMDGPU::EXEC);
891 LIS->InsertMachineInstrInMaps(*MI);
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000892 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000893
Connor Abbott92638ab2017-08-04 18:36:52 +0000894 lowerLiveMaskQueries(LiveMaskReg);
895
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000896 if (GlobalFlags == StateWQM) {
897 // For a shader that needs only WQM, we can just set it once.
898 BuildMI(Entry, EntryMI, DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
899 AMDGPU::EXEC)
900 .addReg(AMDGPU::EXEC);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000901
Connor Abbott8c217d02017-08-04 18:36:49 +0000902 lowerCopyInstrs();
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000903 // EntryMI may become invalid here
904 return true;
905 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000906 }
907
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000908 LLVM_DEBUG(printInfo());
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000909
Connor Abbott8c217d02017-08-04 18:36:49 +0000910 lowerCopyInstrs();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000911
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000912 // Handle the general case
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000913 for (auto BII : Blocks)
914 processBlock(*BII.first, LiveMaskReg, BII.first == &*MF.begin());
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000915
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000916 // Physical registers like SCC aren't tracked by default anyway, so just
917 // removing the ranges we computed is the simplest option for maintaining
918 // the analysis results.
919 LIS->removeRegUnit(*MCRegUnitIterator(AMDGPU::SCC, TRI));
920
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000921 return true;
922}