blob: 4b428d6f31ce40140ce656d568c61130930d133d [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
Bill Wendlingb88bca92008-02-20 06:10:21 +000079/// KillsRegister - Returns true if the machine instruction kills the specified
80/// register.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
82 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +000083 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +000084 if (MO.isRegister() && MO.isKill()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +000085 unsigned MOReg = MO.getReg();
86 if (MOReg == Reg ||
87 (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
Dan Gohman1e57df32008-02-10 18:45:23 +000088 TargetRegisterInfo::isPhysicalRegister(Reg) &&
Bill Wendlingb88bca92008-02-20 06:10:21 +000089 RegInfo->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 return true;
91 }
92 }
93 return false;
94}
95
Bill Wendlingb88bca92008-02-20 06:10:21 +000096/// RegisterDefIsDead - Returns true if the register is dead in this machine
97/// instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
99 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000100 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000101 if (MO.isRegister() && MO.isDead()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000102 unsigned MOReg = MO.getReg();
103 if ((MOReg == Reg) ||
104 (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000105 TargetRegisterInfo::isPhysicalRegister(Reg) &&
Bill Wendlingb88bca92008-02-20 06:10:21 +0000106 RegInfo->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return true;
108 }
109 }
110 return false;
111}
112
Bill Wendlingb88bca92008-02-20 06:10:21 +0000113/// ModifiesRegister - Returns true if the machine instruction modifies the
114/// register.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
116 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000117 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000118 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 return true;
120 }
121 return false;
122}
123
Owen Anderson77d80492008-01-15 22:58:11 +0000124void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
125 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 MachineBasicBlock *MBB,
127 std::vector<MachineBasicBlock*> &WorkList) {
128 unsigned BBNum = MBB->getNumber();
Owen Anderson92a609a2008-01-15 22:02:46 +0000129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendlingb88bca92008-02-20 06:10:21 +0000131 // remove it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
133 if (VRInfo.Kills[i]->getParent() == MBB) {
134 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
135 break;
136 }
Owen Anderson92a609a2008-01-15 22:02:46 +0000137
Owen Anderson77d80492008-01-15 22:58:11 +0000138 if (MBB == DefBlock) return; // Terminate recursion
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139
140 if (VRInfo.AliveBlocks[BBNum])
141 return; // We already know the block is live
142
143 // Mark the variable known alive in this bb
144 VRInfo.AliveBlocks[BBNum] = true;
145
146 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
147 E = MBB->pred_rend(); PI != E; ++PI)
148 WorkList.push_back(*PI);
149}
150
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000151void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Owen Anderson77d80492008-01-15 22:58:11 +0000152 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 MachineBasicBlock *MBB) {
154 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson77d80492008-01-15 22:58:11 +0000155 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 while (!WorkList.empty()) {
158 MachineBasicBlock *Pred = WorkList.back();
159 WorkList.pop_back();
Owen Anderson77d80492008-01-15 22:58:11 +0000160 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 }
162}
163
Owen Anderson92a609a2008-01-15 22:02:46 +0000164void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 MachineInstr *MI) {
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000166 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
Owen Anderson92a609a2008-01-15 22:02:46 +0000167 assert(MRI.getVRegDef(reg) && "Register use before def!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
Owen Anderson721b2cc2007-11-08 01:20:48 +0000169 unsigned BBNum = MBB->getNumber();
170
Owen Anderson92a609a2008-01-15 22:02:46 +0000171 VarInfo& VRInfo = getVarInfo(reg);
Owen Anderson721b2cc2007-11-08 01:20:48 +0000172 VRInfo.UsedBlocks[BBNum] = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 VRInfo.NumUses++;
174
Bill Wendlingb88bca92008-02-20 06:10:21 +0000175 // Check to see if this basic block is already a kill block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000177 // Yes, this register is killed in this basic block already. Increase the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 // live range by updating the kill instruction.
179 VRInfo.Kills.back() = MI;
180 return;
181 }
182
183#ifndef NDEBUG
184 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
185 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
186#endif
187
Owen Anderson92a609a2008-01-15 22:02:46 +0000188 assert(MBB != MRI.getVRegDef(reg)->getParent() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 "Should have kill for defblock!");
190
Bill Wendlingb88bca92008-02-20 06:10:21 +0000191 // Add a new kill entry for this basic block. If this virtual register is
192 // already marked as alive in this basic block, that means it is alive in at
193 // least one of the successor blocks, it's not a kill.
Owen Anderson721b2cc2007-11-08 01:20:48 +0000194 if (!VRInfo.AliveBlocks[BBNum])
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 VRInfo.Kills.push_back(MI);
196
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000197 // Update all dominating blocks to mark them as "known live".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
199 E = MBB->pred_end(); PI != E; ++PI)
Owen Anderson77d80492008-01-15 22:58:11 +0000200 MarkVirtRegAliveInBlock(VRInfo, MRI.getVRegDef(reg)->getParent(), *PI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201}
202
Bill Wendling85b03762008-02-20 09:15:16 +0000203/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
204/// implicit defs to a machine instruction if there was an earlier def of its
205/// super-register.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 // Turn previous partial def's into read/mod/write.
208 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
209 MachineInstr *Def = PhysRegPartDef[Reg][i];
Bill Wendling85b03762008-02-20 09:15:16 +0000210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 // First one is just a def. This means the use is reading some undef bits.
212 if (i != 0)
Bill Wendling85b03762008-02-20 09:15:16 +0000213 Def->addOperand(MachineOperand::CreateReg(Reg,
214 false /*IsDef*/,
215 true /*IsImp*/,
216 true /*IsKill*/));
217
218 Def->addOperand(MachineOperand::CreateReg(Reg,
219 true /*IsDef*/,
220 true /*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 }
Bill Wendlingb88bca92008-02-20 06:10:21 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 PhysRegPartDef[Reg].clear();
224
225 // There was an earlier def of a super-register. Add implicit def to that MI.
Bill Wendling85b03762008-02-20 09:15:16 +0000226 //
227 // A: EAX = ...
228 // B: ... = AX
229 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Add implicit def to A.
Evan Chenge993ca22007-09-11 22:34:47 +0000231 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
232 !PhysRegUsed[Reg]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 MachineInstr *Def = PhysRegInfo[Reg];
Bill Wendling85b03762008-02-20 09:15:16 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 if (!Def->findRegisterDefOperand(Reg))
Bill Wendling85b03762008-02-20 09:15:16 +0000236 Def->addOperand(MachineOperand::CreateReg(Reg,
237 true /*IsDef*/,
238 true /*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 }
240
Evan Chenge993ca22007-09-11 22:34:47 +0000241 // There is a now a proper use, forget about the last partial use.
242 PhysRegPartUse[Reg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 PhysRegInfo[Reg] = MI;
244 PhysRegUsed[Reg] = true;
245
Bill Wendling85b03762008-02-20 09:15:16 +0000246 // Now reset the use information for the sub-registers.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
248 unsigned SubReg = *SubRegs; ++SubRegs) {
Bill Wendling85b03762008-02-20 09:15:16 +0000249 // FIXME: Should we do: "PhysRegPartUse[SubReg] = NULL;" here?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 PhysRegInfo[SubReg] = MI;
251 PhysRegUsed[SubReg] = true;
252 }
253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
Evan Chenge4ec6192007-08-01 20:18:21 +0000255 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Bill Wendling85b03762008-02-20 09:15:16 +0000256 // Remember the partial use of this super-register if it was previously
257 // defined.
Evan Chenge4ec6192007-08-01 20:18:21 +0000258 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
Bill Wendling85b03762008-02-20 09:15:16 +0000259
260 if (!HasPrevDef)
Bill Wendling384458d2008-02-20 20:56:45 +0000261 // No need to go up more levels. A def of a register also sets its sub-
262 // registers. So if PhysRegInfo[SuperReg] is NULL, it means SuperReg's
263 // super-registers are not previously defined.
Evan Chenge4ec6192007-08-01 20:18:21 +0000264 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg);
Bill Wendling85b03762008-02-20 09:15:16 +0000265 unsigned SSReg = *SSRegs; ++SSRegs)
Evan Chenge4ec6192007-08-01 20:18:21 +0000266 if (PhysRegInfo[SSReg] != NULL) {
267 HasPrevDef = true;
268 break;
269 }
Bill Wendling85b03762008-02-20 09:15:16 +0000270
Evan Chenge4ec6192007-08-01 20:18:21 +0000271 if (HasPrevDef) {
272 PhysRegInfo[SuperReg] = MI;
273 PhysRegPartUse[SuperReg] = MI;
274 }
275 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276}
277
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000278/// addRegisterKills - For all of a register's sub-registers that are killed in
Bill Wendling65150ff2008-02-20 19:09:14 +0000279/// at this machine instruction, mark them as "killed". (If the machine operand
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000280/// isn't found, add it first.)
281void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
282 SmallSet<unsigned, 4> &SubKills) {
283 if (SubKills.count(Reg) == 0) {
284 MI->addRegisterKilled(Reg, RegInfo, true);
285 return;
286 }
287
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000289 unsigned SubReg = *SubRegs; ++SubRegs)
290 addRegisterKills(SubReg, MI, SubKills);
291}
292
293/// HandlePhysRegKill - The recursive version of HandlePhysRegKill. Returns true
294/// if:
295///
296/// - The register has no sub-registers and the machine instruction is the
297/// last def/use of the register, or
298/// - The register has sub-registers and none of them are killed elsewhere.
299///
Bill Wendlingbd88ee02008-02-20 19:35:34 +0000300/// SubKills is filled with the set of sub-registers that are killed elsewhere.
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000301bool LiveVariables::HandlePhysRegKill(unsigned Reg, const MachineInstr *RefMI,
302 SmallSet<unsigned, 4> &SubKills) {
303 const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
304
305 for (; unsigned SubReg = *SubRegs; ++SubRegs) {
306 const MachineInstr *LastRef = PhysRegInfo[SubReg];
307
Evan Cheng18ee3322007-09-12 23:02:04 +0000308 if (LastRef != RefMI ||
309 !HandlePhysRegKill(SubReg, RefMI, SubKills))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 SubKills.insert(SubReg);
311 }
312
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000313 if (*SubRegs == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 // No sub-registers, just check if reg is killed by RefMI.
315 if (PhysRegInfo[Reg] == RefMI)
316 return true;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000317 } else if (SubKills.empty()) {
318 // None of the sub-registers are killed elsewhere.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 return true;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000320 }
321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 return false;
323}
324
Bill Wendlingbd88ee02008-02-20 19:35:34 +0000325/// HandlePhysRegKill - Returns true if the whole register is killed in the
326/// machine instruction. If only some of its sub-registers are killed in this
327/// machine instruction, then mark those as killed and return false.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
329 SmallSet<unsigned, 4> SubKills;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000330
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000332 // This machine instruction kills this register.
Owen Anderson58060792008-01-24 01:10:07 +0000333 RefMI->addRegisterKilled(Reg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000336
337 // Some sub-registers are killed by another machine instruction.
338 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
339 unsigned SubReg = *SubRegs; ++SubRegs)
340 addRegisterKills(SubReg, RefMI, SubKills);
341
342 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343}
344
345void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
346 // Does this kill a previous version of this register?
347 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
348 if (PhysRegUsed[Reg]) {
349 if (!HandlePhysRegKill(Reg, LastRef)) {
350 if (PhysRegPartUse[Reg])
Owen Anderson58060792008-01-24 01:10:07 +0000351 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000353 } else if (PhysRegPartUse[Reg]) {
Evan Chenge4ec6192007-08-01 20:18:21 +0000354 // Add implicit use / kill to last partial use.
Owen Anderson58060792008-01-24 01:10:07 +0000355 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000356 } else if (LastRef != MI) {
Evan Cheng9cf8f9c2007-11-05 03:11:55 +0000357 // Defined, but not used. However, watch out for cases where a super-reg
358 // is also defined on the same MI.
Owen Anderson58060792008-01-24 01:10:07 +0000359 LastRef->addRegisterDead(Reg, RegInfo);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000360 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 }
362
363 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
364 unsigned SubReg = *SubRegs; ++SubRegs) {
365 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
366 if (PhysRegUsed[SubReg]) {
367 if (!HandlePhysRegKill(SubReg, LastRef)) {
368 if (PhysRegPartUse[SubReg])
Owen Anderson58060792008-01-24 01:10:07 +0000369 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000371 } else if (PhysRegPartUse[SubReg]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 // Add implicit use / kill to last use of a sub-register.
Owen Anderson58060792008-01-24 01:10:07 +0000373 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000374 } else if (LastRef != MI) {
Evan Chenge993ca22007-09-11 22:34:47 +0000375 // This must be a def of the subreg on the same MI.
Owen Anderson58060792008-01-24 01:10:07 +0000376 LastRef->addRegisterDead(SubReg, RegInfo);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000377 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379 }
380
381 if (MI) {
382 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
383 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Chenge993ca22007-09-11 22:34:47 +0000384 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 // The larger register is previously defined. Now a smaller part is
386 // being re-defined. Treat it as read/mod/write.
387 // EAX =
388 // AX = EAX<imp-use,kill>, EAX<imp-def>
Chris Lattner63ab1f22007-12-30 00:41:17 +0000389 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
390 true/*IsImp*/,true/*IsKill*/));
391 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
392 true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 PhysRegInfo[SuperReg] = MI;
394 PhysRegUsed[SuperReg] = false;
395 PhysRegPartUse[SuperReg] = NULL;
396 } else {
397 // Remember this partial def.
398 PhysRegPartDef[SuperReg].push_back(MI);
399 }
400 }
401
402 PhysRegInfo[Reg] = MI;
403 PhysRegUsed[Reg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000404 PhysRegPartDef[Reg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 PhysRegPartUse[Reg] = NULL;
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000406
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
408 unsigned SubReg = *SubRegs; ++SubRegs) {
409 PhysRegInfo[SubReg] = MI;
410 PhysRegUsed[SubReg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000411 PhysRegPartDef[SubReg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 PhysRegPartUse[SubReg] = NULL;
413 }
414 }
415}
416
417bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
418 MF = &mf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 RegInfo = MF->getTarget().getRegisterInfo();
Owen Anderson77d80492008-01-15 22:58:11 +0000420 MachineRegisterInfo& MRI = mf.getRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 assert(RegInfo && "Target doesn't have register information?");
422
423 ReservedRegisters = RegInfo->getReservedRegs(mf);
424
425 unsigned NumRegs = RegInfo->getNumRegs();
426 PhysRegInfo = new MachineInstr*[NumRegs];
427 PhysRegUsed = new bool[NumRegs];
428 PhysRegPartUse = new MachineInstr*[NumRegs];
429 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
430 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
431 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
432 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
433 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
434
Bill Wendling85b03762008-02-20 09:15:16 +0000435 /// Get some space for a respectable number of registers.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 VirtRegInfo.resize(64);
437
438 analyzePHINodes(mf);
439
440 // Calculate live variable information in depth first order on the CFG of the
441 // function. This guarantees that we will see the definition of a virtual
442 // register before its uses due to dominance properties of SSA (except for PHI
443 // nodes, which are treated as a special case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 MachineBasicBlock *Entry = MF->begin();
445 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling85b03762008-02-20 09:15:16 +0000446
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
448 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
449 DFI != E; ++DFI) {
450 MachineBasicBlock *MBB = *DFI;
451
452 // Mark live-in registers as live-in.
453 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
454 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000455 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 "Cannot have a live-in virtual register!");
457 HandlePhysRegDef(*II, 0);
458 }
459
460 // Loop over all of the instructions, processing them.
461 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
462 I != E; ++I) {
463 MachineInstr *MI = I;
464
465 // Process all of the operands of the instruction...
466 unsigned NumOperandsToProcess = MI->getNumOperands();
467
468 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
469 // of the uses. They will be handled in other basic blocks.
470 if (MI->getOpcode() == TargetInstrInfo::PHI)
471 NumOperandsToProcess = 1;
472
Bill Wendling85b03762008-02-20 09:15:16 +0000473 // Process all uses.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000475 const MachineOperand &MO = MI->getOperand(i);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000476
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000478 unsigned MOReg = MO.getReg();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000479
Bill Wendlingb88bca92008-02-20 06:10:21 +0000480 if (TargetRegisterInfo::isVirtualRegister(MOReg))
481 HandleVirtRegUse(MOReg, MBB, MI);
482 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
483 !ReservedRegisters[MOReg])
484 HandlePhysRegUse(MOReg, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 }
486 }
487
Bill Wendling85b03762008-02-20 09:15:16 +0000488 // Process all defs.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000490 const MachineOperand &MO = MI->getOperand(i);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000491
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000493 unsigned MOReg = MO.getReg();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000494
Bill Wendlingb88bca92008-02-20 06:10:21 +0000495 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
496 VarInfo &VRInfo = getVarInfo(MOReg);
497
Evan Cheng86f26d22008-02-05 20:04:18 +0000498 if (VRInfo.AliveBlocks.none())
499 // If vr is not alive in any block, then defaults to dead.
500 VRInfo.Kills.push_back(MI);
Bill Wendlingb88bca92008-02-20 06:10:21 +0000501 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
502 !ReservedRegisters[MOReg]) {
503 HandlePhysRegDef(MOReg, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 }
505 }
506 }
507 }
508
509 // Handle any virtual assignments from PHI nodes which might be at the
510 // bottom of this basic block. We check all of our successor blocks to see
511 // if they have PHI nodes, and if so, we simulate an assignment at the end
512 // of the current block.
513 if (!PHIVarInfo[MBB->getNumber()].empty()) {
514 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
515
516 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000517 E = VarInfoVec.end(); I != E; ++I)
518 // Mark it alive only in the block we are representing.
Owen Anderson77d80492008-01-15 22:58:11 +0000519 MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(),
520 MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 }
522
Bill Wendling85b03762008-02-20 09:15:16 +0000523 // Finally, if the last instruction in the block is a return, make sure to
524 // mark it as using all of the live-out values in the function.
Chris Lattner5b930372008-01-07 07:27:27 +0000525 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 MachineInstr *Ret = &MBB->back();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000527
Chris Lattner1b989192007-12-31 04:13:23 +0000528 for (MachineRegisterInfo::liveout_iterator
529 I = MF->getRegInfo().liveout_begin(),
530 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000531 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 "Cannot have a live-in virtual register!");
533 HandlePhysRegUse(*I, Ret);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000534
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 // Add live-out registers as implicit uses.
536 if (Ret->findRegisterUseOperandIdx(*I) == -1)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000537 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 }
539 }
540
541 // Loop over PhysRegInfo, killing any registers that are available at the
Bill Wendling85b03762008-02-20 09:15:16 +0000542 // end of the basic block. This also resets the PhysRegInfo map.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 for (unsigned i = 0; i != NumRegs; ++i)
544 if (PhysRegInfo[i])
545 HandlePhysRegDef(i, 0);
546
547 // Clear some states between BB's. These are purely local information.
548 for (unsigned i = 0; i != NumRegs; ++i)
549 PhysRegPartDef[i].clear();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000550
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
552 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
553 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
554 }
555
556 // Convert and transfer the dead / killed information we have gathered into
557 // VirtRegInfo onto MI's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000559 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
560 if (VirtRegInfo[i].Kills[j] ==
561 MRI.getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
562 VirtRegInfo[i]
563 .Kills[j]->addRegisterDead(i +
564 TargetRegisterInfo::FirstVirtualRegister,
565 RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 else
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000567 VirtRegInfo[i]
568 .Kills[j]->addRegisterKilled(i +
569 TargetRegisterInfo::FirstVirtualRegister,
570 RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571
572 // Check to make sure there are no unreachable blocks in the MC CFG for the
573 // function. If so, it is due to a bug in the instruction selector or some
574 // other part of the code generator if this happens.
575#ifndef NDEBUG
576 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
577 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
578#endif
579
580 delete[] PhysRegInfo;
581 delete[] PhysRegUsed;
582 delete[] PhysRegPartUse;
583 delete[] PhysRegPartDef;
584 delete[] PHIVarInfo;
585
586 return false;
587}
588
Bill Wendling85b03762008-02-20 09:15:16 +0000589/// instructionChanged - When the address of an instruction changes, this method
590/// should be called so that live variables can update its internal data
591/// structures. This removes the records for OldMI, transfering them to the
592/// records for NewMI.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593void LiveVariables::instructionChanged(MachineInstr *OldMI,
594 MachineInstr *NewMI) {
595 // If the instruction defines any virtual registers, update the VarInfo,
596 // kill and dead information for the instruction.
597 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
598 MachineOperand &MO = OldMI->getOperand(i);
599 if (MO.isRegister() && MO.getReg() &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000600 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 unsigned Reg = MO.getReg();
602 VarInfo &VI = getVarInfo(Reg);
603 if (MO.isDef()) {
604 if (MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000605 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 addVirtualRegisterDead(Reg, NewMI);
607 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000609 if (MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000610 MO.setIsKill(false);
Dan Gohman2c6a6422007-07-20 23:17:34 +0000611 addVirtualRegisterKilled(Reg, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000613 // If this is a kill of the value, update the VI kills list.
614 if (VI.removeKill(OldMI))
615 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 }
617 }
618}
619
620/// removeVirtualRegistersKilled - Remove all killed info for the specified
621/// instruction.
622void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
623 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
624 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000625 if (MO.isRegister() && MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000626 MO.setIsKill(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000628 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 bool removed = getVarInfo(Reg).removeKill(MI);
630 assert(removed && "kill not in register's VarInfo?");
631 }
632 }
633 }
634}
635
636/// removeVirtualRegistersDead - Remove all of the dead registers for the
637/// specified instruction from the live variable information.
638void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
639 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
640 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000641 if (MO.isRegister() && MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000642 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000644 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 bool removed = getVarInfo(Reg).removeKill(MI);
646 assert(removed && "kill not in register's VarInfo?");
647 }
648 }
649 }
650}
651
652/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling85b03762008-02-20 09:15:16 +0000653/// particular, we want to map the variable information of a virtual register
654/// 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 +0000655///
656void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
657 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
658 I != E; ++I)
659 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
660 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
661 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendlingb88bca92008-02-20 06:10:21 +0000662 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
663 .push_back(BBI->getOperand(i).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664}