xref: /aosp_15_r20/external/llvm/lib/CodeGen/LiveVariables.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the LiveVariable analysis pass.  For each machine
11*9880d681SAndroid Build Coastguard Worker // instruction in the function, this pass calculates the set of registers that
12*9880d681SAndroid Build Coastguard Worker // are immediately dead after the instruction (i.e., the instruction calculates
13*9880d681SAndroid Build Coastguard Worker // the value, but it is never used) and the set of registers that are used by
14*9880d681SAndroid Build Coastguard Worker // the instruction, but are never used after the instruction (i.e., they are
15*9880d681SAndroid Build Coastguard Worker // killed).
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // This class computes live variables using a sparse implementation based on
18*9880d681SAndroid Build Coastguard Worker // the machine code SSA form.  This class computes live variable information for
19*9880d681SAndroid Build Coastguard Worker // each virtual and _register allocatable_ physical register in a function.  It
20*9880d681SAndroid Build Coastguard Worker // uses the dominance properties of SSA form to efficiently compute live
21*9880d681SAndroid Build Coastguard Worker // variables for virtual registers, and assumes that physical registers are only
22*9880d681SAndroid Build Coastguard Worker // live within a single basic block (allowing it to do a single local analysis
23*9880d681SAndroid Build Coastguard Worker // to resolve physical register lifetimes in each basic block).  If a physical
24*9880d681SAndroid Build Coastguard Worker // register is not register allocatable, it is not tracked.  This is useful for
25*9880d681SAndroid Build Coastguard Worker // things like the stack pointer and condition codes.
26*9880d681SAndroid Build Coastguard Worker //
27*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveVariables.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DepthFirstIterator.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
41*9880d681SAndroid Build Coastguard Worker #include <algorithm>
42*9880d681SAndroid Build Coastguard Worker using namespace llvm;
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker char LiveVariables::ID = 0;
45*9880d681SAndroid Build Coastguard Worker char &llvm::LiveVariablesID = LiveVariables::ID;
46*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(LiveVariables, "livevars",
47*9880d681SAndroid Build Coastguard Worker                 "Live Variable Analysis", false, false)
INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)48*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)
49*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(LiveVariables, "livevars",
50*9880d681SAndroid Build Coastguard Worker                 "Live Variable Analysis", false, false)
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
54*9880d681SAndroid Build Coastguard Worker   AU.addRequiredID(UnreachableMachineBlockElimID);
55*9880d681SAndroid Build Coastguard Worker   AU.setPreservesAll();
56*9880d681SAndroid Build Coastguard Worker   MachineFunctionPass::getAnalysisUsage(AU);
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker MachineInstr *
findKill(const MachineBasicBlock * MBB) const60*9880d681SAndroid Build Coastguard Worker LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
61*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Kills.size(); i != e; ++i)
62*9880d681SAndroid Build Coastguard Worker     if (Kills[i]->getParent() == MBB)
63*9880d681SAndroid Build Coastguard Worker       return Kills[i];
64*9880d681SAndroid Build Coastguard Worker   return nullptr;
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker 
dump() const67*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void LiveVariables::VarInfo::dump() const {
68*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
69*9880d681SAndroid Build Coastguard Worker   dbgs() << "  Alive in blocks: ";
70*9880d681SAndroid Build Coastguard Worker   for (SparseBitVector<>::iterator I = AliveBlocks.begin(),
71*9880d681SAndroid Build Coastguard Worker            E = AliveBlocks.end(); I != E; ++I)
72*9880d681SAndroid Build Coastguard Worker     dbgs() << *I << ", ";
73*9880d681SAndroid Build Coastguard Worker   dbgs() << "\n  Killed by:";
74*9880d681SAndroid Build Coastguard Worker   if (Kills.empty())
75*9880d681SAndroid Build Coastguard Worker     dbgs() << " No instructions.\n";
76*9880d681SAndroid Build Coastguard Worker   else {
77*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = Kills.size(); i != e; ++i)
78*9880d681SAndroid Build Coastguard Worker       dbgs() << "\n    #" << i << ": " << *Kills[i];
79*9880d681SAndroid Build Coastguard Worker     dbgs() << "\n";
80*9880d681SAndroid Build Coastguard Worker   }
81*9880d681SAndroid Build Coastguard Worker #endif
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
getVarInfo(unsigned RegIdx)85*9880d681SAndroid Build Coastguard Worker LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
86*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
87*9880d681SAndroid Build Coastguard Worker          "getVarInfo: not a virtual register!");
88*9880d681SAndroid Build Coastguard Worker   VirtRegInfo.grow(RegIdx);
89*9880d681SAndroid Build Coastguard Worker   return VirtRegInfo[RegIdx];
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker 
MarkVirtRegAliveInBlock(VarInfo & VRInfo,MachineBasicBlock * DefBlock,MachineBasicBlock * MBB,std::vector<MachineBasicBlock * > & WorkList)92*9880d681SAndroid Build Coastguard Worker void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
93*9880d681SAndroid Build Coastguard Worker                                             MachineBasicBlock *DefBlock,
94*9880d681SAndroid Build Coastguard Worker                                             MachineBasicBlock *MBB,
95*9880d681SAndroid Build Coastguard Worker                                     std::vector<MachineBasicBlock*> &WorkList) {
96*9880d681SAndroid Build Coastguard Worker   unsigned BBNum = MBB->getNumber();
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker   // Check to see if this basic block is one of the killing blocks.  If so,
99*9880d681SAndroid Build Coastguard Worker   // remove it.
100*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
101*9880d681SAndroid Build Coastguard Worker     if (VRInfo.Kills[i]->getParent() == MBB) {
102*9880d681SAndroid Build Coastguard Worker       VRInfo.Kills.erase(VRInfo.Kills.begin()+i);  // Erase entry
103*9880d681SAndroid Build Coastguard Worker       break;
104*9880d681SAndroid Build Coastguard Worker     }
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   if (MBB == DefBlock) return;  // Terminate recursion
107*9880d681SAndroid Build Coastguard Worker 
108*9880d681SAndroid Build Coastguard Worker   if (VRInfo.AliveBlocks.test(BBNum))
109*9880d681SAndroid Build Coastguard Worker     return;  // We already know the block is live
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker   // Mark the variable known alive in this bb
112*9880d681SAndroid Build Coastguard Worker   VRInfo.AliveBlocks.set(BBNum);
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker   assert(MBB != &MF->front() && "Can't find reaching def for virtreg");
115*9880d681SAndroid Build Coastguard Worker   WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend());
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker 
MarkVirtRegAliveInBlock(VarInfo & VRInfo,MachineBasicBlock * DefBlock,MachineBasicBlock * MBB)118*9880d681SAndroid Build Coastguard Worker void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
119*9880d681SAndroid Build Coastguard Worker                                             MachineBasicBlock *DefBlock,
120*9880d681SAndroid Build Coastguard Worker                                             MachineBasicBlock *MBB) {
121*9880d681SAndroid Build Coastguard Worker   std::vector<MachineBasicBlock*> WorkList;
122*9880d681SAndroid Build Coastguard Worker   MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker   while (!WorkList.empty()) {
125*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *Pred = WorkList.back();
126*9880d681SAndroid Build Coastguard Worker     WorkList.pop_back();
127*9880d681SAndroid Build Coastguard Worker     MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
128*9880d681SAndroid Build Coastguard Worker   }
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker 
HandleVirtRegUse(unsigned reg,MachineBasicBlock * MBB,MachineInstr & MI)131*9880d681SAndroid Build Coastguard Worker void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
132*9880d681SAndroid Build Coastguard Worker                                      MachineInstr &MI) {
133*9880d681SAndroid Build Coastguard Worker   assert(MRI->getVRegDef(reg) && "Register use before def!");
134*9880d681SAndroid Build Coastguard Worker 
135*9880d681SAndroid Build Coastguard Worker   unsigned BBNum = MBB->getNumber();
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker   VarInfo& VRInfo = getVarInfo(reg);
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker   // Check to see if this basic block is already a kill block.
140*9880d681SAndroid Build Coastguard Worker   if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
141*9880d681SAndroid Build Coastguard Worker     // Yes, this register is killed in this basic block already. Increase the
142*9880d681SAndroid Build Coastguard Worker     // live range by updating the kill instruction.
143*9880d681SAndroid Build Coastguard Worker     VRInfo.Kills.back() = &MI;
144*9880d681SAndroid Build Coastguard Worker     return;
145*9880d681SAndroid Build Coastguard Worker   }
146*9880d681SAndroid Build Coastguard Worker 
147*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
148*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
149*9880d681SAndroid Build Coastguard Worker     assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
150*9880d681SAndroid Build Coastguard Worker #endif
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker   // This situation can occur:
153*9880d681SAndroid Build Coastguard Worker   //
154*9880d681SAndroid Build Coastguard Worker   //     ,------.
155*9880d681SAndroid Build Coastguard Worker   //     |      |
156*9880d681SAndroid Build Coastguard Worker   //     |      v
157*9880d681SAndroid Build Coastguard Worker   //     |   t2 = phi ... t1 ...
158*9880d681SAndroid Build Coastguard Worker   //     |      |
159*9880d681SAndroid Build Coastguard Worker   //     |      v
160*9880d681SAndroid Build Coastguard Worker   //     |   t1 = ...
161*9880d681SAndroid Build Coastguard Worker   //     |  ... = ... t1 ...
162*9880d681SAndroid Build Coastguard Worker   //     |      |
163*9880d681SAndroid Build Coastguard Worker   //     `------'
164*9880d681SAndroid Build Coastguard Worker   //
165*9880d681SAndroid Build Coastguard Worker   // where there is a use in a PHI node that's a predecessor to the defining
166*9880d681SAndroid Build Coastguard Worker   // block. We don't want to mark all predecessors as having the value "alive"
167*9880d681SAndroid Build Coastguard Worker   // in this case.
168*9880d681SAndroid Build Coastguard Worker   if (MBB == MRI->getVRegDef(reg)->getParent()) return;
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   // Add a new kill entry for this basic block. If this virtual register is
171*9880d681SAndroid Build Coastguard Worker   // already marked as alive in this basic block, that means it is alive in at
172*9880d681SAndroid Build Coastguard Worker   // least one of the successor blocks, it's not a kill.
173*9880d681SAndroid Build Coastguard Worker   if (!VRInfo.AliveBlocks.test(BBNum))
174*9880d681SAndroid Build Coastguard Worker     VRInfo.Kills.push_back(&MI);
175*9880d681SAndroid Build Coastguard Worker 
176*9880d681SAndroid Build Coastguard Worker   // Update all dominating blocks to mark them as "known live".
177*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
178*9880d681SAndroid Build Coastguard Worker          E = MBB->pred_end(); PI != E; ++PI)
179*9880d681SAndroid Build Coastguard Worker     MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
180*9880d681SAndroid Build Coastguard Worker }
181*9880d681SAndroid Build Coastguard Worker 
HandleVirtRegDef(unsigned Reg,MachineInstr & MI)182*9880d681SAndroid Build Coastguard Worker void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr &MI) {
183*9880d681SAndroid Build Coastguard Worker   VarInfo &VRInfo = getVarInfo(Reg);
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker   if (VRInfo.AliveBlocks.empty())
186*9880d681SAndroid Build Coastguard Worker     // If vr is not alive in any block, then defaults to dead.
187*9880d681SAndroid Build Coastguard Worker     VRInfo.Kills.push_back(&MI);
188*9880d681SAndroid Build Coastguard Worker }
189*9880d681SAndroid Build Coastguard Worker 
190*9880d681SAndroid Build Coastguard Worker /// FindLastPartialDef - Return the last partial def of the specified register.
191*9880d681SAndroid Build Coastguard Worker /// Also returns the sub-registers that're defined by the instruction.
FindLastPartialDef(unsigned Reg,SmallSet<unsigned,4> & PartDefRegs)192*9880d681SAndroid Build Coastguard Worker MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg,
193*9880d681SAndroid Build Coastguard Worker                                             SmallSet<unsigned,4> &PartDefRegs) {
194*9880d681SAndroid Build Coastguard Worker   unsigned LastDefReg = 0;
195*9880d681SAndroid Build Coastguard Worker   unsigned LastDefDist = 0;
196*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastDef = nullptr;
197*9880d681SAndroid Build Coastguard Worker   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
198*9880d681SAndroid Build Coastguard Worker     unsigned SubReg = *SubRegs;
199*9880d681SAndroid Build Coastguard Worker     MachineInstr *Def = PhysRegDef[SubReg];
200*9880d681SAndroid Build Coastguard Worker     if (!Def)
201*9880d681SAndroid Build Coastguard Worker       continue;
202*9880d681SAndroid Build Coastguard Worker     unsigned Dist = DistanceMap[Def];
203*9880d681SAndroid Build Coastguard Worker     if (Dist > LastDefDist) {
204*9880d681SAndroid Build Coastguard Worker       LastDefReg  = SubReg;
205*9880d681SAndroid Build Coastguard Worker       LastDef     = Def;
206*9880d681SAndroid Build Coastguard Worker       LastDefDist = Dist;
207*9880d681SAndroid Build Coastguard Worker     }
208*9880d681SAndroid Build Coastguard Worker   }
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker   if (!LastDef)
211*9880d681SAndroid Build Coastguard Worker     return nullptr;
212*9880d681SAndroid Build Coastguard Worker 
213*9880d681SAndroid Build Coastguard Worker   PartDefRegs.insert(LastDefReg);
214*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) {
215*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = LastDef->getOperand(i);
216*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
217*9880d681SAndroid Build Coastguard Worker       continue;
218*9880d681SAndroid Build Coastguard Worker     unsigned DefReg = MO.getReg();
219*9880d681SAndroid Build Coastguard Worker     if (TRI->isSubRegister(Reg, DefReg)) {
220*9880d681SAndroid Build Coastguard Worker       for (MCSubRegIterator SubRegs(DefReg, TRI, /*IncludeSelf=*/true);
221*9880d681SAndroid Build Coastguard Worker            SubRegs.isValid(); ++SubRegs)
222*9880d681SAndroid Build Coastguard Worker         PartDefRegs.insert(*SubRegs);
223*9880d681SAndroid Build Coastguard Worker     }
224*9880d681SAndroid Build Coastguard Worker   }
225*9880d681SAndroid Build Coastguard Worker   return LastDef;
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker 
228*9880d681SAndroid Build Coastguard Worker /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
229*9880d681SAndroid Build Coastguard Worker /// implicit defs to a machine instruction if there was an earlier def of its
230*9880d681SAndroid Build Coastguard Worker /// super-register.
HandlePhysRegUse(unsigned Reg,MachineInstr & MI)231*9880d681SAndroid Build Coastguard Worker void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr &MI) {
232*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastDef = PhysRegDef[Reg];
233*9880d681SAndroid Build Coastguard Worker   // If there was a previous use or a "full" def all is well.
234*9880d681SAndroid Build Coastguard Worker   if (!LastDef && !PhysRegUse[Reg]) {
235*9880d681SAndroid Build Coastguard Worker     // Otherwise, the last sub-register def implicitly defines this register.
236*9880d681SAndroid Build Coastguard Worker     // e.g.
237*9880d681SAndroid Build Coastguard Worker     // AH =
238*9880d681SAndroid Build Coastguard Worker     // AL = ... <imp-def EAX>, <imp-kill AH>
239*9880d681SAndroid Build Coastguard Worker     //    = AH
240*9880d681SAndroid Build Coastguard Worker     // ...
241*9880d681SAndroid Build Coastguard Worker     //    = EAX
242*9880d681SAndroid Build Coastguard Worker     // All of the sub-registers must have been defined before the use of Reg!
243*9880d681SAndroid Build Coastguard Worker     SmallSet<unsigned, 4> PartDefRegs;
244*9880d681SAndroid Build Coastguard Worker     MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs);
245*9880d681SAndroid Build Coastguard Worker     // If LastPartialDef is NULL, it must be using a livein register.
246*9880d681SAndroid Build Coastguard Worker     if (LastPartialDef) {
247*9880d681SAndroid Build Coastguard Worker       LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
248*9880d681SAndroid Build Coastguard Worker                                                            true/*IsImp*/));
249*9880d681SAndroid Build Coastguard Worker       PhysRegDef[Reg] = LastPartialDef;
250*9880d681SAndroid Build Coastguard Worker       SmallSet<unsigned, 8> Processed;
251*9880d681SAndroid Build Coastguard Worker       for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
252*9880d681SAndroid Build Coastguard Worker         unsigned SubReg = *SubRegs;
253*9880d681SAndroid Build Coastguard Worker         if (Processed.count(SubReg))
254*9880d681SAndroid Build Coastguard Worker           continue;
255*9880d681SAndroid Build Coastguard Worker         if (PartDefRegs.count(SubReg))
256*9880d681SAndroid Build Coastguard Worker           continue;
257*9880d681SAndroid Build Coastguard Worker         // This part of Reg was defined before the last partial def. It's killed
258*9880d681SAndroid Build Coastguard Worker         // here.
259*9880d681SAndroid Build Coastguard Worker         LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
260*9880d681SAndroid Build Coastguard Worker                                                              false/*IsDef*/,
261*9880d681SAndroid Build Coastguard Worker                                                              true/*IsImp*/));
262*9880d681SAndroid Build Coastguard Worker         PhysRegDef[SubReg] = LastPartialDef;
263*9880d681SAndroid Build Coastguard Worker         for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS)
264*9880d681SAndroid Build Coastguard Worker           Processed.insert(*SS);
265*9880d681SAndroid Build Coastguard Worker       }
266*9880d681SAndroid Build Coastguard Worker     }
267*9880d681SAndroid Build Coastguard Worker   } else if (LastDef && !PhysRegUse[Reg] &&
268*9880d681SAndroid Build Coastguard Worker              !LastDef->findRegisterDefOperand(Reg))
269*9880d681SAndroid Build Coastguard Worker     // Last def defines the super register, add an implicit def of reg.
270*9880d681SAndroid Build Coastguard Worker     LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
271*9880d681SAndroid Build Coastguard Worker                                                   true/*IsImp*/));
272*9880d681SAndroid Build Coastguard Worker 
273*9880d681SAndroid Build Coastguard Worker   // Remember this use.
274*9880d681SAndroid Build Coastguard Worker   for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
275*9880d681SAndroid Build Coastguard Worker        SubRegs.isValid(); ++SubRegs)
276*9880d681SAndroid Build Coastguard Worker     PhysRegUse[*SubRegs] = &MI;
277*9880d681SAndroid Build Coastguard Worker }
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker /// FindLastRefOrPartRef - Return the last reference or partial reference of
280*9880d681SAndroid Build Coastguard Worker /// the specified register.
FindLastRefOrPartRef(unsigned Reg)281*9880d681SAndroid Build Coastguard Worker MachineInstr *LiveVariables::FindLastRefOrPartRef(unsigned Reg) {
282*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastDef = PhysRegDef[Reg];
283*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastUse = PhysRegUse[Reg];
284*9880d681SAndroid Build Coastguard Worker   if (!LastDef && !LastUse)
285*9880d681SAndroid Build Coastguard Worker     return nullptr;
286*9880d681SAndroid Build Coastguard Worker 
287*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
288*9880d681SAndroid Build Coastguard Worker   unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
289*9880d681SAndroid Build Coastguard Worker   unsigned LastPartDefDist = 0;
290*9880d681SAndroid Build Coastguard Worker   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
291*9880d681SAndroid Build Coastguard Worker     unsigned SubReg = *SubRegs;
292*9880d681SAndroid Build Coastguard Worker     MachineInstr *Def = PhysRegDef[SubReg];
293*9880d681SAndroid Build Coastguard Worker     if (Def && Def != LastDef) {
294*9880d681SAndroid Build Coastguard Worker       // There was a def of this sub-register in between. This is a partial
295*9880d681SAndroid Build Coastguard Worker       // def, keep track of the last one.
296*9880d681SAndroid Build Coastguard Worker       unsigned Dist = DistanceMap[Def];
297*9880d681SAndroid Build Coastguard Worker       if (Dist > LastPartDefDist)
298*9880d681SAndroid Build Coastguard Worker         LastPartDefDist = Dist;
299*9880d681SAndroid Build Coastguard Worker     } else if (MachineInstr *Use = PhysRegUse[SubReg]) {
300*9880d681SAndroid Build Coastguard Worker       unsigned Dist = DistanceMap[Use];
301*9880d681SAndroid Build Coastguard Worker       if (Dist > LastRefOrPartRefDist) {
302*9880d681SAndroid Build Coastguard Worker         LastRefOrPartRefDist = Dist;
303*9880d681SAndroid Build Coastguard Worker         LastRefOrPartRef = Use;
304*9880d681SAndroid Build Coastguard Worker       }
305*9880d681SAndroid Build Coastguard Worker     }
306*9880d681SAndroid Build Coastguard Worker   }
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker   return LastRefOrPartRef;
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker 
HandlePhysRegKill(unsigned Reg,MachineInstr * MI)311*9880d681SAndroid Build Coastguard Worker bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) {
312*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastDef = PhysRegDef[Reg];
313*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastUse = PhysRegUse[Reg];
314*9880d681SAndroid Build Coastguard Worker   if (!LastDef && !LastUse)
315*9880d681SAndroid Build Coastguard Worker     return false;
316*9880d681SAndroid Build Coastguard Worker 
317*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
318*9880d681SAndroid Build Coastguard Worker   unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
319*9880d681SAndroid Build Coastguard Worker   // The whole register is used.
320*9880d681SAndroid Build Coastguard Worker   // AL =
321*9880d681SAndroid Build Coastguard Worker   // AH =
322*9880d681SAndroid Build Coastguard Worker   //
323*9880d681SAndroid Build Coastguard Worker   //    = AX
324*9880d681SAndroid Build Coastguard Worker   //    = AL, AX<imp-use, kill>
325*9880d681SAndroid Build Coastguard Worker   // AX =
326*9880d681SAndroid Build Coastguard Worker   //
327*9880d681SAndroid Build Coastguard Worker   // Or whole register is defined, but not used at all.
328*9880d681SAndroid Build Coastguard Worker   // AX<dead> =
329*9880d681SAndroid Build Coastguard Worker   // ...
330*9880d681SAndroid Build Coastguard Worker   // AX =
331*9880d681SAndroid Build Coastguard Worker   //
332*9880d681SAndroid Build Coastguard Worker   // Or whole register is defined, but only partly used.
333*9880d681SAndroid Build Coastguard Worker   // AX<dead> = AL<imp-def>
334*9880d681SAndroid Build Coastguard Worker   //    = AL<kill>
335*9880d681SAndroid Build Coastguard Worker   // AX =
336*9880d681SAndroid Build Coastguard Worker   MachineInstr *LastPartDef = nullptr;
337*9880d681SAndroid Build Coastguard Worker   unsigned LastPartDefDist = 0;
338*9880d681SAndroid Build Coastguard Worker   SmallSet<unsigned, 8> PartUses;
339*9880d681SAndroid Build Coastguard Worker   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
340*9880d681SAndroid Build Coastguard Worker     unsigned SubReg = *SubRegs;
341*9880d681SAndroid Build Coastguard Worker     MachineInstr *Def = PhysRegDef[SubReg];
342*9880d681SAndroid Build Coastguard Worker     if (Def && Def != LastDef) {
343*9880d681SAndroid Build Coastguard Worker       // There was a def of this sub-register in between. This is a partial
344*9880d681SAndroid Build Coastguard Worker       // def, keep track of the last one.
345*9880d681SAndroid Build Coastguard Worker       unsigned Dist = DistanceMap[Def];
346*9880d681SAndroid Build Coastguard Worker       if (Dist > LastPartDefDist) {
347*9880d681SAndroid Build Coastguard Worker         LastPartDefDist = Dist;
348*9880d681SAndroid Build Coastguard Worker         LastPartDef = Def;
349*9880d681SAndroid Build Coastguard Worker       }
350*9880d681SAndroid Build Coastguard Worker       continue;
351*9880d681SAndroid Build Coastguard Worker     }
352*9880d681SAndroid Build Coastguard Worker     if (MachineInstr *Use = PhysRegUse[SubReg]) {
353*9880d681SAndroid Build Coastguard Worker       for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); SS.isValid();
354*9880d681SAndroid Build Coastguard Worker            ++SS)
355*9880d681SAndroid Build Coastguard Worker         PartUses.insert(*SS);
356*9880d681SAndroid Build Coastguard Worker       unsigned Dist = DistanceMap[Use];
357*9880d681SAndroid Build Coastguard Worker       if (Dist > LastRefOrPartRefDist) {
358*9880d681SAndroid Build Coastguard Worker         LastRefOrPartRefDist = Dist;
359*9880d681SAndroid Build Coastguard Worker         LastRefOrPartRef = Use;
360*9880d681SAndroid Build Coastguard Worker       }
361*9880d681SAndroid Build Coastguard Worker     }
362*9880d681SAndroid Build Coastguard Worker   }
363*9880d681SAndroid Build Coastguard Worker 
364*9880d681SAndroid Build Coastguard Worker   if (!PhysRegUse[Reg]) {
365*9880d681SAndroid Build Coastguard Worker     // Partial uses. Mark register def dead and add implicit def of
366*9880d681SAndroid Build Coastguard Worker     // sub-registers which are used.
367*9880d681SAndroid Build Coastguard Worker     // EAX<dead>  = op  AL<imp-def>
368*9880d681SAndroid Build Coastguard Worker     // That is, EAX def is dead but AL def extends pass it.
369*9880d681SAndroid Build Coastguard Worker     PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
370*9880d681SAndroid Build Coastguard Worker     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
371*9880d681SAndroid Build Coastguard Worker       unsigned SubReg = *SubRegs;
372*9880d681SAndroid Build Coastguard Worker       if (!PartUses.count(SubReg))
373*9880d681SAndroid Build Coastguard Worker         continue;
374*9880d681SAndroid Build Coastguard Worker       bool NeedDef = true;
375*9880d681SAndroid Build Coastguard Worker       if (PhysRegDef[Reg] == PhysRegDef[SubReg]) {
376*9880d681SAndroid Build Coastguard Worker         MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg);
377*9880d681SAndroid Build Coastguard Worker         if (MO) {
378*9880d681SAndroid Build Coastguard Worker           NeedDef = false;
379*9880d681SAndroid Build Coastguard Worker           assert(!MO->isDead());
380*9880d681SAndroid Build Coastguard Worker         }
381*9880d681SAndroid Build Coastguard Worker       }
382*9880d681SAndroid Build Coastguard Worker       if (NeedDef)
383*9880d681SAndroid Build Coastguard Worker         PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
384*9880d681SAndroid Build Coastguard Worker                                                  true/*IsDef*/, true/*IsImp*/));
385*9880d681SAndroid Build Coastguard Worker       MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
386*9880d681SAndroid Build Coastguard Worker       if (LastSubRef)
387*9880d681SAndroid Build Coastguard Worker         LastSubRef->addRegisterKilled(SubReg, TRI, true);
388*9880d681SAndroid Build Coastguard Worker       else {
389*9880d681SAndroid Build Coastguard Worker         LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
390*9880d681SAndroid Build Coastguard Worker         for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true);
391*9880d681SAndroid Build Coastguard Worker              SS.isValid(); ++SS)
392*9880d681SAndroid Build Coastguard Worker           PhysRegUse[*SS] = LastRefOrPartRef;
393*9880d681SAndroid Build Coastguard Worker       }
394*9880d681SAndroid Build Coastguard Worker       for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS)
395*9880d681SAndroid Build Coastguard Worker         PartUses.erase(*SS);
396*9880d681SAndroid Build Coastguard Worker     }
397*9880d681SAndroid Build Coastguard Worker   } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) {
398*9880d681SAndroid Build Coastguard Worker     if (LastPartDef)
399*9880d681SAndroid Build Coastguard Worker       // The last partial def kills the register.
400*9880d681SAndroid Build Coastguard Worker       LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
401*9880d681SAndroid Build Coastguard Worker                                                 true/*IsImp*/, true/*IsKill*/));
402*9880d681SAndroid Build Coastguard Worker     else {
403*9880d681SAndroid Build Coastguard Worker       MachineOperand *MO =
404*9880d681SAndroid Build Coastguard Worker         LastRefOrPartRef->findRegisterDefOperand(Reg, false, TRI);
405*9880d681SAndroid Build Coastguard Worker       bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg;
406*9880d681SAndroid Build Coastguard Worker       // If the last reference is the last def, then it's not used at all.
407*9880d681SAndroid Build Coastguard Worker       // That is, unless we are currently processing the last reference itself.
408*9880d681SAndroid Build Coastguard Worker       LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
409*9880d681SAndroid Build Coastguard Worker       if (NeedEC) {
410*9880d681SAndroid Build Coastguard Worker         // If we are adding a subreg def and the superreg def is marked early
411*9880d681SAndroid Build Coastguard Worker         // clobber, add an early clobber marker to the subreg def.
412*9880d681SAndroid Build Coastguard Worker         MO = LastRefOrPartRef->findRegisterDefOperand(Reg);
413*9880d681SAndroid Build Coastguard Worker         if (MO)
414*9880d681SAndroid Build Coastguard Worker           MO->setIsEarlyClobber();
415*9880d681SAndroid Build Coastguard Worker       }
416*9880d681SAndroid Build Coastguard Worker     }
417*9880d681SAndroid Build Coastguard Worker   } else
418*9880d681SAndroid Build Coastguard Worker     LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
419*9880d681SAndroid Build Coastguard Worker   return true;
420*9880d681SAndroid Build Coastguard Worker }
421*9880d681SAndroid Build Coastguard Worker 
HandleRegMask(const MachineOperand & MO)422*9880d681SAndroid Build Coastguard Worker void LiveVariables::HandleRegMask(const MachineOperand &MO) {
423*9880d681SAndroid Build Coastguard Worker   // Call HandlePhysRegKill() for all live registers clobbered by Mask.
424*9880d681SAndroid Build Coastguard Worker   // Clobbered registers are always dead, sp there is no need to use
425*9880d681SAndroid Build Coastguard Worker   // HandlePhysRegDef().
426*9880d681SAndroid Build Coastguard Worker   for (unsigned Reg = 1, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) {
427*9880d681SAndroid Build Coastguard Worker     // Skip dead regs.
428*9880d681SAndroid Build Coastguard Worker     if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
429*9880d681SAndroid Build Coastguard Worker       continue;
430*9880d681SAndroid Build Coastguard Worker     // Skip mask-preserved regs.
431*9880d681SAndroid Build Coastguard Worker     if (!MO.clobbersPhysReg(Reg))
432*9880d681SAndroid Build Coastguard Worker       continue;
433*9880d681SAndroid Build Coastguard Worker     // Kill the largest clobbered super-register.
434*9880d681SAndroid Build Coastguard Worker     // This avoids needless implicit operands.
435*9880d681SAndroid Build Coastguard Worker     unsigned Super = Reg;
436*9880d681SAndroid Build Coastguard Worker     for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
437*9880d681SAndroid Build Coastguard Worker       if ((PhysRegDef[*SR] || PhysRegUse[*SR]) && MO.clobbersPhysReg(*SR))
438*9880d681SAndroid Build Coastguard Worker         Super = *SR;
439*9880d681SAndroid Build Coastguard Worker     HandlePhysRegKill(Super, nullptr);
440*9880d681SAndroid Build Coastguard Worker   }
441*9880d681SAndroid Build Coastguard Worker }
442*9880d681SAndroid Build Coastguard Worker 
HandlePhysRegDef(unsigned Reg,MachineInstr * MI,SmallVectorImpl<unsigned> & Defs)443*9880d681SAndroid Build Coastguard Worker void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI,
444*9880d681SAndroid Build Coastguard Worker                                      SmallVectorImpl<unsigned> &Defs) {
445*9880d681SAndroid Build Coastguard Worker   // What parts of the register are previously defined?
446*9880d681SAndroid Build Coastguard Worker   SmallSet<unsigned, 32> Live;
447*9880d681SAndroid Build Coastguard Worker   if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
448*9880d681SAndroid Build Coastguard Worker     for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
449*9880d681SAndroid Build Coastguard Worker          SubRegs.isValid(); ++SubRegs)
450*9880d681SAndroid Build Coastguard Worker       Live.insert(*SubRegs);
451*9880d681SAndroid Build Coastguard Worker   } else {
452*9880d681SAndroid Build Coastguard Worker     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
453*9880d681SAndroid Build Coastguard Worker       unsigned SubReg = *SubRegs;
454*9880d681SAndroid Build Coastguard Worker       // If a register isn't itself defined, but all parts that make up of it
455*9880d681SAndroid Build Coastguard Worker       // are defined, then consider it also defined.
456*9880d681SAndroid Build Coastguard Worker       // e.g.
457*9880d681SAndroid Build Coastguard Worker       // AL =
458*9880d681SAndroid Build Coastguard Worker       // AH =
459*9880d681SAndroid Build Coastguard Worker       //    = AX
460*9880d681SAndroid Build Coastguard Worker       if (Live.count(SubReg))
461*9880d681SAndroid Build Coastguard Worker         continue;
462*9880d681SAndroid Build Coastguard Worker       if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
463*9880d681SAndroid Build Coastguard Worker         for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true);
464*9880d681SAndroid Build Coastguard Worker              SS.isValid(); ++SS)
465*9880d681SAndroid Build Coastguard Worker           Live.insert(*SS);
466*9880d681SAndroid Build Coastguard Worker       }
467*9880d681SAndroid Build Coastguard Worker     }
468*9880d681SAndroid Build Coastguard Worker   }
469*9880d681SAndroid Build Coastguard Worker 
470*9880d681SAndroid Build Coastguard Worker   // Start from the largest piece, find the last time any part of the register
471*9880d681SAndroid Build Coastguard Worker   // is referenced.
472*9880d681SAndroid Build Coastguard Worker   HandlePhysRegKill(Reg, MI);
473*9880d681SAndroid Build Coastguard Worker   // Only some of the sub-registers are used.
474*9880d681SAndroid Build Coastguard Worker   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
475*9880d681SAndroid Build Coastguard Worker     unsigned SubReg = *SubRegs;
476*9880d681SAndroid Build Coastguard Worker     if (!Live.count(SubReg))
477*9880d681SAndroid Build Coastguard Worker       // Skip if this sub-register isn't defined.
478*9880d681SAndroid Build Coastguard Worker       continue;
479*9880d681SAndroid Build Coastguard Worker     HandlePhysRegKill(SubReg, MI);
480*9880d681SAndroid Build Coastguard Worker   }
481*9880d681SAndroid Build Coastguard Worker 
482*9880d681SAndroid Build Coastguard Worker   if (MI)
483*9880d681SAndroid Build Coastguard Worker     Defs.push_back(Reg);  // Remember this def.
484*9880d681SAndroid Build Coastguard Worker }
485*9880d681SAndroid Build Coastguard Worker 
UpdatePhysRegDefs(MachineInstr & MI,SmallVectorImpl<unsigned> & Defs)486*9880d681SAndroid Build Coastguard Worker void LiveVariables::UpdatePhysRegDefs(MachineInstr &MI,
487*9880d681SAndroid Build Coastguard Worker                                       SmallVectorImpl<unsigned> &Defs) {
488*9880d681SAndroid Build Coastguard Worker   while (!Defs.empty()) {
489*9880d681SAndroid Build Coastguard Worker     unsigned Reg = Defs.back();
490*9880d681SAndroid Build Coastguard Worker     Defs.pop_back();
491*9880d681SAndroid Build Coastguard Worker     for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
492*9880d681SAndroid Build Coastguard Worker          SubRegs.isValid(); ++SubRegs) {
493*9880d681SAndroid Build Coastguard Worker       unsigned SubReg = *SubRegs;
494*9880d681SAndroid Build Coastguard Worker       PhysRegDef[SubReg] = &MI;
495*9880d681SAndroid Build Coastguard Worker       PhysRegUse[SubReg]  = nullptr;
496*9880d681SAndroid Build Coastguard Worker     }
497*9880d681SAndroid Build Coastguard Worker   }
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker 
runOnInstr(MachineInstr & MI,SmallVectorImpl<unsigned> & Defs)500*9880d681SAndroid Build Coastguard Worker void LiveVariables::runOnInstr(MachineInstr &MI,
501*9880d681SAndroid Build Coastguard Worker                                SmallVectorImpl<unsigned> &Defs) {
502*9880d681SAndroid Build Coastguard Worker   assert(!MI.isDebugValue());
503*9880d681SAndroid Build Coastguard Worker   // Process all of the operands of the instruction...
504*9880d681SAndroid Build Coastguard Worker   unsigned NumOperandsToProcess = MI.getNumOperands();
505*9880d681SAndroid Build Coastguard Worker 
506*9880d681SAndroid Build Coastguard Worker   // Unless it is a PHI node.  In this case, ONLY process the DEF, not any
507*9880d681SAndroid Build Coastguard Worker   // of the uses.  They will be handled in other basic blocks.
508*9880d681SAndroid Build Coastguard Worker   if (MI.isPHI())
509*9880d681SAndroid Build Coastguard Worker     NumOperandsToProcess = 1;
510*9880d681SAndroid Build Coastguard Worker 
511*9880d681SAndroid Build Coastguard Worker   // Clear kill and dead markers. LV will recompute them.
512*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 4> UseRegs;
513*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 4> DefRegs;
514*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 1> RegMasks;
515*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
516*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI.getOperand(i);
517*9880d681SAndroid Build Coastguard Worker     if (MO.isRegMask()) {
518*9880d681SAndroid Build Coastguard Worker       RegMasks.push_back(i);
519*9880d681SAndroid Build Coastguard Worker       continue;
520*9880d681SAndroid Build Coastguard Worker     }
521*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg() || MO.getReg() == 0)
522*9880d681SAndroid Build Coastguard Worker       continue;
523*9880d681SAndroid Build Coastguard Worker     unsigned MOReg = MO.getReg();
524*9880d681SAndroid Build Coastguard Worker     if (MO.isUse()) {
525*9880d681SAndroid Build Coastguard Worker       if (!(TargetRegisterInfo::isPhysicalRegister(MOReg) &&
526*9880d681SAndroid Build Coastguard Worker             MRI->isReserved(MOReg)))
527*9880d681SAndroid Build Coastguard Worker         MO.setIsKill(false);
528*9880d681SAndroid Build Coastguard Worker       if (MO.readsReg())
529*9880d681SAndroid Build Coastguard Worker         UseRegs.push_back(MOReg);
530*9880d681SAndroid Build Coastguard Worker     } else {
531*9880d681SAndroid Build Coastguard Worker       assert(MO.isDef());
532*9880d681SAndroid Build Coastguard Worker       // FIXME: We should not remove any dead flags. However the MIPS RDDSP
533*9880d681SAndroid Build Coastguard Worker       // instruction needs it at the moment: http://llvm.org/PR27116.
534*9880d681SAndroid Build Coastguard Worker       if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
535*9880d681SAndroid Build Coastguard Worker           !MRI->isReserved(MOReg))
536*9880d681SAndroid Build Coastguard Worker         MO.setIsDead(false);
537*9880d681SAndroid Build Coastguard Worker       DefRegs.push_back(MOReg);
538*9880d681SAndroid Build Coastguard Worker     }
539*9880d681SAndroid Build Coastguard Worker   }
540*9880d681SAndroid Build Coastguard Worker 
541*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *MBB = MI.getParent();
542*9880d681SAndroid Build Coastguard Worker   // Process all uses.
543*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) {
544*9880d681SAndroid Build Coastguard Worker     unsigned MOReg = UseRegs[i];
545*9880d681SAndroid Build Coastguard Worker     if (TargetRegisterInfo::isVirtualRegister(MOReg))
546*9880d681SAndroid Build Coastguard Worker       HandleVirtRegUse(MOReg, MBB, MI);
547*9880d681SAndroid Build Coastguard Worker     else if (!MRI->isReserved(MOReg))
548*9880d681SAndroid Build Coastguard Worker       HandlePhysRegUse(MOReg, MI);
549*9880d681SAndroid Build Coastguard Worker   }
550*9880d681SAndroid Build Coastguard Worker 
551*9880d681SAndroid Build Coastguard Worker   // Process all masked registers. (Call clobbers).
552*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = RegMasks.size(); i != e; ++i)
553*9880d681SAndroid Build Coastguard Worker     HandleRegMask(MI.getOperand(RegMasks[i]));
554*9880d681SAndroid Build Coastguard Worker 
555*9880d681SAndroid Build Coastguard Worker   // Process all defs.
556*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
557*9880d681SAndroid Build Coastguard Worker     unsigned MOReg = DefRegs[i];
558*9880d681SAndroid Build Coastguard Worker     if (TargetRegisterInfo::isVirtualRegister(MOReg))
559*9880d681SAndroid Build Coastguard Worker       HandleVirtRegDef(MOReg, MI);
560*9880d681SAndroid Build Coastguard Worker     else if (!MRI->isReserved(MOReg))
561*9880d681SAndroid Build Coastguard Worker       HandlePhysRegDef(MOReg, &MI, Defs);
562*9880d681SAndroid Build Coastguard Worker   }
563*9880d681SAndroid Build Coastguard Worker   UpdatePhysRegDefs(MI, Defs);
564*9880d681SAndroid Build Coastguard Worker }
565*9880d681SAndroid Build Coastguard Worker 
runOnBlock(MachineBasicBlock * MBB,const unsigned NumRegs)566*9880d681SAndroid Build Coastguard Worker void LiveVariables::runOnBlock(MachineBasicBlock *MBB, const unsigned NumRegs) {
567*9880d681SAndroid Build Coastguard Worker   // Mark live-in registers as live-in.
568*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 4> Defs;
569*9880d681SAndroid Build Coastguard Worker   for (const auto &LI : MBB->liveins()) {
570*9880d681SAndroid Build Coastguard Worker     assert(TargetRegisterInfo::isPhysicalRegister(LI.PhysReg) &&
571*9880d681SAndroid Build Coastguard Worker            "Cannot have a live-in virtual register!");
572*9880d681SAndroid Build Coastguard Worker     HandlePhysRegDef(LI.PhysReg, nullptr, Defs);
573*9880d681SAndroid Build Coastguard Worker   }
574*9880d681SAndroid Build Coastguard Worker 
575*9880d681SAndroid Build Coastguard Worker   // Loop over all of the instructions, processing them.
576*9880d681SAndroid Build Coastguard Worker   DistanceMap.clear();
577*9880d681SAndroid Build Coastguard Worker   unsigned Dist = 0;
578*9880d681SAndroid Build Coastguard Worker   for (MachineInstr &MI : *MBB) {
579*9880d681SAndroid Build Coastguard Worker     if (MI.isDebugValue())
580*9880d681SAndroid Build Coastguard Worker       continue;
581*9880d681SAndroid Build Coastguard Worker     DistanceMap.insert(std::make_pair(&MI, Dist++));
582*9880d681SAndroid Build Coastguard Worker 
583*9880d681SAndroid Build Coastguard Worker     runOnInstr(MI, Defs);
584*9880d681SAndroid Build Coastguard Worker   }
585*9880d681SAndroid Build Coastguard Worker 
586*9880d681SAndroid Build Coastguard Worker   // Handle any virtual assignments from PHI nodes which might be at the
587*9880d681SAndroid Build Coastguard Worker   // bottom of this basic block.  We check all of our successor blocks to see
588*9880d681SAndroid Build Coastguard Worker   // if they have PHI nodes, and if so, we simulate an assignment at the end
589*9880d681SAndroid Build Coastguard Worker   // of the current block.
590*9880d681SAndroid Build Coastguard Worker   if (!PHIVarInfo[MBB->getNumber()].empty()) {
591*9880d681SAndroid Build Coastguard Worker     SmallVectorImpl<unsigned> &VarInfoVec = PHIVarInfo[MBB->getNumber()];
592*9880d681SAndroid Build Coastguard Worker 
593*9880d681SAndroid Build Coastguard Worker     for (SmallVectorImpl<unsigned>::iterator I = VarInfoVec.begin(),
594*9880d681SAndroid Build Coastguard Worker            E = VarInfoVec.end(); I != E; ++I)
595*9880d681SAndroid Build Coastguard Worker       // Mark it alive only in the block we are representing.
596*9880d681SAndroid Build Coastguard Worker       MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
597*9880d681SAndroid Build Coastguard Worker                               MBB);
598*9880d681SAndroid Build Coastguard Worker   }
599*9880d681SAndroid Build Coastguard Worker 
600*9880d681SAndroid Build Coastguard Worker   // MachineCSE may CSE instructions which write to non-allocatable physical
601*9880d681SAndroid Build Coastguard Worker   // registers across MBBs. Remember if any reserved register is liveout.
602*9880d681SAndroid Build Coastguard Worker   SmallSet<unsigned, 4> LiveOuts;
603*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
604*9880d681SAndroid Build Coastguard Worker          SE = MBB->succ_end(); SI != SE; ++SI) {
605*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *SuccMBB = *SI;
606*9880d681SAndroid Build Coastguard Worker     if (SuccMBB->isEHPad())
607*9880d681SAndroid Build Coastguard Worker       continue;
608*9880d681SAndroid Build Coastguard Worker     for (const auto &LI : SuccMBB->liveins()) {
609*9880d681SAndroid Build Coastguard Worker       if (!TRI->isInAllocatableClass(LI.PhysReg))
610*9880d681SAndroid Build Coastguard Worker         // Ignore other live-ins, e.g. those that are live into landing pads.
611*9880d681SAndroid Build Coastguard Worker         LiveOuts.insert(LI.PhysReg);
612*9880d681SAndroid Build Coastguard Worker     }
613*9880d681SAndroid Build Coastguard Worker   }
614*9880d681SAndroid Build Coastguard Worker 
615*9880d681SAndroid Build Coastguard Worker   // Loop over PhysRegDef / PhysRegUse, killing any registers that are
616*9880d681SAndroid Build Coastguard Worker   // available at the end of the basic block.
617*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i != NumRegs; ++i)
618*9880d681SAndroid Build Coastguard Worker     if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i))
619*9880d681SAndroid Build Coastguard Worker       HandlePhysRegDef(i, nullptr, Defs);
620*9880d681SAndroid Build Coastguard Worker }
621*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & mf)622*9880d681SAndroid Build Coastguard Worker bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
623*9880d681SAndroid Build Coastguard Worker   MF = &mf;
624*9880d681SAndroid Build Coastguard Worker   MRI = &mf.getRegInfo();
625*9880d681SAndroid Build Coastguard Worker   TRI = MF->getSubtarget().getRegisterInfo();
626*9880d681SAndroid Build Coastguard Worker 
627*9880d681SAndroid Build Coastguard Worker   const unsigned NumRegs = TRI->getNumRegs();
628*9880d681SAndroid Build Coastguard Worker   PhysRegDef.assign(NumRegs, nullptr);
629*9880d681SAndroid Build Coastguard Worker   PhysRegUse.assign(NumRegs, nullptr);
630*9880d681SAndroid Build Coastguard Worker   PHIVarInfo.resize(MF->getNumBlockIDs());
631*9880d681SAndroid Build Coastguard Worker   PHIJoins.clear();
632*9880d681SAndroid Build Coastguard Worker 
633*9880d681SAndroid Build Coastguard Worker   // FIXME: LiveIntervals will be updated to remove its dependence on
634*9880d681SAndroid Build Coastguard Worker   // LiveVariables to improve compilation time and eliminate bizarre pass
635*9880d681SAndroid Build Coastguard Worker   // dependencies. Until then, we can't change much in -O0.
636*9880d681SAndroid Build Coastguard Worker   if (!MRI->isSSA())
637*9880d681SAndroid Build Coastguard Worker     report_fatal_error("regalloc=... not currently supported with -O0");
638*9880d681SAndroid Build Coastguard Worker 
639*9880d681SAndroid Build Coastguard Worker   analyzePHINodes(mf);
640*9880d681SAndroid Build Coastguard Worker 
641*9880d681SAndroid Build Coastguard Worker   // Calculate live variable information in depth first order on the CFG of the
642*9880d681SAndroid Build Coastguard Worker   // function.  This guarantees that we will see the definition of a virtual
643*9880d681SAndroid Build Coastguard Worker   // register before its uses due to dominance properties of SSA (except for PHI
644*9880d681SAndroid Build Coastguard Worker   // nodes, which are treated as a special case).
645*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *Entry = &MF->front();
646*9880d681SAndroid Build Coastguard Worker   SmallPtrSet<MachineBasicBlock*,16> Visited;
647*9880d681SAndroid Build Coastguard Worker 
648*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) {
649*9880d681SAndroid Build Coastguard Worker     runOnBlock(MBB, NumRegs);
650*9880d681SAndroid Build Coastguard Worker 
651*9880d681SAndroid Build Coastguard Worker     PhysRegDef.assign(NumRegs, nullptr);
652*9880d681SAndroid Build Coastguard Worker     PhysRegUse.assign(NumRegs, nullptr);
653*9880d681SAndroid Build Coastguard Worker   }
654*9880d681SAndroid Build Coastguard Worker 
655*9880d681SAndroid Build Coastguard Worker   // Convert and transfer the dead / killed information we have gathered into
656*9880d681SAndroid Build Coastguard Worker   // VirtRegInfo onto MI's.
657*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) {
658*9880d681SAndroid Build Coastguard Worker     const unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
659*9880d681SAndroid Build Coastguard Worker     for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j)
660*9880d681SAndroid Build Coastguard Worker       if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg))
661*9880d681SAndroid Build Coastguard Worker         VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI);
662*9880d681SAndroid Build Coastguard Worker       else
663*9880d681SAndroid Build Coastguard Worker         VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI);
664*9880d681SAndroid Build Coastguard Worker   }
665*9880d681SAndroid Build Coastguard Worker 
666*9880d681SAndroid Build Coastguard Worker   // Check to make sure there are no unreachable blocks in the MC CFG for the
667*9880d681SAndroid Build Coastguard Worker   // function.  If so, it is due to a bug in the instruction selector or some
668*9880d681SAndroid Build Coastguard Worker   // other part of the code generator if this happens.
669*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
670*9880d681SAndroid Build Coastguard Worker   for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
671*9880d681SAndroid Build Coastguard Worker     assert(Visited.count(&*i) != 0 && "unreachable basic block found");
672*9880d681SAndroid Build Coastguard Worker #endif
673*9880d681SAndroid Build Coastguard Worker 
674*9880d681SAndroid Build Coastguard Worker   PhysRegDef.clear();
675*9880d681SAndroid Build Coastguard Worker   PhysRegUse.clear();
676*9880d681SAndroid Build Coastguard Worker   PHIVarInfo.clear();
677*9880d681SAndroid Build Coastguard Worker 
678*9880d681SAndroid Build Coastguard Worker   return false;
679*9880d681SAndroid Build Coastguard Worker }
680*9880d681SAndroid Build Coastguard Worker 
681*9880d681SAndroid Build Coastguard Worker /// replaceKillInstruction - Update register kill info by replacing a kill
682*9880d681SAndroid Build Coastguard Worker /// instruction with a new one.
replaceKillInstruction(unsigned Reg,MachineInstr & OldMI,MachineInstr & NewMI)683*9880d681SAndroid Build Coastguard Worker void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr &OldMI,
684*9880d681SAndroid Build Coastguard Worker                                            MachineInstr &NewMI) {
685*9880d681SAndroid Build Coastguard Worker   VarInfo &VI = getVarInfo(Reg);
686*9880d681SAndroid Build Coastguard Worker   std::replace(VI.Kills.begin(), VI.Kills.end(), &OldMI, &NewMI);
687*9880d681SAndroid Build Coastguard Worker }
688*9880d681SAndroid Build Coastguard Worker 
689*9880d681SAndroid Build Coastguard Worker /// removeVirtualRegistersKilled - Remove all killed info for the specified
690*9880d681SAndroid Build Coastguard Worker /// instruction.
removeVirtualRegistersKilled(MachineInstr & MI)691*9880d681SAndroid Build Coastguard Worker void LiveVariables::removeVirtualRegistersKilled(MachineInstr &MI) {
692*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
693*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI.getOperand(i);
694*9880d681SAndroid Build Coastguard Worker     if (MO.isReg() && MO.isKill()) {
695*9880d681SAndroid Build Coastguard Worker       MO.setIsKill(false);
696*9880d681SAndroid Build Coastguard Worker       unsigned Reg = MO.getReg();
697*9880d681SAndroid Build Coastguard Worker       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
698*9880d681SAndroid Build Coastguard Worker         bool removed = getVarInfo(Reg).removeKill(MI);
699*9880d681SAndroid Build Coastguard Worker         assert(removed && "kill not in register's VarInfo?");
700*9880d681SAndroid Build Coastguard Worker         (void)removed;
701*9880d681SAndroid Build Coastguard Worker       }
702*9880d681SAndroid Build Coastguard Worker     }
703*9880d681SAndroid Build Coastguard Worker   }
704*9880d681SAndroid Build Coastguard Worker }
705*9880d681SAndroid Build Coastguard Worker 
706*9880d681SAndroid Build Coastguard Worker /// analyzePHINodes - Gather information about the PHI nodes in here. In
707*9880d681SAndroid Build Coastguard Worker /// particular, we want to map the variable information of a virtual register
708*9880d681SAndroid Build Coastguard Worker /// which is used in a PHI node. We map that to the BB the vreg is coming from.
709*9880d681SAndroid Build Coastguard Worker ///
analyzePHINodes(const MachineFunction & Fn)710*9880d681SAndroid Build Coastguard Worker void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
711*9880d681SAndroid Build Coastguard Worker   for (const auto &MBB : Fn)
712*9880d681SAndroid Build Coastguard Worker     for (const auto &BBI : MBB) {
713*9880d681SAndroid Build Coastguard Worker       if (!BBI.isPHI())
714*9880d681SAndroid Build Coastguard Worker         break;
715*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2)
716*9880d681SAndroid Build Coastguard Worker         if (BBI.getOperand(i).readsReg())
717*9880d681SAndroid Build Coastguard Worker           PHIVarInfo[BBI.getOperand(i + 1).getMBB()->getNumber()]
718*9880d681SAndroid Build Coastguard Worker             .push_back(BBI.getOperand(i).getReg());
719*9880d681SAndroid Build Coastguard Worker     }
720*9880d681SAndroid Build Coastguard Worker }
721*9880d681SAndroid Build Coastguard Worker 
isLiveIn(const MachineBasicBlock & MBB,unsigned Reg,MachineRegisterInfo & MRI)722*9880d681SAndroid Build Coastguard Worker bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB,
723*9880d681SAndroid Build Coastguard Worker                                       unsigned Reg,
724*9880d681SAndroid Build Coastguard Worker                                       MachineRegisterInfo &MRI) {
725*9880d681SAndroid Build Coastguard Worker   unsigned Num = MBB.getNumber();
726*9880d681SAndroid Build Coastguard Worker 
727*9880d681SAndroid Build Coastguard Worker   // Reg is live-through.
728*9880d681SAndroid Build Coastguard Worker   if (AliveBlocks.test(Num))
729*9880d681SAndroid Build Coastguard Worker     return true;
730*9880d681SAndroid Build Coastguard Worker 
731*9880d681SAndroid Build Coastguard Worker   // Registers defined in MBB cannot be live in.
732*9880d681SAndroid Build Coastguard Worker   const MachineInstr *Def = MRI.getVRegDef(Reg);
733*9880d681SAndroid Build Coastguard Worker   if (Def && Def->getParent() == &MBB)
734*9880d681SAndroid Build Coastguard Worker     return false;
735*9880d681SAndroid Build Coastguard Worker 
736*9880d681SAndroid Build Coastguard Worker  // Reg was not defined in MBB, was it killed here?
737*9880d681SAndroid Build Coastguard Worker   return findKill(&MBB);
738*9880d681SAndroid Build Coastguard Worker }
739*9880d681SAndroid Build Coastguard Worker 
isLiveOut(unsigned Reg,const MachineBasicBlock & MBB)740*9880d681SAndroid Build Coastguard Worker bool LiveVariables::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB) {
741*9880d681SAndroid Build Coastguard Worker   LiveVariables::VarInfo &VI = getVarInfo(Reg);
742*9880d681SAndroid Build Coastguard Worker 
743*9880d681SAndroid Build Coastguard Worker   SmallPtrSet<const MachineBasicBlock *, 8> Kills;
744*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
745*9880d681SAndroid Build Coastguard Worker     Kills.insert(VI.Kills[i]->getParent());
746*9880d681SAndroid Build Coastguard Worker 
747*9880d681SAndroid Build Coastguard Worker   // Loop over all of the successors of the basic block, checking to see if
748*9880d681SAndroid Build Coastguard Worker   // the value is either live in the block, or if it is killed in the block.
749*9880d681SAndroid Build Coastguard Worker   for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
750*9880d681SAndroid Build Coastguard Worker     // Is it alive in this successor?
751*9880d681SAndroid Build Coastguard Worker     unsigned SuccIdx = SuccMBB->getNumber();
752*9880d681SAndroid Build Coastguard Worker     if (VI.AliveBlocks.test(SuccIdx))
753*9880d681SAndroid Build Coastguard Worker       return true;
754*9880d681SAndroid Build Coastguard Worker     // Or is it live because there is a use in a successor that kills it?
755*9880d681SAndroid Build Coastguard Worker     if (Kills.count(SuccMBB))
756*9880d681SAndroid Build Coastguard Worker       return true;
757*9880d681SAndroid Build Coastguard Worker   }
758*9880d681SAndroid Build Coastguard Worker 
759*9880d681SAndroid Build Coastguard Worker   return false;
760*9880d681SAndroid Build Coastguard Worker }
761*9880d681SAndroid Build Coastguard Worker 
762*9880d681SAndroid Build Coastguard Worker /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
763*9880d681SAndroid Build Coastguard Worker /// variables that are live out of DomBB will be marked as passing live through
764*9880d681SAndroid Build Coastguard Worker /// BB.
addNewBlock(MachineBasicBlock * BB,MachineBasicBlock * DomBB,MachineBasicBlock * SuccBB)765*9880d681SAndroid Build Coastguard Worker void LiveVariables::addNewBlock(MachineBasicBlock *BB,
766*9880d681SAndroid Build Coastguard Worker                                 MachineBasicBlock *DomBB,
767*9880d681SAndroid Build Coastguard Worker                                 MachineBasicBlock *SuccBB) {
768*9880d681SAndroid Build Coastguard Worker   const unsigned NumNew = BB->getNumber();
769*9880d681SAndroid Build Coastguard Worker 
770*9880d681SAndroid Build Coastguard Worker   SmallSet<unsigned, 16> Defs, Kills;
771*9880d681SAndroid Build Coastguard Worker 
772*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::iterator BBI = SuccBB->begin(), BBE = SuccBB->end();
773*9880d681SAndroid Build Coastguard Worker   for (; BBI != BBE && BBI->isPHI(); ++BBI) {
774*9880d681SAndroid Build Coastguard Worker     // Record the def of the PHI node.
775*9880d681SAndroid Build Coastguard Worker     Defs.insert(BBI->getOperand(0).getReg());
776*9880d681SAndroid Build Coastguard Worker 
777*9880d681SAndroid Build Coastguard Worker     // All registers used by PHI nodes in SuccBB must be live through BB.
778*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
779*9880d681SAndroid Build Coastguard Worker       if (BBI->getOperand(i+1).getMBB() == BB)
780*9880d681SAndroid Build Coastguard Worker         getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
781*9880d681SAndroid Build Coastguard Worker   }
782*9880d681SAndroid Build Coastguard Worker 
783*9880d681SAndroid Build Coastguard Worker   // Record all vreg defs and kills of all instructions in SuccBB.
784*9880d681SAndroid Build Coastguard Worker   for (; BBI != BBE; ++BBI) {
785*9880d681SAndroid Build Coastguard Worker     for (MachineInstr::mop_iterator I = BBI->operands_begin(),
786*9880d681SAndroid Build Coastguard Worker          E = BBI->operands_end(); I != E; ++I) {
787*9880d681SAndroid Build Coastguard Worker       if (I->isReg() && TargetRegisterInfo::isVirtualRegister(I->getReg())) {
788*9880d681SAndroid Build Coastguard Worker         if (I->isDef())
789*9880d681SAndroid Build Coastguard Worker           Defs.insert(I->getReg());
790*9880d681SAndroid Build Coastguard Worker         else if (I->isKill())
791*9880d681SAndroid Build Coastguard Worker           Kills.insert(I->getReg());
792*9880d681SAndroid Build Coastguard Worker       }
793*9880d681SAndroid Build Coastguard Worker     }
794*9880d681SAndroid Build Coastguard Worker   }
795*9880d681SAndroid Build Coastguard Worker 
796*9880d681SAndroid Build Coastguard Worker   // Update info for all live variables
797*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
798*9880d681SAndroid Build Coastguard Worker     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
799*9880d681SAndroid Build Coastguard Worker 
800*9880d681SAndroid Build Coastguard Worker     // If the Defs is defined in the successor it can't be live in BB.
801*9880d681SAndroid Build Coastguard Worker     if (Defs.count(Reg))
802*9880d681SAndroid Build Coastguard Worker       continue;
803*9880d681SAndroid Build Coastguard Worker 
804*9880d681SAndroid Build Coastguard Worker     // If the register is either killed in or live through SuccBB it's also live
805*9880d681SAndroid Build Coastguard Worker     // through BB.
806*9880d681SAndroid Build Coastguard Worker     VarInfo &VI = getVarInfo(Reg);
807*9880d681SAndroid Build Coastguard Worker     if (Kills.count(Reg) || VI.AliveBlocks.test(SuccBB->getNumber()))
808*9880d681SAndroid Build Coastguard Worker       VI.AliveBlocks.set(NumNew);
809*9880d681SAndroid Build Coastguard Worker   }
810*9880d681SAndroid Build Coastguard Worker }
811