blob: bc73b6841c4db9584ec99765a6fdacd725e8090f [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"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000033#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000034#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng04104072007-06-27 05:23:00 +000036#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/ADT/STLExtras.h"
Chris Lattner6fcd8d82004-10-25 18:44:14 +000038#include "llvm/Config/alloca.h"
Chris Lattner657b4d12005-08-24 00:09:33 +000039#include <algorithm>
Chris Lattner49a5aaa2004-01-30 22:08:53 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Devang Patel19974732007-05-03 01:11:54 +000042char LiveVariables::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000043static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
Chris Lattnerbc40e892003-01-13 20:01:16 +000044
Chris Lattnerdacceef2006-01-04 05:40:30 +000045void LiveVariables::VarInfo::dump() const {
Bill Wendlingbcd24982006-12-07 20:28:15 +000046 cerr << " Alive in blocks: ";
Chris Lattnerdacceef2006-01-04 05:40:30 +000047 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
Bill Wendlingbcd24982006-12-07 20:28:15 +000048 if (AliveBlocks[i]) cerr << i << ", ";
Owen Andersona0185402007-11-08 01:20:48 +000049 cerr << " Used in blocks: ";
50 for (unsigned i = 0, e = UsedBlocks.size(); i != e; ++i)
51 if (UsedBlocks[i]) cerr << i << ", ";
Bill Wendlingbcd24982006-12-07 20:28:15 +000052 cerr << "\n Killed by:";
Chris Lattnerdacceef2006-01-04 05:40:30 +000053 if (Kills.empty())
Bill Wendlingbcd24982006-12-07 20:28:15 +000054 cerr << " No instructions.\n";
Chris Lattnerdacceef2006-01-04 05:40:30 +000055 else {
56 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
Bill Wendlingbcd24982006-12-07 20:28:15 +000057 cerr << "\n #" << i << ": " << *Kills[i];
58 cerr << "\n";
Chris Lattnerdacceef2006-01-04 05:40:30 +000059 }
60}
61
Bill Wendling90a38682008-02-20 06:10:21 +000062/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
Chris Lattnerfb2cb692003-05-12 14:24:00 +000063LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000064 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000065 "getVarInfo: not a virtual register!");
Dan Gohman6f0d0242008-02-10 18:45:23 +000066 RegIdx -= TargetRegisterInfo::FirstVirtualRegister;
Chris Lattnerfb2cb692003-05-12 14:24:00 +000067 if (RegIdx >= VirtRegInfo.size()) {
68 if (RegIdx >= 2*VirtRegInfo.size())
69 VirtRegInfo.resize(RegIdx*2);
70 else
71 VirtRegInfo.resize(2*VirtRegInfo.size());
72 }
Evan Chengc6a24102007-03-17 09:29:54 +000073 VarInfo &VI = VirtRegInfo[RegIdx];
74 VI.AliveBlocks.resize(MF->getNumBlockIDs());
Owen Andersona0185402007-11-08 01:20:48 +000075 VI.UsedBlocks.resize(MF->getNumBlockIDs());
Evan Chengc6a24102007-03-17 09:29:54 +000076 return VI;
Chris Lattnerfb2cb692003-05-12 14:24:00 +000077}
78
Owen Anderson40a627d2008-01-15 22:58:11 +000079void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
80 MachineBasicBlock *DefBlock,
Evan Cheng56184902007-05-08 19:00:00 +000081 MachineBasicBlock *MBB,
82 std::vector<MachineBasicBlock*> &WorkList) {
Chris Lattner8ba97712004-07-01 04:29:47 +000083 unsigned BBNum = MBB->getNumber();
Owen Anderson7047dd42008-01-15 22:02:46 +000084
Chris Lattnerbc40e892003-01-13 20:01:16 +000085 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendling90a38682008-02-20 06:10:21 +000086 // remove it.
Chris Lattnerbc40e892003-01-13 20:01:16 +000087 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +000088 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000089 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
90 break;
91 }
Owen Anderson7047dd42008-01-15 22:02:46 +000092
Owen Anderson40a627d2008-01-15 22:58:11 +000093 if (MBB == DefBlock) return; // Terminate recursion
Chris Lattnerbc40e892003-01-13 20:01:16 +000094
Chris Lattnerbc40e892003-01-13 20:01:16 +000095 if (VRInfo.AliveBlocks[BBNum])
96 return; // We already know the block is live
97
98 // Mark the variable known alive in this bb
99 VRInfo.AliveBlocks[BBNum] = true;
100
Evan Cheng56184902007-05-08 19:00:00 +0000101 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
102 E = MBB->pred_rend(); PI != E; ++PI)
103 WorkList.push_back(*PI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000104}
105
Bill Wendling420cdeb2008-02-20 07:36:31 +0000106void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Owen Anderson40a627d2008-01-15 22:58:11 +0000107 MachineBasicBlock *DefBlock,
Evan Cheng56184902007-05-08 19:00:00 +0000108 MachineBasicBlock *MBB) {
109 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson40a627d2008-01-15 22:58:11 +0000110 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000111
Evan Cheng56184902007-05-08 19:00:00 +0000112 while (!WorkList.empty()) {
113 MachineBasicBlock *Pred = WorkList.back();
114 WorkList.pop_back();
Owen Anderson40a627d2008-01-15 22:58:11 +0000115 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Evan Cheng56184902007-05-08 19:00:00 +0000116 }
117}
118
Owen Anderson7047dd42008-01-15 22:02:46 +0000119void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Misha Brukman09ba9062004-06-24 21:31:16 +0000120 MachineInstr *MI) {
Evan Chengea1d9cd2008-04-02 18:04:08 +0000121 assert(MRI->getVRegDef(reg) && "Register use before def!");
Alkis Evlogimenos2e58a412004-09-01 22:34:52 +0000122
Owen Andersona0185402007-11-08 01:20:48 +0000123 unsigned BBNum = MBB->getNumber();
124
Owen Anderson7047dd42008-01-15 22:02:46 +0000125 VarInfo& VRInfo = getVarInfo(reg);
Owen Andersona0185402007-11-08 01:20:48 +0000126 VRInfo.UsedBlocks[BBNum] = true;
Evan Cheng38b7ca62007-04-17 20:22:11 +0000127 VRInfo.NumUses++;
Evan Chengc6a24102007-03-17 09:29:54 +0000128
Bill Wendling90a38682008-02-20 06:10:21 +0000129 // Check to see if this basic block is already a kill block.
Chris Lattner74de8b12004-07-19 07:04:55 +0000130 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendling90a38682008-02-20 06:10:21 +0000131 // Yes, this register is killed in this basic block already. Increase the
Chris Lattnerbc40e892003-01-13 20:01:16 +0000132 // live range by updating the kill instruction.
Chris Lattner74de8b12004-07-19 07:04:55 +0000133 VRInfo.Kills.back() = MI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000134 return;
135 }
136
137#ifndef NDEBUG
138 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000139 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000140#endif
141
Bill Wendlingebcba612008-06-23 23:41:14 +0000142 // This situation can occur:
143 //
144 // ,------.
145 // | |
146 // | v
147 // | t2 = phi ... t1 ...
148 // | |
149 // | v
150 // | t1 = ...
151 // | ... = ... t1 ...
152 // | |
153 // `------'
154 //
155 // where there is a use in a PHI node that's a predecessor to the defining
156 // block. We don't want to mark all predecessors as having the value "alive"
157 // in this case.
158 if (MBB == MRI->getVRegDef(reg)->getParent()) return;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000159
Bill Wendling90a38682008-02-20 06:10:21 +0000160 // Add a new kill entry for this basic block. If this virtual register is
161 // already marked as alive in this basic block, that means it is alive in at
162 // least one of the successor blocks, it's not a kill.
Owen Andersona0185402007-11-08 01:20:48 +0000163 if (!VRInfo.AliveBlocks[BBNum])
Evan Chenge2ee9962007-03-09 09:48:56 +0000164 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000165
Bill Wendling420cdeb2008-02-20 07:36:31 +0000166 // Update all dominating blocks to mark them as "known live".
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000167 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
168 E = MBB->pred_end(); PI != E; ++PI)
Evan Chengea1d9cd2008-04-02 18:04:08 +0000169 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000170}
171
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000172/// FindLastPartialDef - Return the last partial def of the specified register.
173/// Also returns the sub-register that's defined.
174MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg,
175 unsigned &PartDefReg) {
176 unsigned LastDefReg = 0;
177 unsigned LastDefDist = 0;
178 MachineInstr *LastDef = NULL;
179 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
180 unsigned SubReg = *SubRegs; ++SubRegs) {
181 MachineInstr *Def = PhysRegDef[SubReg];
182 if (!Def)
183 continue;
184 unsigned Dist = DistanceMap[Def];
185 if (Dist > LastDefDist) {
186 LastDefReg = SubReg;
187 LastDef = Def;
188 LastDefDist = Dist;
189 }
190 }
191 PartDefReg = LastDefReg;
192 return LastDef;
193}
194
Bill Wendling6d794742008-02-20 09:15:16 +0000195/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
196/// implicit defs to a machine instruction if there was an earlier def of its
197/// super-register.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000198void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000199 // If there was a previous use or a "full" def all is well.
200 if (!PhysRegDef[Reg] && !PhysRegUse[Reg]) {
201 // Otherwise, the last sub-register def implicitly defines this register.
202 // e.g.
203 // AH =
204 // AL = ... <imp-def EAX>, <imp-kill AH>
205 // = AH
206 // ...
207 // = EAX
208 // All of the sub-registers must have been defined before the use of Reg!
209 unsigned PartDefReg = 0;
210 MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefReg);
211 // If LastPartialDef is NULL, it must be using a livein register.
212 if (LastPartialDef) {
213 LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
214 true/*IsImp*/));
215 PhysRegDef[Reg] = LastPartialDef;
216 std::set<unsigned> Processed;
217 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
218 unsigned SubReg = *SubRegs; ++SubRegs) {
219 if (Processed.count(SubReg))
220 continue;
221 if (SubReg == PartDefReg || TRI->isSubRegister(PartDefReg, SubReg))
222 continue;
223 // This part of Reg was defined before the last partial def. It's killed
224 // here.
225 LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
226 false/*IsDef*/,
227 true/*IsImp*/));
228 PhysRegDef[SubReg] = LastPartialDef;
229 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
230 Processed.insert(*SS);
231 }
232 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000233 }
Bill Wendling90a38682008-02-20 06:10:21 +0000234
Evan Cheng24a3cc42007-04-25 07:30:23 +0000235 // There was an earlier def of a super-register. Add implicit def to that MI.
Bill Wendling6d794742008-02-20 09:15:16 +0000236 //
237 // A: EAX = ...
238 // B: ... = AX
239 //
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000240 // Add implicit def to A if there isn't a use of AX (or EAX) before B.
241 if (!PhysRegUse[Reg]) {
242 MachineInstr *Def = PhysRegDef[Reg];
243 if (Def && !Def->modifiesRegister(Reg))
Bill Wendling6d794742008-02-20 09:15:16 +0000244 Def->addOperand(MachineOperand::CreateReg(Reg,
245 true /*IsDef*/,
246 true /*IsImp*/));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000247 }
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000248
249 // Remember this use.
250 PhysRegUse[Reg] = MI;
Evan Cheng6130f662008-03-05 00:59:57 +0000251 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000252 unsigned SubReg = *SubRegs; ++SubRegs)
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000253 PhysRegUse[SubReg] = MI;
Evan Cheng4efe7412007-06-26 21:03:35 +0000254}
255
Evan Cheng94202012008-03-19 00:52:20 +0000256/// hasRegisterUseBelow - Return true if the specified register is used after
257/// the current instruction and before it's next definition.
258bool LiveVariables::hasRegisterUseBelow(unsigned Reg,
259 MachineBasicBlock::iterator I,
260 MachineBasicBlock *MBB) {
261 if (I == MBB->end())
262 return false;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000263
264 // First find out if there are any uses / defs below.
265 bool hasDistInfo = true;
266 unsigned CurDist = DistanceMap[I];
267 SmallVector<MachineInstr*, 4> Uses;
268 SmallVector<MachineInstr*, 4> Defs;
269 for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg),
270 RE = MRI->reg_end(); RI != RE; ++RI) {
271 MachineOperand &UDO = RI.getOperand();
272 MachineInstr *UDMI = &*RI;
273 if (UDMI->getParent() != MBB)
274 continue;
275 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
276 bool isBelow = false;
277 if (DI == DistanceMap.end()) {
278 // Must be below if it hasn't been assigned a distance yet.
279 isBelow = true;
280 hasDistInfo = false;
281 } else if (DI->second > CurDist)
282 isBelow = true;
283 if (isBelow) {
284 if (UDO.isUse())
285 Uses.push_back(UDMI);
286 if (UDO.isDef())
287 Defs.push_back(UDMI);
Evan Cheng94202012008-03-19 00:52:20 +0000288 }
289 }
Evan Chengea1d9cd2008-04-02 18:04:08 +0000290
291 if (Uses.empty())
292 // No uses below.
293 return false;
294 else if (!Uses.empty() && Defs.empty())
295 // There are uses below but no defs below.
296 return true;
297 // There are both uses and defs below. We need to know which comes first.
298 if (!hasDistInfo) {
299 // Complete DistanceMap for this MBB. This information is computed only
300 // once per MBB.
301 ++I;
302 ++CurDist;
303 for (MachineBasicBlock::iterator E = MBB->end(); I != E; ++I, ++CurDist)
304 DistanceMap.insert(std::make_pair(I, CurDist));
305 }
306
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000307 unsigned EarliestUse = DistanceMap[Uses[0]];
308 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
Evan Chengea1d9cd2008-04-02 18:04:08 +0000309 unsigned Dist = DistanceMap[Uses[i]];
310 if (Dist < EarliestUse)
311 EarliestUse = Dist;
312 }
313 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
314 unsigned Dist = DistanceMap[Defs[i]];
315 if (Dist < EarliestUse)
316 // The register is defined before its first use below.
317 return false;
318 }
319 return true;
Evan Cheng94202012008-03-19 00:52:20 +0000320}
321
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000322bool LiveVariables::HandlePhysRegKill(unsigned Reg) {
323 if (!PhysRegUse[Reg] && !PhysRegDef[Reg])
324 return false;
325
326 MachineInstr *LastRefOrPartRef = PhysRegUse[Reg]
327 ? PhysRegUse[Reg] : PhysRegDef[Reg];
328 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
329 // The whole register is used.
330 // AL =
331 // AH =
332 //
333 // = AX
334 // = AL, AX<imp-use, kill>
335 // AX =
336 //
337 // Or whole register is defined, but not used at all.
338 // AX<dead> =
339 // ...
340 // AX =
341 //
342 // Or whole register is defined, but only partly used.
343 // AX<dead> = AL<imp-def>
344 // = AL<kill>
345 // AX =
346 std::set<unsigned> PartUses;
347 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
348 unsigned SubReg = *SubRegs; ++SubRegs) {
349 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 }
360 if (LastRefOrPartRef == PhysRegDef[Reg])
361 // Not used at all.
362 LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
363
364 /* Partial uses. Mark register def dead and add implicit def of
365 sub-registers which are used.
366 FIXME: LiveIntervalAnalysis can't handle this yet!
367 EAX<dead> = op AL<imp-def>
368 That is, EAX def is dead but AL def extends pass it.
369 Enable this after live interval analysis is fixed to improve codegen!
370 else if (!PhysRegUse[Reg]) {
371 PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
372 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
373 unsigned SubReg = *SubRegs; ++SubRegs) {
374 if (PartUses.count(SubReg)) {
375 PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
376 true, true));
377 LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
378 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
379 PartUses.erase(*SS);
380 }
381 }
382 } */
383 else
384 LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
385 return true;
386}
387
388void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
389 // What parts of the register are previously defined?
390 std::set<unsigned> Live;
391 if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
392 Live.insert(Reg);
393 for (const unsigned *SS = TRI->getSubRegisters(Reg); *SS; ++SS)
394 Live.insert(*SS);
395 } else {
396 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
397 unsigned SubReg = *SubRegs; ++SubRegs) {
398 // If a register isn't itself defined, but all parts that make up of it
399 // are defined, then consider it also defined.
400 // e.g.
401 // AL =
402 // AH =
403 // = AX
404 if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
405 Live.insert(SubReg);
406 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
407 Live.insert(*SS);
408 }
Bill Wendling420cdeb2008-02-20 07:36:31 +0000409 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000410 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000411
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000412 // Start from the largest piece, find the last time any part of the register
413 // is referenced.
414 if (!HandlePhysRegKill(Reg)) {
415 // Only some of the sub-registers are used.
416 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
417 unsigned SubReg = *SubRegs; ++SubRegs) {
418 if (!Live.count(SubReg))
419 // Skip if this sub-register isn't defined.
420 continue;
421 if (HandlePhysRegKill(SubReg)) {
422 Live.erase(SubReg);
423 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
424 Live.erase(*SS);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000425 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000426 }
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000427 assert(Live.empty() && "Not all defined registers are killed / dead?");
Evan Cheng24a3cc42007-04-25 07:30:23 +0000428 }
429
Evan Cheng4efe7412007-06-26 21:03:35 +0000430 if (MI) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000431 // Does this extend the live range of a super-register?
432 std::set<unsigned> Processed;
Evan Cheng6130f662008-03-05 00:59:57 +0000433 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000434 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000435 if (Processed.count(SuperReg))
436 continue;
437 MachineInstr *LastRef = PhysRegUse[SuperReg]
438 ? PhysRegUse[SuperReg] : PhysRegDef[SuperReg];
439 if (LastRef && LastRef != MI) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000440 // The larger register is previously defined. Now a smaller part is
Evan Cheng94202012008-03-19 00:52:20 +0000441 // being re-defined. Treat it as read/mod/write if there are uses
442 // below.
Evan Cheng24a3cc42007-04-25 07:30:23 +0000443 // EAX =
444 // AX = EAX<imp-use,kill>, EAX<imp-def>
Evan Cheng94202012008-03-19 00:52:20 +0000445 // ...
446 /// = EAX
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000447 if (hasRegisterUseBelow(SuperReg, MI, MI->getParent())) {
Evan Cheng94202012008-03-19 00:52:20 +0000448 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000449 true/*IsImp*/,true/*IsKill*/));
Evan Cheng94202012008-03-19 00:52:20 +0000450 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
451 true/*IsImp*/));
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000452 PhysRegDef[SuperReg] = MI;
453 PhysRegUse[SuperReg] = NULL;
454 Processed.insert(SuperReg);
455 for (const unsigned *SS = TRI->getSubRegisters(SuperReg); *SS; ++SS) {
456 PhysRegDef[*SS] = MI;
457 PhysRegUse[*SS] = NULL;
458 Processed.insert(*SS);
459 }
Evan Cheng94202012008-03-19 00:52:20 +0000460 } else {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000461 // Otherwise, the super register is killed.
462 if (HandlePhysRegKill(SuperReg)) {
463 PhysRegDef[SuperReg] = NULL;
464 PhysRegUse[SuperReg] = NULL;
465 for (const unsigned *SS = TRI->getSubRegisters(SuperReg); *SS; ++SS) {
466 PhysRegDef[*SS] = NULL;
467 PhysRegUse[*SS] = NULL;
468 Processed.insert(*SS);
469 }
470 }
Evan Cheng94202012008-03-19 00:52:20 +0000471 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000472 }
Evan Cheng4efe7412007-06-26 21:03:35 +0000473 }
474
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000475 // Remember this def.
476 PhysRegDef[Reg] = MI;
477 PhysRegUse[Reg] = NULL;
Evan Cheng6130f662008-03-05 00:59:57 +0000478 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Evan Cheng4efe7412007-06-26 21:03:35 +0000479 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000480 PhysRegDef[SubReg] = MI;
481 PhysRegUse[SubReg] = NULL;
Evan Cheng4efe7412007-06-26 21:03:35 +0000482 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000483 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000484}
485
Evan Chengc6a24102007-03-17 09:29:54 +0000486bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
487 MF = &mf;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000488 MRI = &mf.getRegInfo();
Evan Cheng6130f662008-03-05 00:59:57 +0000489 TRI = MF->getTarget().getRegisterInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000490
Evan Cheng6130f662008-03-05 00:59:57 +0000491 ReservedRegisters = TRI->getReservedRegs(mf);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000492
Evan Cheng6130f662008-03-05 00:59:57 +0000493 unsigned NumRegs = TRI->getNumRegs();
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000494 PhysRegDef = new MachineInstr*[NumRegs];
495 PhysRegUse = new MachineInstr*[NumRegs];
Evan Chenge96f5012007-04-25 19:34:00 +0000496 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000497 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
498 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000499
Bill Wendling6d794742008-02-20 09:15:16 +0000500 /// Get some space for a respectable number of registers.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000501 VirtRegInfo.resize(64);
Chris Lattnerd493b342005-04-09 15:23:25 +0000502
Evan Chengc6a24102007-03-17 09:29:54 +0000503 analyzePHINodes(mf);
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000504
Chris Lattnerbc40e892003-01-13 20:01:16 +0000505 // Calculate live variable information in depth first order on the CFG of the
506 // function. This guarantees that we will see the definition of a virtual
507 // register before its uses due to dominance properties of SSA (except for PHI
508 // nodes, which are treated as a special case).
Evan Chengc6a24102007-03-17 09:29:54 +0000509 MachineBasicBlock *Entry = MF->begin();
Evan Cheng04104072007-06-27 05:23:00 +0000510 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling6d794742008-02-20 09:15:16 +0000511
Evan Cheng04104072007-06-27 05:23:00 +0000512 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
513 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
514 DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000515 MachineBasicBlock *MBB = *DFI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000516
Evan Chengb371f452007-02-19 21:49:54 +0000517 // Mark live-in registers as live-in.
518 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000519 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000520 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000521 "Cannot have a live-in virtual register!");
522 HandlePhysRegDef(*II, 0);
523 }
524
Chris Lattnerbc40e892003-01-13 20:01:16 +0000525 // Loop over all of the instructions, processing them.
Evan Chengea1d9cd2008-04-02 18:04:08 +0000526 DistanceMap.clear();
527 unsigned Dist = 0;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000528 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000529 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000530 MachineInstr *MI = I;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000531 DistanceMap.insert(std::make_pair(MI, Dist++));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000532
533 // Process all of the operands of the instruction...
534 unsigned NumOperandsToProcess = MI->getNumOperands();
535
536 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
537 // of the uses. They will be handled in other basic blocks.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000538 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman09ba9062004-06-24 21:31:16 +0000539 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000540
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000541 SmallVector<unsigned, 4> UseRegs;
542 SmallVector<unsigned, 4> DefRegs;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000543 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000544 const MachineOperand &MO = MI->getOperand(i);
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000545 if (MO.isRegister() && MO.getReg()) {
Bill Wendling90a38682008-02-20 06:10:21 +0000546 unsigned MOReg = MO.getReg();
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000547 if (!MOReg)
548 continue;
549 if (MO.isUse())
550 UseRegs.push_back(MOReg);
551 if (MO.isDef())
552 DefRegs.push_back(MOReg);
Misha Brukman09ba9062004-06-24 21:31:16 +0000553 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000554 }
555
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000556 // Process all uses.
557 for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) {
558 unsigned MOReg = UseRegs[i];
559 if (TargetRegisterInfo::isVirtualRegister(MOReg))
560 HandleVirtRegUse(MOReg, MBB, MI);
561 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
562 !ReservedRegisters[MOReg])
563 HandlePhysRegUse(MOReg, MI);
564 }
565
Bill Wendling6d794742008-02-20 09:15:16 +0000566 // Process all defs.
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000567 for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
568 unsigned MOReg = DefRegs[i];
569 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
570 VarInfo &VRInfo = getVarInfo(MOReg);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000571
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000572 if (VRInfo.AliveBlocks.none())
573 // If vr is not alive in any block, then defaults to dead.
574 VRInfo.Kills.push_back(MI);
575 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
576 !ReservedRegisters[MOReg]) {
577 HandlePhysRegDef(MOReg, MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000578 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000579 }
580 }
581
582 // Handle any virtual assignments from PHI nodes which might be at the
583 // bottom of this basic block. We check all of our successor blocks to see
584 // if they have PHI nodes, and if so, we simulate an assignment at the end
585 // of the current block.
Evan Chenge96f5012007-04-25 19:34:00 +0000586 if (!PHIVarInfo[MBB->getNumber()].empty()) {
587 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
Misha Brukmanedf128a2005-04-21 22:36:52 +0000588
Evan Chenge96f5012007-04-25 19:34:00 +0000589 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling420cdeb2008-02-20 07:36:31 +0000590 E = VarInfoVec.end(); I != E; ++I)
591 // Mark it alive only in the block we are representing.
Evan Chengea1d9cd2008-04-02 18:04:08 +0000592 MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
Owen Anderson40a627d2008-01-15 22:58:11 +0000593 MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000594 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000595
Bill Wendling6d794742008-02-20 09:15:16 +0000596 // Finally, if the last instruction in the block is a return, make sure to
597 // mark it as using all of the live-out values in the function.
Chris Lattner749c6f62008-01-07 07:27:27 +0000598 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Chris Lattnerd493b342005-04-09 15:23:25 +0000599 MachineInstr *Ret = &MBB->back();
Bill Wendling420cdeb2008-02-20 07:36:31 +0000600
Chris Lattner84bc5422007-12-31 04:13:23 +0000601 for (MachineRegisterInfo::liveout_iterator
602 I = MF->getRegInfo().liveout_begin(),
603 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000604 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Chris Lattnerd493b342005-04-09 15:23:25 +0000605 "Cannot have a live-in virtual register!");
606 HandlePhysRegUse(*I, Ret);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000607
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000608 // Add live-out registers as implicit uses.
Evan Cheng6130f662008-03-05 00:59:57 +0000609 if (!Ret->readsRegister(*I))
Chris Lattner8019f412007-12-30 00:41:17 +0000610 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Chris Lattnerd493b342005-04-09 15:23:25 +0000611 }
612 }
613
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000614 // Loop over PhysRegDef / PhysRegUse, killing any registers that are
615 // available at the end of the basic block.
Evan Chenge96f5012007-04-25 19:34:00 +0000616 for (unsigned i = 0; i != NumRegs; ++i)
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000617 if (PhysRegDef[i] || PhysRegUse[i])
Misha Brukman09ba9062004-06-24 21:31:16 +0000618 HandlePhysRegDef(i, 0);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000619
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000620 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
621 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000622 }
623
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000624 // Convert and transfer the dead / killed information we have gathered into
625 // VirtRegInfo onto MI's.
Evan Chengf0e3bb12007-03-09 06:02:17 +0000626 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
Bill Wendling420cdeb2008-02-20 07:36:31 +0000627 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
628 if (VirtRegInfo[i].Kills[j] ==
Evan Chengea1d9cd2008-04-02 18:04:08 +0000629 MRI->getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
Bill Wendling420cdeb2008-02-20 07:36:31 +0000630 VirtRegInfo[i]
631 .Kills[j]->addRegisterDead(i +
632 TargetRegisterInfo::FirstVirtualRegister,
Evan Cheng6130f662008-03-05 00:59:57 +0000633 TRI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000634 else
Bill Wendling420cdeb2008-02-20 07:36:31 +0000635 VirtRegInfo[i]
636 .Kills[j]->addRegisterKilled(i +
637 TargetRegisterInfo::FirstVirtualRegister,
Evan Cheng6130f662008-03-05 00:59:57 +0000638 TRI);
Chris Lattnera5287a62004-07-01 04:24:29 +0000639
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000640 // Check to make sure there are no unreachable blocks in the MC CFG for the
641 // function. If so, it is due to a bug in the instruction selector or some
642 // other part of the code generator if this happens.
643#ifndef NDEBUG
Evan Chengc6a24102007-03-17 09:29:54 +0000644 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000645 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
646#endif
647
Evan Cheng0d4bdde2008-04-16 09:46:40 +0000648 delete[] PhysRegDef;
649 delete[] PhysRegUse;
Evan Chenge96f5012007-04-25 19:34:00 +0000650 delete[] PHIVarInfo;
651
Chris Lattnerbc40e892003-01-13 20:01:16 +0000652 return false;
653}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000654
Bill Wendling6d794742008-02-20 09:15:16 +0000655/// instructionChanged - When the address of an instruction changes, this method
656/// should be called so that live variables can update its internal data
657/// structures. This removes the records for OldMI, transfering them to the
658/// records for NewMI.
Chris Lattner5ed001b2004-02-19 18:28:02 +0000659void LiveVariables::instructionChanged(MachineInstr *OldMI,
660 MachineInstr *NewMI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000661 // If the instruction defines any virtual registers, update the VarInfo,
662 // kill and dead information for the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000663 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
664 MachineOperand &MO = OldMI->getOperand(i);
Chris Lattnerd45be362005-01-19 17:09:15 +0000665 if (MO.isRegister() && MO.getReg() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000666 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Chris Lattner5ed001b2004-02-19 18:28:02 +0000667 unsigned Reg = MO.getReg();
668 VarInfo &VI = getVarInfo(Reg);
Chris Lattnerd45be362005-01-19 17:09:15 +0000669 if (MO.isDef()) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000670 if (MO.isDead()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000671 MO.setIsDead(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000672 addVirtualRegisterDead(Reg, NewMI);
673 }
Chris Lattner2a6e1632005-01-19 17:11:51 +0000674 }
Dan Gohmanc674a922007-07-20 23:17:34 +0000675 if (MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000676 MO.setIsKill(false);
Dan Gohmanc674a922007-07-20 23:17:34 +0000677 addVirtualRegisterKilled(Reg, NewMI);
Chris Lattnerd45be362005-01-19 17:09:15 +0000678 }
Dan Gohmanc674a922007-07-20 23:17:34 +0000679 // If this is a kill of the value, update the VI kills list.
680 if (VI.removeKill(OldMI))
681 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Chris Lattner5ed001b2004-02-19 18:28:02 +0000682 }
683 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000684}
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000685
686/// removeVirtualRegistersKilled - Remove all killed info for the specified
687/// instruction.
688void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000689 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
690 MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000691 if (MO.isRegister() && MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000692 MO.setIsKill(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000693 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000694 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000695 bool removed = getVarInfo(Reg).removeKill(MI);
696 assert(removed && "kill not in register's VarInfo?");
697 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000698 }
699 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000700}
701
702/// removeVirtualRegistersDead - Remove all of the dead registers for the
703/// specified instruction from the live variable information.
704void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000705 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
706 MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000707 if (MO.isRegister() && MO.isDead()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000708 MO.setIsDead(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000709 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000710 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000711 bool removed = getVarInfo(Reg).removeKill(MI);
712 assert(removed && "kill not in register's VarInfo?");
713 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000714 }
715 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000716}
717
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000718/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling6d794742008-02-20 09:15:16 +0000719/// particular, we want to map the variable information of a virtual register
720/// 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 +0000721///
722void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
723 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
724 I != E; ++I)
725 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
726 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
727 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendling90a38682008-02-20 06:10:21 +0000728 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
729 .push_back(BBI->getOperand(i).getReg());
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000730}