blob: 70dd02927c2534bb536c27781ab228b2bb75e825 [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 Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/MRegisterInfo.h"
33#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) {
63 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
64 "getVarInfo: not a virtual register!");
65 RegIdx -= MRegisterInfo::FirstVirtualRegister;
66 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) ||
83 (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
84 MRegisterInfo::isPhysicalRegister(Reg) &&
85 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) ||
97 (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
98 MRegisterInfo::isPhysicalRegister(Reg) &&
99 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 Anderson92a609a2008-01-15 22:02:46 +0000115void LiveVariables::MarkVirtRegAliveInBlock(unsigned reg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 MachineBasicBlock *MBB,
117 std::vector<MachineBasicBlock*> &WorkList) {
118 unsigned BBNum = MBB->getNumber();
119
Owen Anderson92a609a2008-01-15 22:02:46 +0000120 VarInfo& VRInfo = getVarInfo(reg);
121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 // Check to see if this basic block is one of the killing blocks. If so,
123 // remove it...
124 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
125 if (VRInfo.Kills[i]->getParent() == MBB) {
126 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
127 break;
128 }
Owen Anderson92a609a2008-01-15 22:02:46 +0000129
130 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
131 if (MBB == MRI.getVRegDef(reg)->getParent()) return; // Terminate recursion
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 if (VRInfo.AliveBlocks[BBNum])
134 return; // We already know the block is live
135
136 // Mark the variable known alive in this bb
137 VRInfo.AliveBlocks[BBNum] = true;
138
139 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
140 E = MBB->pred_rend(); PI != E; ++PI)
141 WorkList.push_back(*PI);
142}
143
Owen Anderson92a609a2008-01-15 22:02:46 +0000144void LiveVariables::MarkVirtRegAliveInBlock(unsigned reg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 MachineBasicBlock *MBB) {
146 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson92a609a2008-01-15 22:02:46 +0000147 MarkVirtRegAliveInBlock(reg, MBB, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 while (!WorkList.empty()) {
149 MachineBasicBlock *Pred = WorkList.back();
150 WorkList.pop_back();
Owen Anderson92a609a2008-01-15 22:02:46 +0000151 MarkVirtRegAliveInBlock(reg, Pred, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 }
153}
154
155
Owen Anderson92a609a2008-01-15 22:02:46 +0000156void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 MachineInstr *MI) {
Owen Anderson92a609a2008-01-15 22:02:46 +0000158 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
159 assert(MRI.getVRegDef(reg) && "Register use before def!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Owen Anderson721b2cc2007-11-08 01:20:48 +0000161 unsigned BBNum = MBB->getNumber();
162
Owen Anderson92a609a2008-01-15 22:02:46 +0000163 VarInfo& VRInfo = getVarInfo(reg);
Owen Anderson721b2cc2007-11-08 01:20:48 +0000164 VRInfo.UsedBlocks[BBNum] = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 VRInfo.NumUses++;
166
167 // Check to see if this basic block is already a kill block...
168 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
169 // Yes, this register is killed in this basic block already. Increase the
170 // live range by updating the kill instruction.
171 VRInfo.Kills.back() = MI;
172 return;
173 }
174
175#ifndef NDEBUG
176 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
177 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
178#endif
179
Owen Anderson92a609a2008-01-15 22:02:46 +0000180 assert(MBB != MRI.getVRegDef(reg)->getParent() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 "Should have kill for defblock!");
182
183 // Add a new kill entry for this basic block.
184 // If this virtual register is already marked as alive in this basic block,
185 // that means it is alive in at least one of the successor block, it's not
186 // a kill.
Owen Anderson721b2cc2007-11-08 01:20:48 +0000187 if (!VRInfo.AliveBlocks[BBNum])
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 VRInfo.Kills.push_back(MI);
189
190 // Update all dominating blocks to mark them known live.
191 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
192 E = MBB->pred_end(); PI != E; ++PI)
Owen Anderson92a609a2008-01-15 22:02:46 +0000193 MarkVirtRegAliveInBlock(reg, *PI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194}
195
196bool LiveVariables::addRegisterKilled(unsigned IncomingReg, MachineInstr *MI,
Evan Chengcecc8222007-11-17 00:40:40 +0000197 const MRegisterInfo *RegInfo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 bool AddIfNotFound) {
199 bool Found = false;
200 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
201 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000202 if (MO.isRegister() && MO.isUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 unsigned Reg = MO.getReg();
204 if (!Reg)
205 continue;
206 if (Reg == IncomingReg) {
207 MO.setIsKill();
208 Found = true;
209 break;
210 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
211 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
212 RegInfo->isSuperRegister(IncomingReg, Reg) &&
213 MO.isKill())
214 // A super-register kill already exists.
Evan Cheng9cf8f9c2007-11-05 03:11:55 +0000215 Found = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 }
217 }
218
219 // If not found, this means an alias of one of the operand is killed. Add a
220 // new implicit operand if required.
221 if (!Found && AddIfNotFound) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000222 MI->addOperand(MachineOperand::CreateReg(IncomingReg, false/*IsDef*/,
223 true/*IsImp*/,true/*IsKill*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return true;
225 }
226 return Found;
227}
228
229bool LiveVariables::addRegisterDead(unsigned IncomingReg, MachineInstr *MI,
Evan Chengcecc8222007-11-17 00:40:40 +0000230 const MRegisterInfo *RegInfo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 bool AddIfNotFound) {
232 bool Found = false;
233 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
234 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000235 if (MO.isRegister() && MO.isDef()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 unsigned Reg = MO.getReg();
237 if (!Reg)
238 continue;
239 if (Reg == IncomingReg) {
240 MO.setIsDead();
241 Found = true;
242 break;
243 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
244 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
245 RegInfo->isSuperRegister(IncomingReg, Reg) &&
246 MO.isDead())
247 // There exists a super-register that's marked dead.
248 return true;
249 }
250 }
251
252 // If not found, this means an alias of one of the operand is dead. Add a
253 // new implicit operand.
254 if (!Found && AddIfNotFound) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000255 MI->addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/,
256 true/*IsImp*/,false/*IsKill*/,
257 true/*IsDead*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 return true;
259 }
260 return Found;
261}
262
263void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 // Turn previous partial def's into read/mod/write.
265 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
266 MachineInstr *Def = PhysRegPartDef[Reg][i];
267 // First one is just a def. This means the use is reading some undef bits.
268 if (i != 0)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000269 Def->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
270 true/*IsImp*/,true/*IsKill*/));
271 Def->addOperand(MachineOperand::CreateReg(Reg,true/*IsDef*/,true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 }
273 PhysRegPartDef[Reg].clear();
274
275 // There was an earlier def of a super-register. Add implicit def to that MI.
276 // A: EAX = ...
277 // B: = AX
278 // Add implicit def to A.
Evan Chenge993ca22007-09-11 22:34:47 +0000279 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
280 !PhysRegUsed[Reg]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 MachineInstr *Def = PhysRegInfo[Reg];
282 if (!Def->findRegisterDefOperand(Reg))
Chris Lattner63ab1f22007-12-30 00:41:17 +0000283 Def->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
284 true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 }
286
Evan Chenge993ca22007-09-11 22:34:47 +0000287 // There is a now a proper use, forget about the last partial use.
288 PhysRegPartUse[Reg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 PhysRegInfo[Reg] = MI;
290 PhysRegUsed[Reg] = true;
291
292 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
293 unsigned SubReg = *SubRegs; ++SubRegs) {
294 PhysRegInfo[SubReg] = MI;
295 PhysRegUsed[SubReg] = true;
296 }
297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
Evan Chenge4ec6192007-08-01 20:18:21 +0000299 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
300 // Remember the partial use of this superreg if it was previously defined.
301 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
302 if (!HasPrevDef) {
303 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg);
304 unsigned SSReg = *SSRegs; ++SSRegs) {
305 if (PhysRegInfo[SSReg] != NULL) {
306 HasPrevDef = true;
307 break;
308 }
309 }
310 }
311 if (HasPrevDef) {
312 PhysRegInfo[SuperReg] = MI;
313 PhysRegPartUse[SuperReg] = MI;
314 }
315 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316}
317
318bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI,
319 SmallSet<unsigned, 4> &SubKills) {
320 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
321 unsigned SubReg = *SubRegs; ++SubRegs) {
322 MachineInstr *LastRef = PhysRegInfo[SubReg];
Evan Cheng18ee3322007-09-12 23:02:04 +0000323 if (LastRef != RefMI ||
324 !HandlePhysRegKill(SubReg, RefMI, SubKills))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 SubKills.insert(SubReg);
326 }
327
328 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) {
329 // No sub-registers, just check if reg is killed by RefMI.
330 if (PhysRegInfo[Reg] == RefMI)
331 return true;
332 } else if (SubKills.empty())
333 // None of the sub-registers are killed elsewhere...
334 return true;
335 return false;
336}
337
338void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
339 SmallSet<unsigned, 4> &SubKills) {
340 if (SubKills.count(Reg) == 0)
Evan Chengcecc8222007-11-17 00:40:40 +0000341 addRegisterKilled(Reg, MI, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 else {
343 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
344 unsigned SubReg = *SubRegs; ++SubRegs)
345 addRegisterKills(SubReg, MI, SubKills);
346 }
347}
348
349bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
350 SmallSet<unsigned, 4> SubKills;
351 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
Evan Chengcecc8222007-11-17 00:40:40 +0000352 addRegisterKilled(Reg, RefMI, RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 return true;
354 } else {
355 // Some sub-registers are killed by another MI.
356 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
357 unsigned SubReg = *SubRegs; ++SubRegs)
358 addRegisterKills(SubReg, RefMI, SubKills);
359 return false;
360 }
361}
362
363void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
364 // Does this kill a previous version of this register?
365 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
366 if (PhysRegUsed[Reg]) {
367 if (!HandlePhysRegKill(Reg, LastRef)) {
368 if (PhysRegPartUse[Reg])
Evan Chengcecc8222007-11-17 00:40:40 +0000369 addRegisterKilled(Reg, PhysRegPartUse[Reg], RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 }
371 } else if (PhysRegPartUse[Reg])
Evan Chenge4ec6192007-08-01 20:18:21 +0000372 // Add implicit use / kill to last partial use.
Evan Chengcecc8222007-11-17 00:40:40 +0000373 addRegisterKilled(Reg, PhysRegPartUse[Reg], RegInfo, true);
Evan Cheng9cf8f9c2007-11-05 03:11:55 +0000374 else if (LastRef != MI)
375 // Defined, but not used. However, watch out for cases where a super-reg
376 // is also defined on the same MI.
Evan Chengcecc8222007-11-17 00:40:40 +0000377 addRegisterDead(Reg, LastRef, RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379
380 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
381 unsigned SubReg = *SubRegs; ++SubRegs) {
382 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
383 if (PhysRegUsed[SubReg]) {
384 if (!HandlePhysRegKill(SubReg, LastRef)) {
385 if (PhysRegPartUse[SubReg])
Evan Chengcecc8222007-11-17 00:40:40 +0000386 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], RegInfo, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 }
388 } else if (PhysRegPartUse[SubReg])
389 // Add implicit use / kill to last use of a sub-register.
Evan Chengcecc8222007-11-17 00:40:40 +0000390 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], RegInfo, true);
Evan Chenge993ca22007-09-11 22:34:47 +0000391 else if (LastRef != MI)
392 // This must be a def of the subreg on the same MI.
Evan Chengcecc8222007-11-17 00:40:40 +0000393 addRegisterDead(SubReg, LastRef, RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 }
395 }
396
397 if (MI) {
398 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
399 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Chenge993ca22007-09-11 22:34:47 +0000400 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 // The larger register is previously defined. Now a smaller part is
402 // being re-defined. Treat it as read/mod/write.
403 // EAX =
404 // AX = EAX<imp-use,kill>, EAX<imp-def>
Chris Lattner63ab1f22007-12-30 00:41:17 +0000405 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
406 true/*IsImp*/,true/*IsKill*/));
407 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
408 true/*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 PhysRegInfo[SuperReg] = MI;
410 PhysRegUsed[SuperReg] = false;
411 PhysRegPartUse[SuperReg] = NULL;
412 } else {
413 // Remember this partial def.
414 PhysRegPartDef[SuperReg].push_back(MI);
415 }
416 }
417
418 PhysRegInfo[Reg] = MI;
419 PhysRegUsed[Reg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000420 PhysRegPartDef[Reg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 PhysRegPartUse[Reg] = NULL;
422 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
423 unsigned SubReg = *SubRegs; ++SubRegs) {
424 PhysRegInfo[SubReg] = MI;
425 PhysRegUsed[SubReg] = false;
Evan Chenge4ec6192007-08-01 20:18:21 +0000426 PhysRegPartDef[SubReg].clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 PhysRegPartUse[SubReg] = NULL;
428 }
429 }
430}
431
432bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
433 MF = &mf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 RegInfo = MF->getTarget().getRegisterInfo();
435 assert(RegInfo && "Target doesn't have register information?");
436
437 ReservedRegisters = RegInfo->getReservedRegs(mf);
438
439 unsigned NumRegs = RegInfo->getNumRegs();
440 PhysRegInfo = new MachineInstr*[NumRegs];
441 PhysRegUsed = new bool[NumRegs];
442 PhysRegPartUse = new MachineInstr*[NumRegs];
443 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
444 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
445 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
446 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
447 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
448
449 /// Get some space for a respectable number of registers...
450 VirtRegInfo.resize(64);
451
452 analyzePHINodes(mf);
453
454 // Calculate live variable information in depth first order on the CFG of the
455 // function. This guarantees that we will see the definition of a virtual
456 // register before its uses due to dominance properties of SSA (except for PHI
457 // nodes, which are treated as a special case).
458 //
459 MachineBasicBlock *Entry = MF->begin();
460 SmallPtrSet<MachineBasicBlock*,16> Visited;
461 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
462 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
463 DFI != E; ++DFI) {
464 MachineBasicBlock *MBB = *DFI;
465
466 // Mark live-in registers as live-in.
467 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
468 EE = MBB->livein_end(); II != EE; ++II) {
469 assert(MRegisterInfo::isPhysicalRegister(*II) &&
470 "Cannot have a live-in virtual register!");
471 HandlePhysRegDef(*II, 0);
472 }
473
474 // Loop over all of the instructions, processing them.
475 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
476 I != E; ++I) {
477 MachineInstr *MI = I;
478
479 // Process all of the operands of the instruction...
480 unsigned NumOperandsToProcess = MI->getNumOperands();
481
482 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
483 // of the uses. They will be handled in other basic blocks.
484 if (MI->getOpcode() == TargetInstrInfo::PHI)
485 NumOperandsToProcess = 1;
486
487 // Process all uses...
488 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
489 MachineOperand &MO = MI->getOperand(i);
490 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
491 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
Owen Anderson92a609a2008-01-15 22:02:46 +0000492 HandleVirtRegUse(MO.getReg(), MBB, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
494 !ReservedRegisters[MO.getReg()]) {
495 HandlePhysRegUse(MO.getReg(), MI);
496 }
497 }
498 }
499
500 // Process all defs...
501 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
502 MachineOperand &MO = MI->getOperand(i);
503 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
504 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
505 VarInfo &VRInfo = getVarInfo(MO.getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 // Defaults to dead
507 VRInfo.Kills.push_back(MI);
508 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
509 !ReservedRegisters[MO.getReg()]) {
510 HandlePhysRegDef(MO.getReg(), MI);
511 }
512 }
513 }
514 }
515
516 // Handle any virtual assignments from PHI nodes which might be at the
517 // bottom of this basic block. We check all of our successor blocks to see
518 // if they have PHI nodes, and if so, we simulate an assignment at the end
519 // of the current block.
520 if (!PHIVarInfo[MBB->getNumber()].empty()) {
521 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
522
523 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
524 E = VarInfoVec.end(); I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 // Only mark it alive only in the block we are representing.
Owen Anderson92a609a2008-01-15 22:02:46 +0000526 MarkVirtRegAliveInBlock(*I, MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 }
528 }
529
530 // Finally, if the last instruction in the block is a return, make sure to mark
531 // it as using all of the live-out values in the function.
Chris Lattner5b930372008-01-07 07:27:27 +0000532 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 MachineInstr *Ret = &MBB->back();
Chris Lattner1b989192007-12-31 04:13:23 +0000534 for (MachineRegisterInfo::liveout_iterator
535 I = MF->getRegInfo().liveout_begin(),
536 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 assert(MRegisterInfo::isPhysicalRegister(*I) &&
538 "Cannot have a live-in virtual register!");
539 HandlePhysRegUse(*I, Ret);
540 // Add live-out registers as implicit uses.
541 if (Ret->findRegisterUseOperandIdx(*I) == -1)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000542 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 }
544 }
545
546 // Loop over PhysRegInfo, killing any registers that are available at the
547 // end of the basic block. This also resets the PhysRegInfo map.
548 for (unsigned i = 0; i != NumRegs; ++i)
549 if (PhysRegInfo[i])
550 HandlePhysRegDef(i, 0);
551
552 // Clear some states between BB's. These are purely local information.
553 for (unsigned i = 0; i != NumRegs; ++i)
554 PhysRegPartDef[i].clear();
555 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
556 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
557 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
558 }
559
560 // Convert and transfer the dead / killed information we have gathered into
561 // VirtRegInfo onto MI's.
562 //
Owen Anderson92a609a2008-01-15 22:02:46 +0000563 MachineRegisterInfo& MRI = mf.getRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
565 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) {
Owen Anderson92a609a2008-01-15 22:02:46 +0000566 if (VirtRegInfo[i].Kills[j] == MRI.getVRegDef(i +
567 MRegisterInfo::FirstVirtualRegister))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 addRegisterDead(i + MRegisterInfo::FirstVirtualRegister,
Evan Chengcecc8222007-11-17 00:40:40 +0000569 VirtRegInfo[i].Kills[j], RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 else
571 addRegisterKilled(i + MRegisterInfo::FirstVirtualRegister,
Evan Chengcecc8222007-11-17 00:40:40 +0000572 VirtRegInfo[i].Kills[j], RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 }
574
575 // Check to make sure there are no unreachable blocks in the MC CFG for the
576 // function. If so, it is due to a bug in the instruction selector or some
577 // other part of the code generator if this happens.
578#ifndef NDEBUG
579 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
580 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
581#endif
582
583 delete[] PhysRegInfo;
584 delete[] PhysRegUsed;
585 delete[] PhysRegPartUse;
586 delete[] PhysRegPartDef;
587 delete[] PHIVarInfo;
588
589 return false;
590}
591
592/// instructionChanged - When the address of an instruction changes, this
593/// method should be called so that live variables can update its internal
594/// data structures. This removes the records for OldMI, transfering them to
595/// the records for NewMI.
596void LiveVariables::instructionChanged(MachineInstr *OldMI,
597 MachineInstr *NewMI) {
598 // If the instruction defines any virtual registers, update the VarInfo,
599 // kill and dead information for the instruction.
600 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
601 MachineOperand &MO = OldMI->getOperand(i);
602 if (MO.isRegister() && MO.getReg() &&
603 MRegisterInfo::isVirtualRegister(MO.getReg())) {
604 unsigned Reg = MO.getReg();
605 VarInfo &VI = getVarInfo(Reg);
606 if (MO.isDef()) {
607 if (MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000608 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 addVirtualRegisterDead(Reg, NewMI);
610 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000612 if (MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000613 MO.setIsKill(false);
Dan Gohman2c6a6422007-07-20 23:17:34 +0000614 addVirtualRegisterKilled(Reg, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 }
Dan Gohman2c6a6422007-07-20 23:17:34 +0000616 // If this is a kill of the value, update the VI kills list.
617 if (VI.removeKill(OldMI))
618 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 }
620 }
621}
622
Evan Chengcecc8222007-11-17 00:40:40 +0000623/// transferKillDeadInfo - Similar to instructionChanged except it does not
624/// update live variables internal data structures.
625void LiveVariables::transferKillDeadInfo(MachineInstr *OldMI,
626 MachineInstr *NewMI,
627 const MRegisterInfo *RegInfo) {
628 // If the instruction defines any virtual registers, update the VarInfo,
629 // kill and dead information for the instruction.
630 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
631 MachineOperand &MO = OldMI->getOperand(i);
632 if (MO.isRegister() && MO.getReg() &&
633 MRegisterInfo::isVirtualRegister(MO.getReg())) {
634 unsigned Reg = MO.getReg();
635 if (MO.isDef()) {
636 if (MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000637 MO.setIsDead(false);
Evan Chengcecc8222007-11-17 00:40:40 +0000638 addRegisterDead(Reg, NewMI, RegInfo);
639 }
640 }
641 if (MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000642 MO.setIsKill(false);
Evan Chengcecc8222007-11-17 00:40:40 +0000643 addRegisterKilled(Reg, NewMI, RegInfo);
644 }
645 }
646 }
647}
648
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650/// removeVirtualRegistersKilled - Remove all killed info for the specified
651/// instruction.
652void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
653 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
654 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000655 if (MO.isRegister() && MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000656 MO.setIsKill(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 unsigned Reg = MO.getReg();
658 if (MRegisterInfo::isVirtualRegister(Reg)) {
659 bool removed = getVarInfo(Reg).removeKill(MI);
660 assert(removed && "kill not in register's VarInfo?");
661 }
662 }
663 }
664}
665
666/// removeVirtualRegistersDead - Remove all of the dead registers for the
667/// specified instruction from the live variable information.
668void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
669 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
670 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000671 if (MO.isRegister() && MO.isDead()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000672 MO.setIsDead(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 unsigned Reg = MO.getReg();
674 if (MRegisterInfo::isVirtualRegister(Reg)) {
675 bool removed = getVarInfo(Reg).removeKill(MI);
676 assert(removed && "kill not in register's VarInfo?");
677 }
678 }
679 }
680}
681
682/// analyzePHINodes - Gather information about the PHI nodes in here. In
683/// particular, we want to map the variable information of a virtual
684/// register which is used in a PHI node. We map that to the BB the vreg is
685/// coming from.
686///
687void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
688 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
689 I != E; ++I)
690 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
691 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
692 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Chris Lattner6017d482007-12-30 23:10:15 +0000693 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()].
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 push_back(BBI->getOperand(i).getReg());
695}