blob: a1bda195ff67adbb3314b9c5fcacc452b7ae11ef [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 Lattner49a5aaa2004-01-30 22:08:53 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattnerbc40e892003-01-13 20:01:16 +000040static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
41
Chris Lattnerfb2cb692003-05-12 14:24:00 +000042LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Chris Lattneref09c632004-01-31 21:27:19 +000043 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattnerfb2cb692003-05-12 14:24:00 +000044 "getVarInfo: not a virtual register!");
45 RegIdx -= MRegisterInfo::FirstVirtualRegister;
46 if (RegIdx >= VirtRegInfo.size()) {
47 if (RegIdx >= 2*VirtRegInfo.size())
48 VirtRegInfo.resize(RegIdx*2);
49 else
50 VirtRegInfo.resize(2*VirtRegInfo.size());
51 }
52 return VirtRegInfo[RegIdx];
53}
54
Chris Lattner657b4d12005-08-24 00:09:33 +000055bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
56 std::map<MachineInstr*, std::vector<unsigned> >::const_iterator I =
57 RegistersKilled.find(MI);
58 if (I == RegistersKilled.end()) return false;
59
60 // Do a binary search, as these lists can grow pretty big, particularly for
61 // call instructions on targets with lots of call-clobbered registers.
62 return std::binary_search(I->second.begin(), I->second.end(), Reg);
63}
64
65bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
66 std::map<MachineInstr*, std::vector<unsigned> >::const_iterator I =
67 RegistersDead.find(MI);
68 if (I == RegistersDead.end()) return false;
69
70 // Do a binary search, as these lists can grow pretty big, particularly for
71 // call instructions on targets with lots of call-clobbered registers.
72 return std::binary_search(I->second.begin(), I->second.end(), Reg);
73}
Chris Lattnerfb2cb692003-05-12 14:24:00 +000074
75
Chris Lattnerbc40e892003-01-13 20:01:16 +000076void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Misha Brukman09ba9062004-06-24 21:31:16 +000077 MachineBasicBlock *MBB) {
Chris Lattner8ba97712004-07-01 04:29:47 +000078 unsigned BBNum = MBB->getNumber();
Chris Lattnerbc40e892003-01-13 20:01:16 +000079
80 // Check to see if this basic block is one of the killing blocks. If so,
81 // remove it...
82 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +000083 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +000084 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
85 break;
86 }
87
Chris Lattner73d4adf2004-07-19 06:26:50 +000088 if (MBB == VRInfo.DefInst->getParent()) return; // Terminate recursion
Chris Lattnerbc40e892003-01-13 20:01:16 +000089
90 if (VRInfo.AliveBlocks.size() <= BBNum)
91 VRInfo.AliveBlocks.resize(BBNum+1); // Make space...
92
93 if (VRInfo.AliveBlocks[BBNum])
94 return; // We already know the block is live
95
96 // Mark the variable known alive in this bb
97 VRInfo.AliveBlocks[BBNum] = true;
98
Chris Lattnerf25fb4b2004-05-01 21:24:24 +000099 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
100 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000101 MarkVirtRegAliveInBlock(VRInfo, *PI);
102}
103
104void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
Misha Brukman09ba9062004-06-24 21:31:16 +0000105 MachineInstr *MI) {
Alkis Evlogimenos2e58a412004-09-01 22:34:52 +0000106 assert(VRInfo.DefInst && "Register use before def!");
107
Chris Lattnerbc40e892003-01-13 20:01:16 +0000108 // Check to see if this basic block is already a kill block...
Chris Lattner74de8b12004-07-19 07:04:55 +0000109 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Chris Lattnerbc40e892003-01-13 20:01:16 +0000110 // Yes, this register is killed in this basic block already. Increase the
111 // live range by updating the kill instruction.
Chris Lattner74de8b12004-07-19 07:04:55 +0000112 VRInfo.Kills.back() = MI;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000113 return;
114 }
115
116#ifndef NDEBUG
117 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattner74de8b12004-07-19 07:04:55 +0000118 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000119#endif
120
Misha Brukmanedf128a2005-04-21 22:36:52 +0000121 assert(MBB != VRInfo.DefInst->getParent() &&
Chris Lattner73d4adf2004-07-19 06:26:50 +0000122 "Should have kill for defblock!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000123
124 // Add a new kill entry for this basic block.
Chris Lattner74de8b12004-07-19 07:04:55 +0000125 VRInfo.Kills.push_back(MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000126
127 // Update all dominating blocks to mark them known live.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000128 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
129 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000130 MarkVirtRegAliveInBlock(VRInfo, *PI);
131}
132
133void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Alkis Evlogimenosc55640f2004-01-13 21:16:25 +0000134 PhysRegInfo[Reg] = MI;
135 PhysRegUsed[Reg] = true;
Chris Lattner6d3848d2004-05-10 05:12:43 +0000136
137 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
138 unsigned Alias = *AliasSet; ++AliasSet) {
139 PhysRegInfo[Alias] = MI;
140 PhysRegUsed[Alias] = true;
141 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000142}
143
144void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
145 // Does this kill a previous version of this register?
146 if (MachineInstr *LastUse = PhysRegInfo[Reg]) {
147 if (PhysRegUsed[Reg])
Chris Lattner44b94c22005-08-23 23:42:17 +0000148 RegistersKilled[LastUse].push_back(Reg);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000149 else
Chris Lattner44b94c22005-08-23 23:42:17 +0000150 RegistersDead[LastUse].push_back(Reg);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000151 }
152 PhysRegInfo[Reg] = MI;
153 PhysRegUsed[Reg] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000154
155 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
Chris Lattner6d3848d2004-05-10 05:12:43 +0000156 unsigned Alias = *AliasSet; ++AliasSet) {
Chris Lattner49948772004-02-09 01:43:23 +0000157 if (MachineInstr *LastUse = PhysRegInfo[Alias]) {
158 if (PhysRegUsed[Alias])
Chris Lattner44b94c22005-08-23 23:42:17 +0000159 RegistersKilled[LastUse].push_back(Alias);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000160 else
Chris Lattner44b94c22005-08-23 23:42:17 +0000161 RegistersDead[LastUse].push_back(Alias);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000162 }
Chris Lattner49948772004-02-09 01:43:23 +0000163 PhysRegInfo[Alias] = MI;
164 PhysRegUsed[Alias] = false;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000165 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000166}
167
168bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000169 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Chris Lattner96aef892004-02-09 01:35:21 +0000170 RegInfo = MF.getTarget().getRegisterInfo();
171 assert(RegInfo && "Target doesn't have register information?");
172
Alkis Evlogimenos22a2f6d2004-08-26 22:23:32 +0000173 AllocatablePhysicalRegisters = RegInfo->getAllocatableSet(MF);
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000174
Chris Lattnerbc40e892003-01-13 20:01:16 +0000175 // PhysRegInfo - Keep track of which instruction was the last use of a
176 // physical register. This is a purely local property, because all physical
177 // register references as presumed dead across basic blocks.
178 //
Misha Brukmanedf128a2005-04-21 22:36:52 +0000179 PhysRegInfo = (MachineInstr**)alloca(sizeof(MachineInstr*) *
Chris Lattner6fcd8d82004-10-25 18:44:14 +0000180 RegInfo->getNumRegs());
181 PhysRegUsed = (bool*)alloca(sizeof(bool)*RegInfo->getNumRegs());
182 std::fill(PhysRegInfo, PhysRegInfo+RegInfo->getNumRegs(), (MachineInstr*)0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000183
Chris Lattnerbc40e892003-01-13 20:01:16 +0000184 /// Get some space for a respectable number of registers...
185 VirtRegInfo.resize(64);
Chris Lattnerd493b342005-04-09 15:23:25 +0000186
187 // Mark live-in registers as live-in.
Chris Lattner712ad0c2005-05-13 07:08:07 +0000188 for (MachineFunction::livein_iterator I = MF.livein_begin(),
Chris Lattnerd493b342005-04-09 15:23:25 +0000189 E = MF.livein_end(); I != E; ++I) {
Chris Lattner712ad0c2005-05-13 07:08:07 +0000190 assert(MRegisterInfo::isPhysicalRegister(I->first) &&
Chris Lattnerd493b342005-04-09 15:23:25 +0000191 "Cannot have a live-in virtual register!");
Chris Lattner712ad0c2005-05-13 07:08:07 +0000192 HandlePhysRegDef(I->first, 0);
Chris Lattnerd493b342005-04-09 15:23:25 +0000193 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000194
Chris Lattnerbc40e892003-01-13 20:01:16 +0000195 // Calculate live variable information in depth first order on the CFG of the
196 // function. This guarantees that we will see the definition of a virtual
197 // register before its uses due to dominance properties of SSA (except for PHI
198 // nodes, which are treated as a special case).
199 //
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000200 MachineBasicBlock *Entry = MF.begin();
Chris Lattnera5287a62004-07-01 04:24:29 +0000201 std::set<MachineBasicBlock*> Visited;
202 for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited),
203 E = df_ext_end(Entry, Visited); DFI != E; ++DFI) {
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000204 MachineBasicBlock *MBB = *DFI;
Chris Lattner8ba97712004-07-01 04:29:47 +0000205 unsigned BBNum = MBB->getNumber();
Chris Lattnerbc40e892003-01-13 20:01:16 +0000206
207 // Loop over all of the instructions, processing them.
208 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000209 I != E; ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000210 MachineInstr *MI = I;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000211 const TargetInstrDescriptor &MID = TII.get(MI->getOpcode());
212
213 // Process all of the operands of the instruction...
214 unsigned NumOperandsToProcess = MI->getNumOperands();
215
216 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
217 // of the uses. They will be handled in other basic blocks.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000218 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman09ba9062004-06-24 21:31:16 +0000219 NumOperandsToProcess = 1;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000220
221 // Loop over implicit uses, using them.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000222 for (const unsigned *ImplicitUses = MID.ImplicitUses;
223 *ImplicitUses; ++ImplicitUses)
Misha Brukman09ba9062004-06-24 21:31:16 +0000224 HandlePhysRegUse(*ImplicitUses, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000225
226 // Process all explicit uses...
227 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000228 MachineOperand &MO = MI->getOperand(i);
229 if (MO.isUse() && MO.isRegister() && MO.getReg()) {
230 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
231 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
232 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattner5cdfbad2003-05-07 20:08:36 +0000233 AllocatablePhysicalRegisters[MO.getReg()]) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000234 HandlePhysRegUse(MO.getReg(), MI);
235 }
236 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000237 }
238
239 // Loop over implicit defs, defining them.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000240 for (const unsigned *ImplicitDefs = MID.ImplicitDefs;
241 *ImplicitDefs; ++ImplicitDefs)
242 HandlePhysRegDef(*ImplicitDefs, MI);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000243
244 // Process all explicit defs...
245 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman09ba9062004-06-24 21:31:16 +0000246 MachineOperand &MO = MI->getOperand(i);
247 if (MO.isDef() && MO.isRegister() && MO.getReg()) {
248 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
249 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000250
Chris Lattner73d4adf2004-07-19 06:26:50 +0000251 assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
Misha Brukman09ba9062004-06-24 21:31:16 +0000252 VRInfo.DefInst = MI;
Chris Lattner472405e2004-07-19 06:55:21 +0000253 // Defaults to dead
Chris Lattner74de8b12004-07-19 07:04:55 +0000254 VRInfo.Kills.push_back(MI);
Misha Brukman09ba9062004-06-24 21:31:16 +0000255 } 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 HandlePhysRegDef(MO.getReg(), MI);
258 }
259 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000260 }
261 }
262
263 // Handle any virtual assignments from PHI nodes which might be at the
264 // bottom of this basic block. We check all of our successor blocks to see
265 // if they have PHI nodes, and if so, we simulate an assignment at the end
266 // of the current block.
Chris Lattnerf25fb4b2004-05-01 21:24:24 +0000267 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
268 E = MBB->succ_end(); SI != E; ++SI) {
269 MachineBasicBlock *Succ = *SI;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000270
Chris Lattnerbc40e892003-01-13 20:01:16 +0000271 // PHI nodes are guaranteed to be at the top of the block...
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000272 for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end();
Misha Brukman09ba9062004-06-24 21:31:16 +0000273 MI != ME && MI->getOpcode() == TargetInstrInfo::PHI; ++MI) {
274 for (unsigned i = 1; ; i += 2) {
Chris Lattner92bc3bc2004-02-29 22:01:51 +0000275 assert(MI->getNumOperands() > i+1 &&
276 "Didn't find an entry for our predecessor??");
Misha Brukman09ba9062004-06-24 21:31:16 +0000277 if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) {
278 MachineOperand &MO = MI->getOperand(i);
279 if (!MO.getVRegValueOrNull()) {
280 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnerac28fbd2005-11-21 07:06:27 +0000281 assert(VRInfo.DefInst && "Register use before def (or no def)!");
Chris Lattnerbc40e892003-01-13 20:01:16 +0000282
Chris Lattnerac28fbd2005-11-21 07:06:27 +0000283 // Only mark it alive only in the block we are representing.
Misha Brukman09ba9062004-06-24 21:31:16 +0000284 MarkVirtRegAliveInBlock(VRInfo, MBB);
Chris Lattnerac28fbd2005-11-21 07:06:27 +0000285 break; // Found the PHI entry for this block.
Misha Brukman09ba9062004-06-24 21:31:16 +0000286 }
287 }
Chris Lattner92bc3bc2004-02-29 22:01:51 +0000288 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000289 }
290 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000291
Chris Lattnerd493b342005-04-09 15:23:25 +0000292 // Finally, if the last block in the function is a return, make sure to mark
293 // it as using all of the live-out values in the function.
294 if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) {
295 MachineInstr *Ret = &MBB->back();
Chris Lattner712ad0c2005-05-13 07:08:07 +0000296 for (MachineFunction::liveout_iterator I = MF.liveout_begin(),
Chris Lattnerd493b342005-04-09 15:23:25 +0000297 E = MF.liveout_end(); I != E; ++I) {
298 assert(MRegisterInfo::isPhysicalRegister(*I) &&
299 "Cannot have a live-in virtual register!");
300 HandlePhysRegUse(*I, Ret);
301 }
302 }
303
Chris Lattnerbc40e892003-01-13 20:01:16 +0000304 // Loop over PhysRegInfo, killing any registers that are available at the
305 // end of the basic block. This also resets the PhysRegInfo map.
Chris Lattner96aef892004-02-09 01:35:21 +0000306 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattnerbc40e892003-01-13 20:01:16 +0000307 if (PhysRegInfo[i])
Misha Brukman09ba9062004-06-24 21:31:16 +0000308 HandlePhysRegDef(i, 0);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000309 }
310
Chris Lattnerbc40e892003-01-13 20:01:16 +0000311 // Convert the information we have gathered into VirtRegInfo and transform it
312 // into a form usable by RegistersKilled.
313 //
314 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
315 for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
Chris Lattner74de8b12004-07-19 07:04:55 +0000316 if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
Chris Lattner44b94c22005-08-23 23:42:17 +0000317 RegistersDead[VirtRegInfo[i].Kills[j]].push_back(
318 i + MRegisterInfo::FirstVirtualRegister);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000319
320 else
Chris Lattner44b94c22005-08-23 23:42:17 +0000321 RegistersKilled[VirtRegInfo[i].Kills[j]].push_back(
322 i + MRegisterInfo::FirstVirtualRegister);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000323 }
Chris Lattnera5287a62004-07-01 04:24:29 +0000324
Chris Lattner657b4d12005-08-24 00:09:33 +0000325 // Walk through the RegistersKilled/Dead sets, and sort the registers killed
326 // or dead. This allows us to use efficient binary search for membership
327 // testing.
328 for (std::map<MachineInstr*, std::vector<unsigned> >::iterator
329 I = RegistersKilled.begin(), E = RegistersKilled.end(); I != E; ++I)
330 std::sort(I->second.begin(), I->second.end());
331 for (std::map<MachineInstr*, std::vector<unsigned> >::iterator
332 I = RegistersDead.begin(), E = RegistersDead.end(); I != E; ++I)
333 std::sort(I->second.begin(), I->second.end());
334
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000335 // Check to make sure there are no unreachable blocks in the MC CFG for the
336 // function. If so, it is due to a bug in the instruction selector or some
337 // other part of the code generator if this happens.
338#ifndef NDEBUG
Misha Brukmanedf128a2005-04-21 22:36:52 +0000339 for(MachineFunction::iterator i = MF.begin(), e = MF.end(); i != e; ++i)
Chris Lattner9fb6cf12004-07-09 16:44:37 +0000340 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
341#endif
342
Chris Lattnerbc40e892003-01-13 20:01:16 +0000343 return false;
344}
Chris Lattner5ed001b2004-02-19 18:28:02 +0000345
346/// instructionChanged - When the address of an instruction changes, this
347/// method should be called so that live variables can update its internal
348/// data structures. This removes the records for OldMI, transfering them to
349/// the records for NewMI.
350void LiveVariables::instructionChanged(MachineInstr *OldMI,
351 MachineInstr *NewMI) {
352 // If the instruction defines any virtual registers, update the VarInfo for
353 // the instruction.
Alkis Evlogimenosa8db01a2004-03-30 22:44:39 +0000354 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
355 MachineOperand &MO = OldMI->getOperand(i);
Chris Lattnerd45be362005-01-19 17:09:15 +0000356 if (MO.isRegister() && MO.getReg() &&
Chris Lattner5ed001b2004-02-19 18:28:02 +0000357 MRegisterInfo::isVirtualRegister(MO.getReg())) {
358 unsigned Reg = MO.getReg();
359 VarInfo &VI = getVarInfo(Reg);
Chris Lattnerd45be362005-01-19 17:09:15 +0000360 if (MO.isDef()) {
361 // Update the defining instruction.
362 if (VI.DefInst == OldMI)
363 VI.DefInst = NewMI;
Chris Lattner2a6e1632005-01-19 17:11:51 +0000364 }
365 if (MO.isUse()) {
Chris Lattnerd45be362005-01-19 17:09:15 +0000366 // If this is a kill of the value, update the VI kills list.
367 if (VI.removeKill(OldMI))
368 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
369 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000370 }
371 }
372
373 // Move the killed information over...
374 killed_iterator I, E;
375 tie(I, E) = killed_range(OldMI);
Chris Lattner44b94c22005-08-23 23:42:17 +0000376 if (I != E) {
377 std::vector<unsigned> &V = RegistersKilled[NewMI];
378 bool WasEmpty = V.empty();
379 V.insert(V.end(), I, E);
380 if (!WasEmpty)
381 std::sort(V.begin(), V.end()); // Keep the reg list sorted.
382 RegistersKilled.erase(OldMI);
383 }
Chris Lattnera96478d2004-02-19 18:32:29 +0000384
Chris Lattner5ed001b2004-02-19 18:28:02 +0000385 // Move the dead information over...
386 tie(I, E) = dead_range(OldMI);
Chris Lattner44b94c22005-08-23 23:42:17 +0000387 if (I != E) {
388 std::vector<unsigned> &V = RegistersDead[NewMI];
389 bool WasEmpty = V.empty();
390 V.insert(V.end(), I, E);
391 if (!WasEmpty)
392 std::sort(V.begin(), V.end()); // Keep the reg list sorted.
393 RegistersDead.erase(OldMI);
394 }
Chris Lattner5ed001b2004-02-19 18:28:02 +0000395}