blob: b469898bef514af31153dfcb7460dc6b821297b8 [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"
51#include "llvm/CodeGen/MachineFunctionPass.h"
Tom Stellard60024a02014-09-24 01:33:24 +000052#include "llvm/CodeGen/MachineInstrBuilder.h"
53#include "llvm/CodeGen/MachinePostDominators.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000054#include "llvm/CodeGen/MachineRegisterInfo.h"
55#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000056#include "llvm/Support/raw_ostream.h"
Tom Stellardb2de94e2014-07-02 20:53:48 +000057#include "llvm/Target/TargetMachine.h"
58
59using namespace llvm;
60
61#define DEBUG_TYPE "si-fix-sgpr-live-ranges"
62
63namespace {
64
65class SIFixSGPRLiveRanges : public MachineFunctionPass {
66public:
67 static char ID;
68
69public:
70 SIFixSGPRLiveRanges() : MachineFunctionPass(ID) {
71 initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry());
72 }
73
Craig Topperfd38cbe2014-08-30 16:48:34 +000074 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardb2de94e2014-07-02 20:53:48 +000075
Craig Topperfd38cbe2014-08-30 16:48:34 +000076 const char *getPassName() const override {
Tom Stellardb2de94e2014-07-02 20:53:48 +000077 return "SI Fix SGPR live ranges";
78 }
79
Craig Topperfd38cbe2014-08-30 16:48:34 +000080 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellardb2de94e2014-07-02 20:53:48 +000081 AU.addRequired<LiveIntervals>();
Tom Stellard60024a02014-09-24 01:33:24 +000082 AU.addRequired<MachinePostDominatorTree>();
Tom Stellardb2de94e2014-07-02 20:53:48 +000083 AU.setPreservesCFG();
84 MachineFunctionPass::getAnalysisUsage(AU);
85 }
86};
87
88} // End anonymous namespace.
89
90INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE,
91 "SI Fix SGPR Live Ranges", false, false)
92INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Tom Stellard60024a02014-09-24 01:33:24 +000093INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
Tom Stellardb2de94e2014-07-02 20:53:48 +000094INITIALIZE_PASS_END(SIFixSGPRLiveRanges, DEBUG_TYPE,
95 "SI Fix SGPR Live Ranges", false, false)
96
97char SIFixSGPRLiveRanges::ID = 0;
98
99char &llvm::SIFixSGPRLiveRangesID = SIFixSGPRLiveRanges::ID;
100
101FunctionPass *llvm::createSIFixSGPRLiveRangesPass() {
102 return new SIFixSGPRLiveRanges();
103}
104
105bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) {
106 MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard60024a02014-09-24 01:33:24 +0000107 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
108 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(
109 MF.getSubtarget().getRegisterInfo());
Tom Stellardb2de94e2014-07-02 20:53:48 +0000110 LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
Tom Stellard60024a02014-09-24 01:33:24 +0000111 MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>();
112 std::vector<std::pair<unsigned, LiveRange *>> SGPRLiveRanges;
Tom Stellardb2de94e2014-07-02 20:53:48 +0000113
Tom Stellard60024a02014-09-24 01:33:24 +0000114 // First pass, collect all live intervals for SGPRs
115 for (const MachineBasicBlock &MBB : MF) {
116 for (const MachineInstr &MI : MBB) {
117 for (const MachineOperand &MO : MI.defs()) {
118 if (MO.isImplicit())
Tom Stellardb2de94e2014-07-02 20:53:48 +0000119 continue;
Tom Stellard60024a02014-09-24 01:33:24 +0000120 unsigned Def = MO.getReg();
121 if (TargetRegisterInfo::isVirtualRegister(Def)) {
122 if (TRI->isSGPRClass(MRI.getRegClass(Def)))
123 SGPRLiveRanges.push_back(
124 std::make_pair(Def, &LIS->getInterval(Def)));
125 } else if (TRI->isSGPRClass(TRI->getPhysRegClass(Def))) {
126 SGPRLiveRanges.push_back(
127 std::make_pair(Def, &LIS->getRegUnit(Def)));
Tom Stellardb2de94e2014-07-02 20:53:48 +0000128 }
129 }
130 }
131 }
132
Tom Stellard60024a02014-09-24 01:33:24 +0000133 // Second pass fix the intervals
134 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
135 BI != BE; ++BI) {
136 MachineBasicBlock &MBB = *BI;
137 if (MBB.succ_size() < 2)
138 continue;
139
Matt Arsenault4275c292015-08-15 00:12:30 +0000140 // We have structured control flow, so the number of successors should be
141 // two.
Tom Stellard60024a02014-09-24 01:33:24 +0000142 assert(MBB.succ_size() == 2);
143 MachineBasicBlock *SuccA = *MBB.succ_begin();
144 MachineBasicBlock *SuccB = *(++MBB.succ_begin());
145 MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB);
146
147 if (!NCD)
148 continue;
149
150 MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator();
151
152 if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) {
153 assert(NCD->succ_size() == 2);
154 // We want to make sure we insert the Use after the ENDIF, not after
155 // the ELSE.
156 NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(),
157 *(++NCD->succ_begin()));
158 }
Matt Arsenaultb7523322015-08-15 00:12:32 +0000159
Tom Stellard60024a02014-09-24 01:33:24 +0000160 for (std::pair<unsigned, LiveRange*> RegLR : SGPRLiveRanges) {
161 unsigned Reg = RegLR.first;
162 LiveRange *LR = RegLR.second;
163
Matt Arsenault4275c292015-08-15 00:12:30 +0000164 // FIXME: We could be smarter here. If the register is Live-In to one
165 // block, but the other doesn't have any SGPR defs, then there won't be a
166 // conflict. Also, if the branch condition is uniform then there will be
167 // no conflict.
Tom Stellard60024a02014-09-24 01:33:24 +0000168 bool LiveInToA = LIS->isLiveInToMBB(*LR, SuccA);
169 bool LiveInToB = LIS->isLiveInToMBB(*LR, SuccB);
170
171 if ((!LiveInToA && !LiveInToB) ||
172 (LiveInToA && LiveInToB))
173 continue;
174
175 // This interval is live in to one successor, but not the other, so
176 // we need to update its range so it is live in to both.
177 DEBUG(dbgs() << "Possible SGPR conflict detected " << " in " << *LR <<
178 " BB#" << SuccA->getNumber() << ", BB#" <<
179 SuccB->getNumber() <<
180 " with NCD = " << NCD->getNumber() << '\n');
181
182 // FIXME: Need to figure out how to update LiveRange here so this pass
183 // will be able to preserve LiveInterval analysis.
184 BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(),
185 TII->get(AMDGPU::SGPR_USE))
186 .addReg(Reg, RegState::Implicit);
187 DEBUG(NCD->getFirstNonPHI()->dump());
188 }
189 }
190
Tom Stellardb2de94e2014-07-02 20:53:48 +0000191 return false;
192}