blob: 759a0b34124a8cb259a6920ca7c4843df3551592 [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"
Chris Lattnerbc40e892003-01-13 20:01:16 +000034#include "Support/DepthFirstIterator.h"
Chris Lattner5ed001b2004-02-19 18:28:02 +000035#include "Support/STLExtras.h"
Chris Lattner49a5aaa2004-01-30 22:08:53 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattnerbc40e892003-01-13 20:01:16 +000038static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
39
Chris Lattner49a5aaa2004-01-30 22:08:53 +000040/// getIndexMachineBasicBlock() - Given a block index, return the
41/// MachineBasicBlock corresponding to it.
42MachineBasicBlock *LiveVariables::getIndexMachineBasicBlock(unsigned Idx) {
43 if (BBIdxMap.empty()) {
44 BBIdxMap.resize(BBMap.size());
Chris Lattnerf25fb4b2004-05-01 21:24:24 +000045 for (std::map<MachineBasicBlock*, unsigned>::iterator I = BBMap.begin(),
46 E = BBMap.end(); I != E; ++I) {
47 assert(BBIdxMap.size() > I->second && "Indices are not sequential");
48 assert(BBIdxMap[I->second] == 0 && "Multiple idx collision!");
49 BBIdxMap[I->second] = I->first;
Chris Lattner49a5aaa2004-01-30 22:08:53 +000050 }
51 }
52 assert(Idx < BBIdxMap.size() && "BB Index out of range!");
53 return BBIdxMap[Idx];
54}
Chris Lattnerfb2cb692003-05-12 14:24:00 +000055
56LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Chris Lattneref09c632004-01-31 21:27:19 +000057 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000058 "getVarInfo: not a virtual register!");
59 RegIdx -= MRegisterInfo::FirstVirtualRegister;
60 if (RegIdx >= VirtRegInfo.size()) {
61 if (RegIdx >= 2*VirtRegInfo.size())
62 VirtRegInfo.resize(RegIdx*2);
63 else
64 VirtRegInfo.resize(2*VirtRegInfo.size());
65 }
66 return VirtRegInfo[RegIdx];
67}
68
69
70
Chris Lattnerbc40e892003-01-13 20:01:16 +000071void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Chris Lattnerf25fb4b2004-05-01 21:24:24 +000072 MachineBasicBlock *MBB) {
73 unsigned BBNum = getMachineBasicBlockIndex(MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +000074
75 // Check to see if this basic block is one of the killing blocks. If so,
76 // remove it...
77 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
78 if (VRInfo.Kills[i].first == MBB) {
79 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
80 break;
81 }
82
83 if (MBB == VRInfo.DefBlock) return; // Terminate recursion
84
85 if (VRInfo.AliveBlocks.size() <= BBNum)
86 VRInfo.AliveBlocks.resize(BBNum+1); // Make space...
87
88 if (VRInfo.AliveBlocks[BBNum])
89 return; // We already know the block is live
90
91 // Mark the variable known alive in this bb
92 VRInfo.AliveBlocks[BBNum] = true;
93
Chris Lattnerf25fb4b2004-05-01 21:24:24 +000094 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
95 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +000096 MarkVirtRegAliveInBlock(VRInfo, *PI);
97}
98
99void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
100 MachineInstr *MI) {
101 // Check to see if this basic block is already a kill block...
102 if (!VRInfo.Kills.empty() && VRInfo.Kills.back().first == MBB) {
103 // Yes, this register is killed in this basic block already. Increase the
104 // live range by updating the kill instruction.
105 VRInfo.Kills.back().second = MI;
106 return;
107 }
108
109#ifndef NDEBUG
110 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
111 assert(VRInfo.Kills[i].first != MBB && "entry should be at end!");
112#endif
113
114 assert(MBB != VRInfo.DefBlock && "Should have kill for defblock!");
115
116 // Add a new kill entry for this basic block.
117 VRInfo.Kills.push_back(std::make_pair(MBB, MI));
118
119 // Update all dominating blocks to mark them known live.
120 const BasicBlock *BB = MBB->getBasicBlock();
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000121 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
122 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000123 MarkVirtRegAliveInBlock(VRInfo, *PI);
124}
125
126void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000127 PhysRegInfo[Reg] = MI;
128 PhysRegUsed[Reg] = true;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000129}
130
131void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
132 // Does this kill a previous version of this register?
133 if (MachineInstr *LastUse = PhysRegInfo[Reg]) {
134 if (PhysRegUsed[Reg])
135 RegistersKilled.insert(std::make_pair(LastUse, Reg));
136 else
137 RegistersDead.insert(std::make_pair(LastUse, Reg));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000138 }
139 PhysRegInfo[Reg] = MI;
140 PhysRegUsed[Reg] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000141
142 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
143 *AliasSet; ++AliasSet) {
Chris Lattner49948772004-02-09 01:43:23 +0000144 unsigned Alias = *AliasSet;
145 if (MachineInstr *LastUse = PhysRegInfo[Alias]) {
146 if (PhysRegUsed[Alias])
147 RegistersKilled.insert(std::make_pair(LastUse, Alias));
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000148 else
Chris Lattner49948772004-02-09 01:43:23 +0000149 RegistersDead.insert(std::make_pair(LastUse, Alias));
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000150 }
Chris Lattner49948772004-02-09 01:43:23 +0000151 PhysRegInfo[Alias] = MI;
152 PhysRegUsed[Alias] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000153 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000154}
155
156bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner96aef892004-02-09 01:35:21 +0000157 const TargetInstrInfo &TII = MF.getTarget().getInstrInfo();
158 RegInfo = MF.getTarget().getRegisterInfo();
159 assert(RegInfo && "Target doesn't have register information?");
160
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000161 // First time though, initialize AllocatablePhysicalRegisters for the target
162 if (AllocatablePhysicalRegisters.empty()) {
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000163 // Make space, initializing to false...
Chris Lattner96aef892004-02-09 01:35:21 +0000164 AllocatablePhysicalRegisters.resize(RegInfo->getNumRegs());
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000165
166 // Loop over all of the register classes...
Chris Lattner96aef892004-02-09 01:35:21 +0000167 for (MRegisterInfo::regclass_iterator RCI = RegInfo->regclass_begin(),
168 E = RegInfo->regclass_end(); RCI != E; ++RCI)
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000169 // Loop over all of the allocatable registers in the function...
170 for (TargetRegisterClass::iterator I = (*RCI)->allocation_order_begin(MF),
171 E = (*RCI)->allocation_order_end(MF); I != E; ++I)
172 AllocatablePhysicalRegisters[*I] = true; // The reg is allocatable!
173 }
174
Chris Lattnerbc40e892003-01-13 20:01:16 +0000175 // Build BBMap...
176 unsigned BBNum = 0;
177 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000178 BBMap[I] = BBNum++;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000179
180 // PhysRegInfo - Keep track of which instruction was the last use of a
181 // physical register. This is a purely local property, because all physical
182 // register references as presumed dead across basic blocks.
183 //
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000184 MachineInstr *PhysRegInfoA[RegInfo->getNumRegs()];
185 bool PhysRegUsedA[RegInfo->getNumRegs()];
186 std::fill(PhysRegInfoA, PhysRegInfoA+RegInfo->getNumRegs(), (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000187 PhysRegInfo = PhysRegInfoA;
188 PhysRegUsed = PhysRegUsedA;
189
Chris Lattnerbc40e892003-01-13 20:01:16 +0000190 /// Get some space for a respectable number of registers...
191 VirtRegInfo.resize(64);
192
193 // Calculate live variable information in depth first order on the CFG of the
194 // function. This guarantees that we will see the definition of a virtual
195 // register before its uses due to dominance properties of SSA (except for PHI
196 // nodes, which are treated as a special case).
197 //
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000198 MachineBasicBlock *Entry = MF.begin();
199 for (df_iterator<MachineBasicBlock*> DFI = df_begin(Entry), E = df_end(Entry);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000200 DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000201 MachineBasicBlock *MBB = *DFI;
202 unsigned BBNum = getMachineBasicBlockIndex(MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000203
204 // Loop over all of the instructions, processing them.
205 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
206 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000207 MachineInstr *MI = I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000208 const TargetInstrDescriptor &MID = TII.get(MI->getOpcode());
209
210 // Process all of the operands of the instruction...
211 unsigned NumOperandsToProcess = MI->getNumOperands();
212
213 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
214 // of the uses. They will be handled in other basic blocks.
215 if (MI->getOpcode() == TargetInstrInfo::PHI)
216 NumOperandsToProcess = 1;
217
218 // Loop over implicit uses, using them.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000219 for (const unsigned *ImplicitUses = MID.ImplicitUses;
220 *ImplicitUses; ++ImplicitUses)
221 HandlePhysRegUse(*ImplicitUses, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000222
223 // Process all explicit uses...
224 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
225 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000226 if (MO.isUse() && MO.isRegister() && MO.getReg()) {
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000227 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000228 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000229 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000230 AllocatablePhysicalRegisters[MO.getReg()]) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000231 HandlePhysRegUse(MO.getReg(), MI);
232 }
233 }
234 }
235
236 // Loop over implicit defs, defining them.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000237 for (const unsigned *ImplicitDefs = MID.ImplicitDefs;
238 *ImplicitDefs; ++ImplicitDefs)
239 HandlePhysRegDef(*ImplicitDefs, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000240
241 // Process all explicit defs...
242 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
243 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000244 if (MO.isDef() && MO.isRegister() && MO.getReg()) {
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000245 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000246 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000247
248 assert(VRInfo.DefBlock == 0 && "Variable multiply defined!");
249 VRInfo.DefBlock = MBB; // Created here...
250 VRInfo.DefInst = MI;
251 VRInfo.Kills.push_back(std::make_pair(MBB, MI)); // Defaults to dead
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000252 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000253 AllocatablePhysicalRegisters[MO.getReg()]) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000254 HandlePhysRegDef(MO.getReg(), MI);
255 }
256 }
257 }
258 }
259
260 // Handle any virtual assignments from PHI nodes which might be at the
261 // bottom of this basic block. We check all of our successor blocks to see
262 // if they have PHI nodes, and if so, we simulate an assignment at the end
263 // of the current block.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000264 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
265 E = MBB->succ_end(); SI != E; ++SI) {
266 MachineBasicBlock *Succ = *SI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000267
268 // PHI nodes are guaranteed to be at the top of the block...
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000269 for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end();
270 MI != ME && MI->getOpcode() == TargetInstrInfo::PHI; ++MI) {
Chris Lattner92bc3bc2004-02-29 22:01:51 +0000271 for (unsigned i = 1; ; i += 2) {
272 assert(MI->getNumOperands() > i+1 &&
273 "Didn't find an entry for our predecessor??");
Chris Lattnerf98358e2003-05-01 21:18:47 +0000274 if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) {
275 MachineOperand &MO = MI->getOperand(i);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000276 if (!MO.getVRegValueOrNull()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000277 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000278
279 // Only mark it alive only in the block we are representing...
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000280 MarkVirtRegAliveInBlock(VRInfo, MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000281 break; // Found the PHI entry for this block...
282 }
283 }
Chris Lattner92bc3bc2004-02-29 22:01:51 +0000284 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000285 }
286 }
287
288 // Loop over PhysRegInfo, killing any registers that are available at the
289 // end of the basic block. This also resets the PhysRegInfo map.
Chris Lattner96aef892004-02-09 01:35:21 +0000290 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000291 if (PhysRegInfo[i])
292 HandlePhysRegDef(i, 0);
293 }
294
Chris Lattnerbc40e892003-01-13 20:01:16 +0000295 // Convert the information we have gathered into VirtRegInfo and transform it
296 // into a form usable by RegistersKilled.
297 //
298 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
299 for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
300 if (VirtRegInfo[i].Kills[j].second == VirtRegInfo[i].DefInst)
301 RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
302 i + MRegisterInfo::FirstVirtualRegister));
303
304 else
305 RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
306 i + MRegisterInfo::FirstVirtualRegister));
307 }
308
309 return false;
310}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000311
312/// instructionChanged - When the address of an instruction changes, this
313/// method should be called so that live variables can update its internal
314/// data structures. This removes the records for OldMI, transfering them to
315/// the records for NewMI.
316void LiveVariables::instructionChanged(MachineInstr *OldMI,
317 MachineInstr *NewMI) {
318 // If the instruction defines any virtual registers, update the VarInfo for
319 // the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000320 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
321 MachineOperand &MO = OldMI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000322 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Chris Lattner5ed001b2004-02-19 18:28:02 +0000323 MRegisterInfo::isVirtualRegister(MO.getReg())) {
324 unsigned Reg = MO.getReg();
325 VarInfo &VI = getVarInfo(Reg);
326 if (VI.DefInst == OldMI)
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000327 VI.DefInst = NewMI;
Chris Lattner5ed001b2004-02-19 18:28:02 +0000328 }
329 }
330
331 // Move the killed information over...
332 killed_iterator I, E;
333 tie(I, E) = killed_range(OldMI);
Chris Lattnera96478d2004-02-19 18:32:29 +0000334 std::vector<unsigned> Regs;
Chris Lattner5ed001b2004-02-19 18:28:02 +0000335 for (killed_iterator A = I; A != E; ++A)
Chris Lattnera96478d2004-02-19 18:32:29 +0000336 Regs.push_back(A->second);
Chris Lattner5ed001b2004-02-19 18:28:02 +0000337 RegistersKilled.erase(I, E);
338
Chris Lattnera96478d2004-02-19 18:32:29 +0000339 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
340 RegistersKilled.insert(std::make_pair(NewMI, Regs[i]));
341 Regs.clear();
342
343
Chris Lattner5ed001b2004-02-19 18:28:02 +0000344 // Move the dead information over...
345 tie(I, E) = dead_range(OldMI);
346 for (killed_iterator A = I; A != E; ++A)
Chris Lattnera96478d2004-02-19 18:32:29 +0000347 Regs.push_back(A->second);
Chris Lattner5ed001b2004-02-19 18:28:02 +0000348 RegistersDead.erase(I, E);
Chris Lattnera96478d2004-02-19 18:32:29 +0000349
350 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
351 RegistersDead.insert(std::make_pair(NewMI, Regs[i]));
Chris Lattner5ed001b2004-02-19 18:28:02 +0000352}