blob: 82016ed591246dcaf4134baa79714f0d864375bd [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"
Owen Anderson9a4cb152008-06-27 07:05:59 +000037#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/ADT/STLExtras.h"
39#include "llvm/Config/alloca.h"
40#include <algorithm>
41using namespace llvm;
42
43char LiveVariables::ID = 0;
44static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
45
46void LiveVariables::VarInfo::dump() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 cerr << " Alive in blocks: ";
48 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
49 if (AliveBlocks[i]) cerr << i << ", ";
Owen Anderson721b2cc2007-11-08 01:20:48 +000050 cerr << " Used in blocks: ";
51 for (unsigned i = 0, e = UsedBlocks.size(); i != e; ++i)
52 if (UsedBlocks[i]) cerr << i << ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 cerr << "\n Killed by:";
54 if (Kills.empty())
55 cerr << " No instructions.\n";
56 else {
57 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
58 cerr << "\n #" << i << ": " << *Kills[i];
59 cerr << "\n";
60 }
61}
62
Bill Wendlingb88bca92008-02-20 06:10:21 +000063/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Dan Gohman1e57df32008-02-10 18:45:23 +000065 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 "getVarInfo: not a virtual register!");
Dan Gohman1e57df32008-02-10 18:45:23 +000067 RegIdx -= TargetRegisterInfo::FirstVirtualRegister;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 if (RegIdx >= VirtRegInfo.size()) {
69 if (RegIdx >= 2*VirtRegInfo.size())
70 VirtRegInfo.resize(RegIdx*2);
71 else
72 VirtRegInfo.resize(2*VirtRegInfo.size());
73 }
74 VarInfo &VI = VirtRegInfo[RegIdx];
75 VI.AliveBlocks.resize(MF->getNumBlockIDs());
Owen Anderson721b2cc2007-11-08 01:20:48 +000076 VI.UsedBlocks.resize(MF->getNumBlockIDs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 return VI;
78}
79
Owen Anderson77d80492008-01-15 22:58:11 +000080void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
81 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 MachineBasicBlock *MBB,
83 std::vector<MachineBasicBlock*> &WorkList) {
84 unsigned BBNum = MBB->getNumber();
Owen Anderson92a609a2008-01-15 22:02:46 +000085
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 // Check to see if this basic block is one of the killing blocks. If so,
Bill Wendlingb88bca92008-02-20 06:10:21 +000087 // remove it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
89 if (VRInfo.Kills[i]->getParent() == MBB) {
90 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
91 break;
92 }
Owen Anderson92a609a2008-01-15 22:02:46 +000093
Owen Anderson77d80492008-01-15 22:58:11 +000094 if (MBB == DefBlock) return; // Terminate recursion
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 if (VRInfo.AliveBlocks[BBNum])
97 return; // We already know the block is live
98
99 // Mark the variable known alive in this bb
100 VRInfo.AliveBlocks[BBNum] = true;
101
102 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
103 E = MBB->pred_rend(); PI != E; ++PI)
104 WorkList.push_back(*PI);
105}
106
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000107void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Owen Anderson77d80492008-01-15 22:58:11 +0000108 MachineBasicBlock *DefBlock,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 MachineBasicBlock *MBB) {
110 std::vector<MachineBasicBlock*> WorkList;
Owen Anderson77d80492008-01-15 22:58:11 +0000111 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 while (!WorkList.empty()) {
114 MachineBasicBlock *Pred = WorkList.back();
115 WorkList.pop_back();
Owen Anderson77d80492008-01-15 22:58:11 +0000116 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 }
118}
119
Owen Anderson92a609a2008-01-15 22:02:46 +0000120void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 MachineInstr *MI) {
Evan Cheng251fa152008-04-02 18:04:08 +0000122 assert(MRI->getVRegDef(reg) && "Register use before def!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
Owen Anderson721b2cc2007-11-08 01:20:48 +0000124 unsigned BBNum = MBB->getNumber();
125
Owen Anderson92a609a2008-01-15 22:02:46 +0000126 VarInfo& VRInfo = getVarInfo(reg);
Owen Anderson721b2cc2007-11-08 01:20:48 +0000127 VRInfo.UsedBlocks[BBNum] = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 VRInfo.NumUses++;
129
Bill Wendlingb88bca92008-02-20 06:10:21 +0000130 // Check to see if this basic block is already a kill block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000132 // Yes, this register is killed in this basic block already. Increase the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 // live range by updating the kill instruction.
134 VRInfo.Kills.back() = MI;
135 return;
136 }
137
138#ifndef NDEBUG
139 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
140 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
141#endif
142
Bill Wendling09d55662008-06-23 23:41:14 +0000143 // This situation can occur:
144 //
145 // ,------.
146 // | |
147 // | v
148 // | t2 = phi ... t1 ...
149 // | |
150 // | v
151 // | t1 = ...
152 // | ... = ... t1 ...
153 // | |
154 // `------'
155 //
156 // where there is a use in a PHI node that's a predecessor to the defining
157 // block. We don't want to mark all predecessors as having the value "alive"
158 // in this case.
159 if (MBB == MRI->getVRegDef(reg)->getParent()) return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Bill Wendlingb88bca92008-02-20 06:10:21 +0000161 // Add a new kill entry for this basic block. If this virtual register is
162 // already marked as alive in this basic block, that means it is alive in at
163 // least one of the successor blocks, it's not a kill.
Owen Anderson721b2cc2007-11-08 01:20:48 +0000164 if (!VRInfo.AliveBlocks[BBNum])
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 VRInfo.Kills.push_back(MI);
166
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000167 // Update all dominating blocks to mark them as "known live".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
169 E = MBB->pred_end(); PI != E; ++PI)
Evan Cheng251fa152008-04-02 18:04:08 +0000170 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171}
172
Evan Cheng1c3ee662008-04-16 09:46:40 +0000173/// FindLastPartialDef - Return the last partial def of the specified register.
174/// Also returns the sub-register that's defined.
175MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg,
176 unsigned &PartDefReg) {
177 unsigned LastDefReg = 0;
178 unsigned LastDefDist = 0;
179 MachineInstr *LastDef = NULL;
180 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
181 unsigned SubReg = *SubRegs; ++SubRegs) {
182 MachineInstr *Def = PhysRegDef[SubReg];
183 if (!Def)
184 continue;
185 unsigned Dist = DistanceMap[Def];
186 if (Dist > LastDefDist) {
187 LastDefReg = SubReg;
188 LastDef = Def;
189 LastDefDist = Dist;
190 }
191 }
192 PartDefReg = LastDefReg;
193 return LastDef;
194}
195
Bill Wendling85b03762008-02-20 09:15:16 +0000196/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
197/// implicit defs to a machine instruction if there was an earlier def of its
198/// super-register.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000200 // If there was a previous use or a "full" def all is well.
201 if (!PhysRegDef[Reg] && !PhysRegUse[Reg]) {
202 // Otherwise, the last sub-register def implicitly defines this register.
203 // e.g.
204 // AH =
205 // AL = ... <imp-def EAX>, <imp-kill AH>
206 // = AH
207 // ...
208 // = EAX
209 // All of the sub-registers must have been defined before the use of Reg!
210 unsigned PartDefReg = 0;
211 MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefReg);
212 // If LastPartialDef is NULL, it must be using a livein register.
213 if (LastPartialDef) {
214 LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
215 true/*IsImp*/));
216 PhysRegDef[Reg] = LastPartialDef;
217 std::set<unsigned> Processed;
218 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
219 unsigned SubReg = *SubRegs; ++SubRegs) {
220 if (Processed.count(SubReg))
221 continue;
222 if (SubReg == PartDefReg || TRI->isSubRegister(PartDefReg, SubReg))
223 continue;
224 // This part of Reg was defined before the last partial def. It's killed
225 // here.
226 LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
227 false/*IsDef*/,
228 true/*IsImp*/));
229 PhysRegDef[SubReg] = LastPartialDef;
230 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
231 Processed.insert(*SS);
232 }
233 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
Bill Wendlingb88bca92008-02-20 06:10:21 +0000235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 // There was an earlier def of a super-register. Add implicit def to that MI.
Bill Wendling85b03762008-02-20 09:15:16 +0000237 //
238 // A: EAX = ...
239 // B: ... = AX
240 //
Evan Cheng1c3ee662008-04-16 09:46:40 +0000241 // Add implicit def to A if there isn't a use of AX (or EAX) before B.
242 if (!PhysRegUse[Reg]) {
243 MachineInstr *Def = PhysRegDef[Reg];
244 if (Def && !Def->modifiesRegister(Reg))
Bill Wendling85b03762008-02-20 09:15:16 +0000245 Def->addOperand(MachineOperand::CreateReg(Reg,
246 true /*IsDef*/,
247 true /*IsImp*/));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000249
250 // Remember this use.
251 PhysRegUse[Reg] = MI;
Evan Chengc7daf1f2008-03-05 00:59:57 +0000252 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000253 unsigned SubReg = *SubRegs; ++SubRegs)
Evan Cheng1c3ee662008-04-16 09:46:40 +0000254 PhysRegUse[SubReg] = MI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255}
256
Evan Cheng97a51302008-03-19 00:52:20 +0000257/// hasRegisterUseBelow - Return true if the specified register is used after
258/// the current instruction and before it's next definition.
259bool LiveVariables::hasRegisterUseBelow(unsigned Reg,
260 MachineBasicBlock::iterator I,
261 MachineBasicBlock *MBB) {
262 if (I == MBB->end())
263 return false;
Evan Cheng251fa152008-04-02 18:04:08 +0000264
265 // First find out if there are any uses / defs below.
266 bool hasDistInfo = true;
267 unsigned CurDist = DistanceMap[I];
268 SmallVector<MachineInstr*, 4> Uses;
269 SmallVector<MachineInstr*, 4> Defs;
270 for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg),
271 RE = MRI->reg_end(); RI != RE; ++RI) {
272 MachineOperand &UDO = RI.getOperand();
273 MachineInstr *UDMI = &*RI;
274 if (UDMI->getParent() != MBB)
275 continue;
276 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
277 bool isBelow = false;
278 if (DI == DistanceMap.end()) {
279 // Must be below if it hasn't been assigned a distance yet.
280 isBelow = true;
281 hasDistInfo = false;
282 } else if (DI->second > CurDist)
283 isBelow = true;
284 if (isBelow) {
285 if (UDO.isUse())
286 Uses.push_back(UDMI);
287 if (UDO.isDef())
288 Defs.push_back(UDMI);
Evan Cheng97a51302008-03-19 00:52:20 +0000289 }
290 }
Evan Cheng251fa152008-04-02 18:04:08 +0000291
292 if (Uses.empty())
293 // No uses below.
294 return false;
295 else if (!Uses.empty() && Defs.empty())
296 // There are uses below but no defs below.
297 return true;
298 // There are both uses and defs below. We need to know which comes first.
299 if (!hasDistInfo) {
300 // Complete DistanceMap for this MBB. This information is computed only
301 // once per MBB.
302 ++I;
303 ++CurDist;
304 for (MachineBasicBlock::iterator E = MBB->end(); I != E; ++I, ++CurDist)
305 DistanceMap.insert(std::make_pair(I, CurDist));
306 }
307
Evan Cheng1c3ee662008-04-16 09:46:40 +0000308 unsigned EarliestUse = DistanceMap[Uses[0]];
309 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
Evan Cheng251fa152008-04-02 18:04:08 +0000310 unsigned Dist = DistanceMap[Uses[i]];
311 if (Dist < EarliestUse)
312 EarliestUse = Dist;
313 }
314 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
315 unsigned Dist = DistanceMap[Defs[i]];
316 if (Dist < EarliestUse)
317 // The register is defined before its first use below.
318 return false;
319 }
320 return true;
Evan Cheng97a51302008-03-19 00:52:20 +0000321}
322
Evan Cheng1c3ee662008-04-16 09:46:40 +0000323bool LiveVariables::HandlePhysRegKill(unsigned Reg) {
324 if (!PhysRegUse[Reg] && !PhysRegDef[Reg])
325 return false;
326
327 MachineInstr *LastRefOrPartRef = PhysRegUse[Reg]
328 ? PhysRegUse[Reg] : PhysRegDef[Reg];
329 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
330 // The whole register is used.
331 // AL =
332 // AH =
333 //
334 // = AX
335 // = AL, AX<imp-use, kill>
336 // AX =
337 //
338 // Or whole register is defined, but not used at all.
339 // AX<dead> =
340 // ...
341 // AX =
342 //
343 // Or whole register is defined, but only partly used.
344 // AX<dead> = AL<imp-def>
345 // = AL<kill>
346 // AX =
347 std::set<unsigned> PartUses;
348 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
349 unsigned SubReg = *SubRegs; ++SubRegs) {
350 if (MachineInstr *Use = PhysRegUse[SubReg]) {
351 PartUses.insert(SubReg);
352 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
353 PartUses.insert(*SS);
354 unsigned Dist = DistanceMap[Use];
355 if (Dist > LastRefOrPartRefDist) {
356 LastRefOrPartRefDist = Dist;
357 LastRefOrPartRef = Use;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000359 }
360 }
361 if (LastRefOrPartRef == PhysRegDef[Reg])
362 // Not used at all.
363 LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
364
365 /* Partial uses. Mark register def dead and add implicit def of
366 sub-registers which are used.
367 FIXME: LiveIntervalAnalysis can't handle this yet!
368 EAX<dead> = op AL<imp-def>
369 That is, EAX def is dead but AL def extends pass it.
370 Enable this after live interval analysis is fixed to improve codegen!
371 else if (!PhysRegUse[Reg]) {
372 PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
373 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
374 unsigned SubReg = *SubRegs; ++SubRegs) {
375 if (PartUses.count(SubReg)) {
376 PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
377 true, true));
378 LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
379 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
380 PartUses.erase(*SS);
381 }
382 }
383 } */
384 else
385 LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
386 return true;
387}
388
389void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
390 // What parts of the register are previously defined?
Owen Anderson9a4cb152008-06-27 07:05:59 +0000391 SmallSet<unsigned, 32> Live;
Evan Cheng1c3ee662008-04-16 09:46:40 +0000392 if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
393 Live.insert(Reg);
394 for (const unsigned *SS = TRI->getSubRegisters(Reg); *SS; ++SS)
395 Live.insert(*SS);
396 } else {
397 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
398 unsigned SubReg = *SubRegs; ++SubRegs) {
399 // If a register isn't itself defined, but all parts that make up of it
400 // are defined, then consider it also defined.
401 // e.g.
402 // AL =
403 // AH =
404 // = AX
405 if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
406 Live.insert(SubReg);
407 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
408 Live.insert(*SS);
409 }
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000410 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 }
412
Evan Cheng1c3ee662008-04-16 09:46:40 +0000413 // Start from the largest piece, find the last time any part of the register
414 // is referenced.
415 if (!HandlePhysRegKill(Reg)) {
416 // Only some of the sub-registers are used.
417 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
418 unsigned SubReg = *SubRegs; ++SubRegs) {
419 if (!Live.count(SubReg))
420 // Skip if this sub-register isn't defined.
421 continue;
422 if (HandlePhysRegKill(SubReg)) {
423 Live.erase(SubReg);
424 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS)
425 Live.erase(*SS);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000426 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 }
Evan Cheng1c3ee662008-04-16 09:46:40 +0000428 assert(Live.empty() && "Not all defined registers are killed / dead?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 }
430
431 if (MI) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000432 // Does this extend the live range of a super-register?
433 std::set<unsigned> Processed;
Evan Chengc7daf1f2008-03-05 00:59:57 +0000434 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000436 if (Processed.count(SuperReg))
437 continue;
438 MachineInstr *LastRef = PhysRegUse[SuperReg]
439 ? PhysRegUse[SuperReg] : PhysRegDef[SuperReg];
440 if (LastRef && LastRef != MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 // The larger register is previously defined. Now a smaller part is
Evan Cheng97a51302008-03-19 00:52:20 +0000442 // being re-defined. Treat it as read/mod/write if there are uses
443 // below.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 // EAX =
445 // AX = EAX<imp-use,kill>, EAX<imp-def>
Evan Cheng97a51302008-03-19 00:52:20 +0000446 // ...
447 /// = EAX
Evan Cheng1c3ee662008-04-16 09:46:40 +0000448 if (hasRegisterUseBelow(SuperReg, MI, MI->getParent())) {
Evan Cheng97a51302008-03-19 00:52:20 +0000449 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
Evan Cheng1c3ee662008-04-16 09:46:40 +0000450 true/*IsImp*/,true/*IsKill*/));
Evan Cheng97a51302008-03-19 00:52:20 +0000451 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
452 true/*IsImp*/));
Evan Cheng1c3ee662008-04-16 09:46:40 +0000453 PhysRegDef[SuperReg] = MI;
454 PhysRegUse[SuperReg] = NULL;
455 Processed.insert(SuperReg);
456 for (const unsigned *SS = TRI->getSubRegisters(SuperReg); *SS; ++SS) {
457 PhysRegDef[*SS] = MI;
458 PhysRegUse[*SS] = NULL;
459 Processed.insert(*SS);
460 }
Evan Cheng97a51302008-03-19 00:52:20 +0000461 } else {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000462 // Otherwise, the super register is killed.
463 if (HandlePhysRegKill(SuperReg)) {
464 PhysRegDef[SuperReg] = NULL;
465 PhysRegUse[SuperReg] = NULL;
466 for (const unsigned *SS = TRI->getSubRegisters(SuperReg); *SS; ++SS) {
467 PhysRegDef[*SS] = NULL;
468 PhysRegUse[*SS] = NULL;
469 Processed.insert(*SS);
470 }
471 }
Evan Cheng97a51302008-03-19 00:52:20 +0000472 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 }
474 }
475
Evan Cheng1c3ee662008-04-16 09:46:40 +0000476 // Remember this def.
477 PhysRegDef[Reg] = MI;
478 PhysRegUse[Reg] = NULL;
Evan Chengc7daf1f2008-03-05 00:59:57 +0000479 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 unsigned SubReg = *SubRegs; ++SubRegs) {
Evan Cheng1c3ee662008-04-16 09:46:40 +0000481 PhysRegDef[SubReg] = MI;
482 PhysRegUse[SubReg] = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 }
484 }
485}
486
487bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
488 MF = &mf;
Evan Cheng251fa152008-04-02 18:04:08 +0000489 MRI = &mf.getRegInfo();
Evan Chengc7daf1f2008-03-05 00:59:57 +0000490 TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491
Evan Chengc7daf1f2008-03-05 00:59:57 +0000492 ReservedRegisters = TRI->getReservedRegs(mf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493
Evan Chengc7daf1f2008-03-05 00:59:57 +0000494 unsigned NumRegs = TRI->getNumRegs();
Evan Cheng1c3ee662008-04-16 09:46:40 +0000495 PhysRegDef = new MachineInstr*[NumRegs];
496 PhysRegUse = new MachineInstr*[NumRegs];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
Evan Cheng1c3ee662008-04-16 09:46:40 +0000498 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
499 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500
Bill Wendling85b03762008-02-20 09:15:16 +0000501 /// Get some space for a respectable number of registers.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 VirtRegInfo.resize(64);
503
504 analyzePHINodes(mf);
505
506 // Calculate live variable information in depth first order on the CFG of the
507 // function. This guarantees that we will see the definition of a virtual
508 // register before its uses due to dominance properties of SSA (except for PHI
509 // nodes, which are treated as a special case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 MachineBasicBlock *Entry = MF->begin();
511 SmallPtrSet<MachineBasicBlock*,16> Visited;
Bill Wendling85b03762008-02-20 09:15:16 +0000512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
514 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
515 DFI != E; ++DFI) {
516 MachineBasicBlock *MBB = *DFI;
517
518 // Mark live-in registers as live-in.
519 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
520 EE = MBB->livein_end(); II != EE; ++II) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000521 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 "Cannot have a live-in virtual register!");
523 HandlePhysRegDef(*II, 0);
524 }
525
526 // Loop over all of the instructions, processing them.
Evan Cheng251fa152008-04-02 18:04:08 +0000527 DistanceMap.clear();
528 unsigned Dist = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
530 I != E; ++I) {
531 MachineInstr *MI = I;
Evan Cheng251fa152008-04-02 18:04:08 +0000532 DistanceMap.insert(std::make_pair(MI, Dist++));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533
534 // Process all of the operands of the instruction...
535 unsigned NumOperandsToProcess = MI->getNumOperands();
536
537 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
538 // of the uses. They will be handled in other basic blocks.
539 if (MI->getOpcode() == TargetInstrInfo::PHI)
540 NumOperandsToProcess = 1;
541
Evan Cheng1c3ee662008-04-16 09:46:40 +0000542 SmallVector<unsigned, 4> UseRegs;
543 SmallVector<unsigned, 4> DefRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000545 const MachineOperand &MO = MI->getOperand(i);
Evan Cheng1c3ee662008-04-16 09:46:40 +0000546 if (MO.isRegister() && MO.getReg()) {
Bill Wendlingb88bca92008-02-20 06:10:21 +0000547 unsigned MOReg = MO.getReg();
Evan Cheng1c3ee662008-04-16 09:46:40 +0000548 if (!MOReg)
549 continue;
550 if (MO.isUse())
551 UseRegs.push_back(MOReg);
552 if (MO.isDef())
553 DefRegs.push_back(MOReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 }
555 }
556
Evan Cheng1c3ee662008-04-16 09:46:40 +0000557 // Process all uses.
558 for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) {
559 unsigned MOReg = UseRegs[i];
560 if (TargetRegisterInfo::isVirtualRegister(MOReg))
561 HandleVirtRegUse(MOReg, MBB, MI);
562 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
563 !ReservedRegisters[MOReg])
564 HandlePhysRegUse(MOReg, MI);
565 }
566
Bill Wendling85b03762008-02-20 09:15:16 +0000567 // Process all defs.
Evan Cheng1c3ee662008-04-16 09:46:40 +0000568 for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
569 unsigned MOReg = DefRegs[i];
570 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
571 VarInfo &VRInfo = getVarInfo(MOReg);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000572
Evan Cheng1c3ee662008-04-16 09:46:40 +0000573 if (VRInfo.AliveBlocks.none())
574 // If vr is not alive in any block, then defaults to dead.
575 VRInfo.Kills.push_back(MI);
576 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
577 !ReservedRegisters[MOReg]) {
578 HandlePhysRegDef(MOReg, MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
580 }
581 }
582
583 // Handle any virtual assignments from PHI nodes which might be at the
584 // bottom of this basic block. We check all of our successor blocks to see
585 // if they have PHI nodes, and if so, we simulate an assignment at the end
586 // of the current block.
587 if (!PHIVarInfo[MBB->getNumber()].empty()) {
588 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
589
590 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000591 E = VarInfoVec.end(); I != E; ++I)
592 // Mark it alive only in the block we are representing.
Evan Cheng251fa152008-04-02 18:04:08 +0000593 MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
Owen Anderson77d80492008-01-15 22:58:11 +0000594 MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 }
596
Bill Wendling85b03762008-02-20 09:15:16 +0000597 // Finally, if the last instruction in the block is a return, make sure to
598 // mark it as using all of the live-out values in the function.
Chris Lattner5b930372008-01-07 07:27:27 +0000599 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 MachineInstr *Ret = &MBB->back();
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000601
Chris Lattner1b989192007-12-31 04:13:23 +0000602 for (MachineRegisterInfo::liveout_iterator
603 I = MF->getRegInfo().liveout_begin(),
604 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000605 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
Dan Gohman2d702012008-06-25 22:14:43 +0000606 "Cannot have a live-out virtual register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 HandlePhysRegUse(*I, Ret);
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000608
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 // Add live-out registers as implicit uses.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000610 if (!Ret->readsRegister(*I))
Chris Lattner63ab1f22007-12-30 00:41:17 +0000611 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
613 }
614
Evan Cheng1c3ee662008-04-16 09:46:40 +0000615 // Loop over PhysRegDef / PhysRegUse, killing any registers that are
616 // available at the end of the basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 for (unsigned i = 0; i != NumRegs; ++i)
Evan Cheng1c3ee662008-04-16 09:46:40 +0000618 if (PhysRegDef[i] || PhysRegUse[i])
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 HandlePhysRegDef(i, 0);
620
Evan Cheng1c3ee662008-04-16 09:46:40 +0000621 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0);
622 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 }
624
625 // Convert and transfer the dead / killed information we have gathered into
626 // VirtRegInfo onto MI's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000628 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
629 if (VirtRegInfo[i].Kills[j] ==
Evan Cheng251fa152008-04-02 18:04:08 +0000630 MRI->getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000631 VirtRegInfo[i]
632 .Kills[j]->addRegisterDead(i +
633 TargetRegisterInfo::FirstVirtualRegister,
Evan Chengc7daf1f2008-03-05 00:59:57 +0000634 TRI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 else
Bill Wendling0fa65bd2008-02-20 07:36:31 +0000636 VirtRegInfo[i]
637 .Kills[j]->addRegisterKilled(i +
638 TargetRegisterInfo::FirstVirtualRegister,
Evan Chengc7daf1f2008-03-05 00:59:57 +0000639 TRI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640
641 // Check to make sure there are no unreachable blocks in the MC CFG for the
642 // function. If so, it is due to a bug in the instruction selector or some
643 // other part of the code generator if this happens.
644#ifndef NDEBUG
645 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
646 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
647#endif
648
Evan Cheng1c3ee662008-04-16 09:46:40 +0000649 delete[] PhysRegDef;
650 delete[] PhysRegUse;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 delete[] PHIVarInfo;
652
653 return false;
654}
655
Evan Chengd1c7e8f2008-07-03 00:07:19 +0000656/// replaceKillInstruction - Update register kill info by replacing a kill
657/// instruction with a new one.
658void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr *OldMI,
659 MachineInstr *NewMI) {
660 VarInfo &VI = getVarInfo(Reg);
Evan Chengc2c8ebb2008-07-03 00:28:27 +0000661 std::replace(VI.Kills.begin(), VI.Kills.end(), OldMI, NewMI);
Evan Chengd1c7e8f2008-07-03 00:07:19 +0000662}
663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664/// removeVirtualRegistersKilled - Remove all killed info for the specified
665/// instruction.
666void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
667 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
668 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000669 if (MO.isRegister() && MO.isKill()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000670 MO.setIsKill(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 unsigned Reg = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000672 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 bool removed = getVarInfo(Reg).removeKill(MI);
674 assert(removed && "kill not in register's VarInfo?");
675 }
676 }
677 }
678}
679
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680/// analyzePHINodes - Gather information about the PHI nodes in here. In
Bill Wendling85b03762008-02-20 09:15:16 +0000681/// particular, we want to map the variable information of a virtual register
682/// which is used in a PHI node. We map that to the BB the vreg is coming from.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683///
684void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
685 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
686 I != E; ++I)
687 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
688 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
689 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Bill Wendlingb88bca92008-02-20 06:10:21 +0000690 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
691 .push_back(BBI->getOperand(i).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692}