blob: 016e2c8c886a03731b8054fb64cc865a12e4eac1 [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"
50#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Matt Arsenault0259a7a2015-08-15 00:12:37 +000051#include "llvm/CodeGen/LiveVariables.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000052#include "llvm/CodeGen/MachineFunctionPass.h"
Tom Stellard60024a02014-09-24 01:33:24 +000053#include "llvm/CodeGen/MachineInstrBuilder.h"
54#include "llvm/CodeGen/MachinePostDominators.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000055#include "llvm/CodeGen/MachineRegisterInfo.h"
56#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000057#include "llvm/Support/raw_ostream.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000058#include "llvm/Target/TargetMachine.h"
59
60using namespace llvm;
61
62#define DEBUG_TYPE "si-fix-sgpr-live-ranges"
63
64namespace {
65
66class SIFixSGPRLiveRanges : public MachineFunctionPass {
67public:
68 static char ID;
69
70public:
71 SIFixSGPRLiveRanges() : MachineFunctionPass(ID) {
72 initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry());
73 }
74
Craig Topperfd38cbe2014-08-30 16:48:34 +000075 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardb2de94e2014-07-02 20:53:48 +000076
Craig Topperfd38cbe2014-08-30 16:48:34 +000077 const char *getPassName() const override {
Tom Stellardb2de94e2014-07-02 20:53:48 +000078 return "SI Fix SGPR live ranges";
79 }
80
Craig Topperfd38cbe2014-08-30 16:48:34 +000081 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellardb2de94e2014-07-02 20:53:48 +000082 AU.addRequired<LiveIntervals>();
Tom Stellard60024a02014-09-24 01:33:24 +000083 AU.addRequired<MachinePostDominatorTree>();
Tom Stellardb2de94e2014-07-02 20:53:48 +000084 AU.setPreservesCFG();
Matt Arsenault670ba462015-08-15 00:12:35 +000085
86 //AU.addPreserved<SlotIndexes>(); // XXX - This might be OK
87 AU.addPreserved<LiveIntervals>();
88
Tom Stellardb2de94e2014-07-02 20:53:48 +000089 MachineFunctionPass::getAnalysisUsage(AU);
90 }
91};
92
93} // End anonymous namespace.
94
95INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE,
96 "SI Fix SGPR Live Ranges", false, false)
97INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
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
111bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) {
112 MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard60024a02014-09-24 01:33:24 +0000113 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
114 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(
115 MF.getSubtarget().getRegisterInfo());
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000116
117 MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>();
Tom Stellard60024a02014-09-24 01:33:24 +0000118 std::vector<std::pair<unsigned, LiveRange *>> SGPRLiveRanges;
Tom Stellardb2de94e2014-07-02 20:53:48 +0000119
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000120 LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
121 LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>();
122
Tom Stellard60024a02014-09-24 01:33:24 +0000123 // First pass, collect all live intervals for SGPRs
124 for (const MachineBasicBlock &MBB : MF) {
125 for (const MachineInstr &MI : MBB) {
126 for (const MachineOperand &MO : MI.defs()) {
127 if (MO.isImplicit())
Tom Stellardb2de94e2014-07-02 20:53:48 +0000128 continue;
Tom Stellard60024a02014-09-24 01:33:24 +0000129 unsigned Def = MO.getReg();
130 if (TargetRegisterInfo::isVirtualRegister(Def)) {
131 if (TRI->isSGPRClass(MRI.getRegClass(Def)))
132 SGPRLiveRanges.push_back(
133 std::make_pair(Def, &LIS->getInterval(Def)));
134 } else if (TRI->isSGPRClass(TRI->getPhysRegClass(Def))) {
135 SGPRLiveRanges.push_back(
136 std::make_pair(Def, &LIS->getRegUnit(Def)));
Tom Stellardb2de94e2014-07-02 20:53:48 +0000137 }
138 }
139 }
140 }
141
Tom Stellard60024a02014-09-24 01:33:24 +0000142 // Second pass fix the intervals
143 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
144 BI != BE; ++BI) {
145 MachineBasicBlock &MBB = *BI;
146 if (MBB.succ_size() < 2)
147 continue;
148
Matt Arsenault4275c292015-08-15 00:12:30 +0000149 // We have structured control flow, so the number of successors should be
150 // two.
Tom Stellard60024a02014-09-24 01:33:24 +0000151 assert(MBB.succ_size() == 2);
152 MachineBasicBlock *SuccA = *MBB.succ_begin();
153 MachineBasicBlock *SuccB = *(++MBB.succ_begin());
154 MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB);
155
156 if (!NCD)
157 continue;
158
159 MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator();
160
161 if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) {
162 assert(NCD->succ_size() == 2);
163 // We want to make sure we insert the Use after the ENDIF, not after
164 // the ELSE.
165 NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(),
166 *(++NCD->succ_begin()));
167 }
Matt Arsenaultb7523322015-08-15 00:12:32 +0000168
Tom Stellard60024a02014-09-24 01:33:24 +0000169 for (std::pair<unsigned, LiveRange*> RegLR : SGPRLiveRanges) {
170 unsigned Reg = RegLR.first;
171 LiveRange *LR = RegLR.second;
172
Matt Arsenault4275c292015-08-15 00:12:30 +0000173 // FIXME: We could be smarter here. If the register is Live-In to one
174 // block, but the other doesn't have any SGPR defs, then there won't be a
175 // conflict. Also, if the branch condition is uniform then there will be
176 // no conflict.
Tom Stellard60024a02014-09-24 01:33:24 +0000177 bool LiveInToA = LIS->isLiveInToMBB(*LR, SuccA);
178 bool LiveInToB = LIS->isLiveInToMBB(*LR, SuccB);
179
180 if ((!LiveInToA && !LiveInToB) ||
181 (LiveInToA && LiveInToB))
182 continue;
183
184 // This interval is live in to one successor, but not the other, so
185 // we need to update its range so it is live in to both.
186 DEBUG(dbgs() << "Possible SGPR conflict detected " << " in " << *LR <<
187 " BB#" << SuccA->getNumber() << ", BB#" <<
188 SuccB->getNumber() <<
189 " with NCD = " << NCD->getNumber() << '\n');
190
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000191 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
192 "Not expecting to extend live range of physreg");
193
Tom Stellard60024a02014-09-24 01:33:24 +0000194 // FIXME: Need to figure out how to update LiveRange here so this pass
195 // will be able to preserve LiveInterval analysis.
Matt Arsenault670ba462015-08-15 00:12:35 +0000196 MachineInstr *NCDSGPRUse =
197 BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(),
198 TII->get(AMDGPU::SGPR_USE))
199 .addReg(Reg, RegState::Implicit);
200
201 SlotIndex SI = LIS->InsertMachineInstrInMaps(NCDSGPRUse);
202 LIS->extendToIndices(*LR, SI.getRegSlot());
203
Matt Arsenault0259a7a2015-08-15 00:12:37 +0000204 if (LV) {
205 // TODO: This won't work post-SSA
206 LV->HandleVirtRegUse(Reg, NCD, NCDSGPRUse);
207 }
208
Matt Arsenault670ba462015-08-15 00:12:35 +0000209 DEBUG(NCDSGPRUse->dump());
Tom Stellard60024a02014-09-24 01:33:24 +0000210 }
211 }
212
Tom Stellardb2de94e2014-07-02 20:53:48 +0000213 return false;
214}