blob: 101d1e866d6b6b6fc08cdf07c098e245b9d54f30 [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
Evan Chengea1d9cd2008-04-02 18:04:08 +0000142 assert(MBB != MRI->getVRegDef(reg)->getParent() &&
Chris Lattner73d4adf2004-07-19 06:26:50 +0000143 "Should have kill for defblock!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000144
Bill Wendling90a38682008-02-20 06:10:21 +0000145 // Add a new kill entry for this basic block. If this virtual register is
146 // already marked as alive in this basic block, that means it is alive in at
147 // least one of the successor blocks, it's not a kill.
Owen Andersona0185402007-11-08 01:20:48 +0000148 if (!VRInfo.AliveBlocks[BBNum])
Evan Chenge2ee9962007-03-09 09:48:56 +0000149 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000150
Bill Wendling420cdeb2008-02-20 07:36:31 +0000151 // Update all dominating blocks to mark them as "known live".
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000152 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
153 E = MBB->pred_end(); PI != E; ++PI)
Evan Chengea1d9cd2008-04-02 18:04:08 +0000154 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000155}
156
Bill Wendling6d794742008-02-20 09:15:16 +0000157/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
158/// implicit defs to a machine instruction if there was an earlier def of its
159/// super-register.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000160void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000161 // Turn previous partial def's into read/mod/write.
162 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
163 MachineInstr *Def = PhysRegPartDef[Reg][i];
Bill Wendling6d794742008-02-20 09:15:16 +0000164
Evan Cheng24a3cc42007-04-25 07:30:23 +0000165 // First one is just a def. This means the use is reading some undef bits.
166 if (i != 0)
Bill Wendling6d794742008-02-20 09:15:16 +0000167 Def->addOperand(MachineOperand::CreateReg(Reg,
168 false /*IsDef*/,
169 true /*IsImp*/,
170 true /*IsKill*/));
171
172 Def->addOperand(MachineOperand::CreateReg(Reg,
173 true /*IsDef*/,
174 true /*IsImp*/));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000175 }
Bill Wendling90a38682008-02-20 06:10:21 +0000176
Evan Cheng24a3cc42007-04-25 07:30:23 +0000177 PhysRegPartDef[Reg].clear();
178
179 // There was an earlier def of a super-register. Add implicit def to that MI.
Bill Wendling6d794742008-02-20 09:15:16 +0000180 //
181 // A: EAX = ...
182 // B: ... = AX
183 //
Evan Cheng24a3cc42007-04-25 07:30:23 +0000184 // Add implicit def to A.
Evan Cheng6d6d3522007-09-11 22:34:47 +0000185 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
186 !PhysRegUsed[Reg]) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000187 MachineInstr *Def = PhysRegInfo[Reg];
Bill Wendling6d794742008-02-20 09:15:16 +0000188
Evan Cheng6130f662008-03-05 00:59:57 +0000189 if (!Def->modifiesRegister(Reg))
Bill Wendling6d794742008-02-20 09:15:16 +0000190 Def->addOperand(MachineOperand::CreateReg(Reg,
191 true /*IsDef*/,
192 true /*IsImp*/));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000193 }
194
Evan Cheng6d6d3522007-09-11 22:34:47 +0000195 // There is a now a proper use, forget about the last partial use.
196 PhysRegPartUse[Reg] = NULL;
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000197 PhysRegInfo[Reg] = MI;
198 PhysRegUsed[Reg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000199
Bill Wendling6d794742008-02-20 09:15:16 +0000200 // Now reset the use information for the sub-registers.
Evan Cheng6130f662008-03-05 00:59:57 +0000201 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000202 unsigned SubReg = *SubRegs; ++SubRegs) {
Bill Wendling1d5e8192008-02-21 19:35:27 +0000203 PhysRegPartUse[SubReg] = NULL;
Evan Cheng24a3cc42007-04-25 07:30:23 +0000204 PhysRegInfo[SubReg] = MI;
205 PhysRegUsed[SubReg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000206 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000207
Evan Cheng6130f662008-03-05 00:59:57 +0000208 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
Evan Cheng21b3bf02007-08-01 20:18:21 +0000209 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Bill Wendling6d794742008-02-20 09:15:16 +0000210 // Remember the partial use of this super-register if it was previously
211 // defined.
Evan Cheng21b3bf02007-08-01 20:18:21 +0000212 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
Bill Wendling6d794742008-02-20 09:15:16 +0000213
214 if (!HasPrevDef)
Bill Wendlingc927cc82008-02-20 20:56:45 +0000215 // No need to go up more levels. A def of a register also sets its sub-
216 // registers. So if PhysRegInfo[SuperReg] is NULL, it means SuperReg's
217 // super-registers are not previously defined.
Evan Cheng6130f662008-03-05 00:59:57 +0000218 for (const unsigned *SSRegs = TRI->getSuperRegisters(SuperReg);
Bill Wendling6d794742008-02-20 09:15:16 +0000219 unsigned SSReg = *SSRegs; ++SSRegs)
Evan Cheng21b3bf02007-08-01 20:18:21 +0000220 if (PhysRegInfo[SSReg] != NULL) {
221 HasPrevDef = true;
222 break;
223 }
Bill Wendling6d794742008-02-20 09:15:16 +0000224
Evan Cheng21b3bf02007-08-01 20:18:21 +0000225 if (HasPrevDef) {
226 PhysRegInfo[SuperReg] = MI;
227 PhysRegPartUse[SuperReg] = MI;
228 }
229 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000230}
231
Bill Wendling420cdeb2008-02-20 07:36:31 +0000232/// addRegisterKills - For all of a register's sub-registers that are killed in
Bill Wendlingfe8276c2008-02-20 19:09:14 +0000233/// at this machine instruction, mark them as "killed". (If the machine operand
Bill Wendling420cdeb2008-02-20 07:36:31 +0000234/// isn't found, add it first.)
235void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
236 SmallSet<unsigned, 4> &SubKills) {
237 if (SubKills.count(Reg) == 0) {
Evan Cheng6130f662008-03-05 00:59:57 +0000238 MI->addRegisterKilled(Reg, TRI, true);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000239 return;
240 }
241
Evan Cheng6130f662008-03-05 00:59:57 +0000242 for (const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000243 unsigned SubReg = *SubRegs; ++SubRegs)
244 addRegisterKills(SubReg, MI, SubKills);
245}
246
247/// HandlePhysRegKill - The recursive version of HandlePhysRegKill. Returns true
248/// if:
249///
250/// - The register has no sub-registers and the machine instruction is the
251/// last def/use of the register, or
252/// - The register has sub-registers and none of them are killed elsewhere.
253///
Bill Wendling55574c22008-02-20 19:35:34 +0000254/// SubKills is filled with the set of sub-registers that are killed elsewhere.
Bill Wendling420cdeb2008-02-20 07:36:31 +0000255bool LiveVariables::HandlePhysRegKill(unsigned Reg, const MachineInstr *RefMI,
256 SmallSet<unsigned, 4> &SubKills) {
Evan Cheng6130f662008-03-05 00:59:57 +0000257 const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000258
259 for (; unsigned SubReg = *SubRegs; ++SubRegs) {
260 const MachineInstr *LastRef = PhysRegInfo[SubReg];
261
Evan Cheng0d8d3162007-09-12 23:02:04 +0000262 if (LastRef != RefMI ||
263 !HandlePhysRegKill(SubReg, RefMI, SubKills))
Evan Cheng4efe7412007-06-26 21:03:35 +0000264 SubKills.insert(SubReg);
265 }
266
Bill Wendling420cdeb2008-02-20 07:36:31 +0000267 if (*SubRegs == 0) {
Evan Cheng4efe7412007-06-26 21:03:35 +0000268 // No sub-registers, just check if reg is killed by RefMI.
Evan Cheng94202012008-03-19 00:52:20 +0000269 if (PhysRegInfo[Reg] == RefMI && PhysRegInfo[Reg]->readsRegister(Reg)) {
Evan Cheng4efe7412007-06-26 21:03:35 +0000270 return true;
Evan Cheng94202012008-03-19 00:52:20 +0000271 }
Bill Wendling420cdeb2008-02-20 07:36:31 +0000272 } else if (SubKills.empty()) {
273 // None of the sub-registers are killed elsewhere.
Evan Cheng4efe7412007-06-26 21:03:35 +0000274 return true;
Bill Wendling420cdeb2008-02-20 07:36:31 +0000275 }
276
Evan Cheng4efe7412007-06-26 21:03:35 +0000277 return false;
278}
279
Bill Wendling55574c22008-02-20 19:35:34 +0000280/// HandlePhysRegKill - Returns true if the whole register is killed in the
281/// machine instruction. If only some of its sub-registers are killed in this
282/// machine instruction, then mark those as killed and return false.
Evan Cheng4efe7412007-06-26 21:03:35 +0000283bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
284 SmallSet<unsigned, 4> SubKills;
Bill Wendling420cdeb2008-02-20 07:36:31 +0000285
Evan Cheng4efe7412007-06-26 21:03:35 +0000286 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
Bill Wendling420cdeb2008-02-20 07:36:31 +0000287 // This machine instruction kills this register.
Evan Cheng6130f662008-03-05 00:59:57 +0000288 RefMI->addRegisterKilled(Reg, TRI, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000289 return true;
Evan Cheng4efe7412007-06-26 21:03:35 +0000290 }
Bill Wendling420cdeb2008-02-20 07:36:31 +0000291
292 // Some sub-registers are killed by another machine instruction.
Evan Cheng6130f662008-03-05 00:59:57 +0000293 for (const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000294 unsigned SubReg = *SubRegs; ++SubRegs)
295 addRegisterKills(SubReg, RefMI, SubKills);
296
297 return false;
Evan Cheng4efe7412007-06-26 21:03:35 +0000298}
299
Evan Cheng94202012008-03-19 00:52:20 +0000300/// hasRegisterUseBelow - Return true if the specified register is used after
301/// the current instruction and before it's next definition.
302bool LiveVariables::hasRegisterUseBelow(unsigned Reg,
303 MachineBasicBlock::iterator I,
304 MachineBasicBlock *MBB) {
305 if (I == MBB->end())
306 return false;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000307
308 // First find out if there are any uses / defs below.
309 bool hasDistInfo = true;
310 unsigned CurDist = DistanceMap[I];
311 SmallVector<MachineInstr*, 4> Uses;
312 SmallVector<MachineInstr*, 4> Defs;
313 for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg),
314 RE = MRI->reg_end(); RI != RE; ++RI) {
315 MachineOperand &UDO = RI.getOperand();
316 MachineInstr *UDMI = &*RI;
317 if (UDMI->getParent() != MBB)
318 continue;
319 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
320 bool isBelow = false;
321 if (DI == DistanceMap.end()) {
322 // Must be below if it hasn't been assigned a distance yet.
323 isBelow = true;
324 hasDistInfo = false;
325 } else if (DI->second > CurDist)
326 isBelow = true;
327 if (isBelow) {
328 if (UDO.isUse())
329 Uses.push_back(UDMI);
330 if (UDO.isDef())
331 Defs.push_back(UDMI);
Evan Cheng94202012008-03-19 00:52:20 +0000332 }
333 }
Evan Chengea1d9cd2008-04-02 18:04:08 +0000334
335 if (Uses.empty())
336 // No uses below.
337 return false;
338 else if (!Uses.empty() && Defs.empty())
339 // There are uses below but no defs below.
340 return true;
341 // There are both uses and defs below. We need to know which comes first.
342 if (!hasDistInfo) {
343 // Complete DistanceMap for this MBB. This information is computed only
344 // once per MBB.
345 ++I;
346 ++CurDist;
347 for (MachineBasicBlock::iterator E = MBB->end(); I != E; ++I, ++CurDist)
348 DistanceMap.insert(std::make_pair(I, CurDist));
349 }
350
351 unsigned EarliestUse = CurDist;
352 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
353 unsigned Dist = DistanceMap[Uses[i]];
354 if (Dist < EarliestUse)
355 EarliestUse = Dist;
356 }
357 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
358 unsigned Dist = DistanceMap[Defs[i]];
359 if (Dist < EarliestUse)
360 // The register is defined before its first use below.
361 return false;
362 }
363 return true;
Evan Cheng94202012008-03-19 00:52:20 +0000364}
365
Chris Lattnerbc40e892003-01-13 20:01:16 +0000366void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
367 // Does this kill a previous version of this register?
Evan Cheng24a3cc42007-04-25 07:30:23 +0000368 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
Evan Cheng4efe7412007-06-26 21:03:35 +0000369 if (PhysRegUsed[Reg]) {
370 if (!HandlePhysRegKill(Reg, LastRef)) {
371 if (PhysRegPartUse[Reg])
Evan Cheng6130f662008-03-05 00:59:57 +0000372 PhysRegPartUse[Reg]->addRegisterKilled(Reg, TRI, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000373 }
Bill Wendling420cdeb2008-02-20 07:36:31 +0000374 } else if (PhysRegPartUse[Reg]) {
Evan Cheng21b3bf02007-08-01 20:18:21 +0000375 // Add implicit use / kill to last partial use.
Evan Cheng6130f662008-03-05 00:59:57 +0000376 PhysRegPartUse[Reg]->addRegisterKilled(Reg, TRI, true);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000377 } else if (LastRef != MI) {
Evan Cheng5942efb2007-11-05 03:11:55 +0000378 // Defined, but not used. However, watch out for cases where a super-reg
379 // is also defined on the same MI.
Evan Cheng6130f662008-03-05 00:59:57 +0000380 LastRef->addRegisterDead(Reg, TRI);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000381 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000382 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000383
Evan Cheng6130f662008-03-05 00:59:57 +0000384 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000385 unsigned SubReg = *SubRegs; ++SubRegs) {
386 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
Evan Cheng4efe7412007-06-26 21:03:35 +0000387 if (PhysRegUsed[SubReg]) {
388 if (!HandlePhysRegKill(SubReg, LastRef)) {
389 if (PhysRegPartUse[SubReg])
Evan Cheng6130f662008-03-05 00:59:57 +0000390 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, TRI, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000391 }
Bill Wendling420cdeb2008-02-20 07:36:31 +0000392 } else if (PhysRegPartUse[SubReg]) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000393 // Add implicit use / kill to last use of a sub-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000394 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, TRI, true);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000395 } else if (LastRef != MI) {
Evan Cheng6d6d3522007-09-11 22:34:47 +0000396 // This must be a def of the subreg on the same MI.
Evan Cheng6130f662008-03-05 00:59:57 +0000397 LastRef->addRegisterDead(SubReg, TRI);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000398 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000399 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000400 }
401
Evan Cheng4efe7412007-06-26 21:03:35 +0000402 if (MI) {
Evan Cheng6130f662008-03-05 00:59:57 +0000403 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000404 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Cheng6d6d3522007-09-11 22:34:47 +0000405 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000406 // The larger register is previously defined. Now a smaller part is
Evan Cheng94202012008-03-19 00:52:20 +0000407 // being re-defined. Treat it as read/mod/write if there are uses
408 // below.
Evan Cheng24a3cc42007-04-25 07:30:23 +0000409 // EAX =
410 // AX = EAX<imp-use,kill>, EAX<imp-def>
Evan Cheng94202012008-03-19 00:52:20 +0000411 // ...
412 /// = EAX
413 if (MI && hasRegisterUseBelow(SuperReg, MI, MI->getParent())) {
414 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
Chris Lattner8019f412007-12-30 00:41:17 +0000415 true/*IsImp*/,true/*IsKill*/));
Evan Cheng94202012008-03-19 00:52:20 +0000416 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
417 true/*IsImp*/));
418 PhysRegInfo[SuperReg] = MI;
419 } else {
420 PhysRegInfo[SuperReg]->addRegisterKilled(SuperReg, TRI, true);
421 PhysRegInfo[SuperReg] = NULL;
422 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000423 PhysRegUsed[SuperReg] = false;
Evan Cheng8b966d92007-05-14 20:39:18 +0000424 PhysRegPartUse[SuperReg] = NULL;
Evan Cheng24a3cc42007-04-25 07:30:23 +0000425 } else {
426 // Remember this partial def.
427 PhysRegPartDef[SuperReg].push_back(MI);
428 }
Evan Cheng4efe7412007-06-26 21:03:35 +0000429 }
430
431 PhysRegInfo[Reg] = MI;
432 PhysRegUsed[Reg] = false;
Evan Cheng21b3bf02007-08-01 20:18:21 +0000433 PhysRegPartDef[Reg].clear();
Evan Cheng4efe7412007-06-26 21:03:35 +0000434 PhysRegPartUse[Reg] = NULL;
Bill Wendling420cdeb2008-02-20 07:36:31 +0000435
Evan Cheng6130f662008-03-05 00:59:57 +0000436 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Evan Cheng4efe7412007-06-26 21:03:35 +0000437 unsigned SubReg = *SubRegs; ++SubRegs) {
438 PhysRegInfo[SubReg] = MI;
439 PhysRegUsed[SubReg] = false;
Evan Cheng21b3bf02007-08-01 20:18:21 +0000440 PhysRegPartDef[SubReg].clear();
Evan Cheng4efe7412007-06-26 21:03:35 +0000441 PhysRegPartUse[SubReg] = NULL;
442 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000443 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000444}
445
Evan Chengc6a24102007-03-17 09:29:54 +0000446bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
447 MF = &mf;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000448 MRI = &mf.getRegInfo();
Evan Cheng6130f662008-03-05 00:59:57 +0000449 TRI = MF->getTarget().getRegisterInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000450
Evan Cheng6130f662008-03-05 00:59:57 +0000451 ReservedRegisters = TRI->getReservedRegs(mf);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000452
Evan Cheng6130f662008-03-05 00:59:57 +0000453 unsigned NumRegs = TRI->getNumRegs();
Evan Chenge96f5012007-04-25 19:34:00 +0000454 PhysRegInfo = new MachineInstr*[NumRegs];
455 PhysRegUsed = new bool[NumRegs];
456 PhysRegPartUse = new MachineInstr*[NumRegs];
457 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
458 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
459 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
460 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
461 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000462
Bill Wendling6d794742008-02-20 09:15:16 +0000463 /// Get some space for a respectable number of registers.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000464 VirtRegInfo.resize(64);
Chris Lattnerd493b342005-04-09 15:23:25 +0000465
Evan Chengc6a24102007-03-17 09:29:54 +0000466 analyzePHINodes(mf);
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000467
Chris Lattnerbc40e892003-01-13 20:01:16 +0000468 // Calculate live variable information in depth first order on the CFG of the
469 // function. This guarantees that we will see the definition of a virtual
470 // register before its uses due to dominance properties of SSA (except for PHI
471 // nodes, which are treated as a special case).
Evan Chengc6a24102007-03-17 09:29:54 +0000472 MachineBasicBlock *Entry = MF->begin();
Evan Cheng04104072007-06-27 05:23:00 +0000473 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling6d794742008-02-20 09:15:16 +0000474
Evan Cheng04104072007-06-27 05:23:00 +0000475 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
476 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
477 DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000478 MachineBasicBlock *MBB = *DFI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000479
Evan Chengb371f452007-02-19 21:49:54 +0000480 // Mark live-in registers as live-in.
481 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000482 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000483 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000484 "Cannot have a live-in virtual register!");
485 HandlePhysRegDef(*II, 0);
486 }
487
Chris Lattnerbc40e892003-01-13 20:01:16 +0000488 // Loop over all of the instructions, processing them.
Evan Chengea1d9cd2008-04-02 18:04:08 +0000489 DistanceMap.clear();
490 unsigned Dist = 0;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000491 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000492 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000493 MachineInstr *MI = I;
Evan Chengea1d9cd2008-04-02 18:04:08 +0000494 DistanceMap.insert(std::make_pair(MI, Dist++));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000495
496 // Process all of the operands of the instruction...
497 unsigned NumOperandsToProcess = MI->getNumOperands();
498
499 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
500 // of the uses. They will be handled in other basic blocks.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000501 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman09ba9062004-06-24 21:31:16 +0000502 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000503
Bill Wendling6d794742008-02-20 09:15:16 +0000504 // Process all uses.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000505 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000506 const MachineOperand &MO = MI->getOperand(i);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000507
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000508 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Bill Wendling90a38682008-02-20 06:10:21 +0000509 unsigned MOReg = MO.getReg();
Bill Wendling420cdeb2008-02-20 07:36:31 +0000510
Bill Wendling90a38682008-02-20 06:10:21 +0000511 if (TargetRegisterInfo::isVirtualRegister(MOReg))
512 HandleVirtRegUse(MOReg, MBB, MI);
513 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
514 !ReservedRegisters[MOReg])
515 HandlePhysRegUse(MOReg, MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000516 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000517 }
518
Bill Wendling6d794742008-02-20 09:15:16 +0000519 // Process all defs.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000520 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000521 const MachineOperand &MO = MI->getOperand(i);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000522
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000523 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Bill Wendling90a38682008-02-20 06:10:21 +0000524 unsigned MOReg = MO.getReg();
Bill Wendling420cdeb2008-02-20 07:36:31 +0000525
Bill Wendling90a38682008-02-20 06:10:21 +0000526 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
527 VarInfo &VRInfo = getVarInfo(MOReg);
528
Evan Chengbb4151b2008-02-05 20:04:18 +0000529 if (VRInfo.AliveBlocks.none())
530 // If vr is not alive in any block, then defaults to dead.
531 VRInfo.Kills.push_back(MI);
Bill Wendling90a38682008-02-20 06:10:21 +0000532 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
533 !ReservedRegisters[MOReg]) {
534 HandlePhysRegDef(MOReg, MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000535 }
536 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000537 }
538 }
539
540 // Handle any virtual assignments from PHI nodes which might be at the
541 // bottom of this basic block. We check all of our successor blocks to see
542 // if they have PHI nodes, and if so, we simulate an assignment at the end
543 // of the current block.
Evan Chenge96f5012007-04-25 19:34:00 +0000544 if (!PHIVarInfo[MBB->getNumber()].empty()) {
545 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
Misha Brukmanedf128a2005-04-21 22:36:52 +0000546
Evan Chenge96f5012007-04-25 19:34:00 +0000547 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling420cdeb2008-02-20 07:36:31 +0000548 E = VarInfoVec.end(); I != E; ++I)
549 // Mark it alive only in the block we are representing.
Evan Chengea1d9cd2008-04-02 18:04:08 +0000550 MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
Owen Anderson40a627d2008-01-15 22:58:11 +0000551 MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000552 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000553
Bill Wendling6d794742008-02-20 09:15:16 +0000554 // Finally, if the last instruction in the block is a return, make sure to
555 // mark it as using all of the live-out values in the function.
Chris Lattner749c6f62008-01-07 07:27:27 +0000556 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Chris Lattnerd493b342005-04-09 15:23:25 +0000557 MachineInstr *Ret = &MBB->back();
Bill Wendling420cdeb2008-02-20 07:36:31 +0000558
Chris Lattner84bc5422007-12-31 04:13:23 +0000559 for (MachineRegisterInfo::liveout_iterator
560 I = MF->getRegInfo().liveout_begin(),
561 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000562 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Chris Lattnerd493b342005-04-09 15:23:25 +0000563 "Cannot have a live-in virtual register!");
564 HandlePhysRegUse(*I, Ret);
Bill Wendling420cdeb2008-02-20 07:36:31 +0000565
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000566 // Add live-out registers as implicit uses.
Evan Cheng6130f662008-03-05 00:59:57 +0000567 if (!Ret->readsRegister(*I))
Chris Lattner8019f412007-12-30 00:41:17 +0000568 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Chris Lattnerd493b342005-04-09 15:23:25 +0000569 }
570 }
571
Chris Lattnerbc40e892003-01-13 20:01:16 +0000572 // Loop over PhysRegInfo, killing any registers that are available at the
Bill Wendling6d794742008-02-20 09:15:16 +0000573 // end of the basic block. This also resets the PhysRegInfo map.
Evan Chenge96f5012007-04-25 19:34:00 +0000574 for (unsigned i = 0; i != NumRegs; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000575 if (PhysRegInfo[i])
Misha Brukman09ba9062004-06-24 21:31:16 +0000576 HandlePhysRegDef(i, 0);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000577
578 // Clear some states between BB's. These are purely local information.
Evan Chengade31f92007-04-25 21:34:08 +0000579 for (unsigned i = 0; i != NumRegs; ++i)
Evan Cheng24a3cc42007-04-25 07:30:23 +0000580 PhysRegPartDef[i].clear();
Bill Wendling420cdeb2008-02-20 07:36:31 +0000581
Evan Cheng4efe7412007-06-26 21:03:35 +0000582 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
583 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
Evan Chenge96f5012007-04-25 19:34:00 +0000584 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000585 }
586
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000587 // Convert and transfer the dead / killed information we have gathered into
588 // VirtRegInfo onto MI's.
Evan Chengf0e3bb12007-03-09 06:02:17 +0000589 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
Bill Wendling420cdeb2008-02-20 07:36:31 +0000590 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
591 if (VirtRegInfo[i].Kills[j] ==
Evan Chengea1d9cd2008-04-02 18:04:08 +0000592 MRI->getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
Bill Wendling420cdeb2008-02-20 07:36:31 +0000593 VirtRegInfo[i]
594 .Kills[j]->addRegisterDead(i +
595 TargetRegisterInfo::FirstVirtualRegister,
Evan Cheng6130f662008-03-05 00:59:57 +0000596 TRI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000597 else
Bill Wendling420cdeb2008-02-20 07:36:31 +0000598 VirtRegInfo[i]
599 .Kills[j]->addRegisterKilled(i +
600 TargetRegisterInfo::FirstVirtualRegister,
Evan Cheng6130f662008-03-05 00:59:57 +0000601 TRI);
Chris Lattnera5287a62004-07-01 04:24:29 +0000602
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000603 // Check to make sure there are no unreachable blocks in the MC CFG for the
604 // function. If so, it is due to a bug in the instruction selector or some
605 // other part of the code generator if this happens.
606#ifndef NDEBUG
Evan Chengc6a24102007-03-17 09:29:54 +0000607 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000608 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
609#endif
610
Evan Chenge96f5012007-04-25 19:34:00 +0000611 delete[] PhysRegInfo;
612 delete[] PhysRegUsed;
613 delete[] PhysRegPartUse;
614 delete[] PhysRegPartDef;
615 delete[] PHIVarInfo;
616
Chris Lattnerbc40e892003-01-13 20:01:16 +0000617 return false;
618}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000619
Bill Wendling6d794742008-02-20 09:15:16 +0000620/// instructionChanged - When the address of an instruction changes, this method
621/// should be called so that live variables can update its internal data
622/// structures. This removes the records for OldMI, transfering them to the
623/// records for NewMI.
Chris Lattner5ed001b2004-02-19 18:28:02 +0000624void LiveVariables::instructionChanged(MachineInstr *OldMI,
625 MachineInstr *NewMI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000626 // If the instruction defines any virtual registers, update the VarInfo,
627 // kill and dead information for the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000628 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
629 MachineOperand &MO = OldMI->getOperand(i);
Chris Lattnerd45be362005-01-19 17:09:15 +0000630 if (MO.isRegister() && MO.getReg() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000631 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Chris Lattner5ed001b2004-02-19 18:28:02 +0000632 unsigned Reg = MO.getReg();
633 VarInfo &VI = getVarInfo(Reg);
Chris Lattnerd45be362005-01-19 17:09:15 +0000634 if (MO.isDef()) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000635 if (MO.isDead()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000636 MO.setIsDead(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000637 addVirtualRegisterDead(Reg, NewMI);
638 }
Chris Lattner2a6e1632005-01-19 17:11:51 +0000639 }
Dan Gohmanc674a922007-07-20 23:17:34 +0000640 if (MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000641 MO.setIsKill(false);
Dan Gohmanc674a922007-07-20 23:17:34 +0000642 addVirtualRegisterKilled(Reg, NewMI);
Chris Lattnerd45be362005-01-19 17:09:15 +0000643 }
Dan Gohmanc674a922007-07-20 23:17:34 +0000644 // If this is a kill of the value, update the VI kills list.
645 if (VI.removeKill(OldMI))
646 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Chris Lattner5ed001b2004-02-19 18:28:02 +0000647 }
648 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000649}
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000650
651/// removeVirtualRegistersKilled - Remove all killed info for the specified
652/// instruction.
653void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000654 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
655 MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000656 if (MO.isRegister() && MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000657 MO.setIsKill(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000658 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000659 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000660 bool removed = getVarInfo(Reg).removeKill(MI);
661 assert(removed && "kill not in register's VarInfo?");
662 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000663 }
664 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000665}
666
667/// removeVirtualRegistersDead - Remove all of the dead registers for the
668/// specified instruction from the live variable information.
669void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000670 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
671 MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000672 if (MO.isRegister() && MO.isDead()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000673 MO.setIsDead(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000674 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000675 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000676 bool removed = getVarInfo(Reg).removeKill(MI);
677 assert(removed && "kill not in register's VarInfo?");
678 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000679 }
680 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000681}
682
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000683/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling6d794742008-02-20 09:15:16 +0000684/// particular, we want to map the variable information of a virtual register
685/// 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 +0000686///
687void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
688 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
689 I != E; ++I)
690 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
691 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
692 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendling90a38682008-02-20 06:10:21 +0000693 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
694 .push_back(BBI->getOperand(i).getReg());
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000695}