blob: 54b9cc18f8528aa33fd99a0488c8f0576985450c [file] [log] [blame]
Chris Lattnerbc40e892003-01-13 20:01:16 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattner5cdfbad2003-05-07 20:08:36 +000010// 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//
Chris Lattnerbc40e892003-01-13 20:01:16 +000027//===----------------------------------------------------------------------===//
28
29#include "llvm/CodeGen/LiveVariables.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000032#include "llvm/CodeGen/Passes.h"
David Greene1d44df62010-01-04 23:02:10 +000033#include "llvm/Support/Debug.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000034#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000035#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng04104072007-06-27 05:23:00 +000037#include "llvm/ADT/SmallPtrSet.h"
Owen Andersonbffdf662008-06-27 07:05:59 +000038#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000039#include "llvm/ADT/STLExtras.h"
Chris Lattner657b4d12005-08-24 00:09:33 +000040#include <algorithm>
Chris Lattner49a5aaa2004-01-30 22:08:53 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Devang Patel19974732007-05-03 01:11:54 +000043char LiveVariables::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000044INITIALIZE_PASS_BEGIN(LiveVariables, "livevars",
45 "Live Variable Analysis", false, false)
46INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)
47INITIALIZE_PASS_END(LiveVariables, "livevars",
Owen Andersonce665bd2010-10-07 22:25:06 +000048 "Live Variable Analysis", false, false)
Chris Lattnerbc40e892003-01-13 20:01:16 +000049
Owen Andersonbd3ba462008-08-04 23:54:43 +000050
51void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequiredID(UnreachableMachineBlockElimID);
53 AU.setPreservesAll();
Dan Gohmanad2afc22009-07-31 18:16:33 +000054 MachineFunctionPass::getAnalysisUsage(AU);
Owen Andersonbd3ba462008-08-04 23:54:43 +000055}
56
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000057MachineInstr *
58LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
59 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
60 if (Kills[i]->getParent() == MBB)
61 return Kills[i];
62 return NULL;
63}
64
Chris Lattnerdacceef2006-01-04 05:40:30 +000065void LiveVariables::VarInfo::dump() const {
David Greene1d44df62010-01-04 23:02:10 +000066 dbgs() << " Alive in blocks: ";
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +000067 for (SparseBitVector<>::iterator I = AliveBlocks.begin(),
68 E = AliveBlocks.end(); I != E; ++I)
David Greene1d44df62010-01-04 23:02:10 +000069 dbgs() << *I << ", ";
70 dbgs() << "\n Killed by:";
Chris Lattnerdacceef2006-01-04 05:40:30 +000071 if (Kills.empty())
David Greene1d44df62010-01-04 23:02:10 +000072 dbgs() << " No instructions.\n";
Chris Lattnerdacceef2006-01-04 05:40:30 +000073 else {
74 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
David Greene1d44df62010-01-04 23:02:10 +000075 dbgs() << "\n #" << i << ": " << *Kills[i];
76 dbgs() << "\n";
Chris Lattnerdacceef2006-01-04 05:40:30 +000077 }
78}
79
Bill Wendling90a38682008-02-20 06:10:21 +000080/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
Chris Lattnerfb2cb692003-05-12 14:24:00 +000081LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000082 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000083 "getVarInfo: not a virtual register!");
Jakob Stoklund Olesenb421c562011-01-08 23:10:57 +000084 VirtRegInfo.grow(RegIdx);
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +000085 return VirtRegInfo[RegIdx];
Chris Lattnerfb2cb692003-05-12 14:24:00 +000086}
87
Owen Anderson40a627d2008-01-15 22:58:11 +000088void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
89 MachineBasicBlock *DefBlock,
Evan Cheng56184902007-05-08 19:00:00 +000090 MachineBasicBlock *MBB,
91 std::vector<MachineBasicBlock*> &WorkList) {
Chris Lattner8ba97712004-07-01 04:29:47 +000092 unsigned BBNum = MBB->getNumber();
Owen Anderson7047dd42008-01-15 22:02:46 +000093
Chris Lattnerbc40e892003-01-13 20:01:16 +000094 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendling90a38682008-02-20 06:10:21 +000095 // remove it.
Chris Lattnerbc40e892003-01-13 20:01:16 +000096 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +000097 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000098 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
99 break;
100 }
Owen Anderson7047dd42008-01-15 22:02:46 +0000101
Owen Anderson40a627d2008-01-15 22:58:11 +0000102 if (MBB == DefBlock) return; // Terminate recursion
Chris Lattnerbc40e892003-01-13 20:01:16 +0000103
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000104 if (VRInfo.AliveBlocks.test(BBNum))
Chris Lattnerbc40e892003-01-13 20:01:16 +0000105 return; // We already know the block is live
106
107 // Mark the variable known alive in this bb
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000108 VRInfo.AliveBlocks.set(BBNum);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000109
Benjamin Kramerf337fb22011-03-08 17:28:36 +0000110 WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000111}
112
Bill Wendling420cdeb2008-02-20 07:36:31 +0000113void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Owen Anderson40a627d2008-01-15 22:58:11 +0000114 MachineBasicBlock *DefBlock,
Evan Cheng56184902007-05-08 19:00:00 +0000115 MachineBasicBlock *MBB) {
116 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson40a627d2008-01-15 22:58:11 +0000117 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000118
Evan Cheng56184902007-05-08 19:00:00 +0000119 while (!WorkList.empty()) {
120 MachineBasicBlock *Pred = WorkList.back();
121 WorkList.pop_back();
Owen Anderson40a627d2008-01-15 22:58:11 +0000122 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Evan Cheng56184902007-05-08 19:00:00 +0000123 }
124}
125
Owen Anderson7047dd42008-01-15 22:02:46 +0000126void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Misha Brukman09ba9062004-06-24 21:31:16 +0000127 MachineInstr *MI) {
Evan Chengea1d9cd2008-04-02 18:04:08 +0000128 assert(MRI->getVRegDef(reg) && "Register use before def!");
Alkis Evlogimenos2e58a412004-09-01 22:34:52 +0000129
Owen Andersona0185402007-11-08 01:20:48 +0000130 unsigned BBNum = MBB->getNumber();
131
Owen Anderson7047dd42008-01-15 22:02:46 +0000132 VarInfo& VRInfo = getVarInfo(reg);
Evan Chengc6a24102007-03-17 09:29:54 +0000133
Bill Wendling90a38682008-02-20 06:10:21 +0000134 // Check to see if this basic block is already a kill block.
Chris Lattner74de8b12004-07-19 07:04:55 +0000135 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendling90a38682008-02-20 06:10:21 +0000136 // Yes, this register is killed in this basic block already. Increase the
Chris Lattnerbc40e892003-01-13 20:01:16 +0000137 // live range by updating the kill instruction.
Chris Lattner74de8b12004-07-19 07:04:55 +0000138 VRInfo.Kills.back() = MI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000139 return;
140 }
141
142#ifndef NDEBUG
143 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000144 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000145#endif
146
Bill Wendlingebcba612008-06-23 23:41:14 +0000147 // This situation can occur:
148 //
149 // ,------.
150 // | |
151 // | v
152 // | t2 = phi ... t1 ...
153 // | |
154 // | v
155 // | t1 = ...
156 // | ... = ... t1 ...
157 // | |
158 // `------'
159 //
160 // where there is a use in a PHI node that's a predecessor to the defining
161 // block. We don't want to mark all predecessors as having the value "alive"
162 // in this case.
163 if (MBB == MRI->getVRegDef(reg)->getParent()) return;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000164
Bill Wendling90a38682008-02-20 06:10:21 +0000165 // Add a new kill entry for this basic block. If this virtual register is
166 // already marked as alive in this basic block, that means it is alive in at
167 // least one of the successor blocks, it's not a kill.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000168 if (!VRInfo.AliveBlocks.test(BBNum))
Evan Chenge2ee9962007-03-09 09:48:56 +0000169 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000170
Bill Wendling420cdeb2008-02-20 07:36:31 +0000171 // Update all dominating blocks to mark them as "known live".
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000172 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
173 E = MBB->pred_end(); PI != E; ++PI)
Evan Chengea1d9cd2008-04-02 18:04:08 +0000174 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000175}
176
Dan Gohman3bdf5fe2008-09-21 21:11:41 +0000177void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr *MI) {
178 VarInfo &VRInfo = getVarInfo(Reg);
179
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000180 if (VRInfo.AliveBlocks.empty())
Dan Gohman3bdf5fe2008-09-21 21:11:41 +0000181 // If vr is not alive in any block, then defaults to dead.
182 VRInfo.Kills.push_back(MI);
183}
184
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000185/// FindLastPartialDef - Return the last partial def of the specified register.
Evan Cheng60c7df22009-09-22 08:34:46 +0000186/// Also returns the sub-registers that're defined by the instruction.
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000187MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg,
Evan Cheng60c7df22009-09-22 08:34:46 +0000188 SmallSet<unsigned,4> &PartDefRegs) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000189 unsigned LastDefReg = 0;
190 unsigned LastDefDist = 0;
191 MachineInstr *LastDef = NULL;
192 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
193 unsigned SubReg = *SubRegs; ++SubRegs) {
194 MachineInstr *Def = PhysRegDef[SubReg];
195 if (!Def)
196 continue;
197 unsigned Dist = DistanceMap[Def];
198 if (Dist > LastDefDist) {
199 LastDefReg = SubReg;
200 LastDef = Def;
201 LastDefDist = Dist;
202 }
203 }
Evan Cheng60c7df22009-09-22 08:34:46 +0000204
205 if (!LastDef)
206 return 0;
207
208 PartDefRegs.insert(LastDefReg);
209 for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) {
210 MachineOperand &MO = LastDef->getOperand(i);
211 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
212 continue;
213 unsigned DefReg = MO.getReg();
214 if (TRI->isSubRegister(Reg, DefReg)) {
215 PartDefRegs.insert(DefReg);
216 for (const unsigned *SubRegs = TRI->getSubRegisters(DefReg);
217 unsigned SubReg = *SubRegs; ++SubRegs)
218 PartDefRegs.insert(SubReg);
219 }
220 }
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000221 return LastDef;
222}
223
Bill Wendling6d794742008-02-20 09:15:16 +0000224/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
225/// implicit defs to a machine instruction if there was an earlier def of its
226/// super-register.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000227void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng236490d2009-11-13 20:36:40 +0000228 MachineInstr *LastDef = PhysRegDef[Reg];
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000229 // If there was a previous use or a "full" def all is well.
Evan Cheng236490d2009-11-13 20:36:40 +0000230 if (!LastDef && !PhysRegUse[Reg]) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000231 // Otherwise, the last sub-register def implicitly defines this register.
232 // e.g.
233 // AH =
234 // AL = ... <imp-def EAX>, <imp-kill AH>
235 // = AH
236 // ...
237 // = EAX
238 // All of the sub-registers must have been defined before the use of Reg!
Evan Cheng60c7df22009-09-22 08:34:46 +0000239 SmallSet<unsigned, 4> PartDefRegs;
240 MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs);
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000241 // If LastPartialDef is NULL, it must be using a livein register.
242 if (LastPartialDef) {
243 LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
244 true/*IsImp*/));
245 PhysRegDef[Reg] = LastPartialDef;
Owen Andersonbbf55832008-08-14 23:41:38 +0000246 SmallSet<unsigned, 8> Processed;
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000247 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
248 unsigned SubReg = *SubRegs; ++SubRegs) {
249 if (Processed.count(SubReg))
250 continue;
Evan Cheng60c7df22009-09-22 08:34:46 +0000251 if (PartDefRegs.count(SubReg))
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000252 continue;
253 // This part of Reg was defined before the last partial def. It's killed
254 // here.
255 LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
256 false/*IsDef*/,
257 true/*IsImp*/));
258 PhysRegDef[SubReg] = LastPartialDef;
259 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
260 Processed.insert(*SS);
261 }
262 }
Evan Chengbfe8afa2012-01-14 01:53:46 +0000263 } else if (LastDef && !PhysRegUse[Reg] &&
264 !LastDef->findRegisterDefOperand(Reg))
Evan Cheng236490d2009-11-13 20:36:40 +0000265 // Last def defines the super register, add an implicit def of reg.
Evan Chengbfe8afa2012-01-14 01:53:46 +0000266 LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
267 true/*IsImp*/));
Bill Wendling90a38682008-02-20 06:10:21 +0000268
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000269 // Remember this use.
270 PhysRegUse[Reg] = MI;
Evan Cheng6130f662008-03-05 00:59:57 +0000271 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000272 unsigned SubReg = *SubRegs; ++SubRegs)
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000273 PhysRegUse[SubReg] = MI;
Evan Cheng4efe7412007-06-26 21:03:35 +0000274}
275
Evan Chenga4025df2009-12-01 00:44:45 +0000276/// FindLastRefOrPartRef - Return the last reference or partial reference of
277/// the specified register.
278MachineInstr *LiveVariables::FindLastRefOrPartRef(unsigned Reg) {
279 MachineInstr *LastDef = PhysRegDef[Reg];
280 MachineInstr *LastUse = PhysRegUse[Reg];
281 if (!LastDef && !LastUse)
Chris Lattner98cdfc72010-06-14 18:28:34 +0000282 return 0;
Evan Chenga4025df2009-12-01 00:44:45 +0000283
284 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
285 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
Evan Chenga4025df2009-12-01 00:44:45 +0000286 unsigned LastPartDefDist = 0;
287 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
288 unsigned SubReg = *SubRegs; ++SubRegs) {
289 MachineInstr *Def = PhysRegDef[SubReg];
290 if (Def && Def != LastDef) {
291 // There was a def of this sub-register in between. This is a partial
292 // def, keep track of the last one.
293 unsigned Dist = DistanceMap[Def];
Benjamin Kramere7078ae2010-01-07 17:29:08 +0000294 if (Dist > LastPartDefDist)
Evan Chenga4025df2009-12-01 00:44:45 +0000295 LastPartDefDist = Dist;
Benjamin Kramere7078ae2010-01-07 17:29:08 +0000296 } else if (MachineInstr *Use = PhysRegUse[SubReg]) {
Evan Chenga4025df2009-12-01 00:44:45 +0000297 unsigned Dist = DistanceMap[Use];
298 if (Dist > LastRefOrPartRefDist) {
299 LastRefOrPartRefDist = Dist;
300 LastRefOrPartRef = Use;
301 }
302 }
303 }
304
305 return LastRefOrPartRef;
306}
307
Evan Chenga894ae12009-01-20 21:25:12 +0000308bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) {
Evan Chengad934b82009-09-24 02:15:22 +0000309 MachineInstr *LastDef = PhysRegDef[Reg];
310 MachineInstr *LastUse = PhysRegUse[Reg];
311 if (!LastDef && !LastUse)
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000312 return false;
313
Evan Chengad934b82009-09-24 02:15:22 +0000314 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000315 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
316 // The whole register is used.
317 // AL =
318 // AH =
319 //
320 // = AX
321 // = AL, AX<imp-use, kill>
322 // AX =
323 //
324 // Or whole register is defined, but not used at all.
325 // AX<dead> =
326 // ...
327 // AX =
328 //
329 // Or whole register is defined, but only partly used.
330 // AX<dead> = AL<imp-def>
331 // = AL<kill>
332 // AX =
Evan Chengad934b82009-09-24 02:15:22 +0000333 MachineInstr *LastPartDef = 0;
334 unsigned LastPartDefDist = 0;
Owen Andersonbbf55832008-08-14 23:41:38 +0000335 SmallSet<unsigned, 8> PartUses;
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000336 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
337 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Chengad934b82009-09-24 02:15:22 +0000338 MachineInstr *Def = PhysRegDef[SubReg];
339 if (Def && Def != LastDef) {
340 // There was a def of this sub-register in between. This is a partial
341 // def, keep track of the last one.
342 unsigned Dist = DistanceMap[Def];
343 if (Dist > LastPartDefDist) {
344 LastPartDefDist = Dist;
345 LastPartDef = Def;
346 }
347 continue;
348 }
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000349 if (MachineInstr *Use = PhysRegUse[SubReg]) {
350 PartUses.insert(SubReg);
351 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
352 PartUses.insert(*SS);
353 unsigned Dist = DistanceMap[Use];
354 if (Dist > LastRefOrPartRefDist) {
355 LastRefOrPartRefDist = Dist;
356 LastRefOrPartRef = Use;
Evan Cheng4efe7412007-06-26 21:03:35 +0000357 }
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000358 }
359 }
Evan Chenga894ae12009-01-20 21:25:12 +0000360
Jakob Stoklund Olesen53e000b2010-03-05 21:49:17 +0000361 if (!PhysRegUse[Reg]) {
Evan Chengad934b82009-09-24 02:15:22 +0000362 // Partial uses. Mark register def dead and add implicit def of
363 // sub-registers which are used.
364 // EAX<dead> = op AL<imp-def>
365 // That is, EAX def is dead but AL def extends pass it.
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000366 PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
367 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
368 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Chengad934b82009-09-24 02:15:22 +0000369 if (!PartUses.count(SubReg))
370 continue;
371 bool NeedDef = true;
372 if (PhysRegDef[Reg] == PhysRegDef[SubReg]) {
373 MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg);
374 if (MO) {
375 NeedDef = false;
376 assert(!MO->isDead());
Evan Cheng2c4d96d2009-07-06 21:34:05 +0000377 }
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000378 }
Evan Chengad934b82009-09-24 02:15:22 +0000379 if (NeedDef)
380 PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
381 true/*IsDef*/, true/*IsImp*/));
Evan Chenga4025df2009-12-01 00:44:45 +0000382 MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
383 if (LastSubRef)
384 LastSubRef->addRegisterKilled(SubReg, TRI, true);
385 else {
386 LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
387 PhysRegUse[SubReg] = LastRefOrPartRef;
388 for (const unsigned *SSRegs = TRI->getSubRegisters(SubReg);
389 unsigned SSReg = *SSRegs; ++SSRegs)
390 PhysRegUse[SSReg] = LastRefOrPartRef;
391 }
Evan Chengad934b82009-09-24 02:15:22 +0000392 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
393 PartUses.erase(*SS);
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000394 }
Jakob Stoklund Olesen53e000b2010-03-05 21:49:17 +0000395 } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) {
396 if (LastPartDef)
397 // The last partial def kills the register.
398 LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
399 true/*IsImp*/, true/*IsKill*/));
400 else {
401 MachineOperand *MO =
402 LastRefOrPartRef->findRegisterDefOperand(Reg, false, TRI);
403 bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg;
404 // If the last reference is the last def, then it's not used at all.
405 // That is, unless we are currently processing the last reference itself.
406 LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
407 if (NeedEC) {
408 // If we are adding a subreg def and the superreg def is marked early
409 // clobber, add an early clobber marker to the subreg def.
410 MO = LastRefOrPartRef->findRegisterDefOperand(Reg);
411 if (MO)
412 MO->setIsEarlyClobber();
413 }
414 }
Evan Chengad934b82009-09-24 02:15:22 +0000415 } else
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000416 LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
417 return true;
418}
419
Jakob Stoklund Olesen8c47ad82012-01-21 00:58:53 +0000420void LiveVariables::HandleRegMask(const MachineOperand &MO) {
421 // Call HandlePhysRegKill() for all live registers clobbered by Mask.
422 // Clobbered registers are always dead, sp there is no need to use
423 // HandlePhysRegDef().
424 for (unsigned Reg = 1, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) {
425 // Skip dead regs.
426 if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
427 continue;
428 // Skip mask-preserved regs.
429 if (!MO.clobbersPhysReg(Reg));
430 continue;
431 // Kill the largest clobbered super-register.
432 // This avoids needless implicit operands.
433 unsigned Super = Reg;
434 for (const unsigned *SR = TRI->getSuperRegisters(Reg); *SR; ++SR)
435 if ((PhysRegDef[*SR] || PhysRegUse[*SR]) && MO.clobbersPhysReg(*SR))
436 Super = *SR;
437 HandlePhysRegKill(Super, 0);
438 }
439}
440
Evan Cheng296925d2009-09-23 06:28:31 +0000441void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI,
Evan Chengad934b82009-09-24 02:15:22 +0000442 SmallVector<unsigned, 4> &Defs) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000443 // What parts of the register are previously defined?
Owen Andersonbffdf662008-06-27 07:05:59 +0000444 SmallSet<unsigned, 32> Live;
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000445 if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
446 Live.insert(Reg);
447 for (const unsigned *SS = TRI->getSubRegisters(Reg); *SS; ++SS)
448 Live.insert(*SS);
449 } else {
450 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
451 unsigned SubReg = *SubRegs; ++SubRegs) {
452 // If a register isn't itself defined, but all parts that make up of it
453 // are defined, then consider it also defined.
454 // e.g.
455 // AL =
456 // AH =
457 // = AX
Evan Chengad934b82009-09-24 02:15:22 +0000458 if (Live.count(SubReg))
459 continue;
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000460 if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
461 Live.insert(SubReg);
462 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
463 Live.insert(*SS);
464 }
Bill Wendling420cdeb2008-02-20 07:36:31 +0000465 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000466 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000467
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000468 // Start from the largest piece, find the last time any part of the register
469 // is referenced.
Evan Chengad934b82009-09-24 02:15:22 +0000470 HandlePhysRegKill(Reg, MI);
471 // Only some of the sub-registers are used.
472 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
473 unsigned SubReg = *SubRegs; ++SubRegs) {
474 if (!Live.count(SubReg))
475 // Skip if this sub-register isn't defined.
476 continue;
477 HandlePhysRegKill(SubReg, MI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000478 }
479
Evan Chengad934b82009-09-24 02:15:22 +0000480 if (MI)
481 Defs.push_back(Reg); // Remember this def.
Evan Cheng296925d2009-09-23 06:28:31 +0000482}
483
484void LiveVariables::UpdatePhysRegDefs(MachineInstr *MI,
485 SmallVector<unsigned, 4> &Defs) {
486 while (!Defs.empty()) {
487 unsigned Reg = Defs.back();
488 Defs.pop_back();
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000489 PhysRegDef[Reg] = MI;
490 PhysRegUse[Reg] = NULL;
Evan Cheng6130f662008-03-05 00:59:57 +0000491 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Evan Cheng4efe7412007-06-26 21:03:35 +0000492 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000493 PhysRegDef[SubReg] = MI;
494 PhysRegUse[SubReg] = NULL;
Evan Cheng4efe7412007-06-26 21:03:35 +0000495 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000496 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000497}
498
Evan Chengc6a24102007-03-17 09:29:54 +0000499bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
500 MF = &mf;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000501 MRI = &mf.getRegInfo();
Evan Cheng6130f662008-03-05 00:59:57 +0000502 TRI = MF->getTarget().getRegisterInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000503
Evan Cheng6130f662008-03-05 00:59:57 +0000504 ReservedRegisters = TRI->getReservedRegs(mf);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000505
Evan Cheng6130f662008-03-05 00:59:57 +0000506 unsigned NumRegs = TRI->getNumRegs();
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000507 PhysRegDef = new MachineInstr*[NumRegs];
508 PhysRegUse = new MachineInstr*[NumRegs];
Evan Chenge96f5012007-04-25 19:34:00 +0000509 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000510 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
511 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Jakob Stoklund Olesendcfe5f32010-02-23 22:43:58 +0000512 PHIJoins.clear();
Chris Lattnerbc40e892003-01-13 20:01:16 +0000513
Evan Chengc6a24102007-03-17 09:29:54 +0000514 analyzePHINodes(mf);
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000515
Chris Lattnerbc40e892003-01-13 20:01:16 +0000516 // Calculate live variable information in depth first order on the CFG of the
517 // function. This guarantees that we will see the definition of a virtual
518 // register before its uses due to dominance properties of SSA (except for PHI
519 // nodes, which are treated as a special case).
Evan Chengc6a24102007-03-17 09:29:54 +0000520 MachineBasicBlock *Entry = MF->begin();
Evan Cheng04104072007-06-27 05:23:00 +0000521 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling6d794742008-02-20 09:15:16 +0000522
Evan Cheng04104072007-06-27 05:23:00 +0000523 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
524 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
525 DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000526 MachineBasicBlock *MBB = *DFI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000527
Evan Chengb371f452007-02-19 21:49:54 +0000528 // Mark live-in registers as live-in.
Evan Cheng296925d2009-09-23 06:28:31 +0000529 SmallVector<unsigned, 4> Defs;
Dan Gohman81bf03e2010-04-13 16:57:55 +0000530 for (MachineBasicBlock::livein_iterator II = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000531 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000532 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000533 "Cannot have a live-in virtual register!");
Evan Chengad934b82009-09-24 02:15:22 +0000534 HandlePhysRegDef(*II, 0, Defs);
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000535 }
536
Chris Lattnerbc40e892003-01-13 20:01:16 +0000537 // Loop over all of the instructions, processing them.
Evan Chengea1d9cd2008-04-02 18:04:08 +0000538 DistanceMap.clear();
539 unsigned Dist = 0;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000540 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000541 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000542 MachineInstr *MI = I;
Chris Lattner518bb532010-02-09 19:54:29 +0000543 if (MI->isDebugValue())
Dale Johannesend94998f2010-02-09 02:01:46 +0000544 continue;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000545 DistanceMap.insert(std::make_pair(MI, Dist++));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000546
547 // Process all of the operands of the instruction...
548 unsigned NumOperandsToProcess = MI->getNumOperands();
549
550 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
551 // of the uses. They will be handled in other basic blocks.
Chris Lattner518bb532010-02-09 19:54:29 +0000552 if (MI->isPHI())
Misha Brukman09ba9062004-06-24 21:31:16 +0000553 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000554
Evan Chengd05e8052010-03-26 02:12:24 +0000555 // Clear kill and dead markers. LV will recompute them.
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000556 SmallVector<unsigned, 4> UseRegs;
557 SmallVector<unsigned, 4> DefRegs;
Jakob Stoklund Olesen8c47ad82012-01-21 00:58:53 +0000558 SmallVector<unsigned, 1> RegMasks;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000559 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Evan Chengd05e8052010-03-26 02:12:24 +0000560 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen8c47ad82012-01-21 00:58:53 +0000561 if (MO.isRegMask()) {
562 RegMasks.push_back(i);
563 continue;
564 }
Evan Chenga894ae12009-01-20 21:25:12 +0000565 if (!MO.isReg() || MO.getReg() == 0)
566 continue;
567 unsigned MOReg = MO.getReg();
Evan Chengd05e8052010-03-26 02:12:24 +0000568 if (MO.isUse()) {
569 MO.setIsKill(false);
Evan Chenga894ae12009-01-20 21:25:12 +0000570 UseRegs.push_back(MOReg);
Evan Chengd05e8052010-03-26 02:12:24 +0000571 } else /*MO.isDef()*/ {
572 MO.setIsDead(false);
Evan Chenga894ae12009-01-20 21:25:12 +0000573 DefRegs.push_back(MOReg);
Evan Chengd05e8052010-03-26 02:12:24 +0000574 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000575 }
576
Evan Cheng0d4bdde2008-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 Gohman3bdf5fe2008-09-21 21:11:41 +0000582 else if (!ReservedRegisters[MOReg])
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000583 HandlePhysRegUse(MOReg, MI);
584 }
585
Jakob Stoklund Olesen8c47ad82012-01-21 00:58:53 +0000586 // Process all masked registers. (Call clobbers).
587 for (unsigned i = 0, e = RegMasks.size(); i != e; ++i)
588 HandleRegMask(MI->getOperand(RegMasks[i]));
589
Bill Wendling6d794742008-02-20 09:15:16 +0000590 // Process all defs.
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000591 for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
592 unsigned MOReg = DefRegs[i];
Dan Gohman3bdf5fe2008-09-21 21:11:41 +0000593 if (TargetRegisterInfo::isVirtualRegister(MOReg))
594 HandleVirtRegDef(MOReg, MI);
Evan Chengad934b82009-09-24 02:15:22 +0000595 else if (!ReservedRegisters[MOReg])
596 HandlePhysRegDef(MOReg, MI, Defs);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000597 }
Evan Cheng296925d2009-09-23 06:28:31 +0000598 UpdatePhysRegDefs(MI, Defs);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000599 }
600
601 // Handle any virtual assignments from PHI nodes which might be at the
602 // bottom of this basic block. We check all of our successor blocks to see
603 // if they have PHI nodes, and if so, we simulate an assignment at the end
604 // of the current block.
Evan Chenge96f5012007-04-25 19:34:00 +0000605 if (!PHIVarInfo[MBB->getNumber()].empty()) {
606 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
Misha Brukmanedf128a2005-04-21 22:36:52 +0000607
Evan Chenge96f5012007-04-25 19:34:00 +0000608 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling420cdeb2008-02-20 07:36:31 +0000609 E = VarInfoVec.end(); I != E; ++I)
610 // Mark it alive only in the block we are representing.
Evan Chengea1d9cd2008-04-02 18:04:08 +0000611 MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
Owen Anderson40a627d2008-01-15 22:58:11 +0000612 MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000613 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000614
Bill Wendling6d794742008-02-20 09:15:16 +0000615 // Finally, if the last instruction in the block is a return, make sure to
616 // mark it as using all of the live-out values in the function.
Dale Johannesen88004c22010-06-05 00:30:45 +0000617 // Things marked both call and return are tail calls; do not do this for
618 // them. The tail callee need not take the same registers as input
619 // that it produces as output, and there are dependencies for its input
620 // registers elsewhere.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000621 if (!MBB->empty() && MBB->back().isReturn()
622 && !MBB->back().isCall()) {
Chris Lattnerd493b342005-04-09 15:23:25 +0000623 MachineInstr *Ret = &MBB->back();
Bill Wendling420cdeb2008-02-20 07:36:31 +0000624
Chris Lattner84bc5422007-12-31 04:13:23 +0000625 for (MachineRegisterInfo::liveout_iterator
626 I = MF->getRegInfo().liveout_begin(),
627 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000628 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Dan Gohman48b0b882008-06-25 22:14:43 +0000629 "Cannot have a live-out virtual register!");
Chris Lattnerd493b342005-04-09 15:23:25 +0000630 HandlePhysRegUse(*I, Ret);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000631
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000632 // Add live-out registers as implicit uses.
Evan Cheng6130f662008-03-05 00:59:57 +0000633 if (!Ret->readsRegister(*I))
Chris Lattner8019f412007-12-30 00:41:17 +0000634 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Chris Lattnerd493b342005-04-09 15:23:25 +0000635 }
636 }
637
Evan Chengbfe8afa2012-01-14 01:53:46 +0000638 // MachineCSE may CSE instructions which write to non-allocatable physical
639 // registers across MBBs. Remember if any reserved register is liveout.
640 SmallSet<unsigned, 4> LiveOuts;
641 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
642 SE = MBB->succ_end(); SI != SE; ++SI) {
643 MachineBasicBlock *SuccMBB = *SI;
644 if (SuccMBB->isLandingPad())
645 continue;
646 for (MachineBasicBlock::livein_iterator LI = SuccMBB->livein_begin(),
647 LE = SuccMBB->livein_end(); LI != LE; ++LI) {
648 unsigned LReg = *LI;
649 if (!TRI->isInAllocatableClass(LReg))
650 // Ignore other live-ins, e.g. those that are live into landing pads.
651 LiveOuts.insert(LReg);
652 }
653 }
654
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000655 // Loop over PhysRegDef / PhysRegUse, killing any registers that are
656 // available at the end of the basic block.
Evan Chenge96f5012007-04-25 19:34:00 +0000657 for (unsigned i = 0; i != NumRegs; ++i)
Evan Chengbfe8afa2012-01-14 01:53:46 +0000658 if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i))
Evan Chengad934b82009-09-24 02:15:22 +0000659 HandlePhysRegDef(i, 0, Defs);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000660
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000661 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
662 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000663 }
664
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000665 // Convert and transfer the dead / killed information we have gathered into
666 // VirtRegInfo onto MI's.
Jakob Stoklund Olesenb421c562011-01-08 23:10:57 +0000667 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) {
668 const unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
669 for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j)
670 if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg))
671 VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000672 else
Jakob Stoklund Olesenb421c562011-01-08 23:10:57 +0000673 VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI);
674 }
Chris Lattnera5287a62004-07-01 04:24:29 +0000675
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000676 // Check to make sure there are no unreachable blocks in the MC CFG for the
677 // function. If so, it is due to a bug in the instruction selector or some
678 // other part of the code generator if this happens.
679#ifndef NDEBUG
Evan Chengc6a24102007-03-17 09:29:54 +0000680 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000681 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
682#endif
683
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000684 delete[] PhysRegDef;
685 delete[] PhysRegUse;
Evan Chenge96f5012007-04-25 19:34:00 +0000686 delete[] PHIVarInfo;
687
Chris Lattnerbc40e892003-01-13 20:01:16 +0000688 return false;
689}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000690
Evan Chengbe04dc12008-07-03 00:07:19 +0000691/// replaceKillInstruction - Update register kill info by replacing a kill
692/// instruction with a new one.
693void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr *OldMI,
694 MachineInstr *NewMI) {
695 VarInfo &VI = getVarInfo(Reg);
Evan Cheng5b9f60b2008-07-03 00:28:27 +0000696 std::replace(VI.Kills.begin(), VI.Kills.end(), OldMI, NewMI);
Evan Chengbe04dc12008-07-03 00:07:19 +0000697}
698
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000699/// removeVirtualRegistersKilled - Remove all killed info for the specified
700/// instruction.
701void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000702 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
703 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000704 if (MO.isReg() && MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000705 MO.setIsKill(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000706 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000707 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000708 bool removed = getVarInfo(Reg).removeKill(MI);
709 assert(removed && "kill not in register's VarInfo?");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000710 (void)removed;
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000711 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000712 }
713 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000714}
715
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000716/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling6d794742008-02-20 09:15:16 +0000717/// particular, we want to map the variable information of a virtual register
718/// which is used in a PHI node. We map that to the BB the vreg is coming from.
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000719///
720void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
721 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
722 I != E; ++I)
723 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000724 BBI != BBE && BBI->isPHI(); ++BBI)
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000725 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendling90a38682008-02-20 06:10:21 +0000726 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
727 .push_back(BBI->getOperand(i).getReg());
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000728}
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +0000729
Jakob Stoklund Olesen323d8c32009-11-21 02:05:21 +0000730bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB,
731 unsigned Reg,
732 MachineRegisterInfo &MRI) {
733 unsigned Num = MBB.getNumber();
734
735 // Reg is live-through.
736 if (AliveBlocks.test(Num))
737 return true;
738
739 // Registers defined in MBB cannot be live in.
740 const MachineInstr *Def = MRI.getVRegDef(Reg);
741 if (Def && Def->getParent() == &MBB)
742 return false;
743
744 // Reg was not defined in MBB, was it killed here?
745 return findKill(&MBB);
746}
747
Jakob Stoklund Olesen8f722352009-12-01 17:13:31 +0000748bool LiveVariables::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB) {
749 LiveVariables::VarInfo &VI = getVarInfo(Reg);
750
751 // Loop over all of the successors of the basic block, checking to see if
752 // the value is either live in the block, or if it is killed in the block.
Benjamin Kramerf337fb22011-03-08 17:28:36 +0000753 SmallVector<MachineBasicBlock*, 8> OpSuccBlocks;
Jakob Stoklund Olesen8f722352009-12-01 17:13:31 +0000754 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
755 E = MBB.succ_end(); SI != E; ++SI) {
756 MachineBasicBlock *SuccMBB = *SI;
757
758 // Is it alive in this successor?
759 unsigned SuccIdx = SuccMBB->getNumber();
760 if (VI.AliveBlocks.test(SuccIdx))
761 return true;
762 OpSuccBlocks.push_back(SuccMBB);
763 }
764
765 // Check to see if this value is live because there is a use in a successor
766 // that kills it.
767 switch (OpSuccBlocks.size()) {
768 case 1: {
769 MachineBasicBlock *SuccMBB = OpSuccBlocks[0];
770 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
771 if (VI.Kills[i]->getParent() == SuccMBB)
772 return true;
773 break;
774 }
775 case 2: {
776 MachineBasicBlock *SuccMBB1 = OpSuccBlocks[0], *SuccMBB2 = OpSuccBlocks[1];
777 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
778 if (VI.Kills[i]->getParent() == SuccMBB1 ||
779 VI.Kills[i]->getParent() == SuccMBB2)
780 return true;
781 break;
782 }
783 default:
784 std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
785 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
786 if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
787 VI.Kills[i]->getParent()))
788 return true;
789 }
790 return false;
791}
792
Jakob Stoklund Olesen3e204752009-11-11 19:31:31 +0000793/// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
794/// variables that are live out of DomBB will be marked as passing live through
795/// BB.
796void LiveVariables::addNewBlock(MachineBasicBlock *BB,
Jakob Stoklund Olesen323d8c32009-11-21 02:05:21 +0000797 MachineBasicBlock *DomBB,
798 MachineBasicBlock *SuccBB) {
Jakob Stoklund Olesen3e204752009-11-11 19:31:31 +0000799 const unsigned NumNew = BB->getNumber();
Jakob Stoklund Olesen323d8c32009-11-21 02:05:21 +0000800
801 // All registers used by PHI nodes in SuccBB must be live through BB.
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000802 for (MachineBasicBlock::iterator BBI = SuccBB->begin(),
Chris Lattner518bb532010-02-09 19:54:29 +0000803 BBE = SuccBB->end(); BBI != BBE && BBI->isPHI(); ++BBI)
Jakob Stoklund Olesen323d8c32009-11-21 02:05:21 +0000804 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
805 if (BBI->getOperand(i+1).getMBB() == BB)
806 getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +0000807
808 // Update info for all live variables
Jakob Stoklund Olesenb421c562011-01-08 23:10:57 +0000809 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
810 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen3e204752009-11-11 19:31:31 +0000811 VarInfo &VI = getVarInfo(Reg);
Jakob Stoklund Olesen323d8c32009-11-21 02:05:21 +0000812 if (!VI.AliveBlocks.test(NumNew) && VI.isLiveIn(*SuccBB, Reg, *MRI))
Jakob Stoklund Olesen3e204752009-11-11 19:31:31 +0000813 VI.AliveBlocks.set(NumNew);
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +0000814 }
815}