blob: 65129eb14ccd3aa081da66018db120068ae8ad1b [file] [log] [blame]
Chris Lattnercab0b442003-01-13 20:01:16 +00001//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00009//
Chris Lattner5ab42e52003-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 Lattnercab0b442003-01-13 20:01:16 +000027//===----------------------------------------------------------------------===//
28
29#include "llvm/CodeGen/LiveVariables.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerb21ec542004-02-10 21:18:55 +000031#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb4d58d72003-01-14 22:00:31 +000032#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercab0b442003-01-13 20:01:16 +000033#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000034#include "llvm/ADT/DepthFirstIterator.h"
35#include "llvm/ADT/STLExtras.h"
Chris Lattner61808812004-10-25 18:44:14 +000036#include "llvm/Config/alloca.h"
Chris Lattnereeacce52005-08-24 00:09:33 +000037#include <algorithm>
Chris Lattner07708622004-01-30 22:08:53 +000038using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000039
Devang Patel8c78a0b2007-05-03 01:11:54 +000040char LiveVariables::ID = 0;
Chris Lattner3c9b2422006-08-27 22:30:17 +000041static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
Chris Lattnercab0b442003-01-13 20:01:16 +000042
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000043void LiveVariables::VarInfo::dump() const {
Bill Wendling355fc5a2006-12-07 20:28:15 +000044 cerr << "Register Defined by: ";
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000045 if (DefInst)
Bill Wendling355fc5a2006-12-07 20:28:15 +000046 cerr << *DefInst;
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000047 else
Bill Wendling355fc5a2006-12-07 20:28:15 +000048 cerr << "<null>\n";
49 cerr << " Alive in blocks: ";
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000050 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
Bill Wendling355fc5a2006-12-07 20:28:15 +000051 if (AliveBlocks[i]) cerr << i << ", ";
52 cerr << "\n Killed by:";
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000053 if (Kills.empty())
Bill Wendling355fc5a2006-12-07 20:28:15 +000054 cerr << " No instructions.\n";
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000055 else {
56 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
Bill Wendling355fc5a2006-12-07 20:28:15 +000057 cerr << "\n #" << i << ": " << *Kills[i];
58 cerr << "\n";
Chris Lattnerbe45b5e2006-01-04 05:40:30 +000059 }
60}
61
Chris Lattner584bae42003-05-12 14:24:00 +000062LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
Chris Lattnerc330b982004-01-31 21:27:19 +000063 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
Chris Lattner584bae42003-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 }
Evan Chengf6f04332007-03-17 09:29:54 +000072 VarInfo &VI = VirtRegInfo[RegIdx];
73 VI.AliveBlocks.resize(MF->getNumBlockIDs());
Evan Chengf6f04332007-03-17 09:29:54 +000074 return VI;
Chris Lattner584bae42003-05-12 14:24:00 +000075}
76
Chris Lattnereeacce52005-08-24 00:09:33 +000077bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
Evan Cheng70ec5282006-11-15 20:51:59 +000078 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
79 MachineOperand &MO = MI->getOperand(i);
80 if (MO.isReg() && MO.isKill()) {
Evan Cheng7818c032007-04-25 07:30:23 +000081 if ((MO.getReg() == Reg) ||
82 (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
83 MRegisterInfo::isPhysicalRegister(Reg) &&
84 RegInfo->isSubRegister(MO.getReg(), Reg)))
Evan Cheng70ec5282006-11-15 20:51:59 +000085 return true;
86 }
87 }
88 return false;
Chris Lattnereeacce52005-08-24 00:09:33 +000089}
90
91bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
Evan Cheng70ec5282006-11-15 20:51:59 +000092 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
93 MachineOperand &MO = MI->getOperand(i);
Evan Cheng7818c032007-04-25 07:30:23 +000094 if (MO.isReg() && MO.isDead()) {
95 if ((MO.getReg() == Reg) ||
96 (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
97 MRegisterInfo::isPhysicalRegister(Reg) &&
98 RegInfo->isSubRegister(MO.getReg(), Reg)))
Evan Cheng70ec5282006-11-15 20:51:59 +000099 return true;
Evan Cheng7818c032007-04-25 07:30:23 +0000100 }
Evan Cheng70ec5282006-11-15 20:51:59 +0000101 }
102 return false;
103}
104
105bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
106 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
107 MachineOperand &MO = MI->getOperand(i);
Evan Cheng7818c032007-04-25 07:30:23 +0000108 if (MO.isReg() && MO.isDef() && MO.getReg() == Reg)
109 return true;
Evan Cheng70ec5282006-11-15 20:51:59 +0000110 }
111 return false;
Chris Lattnereeacce52005-08-24 00:09:33 +0000112}
Chris Lattner584bae42003-05-12 14:24:00 +0000113
Chris Lattnercab0b442003-01-13 20:01:16 +0000114void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
Evan Cheng9e178722007-05-08 19:00:00 +0000115 MachineBasicBlock *MBB,
116 std::vector<MachineBasicBlock*> &WorkList) {
Chris Lattner6c375e42004-07-01 04:29:47 +0000117 unsigned BBNum = MBB->getNumber();
Chris Lattnercab0b442003-01-13 20:01:16 +0000118
119 // Check to see if this basic block is one of the killing blocks. If so,
120 // remove it...
121 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattneraef6c2a2004-07-19 07:04:55 +0000122 if (VRInfo.Kills[i]->getParent() == MBB) {
Chris Lattnercab0b442003-01-13 20:01:16 +0000123 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
124 break;
125 }
126
Chris Lattner5027de32004-07-19 06:26:50 +0000127 if (MBB == VRInfo.DefInst->getParent()) return; // Terminate recursion
Chris Lattnercab0b442003-01-13 20:01:16 +0000128
Chris Lattnercab0b442003-01-13 20:01:16 +0000129 if (VRInfo.AliveBlocks[BBNum])
130 return; // We already know the block is live
131
132 // Mark the variable known alive in this bb
133 VRInfo.AliveBlocks[BBNum] = true;
134
Evan Cheng9e178722007-05-08 19:00:00 +0000135 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
136 E = MBB->pred_rend(); PI != E; ++PI)
137 WorkList.push_back(*PI);
Chris Lattnercab0b442003-01-13 20:01:16 +0000138}
139
Evan Cheng9e178722007-05-08 19:00:00 +0000140void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
141 MachineBasicBlock *MBB) {
142 std::vector<MachineBasicBlock*> WorkList;
143 MarkVirtRegAliveInBlock(VRInfo, MBB, WorkList);
144 while (!WorkList.empty()) {
145 MachineBasicBlock *Pred = WorkList.back();
146 WorkList.pop_back();
147 MarkVirtRegAliveInBlock(VRInfo, Pred, WorkList);
148 }
149}
150
151
Chris Lattnercab0b442003-01-13 20:01:16 +0000152void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000153 MachineInstr *MI) {
Alkis Evlogimenos6a099d42004-09-01 22:34:52 +0000154 assert(VRInfo.DefInst && "Register use before def!");
155
Evan Cheng8387cf12007-04-17 20:22:11 +0000156 VRInfo.NumUses++;
Evan Chengf6f04332007-03-17 09:29:54 +0000157
Chris Lattnercab0b442003-01-13 20:01:16 +0000158 // Check to see if this basic block is already a kill block...
Chris Lattneraef6c2a2004-07-19 07:04:55 +0000159 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
Chris Lattnercab0b442003-01-13 20:01:16 +0000160 // Yes, this register is killed in this basic block already. Increase the
161 // live range by updating the kill instruction.
Chris Lattneraef6c2a2004-07-19 07:04:55 +0000162 VRInfo.Kills.back() = MI;
Chris Lattnercab0b442003-01-13 20:01:16 +0000163 return;
164 }
165
166#ifndef NDEBUG
167 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
Chris Lattneraef6c2a2004-07-19 07:04:55 +0000168 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
Chris Lattnercab0b442003-01-13 20:01:16 +0000169#endif
170
Misha Brukman835702a2005-04-21 22:36:52 +0000171 assert(MBB != VRInfo.DefInst->getParent() &&
Chris Lattner5027de32004-07-19 06:26:50 +0000172 "Should have kill for defblock!");
Chris Lattnercab0b442003-01-13 20:01:16 +0000173
174 // Add a new kill entry for this basic block.
Evan Chengdf7949a2007-03-09 09:48:56 +0000175 // If this virtual register is already marked as alive in this basic block,
176 // that means it is alive in at least one of the successor block, it's not
177 // a kill.
Evan Cheng4c53d322007-04-18 05:04:38 +0000178 if (!VRInfo.AliveBlocks[MBB->getNumber()])
Evan Chengdf7949a2007-03-09 09:48:56 +0000179 VRInfo.Kills.push_back(MI);
Chris Lattnercab0b442003-01-13 20:01:16 +0000180
181 // Update all dominating blocks to mark them known live.
Chris Lattnerc49a9a52004-05-01 21:24:24 +0000182 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
183 E = MBB->pred_end(); PI != E; ++PI)
Chris Lattnercab0b442003-01-13 20:01:16 +0000184 MarkVirtRegAliveInBlock(VRInfo, *PI);
185}
186
Evan Cheng43a17fe2007-04-26 01:40:09 +0000187bool LiveVariables::addRegisterKilled(unsigned IncomingReg, MachineInstr *MI,
188 bool AddIfNotFound) {
Evan Cheng7818c032007-04-25 07:30:23 +0000189 bool Found = false;
Evan Cheng70ec5282006-11-15 20:51:59 +0000190 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
191 MachineOperand &MO = MI->getOperand(i);
Evan Cheng7818c032007-04-25 07:30:23 +0000192 if (MO.isReg() && MO.isUse()) {
193 unsigned Reg = MO.getReg();
194 if (!Reg)
195 continue;
196 if (Reg == IncomingReg) {
197 MO.setIsKill();
198 Found = true;
199 break;
200 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
201 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
202 RegInfo->isSuperRegister(IncomingReg, Reg) &&
203 MO.isKill())
204 // A super-register kill already exists.
Evan Cheng43a17fe2007-04-26 01:40:09 +0000205 return true;
Evan Cheng70ec5282006-11-15 20:51:59 +0000206 }
207 }
Evan Cheng7818c032007-04-25 07:30:23 +0000208
209 // If not found, this means an alias of one of the operand is killed. Add a
Evan Cheng43a17fe2007-04-26 01:40:09 +0000210 // new implicit operand if required.
211 if (!Found && AddIfNotFound) {
Evan Cheng7818c032007-04-25 07:30:23 +0000212 MI->addRegOperand(IncomingReg, false/*IsDef*/,true/*IsImp*/,true/*IsKill*/);
Evan Cheng43a17fe2007-04-26 01:40:09 +0000213 return true;
214 }
215 return Found;
Evan Cheng70ec5282006-11-15 20:51:59 +0000216}
217
Evan Cheng43a17fe2007-04-26 01:40:09 +0000218bool LiveVariables::addRegisterDead(unsigned IncomingReg, MachineInstr *MI,
219 bool AddIfNotFound) {
Evan Cheng7818c032007-04-25 07:30:23 +0000220 bool Found = false;
Evan Cheng70ec5282006-11-15 20:51:59 +0000221 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
222 MachineOperand &MO = MI->getOperand(i);
Evan Cheng7818c032007-04-25 07:30:23 +0000223 if (MO.isReg() && MO.isDef()) {
224 unsigned Reg = MO.getReg();
225 if (!Reg)
226 continue;
227 if (Reg == IncomingReg) {
228 MO.setIsDead();
229 Found = true;
230 break;
231 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
232 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
233 RegInfo->isSuperRegister(IncomingReg, Reg) &&
234 MO.isDead())
235 // There exists a super-register that's marked dead.
Evan Cheng43a17fe2007-04-26 01:40:09 +0000236 return true;
Evan Cheng70ec5282006-11-15 20:51:59 +0000237 }
238 }
Evan Cheng7818c032007-04-25 07:30:23 +0000239
240 // If not found, this means an alias of one of the operand is dead. Add a
241 // new implicit operand.
Evan Cheng43a17fe2007-04-26 01:40:09 +0000242 if (!Found && AddIfNotFound) {
Evan Cheng7818c032007-04-25 07:30:23 +0000243 MI->addRegOperand(IncomingReg, true/*IsDef*/,true/*IsImp*/,false/*IsKill*/,
244 true/*IsDead*/);
Evan Cheng43a17fe2007-04-26 01:40:09 +0000245 return true;
246 }
247 return Found;
Evan Cheng70ec5282006-11-15 20:51:59 +0000248}
249
Chris Lattnercab0b442003-01-13 20:01:16 +0000250void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
Evan Cheng7818c032007-04-25 07:30:23 +0000251 // There is a now a proper use, forget about the last partial use.
252 PhysRegPartUse[Reg] = NULL;
253
254 // Turn previous partial def's into read/mod/write.
255 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
256 MachineInstr *Def = PhysRegPartDef[Reg][i];
257 // First one is just a def. This means the use is reading some undef bits.
258 if (i != 0)
259 Def->addRegOperand(Reg, false/*IsDef*/,true/*IsImp*/,true/*IsKill*/);
260 Def->addRegOperand(Reg, true/*IsDef*/,true/*IsImp*/);
261 }
262 PhysRegPartDef[Reg].clear();
263
264 // There was an earlier def of a super-register. Add implicit def to that MI.
265 // A: EAX = ...
266 // B: = AX
267 // Add implicit def to A.
268 if (PhysRegInfo[Reg] && !PhysRegUsed[Reg]) {
269 MachineInstr *Def = PhysRegInfo[Reg];
270 if (!Def->findRegisterDefOperand(Reg))
271 Def->addRegOperand(Reg, true/*IsDef*/,true/*IsImp*/);
272 }
273
Alkis Evlogimenos9d0c3d22004-01-13 21:16:25 +0000274 PhysRegInfo[Reg] = MI;
275 PhysRegUsed[Reg] = true;
Chris Lattner5eb80942004-05-10 05:12:43 +0000276
Evan Cheng7818c032007-04-25 07:30:23 +0000277 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
278 unsigned SubReg = *SubRegs; ++SubRegs) {
279 PhysRegInfo[SubReg] = MI;
280 PhysRegUsed[SubReg] = true;
Chris Lattner5eb80942004-05-10 05:12:43 +0000281 }
Evan Cheng7818c032007-04-25 07:30:23 +0000282
283 // Remember the partial uses.
284 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
285 unsigned SuperReg = *SuperRegs; ++SuperRegs)
286 PhysRegPartUse[SuperReg] = MI;
Chris Lattnercab0b442003-01-13 20:01:16 +0000287}
288
Evan Chengd8417d92007-06-26 21:03:35 +0000289bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI,
290 SmallSet<unsigned, 4> &SubKills) {
291 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
292 unsigned SubReg = *SubRegs; ++SubRegs) {
293 MachineInstr *LastRef = PhysRegInfo[SubReg];
294 if (LastRef != RefMI)
295 SubKills.insert(SubReg);
296 else if (!HandlePhysRegKill(SubReg, RefMI, SubKills))
297 SubKills.insert(SubReg);
298 }
299
300 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) {
301 // No sub-registers, just check if reg is killed by RefMI.
302 if (PhysRegInfo[Reg] == RefMI)
303 return true;
304 } else if (SubKills.empty())
305 // None of the sub-registers are killed elsewhere...
306 return true;
307 return false;
308}
309
310void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
311 SmallSet<unsigned, 4> &SubKills) {
312 if (SubKills.count(Reg) == 0)
313 addRegisterKilled(Reg, MI, true);
314 else {
315 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
316 unsigned SubReg = *SubRegs; ++SubRegs)
317 addRegisterKills(SubReg, MI, SubKills);
318 }
319}
320
321bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
322 SmallSet<unsigned, 4> SubKills;
323 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
324 addRegisterKilled(Reg, RefMI);
325 return true;
326 } else {
327 // Some sub-registers are killed by another MI.
328 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
329 unsigned SubReg = *SubRegs; ++SubRegs)
330 addRegisterKills(SubReg, RefMI, SubKills);
331 return false;
332 }
333}
334
Chris Lattnercab0b442003-01-13 20:01:16 +0000335void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
336 // Does this kill a previous version of this register?
Evan Cheng7818c032007-04-25 07:30:23 +0000337 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
Evan Chengd8417d92007-06-26 21:03:35 +0000338 if (PhysRegUsed[Reg]) {
339 if (!HandlePhysRegKill(Reg, LastRef)) {
340 if (PhysRegPartUse[Reg])
341 addRegisterKilled(Reg, PhysRegPartUse[Reg], true);
342 }
343 } else if (PhysRegPartUse[Reg])
Evan Cheng7818c032007-04-25 07:30:23 +0000344 // Add implicit use / kill to last use of a sub-register.
Evan Cheng43a17fe2007-04-26 01:40:09 +0000345 addRegisterKilled(Reg, PhysRegPartUse[Reg], true);
Chris Lattnercab0b442003-01-13 20:01:16 +0000346 else
Evan Chenged23a132007-04-26 08:24:22 +0000347 addRegisterDead(Reg, LastRef);
Chris Lattnercab0b442003-01-13 20:01:16 +0000348 }
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000349
Evan Cheng7818c032007-04-25 07:30:23 +0000350 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
351 unsigned SubReg = *SubRegs; ++SubRegs) {
352 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
Evan Chengd8417d92007-06-26 21:03:35 +0000353 if (PhysRegUsed[SubReg]) {
354 if (!HandlePhysRegKill(SubReg, LastRef)) {
355 if (PhysRegPartUse[SubReg])
356 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], true);
357 }
358 //addRegisterKilled(SubReg, LastRef);
359 } else if (PhysRegPartUse[SubReg])
Evan Cheng7818c032007-04-25 07:30:23 +0000360 // Add implicit use / kill to last use of a sub-register.
Evan Chenged23a132007-04-26 08:24:22 +0000361 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], true);
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000362 else
Evan Cheng7818c032007-04-25 07:30:23 +0000363 addRegisterDead(SubReg, LastRef);
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000364 }
Evan Cheng7818c032007-04-25 07:30:23 +0000365 }
366
Evan Chengd8417d92007-06-26 21:03:35 +0000367 if (MI) {
Evan Cheng7818c032007-04-25 07:30:23 +0000368 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
369 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
370 if (PhysRegInfo[SuperReg]) {
371 // The larger register is previously defined. Now a smaller part is
372 // being re-defined. Treat it as read/mod/write.
373 // EAX =
374 // AX = EAX<imp-use,kill>, EAX<imp-def>
375 MI->addRegOperand(SuperReg, false/*IsDef*/,true/*IsImp*/,true/*IsKill*/);
376 MI->addRegOperand(SuperReg, true/*IsDef*/,true/*IsImp*/);
377 PhysRegInfo[SuperReg] = MI;
378 PhysRegUsed[SuperReg] = false;
Evan Chengfc2377d2007-05-14 20:39:18 +0000379 PhysRegPartUse[SuperReg] = NULL;
Evan Cheng7818c032007-04-25 07:30:23 +0000380 } else {
381 // Remember this partial def.
382 PhysRegPartDef[SuperReg].push_back(MI);
383 }
Evan Chengd8417d92007-06-26 21:03:35 +0000384 }
385
386 PhysRegInfo[Reg] = MI;
387 PhysRegUsed[Reg] = false;
388 PhysRegPartUse[Reg] = NULL;
389 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
390 unsigned SubReg = *SubRegs; ++SubRegs) {
391 PhysRegInfo[SubReg] = MI;
392 PhysRegUsed[SubReg] = false;
393 PhysRegPartUse[SubReg] = NULL;
394 }
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000395 }
Chris Lattnercab0b442003-01-13 20:01:16 +0000396}
397
Evan Chengf6f04332007-03-17 09:29:54 +0000398bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
399 MF = &mf;
400 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
401 RegInfo = MF->getTarget().getRegisterInfo();
Chris Lattner26407382004-02-09 01:35:21 +0000402 assert(RegInfo && "Target doesn't have register information?");
403
Evan Chengf6f04332007-03-17 09:29:54 +0000404 ReservedRegisters = RegInfo->getReservedRegs(mf);
Chris Lattner5ab42e52003-05-07 20:08:36 +0000405
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000406 unsigned NumRegs = RegInfo->getNumRegs();
407 PhysRegInfo = new MachineInstr*[NumRegs];
408 PhysRegUsed = new bool[NumRegs];
409 PhysRegPartUse = new MachineInstr*[NumRegs];
410 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
411 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
412 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
413 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
414 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
Chris Lattnercab0b442003-01-13 20:01:16 +0000415
Chris Lattnercab0b442003-01-13 20:01:16 +0000416 /// Get some space for a respectable number of registers...
417 VirtRegInfo.resize(64);
Chris Lattner4c6ab012005-04-09 15:23:25 +0000418
Evan Chengf6f04332007-03-17 09:29:54 +0000419 analyzePHINodes(mf);
Bill Wendling984f0ce2006-10-03 07:20:20 +0000420
Chris Lattnercab0b442003-01-13 20:01:16 +0000421 // Calculate live variable information in depth first order on the CFG of the
422 // function. This guarantees that we will see the definition of a virtual
423 // register before its uses due to dominance properties of SSA (except for PHI
424 // nodes, which are treated as a special case).
425 //
Evan Chengf6f04332007-03-17 09:29:54 +0000426 MachineBasicBlock *Entry = MF->begin();
Chris Lattner7c77fd52004-07-01 04:24:29 +0000427 std::set<MachineBasicBlock*> Visited;
428 for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited),
429 E = df_ext_end(Entry, Visited); DFI != E; ++DFI) {
Chris Lattnerc49a9a52004-05-01 21:24:24 +0000430 MachineBasicBlock *MBB = *DFI;
Chris Lattnercab0b442003-01-13 20:01:16 +0000431
Evan Chengf7ed82d2007-02-19 21:49:54 +0000432 // Mark live-in registers as live-in.
433 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
Evan Chengb612316f2007-02-13 01:30:55 +0000434 EE = MBB->livein_end(); II != EE; ++II) {
435 assert(MRegisterInfo::isPhysicalRegister(*II) &&
436 "Cannot have a live-in virtual register!");
437 HandlePhysRegDef(*II, 0);
438 }
439
Chris Lattnercab0b442003-01-13 20:01:16 +0000440 // Loop over all of the instructions, processing them.
441 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000442 I != E; ++I) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000443 MachineInstr *MI = I;
Chris Lattnercab0b442003-01-13 20:01:16 +0000444
445 // Process all of the operands of the instruction...
446 unsigned NumOperandsToProcess = MI->getNumOperands();
447
448 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
449 // of the uses. They will be handled in other basic blocks.
Misha Brukman835702a2005-04-21 22:36:52 +0000450 if (MI->getOpcode() == TargetInstrInfo::PHI)
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000451 NumOperandsToProcess = 1;
Chris Lattnercab0b442003-01-13 20:01:16 +0000452
Evan Cheng8c9c6d72006-11-10 08:43:01 +0000453 // Process all uses...
Chris Lattnercab0b442003-01-13 20:01:16 +0000454 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000455 MachineOperand &MO = MI->getOperand(i);
Chris Lattner2cb23832006-09-05 20:19:27 +0000456 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000457 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
458 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
459 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Evan Chengf7ed82d2007-02-19 21:49:54 +0000460 !ReservedRegisters[MO.getReg()]) {
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000461 HandlePhysRegUse(MO.getReg(), MI);
462 }
463 }
Chris Lattnercab0b442003-01-13 20:01:16 +0000464 }
465
Evan Cheng8c9c6d72006-11-10 08:43:01 +0000466 // Process all defs...
Chris Lattnercab0b442003-01-13 20:01:16 +0000467 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000468 MachineOperand &MO = MI->getOperand(i);
Chris Lattner2cb23832006-09-05 20:19:27 +0000469 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000470 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
471 VarInfo &VRInfo = getVarInfo(MO.getReg());
Chris Lattnercab0b442003-01-13 20:01:16 +0000472
Chris Lattner5027de32004-07-19 06:26:50 +0000473 assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000474 VRInfo.DefInst = MI;
Chris Lattnera74cf5a2004-07-19 06:55:21 +0000475 // Defaults to dead
Chris Lattneraef6c2a2004-07-19 07:04:55 +0000476 VRInfo.Kills.push_back(MI);
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000477 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Evan Chengf7ed82d2007-02-19 21:49:54 +0000478 !ReservedRegisters[MO.getReg()]) {
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000479 HandlePhysRegDef(MO.getReg(), MI);
480 }
481 }
Chris Lattnercab0b442003-01-13 20:01:16 +0000482 }
483 }
484
485 // Handle any virtual assignments from PHI nodes which might be at the
486 // bottom of this basic block. We check all of our successor blocks to see
487 // if they have PHI nodes, and if so, we simulate an assignment at the end
488 // of the current block.
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000489 if (!PHIVarInfo[MBB->getNumber()].empty()) {
490 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
Misha Brukman835702a2005-04-21 22:36:52 +0000491
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000492 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
Bill Wendling984f0ce2006-10-03 07:20:20 +0000493 E = VarInfoVec.end(); I != E; ++I) {
494 VarInfo& VRInfo = getVarInfo(*I);
495 assert(VRInfo.DefInst && "Register use before def (or no def)!");
Chris Lattnercab0b442003-01-13 20:01:16 +0000496
Bill Wendling984f0ce2006-10-03 07:20:20 +0000497 // Only mark it alive only in the block we are representing.
498 MarkVirtRegAliveInBlock(VRInfo, MBB);
Chris Lattnercab0b442003-01-13 20:01:16 +0000499 }
500 }
Misha Brukman835702a2005-04-21 22:36:52 +0000501
Evan Cheng70ec5282006-11-15 20:51:59 +0000502 // Finally, if the last instruction in the block is a return, make sure to mark
Chris Lattner4c6ab012005-04-09 15:23:25 +0000503 // it as using all of the live-out values in the function.
504 if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) {
505 MachineInstr *Ret = &MBB->back();
Evan Chengf6f04332007-03-17 09:29:54 +0000506 for (MachineFunction::liveout_iterator I = MF->liveout_begin(),
507 E = MF->liveout_end(); I != E; ++I) {
Chris Lattner4c6ab012005-04-09 15:23:25 +0000508 assert(MRegisterInfo::isPhysicalRegister(*I) &&
509 "Cannot have a live-in virtual register!");
510 HandlePhysRegUse(*I, Ret);
Evan Cheng70ec5282006-11-15 20:51:59 +0000511 // Add live-out registers as implicit uses.
Evan Cheng910c8082007-04-26 19:00:32 +0000512 if (Ret->findRegisterUseOperandIdx(*I) == -1)
513 Ret->addRegOperand(*I, false, true);
Chris Lattner4c6ab012005-04-09 15:23:25 +0000514 }
515 }
516
Chris Lattnercab0b442003-01-13 20:01:16 +0000517 // Loop over PhysRegInfo, killing any registers that are available at the
518 // end of the basic block. This also resets the PhysRegInfo map.
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000519 for (unsigned i = 0; i != NumRegs; ++i)
Chris Lattnercab0b442003-01-13 20:01:16 +0000520 if (PhysRegInfo[i])
Misha Brukman7d11fbf2004-06-24 21:31:16 +0000521 HandlePhysRegDef(i, 0);
Evan Cheng7818c032007-04-25 07:30:23 +0000522
523 // Clear some states between BB's. These are purely local information.
Evan Chengd4549c52007-04-25 21:34:08 +0000524 for (unsigned i = 0; i != NumRegs; ++i)
Evan Cheng7818c032007-04-25 07:30:23 +0000525 PhysRegPartDef[i].clear();
Evan Chengd8417d92007-06-26 21:03:35 +0000526 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
527 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000528 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
Chris Lattnercab0b442003-01-13 20:01:16 +0000529 }
530
Evan Cheng70ec5282006-11-15 20:51:59 +0000531 // Convert and transfer the dead / killed information we have gathered into
532 // VirtRegInfo onto MI's.
Chris Lattnercab0b442003-01-13 20:01:16 +0000533 //
Evan Cheng91b07902007-03-09 06:02:17 +0000534 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
535 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) {
Chris Lattneraef6c2a2004-07-19 07:04:55 +0000536 if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
Evan Cheng70ec5282006-11-15 20:51:59 +0000537 addRegisterDead(i + MRegisterInfo::FirstVirtualRegister,
538 VirtRegInfo[i].Kills[j]);
Chris Lattnercab0b442003-01-13 20:01:16 +0000539 else
Evan Cheng70ec5282006-11-15 20:51:59 +0000540 addRegisterKilled(i + MRegisterInfo::FirstVirtualRegister,
541 VirtRegInfo[i].Kills[j]);
Chris Lattnercab0b442003-01-13 20:01:16 +0000542 }
Chris Lattner7c77fd52004-07-01 04:24:29 +0000543
Chris Lattnerd47909e2004-07-09 16:44:37 +0000544 // Check to make sure there are no unreachable blocks in the MC CFG for the
545 // function. If so, it is due to a bug in the instruction selector or some
546 // other part of the code generator if this happens.
547#ifndef NDEBUG
Evan Chengf6f04332007-03-17 09:29:54 +0000548 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
Chris Lattnerd47909e2004-07-09 16:44:37 +0000549 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
550#endif
551
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000552 delete[] PhysRegInfo;
553 delete[] PhysRegUsed;
554 delete[] PhysRegPartUse;
555 delete[] PhysRegPartDef;
556 delete[] PHIVarInfo;
557
Chris Lattnercab0b442003-01-13 20:01:16 +0000558 return false;
559}
Chris Lattnerafa9d7e2004-02-19 18:28:02 +0000560
561/// instructionChanged - When the address of an instruction changes, this
562/// method should be called so that live variables can update its internal
563/// data structures. This removes the records for OldMI, transfering them to
564/// the records for NewMI.
565void LiveVariables::instructionChanged(MachineInstr *OldMI,
566 MachineInstr *NewMI) {
Evan Cheng70ec5282006-11-15 20:51:59 +0000567 // If the instruction defines any virtual registers, update the VarInfo,
568 // kill and dead information for the instruction.
Alkis Evlogimenosa333b132004-03-30 22:44:39 +0000569 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
570 MachineOperand &MO = OldMI->getOperand(i);
Chris Lattner00c43682005-01-19 17:09:15 +0000571 if (MO.isRegister() && MO.getReg() &&
Chris Lattnerafa9d7e2004-02-19 18:28:02 +0000572 MRegisterInfo::isVirtualRegister(MO.getReg())) {
573 unsigned Reg = MO.getReg();
574 VarInfo &VI = getVarInfo(Reg);
Chris Lattner00c43682005-01-19 17:09:15 +0000575 if (MO.isDef()) {
Evan Cheng70ec5282006-11-15 20:51:59 +0000576 if (MO.isDead()) {
577 MO.unsetIsDead();
578 addVirtualRegisterDead(Reg, NewMI);
579 }
Chris Lattner00c43682005-01-19 17:09:15 +0000580 // Update the defining instruction.
581 if (VI.DefInst == OldMI)
582 VI.DefInst = NewMI;
Chris Lattner1cffa732005-01-19 17:11:51 +0000583 }
584 if (MO.isUse()) {
Evan Cheng70ec5282006-11-15 20:51:59 +0000585 if (MO.isKill()) {
586 MO.unsetIsKill();
587 addVirtualRegisterKilled(Reg, NewMI);
588 }
Chris Lattner00c43682005-01-19 17:09:15 +0000589 // If this is a kill of the value, update the VI kills list.
590 if (VI.removeKill(OldMI))
591 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
592 }
Chris Lattnerafa9d7e2004-02-19 18:28:02 +0000593 }
594 }
Chris Lattnerafa9d7e2004-02-19 18:28:02 +0000595}
Chris Lattnerf8f724a2006-09-03 00:05:09 +0000596
597/// removeVirtualRegistersKilled - Remove all killed info for the specified
598/// instruction.
599void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
Evan Cheng70ec5282006-11-15 20:51:59 +0000600 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
601 MachineOperand &MO = MI->getOperand(i);
602 if (MO.isReg() && MO.isKill()) {
603 MO.unsetIsKill();
604 unsigned Reg = MO.getReg();
605 if (MRegisterInfo::isVirtualRegister(Reg)) {
606 bool removed = getVarInfo(Reg).removeKill(MI);
607 assert(removed && "kill not in register's VarInfo?");
608 }
Chris Lattnerf8f724a2006-09-03 00:05:09 +0000609 }
610 }
Chris Lattnerf8f724a2006-09-03 00:05:09 +0000611}
612
613/// removeVirtualRegistersDead - Remove all of the dead registers for the
614/// specified instruction from the live variable information.
615void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
Evan Cheng70ec5282006-11-15 20:51:59 +0000616 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
617 MachineOperand &MO = MI->getOperand(i);
618 if (MO.isReg() && MO.isDead()) {
619 MO.unsetIsDead();
620 unsigned Reg = MO.getReg();
621 if (MRegisterInfo::isVirtualRegister(Reg)) {
622 bool removed = getVarInfo(Reg).removeKill(MI);
623 assert(removed && "kill not in register's VarInfo?");
624 }
Chris Lattnerf8f724a2006-09-03 00:05:09 +0000625 }
626 }
Chris Lattnerf8f724a2006-09-03 00:05:09 +0000627}
628
Bill Wendling984f0ce2006-10-03 07:20:20 +0000629/// analyzePHINodes - Gather information about the PHI nodes in here. In
630/// particular, we want to map the variable information of a virtual
631/// register which is used in a PHI node. We map that to the BB the vreg is
632/// coming from.
633///
634void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
635 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
636 I != E; ++I)
637 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
638 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
639 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Evan Cheng0fbe14a2007-04-25 19:34:00 +0000640 PHIVarInfo[BBI->getOperand(i + 1).getMachineBasicBlock()->getNumber()].
Bill Wendling984f0ce2006-10-03 07:20:20 +0000641 push_back(BBI->getOperand(i).getReg());
642}