blob: d4d71dc472f623a26c5a84583afbfd3436b458cb [file] [log] [blame]
Chris Lattnerbc40e892003-01-13 20:01:16 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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 Lattner61b08f12004-02-10 21:18:55 +000031#include "llvm/Target/MRegisterInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000032#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000033#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/ADT/DepthFirstIterator.h"
35#include "llvm/ADT/STLExtras.h"
Chris Lattner6fcd8d82004-10-25 18:44:14 +000036#include "llvm/Config/alloca.h"
Chris Lattner49a5aaa2004-01-30 22:08:53 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattnerbc40e892003-01-13 20:01:16 +000039static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
40
Chris Lattnerfb2cb692003-05-12 14:24:00 +000041LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Chris Lattneref09c632004-01-31 21:27:19 +000042 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000043 "getVarInfo: not a virtual register!");
44 RegIdx -= MRegisterInfo::FirstVirtualRegister;
45 if (RegIdx >= VirtRegInfo.size()) {
46 if (RegIdx >= 2*VirtRegInfo.size())
47 VirtRegInfo.resize(RegIdx*2);
48 else
49 VirtRegInfo.resize(2*VirtRegInfo.size());
50 }
51 return VirtRegInfo[RegIdx];
52}
53
54
55
Chris Lattnerbc40e892003-01-13 20:01:16 +000056void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Misha Brukman09ba9062004-06-24 21:31:16 +000057 MachineBasicBlock *MBB) {
Chris Lattner8ba97712004-07-01 04:29:47 +000058 unsigned BBNum = MBB->getNumber();
Chris Lattnerbc40e892003-01-13 20:01:16 +000059
60 // Check to see if this basic block is one of the killing blocks. If so,
61 // remove it...
62 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +000063 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000064 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
65 break;
66 }
67
Chris Lattner73d4adf2004-07-19 06:26:50 +000068 if (MBB == VRInfo.DefInst->getParent()) return; // Terminate recursion
Chris Lattnerbc40e892003-01-13 20:01:16 +000069
70 if (VRInfo.AliveBlocks.size() <= BBNum)
71 VRInfo.AliveBlocks.resize(BBNum+1); // Make space...
72
73 if (VRInfo.AliveBlocks[BBNum])
74 return; // We already know the block is live
75
76 // Mark the variable known alive in this bb
77 VRInfo.AliveBlocks[BBNum] = true;
78
Chris Lattnerf25fb4b2004-05-01 21:24:24 +000079 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
80 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +000081 MarkVirtRegAliveInBlock(VRInfo, *PI);
82}
83
84void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
Misha Brukman09ba9062004-06-24 21:31:16 +000085 MachineInstr *MI) {
Alkis Evlogimenos2e58a412004-09-01 22:34:52 +000086 assert(VRInfo.DefInst && "Register use before def!");
87
Chris Lattnerbc40e892003-01-13 20:01:16 +000088 // Check to see if this basic block is already a kill block...
Chris Lattner74de8b12004-07-19 07:04:55 +000089 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000090 // Yes, this register is killed in this basic block already. Increase the
91 // live range by updating the kill instruction.
Chris Lattner74de8b12004-07-19 07:04:55 +000092 VRInfo.Kills.back() = MI;
Chris Lattnerbc40e892003-01-13 20:01:16 +000093 return;
94 }
95
96#ifndef NDEBUG
97 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +000098 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnerbc40e892003-01-13 20:01:16 +000099#endif
100
Chris Lattner73d4adf2004-07-19 06:26:50 +0000101 assert(MBB != VRInfo.DefInst->getParent() &&
102 "Should have kill for defblock!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000103
104 // Add a new kill entry for this basic block.
Chris Lattner74de8b12004-07-19 07:04:55 +0000105 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000106
107 // Update all dominating blocks to mark them known live.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000108 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
109 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000110 MarkVirtRegAliveInBlock(VRInfo, *PI);
111}
112
113void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000114 PhysRegInfo[Reg] = MI;
115 PhysRegUsed[Reg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000116
117 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
118 unsigned Alias = *AliasSet; ++AliasSet) {
119 PhysRegInfo[Alias] = MI;
120 PhysRegUsed[Alias] = true;
121 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000122}
123
124void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
125 // Does this kill a previous version of this register?
126 if (MachineInstr *LastUse = PhysRegInfo[Reg]) {
127 if (PhysRegUsed[Reg])
128 RegistersKilled.insert(std::make_pair(LastUse, Reg));
129 else
130 RegistersDead.insert(std::make_pair(LastUse, Reg));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000131 }
132 PhysRegInfo[Reg] = MI;
133 PhysRegUsed[Reg] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000134
135 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
Chris Lattner6d3848d2004-05-10 05:12:43 +0000136 unsigned Alias = *AliasSet; ++AliasSet) {
Chris Lattner49948772004-02-09 01:43:23 +0000137 if (MachineInstr *LastUse = PhysRegInfo[Alias]) {
138 if (PhysRegUsed[Alias])
Chris Lattner6d3848d2004-05-10 05:12:43 +0000139 RegistersKilled.insert(std::make_pair(LastUse, Alias));
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000140 else
Misha Brukman09ba9062004-06-24 21:31:16 +0000141 RegistersDead.insert(std::make_pair(LastUse, Alias));
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000142 }
Chris Lattner49948772004-02-09 01:43:23 +0000143 PhysRegInfo[Alias] = MI;
144 PhysRegUsed[Alias] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000145 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000146}
147
148bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000149 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000150 RegInfo = MF.getTarget().getRegisterInfo();
151 assert(RegInfo && "Target doesn't have register information?");
152
Alkis Evlogimenos22a2f6d2004-08-26 22:23:32 +0000153 AllocatablePhysicalRegisters = RegInfo->getAllocatableSet(MF);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000154
Chris Lattnerbc40e892003-01-13 20:01:16 +0000155 // PhysRegInfo - Keep track of which instruction was the last use of a
156 // physical register. This is a purely local property, because all physical
157 // register references as presumed dead across basic blocks.
158 //
Chris Lattner6fcd8d82004-10-25 18:44:14 +0000159 PhysRegInfo = (MachineInstr**)alloca(sizeof(MachineInstr*) *
160 RegInfo->getNumRegs());
161 PhysRegUsed = (bool*)alloca(sizeof(bool)*RegInfo->getNumRegs());
162 std::fill(PhysRegInfo, PhysRegInfo+RegInfo->getNumRegs(), (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000163
Chris Lattnerbc40e892003-01-13 20:01:16 +0000164 /// Get some space for a respectable number of registers...
165 VirtRegInfo.resize(64);
166
167 // Calculate live variable information in depth first order on the CFG of the
168 // function. This guarantees that we will see the definition of a virtual
169 // register before its uses due to dominance properties of SSA (except for PHI
170 // nodes, which are treated as a special case).
171 //
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000172 MachineBasicBlock *Entry = MF.begin();
Chris Lattnera5287a62004-07-01 04:24:29 +0000173 std::set<MachineBasicBlock*> Visited;
174 for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited),
175 E = df_ext_end(Entry, Visited); DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000176 MachineBasicBlock *MBB = *DFI;
Chris Lattner8ba97712004-07-01 04:29:47 +0000177 unsigned BBNum = MBB->getNumber();
Chris Lattnerbc40e892003-01-13 20:01:16 +0000178
179 // Loop over all of the instructions, processing them.
180 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000181 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000182 MachineInstr *MI = I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000183 const TargetInstrDescriptor &MID = TII.get(MI->getOpcode());
184
185 // Process all of the operands of the instruction...
186 unsigned NumOperandsToProcess = MI->getNumOperands();
187
188 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
189 // of the uses. They will be handled in other basic blocks.
190 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman09ba9062004-06-24 21:31:16 +0000191 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000192
193 // Loop over implicit uses, using them.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000194 for (const unsigned *ImplicitUses = MID.ImplicitUses;
195 *ImplicitUses; ++ImplicitUses)
Misha Brukman09ba9062004-06-24 21:31:16 +0000196 HandlePhysRegUse(*ImplicitUses, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000197
198 // Process all explicit uses...
199 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000200 MachineOperand &MO = MI->getOperand(i);
201 if (MO.isUse() && MO.isRegister() && MO.getReg()) {
202 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
203 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
204 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000205 AllocatablePhysicalRegisters[MO.getReg()]) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000206 HandlePhysRegUse(MO.getReg(), MI);
207 }
208 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000209 }
210
211 // Loop over implicit defs, defining them.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000212 for (const unsigned *ImplicitDefs = MID.ImplicitDefs;
213 *ImplicitDefs; ++ImplicitDefs)
214 HandlePhysRegDef(*ImplicitDefs, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000215
216 // Process all explicit defs...
217 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000218 MachineOperand &MO = MI->getOperand(i);
219 if (MO.isDef() && MO.isRegister() && MO.getReg()) {
220 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
221 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000222
Chris Lattner73d4adf2004-07-19 06:26:50 +0000223 assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
Misha Brukman09ba9062004-06-24 21:31:16 +0000224 VRInfo.DefInst = MI;
Chris Lattner472405e2004-07-19 06:55:21 +0000225 // Defaults to dead
Chris Lattner74de8b12004-07-19 07:04:55 +0000226 VRInfo.Kills.push_back(MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000227 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000228 AllocatablePhysicalRegisters[MO.getReg()]) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000229 HandlePhysRegDef(MO.getReg(), MI);
230 }
231 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000232 }
233 }
234
235 // Handle any virtual assignments from PHI nodes which might be at the
236 // bottom of this basic block. We check all of our successor blocks to see
237 // if they have PHI nodes, and if so, we simulate an assignment at the end
238 // of the current block.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000239 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
240 E = MBB->succ_end(); SI != E; ++SI) {
241 MachineBasicBlock *Succ = *SI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000242
243 // PHI nodes are guaranteed to be at the top of the block...
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000244 for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000245 MI != ME && MI->getOpcode() == TargetInstrInfo::PHI; ++MI) {
246 for (unsigned i = 1; ; i += 2) {
Chris Lattner92bc3bc2004-02-29 22:01:51 +0000247 assert(MI->getNumOperands() > i+1 &&
248 "Didn't find an entry for our predecessor??");
Misha Brukman09ba9062004-06-24 21:31:16 +0000249 if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) {
250 MachineOperand &MO = MI->getOperand(i);
251 if (!MO.getVRegValueOrNull()) {
252 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000253
Misha Brukman09ba9062004-06-24 21:31:16 +0000254 // Only mark it alive only in the block we are representing...
255 MarkVirtRegAliveInBlock(VRInfo, MBB);
256 break; // Found the PHI entry for this block...
257 }
258 }
Chris Lattner92bc3bc2004-02-29 22:01:51 +0000259 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000260 }
261 }
262
263 // Loop over PhysRegInfo, killing any registers that are available at the
264 // end of the basic block. This also resets the PhysRegInfo map.
Chris Lattner96aef892004-02-09 01:35:21 +0000265 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000266 if (PhysRegInfo[i])
Misha Brukman09ba9062004-06-24 21:31:16 +0000267 HandlePhysRegDef(i, 0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000268 }
269
Chris Lattnerbc40e892003-01-13 20:01:16 +0000270 // Convert the information we have gathered into VirtRegInfo and transform it
271 // into a form usable by RegistersKilled.
272 //
273 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
274 for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
Chris Lattner74de8b12004-07-19 07:04:55 +0000275 if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
276 RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j],
Misha Brukman09ba9062004-06-24 21:31:16 +0000277 i + MRegisterInfo::FirstVirtualRegister));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000278
279 else
Chris Lattner74de8b12004-07-19 07:04:55 +0000280 RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j],
Misha Brukman09ba9062004-06-24 21:31:16 +0000281 i + MRegisterInfo::FirstVirtualRegister));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000282 }
Chris Lattnera5287a62004-07-01 04:24:29 +0000283
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000284 // Check to make sure there are no unreachable blocks in the MC CFG for the
285 // function. If so, it is due to a bug in the instruction selector or some
286 // other part of the code generator if this happens.
287#ifndef NDEBUG
288 for(MachineFunction::iterator i = MF.begin(), e = MF.end(); i != e; ++i)
289 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
290#endif
291
Chris Lattnerbc40e892003-01-13 20:01:16 +0000292 return false;
293}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000294
295/// instructionChanged - When the address of an instruction changes, this
296/// method should be called so that live variables can update its internal
297/// data structures. This removes the records for OldMI, transfering them to
298/// the records for NewMI.
299void LiveVariables::instructionChanged(MachineInstr *OldMI,
300 MachineInstr *NewMI) {
301 // If the instruction defines any virtual registers, update the VarInfo for
302 // the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000303 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
304 MachineOperand &MO = OldMI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000305 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Chris Lattner5ed001b2004-02-19 18:28:02 +0000306 MRegisterInfo::isVirtualRegister(MO.getReg())) {
307 unsigned Reg = MO.getReg();
308 VarInfo &VI = getVarInfo(Reg);
309 if (VI.DefInst == OldMI)
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000310 VI.DefInst = NewMI;
Chris Lattner5ed001b2004-02-19 18:28:02 +0000311 }
312 }
313
314 // Move the killed information over...
315 killed_iterator I, E;
316 tie(I, E) = killed_range(OldMI);
Chris Lattnera96478d2004-02-19 18:32:29 +0000317 std::vector<unsigned> Regs;
Chris Lattner5ed001b2004-02-19 18:28:02 +0000318 for (killed_iterator A = I; A != E; ++A)
Chris Lattnera96478d2004-02-19 18:32:29 +0000319 Regs.push_back(A->second);
Chris Lattner5ed001b2004-02-19 18:28:02 +0000320 RegistersKilled.erase(I, E);
321
Chris Lattnera96478d2004-02-19 18:32:29 +0000322 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
323 RegistersKilled.insert(std::make_pair(NewMI, Regs[i]));
324 Regs.clear();
325
Chris Lattner5ed001b2004-02-19 18:28:02 +0000326 // Move the dead information over...
327 tie(I, E) = dead_range(OldMI);
328 for (killed_iterator A = I; A != E; ++A)
Chris Lattnera96478d2004-02-19 18:32:29 +0000329 Regs.push_back(A->second);
Chris Lattner5ed001b2004-02-19 18:28:02 +0000330 RegistersDead.erase(I, E);
Chris Lattnera96478d2004-02-19 18:32:29 +0000331
332 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
333 RegistersDead.insert(std::make_pair(NewMI, Regs[i]));
Chris Lattner5ed001b2004-02-19 18:28:02 +0000334}