blob: 27f459d4f911e25ace467c28b2c30573194ba33e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveVariable analysis pass. For each machine
11// instruction in the function, this pass calculates the set of registers that
12// are immediately dead after the instruction (i.e., the instruction calculates
13// the value, but it is never used) and the set of registers that are used by
14// the instruction, but are never used after the instruction (i.e., they are
15// killed).
16//
17// This class computes live variables using are sparse implementation based on
18// the machine code SSA form. This class computes live variable information for
19// each virtual and _register allocatable_ physical register in a function. It
20// uses the dominance properties of SSA form to efficiently compute live
21// variables for virtual registers, and assumes that physical registers are only
22// live within a single basic block (allowing it to do a single local analysis
23// to resolve physical register lifetimes in each basic block). If a physical
24// register is not register allocatable, it is not tracked. This is useful for
25// things like the stack pointer and condition codes.
26//
27//===----------------------------------------------------------------------===//
28
29#include "llvm/CodeGen/LiveVariables.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner1b989192007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/ADT/DepthFirstIterator.h"
36#include "llvm/ADT/SmallPtrSet.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/Config/alloca.h"
39#include <algorithm>
40using namespace llvm;
41
42char LiveVariables::ID = 0;
43static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
44
45void LiveVariables::VarInfo::dump() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 cerr << " Alive in blocks: ";
47 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
48 if (AliveBlocks[i]) cerr << i << ", ";
Owen Anderson721b2cc2007-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 << ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 cerr << "\n Killed by:";
53 if (Kills.empty())
54 cerr << " No instructions.\n";
55 else {
56 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
57 cerr << "\n #" << i << ": " << *Kills[i];
58 cerr << "\n";
59 }
60}
61
Bill Wendlingb88bca92008-02-20 06:10:21 +000062/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Dan Gohman1e57df32008-02-10 18:45:23 +000064 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 "getVarInfo: not a virtual register!");
Dan Gohman1e57df32008-02-10 18:45:23 +000066 RegIdx -= TargetRegisterInfo::FirstVirtualRegister;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
73 VarInfo &VI = VirtRegInfo[RegIdx];
74 VI.AliveBlocks.resize(MF->getNumBlockIDs());
Owen Anderson721b2cc2007-11-08 01:20:48 +000075 VI.UsedBlocks.resize(MF->getNumBlockIDs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 return VI;
77}
78
Owen Anderson77d80492008-01-15 22:58:11 +000079void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
80 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 MachineBasicBlock *MBB,
82 std::vector<MachineBasicBlock*> &WorkList) {
83 unsigned BBNum = MBB->getNumber();
Owen Anderson92a609a2008-01-15 22:02:46 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendlingb88bca92008-02-20 06:10:21 +000086 // remove it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
88 if (VRInfo.Kills[i]->getParent() == MBB) {
89 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
90 break;
91 }
Owen Anderson92a609a2008-01-15 22:02:46 +000092
Owen Anderson77d80492008-01-15 22:58:11 +000093 if (MBB == DefBlock) return; // Terminate recursion
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 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
101 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
102 E = MBB->pred_rend(); PI != E; ++PI)
103 WorkList.push_back(*PI);
104}
105
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000106void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Owen Anderson77d80492008-01-15 22:58:11 +0000107 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 MachineBasicBlock *MBB) {
109 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson77d80492008-01-15 22:58:11 +0000110 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 while (!WorkList.empty()) {
113 MachineBasicBlock *Pred = WorkList.back();
114 WorkList.pop_back();
Owen Anderson77d80492008-01-15 22:58:11 +0000115 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 }
117}
118
Owen Anderson92a609a2008-01-15 22:02:46 +0000119void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 MachineInstr *MI) {
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000121 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
Owen Anderson92a609a2008-01-15 22:02:46 +0000122 assert(MRI.getVRegDef(reg) && "Register use before def!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
Owen Anderson721b2cc2007-11-08 01:20:48 +0000124 unsigned BBNum = MBB->getNumber();
125
Owen Anderson92a609a2008-01-15 22:02:46 +0000126 VarInfo& VRInfo = getVarInfo(reg);
Owen Anderson721b2cc2007-11-08 01:20:48 +0000127 VRInfo.UsedBlocks[BBNum] = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 VRInfo.NumUses++;
129
Bill Wendlingb88bca92008-02-20 06:10:21 +0000130 // Check to see if this basic block is already a kill block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000132 // Yes, this register is killed in this basic block already. Increase the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 // live range by updating the kill instruction.
134 VRInfo.Kills.back() = MI;
135 return;
136 }
137
138#ifndef NDEBUG
139 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
140 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
141#endif
142
Owen Anderson92a609a2008-01-15 22:02:46 +0000143 assert(MBB != MRI.getVRegDef(reg)->getParent() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 "Should have kill for defblock!");
145
Bill Wendlingb88bca92008-02-20 06:10:21 +0000146 // Add a new kill entry for this basic block. If this virtual register is
147 // already marked as alive in this basic block, that means it is alive in at
148 // least one of the successor blocks, it's not a kill.
Owen Anderson721b2cc2007-11-08 01:20:48 +0000149 if (!VRInfo.AliveBlocks[BBNum])
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 VRInfo.Kills.push_back(MI);
151
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000152 // Update all dominating blocks to mark them as "known live".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
154 E = MBB->pred_end(); PI != E; ++PI)
Owen Anderson77d80492008-01-15 22:58:11 +0000155 MarkVirtRegAliveInBlock(VRInfo, MRI.getVRegDef(reg)->getParent(), *PI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156}
157
Bill Wendling85b03762008-02-20 09:15:16 +0000158/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
159/// implicit defs to a machine instruction if there was an earlier def of its
160/// super-register.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 // Turn previous partial def's into read/mod/write.
163 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
164 MachineInstr *Def = PhysRegPartDef[Reg][i];
Bill Wendling85b03762008-02-20 09:15:16 +0000165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 // First one is just a def. This means the use is reading some undef bits.
167 if (i != 0)
Bill Wendling85b03762008-02-20 09:15:16 +0000168 Def->addOperand(MachineOperand::CreateReg(Reg,
169 false /*IsDef*/,
170 true /*IsImp*/,
171 true /*IsKill*/));
172
173 Def->addOperand(MachineOperand::CreateReg(Reg,
174 true /*IsDef*/,
175 true /*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
Bill Wendlingb88bca92008-02-20 06:10:21 +0000177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 PhysRegPartDef[Reg].clear();
179
180 // There was an earlier def of a super-register. Add implicit def to that MI.
Bill Wendling85b03762008-02-20 09:15:16 +0000181 //
182 // A: EAX = ...
183 // B: ... = AX
184 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 // Add implicit def to A.
Evan Chenge993ca22007-09-11 22:34:47 +0000186 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
187 !PhysRegUsed[Reg]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 MachineInstr *Def = PhysRegInfo[Reg];
Bill Wendling85b03762008-02-20 09:15:16 +0000189
Evan Chengc7daf1f2008-03-05 00:59:57 +0000190 if (!Def->modifiesRegister(Reg))
Bill Wendling85b03762008-02-20 09:15:16 +0000191 Def->addOperand(MachineOperand::CreateReg(Reg,
192 true /*IsDef*/,
193 true /*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 }
195
Evan Chenge993ca22007-09-11 22:34:47 +0000196 // There is a now a proper use, forget about the last partial use.
197 PhysRegPartUse[Reg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 PhysRegInfo[Reg] = MI;
199 PhysRegUsed[Reg] = true;
200
Bill Wendling85b03762008-02-20 09:15:16 +0000201 // Now reset the use information for the sub-registers.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000202 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 unsigned SubReg = *SubRegs; ++SubRegs) {
Bill Wendlingf01bd9e2008-02-21 19:35:27 +0000204 PhysRegPartUse[SubReg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 PhysRegInfo[SubReg] = MI;
206 PhysRegUsed[SubReg] = true;
207 }
208
Evan Chengc7daf1f2008-03-05 00:59:57 +0000209 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
Evan Chenge4ec6192007-08-01 20:18:21 +0000210 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Bill Wendling85b03762008-02-20 09:15:16 +0000211 // Remember the partial use of this super-register if it was previously
212 // defined.
Evan Chenge4ec6192007-08-01 20:18:21 +0000213 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
Bill Wendling85b03762008-02-20 09:15:16 +0000214
215 if (!HasPrevDef)
Bill Wendling384458d2008-02-20 20:56:45 +0000216 // No need to go up more levels. A def of a register also sets its sub-
217 // registers. So if PhysRegInfo[SuperReg] is NULL, it means SuperReg's
218 // super-registers are not previously defined.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000219 for (const unsigned *SSRegs = TRI->getSuperRegisters(SuperReg);
Bill Wendling85b03762008-02-20 09:15:16 +0000220 unsigned SSReg = *SSRegs; ++SSRegs)
Evan Chenge4ec6192007-08-01 20:18:21 +0000221 if (PhysRegInfo[SSReg] != NULL) {
222 HasPrevDef = true;
223 break;
224 }
Bill Wendling85b03762008-02-20 09:15:16 +0000225
Evan Chenge4ec6192007-08-01 20:18:21 +0000226 if (HasPrevDef) {
227 PhysRegInfo[SuperReg] = MI;
228 PhysRegPartUse[SuperReg] = MI;
229 }
230 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231}
232
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000233/// addRegisterKills - For all of a register's sub-registers that are killed in
Bill Wendling65150ff2008-02-20 19:09:14 +0000234/// at this machine instruction, mark them as "killed". (If the machine operand
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000235/// isn't found, add it first.)
236void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
237 SmallSet<unsigned, 4> &SubKills) {
238 if (SubKills.count(Reg) == 0) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000239 MI->addRegisterKilled(Reg, TRI, true);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000240 return;
241 }
242
Evan Chengc7daf1f2008-03-05 00:59:57 +0000243 for (const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000244 unsigned SubReg = *SubRegs; ++SubRegs)
245 addRegisterKills(SubReg, MI, SubKills);
246}
247
248/// HandlePhysRegKill - The recursive version of HandlePhysRegKill. Returns true
249/// if:
250///
251/// - The register has no sub-registers and the machine instruction is the
252/// last def/use of the register, or
253/// - The register has sub-registers and none of them are killed elsewhere.
254///
Bill Wendlingbd88ee02008-02-20 19:35:34 +0000255/// SubKills is filled with the set of sub-registers that are killed elsewhere.
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000256bool LiveVariables::HandlePhysRegKill(unsigned Reg, const MachineInstr *RefMI,
257 SmallSet<unsigned, 4> &SubKills) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000258 const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000259
260 for (; unsigned SubReg = *SubRegs; ++SubRegs) {
261 const MachineInstr *LastRef = PhysRegInfo[SubReg];
262
Evan Cheng18ee3322007-09-12 23:02:04 +0000263 if (LastRef != RefMI ||
264 !HandlePhysRegKill(SubReg, RefMI, SubKills))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 SubKills.insert(SubReg);
266 }
267
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000268 if (*SubRegs == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // No sub-registers, just check if reg is killed by RefMI.
270 if (PhysRegInfo[Reg] == RefMI)
271 return true;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000272 } else if (SubKills.empty()) {
273 // None of the sub-registers are killed elsewhere.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 return true;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000275 }
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 return false;
278}
279
Bill Wendlingbd88ee02008-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
284 SmallSet<unsigned, 4> SubKills;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000285
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000287 // This machine instruction kills this register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000288 RefMI->addRegisterKilled(Reg, TRI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000291
292 // Some sub-registers are killed by another machine instruction.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000293 for (const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000294 unsigned SubReg = *SubRegs; ++SubRegs)
295 addRegisterKills(SubReg, RefMI, SubKills);
296
297 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298}
299
300void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
301 // Does this kill a previous version of this register?
302 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
303 if (PhysRegUsed[Reg]) {
304 if (!HandlePhysRegKill(Reg, LastRef)) {
305 if (PhysRegPartUse[Reg])
Evan Chengc7daf1f2008-03-05 00:59:57 +0000306 PhysRegPartUse[Reg]->addRegisterKilled(Reg, TRI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000308 } else if (PhysRegPartUse[Reg]) {
Evan Chenge4ec6192007-08-01 20:18:21 +0000309 // Add implicit use / kill to last partial use.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000310 PhysRegPartUse[Reg]->addRegisterKilled(Reg, TRI, true);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000311 } else if (LastRef != MI) {
Evan Cheng9cf8f9c2007-11-05 03:11:55 +0000312 // Defined, but not used. However, watch out for cases where a super-reg
313 // is also defined on the same MI.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000314 LastRef->addRegisterDead(Reg, TRI);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000315 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 }
317
Evan Chengc7daf1f2008-03-05 00:59:57 +0000318 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 unsigned SubReg = *SubRegs; ++SubRegs) {
320 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
321 if (PhysRegUsed[SubReg]) {
322 if (!HandlePhysRegKill(SubReg, LastRef)) {
323 if (PhysRegPartUse[SubReg])
Evan Chengc7daf1f2008-03-05 00:59:57 +0000324 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, TRI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000326 } else if (PhysRegPartUse[SubReg]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 // Add implicit use / kill to last use of a sub-register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000328 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, TRI, true);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000329 } else if (LastRef != MI) {
Evan Chenge993ca22007-09-11 22:34:47 +0000330 // This must be a def of the subreg on the same MI.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000331 LastRef->addRegisterDead(SubReg, TRI);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000332 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 }
334 }
335
336 if (MI) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000337 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Chenge993ca22007-09-11 22:34:47 +0000339 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 // The larger register is previously defined. Now a smaller part is
341 // being re-defined. Treat it as read/mod/write.
342 // EAX =
343 // AX = EAX<imp-use,kill>, EAX<imp-def>
Chris Lattner63ab1f22007-12-30 00:41:17 +0000344 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
345 true/*IsImp*/,true/*IsKill*/));
346 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
347 true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 PhysRegInfo[SuperReg] = MI;
349 PhysRegUsed[SuperReg] = false;
350 PhysRegPartUse[SuperReg] = NULL;
351 } else {
352 // Remember this partial def.
353 PhysRegPartDef[SuperReg].push_back(MI);
354 }
355 }
356
357 PhysRegInfo[Reg] = MI;
358 PhysRegUsed[Reg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000359 PhysRegPartDef[Reg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 PhysRegPartUse[Reg] = NULL;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000361
Evan Chengc7daf1f2008-03-05 00:59:57 +0000362 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 unsigned SubReg = *SubRegs; ++SubRegs) {
364 PhysRegInfo[SubReg] = MI;
365 PhysRegUsed[SubReg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000366 PhysRegPartDef[SubReg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 PhysRegPartUse[SubReg] = NULL;
368 }
369 }
370}
371
372bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
373 MF = &mf;
Evan Chengc7daf1f2008-03-05 00:59:57 +0000374 TRI = MF->getTarget().getRegisterInfo();
Owen Anderson77d80492008-01-15 22:58:11 +0000375 MachineRegisterInfo& MRI = mf.getRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
Evan Chengc7daf1f2008-03-05 00:59:57 +0000377 ReservedRegisters = TRI->getReservedRegs(mf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378
Evan Chengc7daf1f2008-03-05 00:59:57 +0000379 unsigned NumRegs = TRI->getNumRegs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 PhysRegInfo = new MachineInstr*[NumRegs];
381 PhysRegUsed = new bool[NumRegs];
382 PhysRegPartUse = new MachineInstr*[NumRegs];
383 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
384 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
385 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
386 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
387 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
388
Bill Wendling85b03762008-02-20 09:15:16 +0000389 /// Get some space for a respectable number of registers.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 VirtRegInfo.resize(64);
391
392 analyzePHINodes(mf);
393
394 // Calculate live variable information in depth first order on the CFG of the
395 // function. This guarantees that we will see the definition of a virtual
396 // register before its uses due to dominance properties of SSA (except for PHI
397 // nodes, which are treated as a special case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 MachineBasicBlock *Entry = MF->begin();
399 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling85b03762008-02-20 09:15:16 +0000400
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
402 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
403 DFI != E; ++DFI) {
404 MachineBasicBlock *MBB = *DFI;
405
406 // Mark live-in registers as live-in.
407 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
408 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000409 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 "Cannot have a live-in virtual register!");
411 HandlePhysRegDef(*II, 0);
412 }
413
414 // Loop over all of the instructions, processing them.
415 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
416 I != E; ++I) {
417 MachineInstr *MI = I;
418
419 // Process all of the operands of the instruction...
420 unsigned NumOperandsToProcess = MI->getNumOperands();
421
422 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
423 // of the uses. They will be handled in other basic blocks.
424 if (MI->getOpcode() == TargetInstrInfo::PHI)
425 NumOperandsToProcess = 1;
426
Bill Wendling85b03762008-02-20 09:15:16 +0000427 // Process all uses.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000429 const MachineOperand &MO = MI->getOperand(i);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000430
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000432 unsigned MOReg = MO.getReg();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000433
Bill Wendlingb88bca92008-02-20 06:10:21 +0000434 if (TargetRegisterInfo::isVirtualRegister(MOReg))
435 HandleVirtRegUse(MOReg, MBB, MI);
436 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
437 !ReservedRegisters[MOReg])
438 HandlePhysRegUse(MOReg, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 }
440 }
441
Bill Wendling85b03762008-02-20 09:15:16 +0000442 // Process all defs.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000444 const MachineOperand &MO = MI->getOperand(i);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000447 unsigned MOReg = MO.getReg();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000448
Bill Wendlingb88bca92008-02-20 06:10:21 +0000449 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
450 VarInfo &VRInfo = getVarInfo(MOReg);
451
Evan Cheng86f26d22008-02-05 20:04:18 +0000452 if (VRInfo.AliveBlocks.none())
453 // If vr is not alive in any block, then defaults to dead.
454 VRInfo.Kills.push_back(MI);
Bill Wendlingb88bca92008-02-20 06:10:21 +0000455 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
456 !ReservedRegisters[MOReg]) {
457 HandlePhysRegDef(MOReg, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 }
459 }
460 }
461 }
462
463 // Handle any virtual assignments from PHI nodes which might be at the
464 // bottom of this basic block. We check all of our successor blocks to see
465 // if they have PHI nodes, and if so, we simulate an assignment at the end
466 // of the current block.
467 if (!PHIVarInfo[MBB->getNumber()].empty()) {
468 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
469
470 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000471 E = VarInfoVec.end(); I != E; ++I)
472 // Mark it alive only in the block we are representing.
Owen Anderson77d80492008-01-15 22:58:11 +0000473 MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(),
474 MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 }
476
Bill Wendling85b03762008-02-20 09:15:16 +0000477 // Finally, if the last instruction in the block is a return, make sure to
478 // mark it as using all of the live-out values in the function.
Chris Lattner5b930372008-01-07 07:27:27 +0000479 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 MachineInstr *Ret = &MBB->back();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000481
Chris Lattner1b989192007-12-31 04:13:23 +0000482 for (MachineRegisterInfo::liveout_iterator
483 I = MF->getRegInfo().liveout_begin(),
484 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000485 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 "Cannot have a live-in virtual register!");
487 HandlePhysRegUse(*I, Ret);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 // Add live-out registers as implicit uses.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000490 if (!Ret->readsRegister(*I))
Chris Lattner63ab1f22007-12-30 00:41:17 +0000491 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 }
493 }
494
495 // Loop over PhysRegInfo, killing any registers that are available at the
Bill Wendling85b03762008-02-20 09:15:16 +0000496 // end of the basic block. This also resets the PhysRegInfo map.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 for (unsigned i = 0; i != NumRegs; ++i)
498 if (PhysRegInfo[i])
499 HandlePhysRegDef(i, 0);
500
501 // Clear some states between BB's. These are purely local information.
502 for (unsigned i = 0; i != NumRegs; ++i)
503 PhysRegPartDef[i].clear();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
506 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
507 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
508 }
509
510 // Convert and transfer the dead / killed information we have gathered into
511 // VirtRegInfo onto MI's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000513 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
514 if (VirtRegInfo[i].Kills[j] ==
515 MRI.getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
516 VirtRegInfo[i]
517 .Kills[j]->addRegisterDead(i +
518 TargetRegisterInfo::FirstVirtualRegister,
Evan Chengc7daf1f2008-03-05 00:59:57 +0000519 TRI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 else
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000521 VirtRegInfo[i]
522 .Kills[j]->addRegisterKilled(i +
523 TargetRegisterInfo::FirstVirtualRegister,
Evan Chengc7daf1f2008-03-05 00:59:57 +0000524 TRI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525
526 // Check to make sure there are no unreachable blocks in the MC CFG for the
527 // function. If so, it is due to a bug in the instruction selector or some
528 // other part of the code generator if this happens.
529#ifndef NDEBUG
530 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
531 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
532#endif
533
534 delete[] PhysRegInfo;
535 delete[] PhysRegUsed;
536 delete[] PhysRegPartUse;
537 delete[] PhysRegPartDef;
538 delete[] PHIVarInfo;
539
540 return false;
541}
542
Bill Wendling85b03762008-02-20 09:15:16 +0000543/// instructionChanged - When the address of an instruction changes, this method
544/// should be called so that live variables can update its internal data
545/// structures. This removes the records for OldMI, transfering them to the
546/// records for NewMI.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547void LiveVariables::instructionChanged(MachineInstr *OldMI,
548 MachineInstr *NewMI) {
549 // If the instruction defines any virtual registers, update the VarInfo,
550 // kill and dead information for the instruction.
551 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
552 MachineOperand &MO = OldMI->getOperand(i);
553 if (MO.isRegister() && MO.getReg() &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000554 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 unsigned Reg = MO.getReg();
556 VarInfo &VI = getVarInfo(Reg);
557 if (MO.isDef()) {
558 if (MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000559 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 addVirtualRegisterDead(Reg, NewMI);
561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000563 if (MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000564 MO.setIsKill(false);
Dan Gohman2c6a6422007-07-20 23:17:34 +0000565 addVirtualRegisterKilled(Reg, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000567 // If this is a kill of the value, update the VI kills list.
568 if (VI.removeKill(OldMI))
569 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 }
571 }
572}
573
574/// removeVirtualRegistersKilled - Remove all killed info for the specified
575/// instruction.
576void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
577 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
578 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000579 if (MO.isRegister() && MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000580 MO.setIsKill(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000582 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 bool removed = getVarInfo(Reg).removeKill(MI);
584 assert(removed && "kill not in register's VarInfo?");
585 }
586 }
587 }
588}
589
590/// removeVirtualRegistersDead - Remove all of the dead registers for the
591/// specified instruction from the live variable information.
592void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
593 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
594 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000595 if (MO.isRegister() && MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000596 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000598 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 bool removed = getVarInfo(Reg).removeKill(MI);
600 assert(removed && "kill not in register's VarInfo?");
601 }
602 }
603 }
604}
605
606/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling85b03762008-02-20 09:15:16 +0000607/// particular, we want to map the variable information of a virtual register
608/// which is used in a PHI node. We map that to the BB the vreg is coming from.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609///
610void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
611 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
612 I != E; ++I)
613 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
614 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
615 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendlingb88bca92008-02-20 06:10:21 +0000616 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
617 .push_back(BBI->getOperand(i).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618}