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