blob: 5a73131b3e472f47ff7ff46f9309638ea86d3af2 [file] [log] [blame]
Chris Lattnerbc40e892003-01-13 20:01:16 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattner5cdfbad2003-05-07 20:08:36 +000010// This file implements the LiveVariable analysis pass. For each machine
11// instruction in the function, this pass calculates the set of registers that
12// are immediately dead after the instruction (i.e., the instruction calculates
13// the value, but it is never used) and the set of registers that are used by
14// the instruction, but are never used after the instruction (i.e., they are
15// killed).
16//
17// This class computes live variables using are sparse implementation based on
18// the machine code SSA form. This class computes live variable information for
19// each virtual and _register allocatable_ physical register in a function. It
20// uses the dominance properties of SSA form to efficiently compute live
21// variables for virtual registers, and assumes that physical registers are only
22// live within a single basic block (allowing it to do a single local analysis
23// to resolve physical register lifetimes in each basic block). If a physical
24// register is not register allocatable, it is not tracked. This is useful for
25// things like the stack pointer and condition codes.
26//
Chris Lattnerbc40e892003-01-13 20:01:16 +000027//===----------------------------------------------------------------------===//
28
29#include "llvm/CodeGen/LiveVariables.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner61b08f12004-02-10 21:18:55 +000031#include "llvm/Target/MRegisterInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000032#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000033#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/ADT/DepthFirstIterator.h"
35#include "llvm/ADT/STLExtras.h"
Chris Lattner6fcd8d82004-10-25 18:44:14 +000036#include "llvm/Config/alloca.h"
Chris Lattner657b4d12005-08-24 00:09:33 +000037#include <algorithm>
Chris Lattnerdacceef2006-01-04 05:40:30 +000038#include <iostream>
Chris Lattner49a5aaa2004-01-30 22:08:53 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattner5d8925c2006-08-27 22:30:17 +000041static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
Chris Lattnerbc40e892003-01-13 20:01:16 +000042
Chris Lattnerdacceef2006-01-04 05:40:30 +000043void LiveVariables::VarInfo::dump() const {
44 std::cerr << "Register Defined by: ";
45 if (DefInst)
46 std::cerr << *DefInst;
47 else
48 std::cerr << "<null>\n";
49 std::cerr << " Alive in blocks: ";
50 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
51 if (AliveBlocks[i]) std::cerr << i << ", ";
52 std::cerr << "\n Killed by:";
53 if (Kills.empty())
54 std::cerr << " No instructions.\n";
55 else {
56 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
57 std::cerr << "\n #" << i << ": " << *Kills[i];
58 std::cerr << "\n";
59 }
60}
61
Chris Lattnerfb2cb692003-05-12 14:24:00 +000062LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Chris Lattneref09c632004-01-31 21:27:19 +000063 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000064 "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 return VirtRegInfo[RegIdx];
73}
74
Chris Lattner657b4d12005-08-24 00:09:33 +000075bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
76 std::map<MachineInstr*, std::vector<unsigned> >::const_iterator I =
77 RegistersKilled.find(MI);
78 if (I == RegistersKilled.end()) return false;
79
80 // Do a binary search, as these lists can grow pretty big, particularly for
81 // call instructions on targets with lots of call-clobbered registers.
82 return std::binary_search(I->second.begin(), I->second.end(), Reg);
83}
84
85bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
86 std::map<MachineInstr*, std::vector<unsigned> >::const_iterator I =
87 RegistersDead.find(MI);
88 if (I == RegistersDead.end()) return false;
89
90 // Do a binary search, as these lists can grow pretty big, particularly for
91 // call instructions on targets with lots of call-clobbered registers.
92 return std::binary_search(I->second.begin(), I->second.end(), Reg);
93}
Chris Lattnerfb2cb692003-05-12 14:24:00 +000094
Chris Lattnerbc40e892003-01-13 20:01:16 +000095void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Misha Brukman09ba9062004-06-24 21:31:16 +000096 MachineBasicBlock *MBB) {
Chris Lattner8ba97712004-07-01 04:29:47 +000097 unsigned BBNum = MBB->getNumber();
Chris Lattnerbc40e892003-01-13 20:01:16 +000098
99 // Check to see if this basic block is one of the killing blocks. If so,
100 // remove it...
101 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000102 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000103 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
104 break;
105 }
106
Chris Lattner73d4adf2004-07-19 06:26:50 +0000107 if (MBB == VRInfo.DefInst->getParent()) return; // Terminate recursion
Chris Lattnerbc40e892003-01-13 20:01:16 +0000108
109 if (VRInfo.AliveBlocks.size() <= BBNum)
110 VRInfo.AliveBlocks.resize(BBNum+1); // Make space...
111
112 if (VRInfo.AliveBlocks[BBNum])
113 return; // We already know the block is live
114
115 // Mark the variable known alive in this bb
116 VRInfo.AliveBlocks[BBNum] = true;
117
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000118 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
119 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000120 MarkVirtRegAliveInBlock(VRInfo, *PI);
121}
122
123void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
Misha Brukman09ba9062004-06-24 21:31:16 +0000124 MachineInstr *MI) {
Alkis Evlogimenos2e58a412004-09-01 22:34:52 +0000125 assert(VRInfo.DefInst && "Register use before def!");
126
Chris Lattnerbc40e892003-01-13 20:01:16 +0000127 // Check to see if this basic block is already a kill block...
Chris Lattner74de8b12004-07-19 07:04:55 +0000128 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000129 // Yes, this register is killed in this basic block already. Increase the
130 // live range by updating the kill instruction.
Chris Lattner74de8b12004-07-19 07:04:55 +0000131 VRInfo.Kills.back() = MI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000132 return;
133 }
134
135#ifndef NDEBUG
136 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000137 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000138#endif
139
Misha Brukmanedf128a2005-04-21 22:36:52 +0000140 assert(MBB != VRInfo.DefInst->getParent() &&
Chris Lattner73d4adf2004-07-19 06:26:50 +0000141 "Should have kill for defblock!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000142
143 // Add a new kill entry for this basic block.
Chris Lattner74de8b12004-07-19 07:04:55 +0000144 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000145
146 // Update all dominating blocks to mark them known live.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000147 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
148 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000149 MarkVirtRegAliveInBlock(VRInfo, *PI);
150}
151
152void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000153 PhysRegInfo[Reg] = MI;
154 PhysRegUsed[Reg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000155
156 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
157 unsigned Alias = *AliasSet; ++AliasSet) {
158 PhysRegInfo[Alias] = MI;
159 PhysRegUsed[Alias] = true;
160 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000161}
162
163void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
164 // Does this kill a previous version of this register?
165 if (MachineInstr *LastUse = PhysRegInfo[Reg]) {
166 if (PhysRegUsed[Reg])
Chris Lattner44b94c22005-08-23 23:42:17 +0000167 RegistersKilled[LastUse].push_back(Reg);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000168 else
Chris Lattner44b94c22005-08-23 23:42:17 +0000169 RegistersDead[LastUse].push_back(Reg);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000170 }
171 PhysRegInfo[Reg] = MI;
172 PhysRegUsed[Reg] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000173
174 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
Chris Lattner6d3848d2004-05-10 05:12:43 +0000175 unsigned Alias = *AliasSet; ++AliasSet) {
Chris Lattner49948772004-02-09 01:43:23 +0000176 if (MachineInstr *LastUse = PhysRegInfo[Alias]) {
177 if (PhysRegUsed[Alias])
Chris Lattner44b94c22005-08-23 23:42:17 +0000178 RegistersKilled[LastUse].push_back(Alias);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000179 else
Chris Lattner44b94c22005-08-23 23:42:17 +0000180 RegistersDead[LastUse].push_back(Alias);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000181 }
Chris Lattner49948772004-02-09 01:43:23 +0000182 PhysRegInfo[Alias] = MI;
183 PhysRegUsed[Alias] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000184 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000185}
186
187bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000188 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000189 RegInfo = MF.getTarget().getRegisterInfo();
190 assert(RegInfo && "Target doesn't have register information?");
191
Alkis Evlogimenos22a2f6d2004-08-26 22:23:32 +0000192 AllocatablePhysicalRegisters = RegInfo->getAllocatableSet(MF);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000193
Chris Lattnerbc40e892003-01-13 20:01:16 +0000194 // PhysRegInfo - Keep track of which instruction was the last use of a
195 // physical register. This is a purely local property, because all physical
196 // register references as presumed dead across basic blocks.
197 //
Misha Brukmanedf128a2005-04-21 22:36:52 +0000198 PhysRegInfo = (MachineInstr**)alloca(sizeof(MachineInstr*) *
Chris Lattner6fcd8d82004-10-25 18:44:14 +0000199 RegInfo->getNumRegs());
200 PhysRegUsed = (bool*)alloca(sizeof(bool)*RegInfo->getNumRegs());
201 std::fill(PhysRegInfo, PhysRegInfo+RegInfo->getNumRegs(), (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000202
Chris Lattnerbc40e892003-01-13 20:01:16 +0000203 /// Get some space for a respectable number of registers...
204 VirtRegInfo.resize(64);
Chris Lattnerd493b342005-04-09 15:23:25 +0000205
206 // Mark live-in registers as live-in.
Chris Lattner712ad0c2005-05-13 07:08:07 +0000207 for (MachineFunction::livein_iterator I = MF.livein_begin(),
Chris Lattnerd493b342005-04-09 15:23:25 +0000208 E = MF.livein_end(); I != E; ++I) {
Chris Lattner712ad0c2005-05-13 07:08:07 +0000209 assert(MRegisterInfo::isPhysicalRegister(I->first) &&
Chris Lattnerd493b342005-04-09 15:23:25 +0000210 "Cannot have a live-in virtual register!");
Chris Lattner712ad0c2005-05-13 07:08:07 +0000211 HandlePhysRegDef(I->first, 0);
Chris Lattnerd493b342005-04-09 15:23:25 +0000212 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000213
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000214 analyzePHINodes(MF);
215
Chris Lattnerbc40e892003-01-13 20:01:16 +0000216 // Calculate live variable information in depth first order on the CFG of the
217 // function. This guarantees that we will see the definition of a virtual
218 // register before its uses due to dominance properties of SSA (except for PHI
219 // nodes, which are treated as a special case).
220 //
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000221 MachineBasicBlock *Entry = MF.begin();
Chris Lattnera5287a62004-07-01 04:24:29 +0000222 std::set<MachineBasicBlock*> Visited;
223 for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited),
224 E = df_ext_end(Entry, Visited); DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000225 MachineBasicBlock *MBB = *DFI;
Chris Lattner8ba97712004-07-01 04:29:47 +0000226 unsigned BBNum = MBB->getNumber();
Chris Lattnerbc40e892003-01-13 20:01:16 +0000227
228 // Loop over all of the instructions, processing them.
229 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000230 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000231 MachineInstr *MI = I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000232 const TargetInstrDescriptor &MID = TII.get(MI->getOpcode());
233
234 // Process all of the operands of the instruction...
235 unsigned NumOperandsToProcess = MI->getNumOperands();
236
237 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
238 // of the uses. They will be handled in other basic blocks.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000239 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman09ba9062004-06-24 21:31:16 +0000240 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000241
242 // Loop over implicit uses, using them.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000243 if (MID.ImplicitUses) {
244 for (const unsigned *ImplicitUses = MID.ImplicitUses;
245 *ImplicitUses; ++ImplicitUses)
246 HandlePhysRegUse(*ImplicitUses, MI);
247 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000248
249 // Process all explicit uses...
250 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000251 MachineOperand &MO = MI->getOperand(i);
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000252 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000253 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
254 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
255 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000256 AllocatablePhysicalRegisters[MO.getReg()]) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000257 HandlePhysRegUse(MO.getReg(), MI);
258 }
259 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000260 }
261
262 // Loop over implicit defs, defining them.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000263 if (MID.ImplicitDefs) {
264 for (const unsigned *ImplicitDefs = MID.ImplicitDefs;
265 *ImplicitDefs; ++ImplicitDefs)
266 HandlePhysRegDef(*ImplicitDefs, MI);
267 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000268
269 // Process all explicit defs...
270 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000271 MachineOperand &MO = MI->getOperand(i);
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000272 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000273 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
274 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000275
Chris Lattner73d4adf2004-07-19 06:26:50 +0000276 assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
Misha Brukman09ba9062004-06-24 21:31:16 +0000277 VRInfo.DefInst = MI;
Chris Lattner472405e2004-07-19 06:55:21 +0000278 // Defaults to dead
Chris Lattner74de8b12004-07-19 07:04:55 +0000279 VRInfo.Kills.push_back(MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000280 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000281 AllocatablePhysicalRegisters[MO.getReg()]) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000282 HandlePhysRegDef(MO.getReg(), MI);
283 }
284 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000285 }
286 }
287
288 // Handle any virtual assignments from PHI nodes which might be at the
289 // bottom of this basic block. We check all of our successor blocks to see
290 // if they have PHI nodes, and if so, we simulate an assignment at the end
291 // of the current block.
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000292 if (!PHIVarInfo[MBB].empty()) {
293 std::vector<unsigned>& VarInfoVec = PHIVarInfo[MBB];
Misha Brukmanedf128a2005-04-21 22:36:52 +0000294
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000295 for (std::vector<unsigned>::iterator I = VarInfoVec.begin(),
296 E = VarInfoVec.end(); I != E; ++I) {
297 VarInfo& VRInfo = getVarInfo(*I);
298 assert(VRInfo.DefInst && "Register use before def (or no def)!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000299
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000300 // Only mark it alive only in the block we are representing.
301 MarkVirtRegAliveInBlock(VRInfo, MBB);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000302 }
303 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000304
Chris Lattnerd493b342005-04-09 15:23:25 +0000305 // Finally, if the last block in the function is a return, make sure to mark
306 // it as using all of the live-out values in the function.
307 if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) {
308 MachineInstr *Ret = &MBB->back();
Chris Lattner712ad0c2005-05-13 07:08:07 +0000309 for (MachineFunction::liveout_iterator I = MF.liveout_begin(),
Chris Lattnerd493b342005-04-09 15:23:25 +0000310 E = MF.liveout_end(); I != E; ++I) {
311 assert(MRegisterInfo::isPhysicalRegister(*I) &&
312 "Cannot have a live-in virtual register!");
313 HandlePhysRegUse(*I, Ret);
314 }
315 }
316
Chris Lattnerbc40e892003-01-13 20:01:16 +0000317 // Loop over PhysRegInfo, killing any registers that are available at the
318 // end of the basic block. This also resets the PhysRegInfo map.
Chris Lattner96aef892004-02-09 01:35:21 +0000319 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000320 if (PhysRegInfo[i])
Misha Brukman09ba9062004-06-24 21:31:16 +0000321 HandlePhysRegDef(i, 0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000322 }
323
Chris Lattnerbc40e892003-01-13 20:01:16 +0000324 // Convert the information we have gathered into VirtRegInfo and transform it
325 // into a form usable by RegistersKilled.
326 //
327 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
328 for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
Chris Lattner74de8b12004-07-19 07:04:55 +0000329 if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
Chris Lattner44b94c22005-08-23 23:42:17 +0000330 RegistersDead[VirtRegInfo[i].Kills[j]].push_back(
331 i + MRegisterInfo::FirstVirtualRegister);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000332
333 else
Chris Lattner44b94c22005-08-23 23:42:17 +0000334 RegistersKilled[VirtRegInfo[i].Kills[j]].push_back(
335 i + MRegisterInfo::FirstVirtualRegister);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000336 }
Chris Lattnera5287a62004-07-01 04:24:29 +0000337
Chris Lattner657b4d12005-08-24 00:09:33 +0000338 // Walk through the RegistersKilled/Dead sets, and sort the registers killed
339 // or dead. This allows us to use efficient binary search for membership
340 // testing.
341 for (std::map<MachineInstr*, std::vector<unsigned> >::iterator
342 I = RegistersKilled.begin(), E = RegistersKilled.end(); I != E; ++I)
343 std::sort(I->second.begin(), I->second.end());
344 for (std::map<MachineInstr*, std::vector<unsigned> >::iterator
345 I = RegistersDead.begin(), E = RegistersDead.end(); I != E; ++I)
346 std::sort(I->second.begin(), I->second.end());
347
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000348 // Check to make sure there are no unreachable blocks in the MC CFG for the
349 // function. If so, it is due to a bug in the instruction selector or some
350 // other part of the code generator if this happens.
351#ifndef NDEBUG
Misha Brukmanedf128a2005-04-21 22:36:52 +0000352 for(MachineFunction::iterator i = MF.begin(), e = MF.end(); i != e; ++i)
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000353 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
354#endif
355
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000356 PHIVarInfo.clear();
Chris Lattnerbc40e892003-01-13 20:01:16 +0000357 return false;
358}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000359
360/// instructionChanged - When the address of an instruction changes, this
361/// method should be called so that live variables can update its internal
362/// data structures. This removes the records for OldMI, transfering them to
363/// the records for NewMI.
364void LiveVariables::instructionChanged(MachineInstr *OldMI,
365 MachineInstr *NewMI) {
366 // If the instruction defines any virtual registers, update the VarInfo for
367 // the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000368 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
369 MachineOperand &MO = OldMI->getOperand(i);
Chris Lattnerd45be362005-01-19 17:09:15 +0000370 if (MO.isRegister() && MO.getReg() &&
Chris Lattner5ed001b2004-02-19 18:28:02 +0000371 MRegisterInfo::isVirtualRegister(MO.getReg())) {
372 unsigned Reg = MO.getReg();
373 VarInfo &VI = getVarInfo(Reg);
Chris Lattnerd45be362005-01-19 17:09:15 +0000374 if (MO.isDef()) {
375 // Update the defining instruction.
376 if (VI.DefInst == OldMI)
377 VI.DefInst = NewMI;
Chris Lattner2a6e1632005-01-19 17:11:51 +0000378 }
379 if (MO.isUse()) {
Chris Lattnerd45be362005-01-19 17:09:15 +0000380 // If this is a kill of the value, update the VI kills list.
381 if (VI.removeKill(OldMI))
382 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
383 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000384 }
385 }
386
387 // Move the killed information over...
388 killed_iterator I, E;
389 tie(I, E) = killed_range(OldMI);
Chris Lattner44b94c22005-08-23 23:42:17 +0000390 if (I != E) {
391 std::vector<unsigned> &V = RegistersKilled[NewMI];
392 bool WasEmpty = V.empty();
393 V.insert(V.end(), I, E);
394 if (!WasEmpty)
395 std::sort(V.begin(), V.end()); // Keep the reg list sorted.
396 RegistersKilled.erase(OldMI);
397 }
Chris Lattnera96478d2004-02-19 18:32:29 +0000398
Chris Lattner5ed001b2004-02-19 18:28:02 +0000399 // Move the dead information over...
400 tie(I, E) = dead_range(OldMI);
Chris Lattner44b94c22005-08-23 23:42:17 +0000401 if (I != E) {
402 std::vector<unsigned> &V = RegistersDead[NewMI];
403 bool WasEmpty = V.empty();
404 V.insert(V.end(), I, E);
405 if (!WasEmpty)
406 std::sort(V.begin(), V.end()); // Keep the reg list sorted.
407 RegistersDead.erase(OldMI);
408 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000409}
Chris Lattner7a3abdc2006-09-03 00:05:09 +0000410
411/// removeVirtualRegistersKilled - Remove all killed info for the specified
412/// instruction.
413void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
414 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
415 RegistersKilled.find(MI);
416 if (I == RegistersKilled.end()) return;
417
418 std::vector<unsigned> &Regs = I->second;
419 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
420 if (MRegisterInfo::isVirtualRegister(Regs[i])) {
421 bool removed = getVarInfo(Regs[i]).removeKill(MI);
422 assert(removed && "kill not in register's VarInfo?");
423 }
424 }
425 RegistersKilled.erase(I);
426}
427
428/// removeVirtualRegistersDead - Remove all of the dead registers for the
429/// specified instruction from the live variable information.
430void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
431 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
432 RegistersDead.find(MI);
433 if (I == RegistersDead.end()) return;
434
435 std::vector<unsigned> &Regs = I->second;
436 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
437 if (MRegisterInfo::isVirtualRegister(Regs[i])) {
438 bool removed = getVarInfo(Regs[i]).removeKill(MI);
439 assert(removed && "kill not in register's VarInfo?");
440 }
441 }
442 RegistersDead.erase(I);
443}
444
Bill Wendlingf7da4e92006-10-03 07:20:20 +0000445/// analyzePHINodes - Gather information about the PHI nodes in here. In
446/// particular, we want to map the variable information of a virtual
447/// register which is used in a PHI node. We map that to the BB the vreg is
448/// coming from.
449///
450void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
451 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
452 I != E; ++I)
453 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
454 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
455 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
456 PHIVarInfo[BBI->getOperand(i + 1).getMachineBasicBlock()].
457 push_back(BBI->getOperand(i).getReg());
458}