blob: 4acc6aa2ff8ad7568fdd84f9d4e790530a680d29 [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
Bill Wendling90a38682008-02-20 06:10:21 +000079/// KillsRegister - Returns true if the machine instruction kills the specified
80/// register.
Chris Lattner657b4d12005-08-24 00:09:33 +000081bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
Evan Chenga6c4c1e2006-11-15 20:51:59 +000082 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +000083 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +000084 if (MO.isRegister() && MO.isKill()) {
Bill Wendling90a38682008-02-20 06:10:21 +000085 unsigned MOReg = MO.getReg();
86 if (MOReg == Reg ||
87 (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
Dan Gohman6f0d0242008-02-10 18:45:23 +000088 TargetRegisterInfo::isPhysicalRegister(Reg) &&
Bill Wendling90a38682008-02-20 06:10:21 +000089 RegInfo->isSubRegister(MOReg, Reg)))
Evan Chenga6c4c1e2006-11-15 20:51:59 +000090 return true;
91 }
92 }
93 return false;
Chris Lattner657b4d12005-08-24 00:09:33 +000094}
95
Bill Wendling90a38682008-02-20 06:10:21 +000096/// RegisterDefIsDead - Returns true if the register is dead in this machine
97/// instruction.
Chris Lattner657b4d12005-08-24 00:09:33 +000098bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
Evan Chenga6c4c1e2006-11-15 20:51:59 +000099 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000100 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000101 if (MO.isRegister() && MO.isDead()) {
Bill Wendling90a38682008-02-20 06:10:21 +0000102 unsigned MOReg = MO.getReg();
103 if ((MOReg == Reg) ||
104 (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000105 TargetRegisterInfo::isPhysicalRegister(Reg) &&
Bill Wendling90a38682008-02-20 06:10:21 +0000106 RegInfo->isSubRegister(MOReg, Reg)))
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000107 return true;
Evan Cheng24a3cc42007-04-25 07:30:23 +0000108 }
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000109 }
110 return false;
111}
112
Bill Wendling90a38682008-02-20 06:10:21 +0000113/// ModifiesRegister - Returns true if the machine instruction modifies the
114/// register.
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000115bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
116 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000117 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000118 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Evan Cheng24a3cc42007-04-25 07:30:23 +0000119 return true;
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000120 }
121 return false;
Chris Lattner657b4d12005-08-24 00:09:33 +0000122}
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000123
Owen Anderson40a627d2008-01-15 22:58:11 +0000124void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
125 MachineBasicBlock *DefBlock,
Evan Cheng56184902007-05-08 19:00:00 +0000126 MachineBasicBlock *MBB,
127 std::vector<MachineBasicBlock*> &WorkList) {
Chris Lattner8ba97712004-07-01 04:29:47 +0000128 unsigned BBNum = MBB->getNumber();
Owen Anderson7047dd42008-01-15 22:02:46 +0000129
Chris Lattnerbc40e892003-01-13 20:01:16 +0000130 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendling90a38682008-02-20 06:10:21 +0000131 // remove it.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000132 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000133 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000134 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
135 break;
136 }
Owen Anderson7047dd42008-01-15 22:02:46 +0000137
Owen Anderson40a627d2008-01-15 22:58:11 +0000138 if (MBB == DefBlock) return; // Terminate recursion
Chris Lattnerbc40e892003-01-13 20:01:16 +0000139
Chris Lattnerbc40e892003-01-13 20:01:16 +0000140 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
Evan Cheng56184902007-05-08 19:00:00 +0000146 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
147 E = MBB->pred_rend(); PI != E; ++PI)
148 WorkList.push_back(*PI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000149}
150
Owen Anderson40a627d2008-01-15 22:58:11 +0000151void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
152 MachineBasicBlock *DefBlock,
Evan Cheng56184902007-05-08 19:00:00 +0000153 MachineBasicBlock *MBB) {
154 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson40a627d2008-01-15 22:58:11 +0000155 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Evan Cheng56184902007-05-08 19:00:00 +0000156 while (!WorkList.empty()) {
157 MachineBasicBlock *Pred = WorkList.back();
158 WorkList.pop_back();
Owen Anderson40a627d2008-01-15 22:58:11 +0000159 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Evan Cheng56184902007-05-08 19:00:00 +0000160 }
161}
162
163
Owen Anderson7047dd42008-01-15 22:02:46 +0000164void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Misha Brukman09ba9062004-06-24 21:31:16 +0000165 MachineInstr *MI) {
Owen Anderson7047dd42008-01-15 22:02:46 +0000166 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
167 assert(MRI.getVRegDef(reg) && "Register use before def!");
Alkis Evlogimenos2e58a412004-09-01 22:34:52 +0000168
Owen Andersona0185402007-11-08 01:20:48 +0000169 unsigned BBNum = MBB->getNumber();
170
Owen Anderson7047dd42008-01-15 22:02:46 +0000171 VarInfo& VRInfo = getVarInfo(reg);
Owen Andersona0185402007-11-08 01:20:48 +0000172 VRInfo.UsedBlocks[BBNum] = true;
Evan Cheng38b7ca62007-04-17 20:22:11 +0000173 VRInfo.NumUses++;
Evan Chengc6a24102007-03-17 09:29:54 +0000174
Bill Wendling90a38682008-02-20 06:10:21 +0000175 // Check to see if this basic block is already a kill block.
Chris Lattner74de8b12004-07-19 07:04:55 +0000176 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendling90a38682008-02-20 06:10:21 +0000177 // Yes, this register is killed in this basic block already. Increase the
Chris Lattnerbc40e892003-01-13 20:01:16 +0000178 // live range by updating the kill instruction.
Chris Lattner74de8b12004-07-19 07:04:55 +0000179 VRInfo.Kills.back() = MI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000180 return;
181 }
182
183#ifndef NDEBUG
184 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000185 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000186#endif
187
Owen Anderson7047dd42008-01-15 22:02:46 +0000188 assert(MBB != MRI.getVRegDef(reg)->getParent() &&
Chris Lattner73d4adf2004-07-19 06:26:50 +0000189 "Should have kill for defblock!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000190
Bill Wendling90a38682008-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 Andersona0185402007-11-08 01:20:48 +0000194 if (!VRInfo.AliveBlocks[BBNum])
Evan Chenge2ee9962007-03-09 09:48:56 +0000195 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000196
197 // Update all dominating blocks to mark them known live.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000198 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
199 E = MBB->pred_end(); PI != E; ++PI)
Owen Anderson40a627d2008-01-15 22:58:11 +0000200 MarkVirtRegAliveInBlock(VRInfo, MRI.getVRegDef(reg)->getParent(), *PI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000201}
202
203void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000204 // Turn previous partial def's into read/mod/write.
205 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
206 MachineInstr *Def = PhysRegPartDef[Reg][i];
207 // First one is just a def. This means the use is reading some undef bits.
208 if (i != 0)
Chris Lattner8019f412007-12-30 00:41:17 +0000209 Def->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
210 true/*IsImp*/,true/*IsKill*/));
211 Def->addOperand(MachineOperand::CreateReg(Reg,true/*IsDef*/,true/*IsImp*/));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000212 }
Bill Wendling90a38682008-02-20 06:10:21 +0000213
Evan Cheng24a3cc42007-04-25 07:30:23 +0000214 PhysRegPartDef[Reg].clear();
215
216 // There was an earlier def of a super-register. Add implicit def to that MI.
217 // A: EAX = ...
218 // B: = AX
219 // Add implicit def to A.
Evan Cheng6d6d3522007-09-11 22:34:47 +0000220 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
221 !PhysRegUsed[Reg]) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000222 MachineInstr *Def = PhysRegInfo[Reg];
223 if (!Def->findRegisterDefOperand(Reg))
Chris Lattner8019f412007-12-30 00:41:17 +0000224 Def->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
225 true/*IsImp*/));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000226 }
227
Evan Cheng6d6d3522007-09-11 22:34:47 +0000228 // There is a now a proper use, forget about the last partial use.
229 PhysRegPartUse[Reg] = NULL;
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000230 PhysRegInfo[Reg] = MI;
231 PhysRegUsed[Reg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000232
Evan Cheng24a3cc42007-04-25 07:30:23 +0000233 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
234 unsigned SubReg = *SubRegs; ++SubRegs) {
235 PhysRegInfo[SubReg] = MI;
236 PhysRegUsed[SubReg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000237 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000238
Evan Cheng24a3cc42007-04-25 07:30:23 +0000239 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
Evan Cheng21b3bf02007-08-01 20:18:21 +0000240 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
241 // Remember the partial use of this superreg if it was previously defined.
242 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
243 if (!HasPrevDef) {
244 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg);
245 unsigned SSReg = *SSRegs; ++SSRegs) {
246 if (PhysRegInfo[SSReg] != NULL) {
247 HasPrevDef = true;
248 break;
249 }
250 }
251 }
252 if (HasPrevDef) {
253 PhysRegInfo[SuperReg] = MI;
254 PhysRegPartUse[SuperReg] = MI;
255 }
256 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000257}
258
Evan Cheng4efe7412007-06-26 21:03:35 +0000259bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI,
260 SmallSet<unsigned, 4> &SubKills) {
261 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
262 unsigned SubReg = *SubRegs; ++SubRegs) {
263 MachineInstr *LastRef = PhysRegInfo[SubReg];
Evan Cheng0d8d3162007-09-12 23:02:04 +0000264 if (LastRef != RefMI ||
265 !HandlePhysRegKill(SubReg, RefMI, SubKills))
Evan Cheng4efe7412007-06-26 21:03:35 +0000266 SubKills.insert(SubReg);
267 }
268
269 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) {
270 // No sub-registers, just check if reg is killed by RefMI.
271 if (PhysRegInfo[Reg] == RefMI)
272 return true;
273 } else if (SubKills.empty())
274 // None of the sub-registers are killed elsewhere...
275 return true;
276 return false;
277}
278
279void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
280 SmallSet<unsigned, 4> &SubKills) {
281 if (SubKills.count(Reg) == 0)
Owen Andersonb487e722008-01-24 01:10:07 +0000282 MI->addRegisterKilled(Reg, RegInfo, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000283 else {
284 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
285 unsigned SubReg = *SubRegs; ++SubRegs)
286 addRegisterKills(SubReg, MI, SubKills);
287 }
288}
289
290bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
291 SmallSet<unsigned, 4> SubKills;
292 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
Owen Andersonb487e722008-01-24 01:10:07 +0000293 RefMI->addRegisterKilled(Reg, RegInfo, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000294 return true;
295 } else {
296 // Some sub-registers are killed by another MI.
297 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
298 unsigned SubReg = *SubRegs; ++SubRegs)
299 addRegisterKills(SubReg, RefMI, SubKills);
300 return false;
301 }
302}
303
Chris Lattnerbc40e892003-01-13 20:01:16 +0000304void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
305 // Does this kill a previous version of this register?
Evan Cheng24a3cc42007-04-25 07:30:23 +0000306 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
Evan Cheng4efe7412007-06-26 21:03:35 +0000307 if (PhysRegUsed[Reg]) {
308 if (!HandlePhysRegKill(Reg, LastRef)) {
309 if (PhysRegPartUse[Reg])
Owen Andersonb487e722008-01-24 01:10:07 +0000310 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000311 }
312 } else if (PhysRegPartUse[Reg])
Evan Cheng21b3bf02007-08-01 20:18:21 +0000313 // Add implicit use / kill to last partial use.
Owen Andersonb487e722008-01-24 01:10:07 +0000314 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
Evan Cheng5942efb2007-11-05 03:11:55 +0000315 else if (LastRef != MI)
316 // Defined, but not used. However, watch out for cases where a super-reg
317 // is also defined on the same MI.
Owen Andersonb487e722008-01-24 01:10:07 +0000318 LastRef->addRegisterDead(Reg, RegInfo);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000319 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000320
Evan Cheng24a3cc42007-04-25 07:30:23 +0000321 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
322 unsigned SubReg = *SubRegs; ++SubRegs) {
323 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
Evan Cheng4efe7412007-06-26 21:03:35 +0000324 if (PhysRegUsed[SubReg]) {
325 if (!HandlePhysRegKill(SubReg, LastRef)) {
326 if (PhysRegPartUse[SubReg])
Owen Andersonb487e722008-01-24 01:10:07 +0000327 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
Evan Cheng4efe7412007-06-26 21:03:35 +0000328 }
Evan Cheng4efe7412007-06-26 21:03:35 +0000329 } else if (PhysRegPartUse[SubReg])
Evan Cheng24a3cc42007-04-25 07:30:23 +0000330 // Add implicit use / kill to last use of a sub-register.
Owen Andersonb487e722008-01-24 01:10:07 +0000331 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
Evan Cheng6d6d3522007-09-11 22:34:47 +0000332 else if (LastRef != MI)
333 // This must be a def of the subreg on the same MI.
Owen Andersonb487e722008-01-24 01:10:07 +0000334 LastRef->addRegisterDead(SubReg, RegInfo);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000335 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000336 }
337
Evan Cheng4efe7412007-06-26 21:03:35 +0000338 if (MI) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000339 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
340 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Cheng6d6d3522007-09-11 22:34:47 +0000341 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
Evan Cheng24a3cc42007-04-25 07:30:23 +0000342 // The larger register is previously defined. Now a smaller part is
343 // being re-defined. Treat it as read/mod/write.
344 // EAX =
345 // AX = EAX<imp-use,kill>, EAX<imp-def>
Chris Lattner8019f412007-12-30 00:41:17 +0000346 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
347 true/*IsImp*/,true/*IsKill*/));
348 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
349 true/*IsImp*/));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000350 PhysRegInfo[SuperReg] = MI;
351 PhysRegUsed[SuperReg] = false;
Evan Cheng8b966d92007-05-14 20:39:18 +0000352 PhysRegPartUse[SuperReg] = NULL;
Evan Cheng24a3cc42007-04-25 07:30:23 +0000353 } else {
354 // Remember this partial def.
355 PhysRegPartDef[SuperReg].push_back(MI);
356 }
Evan Cheng4efe7412007-06-26 21:03:35 +0000357 }
358
359 PhysRegInfo[Reg] = MI;
360 PhysRegUsed[Reg] = false;
Evan Cheng21b3bf02007-08-01 20:18:21 +0000361 PhysRegPartDef[Reg].clear();
Evan Cheng4efe7412007-06-26 21:03:35 +0000362 PhysRegPartUse[Reg] = NULL;
363 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
364 unsigned SubReg = *SubRegs; ++SubRegs) {
365 PhysRegInfo[SubReg] = MI;
366 PhysRegUsed[SubReg] = false;
Evan Cheng21b3bf02007-08-01 20:18:21 +0000367 PhysRegPartDef[SubReg].clear();
Evan Cheng4efe7412007-06-26 21:03:35 +0000368 PhysRegPartUse[SubReg] = NULL;
369 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000370 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000371}
372
Evan Chengc6a24102007-03-17 09:29:54 +0000373bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
374 MF = &mf;
Evan Chengc6a24102007-03-17 09:29:54 +0000375 RegInfo = MF->getTarget().getRegisterInfo();
Owen Anderson40a627d2008-01-15 22:58:11 +0000376 MachineRegisterInfo& MRI = mf.getRegInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000377 assert(RegInfo && "Target doesn't have register information?");
378
Evan Chengc6a24102007-03-17 09:29:54 +0000379 ReservedRegisters = RegInfo->getReservedRegs(mf);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000380
Evan Chenge96f5012007-04-25 19:34:00 +0000381 unsigned NumRegs = RegInfo->getNumRegs();
382 PhysRegInfo = new MachineInstr*[NumRegs];
383 PhysRegUsed = new bool[NumRegs];
384 PhysRegPartUse = new MachineInstr*[NumRegs];
385 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
386 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
387 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
388 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
389 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000390
Chris Lattnerbc40e892003-01-13 20:01:16 +0000391 /// Get some space for a respectable number of registers...
392 VirtRegInfo.resize(64);
Chris Lattnerd493b342005-04-09 15:23:25 +0000393
Evan Chengc6a24102007-03-17 09:29:54 +0000394 analyzePHINodes(mf);
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000395
Chris Lattnerbc40e892003-01-13 20:01:16 +0000396 // Calculate live variable information in depth first order on the CFG of the
397 // function. This guarantees that we will see the definition of a virtual
398 // register before its uses due to dominance properties of SSA (except for PHI
399 // nodes, which are treated as a special case).
400 //
Evan Chengc6a24102007-03-17 09:29:54 +0000401 MachineBasicBlock *Entry = MF->begin();
Evan Cheng04104072007-06-27 05:23:00 +0000402 SmallPtrSet<MachineBasicBlock*,16> Visited;
403 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
404 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
405 DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000406 MachineBasicBlock *MBB = *DFI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000407
Evan Chengb371f452007-02-19 21:49:54 +0000408 // Mark live-in registers as live-in.
409 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000410 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000411 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000412 "Cannot have a live-in virtual register!");
413 HandlePhysRegDef(*II, 0);
414 }
415
Chris Lattnerbc40e892003-01-13 20:01:16 +0000416 // Loop over all of the instructions, processing them.
417 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000418 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000419 MachineInstr *MI = I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000420
421 // Process all of the operands of the instruction...
422 unsigned NumOperandsToProcess = MI->getNumOperands();
423
424 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
425 // of the uses. They will be handled in other basic blocks.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000426 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman09ba9062004-06-24 21:31:16 +0000427 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000428
Evan Cheng438f7bc2006-11-10 08:43:01 +0000429 // Process all uses...
Chris Lattnerbc40e892003-01-13 20:01:16 +0000430 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000431 const MachineOperand &MO = MI->getOperand(i);
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000432 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Bill Wendling90a38682008-02-20 06:10:21 +0000433 unsigned MOReg = MO.getReg();
434 if (TargetRegisterInfo::isVirtualRegister(MOReg))
435 HandleVirtRegUse(MOReg, MBB, MI);
436 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
437 !ReservedRegisters[MOReg])
438 HandlePhysRegUse(MOReg, MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000439 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000440 }
441
Evan Cheng438f7bc2006-11-10 08:43:01 +0000442 // Process all defs...
Chris Lattnerbc40e892003-01-13 20:01:16 +0000443 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendling90a38682008-02-20 06:10:21 +0000444 const MachineOperand &MO = MI->getOperand(i);
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000445 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Bill Wendling90a38682008-02-20 06:10:21 +0000446 unsigned MOReg = MO.getReg();
447 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
448 VarInfo &VRInfo = getVarInfo(MOReg);
449
Evan Chengbb4151b2008-02-05 20:04:18 +0000450 if (VRInfo.AliveBlocks.none())
451 // If vr is not alive in any block, then defaults to dead.
452 VRInfo.Kills.push_back(MI);
Bill Wendling90a38682008-02-20 06:10:21 +0000453 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
454 !ReservedRegisters[MOReg]) {
455 HandlePhysRegDef(MOReg, MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000456 }
457 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000458 }
459 }
460
461 // Handle any virtual assignments from PHI nodes which might be at the
462 // bottom of this basic block. We check all of our successor blocks to see
463 // if they have PHI nodes, and if so, we simulate an assignment at the end
464 // of the current block.
Evan Chenge96f5012007-04-25 19:34:00 +0000465 if (!PHIVarInfo[MBB->getNumber()].empty()) {
466 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
Misha Brukmanedf128a2005-04-21 22:36:52 +0000467
Evan Chenge96f5012007-04-25 19:34:00 +0000468 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000469 E = VarInfoVec.end(); I != E; ++I) {
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000470 // Only mark it alive only in the block we are representing.
Owen Anderson40a627d2008-01-15 22:58:11 +0000471 MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(),
472 MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000473 }
474 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000475
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000476 // Finally, if the last instruction in the block is a return, make sure to mark
Chris Lattnerd493b342005-04-09 15:23:25 +0000477 // it as using all of the live-out values in the function.
Chris Lattner749c6f62008-01-07 07:27:27 +0000478 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Chris Lattnerd493b342005-04-09 15:23:25 +0000479 MachineInstr *Ret = &MBB->back();
Chris Lattner84bc5422007-12-31 04:13:23 +0000480 for (MachineRegisterInfo::liveout_iterator
481 I = MF->getRegInfo().liveout_begin(),
482 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000483 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Chris Lattnerd493b342005-04-09 15:23:25 +0000484 "Cannot have a live-in virtual register!");
485 HandlePhysRegUse(*I, Ret);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000486 // Add live-out registers as implicit uses.
Evan Chengfaa51072007-04-26 19:00:32 +0000487 if (Ret->findRegisterUseOperandIdx(*I) == -1)
Chris Lattner8019f412007-12-30 00:41:17 +0000488 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Chris Lattnerd493b342005-04-09 15:23:25 +0000489 }
490 }
491
Chris Lattnerbc40e892003-01-13 20:01:16 +0000492 // Loop over PhysRegInfo, killing any registers that are available at the
493 // end of the basic block. This also resets the PhysRegInfo map.
Evan Chenge96f5012007-04-25 19:34:00 +0000494 for (unsigned i = 0; i != NumRegs; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000495 if (PhysRegInfo[i])
Misha Brukman09ba9062004-06-24 21:31:16 +0000496 HandlePhysRegDef(i, 0);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000497
498 // Clear some states between BB's. These are purely local information.
Evan Chengade31f92007-04-25 21:34:08 +0000499 for (unsigned i = 0; i != NumRegs; ++i)
Evan Cheng24a3cc42007-04-25 07:30:23 +0000500 PhysRegPartDef[i].clear();
Evan Cheng4efe7412007-06-26 21:03:35 +0000501 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
502 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
Evan Chenge96f5012007-04-25 19:34:00 +0000503 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000504 }
505
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000506 // Convert and transfer the dead / killed information we have gathered into
507 // VirtRegInfo onto MI's.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000508 //
Evan Chengf0e3bb12007-03-09 06:02:17 +0000509 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
510 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) {
Owen Anderson7047dd42008-01-15 22:02:46 +0000511 if (VirtRegInfo[i].Kills[j] == MRI.getVRegDef(i +
Dan Gohman6f0d0242008-02-10 18:45:23 +0000512 TargetRegisterInfo::FirstVirtualRegister))
Owen Andersonb487e722008-01-24 01:10:07 +0000513 VirtRegInfo[i].Kills[j]->addRegisterDead(i +
Dan Gohman6f0d0242008-02-10 18:45:23 +0000514 TargetRegisterInfo::FirstVirtualRegister,
Owen Andersonb487e722008-01-24 01:10:07 +0000515 RegInfo);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000516 else
Owen Andersonb487e722008-01-24 01:10:07 +0000517 VirtRegInfo[i].Kills[j]->addRegisterKilled(i +
Dan Gohman6f0d0242008-02-10 18:45:23 +0000518 TargetRegisterInfo::FirstVirtualRegister,
Owen Andersonb487e722008-01-24 01:10:07 +0000519 RegInfo);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000520 }
Chris Lattnera5287a62004-07-01 04:24:29 +0000521
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000522 // Check to make sure there are no unreachable blocks in the MC CFG for the
523 // function. If so, it is due to a bug in the instruction selector or some
524 // other part of the code generator if this happens.
525#ifndef NDEBUG
Evan Chengc6a24102007-03-17 09:29:54 +0000526 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000527 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
528#endif
529
Evan Chenge96f5012007-04-25 19:34:00 +0000530 delete[] PhysRegInfo;
531 delete[] PhysRegUsed;
532 delete[] PhysRegPartUse;
533 delete[] PhysRegPartDef;
534 delete[] PHIVarInfo;
535
Chris Lattnerbc40e892003-01-13 20:01:16 +0000536 return false;
537}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000538
539/// instructionChanged - When the address of an instruction changes, this
540/// method should be called so that live variables can update its internal
541/// data structures. This removes the records for OldMI, transfering them to
542/// the records for NewMI.
543void LiveVariables::instructionChanged(MachineInstr *OldMI,
544 MachineInstr *NewMI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000545 // If the instruction defines any virtual registers, update the VarInfo,
546 // kill and dead information for the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000547 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
548 MachineOperand &MO = OldMI->getOperand(i);
Chris Lattnerd45be362005-01-19 17:09:15 +0000549 if (MO.isRegister() && MO.getReg() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000550 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Chris Lattner5ed001b2004-02-19 18:28:02 +0000551 unsigned Reg = MO.getReg();
552 VarInfo &VI = getVarInfo(Reg);
Chris Lattnerd45be362005-01-19 17:09:15 +0000553 if (MO.isDef()) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000554 if (MO.isDead()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000555 MO.setIsDead(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000556 addVirtualRegisterDead(Reg, NewMI);
557 }
Chris Lattner2a6e1632005-01-19 17:11:51 +0000558 }
Dan Gohmanc674a922007-07-20 23:17:34 +0000559 if (MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000560 MO.setIsKill(false);
Dan Gohmanc674a922007-07-20 23:17:34 +0000561 addVirtualRegisterKilled(Reg, NewMI);
Chris Lattnerd45be362005-01-19 17:09:15 +0000562 }
Dan Gohmanc674a922007-07-20 23:17:34 +0000563 // If this is a kill of the value, update the VI kills list.
564 if (VI.removeKill(OldMI))
565 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Chris Lattner5ed001b2004-02-19 18:28:02 +0000566 }
567 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000568}
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000569
570/// removeVirtualRegistersKilled - Remove all killed info for the specified
571/// instruction.
572void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000573 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
574 MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000575 if (MO.isRegister() && MO.isKill()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000576 MO.setIsKill(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000577 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000578 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000579 bool removed = getVarInfo(Reg).removeKill(MI);
580 assert(removed && "kill not in register's VarInfo?");
581 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000582 }
583 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000584}
585
586/// removeVirtualRegistersDead - Remove all of the dead registers for the
587/// specified instruction from the live variable information.
588void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000589 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
590 MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000591 if (MO.isRegister() && MO.isDead()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000592 MO.setIsDead(false);
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000593 unsigned Reg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000594 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chenga6c4c1e2006-11-15 20:51:59 +0000595 bool removed = getVarInfo(Reg).removeKill(MI);
596 assert(removed && "kill not in register's VarInfo?");
597 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000598 }
599 }
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000600}
601
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000602/// analyzePHINodes - Gather information about the PHI nodes in here. In
603/// particular, we want to map the variable information of a virtual
604/// register which is used in a PHI node. We map that to the BB the vreg is
605/// coming from.
606///
607void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
608 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
609 I != E; ++I)
610 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
611 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
612 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendling90a38682008-02-20 06:10:21 +0000613 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
614 .push_back(BBI->getOperand(i).getReg());
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000615}