blob: 34ad23898160dc83bb553184e30145d69fa5c77f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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//
27//===----------------------------------------------------------------------===//
28
29#include "llvm/CodeGen/LiveVariables.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner1b989192007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/ADT/DepthFirstIterator.h"
36#include "llvm/ADT/SmallPtrSet.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/Config/alloca.h"
39#include <algorithm>
40using namespace llvm;
41
42char LiveVariables::ID = 0;
43static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
44
45void LiveVariables::VarInfo::dump() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 cerr << " Alive in blocks: ";
47 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
48 if (AliveBlocks[i]) cerr << i << ", ";
Owen Anderson721b2cc2007-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 << ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 cerr << "\n Killed by:";
53 if (Kills.empty())
54 cerr << " No instructions.\n";
55 else {
56 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
57 cerr << "\n #" << i << ": " << *Kills[i];
58 cerr << "\n";
59 }
60}
61
62LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Dan Gohman1e57df32008-02-10 18:45:23 +000063 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 "getVarInfo: not a virtual register!");
Dan Gohman1e57df32008-02-10 18:45:23 +000065 RegIdx -= TargetRegisterInfo::FirstVirtualRegister;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 if (RegIdx >= VirtRegInfo.size()) {
67 if (RegIdx >= 2*VirtRegInfo.size())
68 VirtRegInfo.resize(RegIdx*2);
69 else
70 VirtRegInfo.resize(2*VirtRegInfo.size());
71 }
72 VarInfo &VI = VirtRegInfo[RegIdx];
73 VI.AliveBlocks.resize(MF->getNumBlockIDs());
Owen Anderson721b2cc2007-11-08 01:20:48 +000074 VI.UsedBlocks.resize(MF->getNumBlockIDs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 return VI;
76}
77
78bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
79 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
80 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +000081 if (MO.isRegister() && MO.isKill()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 if ((MO.getReg() == Reg) ||
Dan Gohman1e57df32008-02-10 18:45:23 +000083 (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
84 TargetRegisterInfo::isPhysicalRegister(Reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 RegInfo->isSubRegister(MO.getReg(), Reg)))
86 return true;
87 }
88 }
89 return false;
90}
91
92bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
93 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
94 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +000095 if (MO.isRegister() && MO.isDead()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 if ((MO.getReg() == Reg) ||
Dan Gohman1e57df32008-02-10 18:45:23 +000097 (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
98 TargetRegisterInfo::isPhysicalRegister(Reg) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 RegInfo->isSubRegister(MO.getReg(), Reg)))
100 return true;
101 }
102 }
103 return false;
104}
105
106bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
107 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
108 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000109 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 return true;
111 }
112 return false;
113}
114
Owen Anderson77d80492008-01-15 22:58:11 +0000115void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
116 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 MachineBasicBlock *MBB,
118 std::vector<MachineBasicBlock*> &WorkList) {
119 unsigned BBNum = MBB->getNumber();
Owen Anderson92a609a2008-01-15 22:02:46 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // Check to see if this basic block is one of the killing blocks. If so,
122 // remove it...
123 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
124 if (VRInfo.Kills[i]->getParent() == MBB) {
125 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
126 break;
127 }
Owen Anderson92a609a2008-01-15 22:02:46 +0000128
Owen Anderson77d80492008-01-15 22:58:11 +0000129 if (MBB == DefBlock) return; // Terminate recursion
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130
131 if (VRInfo.AliveBlocks[BBNum])
132 return; // We already know the block is live
133
134 // Mark the variable known alive in this bb
135 VRInfo.AliveBlocks[BBNum] = true;
136
137 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
138 E = MBB->pred_rend(); PI != E; ++PI)
139 WorkList.push_back(*PI);
140}
141
Owen Anderson77d80492008-01-15 22:58:11 +0000142void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
143 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 MachineBasicBlock *MBB) {
145 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson77d80492008-01-15 22:58:11 +0000146 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 while (!WorkList.empty()) {
148 MachineBasicBlock *Pred = WorkList.back();
149 WorkList.pop_back();
Owen Anderson77d80492008-01-15 22:58:11 +0000150 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 }
152}
153
154
Owen Anderson92a609a2008-01-15 22:02:46 +0000155void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 MachineInstr *MI) {
Owen Anderson92a609a2008-01-15 22:02:46 +0000157 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
158 assert(MRI.getVRegDef(reg) && "Register use before def!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Owen Anderson721b2cc2007-11-08 01:20:48 +0000160 unsigned BBNum = MBB->getNumber();
161
Owen Anderson92a609a2008-01-15 22:02:46 +0000162 VarInfo& VRInfo = getVarInfo(reg);
Owen Anderson721b2cc2007-11-08 01:20:48 +0000163 VRInfo.UsedBlocks[BBNum] = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 VRInfo.NumUses++;
165
166 // Check to see if this basic block is already a kill block...
167 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
168 // Yes, this register is killed in this basic block already. Increase the
169 // live range by updating the kill instruction.
170 VRInfo.Kills.back() = MI;
171 return;
172 }
173
174#ifndef NDEBUG
175 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
176 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
177#endif
178
Owen Anderson92a609a2008-01-15 22:02:46 +0000179 assert(MBB != MRI.getVRegDef(reg)->getParent() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 "Should have kill for defblock!");
181
182 // Add a new kill entry for this basic block.
183 // If this virtual register is already marked as alive in this basic block,
184 // that means it is alive in at least one of the successor block, it's not
185 // a kill.
Owen Anderson721b2cc2007-11-08 01:20:48 +0000186 if (!VRInfo.AliveBlocks[BBNum])
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 VRInfo.Kills.push_back(MI);
188
189 // Update all dominating blocks to mark them known live.
190 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
191 E = MBB->pred_end(); PI != E; ++PI)
Owen Anderson77d80492008-01-15 22:58:11 +0000192 MarkVirtRegAliveInBlock(VRInfo, MRI.getVRegDef(reg)->getParent(), *PI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193}
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // Turn previous partial def's into read/mod/write.
197 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
198 MachineInstr *Def = PhysRegPartDef[Reg][i];
199 // First one is just a def. This means the use is reading some undef bits.
200 if (i != 0)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000201 Def->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
202 true/*IsImp*/,true/*IsKill*/));
203 Def->addOperand(MachineOperand::CreateReg(Reg,true/*IsDef*/,true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205 PhysRegPartDef[Reg].clear();
206
207 // There was an earlier def of a super-register. Add implicit def to that MI.
208 // A: EAX = ...
209 // B: = AX
210 // Add implicit def to A.
Evan Chenge993ca22007-09-11 22:34:47 +0000211 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
212 !PhysRegUsed[Reg]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 MachineInstr *Def = PhysRegInfo[Reg];
214 if (!Def->findRegisterDefOperand(Reg))
Chris Lattner63ab1f22007-12-30 00:41:17 +0000215 Def->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
216 true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
218
Evan Chenge993ca22007-09-11 22:34:47 +0000219 // There is a now a proper use, forget about the last partial use.
220 PhysRegPartUse[Reg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 PhysRegInfo[Reg] = MI;
222 PhysRegUsed[Reg] = true;
223
224 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
225 unsigned SubReg = *SubRegs; ++SubRegs) {
226 PhysRegInfo[SubReg] = MI;
227 PhysRegUsed[SubReg] = true;
228 }
229
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
Evan Chenge4ec6192007-08-01 20:18:21 +0000231 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
232 // Remember the partial use of this superreg if it was previously defined.
233 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
234 if (!HasPrevDef) {
235 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg);
236 unsigned SSReg = *SSRegs; ++SSRegs) {
237 if (PhysRegInfo[SSReg] != NULL) {
238 HasPrevDef = true;
239 break;
240 }
241 }
242 }
243 if (HasPrevDef) {
244 PhysRegInfo[SuperReg] = MI;
245 PhysRegPartUse[SuperReg] = MI;
246 }
247 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248}
249
250bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI,
251 SmallSet<unsigned, 4> &SubKills) {
252 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
253 unsigned SubReg = *SubRegs; ++SubRegs) {
254 MachineInstr *LastRef = PhysRegInfo[SubReg];
Evan Cheng18ee3322007-09-12 23:02:04 +0000255 if (LastRef != RefMI ||
256 !HandlePhysRegKill(SubReg, RefMI, SubKills))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 SubKills.insert(SubReg);
258 }
259
260 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) {
261 // No sub-registers, just check if reg is killed by RefMI.
262 if (PhysRegInfo[Reg] == RefMI)
263 return true;
264 } else if (SubKills.empty())
265 // None of the sub-registers are killed elsewhere...
266 return true;
267 return false;
268}
269
270void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
271 SmallSet<unsigned, 4> &SubKills) {
272 if (SubKills.count(Reg) == 0)
Owen Anderson58060792008-01-24 01:10:07 +0000273 MI->addRegisterKilled(Reg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 else {
275 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
276 unsigned SubReg = *SubRegs; ++SubRegs)
277 addRegisterKills(SubReg, MI, SubKills);
278 }
279}
280
281bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
282 SmallSet<unsigned, 4> SubKills;
283 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
Owen Anderson58060792008-01-24 01:10:07 +0000284 RefMI->addRegisterKilled(Reg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 return true;
286 } else {
287 // Some sub-registers are killed by another MI.
288 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
289 unsigned SubReg = *SubRegs; ++SubRegs)
290 addRegisterKills(SubReg, RefMI, SubKills);
291 return false;
292 }
293}
294
295void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
296 // Does this kill a previous version of this register?
297 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
298 if (PhysRegUsed[Reg]) {
299 if (!HandlePhysRegKill(Reg, LastRef)) {
300 if (PhysRegPartUse[Reg])
Owen Anderson58060792008-01-24 01:10:07 +0000301 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 }
303 } else if (PhysRegPartUse[Reg])
Evan Chenge4ec6192007-08-01 20:18:21 +0000304 // Add implicit use / kill to last partial use.
Owen Anderson58060792008-01-24 01:10:07 +0000305 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
Evan Cheng9cf8f9c2007-11-05 03:11:55 +0000306 else if (LastRef != MI)
307 // Defined, but not used. However, watch out for cases where a super-reg
308 // is also defined on the same MI.
Owen Anderson58060792008-01-24 01:10:07 +0000309 LastRef->addRegisterDead(Reg, RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 }
311
312 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
313 unsigned SubReg = *SubRegs; ++SubRegs) {
314 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
315 if (PhysRegUsed[SubReg]) {
316 if (!HandlePhysRegKill(SubReg, LastRef)) {
317 if (PhysRegPartUse[SubReg])
Owen Anderson58060792008-01-24 01:10:07 +0000318 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 }
320 } else if (PhysRegPartUse[SubReg])
321 // Add implicit use / kill to last use of a sub-register.
Owen Anderson58060792008-01-24 01:10:07 +0000322 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
Evan Chenge993ca22007-09-11 22:34:47 +0000323 else if (LastRef != MI)
324 // This must be a def of the subreg on the same MI.
Owen Anderson58060792008-01-24 01:10:07 +0000325 LastRef->addRegisterDead(SubReg, RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 }
327 }
328
329 if (MI) {
330 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
331 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Chenge993ca22007-09-11 22:34:47 +0000332 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // The larger register is previously defined. Now a smaller part is
334 // being re-defined. Treat it as read/mod/write.
335 // EAX =
336 // AX = EAX<imp-use,kill>, EAX<imp-def>
Chris Lattner63ab1f22007-12-30 00:41:17 +0000337 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
338 true/*IsImp*/,true/*IsKill*/));
339 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
340 true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 PhysRegInfo[SuperReg] = MI;
342 PhysRegUsed[SuperReg] = false;
343 PhysRegPartUse[SuperReg] = NULL;
344 } else {
345 // Remember this partial def.
346 PhysRegPartDef[SuperReg].push_back(MI);
347 }
348 }
349
350 PhysRegInfo[Reg] = MI;
351 PhysRegUsed[Reg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000352 PhysRegPartDef[Reg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 PhysRegPartUse[Reg] = NULL;
354 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
355 unsigned SubReg = *SubRegs; ++SubRegs) {
356 PhysRegInfo[SubReg] = MI;
357 PhysRegUsed[SubReg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000358 PhysRegPartDef[SubReg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 PhysRegPartUse[SubReg] = NULL;
360 }
361 }
362}
363
364bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
365 MF = &mf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 RegInfo = MF->getTarget().getRegisterInfo();
Owen Anderson77d80492008-01-15 22:58:11 +0000367 MachineRegisterInfo& MRI = mf.getRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 assert(RegInfo && "Target doesn't have register information?");
369
370 ReservedRegisters = RegInfo->getReservedRegs(mf);
371
372 unsigned NumRegs = RegInfo->getNumRegs();
373 PhysRegInfo = new MachineInstr*[NumRegs];
374 PhysRegUsed = new bool[NumRegs];
375 PhysRegPartUse = new MachineInstr*[NumRegs];
376 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
377 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
378 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
379 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
380 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
381
382 /// Get some space for a respectable number of registers...
383 VirtRegInfo.resize(64);
384
385 analyzePHINodes(mf);
386
387 // Calculate live variable information in depth first order on the CFG of the
388 // function. This guarantees that we will see the definition of a virtual
389 // register before its uses due to dominance properties of SSA (except for PHI
390 // nodes, which are treated as a special case).
391 //
392 MachineBasicBlock *Entry = MF->begin();
393 SmallPtrSet<MachineBasicBlock*,16> Visited;
394 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
395 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
396 DFI != E; ++DFI) {
397 MachineBasicBlock *MBB = *DFI;
398
399 // Mark live-in registers as live-in.
400 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
401 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000402 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 "Cannot have a live-in virtual register!");
404 HandlePhysRegDef(*II, 0);
405 }
406
407 // Loop over all of the instructions, processing them.
408 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
409 I != E; ++I) {
410 MachineInstr *MI = I;
411
412 // Process all of the operands of the instruction...
413 unsigned NumOperandsToProcess = MI->getNumOperands();
414
415 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
416 // of the uses. They will be handled in other basic blocks.
417 if (MI->getOpcode() == TargetInstrInfo::PHI)
418 NumOperandsToProcess = 1;
419
420 // Process all uses...
421 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
422 MachineOperand &MO = MI->getOperand(i);
423 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000424 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())){
Owen Anderson92a609a2008-01-15 22:02:46 +0000425 HandleVirtRegUse(MO.getReg(), MBB, MI);
Dan Gohman1e57df32008-02-10 18:45:23 +0000426 } else if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 !ReservedRegisters[MO.getReg()]) {
428 HandlePhysRegUse(MO.getReg(), MI);
429 }
430 }
431 }
432
433 // Process all defs...
434 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
435 MachineOperand &MO = MI->getOperand(i);
436 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000437 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 VarInfo &VRInfo = getVarInfo(MO.getReg());
Evan Cheng86f26d22008-02-05 20:04:18 +0000439 if (VRInfo.AliveBlocks.none())
440 // If vr is not alive in any block, then defaults to dead.
441 VRInfo.Kills.push_back(MI);
Dan Gohman1e57df32008-02-10 18:45:23 +0000442 } else if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 !ReservedRegisters[MO.getReg()]) {
444 HandlePhysRegDef(MO.getReg(), MI);
445 }
446 }
447 }
448 }
449
450 // Handle any virtual assignments from PHI nodes which might be at the
451 // bottom of this basic block. We check all of our successor blocks to see
452 // if they have PHI nodes, and if so, we simulate an assignment at the end
453 // of the current block.
454 if (!PHIVarInfo[MBB->getNumber()].empty()) {
455 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
456
457 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
458 E = VarInfoVec.end(); I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 // Only mark it alive only in the block we are representing.
Owen Anderson77d80492008-01-15 22:58:11 +0000460 MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(),
461 MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 }
463 }
464
465 // Finally, if the last instruction in the block is a return, make sure to mark
466 // it as using all of the live-out values in the function.
Chris Lattner5b930372008-01-07 07:27:27 +0000467 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 MachineInstr *Ret = &MBB->back();
Chris Lattner1b989192007-12-31 04:13:23 +0000469 for (MachineRegisterInfo::liveout_iterator
470 I = MF->getRegInfo().liveout_begin(),
471 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000472 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 "Cannot have a live-in virtual register!");
474 HandlePhysRegUse(*I, Ret);
475 // Add live-out registers as implicit uses.
476 if (Ret->findRegisterUseOperandIdx(*I) == -1)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000477 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 }
479 }
480
481 // Loop over PhysRegInfo, killing any registers that are available at the
482 // end of the basic block. This also resets the PhysRegInfo map.
483 for (unsigned i = 0; i != NumRegs; ++i)
484 if (PhysRegInfo[i])
485 HandlePhysRegDef(i, 0);
486
487 // Clear some states between BB's. These are purely local information.
488 for (unsigned i = 0; i != NumRegs; ++i)
489 PhysRegPartDef[i].clear();
490 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
491 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
492 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
493 }
494
495 // Convert and transfer the dead / killed information we have gathered into
496 // VirtRegInfo onto MI's.
497 //
498 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
499 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) {
Owen Anderson92a609a2008-01-15 22:02:46 +0000500 if (VirtRegInfo[i].Kills[j] == MRI.getVRegDef(i +
Dan Gohman1e57df32008-02-10 18:45:23 +0000501 TargetRegisterInfo::FirstVirtualRegister))
Owen Anderson58060792008-01-24 01:10:07 +0000502 VirtRegInfo[i].Kills[j]->addRegisterDead(i +
Dan Gohman1e57df32008-02-10 18:45:23 +0000503 TargetRegisterInfo::FirstVirtualRegister,
Owen Anderson58060792008-01-24 01:10:07 +0000504 RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 else
Owen Anderson58060792008-01-24 01:10:07 +0000506 VirtRegInfo[i].Kills[j]->addRegisterKilled(i +
Dan Gohman1e57df32008-02-10 18:45:23 +0000507 TargetRegisterInfo::FirstVirtualRegister,
Owen Anderson58060792008-01-24 01:10:07 +0000508 RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 }
510
511 // Check to make sure there are no unreachable blocks in the MC CFG for the
512 // function. If so, it is due to a bug in the instruction selector or some
513 // other part of the code generator if this happens.
514#ifndef NDEBUG
515 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
516 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
517#endif
518
519 delete[] PhysRegInfo;
520 delete[] PhysRegUsed;
521 delete[] PhysRegPartUse;
522 delete[] PhysRegPartDef;
523 delete[] PHIVarInfo;
524
525 return false;
526}
527
528/// instructionChanged - When the address of an instruction changes, this
529/// method should be called so that live variables can update its internal
530/// data structures. This removes the records for OldMI, transfering them to
531/// the records for NewMI.
532void LiveVariables::instructionChanged(MachineInstr *OldMI,
533 MachineInstr *NewMI) {
534 // If the instruction defines any virtual registers, update the VarInfo,
535 // kill and dead information for the instruction.
536 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
537 MachineOperand &MO = OldMI->getOperand(i);
538 if (MO.isRegister() && MO.getReg() &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000539 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 unsigned Reg = MO.getReg();
541 VarInfo &VI = getVarInfo(Reg);
542 if (MO.isDef()) {
543 if (MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000544 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 addVirtualRegisterDead(Reg, NewMI);
546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000548 if (MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000549 MO.setIsKill(false);
Dan Gohman2c6a6422007-07-20 23:17:34 +0000550 addVirtualRegisterKilled(Reg, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000552 // If this is a kill of the value, update the VI kills list.
553 if (VI.removeKill(OldMI))
554 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 }
556 }
557}
558
559/// removeVirtualRegistersKilled - Remove all killed info for the specified
560/// instruction.
561void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
562 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
563 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000564 if (MO.isRegister() && MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000565 MO.setIsKill(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000567 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 bool removed = getVarInfo(Reg).removeKill(MI);
569 assert(removed && "kill not in register's VarInfo?");
570 }
571 }
572 }
573}
574
575/// removeVirtualRegistersDead - Remove all of the dead registers for the
576/// specified instruction from the live variable information.
577void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
578 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
579 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000580 if (MO.isRegister() && MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000581 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000583 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 bool removed = getVarInfo(Reg).removeKill(MI);
585 assert(removed && "kill not in register's VarInfo?");
586 }
587 }
588 }
589}
590
591/// analyzePHINodes - Gather information about the PHI nodes in here. In
592/// particular, we want to map the variable information of a virtual
593/// register which is used in a PHI node. We map that to the BB the vreg is
594/// coming from.
595///
596void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
597 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
598 I != E; ++I)
599 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
600 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
601 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Chris Lattner6017d482007-12-30 23:10:15 +0000602 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()].
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 push_back(BBI->getOperand(i).getReg());
604}