xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/R600/SIFixSGPRLiveRanges.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- SIFixSGPRLiveRanges.cpp - Fix SGPR live ranges ----------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc /// \file
11*0a6a1f1dSLionel Sambuc /// SALU instructions ignore control flow, so we need to modify the live ranges
12*0a6a1f1dSLionel Sambuc /// of the registers they define in some cases.
13*0a6a1f1dSLionel Sambuc ///
14*0a6a1f1dSLionel Sambuc /// The main case we need to handle is when a def is used in one side of a
15*0a6a1f1dSLionel Sambuc /// branch and not another.  For example:
16*0a6a1f1dSLionel Sambuc ///
17*0a6a1f1dSLionel Sambuc /// %def
18*0a6a1f1dSLionel Sambuc /// IF
19*0a6a1f1dSLionel Sambuc ///   ...
20*0a6a1f1dSLionel Sambuc ///   ...
21*0a6a1f1dSLionel Sambuc /// ELSE
22*0a6a1f1dSLionel Sambuc ///   %use
23*0a6a1f1dSLionel Sambuc ///   ...
24*0a6a1f1dSLionel Sambuc /// ENDIF
25*0a6a1f1dSLionel Sambuc ///
26*0a6a1f1dSLionel Sambuc /// Here we need the register allocator to avoid assigning any of the defs
27*0a6a1f1dSLionel Sambuc /// inside of the IF to the same register as %def.  In traditional live
28*0a6a1f1dSLionel Sambuc /// interval analysis %def is not live inside the IF branch, however, since
29*0a6a1f1dSLionel Sambuc /// SALU instructions inside of IF will be executed even if the branch is not
30*0a6a1f1dSLionel Sambuc /// taken, there is the chance that one of the instructions will overwrite the
31*0a6a1f1dSLionel Sambuc /// value of %def, so the use in ELSE will see the wrong value.
32*0a6a1f1dSLionel Sambuc ///
33*0a6a1f1dSLionel Sambuc /// The strategy we use for solving this is to add an extra use after the ENDIF:
34*0a6a1f1dSLionel Sambuc ///
35*0a6a1f1dSLionel Sambuc /// %def
36*0a6a1f1dSLionel Sambuc /// IF
37*0a6a1f1dSLionel Sambuc ///   ...
38*0a6a1f1dSLionel Sambuc ///   ...
39*0a6a1f1dSLionel Sambuc /// ELSE
40*0a6a1f1dSLionel Sambuc ///   %use
41*0a6a1f1dSLionel Sambuc ///   ...
42*0a6a1f1dSLionel Sambuc /// ENDIF
43*0a6a1f1dSLionel Sambuc /// %use
44*0a6a1f1dSLionel Sambuc ///
45*0a6a1f1dSLionel Sambuc /// Adding this use will make the def live thoughout the IF branch, which is
46*0a6a1f1dSLionel Sambuc /// what we want.
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc #include "AMDGPU.h"
49*0a6a1f1dSLionel Sambuc #include "SIInstrInfo.h"
50*0a6a1f1dSLionel Sambuc #include "SIRegisterInfo.h"
51*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/LiveIntervalAnalysis.h"
52*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineFunctionPass.h"
53*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
54*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachinePostDominators.h"
55*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
56*0a6a1f1dSLionel Sambuc #include "llvm/Support/Debug.h"
57*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetMachine.h"
58*0a6a1f1dSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc using namespace llvm;
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "si-fix-sgpr-live-ranges"
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc namespace {
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc class SIFixSGPRLiveRanges : public MachineFunctionPass {
66*0a6a1f1dSLionel Sambuc public:
67*0a6a1f1dSLionel Sambuc   static char ID;
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc public:
SIFixSGPRLiveRanges()70*0a6a1f1dSLionel Sambuc   SIFixSGPRLiveRanges() : MachineFunctionPass(ID) {
71*0a6a1f1dSLionel Sambuc     initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry());
72*0a6a1f1dSLionel Sambuc   }
73*0a6a1f1dSLionel Sambuc 
74*0a6a1f1dSLionel Sambuc   bool runOnMachineFunction(MachineFunction &MF) override;
75*0a6a1f1dSLionel Sambuc 
getPassName() const76*0a6a1f1dSLionel Sambuc   const char *getPassName() const override {
77*0a6a1f1dSLionel Sambuc     return "SI Fix SGPR live ranges";
78*0a6a1f1dSLionel Sambuc   }
79*0a6a1f1dSLionel Sambuc 
getAnalysisUsage(AnalysisUsage & AU) const80*0a6a1f1dSLionel Sambuc   void getAnalysisUsage(AnalysisUsage &AU) const override {
81*0a6a1f1dSLionel Sambuc     AU.addRequired<LiveIntervals>();
82*0a6a1f1dSLionel Sambuc     AU.addRequired<MachinePostDominatorTree>();
83*0a6a1f1dSLionel Sambuc     AU.setPreservesCFG();
84*0a6a1f1dSLionel Sambuc     MachineFunctionPass::getAnalysisUsage(AU);
85*0a6a1f1dSLionel Sambuc   }
86*0a6a1f1dSLionel Sambuc };
87*0a6a1f1dSLionel Sambuc 
88*0a6a1f1dSLionel Sambuc } // End anonymous namespace.
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE,
91*0a6a1f1dSLionel Sambuc                       "SI Fix SGPR Live Ranges", false, false)
92*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
93*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
94*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_END(SIFixSGPRLiveRanges, DEBUG_TYPE,
95*0a6a1f1dSLionel Sambuc                     "SI Fix SGPR Live Ranges", false, false)
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc char SIFixSGPRLiveRanges::ID = 0;
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc char &llvm::SIFixSGPRLiveRangesID = SIFixSGPRLiveRanges::ID;
100*0a6a1f1dSLionel Sambuc 
createSIFixSGPRLiveRangesPass()101*0a6a1f1dSLionel Sambuc FunctionPass *llvm::createSIFixSGPRLiveRangesPass() {
102*0a6a1f1dSLionel Sambuc   return new SIFixSGPRLiveRanges();
103*0a6a1f1dSLionel Sambuc }
104*0a6a1f1dSLionel Sambuc 
runOnMachineFunction(MachineFunction & MF)105*0a6a1f1dSLionel Sambuc bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) {
106*0a6a1f1dSLionel Sambuc   MachineRegisterInfo &MRI = MF.getRegInfo();
107*0a6a1f1dSLionel Sambuc   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
108*0a6a1f1dSLionel Sambuc   const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(
109*0a6a1f1dSLionel Sambuc       MF.getSubtarget().getRegisterInfo());
110*0a6a1f1dSLionel Sambuc   LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
111*0a6a1f1dSLionel Sambuc  MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>();
112*0a6a1f1dSLionel Sambuc   std::vector<std::pair<unsigned, LiveRange *>> SGPRLiveRanges;
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc   // First pass, collect all live intervals for SGPRs
115*0a6a1f1dSLionel Sambuc   for (const MachineBasicBlock &MBB : MF) {
116*0a6a1f1dSLionel Sambuc     for (const MachineInstr &MI : MBB) {
117*0a6a1f1dSLionel Sambuc       for (const MachineOperand &MO : MI.defs()) {
118*0a6a1f1dSLionel Sambuc         if (MO.isImplicit())
119*0a6a1f1dSLionel Sambuc           continue;
120*0a6a1f1dSLionel Sambuc         unsigned Def = MO.getReg();
121*0a6a1f1dSLionel Sambuc         if (TargetRegisterInfo::isVirtualRegister(Def)) {
122*0a6a1f1dSLionel Sambuc           if (TRI->isSGPRClass(MRI.getRegClass(Def)))
123*0a6a1f1dSLionel Sambuc             SGPRLiveRanges.push_back(
124*0a6a1f1dSLionel Sambuc                 std::make_pair(Def, &LIS->getInterval(Def)));
125*0a6a1f1dSLionel Sambuc         } else if (TRI->isSGPRClass(TRI->getPhysRegClass(Def))) {
126*0a6a1f1dSLionel Sambuc             SGPRLiveRanges.push_back(
127*0a6a1f1dSLionel Sambuc                 std::make_pair(Def, &LIS->getRegUnit(Def)));
128*0a6a1f1dSLionel Sambuc         }
129*0a6a1f1dSLionel Sambuc       }
130*0a6a1f1dSLionel Sambuc     }
131*0a6a1f1dSLionel Sambuc   }
132*0a6a1f1dSLionel Sambuc 
133*0a6a1f1dSLionel Sambuc   // Second pass fix the intervals
134*0a6a1f1dSLionel Sambuc   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
135*0a6a1f1dSLionel Sambuc                                                   BI != BE; ++BI) {
136*0a6a1f1dSLionel Sambuc     MachineBasicBlock &MBB = *BI;
137*0a6a1f1dSLionel Sambuc     if (MBB.succ_size() < 2)
138*0a6a1f1dSLionel Sambuc       continue;
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc     // We have structured control flow, so number of succesors should be two.
141*0a6a1f1dSLionel Sambuc     assert(MBB.succ_size() == 2);
142*0a6a1f1dSLionel Sambuc     MachineBasicBlock *SuccA = *MBB.succ_begin();
143*0a6a1f1dSLionel Sambuc     MachineBasicBlock *SuccB = *(++MBB.succ_begin());
144*0a6a1f1dSLionel Sambuc     MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB);
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc     if (!NCD)
147*0a6a1f1dSLionel Sambuc       continue;
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc     MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator();
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc     if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) {
152*0a6a1f1dSLionel Sambuc       assert(NCD->succ_size() == 2);
153*0a6a1f1dSLionel Sambuc       // We want to make sure we insert the Use after the ENDIF, not after
154*0a6a1f1dSLionel Sambuc       // the ELSE.
155*0a6a1f1dSLionel Sambuc       NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(),
156*0a6a1f1dSLionel Sambuc                                             *(++NCD->succ_begin()));
157*0a6a1f1dSLionel Sambuc     }
158*0a6a1f1dSLionel Sambuc     assert(SuccA && SuccB);
159*0a6a1f1dSLionel Sambuc     for (std::pair<unsigned, LiveRange*> RegLR : SGPRLiveRanges) {
160*0a6a1f1dSLionel Sambuc       unsigned Reg = RegLR.first;
161*0a6a1f1dSLionel Sambuc       LiveRange *LR = RegLR.second;
162*0a6a1f1dSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc       // FIXME: We could be smarter here.  If the register is Live-In to
164*0a6a1f1dSLionel Sambuc       // one block, but the other doesn't have any SGPR defs, then there
165*0a6a1f1dSLionel Sambuc       // won't be a conflict.  Also, if the branch decision is based on
166*0a6a1f1dSLionel Sambuc       // a value in an SGPR, then there will be no conflict.
167*0a6a1f1dSLionel Sambuc       bool LiveInToA = LIS->isLiveInToMBB(*LR, SuccA);
168*0a6a1f1dSLionel Sambuc       bool LiveInToB = LIS->isLiveInToMBB(*LR, SuccB);
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc       if ((!LiveInToA && !LiveInToB) ||
171*0a6a1f1dSLionel Sambuc           (LiveInToA && LiveInToB))
172*0a6a1f1dSLionel Sambuc         continue;
173*0a6a1f1dSLionel Sambuc 
174*0a6a1f1dSLionel Sambuc       // This interval is live in to one successor, but not the other, so
175*0a6a1f1dSLionel Sambuc       // we need to update its range so it is live in to both.
176*0a6a1f1dSLionel Sambuc       DEBUG(dbgs() << "Possible SGPR conflict detected " <<  " in " << *LR <<
177*0a6a1f1dSLionel Sambuc                       " BB#" << SuccA->getNumber() << ", BB#" <<
178*0a6a1f1dSLionel Sambuc                       SuccB->getNumber() <<
179*0a6a1f1dSLionel Sambuc                       " with NCD = " << NCD->getNumber() << '\n');
180*0a6a1f1dSLionel Sambuc 
181*0a6a1f1dSLionel Sambuc       // FIXME: Need to figure out how to update LiveRange here so this pass
182*0a6a1f1dSLionel Sambuc       // will be able to preserve LiveInterval analysis.
183*0a6a1f1dSLionel Sambuc       BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(),
184*0a6a1f1dSLionel Sambuc               TII->get(AMDGPU::SGPR_USE))
185*0a6a1f1dSLionel Sambuc               .addReg(Reg, RegState::Implicit);
186*0a6a1f1dSLionel Sambuc       DEBUG(NCD->getFirstNonPHI()->dump());
187*0a6a1f1dSLionel Sambuc     }
188*0a6a1f1dSLionel Sambuc   }
189*0a6a1f1dSLionel Sambuc 
190*0a6a1f1dSLionel Sambuc   return false;
191*0a6a1f1dSLionel Sambuc }
192