blob: 358d5d0829df7fbdab3f48a9edd57b7254f17986 [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"
35
Brian Gaeked0fde302003-11-11 22:41:34 +000036namespace llvm {
37
Chris Lattnerbc40e892003-01-13 20:01:16 +000038static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
39
Chris Lattnerfb2cb692003-05-12 14:24:00 +000040const std::pair<MachineBasicBlock*, unsigned> &
41LiveVariables::getMachineBasicBlockInfo(MachineBasicBlock *MBB) const{
42 return BBMap.find(MBB->getBasicBlock())->second;
43}
44
45LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
46 assert(RegIdx >= MRegisterInfo::FirstVirtualRegister &&
47 "getVarInfo: not a virtual register!");
48 RegIdx -= MRegisterInfo::FirstVirtualRegister;
49 if (RegIdx >= VirtRegInfo.size()) {
50 if (RegIdx >= 2*VirtRegInfo.size())
51 VirtRegInfo.resize(RegIdx*2);
52 else
53 VirtRegInfo.resize(2*VirtRegInfo.size());
54 }
55 return VirtRegInfo[RegIdx];
56}
57
58
59
Chris Lattnerbc40e892003-01-13 20:01:16 +000060void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
61 const BasicBlock *BB) {
62 const std::pair<MachineBasicBlock*,unsigned> &Info = BBMap.find(BB)->second;
63 MachineBasicBlock *MBB = Info.first;
64 unsigned BBNum = Info.second;
65
66 // Check to see if this basic block is one of the killing blocks. If so,
67 // remove it...
68 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
69 if (VRInfo.Kills[i].first == MBB) {
70 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
71 break;
72 }
73
74 if (MBB == VRInfo.DefBlock) return; // Terminate recursion
75
76 if (VRInfo.AliveBlocks.size() <= BBNum)
77 VRInfo.AliveBlocks.resize(BBNum+1); // Make space...
78
79 if (VRInfo.AliveBlocks[BBNum])
80 return; // We already know the block is live
81
82 // Mark the variable known alive in this bb
83 VRInfo.AliveBlocks[BBNum] = true;
84
85 for (pred_const_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
86 MarkVirtRegAliveInBlock(VRInfo, *PI);
87}
88
89void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
90 MachineInstr *MI) {
91 // Check to see if this basic block is already a kill block...
92 if (!VRInfo.Kills.empty() && VRInfo.Kills.back().first == MBB) {
93 // Yes, this register is killed in this basic block already. Increase the
94 // live range by updating the kill instruction.
95 VRInfo.Kills.back().second = MI;
96 return;
97 }
98
99#ifndef NDEBUG
100 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
101 assert(VRInfo.Kills[i].first != MBB && "entry should be at end!");
102#endif
103
104 assert(MBB != VRInfo.DefBlock && "Should have kill for defblock!");
105
106 // Add a new kill entry for this basic block.
107 VRInfo.Kills.push_back(std::make_pair(MBB, MI));
108
109 // Update all dominating blocks to mark them known live.
110 const BasicBlock *BB = MBB->getBasicBlock();
111 for (pred_const_iterator PI = pred_begin(BB), E = pred_end(BB);
112 PI != E; ++PI)
113 MarkVirtRegAliveInBlock(VRInfo, *PI);
114}
115
116void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
117 if (PhysRegInfo[Reg]) {
118 PhysRegInfo[Reg] = MI;
119 PhysRegUsed[Reg] = true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000120 } else {
121 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
122 *AliasSet; ++AliasSet) {
123 if (MachineInstr *LastUse = PhysRegInfo[*AliasSet]) {
124 PhysRegInfo[*AliasSet] = MI;
125 PhysRegUsed[*AliasSet] = true;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000126 }
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000127 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000128 }
129}
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));
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000138 } else {
139 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
140 *AliasSet; ++AliasSet) {
141 if (MachineInstr *LastUse = PhysRegInfo[*AliasSet]) {
142 if (PhysRegUsed[*AliasSet])
143 RegistersKilled.insert(std::make_pair(LastUse, *AliasSet));
Chris Lattnerbc40e892003-01-13 20:01:16 +0000144 else
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000145 RegistersDead.insert(std::make_pair(LastUse, *AliasSet));
146 PhysRegInfo[*AliasSet] = 0; // Kill the aliased register
Chris Lattnerbc40e892003-01-13 20:01:16 +0000147 }
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000148 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000149 }
150 PhysRegInfo[Reg] = MI;
151 PhysRegUsed[Reg] = false;
152}
153
154bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000155 // First time though, initialize AllocatablePhysicalRegisters for the target
156 if (AllocatablePhysicalRegisters.empty()) {
157 const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo();
158 assert(&MRI && "Target doesn't have register information?");
159
160 // Make space, initializing to false...
161 AllocatablePhysicalRegisters.resize(MRegisterInfo::FirstVirtualRegister);
162
163 // Loop over all of the register classes...
164 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
165 E = MRI.regclass_end(); RCI != E; ++RCI)
166 // Loop over all of the allocatable registers in the function...
167 for (TargetRegisterClass::iterator I = (*RCI)->allocation_order_begin(MF),
168 E = (*RCI)->allocation_order_end(MF); I != E; ++I)
169 AllocatablePhysicalRegisters[*I] = true; // The reg is allocatable!
170 }
171
Chris Lattnerbc40e892003-01-13 20:01:16 +0000172 // Build BBMap...
173 unsigned BBNum = 0;
174 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
175 BBMap[I->getBasicBlock()] = std::make_pair(I, BBNum++);
176
177 // PhysRegInfo - Keep track of which instruction was the last use of a
178 // physical register. This is a purely local property, because all physical
179 // register references as presumed dead across basic blocks.
180 //
181 MachineInstr *PhysRegInfoA[MRegisterInfo::FirstVirtualRegister];
182 bool PhysRegUsedA[MRegisterInfo::FirstVirtualRegister];
183 std::fill(PhysRegInfoA, PhysRegInfoA+MRegisterInfo::FirstVirtualRegister,
184 (MachineInstr*)0);
185 PhysRegInfo = PhysRegInfoA;
186 PhysRegUsed = PhysRegUsedA;
187
188 const TargetInstrInfo &TII = MF.getTarget().getInstrInfo();
189 RegInfo = MF.getTarget().getRegisterInfo();
190
191 /// Get some space for a respectable number of registers...
192 VirtRegInfo.resize(64);
193
194 // Calculate live variable information in depth first order on the CFG of the
195 // function. This guarantees that we will see the definition of a virtual
196 // register before its uses due to dominance properties of SSA (except for PHI
197 // nodes, which are treated as a special case).
198 //
199 const BasicBlock *Entry = MF.getFunction()->begin();
200 for (df_iterator<const BasicBlock*> DFI = df_begin(Entry), E = df_end(Entry);
201 DFI != E; ++DFI) {
202 const BasicBlock *BB = *DFI;
203 std::pair<MachineBasicBlock*, unsigned> &BBRec = BBMap.find(BB)->second;
204 MachineBasicBlock *MBB = BBRec.first;
205 unsigned BBNum = BBRec.second;
206
207 // Loop over all of the instructions, processing them.
208 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
209 I != E; ++I) {
210 MachineInstr *MI = *I;
211 const TargetInstrDescriptor &MID = TII.get(MI->getOpcode());
212
213 // Process all of the operands of the instruction...
214 unsigned NumOperandsToProcess = MI->getNumOperands();
215
216 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
217 // of the uses. They will be handled in other basic blocks.
218 if (MI->getOpcode() == TargetInstrInfo::PHI)
219 NumOperandsToProcess = 1;
220
221 // Loop over implicit uses, using them.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000222 for (const unsigned *ImplicitUses = MID.ImplicitUses;
223 *ImplicitUses; ++ImplicitUses)
224 HandlePhysRegUse(*ImplicitUses, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000225
226 // Process all explicit uses...
227 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
228 MachineOperand &MO = MI->getOperand(i);
229 if (MO.opIsUse() || MO.opIsDefAndUse()) {
230 if (MO.isVirtualRegister() && !MO.getVRegValueOrNull()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000231 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000232 } else if (MO.isPhysicalRegister() &&
233 AllocatablePhysicalRegisters[MO.getReg()]) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000234 HandlePhysRegUse(MO.getReg(), MI);
235 }
236 }
237 }
238
239 // Loop over implicit defs, defining them.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000240 for (const unsigned *ImplicitDefs = MID.ImplicitDefs;
241 *ImplicitDefs; ++ImplicitDefs)
242 HandlePhysRegDef(*ImplicitDefs, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000243
244 // Process all explicit defs...
245 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
246 MachineOperand &MO = MI->getOperand(i);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000247 if (MO.opIsDefOnly() || MO.opIsDefAndUse()) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000248 if (MO.isVirtualRegister()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000249 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000250
251 assert(VRInfo.DefBlock == 0 && "Variable multiply defined!");
252 VRInfo.DefBlock = MBB; // Created here...
253 VRInfo.DefInst = MI;
254 VRInfo.Kills.push_back(std::make_pair(MBB, MI)); // Defaults to dead
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000255 } else if (MO.isPhysicalRegister() &&
256 AllocatablePhysicalRegisters[MO.getReg()]) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000257 HandlePhysRegDef(MO.getReg(), MI);
258 }
259 }
260 }
261 }
262
263 // Handle any virtual assignments from PHI nodes which might be at the
264 // bottom of this basic block. We check all of our successor blocks to see
265 // if they have PHI nodes, and if so, we simulate an assignment at the end
266 // of the current block.
Chris Lattnerf98358e2003-05-01 21:18:47 +0000267 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB);
268 SI != E; ++SI) {
269 MachineBasicBlock *Succ = BBMap.find(*SI)->second.first;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000270
271 // PHI nodes are guaranteed to be at the top of the block...
272 for (MachineBasicBlock::iterator I = Succ->begin(), E = Succ->end();
273 I != E && (*I)->getOpcode() == TargetInstrInfo::PHI; ++I) {
Chris Lattnerf98358e2003-05-01 21:18:47 +0000274 MachineInstr *MI = *I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000275 for (unsigned i = 1; ; i += 2)
Chris Lattnerf98358e2003-05-01 21:18:47 +0000276 if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) {
277 MachineOperand &MO = MI->getOperand(i);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000278 if (!MO.getVRegValueOrNull()) {
Chris Lattnerfb2cb692003-05-12 14:24:00 +0000279 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000280
281 // Only mark it alive only in the block we are representing...
282 MarkVirtRegAliveInBlock(VRInfo, BB);
283 break; // Found the PHI entry for this block...
284 }
285 }
286 }
287 }
288
289 // Loop over PhysRegInfo, killing any registers that are available at the
290 // end of the basic block. This also resets the PhysRegInfo map.
291 for (unsigned i = 0, e = MRegisterInfo::FirstVirtualRegister; i != e; ++i)
292 if (PhysRegInfo[i])
293 HandlePhysRegDef(i, 0);
294 }
295
Chris Lattnerbc40e892003-01-13 20:01:16 +0000296 // Convert the information we have gathered into VirtRegInfo and transform it
297 // into a form usable by RegistersKilled.
298 //
299 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
300 for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
301 if (VirtRegInfo[i].Kills[j].second == VirtRegInfo[i].DefInst)
302 RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
303 i + MRegisterInfo::FirstVirtualRegister));
304
305 else
306 RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
307 i + MRegisterInfo::FirstVirtualRegister));
308 }
309
310 return false;
311}
Brian Gaeked0fde302003-11-11 22:41:34 +0000312
313} // End llvm namespace