blob: a8627396cccd23a4707185156f3d77bbac759e69 [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 Lattner3501fea2003-01-14 22:00:31 +000031#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/CFG.h"
34#include "Support/DepthFirstIterator.h"
Chris Lattner49a5aaa2004-01-30 22:08:53 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattnerbc40e892003-01-13 20:01:16 +000037static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
38
Chris Lattnerfb2cb692003-05-12 14:24:00 +000039const std::pair<MachineBasicBlock*, unsigned> &
40LiveVariables::getMachineBasicBlockInfo(MachineBasicBlock *MBB) const{
41 return BBMap.find(MBB->getBasicBlock())->second;
42}
Chris Lattner49a5aaa2004-01-30 22:08:53 +000043
44/// getIndexMachineBasicBlock() - Given a block index, return the
45/// MachineBasicBlock corresponding to it.
46MachineBasicBlock *LiveVariables::getIndexMachineBasicBlock(unsigned Idx) {
47 if (BBIdxMap.empty()) {
48 BBIdxMap.resize(BBMap.size());
49 for (std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> >
50 ::iterator I = BBMap.begin(), E = BBMap.end(); I != E; ++I) {
51 assert(BBIdxMap.size() > I->second.second &&"Indices are not sequential");
52 assert(BBIdxMap[I->second.second] == 0 && "Multiple idx collision!");
53 BBIdxMap[I->second.second] = I->second.first;
54 }
55 }
56 assert(Idx < BBIdxMap.size() && "BB Index out of range!");
57 return BBIdxMap[Idx];
58}
Chris Lattnerfb2cb692003-05-12 14:24:00 +000059
60LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Chris Lattneref09c632004-01-31 21:27:19 +000061 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000062 "getVarInfo: not a virtual register!");
63 RegIdx -= MRegisterInfo::FirstVirtualRegister;
64 if (RegIdx >= VirtRegInfo.size()) {
65 if (RegIdx >= 2*VirtRegInfo.size())
66 VirtRegInfo.resize(RegIdx*2);
67 else
68 VirtRegInfo.resize(2*VirtRegInfo.size());
69 }
70 return VirtRegInfo[RegIdx];
71}
72
73
74
Chris Lattnerbc40e892003-01-13 20:01:16 +000075void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
76 const BasicBlock *BB) {
77 const std::pair<MachineBasicBlock*,unsigned> &Info = BBMap.find(BB)->second;
78 MachineBasicBlock *MBB = Info.first;
79 unsigned BBNum = Info.second;
80
81 // Check to see if this basic block is one of the killing blocks. If so,
82 // remove it...
83 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
84 if (VRInfo.Kills[i].first == MBB) {
85 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
86 break;
87 }
88
89 if (MBB == VRInfo.DefBlock) return; // Terminate recursion
90
91 if (VRInfo.AliveBlocks.size() <= BBNum)
92 VRInfo.AliveBlocks.resize(BBNum+1); // Make space...
93
94 if (VRInfo.AliveBlocks[BBNum])
95 return; // We already know the block is live
96
97 // Mark the variable known alive in this bb
98 VRInfo.AliveBlocks[BBNum] = true;
99
100 for (pred_const_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
101 MarkVirtRegAliveInBlock(VRInfo, *PI);
102}
103
104void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
105 MachineInstr *MI) {
106 // Check to see if this basic block is already a kill block...
107 if (!VRInfo.Kills.empty() && VRInfo.Kills.back().first == MBB) {
108 // Yes, this register is killed in this basic block already. Increase the
109 // live range by updating the kill instruction.
110 VRInfo.Kills.back().second = MI;
111 return;
112 }
113
114#ifndef NDEBUG
115 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
116 assert(VRInfo.Kills[i].first != MBB && "entry should be at end!");
117#endif
118
119 assert(MBB != VRInfo.DefBlock && "Should have kill for defblock!");
120
121 // Add a new kill entry for this basic block.
122 VRInfo.Kills.push_back(std::make_pair(MBB, MI));
123
124 // Update all dominating blocks to mark them known live.
125 const BasicBlock *BB = MBB->getBasicBlock();
126 for (pred_const_iterator PI = pred_begin(BB), E = pred_end(BB);
127 PI != E; ++PI)
128 MarkVirtRegAliveInBlock(VRInfo, *PI);
129}
130
131void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000132 PhysRegInfo[Reg] = MI;
133 PhysRegUsed[Reg] = true;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000134}
135
136void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
137 // Does this kill a previous version of this register?
138 if (MachineInstr *LastUse = PhysRegInfo[Reg]) {
139 if (PhysRegUsed[Reg])
140 RegistersKilled.insert(std::make_pair(LastUse, Reg));
141 else
142 RegistersDead.insert(std::make_pair(LastUse, Reg));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000143 }
144 PhysRegInfo[Reg] = MI;
145 PhysRegUsed[Reg] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000146
147 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
148 *AliasSet; ++AliasSet) {
Chris Lattner49948772004-02-09 01:43:23 +0000149 unsigned Alias = *AliasSet;
150 if (MachineInstr *LastUse = PhysRegInfo[Alias]) {
151 if (PhysRegUsed[Alias])
152 RegistersKilled.insert(std::make_pair(LastUse, Alias));
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000153 else
Chris Lattner49948772004-02-09 01:43:23 +0000154 RegistersDead.insert(std::make_pair(LastUse, Alias));
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000155 }
Chris Lattner49948772004-02-09 01:43:23 +0000156 PhysRegInfo[Alias] = MI;
157 PhysRegUsed[Alias] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000158 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000159}
160
161bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner96aef892004-02-09 01:35:21 +0000162 const TargetInstrInfo &TII = MF.getTarget().getInstrInfo();
163 RegInfo = MF.getTarget().getRegisterInfo();
164 assert(RegInfo && "Target doesn't have register information?");
165
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000166 // First time though, initialize AllocatablePhysicalRegisters for the target
167 if (AllocatablePhysicalRegisters.empty()) {
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000168 // Make space, initializing to false...
Chris Lattner96aef892004-02-09 01:35:21 +0000169 AllocatablePhysicalRegisters.resize(RegInfo->getNumRegs());
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000170
171 // Loop over all of the register classes...
Chris Lattner96aef892004-02-09 01:35:21 +0000172 for (MRegisterInfo::regclass_iterator RCI = RegInfo->regclass_begin(),
173 E = RegInfo->regclass_end(); RCI != E; ++RCI)
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000174 // Loop over all of the allocatable registers in the function...
175 for (TargetRegisterClass::iterator I = (*RCI)->allocation_order_begin(MF),
176 E = (*RCI)->allocation_order_end(MF); I != E; ++I)
177 AllocatablePhysicalRegisters[*I] = true; // The reg is allocatable!
178 }
179
Chris Lattnerbc40e892003-01-13 20:01:16 +0000180 // Build BBMap...
181 unsigned BBNum = 0;
182 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
183 BBMap[I->getBasicBlock()] = std::make_pair(I, BBNum++);
184
185 // PhysRegInfo - Keep track of which instruction was the last use of a
186 // physical register. This is a purely local property, because all physical
187 // register references as presumed dead across basic blocks.
188 //
189 MachineInstr *PhysRegInfoA[MRegisterInfo::FirstVirtualRegister];
190 bool PhysRegUsedA[MRegisterInfo::FirstVirtualRegister];
191 std::fill(PhysRegInfoA, PhysRegInfoA+MRegisterInfo::FirstVirtualRegister,
192 (MachineInstr*)0);
193 PhysRegInfo = PhysRegInfoA;
194 PhysRegUsed = PhysRegUsedA;
195
Chris Lattnerbc40e892003-01-13 20:01:16 +0000196 /// Get some space for a respectable number of registers...
197 VirtRegInfo.resize(64);
198
199 // Calculate live variable information in depth first order on the CFG of the
200 // function. This guarantees that we will see the definition of a virtual
201 // register before its uses due to dominance properties of SSA (except for PHI
202 // nodes, which are treated as a special case).
203 //
204 const BasicBlock *Entry = MF.getFunction()->begin();
205 for (df_iterator<const BasicBlock*> DFI = df_begin(Entry), E = df_end(Entry);
206 DFI != E; ++DFI) {
207 const BasicBlock *BB = *DFI;
208 std::pair<MachineBasicBlock*, unsigned> &BBRec = BBMap.find(BB)->second;
209 MachineBasicBlock *MBB = BBRec.first;
210 unsigned BBNum = BBRec.second;
211
212 // Loop over all of the instructions, processing them.
213 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
214 I != E; ++I) {
215 MachineInstr *MI = *I;
216 const TargetInstrDescriptor &MID = TII.get(MI->getOpcode());
217
218 // Process all of the operands of the instruction...
219 unsigned NumOperandsToProcess = MI->getNumOperands();
220
221 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
222 // of the uses. They will be handled in other basic blocks.
223 if (MI->getOpcode() == TargetInstrInfo::PHI)
224 NumOperandsToProcess = 1;
225
226 // Loop over implicit uses, using them.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000227 for (const unsigned *ImplicitUses = MID.ImplicitUses;
228 *ImplicitUses; ++ImplicitUses)
229 HandlePhysRegUse(*ImplicitUses, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000230
231 // Process all explicit uses...
232 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
233 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000234 if (MO.isUse()) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000235 if (MO.isVirtualRegister() && !MO.getVRegValueOrNull()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000236 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000237 } else if (MO.isPhysicalRegister() &&
238 AllocatablePhysicalRegisters[MO.getReg()]) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000239 HandlePhysRegUse(MO.getReg(), MI);
240 }
241 }
242 }
243
244 // Loop over implicit defs, defining them.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000245 for (const unsigned *ImplicitDefs = MID.ImplicitDefs;
246 *ImplicitDefs; ++ImplicitDefs)
247 HandlePhysRegDef(*ImplicitDefs, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000248
249 // Process all explicit defs...
250 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
251 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000252 if (MO.isDef()) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000253 if (MO.isVirtualRegister()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000254 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000255
256 assert(VRInfo.DefBlock == 0 && "Variable multiply defined!");
257 VRInfo.DefBlock = MBB; // Created here...
258 VRInfo.DefInst = MI;
259 VRInfo.Kills.push_back(std::make_pair(MBB, MI)); // Defaults to dead
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000260 } else if (MO.isPhysicalRegister() &&
261 AllocatablePhysicalRegisters[MO.getReg()]) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000262 HandlePhysRegDef(MO.getReg(), MI);
263 }
264 }
265 }
266 }
267
268 // Handle any virtual assignments from PHI nodes which might be at the
269 // bottom of this basic block. We check all of our successor blocks to see
270 // if they have PHI nodes, and if so, we simulate an assignment at the end
271 // of the current block.
Chris Lattnerf98358e2003-05-01 21:18:47 +0000272 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB);
273 SI != E; ++SI) {
274 MachineBasicBlock *Succ = BBMap.find(*SI)->second.first;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000275
276 // PHI nodes are guaranteed to be at the top of the block...
277 for (MachineBasicBlock::iterator I = Succ->begin(), E = Succ->end();
278 I != E && (*I)->getOpcode() == TargetInstrInfo::PHI; ++I) {
Chris Lattnerf98358e2003-05-01 21:18:47 +0000279 MachineInstr *MI = *I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000280 for (unsigned i = 1; ; i += 2)
Chris Lattnerf98358e2003-05-01 21:18:47 +0000281 if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) {
282 MachineOperand &MO = MI->getOperand(i);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000283 if (!MO.getVRegValueOrNull()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000284 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000285
286 // Only mark it alive only in the block we are representing...
287 MarkVirtRegAliveInBlock(VRInfo, BB);
288 break; // Found the PHI entry for this block...
289 }
290 }
291 }
292 }
293
294 // Loop over PhysRegInfo, killing any registers that are available at the
295 // end of the basic block. This also resets the PhysRegInfo map.
Chris Lattner96aef892004-02-09 01:35:21 +0000296 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000297 if (PhysRegInfo[i])
298 HandlePhysRegDef(i, 0);
299 }
300
Chris Lattnerbc40e892003-01-13 20:01:16 +0000301 // Convert the information we have gathered into VirtRegInfo and transform it
302 // into a form usable by RegistersKilled.
303 //
304 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
305 for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
306 if (VirtRegInfo[i].Kills[j].second == VirtRegInfo[i].DefInst)
307 RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
308 i + MRegisterInfo::FirstVirtualRegister));
309
310 else
311 RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
312 i + MRegisterInfo::FirstVirtualRegister));
313 }
314
315 return false;
316}