blob: 18649733103321ac75d191202f67d93ac00e07cf [file] [log] [blame]
Nicolai Haehnle213e87f2016-03-21 20:28:33 +00001//===-- SIWholeQuadMode.cpp - enter and suspend whole quad mode -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief This pass adds instructions to enable whole quad mode for pixel
Connor Abbott92638ab2017-08-04 18:36:52 +000012/// shaders, and whole wavefront mode for all programs.
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000013///
14/// Whole quad mode is required for derivative computations, but it interferes
15/// with shader side effects (stores and atomics). This pass is run on the
16/// scheduled machine IR but before register coalescing, so that machine SSA is
17/// available for analysis. It ensures that WQM is enabled when necessary, but
18/// disabled around stores and atomics.
19///
20/// When necessary, this pass creates a function prolog
21///
22/// S_MOV_B64 LiveMask, EXEC
23/// S_WQM_B64 EXEC, EXEC
24///
25/// to enter WQM at the top of the function and surrounds blocks of Exact
26/// instructions by
27///
28/// S_AND_SAVEEXEC_B64 Tmp, LiveMask
29/// ...
30/// S_MOV_B64 EXEC, Tmp
31///
Connor Abbott92638ab2017-08-04 18:36:52 +000032/// We also compute when a sequence of instructions requires Whole Wavefront
33/// Mode (WWM) and insert instructions to save and restore it:
34///
35/// S_OR_SAVEEXEC_B64 Tmp, -1
36/// ...
37/// S_MOV_B64 EXEC, Tmp
38///
Nicolai Haehnle213e87f2016-03-21 20:28:33 +000039/// In order to avoid excessive switching during sequences of Exact
40/// instructions, the pass first analyzes which instructions must be run in WQM
41/// (aka which instructions produce values that lead to derivative
42/// computations).
43///
44/// Basic blocks are always exited in WQM as long as some successor needs WQM.
45///
46/// There is room for improvement given better control flow analysis:
47///
48/// (1) at the top level (outside of control flow statements, and as long as
49/// kill hasn't been used), one SGPR can be saved by recovering WQM from
50/// the LiveMask (this is implemented for the entry block).
51///
52/// (2) when entire regions (e.g. if-else blocks or entire loops) only
53/// consist of exact and don't-care instructions, the switch only has to
54/// be done at the entry and exit points rather than potentially in each
55/// block of the region.
56///
57//===----------------------------------------------------------------------===//
58
59#include "AMDGPU.h"
60#include "AMDGPUSubtarget.h"
61#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"
68#include "llvm/CodeGen/LiveIntervalAnalysis.h"
69#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>();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000203 AU.setPreservesCFG();
204 MachineFunctionPass::getAnalysisUsage(AU);
205 }
206};
207
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000208} // end anonymous namespace
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000209
210char SIWholeQuadMode::ID = 0;
211
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000212INITIALIZE_PASS_BEGIN(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false,
213 false)
214INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
215INITIALIZE_PASS_END(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false,
216 false)
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000217
218char &llvm::SIWholeQuadModeID = SIWholeQuadMode::ID;
219
220FunctionPass *llvm::createSIWholeQuadModePass() {
221 return new SIWholeQuadMode;
222}
223
Eric Christopher3148a1b2017-11-16 03:18:15 +0000224#ifndef NDEBUG
Eric Christopher63481882017-11-16 03:25:02 +0000225LLVM_DUMP_METHOD void SIWholeQuadMode::printInfo() {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000226 for (const auto &BII : Blocks) {
227 dbgs() << "\nBB#" << BII.first->getNumber() << ":\n"
228 << " InNeeds = " << PrintState(BII.second.InNeeds)
229 << ", Needs = " << PrintState(BII.second.Needs)
230 << ", OutNeeds = " << PrintState(BII.second.OutNeeds) << "\n\n";
231
232 for (const MachineInstr &MI : *BII.first) {
233 auto III = Instructions.find(&MI);
234 if (III == Instructions.end())
235 continue;
236
237 dbgs() << " " << MI << " Needs = " << PrintState(III->second.Needs)
238 << ", OutNeeds = " << PrintState(III->second.OutNeeds) << '\n';
239 }
240 }
241}
Eric Christopher3148a1b2017-11-16 03:18:15 +0000242#endif
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000243
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000244void SIWholeQuadMode::markInstruction(MachineInstr &MI, char Flag,
245 std::vector<WorkItem> &Worklist) {
246 InstrInfo &II = Instructions[&MI];
247
Connor Abbott92638ab2017-08-04 18:36:52 +0000248 assert(!(Flag & StateExact) && Flag != 0);
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000249
Connor Abbottde068fe2017-08-04 18:36:50 +0000250 // Remove any disabled states from the flag. The user that required it gets
251 // an undefined value in the helper lanes. For example, this can happen if
252 // the result of an atomic is used by instruction that requires WQM, where
253 // ignoring the request for WQM is correct as per the relevant specs.
254 Flag &= ~II.Disabled;
255
256 // Ignore if the flag is already encompassed by the existing needs, or we
257 // just disabled everything.
258 if ((II.Needs & Flag) == Flag)
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000259 return;
260
Connor Abbottde068fe2017-08-04 18:36:50 +0000261 II.Needs |= Flag;
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000262 Worklist.push_back(&MI);
263}
264
Connor Abbottde068fe2017-08-04 18:36:50 +0000265/// Mark all instructions defining the uses in \p MI with \p Flag.
266void SIWholeQuadMode::markInstructionUses(const MachineInstr &MI, char Flag,
267 std::vector<WorkItem> &Worklist) {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000268 for (const MachineOperand &Use : MI.uses()) {
269 if (!Use.isReg() || !Use.isUse())
270 continue;
271
272 unsigned Reg = Use.getReg();
273
274 // Handle physical registers that we need to track; this is mostly relevant
275 // for VCC, which can appear as the (implicit) input of a uniform branch,
276 // e.g. when a loop counter is stored in a VGPR.
277 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
278 if (Reg == AMDGPU::EXEC)
279 continue;
280
281 for (MCRegUnitIterator RegUnit(Reg, TRI); RegUnit.isValid(); ++RegUnit) {
282 LiveRange &LR = LIS->getRegUnit(*RegUnit);
283 const VNInfo *Value = LR.Query(LIS->getInstructionIndex(MI)).valueIn();
284 if (!Value)
285 continue;
286
287 // Since we're in machine SSA, we do not need to track physical
288 // registers across basic blocks.
289 if (Value->isPHIDef())
290 continue;
291
Connor Abbottde068fe2017-08-04 18:36:50 +0000292 markInstruction(*LIS->getInstructionFromIndex(Value->def), Flag,
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000293 Worklist);
294 }
295
296 continue;
297 }
298
299 for (MachineInstr &DefMI : MRI->def_instructions(Use.getReg()))
Connor Abbottde068fe2017-08-04 18:36:50 +0000300 markInstruction(DefMI, Flag, Worklist);
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000301 }
302}
303
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000304// Scan instructions to determine which ones require an Exact execmask and
305// which ones seed WQM requirements.
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000306char SIWholeQuadMode::scanInstructions(MachineFunction &MF,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000307 std::vector<WorkItem> &Worklist) {
308 char GlobalFlags = 0;
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000309 bool WQMOutputs = MF.getFunction()->hasFnAttribute("amdgpu-ps-wqm-outputs");
Connor Abbott66b9bd62017-08-04 18:36:54 +0000310 SmallVector<MachineInstr *, 4> SetInactiveInstrs;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000311
Connor Abbottde068fe2017-08-04 18:36:50 +0000312 // We need to visit the basic blocks in reverse post-order so that we visit
313 // defs before uses, in particular so that we don't accidentally mark an
314 // instruction as needing e.g. WQM before visiting it and realizing it needs
315 // WQM disabled.
316 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
317 for (auto BI = RPOT.begin(), BE = RPOT.end(); BI != BE; ++BI) {
318 MachineBasicBlock &MBB = **BI;
319 BlockInfo &BBI = Blocks[&MBB];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000320
321 for (auto II = MBB.begin(), IE = MBB.end(); II != IE; ++II) {
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000322 MachineInstr &MI = *II;
Connor Abbottde068fe2017-08-04 18:36:50 +0000323 InstrInfo &III = Instructions[&MI];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000324 unsigned Opcode = MI.getOpcode();
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000325 char Flags = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000326
Connor Abbott92638ab2017-08-04 18:36:52 +0000327 if (TII->isDS(Opcode) && CallingConv == CallingConv::AMDGPU_PS) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000328 Flags = StateWQM;
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000329 } else if (TII->isWQM(Opcode)) {
330 // Sampling instructions don't need to produce results for all pixels
331 // in a quad, they just require all inputs of a quad to have been
332 // computed for derivatives.
Connor Abbottde068fe2017-08-04 18:36:50 +0000333 markInstructionUses(MI, StateWQM, Worklist);
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000334 GlobalFlags |= StateWQM;
335 continue;
Connor Abbott8c217d02017-08-04 18:36:49 +0000336 } else if (Opcode == AMDGPU::WQM) {
337 // The WQM intrinsic requires its output to have all the helper lanes
338 // correct, so we need it to be in WQM.
339 Flags = StateWQM;
340 LowerToCopyInstrs.push_back(&MI);
Connor Abbott92638ab2017-08-04 18:36:52 +0000341 } else if (Opcode == AMDGPU::WWM) {
342 // The WWM intrinsic doesn't make the same guarantee, and plus it needs
343 // to be executed in WQM or Exact so that its copy doesn't clobber
344 // inactive lanes.
345 markInstructionUses(MI, StateWWM, Worklist);
346 GlobalFlags |= StateWWM;
347 LowerToCopyInstrs.push_back(&MI);
348 continue;
Connor Abbott66b9bd62017-08-04 18:36:54 +0000349 } else if (Opcode == AMDGPU::V_SET_INACTIVE_B32 ||
350 Opcode == AMDGPU::V_SET_INACTIVE_B64) {
351 III.Disabled = StateWWM;
352 MachineOperand &Inactive = MI.getOperand(2);
353 if (Inactive.isReg()) {
354 if (Inactive.isUndef()) {
355 LowerToCopyInstrs.push_back(&MI);
356 } else {
357 unsigned Reg = Inactive.getReg();
358 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
359 for (MachineInstr &DefMI : MRI->def_instructions(Reg))
360 markInstruction(DefMI, StateWWM, Worklist);
361 }
362 }
363 }
364 SetInactiveInstrs.push_back(&MI);
365 continue;
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000366 } else if (TII->isDisableWQM(MI)) {
Connor Abbottde068fe2017-08-04 18:36:50 +0000367 BBI.Needs |= StateExact;
368 if (!(BBI.InNeeds & StateExact)) {
369 BBI.InNeeds |= StateExact;
370 Worklist.push_back(&MBB);
371 }
372 GlobalFlags |= StateExact;
Connor Abbott92638ab2017-08-04 18:36:52 +0000373 III.Disabled = StateWQM | StateWWM;
Connor Abbottde068fe2017-08-04 18:36:50 +0000374 continue;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000375 } else {
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000376 if (Opcode == AMDGPU::SI_PS_LIVE) {
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000377 LiveMaskQueries.push_back(&MI);
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000378 } else if (WQMOutputs) {
379 // The function is in machine SSA form, which means that physical
380 // VGPRs correspond to shader inputs and outputs. Inputs are
381 // only used, outputs are only defined.
382 for (const MachineOperand &MO : MI.defs()) {
383 if (!MO.isReg())
384 continue;
385
386 unsigned Reg = MO.getReg();
387
388 if (!TRI->isVirtualRegister(Reg) &&
389 TRI->hasVGPRs(TRI->getPhysRegClass(Reg))) {
390 Flags = StateWQM;
391 break;
392 }
393 }
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000394 }
395
Nicolai Haehnlec00e03b2016-06-07 21:37:17 +0000396 if (!Flags)
397 continue;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000398 }
399
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000400 markInstruction(MI, Flags, Worklist);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000401 GlobalFlags |= Flags;
402 }
403 }
404
Connor Abbott66b9bd62017-08-04 18:36:54 +0000405 // Mark sure that any SET_INACTIVE instructions are computed in WQM if WQM is
406 // ever used anywhere in the function. This implements the corresponding
407 // semantics of @llvm.amdgcn.set.inactive.
408 if (GlobalFlags & StateWQM) {
409 for (MachineInstr *MI : SetInactiveInstrs)
410 markInstruction(*MI, StateWQM, Worklist);
411 }
412
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000413 return GlobalFlags;
414}
415
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000416void SIWholeQuadMode::propagateInstruction(MachineInstr &MI,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000417 std::vector<WorkItem>& Worklist) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000418 MachineBasicBlock *MBB = MI.getParent();
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000419 InstrInfo II = Instructions[&MI]; // take a copy to prevent dangling references
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000420 BlockInfo &BI = Blocks[MBB];
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000421
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000422 // Control flow-type instructions and stores to temporary memory that are
423 // followed by WQM computations must themselves be in WQM.
Connor Abbottde068fe2017-08-04 18:36:50 +0000424 if ((II.OutNeeds & StateWQM) && !(II.Disabled & StateWQM) &&
Nicolai Haehnle8a482b32016-08-02 19:31:14 +0000425 (MI.isTerminator() || (TII->usesVM_CNT(MI) && MI.mayStore()))) {
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000426 Instructions[&MI].Needs = StateWQM;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000427 II.Needs = StateWQM;
Nicolai Haehnle0a33abd2016-03-21 22:54:02 +0000428 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000429
430 // Propagate to block level
Connor Abbottde068fe2017-08-04 18:36:50 +0000431 if (II.Needs & StateWQM) {
432 BI.Needs |= StateWQM;
433 if (!(BI.InNeeds & StateWQM)) {
434 BI.InNeeds |= StateWQM;
435 Worklist.push_back(MBB);
436 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000437 }
438
439 // Propagate backwards within block
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000440 if (MachineInstr *PrevMI = MI.getPrevNode()) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000441 char InNeeds = (II.Needs & ~StateWWM) | II.OutNeeds;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000442 if (!PrevMI->isPHI()) {
443 InstrInfo &PrevII = Instructions[PrevMI];
444 if ((PrevII.OutNeeds | InNeeds) != PrevII.OutNeeds) {
445 PrevII.OutNeeds |= InNeeds;
446 Worklist.push_back(PrevMI);
447 }
448 }
449 }
450
451 // Propagate WQM flag to instruction inputs
Connor Abbottde068fe2017-08-04 18:36:50 +0000452 assert(!(II.Needs & StateExact));
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000453
Connor Abbottde068fe2017-08-04 18:36:50 +0000454 if (II.Needs != 0)
455 markInstructionUses(MI, II.Needs, Worklist);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000456}
457
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000458void SIWholeQuadMode::propagateBlock(MachineBasicBlock &MBB,
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000459 std::vector<WorkItem>& Worklist) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000460 BlockInfo BI = Blocks[&MBB]; // Make a copy to prevent dangling references.
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000461
462 // Propagate through instructions
463 if (!MBB.empty()) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000464 MachineInstr *LastMI = &*MBB.rbegin();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000465 InstrInfo &LastII = Instructions[LastMI];
466 if ((LastII.OutNeeds | BI.OutNeeds) != LastII.OutNeeds) {
467 LastII.OutNeeds |= BI.OutNeeds;
468 Worklist.push_back(LastMI);
469 }
470 }
471
472 // Predecessor blocks must provide for our WQM/Exact needs.
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000473 for (MachineBasicBlock *Pred : MBB.predecessors()) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000474 BlockInfo &PredBI = Blocks[Pred];
475 if ((PredBI.OutNeeds | BI.InNeeds) == PredBI.OutNeeds)
476 continue;
477
478 PredBI.OutNeeds |= BI.InNeeds;
479 PredBI.InNeeds |= BI.InNeeds;
480 Worklist.push_back(Pred);
481 }
482
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000483 // All successors must be prepared to accept the same set of WQM/Exact data.
484 for (MachineBasicBlock *Succ : MBB.successors()) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000485 BlockInfo &SuccBI = Blocks[Succ];
486 if ((SuccBI.InNeeds | BI.OutNeeds) == SuccBI.InNeeds)
487 continue;
488
489 SuccBI.InNeeds |= BI.OutNeeds;
490 Worklist.push_back(Succ);
491 }
492}
493
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000494char SIWholeQuadMode::analyzeFunction(MachineFunction &MF) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000495 std::vector<WorkItem> Worklist;
496 char GlobalFlags = scanInstructions(MF, Worklist);
497
498 while (!Worklist.empty()) {
499 WorkItem WI = Worklist.back();
500 Worklist.pop_back();
501
502 if (WI.MI)
503 propagateInstruction(*WI.MI, Worklist);
504 else
505 propagateBlock(*WI.MBB, Worklist);
506 }
507
508 return GlobalFlags;
509}
510
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000511/// Whether \p MI really requires the exec state computed during analysis.
512///
513/// Scalar instructions must occasionally be marked WQM for correct propagation
514/// (e.g. thread masks leading up to branches), but when it comes to actual
515/// execution, they don't care about EXEC.
516bool SIWholeQuadMode::requiresCorrectState(const MachineInstr &MI) const {
517 if (MI.isTerminator())
518 return true;
519
520 // Skip instructions that are not affected by EXEC
521 if (TII->isScalarUnit(MI))
522 return false;
523
524 // Generic instructions such as COPY will either disappear by register
525 // coalescing or be lowered to SALU or VALU instructions.
526 if (MI.isTransient()) {
527 if (MI.getNumExplicitOperands() >= 1) {
528 const MachineOperand &Op = MI.getOperand(0);
529 if (Op.isReg()) {
530 if (TRI->isSGPRReg(*MRI, Op.getReg())) {
531 // SGPR instructions are not affected by EXEC
532 return false;
533 }
534 }
535 }
536 }
537
538 return true;
539}
540
541MachineBasicBlock::iterator
542SIWholeQuadMode::saveSCC(MachineBasicBlock &MBB,
543 MachineBasicBlock::iterator Before) {
Marek Olsak79c05872016-11-25 17:37:09 +0000544 unsigned SaveReg = MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000545
546 MachineInstr *Save =
547 BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), SaveReg)
548 .addReg(AMDGPU::SCC);
549 MachineInstr *Restore =
550 BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), AMDGPU::SCC)
551 .addReg(SaveReg);
552
553 LIS->InsertMachineInstrInMaps(*Save);
554 LIS->InsertMachineInstrInMaps(*Restore);
555 LIS->createAndComputeVirtRegInterval(SaveReg);
556
557 return Restore;
558}
559
560// Return an iterator in the (inclusive) range [First, Last] at which
561// instructions can be safely inserted, keeping in mind that some of the
562// instructions we want to add necessarily clobber SCC.
563MachineBasicBlock::iterator SIWholeQuadMode::prepareInsertion(
564 MachineBasicBlock &MBB, MachineBasicBlock::iterator First,
565 MachineBasicBlock::iterator Last, bool PreferLast, bool SaveSCC) {
566 if (!SaveSCC)
567 return PreferLast ? Last : First;
568
569 LiveRange &LR = LIS->getRegUnit(*MCRegUnitIterator(AMDGPU::SCC, TRI));
570 auto MBBE = MBB.end();
571 SlotIndex FirstIdx = First != MBBE ? LIS->getInstructionIndex(*First)
572 : LIS->getMBBEndIdx(&MBB);
573 SlotIndex LastIdx =
574 Last != MBBE ? LIS->getInstructionIndex(*Last) : LIS->getMBBEndIdx(&MBB);
575 SlotIndex Idx = PreferLast ? LastIdx : FirstIdx;
576 const LiveRange::Segment *S;
577
578 for (;;) {
579 S = LR.getSegmentContaining(Idx);
580 if (!S)
581 break;
582
583 if (PreferLast) {
584 SlotIndex Next = S->start.getBaseIndex();
585 if (Next < FirstIdx)
586 break;
587 Idx = Next;
588 } else {
589 SlotIndex Next = S->end.getNextIndex().getBaseIndex();
590 if (Next > LastIdx)
591 break;
592 Idx = Next;
593 }
594 }
595
596 MachineBasicBlock::iterator MBBI;
597
598 if (MachineInstr *MI = LIS->getInstructionFromIndex(Idx))
599 MBBI = MI;
600 else {
601 assert(Idx == LIS->getMBBEndIdx(&MBB));
602 MBBI = MBB.end();
603 }
604
605 if (S)
606 MBBI = saveSCC(MBB, MBBI);
607
608 return MBBI;
609}
610
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000611void SIWholeQuadMode::toExact(MachineBasicBlock &MBB,
612 MachineBasicBlock::iterator Before,
Nicolai Haehnlea56e6b62016-03-21 20:39:24 +0000613 unsigned SaveWQM, unsigned LiveMaskReg) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000614 MachineInstr *MI;
615
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000616 if (SaveWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000617 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_AND_SAVEEXEC_B64),
618 SaveWQM)
619 .addReg(LiveMaskReg);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000620 } else {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000621 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_AND_B64),
622 AMDGPU::EXEC)
623 .addReg(AMDGPU::EXEC)
624 .addReg(LiveMaskReg);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000625 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000626
627 LIS->InsertMachineInstrInMaps(*MI);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000628}
629
630void SIWholeQuadMode::toWQM(MachineBasicBlock &MBB,
631 MachineBasicBlock::iterator Before,
Nicolai Haehnlea56e6b62016-03-21 20:39:24 +0000632 unsigned SavedWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000633 MachineInstr *MI;
634
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000635 if (SavedWQM) {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000636 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::COPY), AMDGPU::EXEC)
637 .addReg(SavedWQM);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000638 } else {
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000639 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
640 AMDGPU::EXEC)
641 .addReg(AMDGPU::EXEC);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000642 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000643
644 LIS->InsertMachineInstrInMaps(*MI);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000645}
646
Connor Abbott92638ab2017-08-04 18:36:52 +0000647void SIWholeQuadMode::toWWM(MachineBasicBlock &MBB,
648 MachineBasicBlock::iterator Before,
649 unsigned SaveOrig) {
650 MachineInstr *MI;
651
652 assert(SaveOrig);
653 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::S_OR_SAVEEXEC_B64),
654 SaveOrig)
655 .addImm(-1);
656 LIS->InsertMachineInstrInMaps(*MI);
657}
658
659void SIWholeQuadMode::fromWWM(MachineBasicBlock &MBB,
660 MachineBasicBlock::iterator Before,
661 unsigned SavedOrig) {
662 MachineInstr *MI;
663
664 assert(SavedOrig);
665 MI = BuildMI(MBB, Before, DebugLoc(), TII->get(AMDGPU::EXIT_WWM), AMDGPU::EXEC)
666 .addReg(SavedOrig);
667 LIS->InsertMachineInstrInMaps(*MI);
668}
669
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000670void SIWholeQuadMode::processBlock(MachineBasicBlock &MBB, unsigned LiveMaskReg,
671 bool isEntry) {
672 auto BII = Blocks.find(&MBB);
673 if (BII == Blocks.end())
674 return;
675
676 const BlockInfo &BI = BII->second;
677
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000678 // This is a non-entry block that is WQM throughout, so no need to do
679 // anything.
Connor Abbott92638ab2017-08-04 18:36:52 +0000680 if (!isEntry && BI.Needs == StateWQM && BI.OutNeeds != StateExact)
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000681 return;
682
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000683 DEBUG(dbgs() << "\nProcessing block BB#" << MBB.getNumber() << ":\n");
684
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000685 unsigned SavedWQMReg = 0;
Connor Abbott92638ab2017-08-04 18:36:52 +0000686 unsigned SavedNonWWMReg = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000687 bool WQMFromExec = isEntry;
Connor Abbott92638ab2017-08-04 18:36:52 +0000688 char State = (isEntry || !(BI.InNeeds & StateWQM)) ? StateExact : StateWQM;
689 char NonWWMState = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000690
691 auto II = MBB.getFirstNonPHI(), IE = MBB.end();
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000692 if (isEntry)
693 ++II; // Skip the instruction that saves LiveMask
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000694
Connor Abbott92638ab2017-08-04 18:36:52 +0000695 // This stores the first instruction where it's safe to switch from WQM to
696 // Exact or vice versa.
697 MachineBasicBlock::iterator FirstWQM = IE;
698
699 // This stores the first instruction where it's safe to switch from WWM to
700 // Exact/WQM or to switch to WWM. It must always be the same as, or after,
701 // FirstWQM since if it's safe to switch to/from WWM, it must be safe to
702 // switch to/from WQM as well.
703 MachineBasicBlock::iterator FirstWWM = IE;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000704 for (;;) {
705 MachineBasicBlock::iterator Next = II;
Connor Abbott92638ab2017-08-04 18:36:52 +0000706 char Needs = StateExact | StateWQM; // WWM is disabled by default
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000707 char OutNeeds = 0;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000708
Connor Abbott92638ab2017-08-04 18:36:52 +0000709 if (FirstWQM == IE)
710 FirstWQM = II;
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000711
Connor Abbott92638ab2017-08-04 18:36:52 +0000712 if (FirstWWM == IE)
713 FirstWWM = II;
714
715 // First, figure out the allowed states (Needs) based on the propagated
716 // flags.
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000717 if (II != IE) {
718 MachineInstr &MI = *II;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000719
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000720 if (requiresCorrectState(MI)) {
721 auto III = Instructions.find(&MI);
722 if (III != Instructions.end()) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000723 if (III->second.Needs & StateWWM)
724 Needs = StateWWM;
725 else if (III->second.Needs & StateWQM)
Connor Abbottde068fe2017-08-04 18:36:50 +0000726 Needs = StateWQM;
727 else
728 Needs &= ~III->second.Disabled;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000729 OutNeeds = III->second.OutNeeds;
730 }
Connor Abbott92638ab2017-08-04 18:36:52 +0000731 } else {
732 // If the instruction doesn't actually need a correct EXEC, then we can
733 // safely leave WWM enabled.
734 Needs = StateExact | StateWQM | StateWWM;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000735 }
736
Connor Abbottde068fe2017-08-04 18:36:50 +0000737 if (MI.isTerminator() && OutNeeds == StateExact)
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000738 Needs = StateExact;
739
740 if (MI.getOpcode() == AMDGPU::SI_ELSE && BI.OutNeeds == StateExact)
741 MI.getOperand(3).setImm(1);
742
743 ++Next;
744 } else {
745 // End of basic block
746 if (BI.OutNeeds & StateWQM)
747 Needs = StateWQM;
748 else if (BI.OutNeeds == StateExact)
749 Needs = StateExact;
Connor Abbottde068fe2017-08-04 18:36:50 +0000750 else
751 Needs = StateWQM | StateExact;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000752 }
Nicolai Haehnle3b572002016-07-28 11:39:24 +0000753
Connor Abbott92638ab2017-08-04 18:36:52 +0000754 // Now, transition if necessary.
Connor Abbottde068fe2017-08-04 18:36:50 +0000755 if (!(Needs & State)) {
Connor Abbott92638ab2017-08-04 18:36:52 +0000756 MachineBasicBlock::iterator First;
757 if (State == StateWWM || Needs == StateWWM) {
758 // We must switch to or from WWM
759 First = FirstWWM;
760 } else {
761 // We only need to switch to/from WQM, so we can use FirstWQM
762 First = FirstWQM;
763 }
764
Connor Abbottde068fe2017-08-04 18:36:50 +0000765 MachineBasicBlock::iterator Before =
766 prepareInsertion(MBB, First, II, Needs == StateWQM,
767 Needs == StateExact || WQMFromExec);
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000768
Connor Abbott92638ab2017-08-04 18:36:52 +0000769 if (State == StateWWM) {
770 assert(SavedNonWWMReg);
771 fromWWM(MBB, Before, SavedNonWWMReg);
772 State = NonWWMState;
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000773 }
774
Connor Abbott92638ab2017-08-04 18:36:52 +0000775 if (Needs == StateWWM) {
776 NonWWMState = State;
777 SavedNonWWMReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
778 toWWM(MBB, Before, SavedNonWWMReg);
779 State = StateWWM;
780 } else {
781 if (State == StateWQM && (Needs & StateExact) && !(Needs & StateWQM)) {
782 if (!WQMFromExec && (OutNeeds & StateWQM))
783 SavedWQMReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
784
785 toExact(MBB, Before, SavedWQMReg, LiveMaskReg);
786 State = StateExact;
787 } else if (State == StateExact && (Needs & StateWQM) &&
788 !(Needs & StateExact)) {
789 assert(WQMFromExec == (SavedWQMReg == 0));
790
791 toWQM(MBB, Before, SavedWQMReg);
792
793 if (SavedWQMReg) {
794 LIS->createAndComputeVirtRegInterval(SavedWQMReg);
795 SavedWQMReg = 0;
796 }
797 State = StateWQM;
798 } else {
799 // We can get here if we transitioned from WWM to a non-WWM state that
800 // already matches our needs, but we shouldn't need to do anything.
801 assert(Needs & State);
802 }
803 }
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000804 }
805
Connor Abbott92638ab2017-08-04 18:36:52 +0000806 if (Needs != (StateExact | StateWQM | StateWWM)) {
807 if (Needs != (StateExact | StateWQM))
808 FirstWQM = IE;
809 FirstWWM = IE;
810 }
Connor Abbottde068fe2017-08-04 18:36:50 +0000811
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000812 if (II == IE)
813 break;
814 II = Next;
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000815 }
816}
817
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000818void SIWholeQuadMode::lowerLiveMaskQueries(unsigned LiveMaskReg) {
819 for (MachineInstr *MI : LiveMaskQueries) {
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000820 const DebugLoc &DL = MI->getDebugLoc();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000821 unsigned Dest = MI->getOperand(0).getReg();
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000822 MachineInstr *Copy =
823 BuildMI(*MI->getParent(), MI, DL, TII->get(AMDGPU::COPY), Dest)
824 .addReg(LiveMaskReg);
825
826 LIS->ReplaceMachineInstrInMaps(*MI, *Copy);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000827 MI->eraseFromParent();
828 }
829}
830
Connor Abbott8c217d02017-08-04 18:36:49 +0000831void SIWholeQuadMode::lowerCopyInstrs() {
Connor Abbott66b9bd62017-08-04 18:36:54 +0000832 for (MachineInstr *MI : LowerToCopyInstrs) {
833 for (unsigned i = MI->getNumExplicitOperands() - 1; i > 1; i--)
834 MI->RemoveOperand(i);
Connor Abbott8c217d02017-08-04 18:36:49 +0000835 MI->setDesc(TII->get(AMDGPU::COPY));
Connor Abbott66b9bd62017-08-04 18:36:54 +0000836 }
Connor Abbott8c217d02017-08-04 18:36:49 +0000837}
838
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000839bool SIWholeQuadMode::runOnMachineFunction(MachineFunction &MF) {
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000840 Instructions.clear();
841 Blocks.clear();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000842 LiveMaskQueries.clear();
Connor Abbott8c217d02017-08-04 18:36:49 +0000843 LowerToCopyInstrs.clear();
Connor Abbott92638ab2017-08-04 18:36:52 +0000844 CallingConv = MF.getFunction()->getCallingConv();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000845
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000846 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
847
848 TII = ST.getInstrInfo();
849 TRI = &TII->getRegisterInfo();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000850 MRI = &MF.getRegInfo();
Nicolai Haehnlebef0e902016-08-02 19:17:37 +0000851 LIS = &getAnalysis<LiveIntervals>();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000852
853 char GlobalFlags = analyzeFunction(MF);
Connor Abbott92638ab2017-08-04 18:36:52 +0000854 unsigned LiveMaskReg = 0;
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000855 if (!(GlobalFlags & StateWQM)) {
856 lowerLiveMaskQueries(AMDGPU::EXEC);
Connor Abbott92638ab2017-08-04 18:36:52 +0000857 if (!(GlobalFlags & StateWWM))
858 return !LiveMaskQueries.empty();
859 } else {
860 // Store a copy of the original live mask when required
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000861 MachineBasicBlock &Entry = MF.front();
862 MachineBasicBlock::iterator EntryMI = Entry.getFirstNonPHI();
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000863
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000864 if (GlobalFlags & StateExact || !LiveMaskQueries.empty()) {
865 LiveMaskReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000866 MachineInstr *MI = BuildMI(Entry, EntryMI, DebugLoc(),
867 TII->get(AMDGPU::COPY), LiveMaskReg)
868 .addReg(AMDGPU::EXEC);
869 LIS->InsertMachineInstrInMaps(*MI);
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000870 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000871
Connor Abbott92638ab2017-08-04 18:36:52 +0000872 lowerLiveMaskQueries(LiveMaskReg);
873
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000874 if (GlobalFlags == StateWQM) {
875 // For a shader that needs only WQM, we can just set it once.
876 BuildMI(Entry, EntryMI, DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
877 AMDGPU::EXEC)
878 .addReg(AMDGPU::EXEC);
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000879
Connor Abbott8c217d02017-08-04 18:36:49 +0000880 lowerCopyInstrs();
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000881 // EntryMI may become invalid here
882 return true;
883 }
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000884 }
885
Nicolai Haehnle3bba6a82016-09-03 12:26:38 +0000886 DEBUG(printInfo());
887
Connor Abbott8c217d02017-08-04 18:36:49 +0000888 lowerCopyInstrs();
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000889
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000890 // Handle the general case
Matt Arsenault8dff86d2016-07-13 05:55:15 +0000891 for (auto BII : Blocks)
892 processBlock(*BII.first, LiveMaskReg, BII.first == &*MF.begin());
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000893
Nicolai Haehnlee58e0e32016-09-12 16:25:20 +0000894 // Physical registers like SCC aren't tracked by default anyway, so just
895 // removing the ranges we computed is the simplest option for maintaining
896 // the analysis results.
897 LIS->removeRegUnit(*MCRegUnitIterator(AMDGPU::SCC, TRI));
898
Nicolai Haehnle213e87f2016-03-21 20:28:33 +0000899 return true;
900}