blob: 1d935b207396ccf9d43ffb072561a00d4881697e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveVariable analysis pass. For each machine
11// instruction in the function, this pass calculates the set of registers that
12// are immediately dead after the instruction (i.e., the instruction calculates
13// the value, but it is never used) and the set of registers that are used by
14// the instruction, but are never used after the instruction (i.e., they are
15// killed).
16//
17// This class computes live variables using are sparse implementation based on
18// the machine code SSA form. This class computes live variable information for
19// each virtual and _register allocatable_ physical register in a function. It
20// uses the dominance properties of SSA form to efficiently compute live
21// variables for virtual registers, and assumes that physical registers are only
22// live within a single basic block (allowing it to do a single local analysis
23// to resolve physical register lifetimes in each basic block). If a physical
24// register is not register allocatable, it is not tracked. This is useful for
25// things like the stack pointer and condition codes.
26//
27//===----------------------------------------------------------------------===//
28
29#include "llvm/CodeGen/LiveVariables.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner1b989192007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Andersonfb6914f2008-08-04 23:54:43 +000032#include "llvm/CodeGen/Passes.h"
David Greene28806ab2010-01-04 23:02:10 +000033#include "llvm/Support/Debug.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/ADT/DepthFirstIterator.h"
38#include "llvm/ADT/SmallPtrSet.h"
Owen Anderson9a4cb152008-06-27 07:05:59 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include <algorithm>
42using namespace llvm;
43
44char LiveVariables::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +000045INITIALIZE_PASS(LiveVariables, "livevars",
46 "Live Variable Analysis", false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
Owen Andersonfb6914f2008-08-04 23:54:43 +000048
49void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.addRequiredID(UnreachableMachineBlockElimID);
51 AU.setPreservesAll();
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000052 MachineFunctionPass::getAnalysisUsage(AU);
Owen Andersonfb6914f2008-08-04 23:54:43 +000053}
54
Jakob Stoklund Olesenbe9cdbf2009-11-10 22:01:05 +000055MachineInstr *
56LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
57 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
58 if (Kills[i]->getParent() == MBB)
59 return Kills[i];
60 return NULL;
61}
62
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063void LiveVariables::VarInfo::dump() const {
David Greene28806ab2010-01-04 23:02:10 +000064 dbgs() << " Alive in blocks: ";
Jeffrey Yasskin02d392b2009-05-26 18:27:15 +000065 for (SparseBitVector<>::iterator I = AliveBlocks.begin(),
66 E = AliveBlocks.end(); I != E; ++I)
David Greene28806ab2010-01-04 23:02:10 +000067 dbgs() << *I << ", ";
68 dbgs() << "\n Killed by:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 if (Kills.empty())
David Greene28806ab2010-01-04 23:02:10 +000070 dbgs() << " No instructions.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 else {
72 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
David Greene28806ab2010-01-04 23:02:10 +000073 dbgs() << "\n #" << i << ": " << *Kills[i];
74 dbgs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 }
76}
77
Bill Wendlingb88bca92008-02-20 06:10:21 +000078/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Dan Gohman1e57df32008-02-10 18:45:23 +000080 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 "getVarInfo: not a virtual register!");
Dan Gohman1e57df32008-02-10 18:45:23 +000082 RegIdx -= TargetRegisterInfo::FirstVirtualRegister;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 if (RegIdx >= VirtRegInfo.size()) {
84 if (RegIdx >= 2*VirtRegInfo.size())
85 VirtRegInfo.resize(RegIdx*2);
86 else
87 VirtRegInfo.resize(2*VirtRegInfo.size());
88 }
Jeffrey Yasskin02d392b2009-05-26 18:27:15 +000089 return VirtRegInfo[RegIdx];
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090}
91
Owen Anderson77d80492008-01-15 22:58:11 +000092void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
93 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 MachineBasicBlock *MBB,
95 std::vector<MachineBasicBlock*> &WorkList) {
96 unsigned BBNum = MBB->getNumber();
Owen Anderson92a609a2008-01-15 22:02:46 +000097
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendlingb88bca92008-02-20 06:10:21 +000099 // remove it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
101 if (VRInfo.Kills[i]->getParent() == MBB) {
102 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
103 break;
104 }
Owen Anderson92a609a2008-01-15 22:02:46 +0000105
Owen Anderson77d80492008-01-15 22:58:11 +0000106 if (MBB == DefBlock) return; // Terminate recursion
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
Jeffrey Yasskin02d392b2009-05-26 18:27:15 +0000108 if (VRInfo.AliveBlocks.test(BBNum))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return; // We already know the block is live
110
111 // Mark the variable known alive in this bb
Jeffrey Yasskin02d392b2009-05-26 18:27:15 +0000112 VRInfo.AliveBlocks.set(BBNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
114 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
115 E = MBB->pred_rend(); PI != E; ++PI)
116 WorkList.push_back(*PI);
117}
118
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000119void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Owen Anderson77d80492008-01-15 22:58:11 +0000120 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 MachineBasicBlock *MBB) {
122 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson77d80492008-01-15 22:58:11 +0000123 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 while (!WorkList.empty()) {
126 MachineBasicBlock *Pred = WorkList.back();
127 WorkList.pop_back();
Owen Anderson77d80492008-01-15 22:58:11 +0000128 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 }
130}
131
Owen Anderson92a609a2008-01-15 22:02:46 +0000132void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 MachineInstr *MI) {
Evan Cheng251fa152008-04-02 18:04:08 +0000134 assert(MRI->getVRegDef(reg) && "Register use before def!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
Owen Anderson721b2cc2007-11-08 01:20:48 +0000136 unsigned BBNum = MBB->getNumber();
137
Owen Anderson92a609a2008-01-15 22:02:46 +0000138 VarInfo& VRInfo = getVarInfo(reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 VRInfo.NumUses++;
140
Bill Wendlingb88bca92008-02-20 06:10:21 +0000141 // Check to see if this basic block is already a kill block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000143 // Yes, this register is killed in this basic block already. Increase the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 // live range by updating the kill instruction.
145 VRInfo.Kills.back() = MI;
146 return;
147 }
148
149#ifndef NDEBUG
150 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
151 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
152#endif
153
Bill Wendling09d55662008-06-23 23:41:14 +0000154 // This situation can occur:
155 //
156 // ,------.
157 // | |
158 // | v
159 // | t2 = phi ... t1 ...
160 // | |
161 // | v
162 // | t1 = ...
163 // | ... = ... t1 ...
164 // | |
165 // `------'
166 //
167 // where there is a use in a PHI node that's a predecessor to the defining
168 // block. We don't want to mark all predecessors as having the value "alive"
169 // in this case.
170 if (MBB == MRI->getVRegDef(reg)->getParent()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171
Bill Wendlingb88bca92008-02-20 06:10:21 +0000172 // Add a new kill entry for this basic block. If this virtual register is
173 // already marked as alive in this basic block, that means it is alive in at
174 // least one of the successor blocks, it's not a kill.
Jeffrey Yasskin02d392b2009-05-26 18:27:15 +0000175 if (!VRInfo.AliveBlocks.test(BBNum))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 VRInfo.Kills.push_back(MI);
177
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000178 // Update all dominating blocks to mark them as "known live".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
180 E = MBB->pred_end(); PI != E; ++PI)
Evan Cheng251fa152008-04-02 18:04:08 +0000181 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182}
183
Dan Gohman706847e2008-09-21 21:11:41 +0000184void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr *MI) {
185 VarInfo &VRInfo = getVarInfo(Reg);
186
Jeffrey Yasskin02d392b2009-05-26 18:27:15 +0000187 if (VRInfo.AliveBlocks.empty())
Dan Gohman706847e2008-09-21 21:11:41 +0000188 // If vr is not alive in any block, then defaults to dead.
189 VRInfo.Kills.push_back(MI);
190}
191
Evan Cheng1c3ee662008-04-16 09:46:40 +0000192/// FindLastPartialDef - Return the last partial def of the specified register.
Evan Chengcd216d52009-09-22 08:34:46 +0000193/// Also returns the sub-registers that're defined by the instruction.
Evan Cheng1c3ee662008-04-16 09:46:40 +0000194MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg,
Evan Chengcd216d52009-09-22 08:34:46 +0000195 SmallSet<unsigned,4> &PartDefRegs) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000196 unsigned LastDefReg = 0;
197 unsigned LastDefDist = 0;
198 MachineInstr *LastDef = NULL;
199 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
200 unsigned SubReg = *SubRegs; ++SubRegs) {
201 MachineInstr *Def = PhysRegDef[SubReg];
202 if (!Def)
203 continue;
204 unsigned Dist = DistanceMap[Def];
205 if (Dist > LastDefDist) {
206 LastDefReg = SubReg;
207 LastDef = Def;
208 LastDefDist = Dist;
209 }
210 }
Evan Chengcd216d52009-09-22 08:34:46 +0000211
212 if (!LastDef)
213 return 0;
214
215 PartDefRegs.insert(LastDefReg);
216 for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) {
217 MachineOperand &MO = LastDef->getOperand(i);
218 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
219 continue;
220 unsigned DefReg = MO.getReg();
221 if (TRI->isSubRegister(Reg, DefReg)) {
222 PartDefRegs.insert(DefReg);
223 for (const unsigned *SubRegs = TRI->getSubRegisters(DefReg);
224 unsigned SubReg = *SubRegs; ++SubRegs)
225 PartDefRegs.insert(SubReg);
226 }
227 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000228 return LastDef;
229}
230
Bill Wendling85b03762008-02-20 09:15:16 +0000231/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
232/// implicit defs to a machine instruction if there was an earlier def of its
233/// super-register.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng5cec5f62009-11-13 20:36:40 +0000235 MachineInstr *LastDef = PhysRegDef[Reg];
Evan Cheng1c3ee662008-04-16 09:46:40 +0000236 // If there was a previous use or a "full" def all is well.
Evan Cheng5cec5f62009-11-13 20:36:40 +0000237 if (!LastDef && !PhysRegUse[Reg]) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000238 // Otherwise, the last sub-register def implicitly defines this register.
239 // e.g.
240 // AH =
241 // AL = ... <imp-def EAX>, <imp-kill AH>
242 // = AH
243 // ...
244 // = EAX
245 // All of the sub-registers must have been defined before the use of Reg!
Evan Chengcd216d52009-09-22 08:34:46 +0000246 SmallSet<unsigned, 4> PartDefRegs;
247 MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs);
Evan Cheng1c3ee662008-04-16 09:46:40 +0000248 // If LastPartialDef is NULL, it must be using a livein register.
249 if (LastPartialDef) {
250 LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
251 true/*IsImp*/));
252 PhysRegDef[Reg] = LastPartialDef;
Owen Anderson7ba9a8f2008-08-14 23:41:38 +0000253 SmallSet<unsigned, 8> Processed;
Evan Cheng1c3ee662008-04-16 09:46:40 +0000254 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
255 unsigned SubReg = *SubRegs; ++SubRegs) {
256 if (Processed.count(SubReg))
257 continue;
Evan Chengcd216d52009-09-22 08:34:46 +0000258 if (PartDefRegs.count(SubReg))
Evan Cheng1c3ee662008-04-16 09:46:40 +0000259 continue;
260 // This part of Reg was defined before the last partial def. It's killed
261 // here.
262 LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
263 false/*IsDef*/,
264 true/*IsImp*/));
265 PhysRegDef[SubReg] = LastPartialDef;
266 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
267 Processed.insert(*SS);
268 }
269 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 }
Evan Cheng5cec5f62009-11-13 20:36:40 +0000271 else if (LastDef && !PhysRegUse[Reg] &&
272 !LastDef->findRegisterDefOperand(Reg))
273 // Last def defines the super register, add an implicit def of reg.
274 LastDef->addOperand(MachineOperand::CreateReg(Reg,
275 true/*IsDef*/, true/*IsImp*/));
Bill Wendlingb88bca92008-02-20 06:10:21 +0000276
Evan Cheng1c3ee662008-04-16 09:46:40 +0000277 // Remember this use.
278 PhysRegUse[Reg] = MI;
Evan Chengc7daf1f2008-03-05 00:59:57 +0000279 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000280 unsigned SubReg = *SubRegs; ++SubRegs)
Evan Cheng1c3ee662008-04-16 09:46:40 +0000281 PhysRegUse[SubReg] = MI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282}
283
Evan Cheng1e996142009-12-01 00:44:45 +0000284/// FindLastRefOrPartRef - Return the last reference or partial reference of
285/// the specified register.
286MachineInstr *LiveVariables::FindLastRefOrPartRef(unsigned Reg) {
287 MachineInstr *LastDef = PhysRegDef[Reg];
288 MachineInstr *LastUse = PhysRegUse[Reg];
289 if (!LastDef && !LastUse)
Chris Lattner1b6f5792010-06-14 18:28:34 +0000290 return 0;
Evan Cheng1e996142009-12-01 00:44:45 +0000291
292 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
293 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
Evan Cheng1e996142009-12-01 00:44:45 +0000294 unsigned LastPartDefDist = 0;
295 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
296 unsigned SubReg = *SubRegs; ++SubRegs) {
297 MachineInstr *Def = PhysRegDef[SubReg];
298 if (Def && Def != LastDef) {
299 // There was a def of this sub-register in between. This is a partial
300 // def, keep track of the last one.
301 unsigned Dist = DistanceMap[Def];
Benjamin Kramer650c0fa2010-01-07 17:29:08 +0000302 if (Dist > LastPartDefDist)
Evan Cheng1e996142009-12-01 00:44:45 +0000303 LastPartDefDist = Dist;
Benjamin Kramer650c0fa2010-01-07 17:29:08 +0000304 } else if (MachineInstr *Use = PhysRegUse[SubReg]) {
Evan Cheng1e996142009-12-01 00:44:45 +0000305 unsigned Dist = DistanceMap[Use];
306 if (Dist > LastRefOrPartRefDist) {
307 LastRefOrPartRefDist = Dist;
308 LastRefOrPartRef = Use;
309 }
310 }
311 }
312
313 return LastRefOrPartRef;
314}
315
Evan Cheng06df4d02009-01-20 21:25:12 +0000316bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) {
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000317 MachineInstr *LastDef = PhysRegDef[Reg];
318 MachineInstr *LastUse = PhysRegUse[Reg];
319 if (!LastDef && !LastUse)
Evan Cheng1c3ee662008-04-16 09:46:40 +0000320 return false;
321
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000322 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
Evan Cheng1c3ee662008-04-16 09:46:40 +0000323 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
324 // The whole register is used.
325 // AL =
326 // AH =
327 //
328 // = AX
329 // = AL, AX<imp-use, kill>
330 // AX =
331 //
332 // Or whole register is defined, but not used at all.
333 // AX<dead> =
334 // ...
335 // AX =
336 //
337 // Or whole register is defined, but only partly used.
338 // AX<dead> = AL<imp-def>
339 // = AL<kill>
340 // AX =
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000341 MachineInstr *LastPartDef = 0;
342 unsigned LastPartDefDist = 0;
Owen Anderson7ba9a8f2008-08-14 23:41:38 +0000343 SmallSet<unsigned, 8> PartUses;
Evan Cheng1c3ee662008-04-16 09:46:40 +0000344 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
345 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000346 MachineInstr *Def = PhysRegDef[SubReg];
347 if (Def && Def != LastDef) {
348 // There was a def of this sub-register in between. This is a partial
349 // def, keep track of the last one.
350 unsigned Dist = DistanceMap[Def];
351 if (Dist > LastPartDefDist) {
352 LastPartDefDist = Dist;
353 LastPartDef = Def;
354 }
355 continue;
356 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000357 if (MachineInstr *Use = PhysRegUse[SubReg]) {
358 PartUses.insert(SubReg);
359 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
360 PartUses.insert(*SS);
361 unsigned Dist = DistanceMap[Use];
362 if (Dist > LastRefOrPartRefDist) {
363 LastRefOrPartRefDist = Dist;
364 LastRefOrPartRef = Use;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000366 }
367 }
Evan Cheng06df4d02009-01-20 21:25:12 +0000368
Jakob Stoklund Olesen6207e5d2010-03-05 21:49:17 +0000369 if (!PhysRegUse[Reg]) {
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000370 // Partial uses. Mark register def dead and add implicit def of
371 // sub-registers which are used.
372 // EAX<dead> = op AL<imp-def>
373 // That is, EAX def is dead but AL def extends pass it.
Evan Cheng1c3ee662008-04-16 09:46:40 +0000374 PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
375 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
376 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000377 if (!PartUses.count(SubReg))
378 continue;
379 bool NeedDef = true;
380 if (PhysRegDef[Reg] == PhysRegDef[SubReg]) {
381 MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg);
382 if (MO) {
383 NeedDef = false;
384 assert(!MO->isDead());
Evan Cheng2fe17a52009-07-06 21:34:05 +0000385 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000386 }
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000387 if (NeedDef)
388 PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
389 true/*IsDef*/, true/*IsImp*/));
Evan Cheng1e996142009-12-01 00:44:45 +0000390 MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
391 if (LastSubRef)
392 LastSubRef->addRegisterKilled(SubReg, TRI, true);
393 else {
394 LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
395 PhysRegUse[SubReg] = LastRefOrPartRef;
396 for (const unsigned *SSRegs = TRI->getSubRegisters(SubReg);
397 unsigned SSReg = *SSRegs; ++SSRegs)
398 PhysRegUse[SSReg] = LastRefOrPartRef;
399 }
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000400 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
401 PartUses.erase(*SS);
Evan Cheng1c3ee662008-04-16 09:46:40 +0000402 }
Jakob Stoklund Olesen6207e5d2010-03-05 21:49:17 +0000403 } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) {
404 if (LastPartDef)
405 // The last partial def kills the register.
406 LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
407 true/*IsImp*/, true/*IsKill*/));
408 else {
409 MachineOperand *MO =
410 LastRefOrPartRef->findRegisterDefOperand(Reg, false, TRI);
411 bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg;
412 // If the last reference is the last def, then it's not used at all.
413 // That is, unless we are currently processing the last reference itself.
414 LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
415 if (NeedEC) {
416 // If we are adding a subreg def and the superreg def is marked early
417 // clobber, add an early clobber marker to the subreg def.
418 MO = LastRefOrPartRef->findRegisterDefOperand(Reg);
419 if (MO)
420 MO->setIsEarlyClobber();
421 }
422 }
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000423 } else
Evan Cheng1c3ee662008-04-16 09:46:40 +0000424 LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
425 return true;
426}
427
Evan Chengd062bf72009-09-23 06:28:31 +0000428void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI,
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000429 SmallVector<unsigned, 4> &Defs) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000430 // What parts of the register are previously defined?
Owen Anderson9a4cb152008-06-27 07:05:59 +0000431 SmallSet<unsigned, 32> Live;
Evan Cheng1c3ee662008-04-16 09:46:40 +0000432 if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
433 Live.insert(Reg);
434 for (const unsigned *SS = TRI->getSubRegisters(Reg); *SS; ++SS)
435 Live.insert(*SS);
436 } else {
437 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
438 unsigned SubReg = *SubRegs; ++SubRegs) {
439 // If a register isn't itself defined, but all parts that make up of it
440 // are defined, then consider it also defined.
441 // e.g.
442 // AL =
443 // AH =
444 // = AX
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000445 if (Live.count(SubReg))
446 continue;
Evan Cheng1c3ee662008-04-16 09:46:40 +0000447 if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
448 Live.insert(SubReg);
449 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
450 Live.insert(*SS);
451 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000452 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 }
454
Evan Cheng1c3ee662008-04-16 09:46:40 +0000455 // Start from the largest piece, find the last time any part of the register
456 // is referenced.
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000457 HandlePhysRegKill(Reg, MI);
458 // Only some of the sub-registers are used.
459 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
460 unsigned SubReg = *SubRegs; ++SubRegs) {
461 if (!Live.count(SubReg))
462 // Skip if this sub-register isn't defined.
463 continue;
464 HandlePhysRegKill(SubReg, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 }
466
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000467 if (MI)
468 Defs.push_back(Reg); // Remember this def.
Evan Chengd062bf72009-09-23 06:28:31 +0000469}
470
471void LiveVariables::UpdatePhysRegDefs(MachineInstr *MI,
472 SmallVector<unsigned, 4> &Defs) {
473 while (!Defs.empty()) {
474 unsigned Reg = Defs.back();
475 Defs.pop_back();
Evan Cheng1c3ee662008-04-16 09:46:40 +0000476 PhysRegDef[Reg] = MI;
477 PhysRegUse[Reg] = NULL;
Evan Chengc7daf1f2008-03-05 00:59:57 +0000478 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000480 PhysRegDef[SubReg] = MI;
481 PhysRegUse[SubReg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 }
483 }
484}
485
Evan Chengd062bf72009-09-23 06:28:31 +0000486namespace {
487 struct RegSorter {
488 const TargetRegisterInfo *TRI;
489
490 RegSorter(const TargetRegisterInfo *tri) : TRI(tri) { }
491 bool operator()(unsigned A, unsigned B) {
492 if (TRI->isSubRegister(A, B))
493 return true;
494 else if (TRI->isSubRegister(B, A))
495 return false;
496 return A < B;
497 }
498 };
499}
500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
502 MF = &mf;
Evan Cheng251fa152008-04-02 18:04:08 +0000503 MRI = &mf.getRegInfo();
Evan Chengc7daf1f2008-03-05 00:59:57 +0000504 TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505
Evan Chengc7daf1f2008-03-05 00:59:57 +0000506 ReservedRegisters = TRI->getReservedRegs(mf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507
Evan Chengc7daf1f2008-03-05 00:59:57 +0000508 unsigned NumRegs = TRI->getNumRegs();
Evan Cheng1c3ee662008-04-16 09:46:40 +0000509 PhysRegDef = new MachineInstr*[NumRegs];
510 PhysRegUse = new MachineInstr*[NumRegs];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
Evan Cheng1c3ee662008-04-16 09:46:40 +0000512 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
513 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Jakob Stoklund Olesen4bfc2ff2010-02-23 22:43:58 +0000514 PHIJoins.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515
Bill Wendling85b03762008-02-20 09:15:16 +0000516 /// Get some space for a respectable number of registers.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 VirtRegInfo.resize(64);
518
519 analyzePHINodes(mf);
520
521 // Calculate live variable information in depth first order on the CFG of the
522 // function. This guarantees that we will see the definition of a virtual
523 // register before its uses due to dominance properties of SSA (except for PHI
524 // nodes, which are treated as a special case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 MachineBasicBlock *Entry = MF->begin();
526 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling85b03762008-02-20 09:15:16 +0000527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
529 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
530 DFI != E; ++DFI) {
531 MachineBasicBlock *MBB = *DFI;
532
533 // Mark live-in registers as live-in.
Evan Chengd062bf72009-09-23 06:28:31 +0000534 SmallVector<unsigned, 4> Defs;
Dan Gohman1e60b692010-04-13 16:57:55 +0000535 for (MachineBasicBlock::livein_iterator II = MBB->livein_begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000537 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 "Cannot have a live-in virtual register!");
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000539 HandlePhysRegDef(*II, 0, Defs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 }
541
542 // Loop over all of the instructions, processing them.
Evan Cheng251fa152008-04-02 18:04:08 +0000543 DistanceMap.clear();
544 unsigned Dist = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
546 I != E; ++I) {
547 MachineInstr *MI = I;
Chris Lattner4052b292010-02-09 19:54:29 +0000548 if (MI->isDebugValue())
Dale Johannesenfe5c3802010-02-09 02:01:46 +0000549 continue;
Evan Cheng251fa152008-04-02 18:04:08 +0000550 DistanceMap.insert(std::make_pair(MI, Dist++));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551
552 // Process all of the operands of the instruction...
553 unsigned NumOperandsToProcess = MI->getNumOperands();
554
555 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
556 // of the uses. They will be handled in other basic blocks.
Chris Lattner4052b292010-02-09 19:54:29 +0000557 if (MI->isPHI())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 NumOperandsToProcess = 1;
559
Evan Chengae54e1b2010-03-26 02:12:24 +0000560 // Clear kill and dead markers. LV will recompute them.
Evan Cheng1c3ee662008-04-16 09:46:40 +0000561 SmallVector<unsigned, 4> UseRegs;
562 SmallVector<unsigned, 4> DefRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Evan Chengae54e1b2010-03-26 02:12:24 +0000564 MachineOperand &MO = MI->getOperand(i);
Evan Cheng06df4d02009-01-20 21:25:12 +0000565 if (!MO.isReg() || MO.getReg() == 0)
566 continue;
567 unsigned MOReg = MO.getReg();
Evan Chengae54e1b2010-03-26 02:12:24 +0000568 if (MO.isUse()) {
569 MO.setIsKill(false);
Evan Cheng06df4d02009-01-20 21:25:12 +0000570 UseRegs.push_back(MOReg);
Evan Chengae54e1b2010-03-26 02:12:24 +0000571 } else /*MO.isDef()*/ {
572 MO.setIsDead(false);
Evan Cheng06df4d02009-01-20 21:25:12 +0000573 DefRegs.push_back(MOReg);
Evan Chengae54e1b2010-03-26 02:12:24 +0000574 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 }
576
Evan Cheng1c3ee662008-04-16 09:46:40 +0000577 // Process all uses.
578 for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) {
579 unsigned MOReg = UseRegs[i];
580 if (TargetRegisterInfo::isVirtualRegister(MOReg))
581 HandleVirtRegUse(MOReg, MBB, MI);
Dan Gohman706847e2008-09-21 21:11:41 +0000582 else if (!ReservedRegisters[MOReg])
Evan Cheng1c3ee662008-04-16 09:46:40 +0000583 HandlePhysRegUse(MOReg, MI);
584 }
585
Bill Wendling85b03762008-02-20 09:15:16 +0000586 // Process all defs.
Evan Cheng1c3ee662008-04-16 09:46:40 +0000587 for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
588 unsigned MOReg = DefRegs[i];
Dan Gohman706847e2008-09-21 21:11:41 +0000589 if (TargetRegisterInfo::isVirtualRegister(MOReg))
590 HandleVirtRegDef(MOReg, MI);
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000591 else if (!ReservedRegisters[MOReg])
592 HandlePhysRegDef(MOReg, MI, Defs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 }
Evan Chengd062bf72009-09-23 06:28:31 +0000594 UpdatePhysRegDefs(MI, Defs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 }
596
597 // Handle any virtual assignments from PHI nodes which might be at the
598 // bottom of this basic block. We check all of our successor blocks to see
599 // if they have PHI nodes, and if so, we simulate an assignment at the end
600 // of the current block.
601 if (!PHIVarInfo[MBB->getNumber()].empty()) {
602 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
603
604 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000605 E = VarInfoVec.end(); I != E; ++I)
606 // Mark it alive only in the block we are representing.
Evan Cheng251fa152008-04-02 18:04:08 +0000607 MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
Owen Anderson77d80492008-01-15 22:58:11 +0000608 MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 }
610
Bill Wendling85b03762008-02-20 09:15:16 +0000611 // Finally, if the last instruction in the block is a return, make sure to
612 // mark it as using all of the live-out values in the function.
Dale Johannesenfd642742010-06-05 00:30:45 +0000613 // Things marked both call and return are tail calls; do not do this for
614 // them. The tail callee need not take the same registers as input
615 // that it produces as output, and there are dependencies for its input
616 // registers elsewhere.
617 if (!MBB->empty() && MBB->back().getDesc().isReturn()
618 && !MBB->back().getDesc().isCall()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 MachineInstr *Ret = &MBB->back();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000620
Chris Lattner1b989192007-12-31 04:13:23 +0000621 for (MachineRegisterInfo::liveout_iterator
622 I = MF->getRegInfo().liveout_begin(),
623 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000624 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Dan Gohman2d702012008-06-25 22:14:43 +0000625 "Cannot have a live-out virtual register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 HandlePhysRegUse(*I, Ret);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 // Add live-out registers as implicit uses.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000629 if (!Ret->readsRegister(*I))
Chris Lattner63ab1f22007-12-30 00:41:17 +0000630 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 }
632 }
633
Evan Cheng1c3ee662008-04-16 09:46:40 +0000634 // Loop over PhysRegDef / PhysRegUse, killing any registers that are
635 // available at the end of the basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 for (unsigned i = 0; i != NumRegs; ++i)
Evan Cheng04f3d1d2009-09-24 02:15:22 +0000637 if (PhysRegDef[i] || PhysRegUse[i])
638 HandlePhysRegDef(i, 0, Defs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639
Evan Cheng1c3ee662008-04-16 09:46:40 +0000640 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
641 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 }
643
644 // Convert and transfer the dead / killed information we have gathered into
645 // VirtRegInfo onto MI's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000647 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
648 if (VirtRegInfo[i].Kills[j] ==
Evan Cheng251fa152008-04-02 18:04:08 +0000649 MRI->getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000650 VirtRegInfo[i]
651 .Kills[j]->addRegisterDead(i +
652 TargetRegisterInfo::FirstVirtualRegister,
Evan Chengc7daf1f2008-03-05 00:59:57 +0000653 TRI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 else
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000655 VirtRegInfo[i]
656 .Kills[j]->addRegisterKilled(i +
657 TargetRegisterInfo::FirstVirtualRegister,
Evan Chengc7daf1f2008-03-05 00:59:57 +0000658 TRI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659
660 // Check to make sure there are no unreachable blocks in the MC CFG for the
661 // function. If so, it is due to a bug in the instruction selector or some
662 // other part of the code generator if this happens.
663#ifndef NDEBUG
664 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
665 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
666#endif
667
Evan Cheng1c3ee662008-04-16 09:46:40 +0000668 delete[] PhysRegDef;
669 delete[] PhysRegUse;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 delete[] PHIVarInfo;
671
672 return false;
673}
674
Evan Chengd1c7e8f2008-07-03 00:07:19 +0000675/// replaceKillInstruction - Update register kill info by replacing a kill
676/// instruction with a new one.
677void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr *OldMI,
678 MachineInstr *NewMI) {
679 VarInfo &VI = getVarInfo(Reg);
Evan Chengc2c8ebb2008-07-03 00:28:27 +0000680 std::replace(VI.Kills.begin(), VI.Kills.end(), OldMI, NewMI);
Evan Chengd1c7e8f2008-07-03 00:07:19 +0000681}
682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683/// removeVirtualRegistersKilled - Remove all killed info for the specified
684/// instruction.
685void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
686 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
687 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000688 if (MO.isReg() && MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000689 MO.setIsKill(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000691 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 bool removed = getVarInfo(Reg).removeKill(MI);
693 assert(removed && "kill not in register's VarInfo?");
Devang Patel4354f5c2008-11-21 20:00:59 +0000694 removed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 }
696 }
697 }
698}
699
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling85b03762008-02-20 09:15:16 +0000701/// particular, we want to map the variable information of a virtual register
702/// which is used in a PHI node. We map that to the BB the vreg is coming from.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703///
704void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
705 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
706 I != E; ++I)
707 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
Chris Lattner4052b292010-02-09 19:54:29 +0000708 BBI != BBE && BBI->isPHI(); ++BBI)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendlingb88bca92008-02-20 06:10:21 +0000710 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
711 .push_back(BBI->getOperand(i).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712}
Jakob Stoklund Olesenbe9cdbf2009-11-10 22:01:05 +0000713
Jakob Stoklund Olesene79ff412009-11-21 02:05:21 +0000714bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB,
715 unsigned Reg,
716 MachineRegisterInfo &MRI) {
717 unsigned Num = MBB.getNumber();
718
719 // Reg is live-through.
720 if (AliveBlocks.test(Num))
721 return true;
722
723 // Registers defined in MBB cannot be live in.
724 const MachineInstr *Def = MRI.getVRegDef(Reg);
725 if (Def && Def->getParent() == &MBB)
726 return false;
727
728 // Reg was not defined in MBB, was it killed here?
729 return findKill(&MBB);
730}
731
Jakob Stoklund Olesen9a929cf2009-12-01 17:13:31 +0000732bool LiveVariables::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB) {
733 LiveVariables::VarInfo &VI = getVarInfo(Reg);
734
735 // Loop over all of the successors of the basic block, checking to see if
736 // the value is either live in the block, or if it is killed in the block.
737 std::vector<MachineBasicBlock*> OpSuccBlocks;
738 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
739 E = MBB.succ_end(); SI != E; ++SI) {
740 MachineBasicBlock *SuccMBB = *SI;
741
742 // Is it alive in this successor?
743 unsigned SuccIdx = SuccMBB->getNumber();
744 if (VI.AliveBlocks.test(SuccIdx))
745 return true;
746 OpSuccBlocks.push_back(SuccMBB);
747 }
748
749 // Check to see if this value is live because there is a use in a successor
750 // that kills it.
751 switch (OpSuccBlocks.size()) {
752 case 1: {
753 MachineBasicBlock *SuccMBB = OpSuccBlocks[0];
754 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
755 if (VI.Kills[i]->getParent() == SuccMBB)
756 return true;
757 break;
758 }
759 case 2: {
760 MachineBasicBlock *SuccMBB1 = OpSuccBlocks[0], *SuccMBB2 = OpSuccBlocks[1];
761 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
762 if (VI.Kills[i]->getParent() == SuccMBB1 ||
763 VI.Kills[i]->getParent() == SuccMBB2)
764 return true;
765 break;
766 }
767 default:
768 std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
769 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
770 if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
771 VI.Kills[i]->getParent()))
772 return true;
773 }
774 return false;
775}
776
Jakob Stoklund Olesen409558d2009-11-11 19:31:31 +0000777/// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
778/// variables that are live out of DomBB will be marked as passing live through
779/// BB.
780void LiveVariables::addNewBlock(MachineBasicBlock *BB,
Jakob Stoklund Olesene79ff412009-11-21 02:05:21 +0000781 MachineBasicBlock *DomBB,
782 MachineBasicBlock *SuccBB) {
Jakob Stoklund Olesen409558d2009-11-11 19:31:31 +0000783 const unsigned NumNew = BB->getNumber();
Jakob Stoklund Olesene79ff412009-11-21 02:05:21 +0000784
785 // All registers used by PHI nodes in SuccBB must be live through BB.
786 for (MachineBasicBlock::const_iterator BBI = SuccBB->begin(),
Chris Lattner4052b292010-02-09 19:54:29 +0000787 BBE = SuccBB->end(); BBI != BBE && BBI->isPHI(); ++BBI)
Jakob Stoklund Olesene79ff412009-11-21 02:05:21 +0000788 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
789 if (BBI->getOperand(i+1).getMBB() == BB)
790 getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
Jakob Stoklund Olesenbe9cdbf2009-11-10 22:01:05 +0000791
792 // Update info for all live variables
Jakob Stoklund Olesen409558d2009-11-11 19:31:31 +0000793 for (unsigned Reg = TargetRegisterInfo::FirstVirtualRegister,
794 E = MRI->getLastVirtReg()+1; Reg != E; ++Reg) {
795 VarInfo &VI = getVarInfo(Reg);
Jakob Stoklund Olesene79ff412009-11-21 02:05:21 +0000796 if (!VI.AliveBlocks.test(NumNew) && VI.isLiveIn(*SuccBB, Reg, *MRI))
Jakob Stoklund Olesen409558d2009-11-11 19:31:31 +0000797 VI.AliveBlocks.set(NumNew);
Jakob Stoklund Olesenbe9cdbf2009-11-10 22:01:05 +0000798 }
799}