Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 1 | //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5cdfbad | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #include "llvm/CodeGen/LiveVariables.h" |
| 30 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/DepthFirstIterator.h" |
Evan Cheng | 0410407 | 2007-06-27 05:23:00 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 6fcd8d8 | 2004-10-25 18:44:14 +0000 | [diff] [blame] | 38 | #include "llvm/Config/alloca.h" |
Chris Lattner | 657b4d1 | 2005-08-24 00:09:33 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Chris Lattner | 49a5aaa | 2004-01-30 22:08:53 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 41 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 42 | char LiveVariables::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 43 | static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 44 | |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 45 | void LiveVariables::VarInfo::dump() const { |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 46 | cerr << " Alive in blocks: "; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 47 | for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i) |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 48 | if (AliveBlocks[i]) cerr << i << ", "; |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 49 | cerr << " Used in blocks: "; |
| 50 | for (unsigned i = 0, e = UsedBlocks.size(); i != e; ++i) |
| 51 | if (UsedBlocks[i]) cerr << i << ", "; |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 52 | cerr << "\n Killed by:"; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 53 | if (Kills.empty()) |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 54 | cerr << " No instructions.\n"; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 55 | else { |
| 56 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 57 | cerr << "\n #" << i << ": " << *Kills[i]; |
| 58 | cerr << "\n"; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 62 | /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg. |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 63 | LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 64 | assert(TargetRegisterInfo::isVirtualRegister(RegIdx) && |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 65 | "getVarInfo: not a virtual register!"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 66 | RegIdx -= TargetRegisterInfo::FirstVirtualRegister; |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 67 | if (RegIdx >= VirtRegInfo.size()) { |
| 68 | if (RegIdx >= 2*VirtRegInfo.size()) |
| 69 | VirtRegInfo.resize(RegIdx*2); |
| 70 | else |
| 71 | VirtRegInfo.resize(2*VirtRegInfo.size()); |
| 72 | } |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 73 | VarInfo &VI = VirtRegInfo[RegIdx]; |
| 74 | VI.AliveBlocks.resize(MF->getNumBlockIDs()); |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 75 | VI.UsedBlocks.resize(MF->getNumBlockIDs()); |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 76 | return VI; |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 79 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo, |
| 80 | MachineBasicBlock *DefBlock, |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 81 | MachineBasicBlock *MBB, |
| 82 | std::vector<MachineBasicBlock*> &WorkList) { |
Chris Lattner | 8ba9771 | 2004-07-01 04:29:47 +0000 | [diff] [blame] | 83 | unsigned BBNum = MBB->getNumber(); |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 84 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 85 | // Check to see if this basic block is one of the killing blocks. If so, |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 86 | // remove it. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 87 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 88 | if (VRInfo.Kills[i]->getParent() == MBB) { |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 89 | VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry |
| 90 | break; |
| 91 | } |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 92 | |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 93 | if (MBB == DefBlock) return; // Terminate recursion |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 94 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 95 | if (VRInfo.AliveBlocks[BBNum]) |
| 96 | return; // We already know the block is live |
| 97 | |
| 98 | // Mark the variable known alive in this bb |
| 99 | VRInfo.AliveBlocks[BBNum] = true; |
| 100 | |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 101 | for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(), |
| 102 | E = MBB->pred_rend(); PI != E; ++PI) |
| 103 | WorkList.push_back(*PI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 106 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo, |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 107 | MachineBasicBlock *DefBlock, |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 108 | MachineBasicBlock *MBB) { |
| 109 | std::vector<MachineBasicBlock*> WorkList; |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 110 | MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 111 | |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 112 | while (!WorkList.empty()) { |
| 113 | MachineBasicBlock *Pred = WorkList.back(); |
| 114 | WorkList.pop_back(); |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 115 | MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList); |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 119 | void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 120 | MachineInstr *MI) { |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 121 | const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 122 | assert(MRI.getVRegDef(reg) && "Register use before def!"); |
Alkis Evlogimenos | 2e58a41 | 2004-09-01 22:34:52 +0000 | [diff] [blame] | 123 | |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 124 | unsigned BBNum = MBB->getNumber(); |
| 125 | |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 126 | VarInfo& VRInfo = getVarInfo(reg); |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 127 | VRInfo.UsedBlocks[BBNum] = true; |
Evan Cheng | 38b7ca6 | 2007-04-17 20:22:11 +0000 | [diff] [blame] | 128 | VRInfo.NumUses++; |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 129 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 130 | // Check to see if this basic block is already a kill block. |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 131 | if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) { |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 132 | // Yes, this register is killed in this basic block already. Increase the |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 133 | // live range by updating the kill instruction. |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 134 | VRInfo.Kills.back() = MI; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
| 138 | #ifndef NDEBUG |
| 139 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 140 | assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 143 | assert(MBB != MRI.getVRegDef(reg)->getParent() && |
Chris Lattner | 73d4adf | 2004-07-19 06:26:50 +0000 | [diff] [blame] | 144 | "Should have kill for defblock!"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 145 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 146 | // Add a new kill entry for this basic block. If this virtual register is |
| 147 | // already marked as alive in this basic block, that means it is alive in at |
| 148 | // least one of the successor blocks, it's not a kill. |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 149 | if (!VRInfo.AliveBlocks[BBNum]) |
Evan Cheng | e2ee996 | 2007-03-09 09:48:56 +0000 | [diff] [blame] | 150 | VRInfo.Kills.push_back(MI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 151 | |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 152 | // Update all dominating blocks to mark them as "known live". |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 153 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 154 | E = MBB->pred_end(); PI != E; ++PI) |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 155 | MarkVirtRegAliveInBlock(VRInfo, MRI.getVRegDef(reg)->getParent(), *PI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 158 | /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add |
| 159 | /// implicit defs to a machine instruction if there was an earlier def of its |
| 160 | /// super-register. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 161 | void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) { |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 162 | // Turn previous partial def's into read/mod/write. |
| 163 | for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) { |
| 164 | MachineInstr *Def = PhysRegPartDef[Reg][i]; |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 165 | |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 166 | // First one is just a def. This means the use is reading some undef bits. |
| 167 | if (i != 0) |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 168 | Def->addOperand(MachineOperand::CreateReg(Reg, |
| 169 | false /*IsDef*/, |
| 170 | true /*IsImp*/, |
| 171 | true /*IsKill*/)); |
| 172 | |
| 173 | Def->addOperand(MachineOperand::CreateReg(Reg, |
| 174 | true /*IsDef*/, |
| 175 | true /*IsImp*/)); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 176 | } |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 177 | |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 178 | PhysRegPartDef[Reg].clear(); |
| 179 | |
| 180 | // There was an earlier def of a super-register. Add implicit def to that MI. |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 181 | // |
| 182 | // A: EAX = ... |
| 183 | // B: ... = AX |
| 184 | // |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 185 | // Add implicit def to A. |
Evan Cheng | 6d6d352 | 2007-09-11 22:34:47 +0000 | [diff] [blame] | 186 | if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] && |
| 187 | !PhysRegUsed[Reg]) { |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 188 | MachineInstr *Def = PhysRegInfo[Reg]; |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 189 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 190 | if (!Def->modifiesRegister(Reg)) |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 191 | Def->addOperand(MachineOperand::CreateReg(Reg, |
| 192 | true /*IsDef*/, |
| 193 | true /*IsImp*/)); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Evan Cheng | 6d6d352 | 2007-09-11 22:34:47 +0000 | [diff] [blame] | 196 | // There is a now a proper use, forget about the last partial use. |
| 197 | PhysRegPartUse[Reg] = NULL; |
Alkis Evlogimenos | c55640f | 2004-01-13 21:16:25 +0000 | [diff] [blame] | 198 | PhysRegInfo[Reg] = MI; |
| 199 | PhysRegUsed[Reg] = true; |
Chris Lattner | 6d3848d | 2004-05-10 05:12:43 +0000 | [diff] [blame] | 200 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 201 | // Now reset the use information for the sub-registers. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 202 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 203 | unsigned SubReg = *SubRegs; ++SubRegs) { |
Bill Wendling | 1d5e819 | 2008-02-21 19:35:27 +0000 | [diff] [blame] | 204 | PhysRegPartUse[SubReg] = NULL; |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 205 | PhysRegInfo[SubReg] = MI; |
| 206 | PhysRegUsed[SubReg] = true; |
Chris Lattner | 6d3848d | 2004-05-10 05:12:43 +0000 | [diff] [blame] | 207 | } |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 208 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 209 | for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg); |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 210 | unsigned SuperReg = *SuperRegs; ++SuperRegs) { |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 211 | // Remember the partial use of this super-register if it was previously |
| 212 | // defined. |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 213 | bool HasPrevDef = PhysRegInfo[SuperReg] != NULL; |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 214 | |
| 215 | if (!HasPrevDef) |
Bill Wendling | c927cc8 | 2008-02-20 20:56:45 +0000 | [diff] [blame] | 216 | // No need to go up more levels. A def of a register also sets its sub- |
| 217 | // registers. So if PhysRegInfo[SuperReg] is NULL, it means SuperReg's |
| 218 | // super-registers are not previously defined. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 219 | for (const unsigned *SSRegs = TRI->getSuperRegisters(SuperReg); |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 220 | unsigned SSReg = *SSRegs; ++SSRegs) |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 221 | if (PhysRegInfo[SSReg] != NULL) { |
| 222 | HasPrevDef = true; |
| 223 | break; |
| 224 | } |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 225 | |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 226 | if (HasPrevDef) { |
| 227 | PhysRegInfo[SuperReg] = MI; |
| 228 | PhysRegPartUse[SuperReg] = MI; |
| 229 | } |
| 230 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 233 | /// addRegisterKills - For all of a register's sub-registers that are killed in |
Bill Wendling | fe8276c | 2008-02-20 19:09:14 +0000 | [diff] [blame] | 234 | /// at this machine instruction, mark them as "killed". (If the machine operand |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 235 | /// isn't found, add it first.) |
| 236 | void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI, |
| 237 | SmallSet<unsigned, 4> &SubKills) { |
| 238 | if (SubKills.count(Reg) == 0) { |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 239 | MI->addRegisterKilled(Reg, TRI, true); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 243 | for (const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 244 | unsigned SubReg = *SubRegs; ++SubRegs) |
| 245 | addRegisterKills(SubReg, MI, SubKills); |
| 246 | } |
| 247 | |
| 248 | /// HandlePhysRegKill - The recursive version of HandlePhysRegKill. Returns true |
| 249 | /// if: |
| 250 | /// |
| 251 | /// - The register has no sub-registers and the machine instruction is the |
| 252 | /// last def/use of the register, or |
| 253 | /// - The register has sub-registers and none of them are killed elsewhere. |
| 254 | /// |
Bill Wendling | 55574c2 | 2008-02-20 19:35:34 +0000 | [diff] [blame] | 255 | /// SubKills is filled with the set of sub-registers that are killed elsewhere. |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 256 | bool LiveVariables::HandlePhysRegKill(unsigned Reg, const MachineInstr *RefMI, |
| 257 | SmallSet<unsigned, 4> &SubKills) { |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 258 | const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 259 | |
| 260 | for (; unsigned SubReg = *SubRegs; ++SubRegs) { |
| 261 | const MachineInstr *LastRef = PhysRegInfo[SubReg]; |
| 262 | |
Evan Cheng | 0d8d316 | 2007-09-12 23:02:04 +0000 | [diff] [blame] | 263 | if (LastRef != RefMI || |
| 264 | !HandlePhysRegKill(SubReg, RefMI, SubKills)) |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 265 | SubKills.insert(SubReg); |
| 266 | } |
| 267 | |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 268 | if (*SubRegs == 0) { |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 269 | // No sub-registers, just check if reg is killed by RefMI. |
Evan Cheng | 9420201 | 2008-03-19 00:52:20 +0000 | [diff] [blame^] | 270 | if (PhysRegInfo[Reg] == RefMI && PhysRegInfo[Reg]->readsRegister(Reg)) { |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 271 | return true; |
Evan Cheng | 9420201 | 2008-03-19 00:52:20 +0000 | [diff] [blame^] | 272 | } |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 273 | } else if (SubKills.empty()) { |
| 274 | // None of the sub-registers are killed elsewhere. |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 275 | return true; |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Bill Wendling | 55574c2 | 2008-02-20 19:35:34 +0000 | [diff] [blame] | 281 | /// HandlePhysRegKill - Returns true if the whole register is killed in the |
| 282 | /// machine instruction. If only some of its sub-registers are killed in this |
| 283 | /// machine instruction, then mark those as killed and return false. |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 284 | bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) { |
| 285 | SmallSet<unsigned, 4> SubKills; |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 286 | |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 287 | if (HandlePhysRegKill(Reg, RefMI, SubKills)) { |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 288 | // This machine instruction kills this register. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 289 | RefMI->addRegisterKilled(Reg, TRI, true); |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 290 | return true; |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 291 | } |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 292 | |
| 293 | // Some sub-registers are killed by another machine instruction. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 294 | for (const unsigned *SubRegs = TRI->getImmediateSubRegisters(Reg); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 295 | unsigned SubReg = *SubRegs; ++SubRegs) |
| 296 | addRegisterKills(SubReg, RefMI, SubKills); |
| 297 | |
| 298 | return false; |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Evan Cheng | 9420201 | 2008-03-19 00:52:20 +0000 | [diff] [blame^] | 301 | /// hasRegisterUseBelow - Return true if the specified register is used after |
| 302 | /// the current instruction and before it's next definition. |
| 303 | bool LiveVariables::hasRegisterUseBelow(unsigned Reg, |
| 304 | MachineBasicBlock::iterator I, |
| 305 | MachineBasicBlock *MBB) { |
| 306 | if (I == MBB->end()) |
| 307 | return false; |
| 308 | ++I; |
| 309 | // FIXME: This is slow. We probably need a smarter solution. Possibilities: |
| 310 | // 1. Scan all instructions once and build def / use information of physical |
| 311 | // registers. We also need a fast way to compare relative ordering of |
| 312 | // instructions. |
| 313 | // 2. Cache information so this function only has to scan instructions that |
| 314 | // read / def physical instructions. |
| 315 | for (MachineBasicBlock::iterator E = MBB->end(); I != E; ++I) { |
| 316 | MachineInstr *MI = I; |
| 317 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 318 | const MachineOperand &MO = MI->getOperand(i); |
| 319 | if (!MO.isRegister() || MO.getReg() != Reg) |
| 320 | continue; |
| 321 | if (MO.isDef()) |
| 322 | return false; |
| 323 | return true; |
| 324 | } |
| 325 | } |
| 326 | return false; |
| 327 | } |
| 328 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 329 | void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) { |
| 330 | // Does this kill a previous version of this register? |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 331 | if (MachineInstr *LastRef = PhysRegInfo[Reg]) { |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 332 | if (PhysRegUsed[Reg]) { |
| 333 | if (!HandlePhysRegKill(Reg, LastRef)) { |
| 334 | if (PhysRegPartUse[Reg]) |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 335 | PhysRegPartUse[Reg]->addRegisterKilled(Reg, TRI, true); |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 336 | } |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 337 | } else if (PhysRegPartUse[Reg]) { |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 338 | // Add implicit use / kill to last partial use. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 339 | PhysRegPartUse[Reg]->addRegisterKilled(Reg, TRI, true); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 340 | } else if (LastRef != MI) { |
Evan Cheng | 5942efb | 2007-11-05 03:11:55 +0000 | [diff] [blame] | 341 | // Defined, but not used. However, watch out for cases where a super-reg |
| 342 | // is also defined on the same MI. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 343 | LastRef->addRegisterDead(Reg, TRI); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 345 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 346 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 347 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 348 | unsigned SubReg = *SubRegs; ++SubRegs) { |
| 349 | if (MachineInstr *LastRef = PhysRegInfo[SubReg]) { |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 350 | if (PhysRegUsed[SubReg]) { |
| 351 | if (!HandlePhysRegKill(SubReg, LastRef)) { |
| 352 | if (PhysRegPartUse[SubReg]) |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 353 | PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, TRI, true); |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 354 | } |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 355 | } else if (PhysRegPartUse[SubReg]) { |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 356 | // Add implicit use / kill to last use of a sub-register. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 357 | PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, TRI, true); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 358 | } else if (LastRef != MI) { |
Evan Cheng | 6d6d352 | 2007-09-11 22:34:47 +0000 | [diff] [blame] | 359 | // This must be a def of the subreg on the same MI. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 360 | LastRef->addRegisterDead(SubReg, TRI); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 361 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 362 | } |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 365 | if (MI) { |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 366 | for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 367 | unsigned SuperReg = *SuperRegs; ++SuperRegs) { |
Evan Cheng | 6d6d352 | 2007-09-11 22:34:47 +0000 | [diff] [blame] | 368 | if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) { |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 369 | // The larger register is previously defined. Now a smaller part is |
Evan Cheng | 9420201 | 2008-03-19 00:52:20 +0000 | [diff] [blame^] | 370 | // being re-defined. Treat it as read/mod/write if there are uses |
| 371 | // below. |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 372 | // EAX = |
| 373 | // AX = EAX<imp-use,kill>, EAX<imp-def> |
Evan Cheng | 9420201 | 2008-03-19 00:52:20 +0000 | [diff] [blame^] | 374 | // ... |
| 375 | /// = EAX |
| 376 | if (MI && hasRegisterUseBelow(SuperReg, MI, MI->getParent())) { |
| 377 | MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/, |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 378 | true/*IsImp*/,true/*IsKill*/)); |
Evan Cheng | 9420201 | 2008-03-19 00:52:20 +0000 | [diff] [blame^] | 379 | MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/, |
| 380 | true/*IsImp*/)); |
| 381 | PhysRegInfo[SuperReg] = MI; |
| 382 | } else { |
| 383 | PhysRegInfo[SuperReg]->addRegisterKilled(SuperReg, TRI, true); |
| 384 | PhysRegInfo[SuperReg] = NULL; |
| 385 | } |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 386 | PhysRegUsed[SuperReg] = false; |
Evan Cheng | 8b966d9 | 2007-05-14 20:39:18 +0000 | [diff] [blame] | 387 | PhysRegPartUse[SuperReg] = NULL; |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 388 | } else { |
| 389 | // Remember this partial def. |
| 390 | PhysRegPartDef[SuperReg].push_back(MI); |
| 391 | } |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | PhysRegInfo[Reg] = MI; |
| 395 | PhysRegUsed[Reg] = false; |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 396 | PhysRegPartDef[Reg].clear(); |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 397 | PhysRegPartUse[Reg] = NULL; |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 398 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 399 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 400 | unsigned SubReg = *SubRegs; ++SubRegs) { |
| 401 | PhysRegInfo[SubReg] = MI; |
| 402 | PhysRegUsed[SubReg] = false; |
Evan Cheng | 21b3bf0 | 2007-08-01 20:18:21 +0000 | [diff] [blame] | 403 | PhysRegPartDef[SubReg].clear(); |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 404 | PhysRegPartUse[SubReg] = NULL; |
| 405 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 406 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 409 | bool LiveVariables::runOnMachineFunction(MachineFunction &mf) { |
| 410 | MF = &mf; |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 411 | TRI = MF->getTarget().getRegisterInfo(); |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 412 | MachineRegisterInfo& MRI = mf.getRegInfo(); |
Chris Lattner | 96aef89 | 2004-02-09 01:35:21 +0000 | [diff] [blame] | 413 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 414 | ReservedRegisters = TRI->getReservedRegs(mf); |
Chris Lattner | 5cdfbad | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 415 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 416 | unsigned NumRegs = TRI->getNumRegs(); |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 417 | PhysRegInfo = new MachineInstr*[NumRegs]; |
| 418 | PhysRegUsed = new bool[NumRegs]; |
| 419 | PhysRegPartUse = new MachineInstr*[NumRegs]; |
| 420 | PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs]; |
| 421 | PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()]; |
| 422 | std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0); |
| 423 | std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false); |
| 424 | std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 425 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 426 | /// Get some space for a respectable number of registers. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 427 | VirtRegInfo.resize(64); |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 428 | |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 429 | analyzePHINodes(mf); |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 430 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 431 | // Calculate live variable information in depth first order on the CFG of the |
| 432 | // function. This guarantees that we will see the definition of a virtual |
| 433 | // register before its uses due to dominance properties of SSA (except for PHI |
| 434 | // nodes, which are treated as a special case). |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 435 | MachineBasicBlock *Entry = MF->begin(); |
Evan Cheng | 0410407 | 2007-06-27 05:23:00 +0000 | [diff] [blame] | 436 | SmallPtrSet<MachineBasicBlock*,16> Visited; |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 437 | |
Evan Cheng | 0410407 | 2007-06-27 05:23:00 +0000 | [diff] [blame] | 438 | for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> > |
| 439 | DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); |
| 440 | DFI != E; ++DFI) { |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 441 | MachineBasicBlock *MBB = *DFI; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 442 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 443 | // Mark live-in registers as live-in. |
| 444 | for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(), |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 445 | EE = MBB->livein_end(); II != EE; ++II) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 446 | assert(TargetRegisterInfo::isPhysicalRegister(*II) && |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 447 | "Cannot have a live-in virtual register!"); |
| 448 | HandlePhysRegDef(*II, 0); |
| 449 | } |
| 450 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 451 | // Loop over all of the instructions, processing them. |
| 452 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 453 | I != E; ++I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 454 | MachineInstr *MI = I; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 455 | |
| 456 | // Process all of the operands of the instruction... |
| 457 | unsigned NumOperandsToProcess = MI->getNumOperands(); |
| 458 | |
| 459 | // Unless it is a PHI node. In this case, ONLY process the DEF, not any |
| 460 | // of the uses. They will be handled in other basic blocks. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 461 | if (MI->getOpcode() == TargetInstrInfo::PHI) |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 462 | NumOperandsToProcess = 1; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 463 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 464 | // Process all uses. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 465 | for (unsigned i = 0; i != NumOperandsToProcess; ++i) { |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 466 | const MachineOperand &MO = MI->getOperand(i); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 467 | |
Chris Lattner | d8f44e0 | 2006-09-05 20:19:27 +0000 | [diff] [blame] | 468 | if (MO.isRegister() && MO.isUse() && MO.getReg()) { |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 469 | unsigned MOReg = MO.getReg(); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 470 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 471 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) |
| 472 | HandleVirtRegUse(MOReg, MBB, MI); |
| 473 | else if (TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 474 | !ReservedRegisters[MOReg]) |
| 475 | HandlePhysRegUse(MOReg, MI); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 476 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 479 | // Process all defs. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 480 | for (unsigned i = 0; i != NumOperandsToProcess; ++i) { |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 481 | const MachineOperand &MO = MI->getOperand(i); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 482 | |
Chris Lattner | d8f44e0 | 2006-09-05 20:19:27 +0000 | [diff] [blame] | 483 | if (MO.isRegister() && MO.isDef() && MO.getReg()) { |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 484 | unsigned MOReg = MO.getReg(); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 485 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 486 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) { |
| 487 | VarInfo &VRInfo = getVarInfo(MOReg); |
| 488 | |
Evan Cheng | bb4151b | 2008-02-05 20:04:18 +0000 | [diff] [blame] | 489 | if (VRInfo.AliveBlocks.none()) |
| 490 | // If vr is not alive in any block, then defaults to dead. |
| 491 | VRInfo.Kills.push_back(MI); |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 492 | } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 493 | !ReservedRegisters[MOReg]) { |
| 494 | HandlePhysRegDef(MOReg, MI); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | // Handle any virtual assignments from PHI nodes which might be at the |
| 501 | // bottom of this basic block. We check all of our successor blocks to see |
| 502 | // if they have PHI nodes, and if so, we simulate an assignment at the end |
| 503 | // of the current block. |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 504 | if (!PHIVarInfo[MBB->getNumber()].empty()) { |
| 505 | SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()]; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 506 | |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 507 | for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(), |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 508 | E = VarInfoVec.end(); I != E; ++I) |
| 509 | // Mark it alive only in the block we are representing. |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 510 | MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(), |
| 511 | MBB); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 512 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 513 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 514 | // Finally, if the last instruction in the block is a return, make sure to |
| 515 | // mark it as using all of the live-out values in the function. |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 516 | if (!MBB->empty() && MBB->back().getDesc().isReturn()) { |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 517 | MachineInstr *Ret = &MBB->back(); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 519 | for (MachineRegisterInfo::liveout_iterator |
| 520 | I = MF->getRegInfo().liveout_begin(), |
| 521 | E = MF->getRegInfo().liveout_end(); I != E; ++I) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 522 | assert(TargetRegisterInfo::isPhysicalRegister(*I) && |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 523 | "Cannot have a live-in virtual register!"); |
| 524 | HandlePhysRegUse(*I, Ret); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 525 | |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 526 | // Add live-out registers as implicit uses. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 527 | if (!Ret->readsRegister(*I)) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 528 | Ret->addOperand(MachineOperand::CreateReg(*I, false, true)); |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 532 | // Loop over PhysRegInfo, killing any registers that are available at the |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 533 | // end of the basic block. This also resets the PhysRegInfo map. |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 534 | for (unsigned i = 0; i != NumRegs; ++i) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 535 | if (PhysRegInfo[i]) |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 536 | HandlePhysRegDef(i, 0); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 537 | |
| 538 | // Clear some states between BB's. These are purely local information. |
Evan Cheng | ade31f9 | 2007-04-25 21:34:08 +0000 | [diff] [blame] | 539 | for (unsigned i = 0; i != NumRegs; ++i) |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 540 | PhysRegPartDef[i].clear(); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 541 | |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 542 | std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0); |
| 543 | std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false); |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 544 | std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 547 | // Convert and transfer the dead / killed information we have gathered into |
| 548 | // VirtRegInfo onto MI's. |
Evan Cheng | f0e3bb1 | 2007-03-09 06:02:17 +0000 | [diff] [blame] | 549 | for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 550 | for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) |
| 551 | if (VirtRegInfo[i].Kills[j] == |
| 552 | MRI.getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister)) |
| 553 | VirtRegInfo[i] |
| 554 | .Kills[j]->addRegisterDead(i + |
| 555 | TargetRegisterInfo::FirstVirtualRegister, |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 556 | TRI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 557 | else |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 558 | VirtRegInfo[i] |
| 559 | .Kills[j]->addRegisterKilled(i + |
| 560 | TargetRegisterInfo::FirstVirtualRegister, |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 561 | TRI); |
Chris Lattner | a5287a6 | 2004-07-01 04:24:29 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 9fb6cf1 | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 563 | // Check to make sure there are no unreachable blocks in the MC CFG for the |
| 564 | // function. If so, it is due to a bug in the instruction selector or some |
| 565 | // other part of the code generator if this happens. |
| 566 | #ifndef NDEBUG |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 567 | for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i) |
Chris Lattner | 9fb6cf1 | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 568 | assert(Visited.count(&*i) != 0 && "unreachable basic block found"); |
| 569 | #endif |
| 570 | |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 571 | delete[] PhysRegInfo; |
| 572 | delete[] PhysRegUsed; |
| 573 | delete[] PhysRegPartUse; |
| 574 | delete[] PhysRegPartDef; |
| 575 | delete[] PHIVarInfo; |
| 576 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 577 | return false; |
| 578 | } |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 579 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 580 | /// instructionChanged - When the address of an instruction changes, this method |
| 581 | /// should be called so that live variables can update its internal data |
| 582 | /// structures. This removes the records for OldMI, transfering them to the |
| 583 | /// records for NewMI. |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 584 | void LiveVariables::instructionChanged(MachineInstr *OldMI, |
| 585 | MachineInstr *NewMI) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 586 | // If the instruction defines any virtual registers, update the VarInfo, |
| 587 | // kill and dead information for the instruction. |
Alkis Evlogimenos | a8db01a | 2004-03-30 22:44:39 +0000 | [diff] [blame] | 588 | for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) { |
| 589 | MachineOperand &MO = OldMI->getOperand(i); |
Chris Lattner | d45be36 | 2005-01-19 17:09:15 +0000 | [diff] [blame] | 590 | if (MO.isRegister() && MO.getReg() && |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 591 | TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 592 | unsigned Reg = MO.getReg(); |
| 593 | VarInfo &VI = getVarInfo(Reg); |
Chris Lattner | d45be36 | 2005-01-19 17:09:15 +0000 | [diff] [blame] | 594 | if (MO.isDef()) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 595 | if (MO.isDead()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 596 | MO.setIsDead(false); |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 597 | addVirtualRegisterDead(Reg, NewMI); |
| 598 | } |
Chris Lattner | 2a6e163 | 2005-01-19 17:11:51 +0000 | [diff] [blame] | 599 | } |
Dan Gohman | c674a92 | 2007-07-20 23:17:34 +0000 | [diff] [blame] | 600 | if (MO.isKill()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 601 | MO.setIsKill(false); |
Dan Gohman | c674a92 | 2007-07-20 23:17:34 +0000 | [diff] [blame] | 602 | addVirtualRegisterKilled(Reg, NewMI); |
Chris Lattner | d45be36 | 2005-01-19 17:09:15 +0000 | [diff] [blame] | 603 | } |
Dan Gohman | c674a92 | 2007-07-20 23:17:34 +0000 | [diff] [blame] | 604 | // If this is a kill of the value, update the VI kills list. |
| 605 | if (VI.removeKill(OldMI)) |
| 606 | VI.Kills.push_back(NewMI); // Yes, there was a kill of it |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 610 | |
| 611 | /// removeVirtualRegistersKilled - Remove all killed info for the specified |
| 612 | /// instruction. |
| 613 | void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 614 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 615 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 616 | if (MO.isRegister() && MO.isKill()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 617 | MO.setIsKill(false); |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 618 | unsigned Reg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 619 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 620 | bool removed = getVarInfo(Reg).removeKill(MI); |
| 621 | assert(removed && "kill not in register's VarInfo?"); |
| 622 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /// removeVirtualRegistersDead - Remove all of the dead registers for the |
| 628 | /// specified instruction from the live variable information. |
| 629 | void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 630 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 631 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 632 | if (MO.isRegister() && MO.isDead()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 633 | MO.setIsDead(false); |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 634 | unsigned Reg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 635 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 636 | bool removed = getVarInfo(Reg).removeKill(MI); |
| 637 | assert(removed && "kill not in register's VarInfo?"); |
| 638 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 643 | /// analyzePHINodes - Gather information about the PHI nodes in here. In |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 644 | /// particular, we want to map the variable information of a virtual register |
| 645 | /// which is used in a PHI node. We map that to the BB the vreg is coming from. |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 646 | /// |
| 647 | void LiveVariables::analyzePHINodes(const MachineFunction& Fn) { |
| 648 | for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end(); |
| 649 | I != E; ++I) |
| 650 | for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end(); |
| 651 | BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) |
| 652 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 653 | PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()] |
| 654 | .push_back(BBI->getOperand(i).getReg()); |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 655 | } |