blob: 4de01707bb6cef8e3240e29f0e95803d76a0f245 [file] [log] [blame]
Tom Stellardb2de94e2014-07-02 20:53:48 +00001//===-- SIFixSGPRLiveRanges.cpp - Fix SGPR live ranges ----------------------===//
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//
Matt Arsenault4275c292015-08-15 00:12:30 +000010/// \file SALU instructions ignore the execution mask, so we need to modify the
11/// live ranges of the registers they define in some cases.
Tom Stellardb2de94e2014-07-02 20:53:48 +000012///
Tom Stellard60024a02014-09-24 01:33:24 +000013/// The main case we need to handle is when a def is used in one side of a
14/// branch and not another. For example:
15///
16/// %def
17/// IF
18/// ...
19/// ...
20/// ELSE
21/// %use
22/// ...
23/// ENDIF
24///
25/// Here we need the register allocator to avoid assigning any of the defs
26/// inside of the IF to the same register as %def. In traditional live
27/// interval analysis %def is not live inside the IF branch, however, since
28/// SALU instructions inside of IF will be executed even if the branch is not
29/// taken, there is the chance that one of the instructions will overwrite the
30/// value of %def, so the use in ELSE will see the wrong value.
31///
32/// The strategy we use for solving this is to add an extra use after the ENDIF:
33///
34/// %def
35/// IF
36/// ...
37/// ...
38/// ELSE
39/// %use
40/// ...
41/// ENDIF
42/// %use
43///
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000044/// Adding this use will make the def live throughout the IF branch, which is
Tom Stellard60024a02014-09-24 01:33:24 +000045/// what we want.
Tom Stellardb2de94e2014-07-02 20:53:48 +000046
47#include "AMDGPU.h"
Tom Stellard60024a02014-09-24 01:33:24 +000048#include "SIInstrInfo.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000049#include "SIRegisterInfo.h"
Matt Arsenault33010102015-08-22 00:43:38 +000050#include "llvm/ADT/DepthFirstIterator.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000051#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Matt Arsenault0259a7a2015-08-15 00:12:37 +000052#include "llvm/CodeGen/LiveVariables.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000053#include "llvm/CodeGen/MachineFunctionPass.h"
Tom Stellard60024a02014-09-24 01:33:24 +000054#include "llvm/CodeGen/MachineInstrBuilder.h"
55#include "llvm/CodeGen/MachinePostDominators.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000056#include "llvm/CodeGen/MachineRegisterInfo.h"
57#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000058#include "llvm/Support/raw_ostream.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000059#include "llvm/Target/TargetMachine.h"
60
61using namespace llvm;
62
63#define DEBUG_TYPE "si-fix-sgpr-live-ranges"
64
65namespace {
66
67class SIFixSGPRLiveRanges : public MachineFunctionPass {
68public:
69 static char ID;
70
71public:
72 SIFixSGPRLiveRanges() : MachineFunctionPass(ID) {
73 initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry());
74 }
75
Craig Topperfd38cbe2014-08-30 16:48:34 +000076 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardb2de94e2014-07-02 20:53:48 +000077
Craig Topperfd38cbe2014-08-30 16:48:34 +000078 const char *getPassName() const override {
Tom Stellardb2de94e2014-07-02 20:53:48 +000079 return "SI Fix SGPR live ranges";
80 }
81
Craig Topperfd38cbe2014-08-30 16:48:34 +000082 void getAnalysisUsage(AnalysisUsage &AU) const override {
Matt Arsenaultb87fc222015-10-01 22:10:03 +000083 AU.addRequired<LiveVariables>();
84 AU.addPreserved<LiveVariables>();
Matt Arsenault670ba462015-08-15 00:12:35 +000085
Matt Arsenaultb87fc222015-10-01 22:10:03 +000086 AU.addRequired<MachinePostDominatorTree>();
87 AU.addPreserved<MachinePostDominatorTree>();
88 AU.setPreservesCFG();
Matt Arsenault670ba462015-08-15 00:12:35 +000089
Tom Stellardb2de94e2014-07-02 20:53:48 +000090 MachineFunctionPass::getAnalysisUsage(AU);
91 }
92};
93
94} // End anonymous namespace.
95
96INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE,
97 "SI Fix SGPR Live Ranges", false, false)
Matt Arsenault0259a7a2015-08-15 00:12:37 +000098INITIALIZE_PASS_DEPENDENCY(LiveVariables)
Tom Stellard60024a02014-09-24 01:33:24 +000099INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
Tom Stellardb2de94e2014-07-02 20:53:48 +0000100INITIALIZE_PASS_END(SIFixSGPRLiveRanges, DEBUG_TYPE,
101 "SI Fix SGPR Live Ranges", false, false)
102
103char SIFixSGPRLiveRanges::ID = 0;
104
105char &llvm::SIFixSGPRLiveRangesID = SIFixSGPRLiveRanges::ID;
106
107FunctionPass *llvm::createSIFixSGPRLiveRangesPass() {
108 return new SIFixSGPRLiveRanges();
109}
110
Tom Stellardbc4497b2016-02-12 23:45:29 +0000111static bool hasOnlyScalarBr(const MachineBasicBlock *MBB,
112 const SIInstrInfo *TII) {
113 for (MachineBasicBlock::const_iterator I = MBB->getFirstTerminator(),
114 E = MBB->end(); I != E; ++I) {
115 if (!TII->isSOPP(*I))
116 return false;
117 }
118 return true;
119}
120
Tom Stellardb2de94e2014-07-02 20:53:48 +0000121bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) {
122 MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellardbc4497b2016-02-12 23:45:29 +0000123 const SIInstrInfo *TII =
124 static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
Tom Stellard60024a02014-09-24 01:33:24 +0000125 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(
126 MF.getSubtarget().getRegisterInfo());
Matt Arsenault602a16d2015-08-26 19:12:03 +0000127 bool MadeChange = false;
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000128
129 MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>();
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000130 SmallVector<unsigned, 16> SGPRLiveRanges;
Tom Stellardb2de94e2014-07-02 20:53:48 +0000131
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000132 LiveVariables *LV = &getAnalysis<LiveVariables>();
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000133 MachineBasicBlock *Entry = &MF.front();
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000134
Matt Arsenault33010102015-08-22 00:43:38 +0000135 // Use a depth first order so that in SSA, we encounter all defs before
136 // uses. Once the defs of the block have been found, attempt to insert
137 // SGPR_USE instructions in successor blocks if required.
138 for (MachineBasicBlock *MBB : depth_first(Entry)) {
139 for (const MachineInstr &MI : *MBB) {
Tom Stellard60024a02014-09-24 01:33:24 +0000140 for (const MachineOperand &MO : MI.defs()) {
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000141 // We should never see a live out def of a physical register, so we also
142 // do not need to worry about implicit_defs().
Tom Stellard60024a02014-09-24 01:33:24 +0000143 unsigned Def = MO.getReg();
144 if (TargetRegisterInfo::isVirtualRegister(Def)) {
Matt Arsenault588732b2015-08-15 02:58:49 +0000145 if (TRI->isSGPRClass(MRI.getRegClass(Def))) {
146 // Only consider defs that are live outs. We don't care about def /
147 // use within the same block.
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000148
149 // LiveVariables does not consider registers that are only used in a
150 // phi in a sucessor block as live out, unlike LiveIntervals.
151 //
152 // This is OK because SIFixSGPRCopies replaced any SGPR phis with
153 // VGPRs.
154 if (LV->isLiveOut(Def, *MBB))
155 SGPRLiveRanges.push_back(Def);
Matt Arsenault588732b2015-08-15 02:58:49 +0000156 }
Tom Stellardb2de94e2014-07-02 20:53:48 +0000157 }
158 }
159 }
Tom Stellardb2de94e2014-07-02 20:53:48 +0000160
Tom Stellardbc4497b2016-02-12 23:45:29 +0000161 if (MBB->succ_size() < 2 || hasOnlyScalarBr(MBB, TII))
Tom Stellard60024a02014-09-24 01:33:24 +0000162 continue;
163
Matt Arsenault4275c292015-08-15 00:12:30 +0000164 // We have structured control flow, so the number of successors should be
165 // two.
Matt Arsenault33010102015-08-22 00:43:38 +0000166 assert(MBB->succ_size() == 2);
167 MachineBasicBlock *SuccA = *MBB->succ_begin();
168 MachineBasicBlock *SuccB = *(++MBB->succ_begin());
Tom Stellard60024a02014-09-24 01:33:24 +0000169 MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB);
170
171 if (!NCD)
172 continue;
173
174 MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator();
175
176 if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) {
177 assert(NCD->succ_size() == 2);
178 // We want to make sure we insert the Use after the ENDIF, not after
179 // the ELSE.
180 NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(),
181 *(++NCD->succ_begin()));
182 }
Matt Arsenaultb7523322015-08-15 00:12:32 +0000183
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000184 for (unsigned Reg : SGPRLiveRanges) {
Matt Arsenault4275c292015-08-15 00:12:30 +0000185 // FIXME: We could be smarter here. If the register is Live-In to one
186 // block, but the other doesn't have any SGPR defs, then there won't be a
187 // conflict. Also, if the branch condition is uniform then there will be
188 // no conflict.
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000189 bool LiveInToA = LV->isLiveIn(Reg, *SuccA);
190 bool LiveInToB = LV->isLiveIn(Reg, *SuccB);
Tom Stellard60024a02014-09-24 01:33:24 +0000191
Matt Arsenaultaba29d62015-08-22 00:19:25 +0000192 if (!LiveInToA && !LiveInToB) {
193 DEBUG(dbgs() << PrintReg(Reg, TRI, 0)
194 << " is live into neither successor\n");
Tom Stellard60024a02014-09-24 01:33:24 +0000195 continue;
Matt Arsenaultaba29d62015-08-22 00:19:25 +0000196 }
197
198 if (LiveInToA && LiveInToB) {
199 DEBUG(dbgs() << PrintReg(Reg, TRI, 0)
200 << " is live into both successors\n");
201 continue;
202 }
Tom Stellard60024a02014-09-24 01:33:24 +0000203
204 // This interval is live in to one successor, but not the other, so
205 // we need to update its range so it is live in to both.
Matt Arsenaultaba29d62015-08-22 00:19:25 +0000206 DEBUG(dbgs() << "Possible SGPR conflict detected for "
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000207 << PrintReg(Reg, TRI, 0)
208 << " BB#" << SuccA->getNumber()
209 << ", BB#" << SuccB->getNumber()
Matt Arsenaultaba29d62015-08-22 00:19:25 +0000210 << " with NCD = BB#" << NCD->getNumber() << '\n');
Tom Stellard60024a02014-09-24 01:33:24 +0000211
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000212 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
213 "Not expecting to extend live range of physreg");
214
Tom Stellard60024a02014-09-24 01:33:24 +0000215 // FIXME: Need to figure out how to update LiveRange here so this pass
216 // will be able to preserve LiveInterval analysis.
Matt Arsenault670ba462015-08-15 00:12:35 +0000217 MachineInstr *NCDSGPRUse =
218 BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(),
219 TII->get(AMDGPU::SGPR_USE))
220 .addReg(Reg, RegState::Implicit);
221
Matt Arsenault602a16d2015-08-26 19:12:03 +0000222 MadeChange = true;
Matt Arsenaultb87fc222015-10-01 22:10:03 +0000223 LV->HandleVirtRegUse(Reg, NCD, NCDSGPRUse);
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000224
Matt Arsenault670ba462015-08-15 00:12:35 +0000225 DEBUG(NCDSGPRUse->dump());
Tom Stellard60024a02014-09-24 01:33:24 +0000226 }
227 }
228
Matt Arsenault602a16d2015-08-26 19:12:03 +0000229 return MadeChange;
Tom Stellardb2de94e2014-07-02 20:53:48 +0000230}