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 | // |
Lang Hames | 16d6eae | 2012-04-01 19:27:25 +0000 | [diff] [blame] | 17 | // This class computes live variables using a sparse implementation based on |
Chris Lattner | 5cdfbad | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DepthFirstIterator.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
| 32 | #include "llvm/ADT/SmallPtrSet.h" |
| 33 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/Passes.h" |
David Greene | 1d44df6 | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 657b4d1 | 2005-08-24 00:09:33 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Chris Lattner | 49a5aaa | 2004-01-30 22:08:53 +0000 | [diff] [blame] | 42 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 43 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 44 | char LiveVariables::ID = 0; |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 45 | char &llvm::LiveVariablesID = LiveVariables::ID; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS_BEGIN(LiveVariables, "livevars", |
| 47 | "Live Variable Analysis", false, false) |
| 48 | INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim) |
| 49 | INITIALIZE_PASS_END(LiveVariables, "livevars", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 50 | "Live Variable Analysis", false, false) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 51 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 52 | |
| 53 | void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const { |
| 54 | AU.addRequiredID(UnreachableMachineBlockElimID); |
| 55 | AU.setPreservesAll(); |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 56 | MachineFunctionPass::getAnalysisUsage(AU); |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 59 | MachineInstr * |
| 60 | LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const { |
| 61 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) |
| 62 | if (Kills[i]->getParent() == MBB) |
| 63 | return Kills[i]; |
| 64 | return NULL; |
| 65 | } |
| 66 | |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 67 | void LiveVariables::VarInfo::dump() const { |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 68 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
David Greene | 1d44df6 | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 69 | dbgs() << " Alive in blocks: "; |
Jeffrey Yasskin | 493a3d0 | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 70 | for (SparseBitVector<>::iterator I = AliveBlocks.begin(), |
| 71 | E = AliveBlocks.end(); I != E; ++I) |
David Greene | 1d44df6 | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 72 | dbgs() << *I << ", "; |
| 73 | dbgs() << "\n Killed by:"; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 74 | if (Kills.empty()) |
David Greene | 1d44df6 | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 75 | dbgs() << " No instructions.\n"; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 76 | else { |
| 77 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) |
David Greene | 1d44df6 | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 78 | dbgs() << "\n #" << i << ": " << *Kills[i]; |
| 79 | dbgs() << "\n"; |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 80 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 81 | #endif |
Chris Lattner | dacceef | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 84 | /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg. |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 85 | LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 86 | assert(TargetRegisterInfo::isVirtualRegister(RegIdx) && |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 87 | "getVarInfo: not a virtual register!"); |
Jakob Stoklund Olesen | b421c56 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 88 | VirtRegInfo.grow(RegIdx); |
Jeffrey Yasskin | 493a3d0 | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 89 | return VirtRegInfo[RegIdx]; |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 92 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo, |
| 93 | MachineBasicBlock *DefBlock, |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 94 | MachineBasicBlock *MBB, |
| 95 | std::vector<MachineBasicBlock*> &WorkList) { |
Chris Lattner | 8ba9771 | 2004-07-01 04:29:47 +0000 | [diff] [blame] | 96 | unsigned BBNum = MBB->getNumber(); |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 97 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 98 | // 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] | 99 | // remove it. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 100 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 101 | if (VRInfo.Kills[i]->getParent() == MBB) { |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 102 | VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry |
| 103 | break; |
| 104 | } |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 105 | |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 106 | if (MBB == DefBlock) return; // Terminate recursion |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 107 | |
Jeffrey Yasskin | 493a3d0 | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 108 | if (VRInfo.AliveBlocks.test(BBNum)) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 109 | return; // We already know the block is live |
| 110 | |
| 111 | // Mark the variable known alive in this bb |
Jeffrey Yasskin | 493a3d0 | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 112 | VRInfo.AliveBlocks.set(BBNum); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 113 | |
Jakob Stoklund Olesen | 9ab3dbe | 2012-03-09 23:41:44 +0000 | [diff] [blame] | 114 | assert(MBB != &MF->front() && "Can't find reaching def for virtreg"); |
Benjamin Kramer | f337fb2 | 2011-03-08 17:28:36 +0000 | [diff] [blame] | 115 | WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend()); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 118 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo, |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 119 | MachineBasicBlock *DefBlock, |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 120 | MachineBasicBlock *MBB) { |
| 121 | std::vector<MachineBasicBlock*> WorkList; |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 122 | MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList); |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 123 | |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 124 | while (!WorkList.empty()) { |
| 125 | MachineBasicBlock *Pred = WorkList.back(); |
| 126 | WorkList.pop_back(); |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 127 | MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList); |
Evan Cheng | 5618490 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 131 | void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 132 | MachineInstr *MI) { |
Evan Cheng | ea1d9cd | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 133 | assert(MRI->getVRegDef(reg) && "Register use before def!"); |
Alkis Evlogimenos | 2e58a41 | 2004-09-01 22:34:52 +0000 | [diff] [blame] | 134 | |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 135 | unsigned BBNum = MBB->getNumber(); |
| 136 | |
Owen Anderson | 7047dd4 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 137 | VarInfo& VRInfo = getVarInfo(reg); |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 138 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 139 | // Check to see if this basic block is already a kill block. |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 140 | if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) { |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 141 | // Yes, this register is killed in this basic block already. Increase the |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 142 | // live range by updating the kill instruction. |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 143 | VRInfo.Kills.back() = MI; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
| 147 | #ifndef NDEBUG |
| 148 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 149 | assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Bill Wendling | ebcba61 | 2008-06-23 23:41:14 +0000 | [diff] [blame] | 152 | // This situation can occur: |
| 153 | // |
| 154 | // ,------. |
| 155 | // | | |
| 156 | // | v |
| 157 | // | t2 = phi ... t1 ... |
| 158 | // | | |
| 159 | // | v |
| 160 | // | t1 = ... |
| 161 | // | ... = ... t1 ... |
| 162 | // | | |
| 163 | // `------' |
| 164 | // |
| 165 | // where there is a use in a PHI node that's a predecessor to the defining |
| 166 | // block. We don't want to mark all predecessors as having the value "alive" |
| 167 | // in this case. |
| 168 | if (MBB == MRI->getVRegDef(reg)->getParent()) return; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 169 | |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 170 | // Add a new kill entry for this basic block. If this virtual register is |
| 171 | // already marked as alive in this basic block, that means it is alive in at |
| 172 | // least one of the successor blocks, it's not a kill. |
Jeffrey Yasskin | 493a3d0 | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 173 | if (!VRInfo.AliveBlocks.test(BBNum)) |
Evan Cheng | e2ee996 | 2007-03-09 09:48:56 +0000 | [diff] [blame] | 174 | VRInfo.Kills.push_back(MI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 175 | |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 176 | // Update all dominating blocks to mark them as "known live". |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 177 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 178 | E = MBB->pred_end(); PI != E; ++PI) |
Evan Cheng | ea1d9cd | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 179 | MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Dan Gohman | 3bdf5fe | 2008-09-21 21:11:41 +0000 | [diff] [blame] | 182 | void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr *MI) { |
| 183 | VarInfo &VRInfo = getVarInfo(Reg); |
| 184 | |
Jeffrey Yasskin | 493a3d0 | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 185 | if (VRInfo.AliveBlocks.empty()) |
Dan Gohman | 3bdf5fe | 2008-09-21 21:11:41 +0000 | [diff] [blame] | 186 | // If vr is not alive in any block, then defaults to dead. |
| 187 | VRInfo.Kills.push_back(MI); |
| 188 | } |
| 189 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 190 | /// FindLastPartialDef - Return the last partial def of the specified register. |
Evan Cheng | 60c7df2 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 191 | /// Also returns the sub-registers that're defined by the instruction. |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 192 | MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg, |
Evan Cheng | 60c7df2 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 193 | SmallSet<unsigned,4> &PartDefRegs) { |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 194 | unsigned LastDefReg = 0; |
| 195 | unsigned LastDefDist = 0; |
| 196 | MachineInstr *LastDef = NULL; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 197 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 198 | unsigned SubReg = *SubRegs; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 199 | MachineInstr *Def = PhysRegDef[SubReg]; |
| 200 | if (!Def) |
| 201 | continue; |
| 202 | unsigned Dist = DistanceMap[Def]; |
| 203 | if (Dist > LastDefDist) { |
| 204 | LastDefReg = SubReg; |
| 205 | LastDef = Def; |
| 206 | LastDefDist = Dist; |
| 207 | } |
| 208 | } |
Evan Cheng | 60c7df2 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 209 | |
| 210 | if (!LastDef) |
| 211 | return 0; |
| 212 | |
| 213 | PartDefRegs.insert(LastDefReg); |
| 214 | for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) { |
| 215 | MachineOperand &MO = LastDef->getOperand(i); |
| 216 | if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) |
| 217 | continue; |
| 218 | unsigned DefReg = MO.getReg(); |
| 219 | if (TRI->isSubRegister(Reg, DefReg)) { |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 220 | for (MCSubRegIterator SubRegs(DefReg, TRI, /*IncludeSelf=*/true); |
| 221 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 222 | PartDefRegs.insert(*SubRegs); |
Evan Cheng | 60c7df2 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 225 | return LastDef; |
| 226 | } |
| 227 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 228 | /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add |
| 229 | /// implicit defs to a machine instruction if there was an earlier def of its |
| 230 | /// super-register. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 231 | void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) { |
Evan Cheng | 236490d | 2009-11-13 20:36:40 +0000 | [diff] [blame] | 232 | MachineInstr *LastDef = PhysRegDef[Reg]; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 233 | // If there was a previous use or a "full" def all is well. |
Evan Cheng | 236490d | 2009-11-13 20:36:40 +0000 | [diff] [blame] | 234 | if (!LastDef && !PhysRegUse[Reg]) { |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 235 | // Otherwise, the last sub-register def implicitly defines this register. |
| 236 | // e.g. |
| 237 | // AH = |
| 238 | // AL = ... <imp-def EAX>, <imp-kill AH> |
| 239 | // = AH |
| 240 | // ... |
| 241 | // = EAX |
| 242 | // All of the sub-registers must have been defined before the use of Reg! |
Evan Cheng | 60c7df2 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 243 | SmallSet<unsigned, 4> PartDefRegs; |
| 244 | MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs); |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 245 | // If LastPartialDef is NULL, it must be using a livein register. |
| 246 | if (LastPartialDef) { |
| 247 | LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/, |
| 248 | true/*IsImp*/)); |
| 249 | PhysRegDef[Reg] = LastPartialDef; |
Owen Anderson | bbf5583 | 2008-08-14 23:41:38 +0000 | [diff] [blame] | 250 | SmallSet<unsigned, 8> Processed; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 251 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 252 | unsigned SubReg = *SubRegs; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 253 | if (Processed.count(SubReg)) |
| 254 | continue; |
Evan Cheng | 60c7df2 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 255 | if (PartDefRegs.count(SubReg)) |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 256 | continue; |
| 257 | // This part of Reg was defined before the last partial def. It's killed |
| 258 | // here. |
| 259 | LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg, |
| 260 | false/*IsDef*/, |
| 261 | true/*IsImp*/)); |
| 262 | PhysRegDef[SubReg] = LastPartialDef; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 263 | for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS) |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 264 | Processed.insert(*SS); |
| 265 | } |
| 266 | } |
Evan Cheng | bfe8afa | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 267 | } else if (LastDef && !PhysRegUse[Reg] && |
| 268 | !LastDef->findRegisterDefOperand(Reg)) |
Evan Cheng | 236490d | 2009-11-13 20:36:40 +0000 | [diff] [blame] | 269 | // Last def defines the super register, add an implicit def of reg. |
Evan Cheng | bfe8afa | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 270 | LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/, |
| 271 | true/*IsImp*/)); |
Bill Wendling | 90a3868 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 272 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 273 | // Remember this use. |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 274 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 275 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 276 | PhysRegUse[*SubRegs] = MI; |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 279 | /// FindLastRefOrPartRef - Return the last reference or partial reference of |
| 280 | /// the specified register. |
| 281 | MachineInstr *LiveVariables::FindLastRefOrPartRef(unsigned Reg) { |
| 282 | MachineInstr *LastDef = PhysRegDef[Reg]; |
| 283 | MachineInstr *LastUse = PhysRegUse[Reg]; |
| 284 | if (!LastDef && !LastUse) |
Chris Lattner | 98cdfc7 | 2010-06-14 18:28:34 +0000 | [diff] [blame] | 285 | return 0; |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 286 | |
| 287 | MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef; |
| 288 | unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef]; |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 289 | unsigned LastPartDefDist = 0; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 290 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 291 | unsigned SubReg = *SubRegs; |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 292 | MachineInstr *Def = PhysRegDef[SubReg]; |
| 293 | if (Def && Def != LastDef) { |
| 294 | // There was a def of this sub-register in between. This is a partial |
| 295 | // def, keep track of the last one. |
| 296 | unsigned Dist = DistanceMap[Def]; |
Benjamin Kramer | e7078ae | 2010-01-07 17:29:08 +0000 | [diff] [blame] | 297 | if (Dist > LastPartDefDist) |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 298 | LastPartDefDist = Dist; |
Benjamin Kramer | e7078ae | 2010-01-07 17:29:08 +0000 | [diff] [blame] | 299 | } else if (MachineInstr *Use = PhysRegUse[SubReg]) { |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 300 | unsigned Dist = DistanceMap[Use]; |
| 301 | if (Dist > LastRefOrPartRefDist) { |
| 302 | LastRefOrPartRefDist = Dist; |
| 303 | LastRefOrPartRef = Use; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return LastRefOrPartRef; |
| 309 | } |
| 310 | |
Evan Cheng | a894ae1 | 2009-01-20 21:25:12 +0000 | [diff] [blame] | 311 | bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) { |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 312 | MachineInstr *LastDef = PhysRegDef[Reg]; |
| 313 | MachineInstr *LastUse = PhysRegUse[Reg]; |
| 314 | if (!LastDef && !LastUse) |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 315 | return false; |
| 316 | |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 317 | MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 318 | unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef]; |
| 319 | // The whole register is used. |
| 320 | // AL = |
| 321 | // AH = |
| 322 | // |
| 323 | // = AX |
| 324 | // = AL, AX<imp-use, kill> |
| 325 | // AX = |
| 326 | // |
| 327 | // Or whole register is defined, but not used at all. |
| 328 | // AX<dead> = |
| 329 | // ... |
| 330 | // AX = |
| 331 | // |
| 332 | // Or whole register is defined, but only partly used. |
| 333 | // AX<dead> = AL<imp-def> |
| 334 | // = AL<kill> |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 335 | // AX = |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 336 | MachineInstr *LastPartDef = 0; |
| 337 | unsigned LastPartDefDist = 0; |
Owen Anderson | bbf5583 | 2008-08-14 23:41:38 +0000 | [diff] [blame] | 338 | SmallSet<unsigned, 8> PartUses; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 339 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 340 | unsigned SubReg = *SubRegs; |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 341 | MachineInstr *Def = PhysRegDef[SubReg]; |
| 342 | if (Def && Def != LastDef) { |
| 343 | // There was a def of this sub-register in between. This is a partial |
| 344 | // def, keep track of the last one. |
| 345 | unsigned Dist = DistanceMap[Def]; |
| 346 | if (Dist > LastPartDefDist) { |
| 347 | LastPartDefDist = Dist; |
| 348 | LastPartDef = Def; |
| 349 | } |
| 350 | continue; |
| 351 | } |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 352 | if (MachineInstr *Use = PhysRegUse[SubReg]) { |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 353 | for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); SS.isValid(); |
| 354 | ++SS) |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 355 | PartUses.insert(*SS); |
| 356 | unsigned Dist = DistanceMap[Use]; |
| 357 | if (Dist > LastRefOrPartRefDist) { |
| 358 | LastRefOrPartRefDist = Dist; |
| 359 | LastRefOrPartRef = Use; |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 360 | } |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
Evan Cheng | a894ae1 | 2009-01-20 21:25:12 +0000 | [diff] [blame] | 363 | |
Jakob Stoklund Olesen | 53e000b | 2010-03-05 21:49:17 +0000 | [diff] [blame] | 364 | if (!PhysRegUse[Reg]) { |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 365 | // Partial uses. Mark register def dead and add implicit def of |
| 366 | // sub-registers which are used. |
| 367 | // EAX<dead> = op AL<imp-def> |
| 368 | // That is, EAX def is dead but AL def extends pass it. |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 369 | PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 370 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 371 | unsigned SubReg = *SubRegs; |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 372 | if (!PartUses.count(SubReg)) |
| 373 | continue; |
| 374 | bool NeedDef = true; |
| 375 | if (PhysRegDef[Reg] == PhysRegDef[SubReg]) { |
| 376 | MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg); |
| 377 | if (MO) { |
| 378 | NeedDef = false; |
| 379 | assert(!MO->isDead()); |
Evan Cheng | 2c4d96d | 2009-07-06 21:34:05 +0000 | [diff] [blame] | 380 | } |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 381 | } |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 382 | if (NeedDef) |
| 383 | PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg, |
| 384 | true/*IsDef*/, true/*IsImp*/)); |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 385 | MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg); |
| 386 | if (LastSubRef) |
| 387 | LastSubRef->addRegisterKilled(SubReg, TRI, true); |
| 388 | else { |
| 389 | LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true); |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 390 | for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); |
| 391 | SS.isValid(); ++SS) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 392 | PhysRegUse[*SS] = LastRefOrPartRef; |
Evan Cheng | a4025df | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 393 | } |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 394 | for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS) |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 395 | PartUses.erase(*SS); |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 396 | } |
Jakob Stoklund Olesen | 53e000b | 2010-03-05 21:49:17 +0000 | [diff] [blame] | 397 | } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) { |
| 398 | if (LastPartDef) |
| 399 | // The last partial def kills the register. |
| 400 | LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/, |
| 401 | true/*IsImp*/, true/*IsKill*/)); |
| 402 | else { |
| 403 | MachineOperand *MO = |
| 404 | LastRefOrPartRef->findRegisterDefOperand(Reg, false, TRI); |
| 405 | bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg; |
| 406 | // If the last reference is the last def, then it's not used at all. |
| 407 | // That is, unless we are currently processing the last reference itself. |
| 408 | LastRefOrPartRef->addRegisterDead(Reg, TRI, true); |
| 409 | if (NeedEC) { |
| 410 | // If we are adding a subreg def and the superreg def is marked early |
| 411 | // clobber, add an early clobber marker to the subreg def. |
| 412 | MO = LastRefOrPartRef->findRegisterDefOperand(Reg); |
| 413 | if (MO) |
| 414 | MO->setIsEarlyClobber(); |
| 415 | } |
| 416 | } |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 417 | } else |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 418 | LastRefOrPartRef->addRegisterKilled(Reg, TRI, true); |
| 419 | return true; |
| 420 | } |
| 421 | |
Jakob Stoklund Olesen | 8c47ad8 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 422 | void LiveVariables::HandleRegMask(const MachineOperand &MO) { |
| 423 | // Call HandlePhysRegKill() for all live registers clobbered by Mask. |
| 424 | // Clobbered registers are always dead, sp there is no need to use |
| 425 | // HandlePhysRegDef(). |
| 426 | for (unsigned Reg = 1, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) { |
| 427 | // Skip dead regs. |
| 428 | if (!PhysRegDef[Reg] && !PhysRegUse[Reg]) |
| 429 | continue; |
| 430 | // Skip mask-preserved regs. |
Evan Cheng | 7423db2 | 2012-01-21 03:31:03 +0000 | [diff] [blame] | 431 | if (!MO.clobbersPhysReg(Reg)) |
Jakob Stoklund Olesen | 8c47ad8 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 432 | continue; |
| 433 | // Kill the largest clobbered super-register. |
| 434 | // This avoids needless implicit operands. |
| 435 | unsigned Super = Reg; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 436 | for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) |
Jakob Stoklund Olesen | 8c47ad8 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 437 | if ((PhysRegDef[*SR] || PhysRegUse[*SR]) && MO.clobbersPhysReg(*SR)) |
| 438 | Super = *SR; |
| 439 | HandlePhysRegKill(Super, 0); |
| 440 | } |
| 441 | } |
| 442 | |
Evan Cheng | 296925d | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 443 | void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI, |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 444 | SmallVectorImpl<unsigned> &Defs) { |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 445 | // What parts of the register are previously defined? |
Owen Anderson | bffdf66 | 2008-06-27 07:05:59 +0000 | [diff] [blame] | 446 | SmallSet<unsigned, 32> Live; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 447 | if (PhysRegDef[Reg] || PhysRegUse[Reg]) { |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 448 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 449 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 450 | Live.insert(*SubRegs); |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 451 | } else { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 452 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 453 | unsigned SubReg = *SubRegs; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 454 | // If a register isn't itself defined, but all parts that make up of it |
| 455 | // are defined, then consider it also defined. |
| 456 | // e.g. |
| 457 | // AL = |
| 458 | // AH = |
| 459 | // = AX |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 460 | if (Live.count(SubReg)) |
| 461 | continue; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 462 | if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) { |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 463 | for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); |
| 464 | SS.isValid(); ++SS) |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 465 | Live.insert(*SS); |
| 466 | } |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 467 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 468 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 469 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 470 | // Start from the largest piece, find the last time any part of the register |
| 471 | // is referenced. |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 472 | HandlePhysRegKill(Reg, MI); |
| 473 | // Only some of the sub-registers are used. |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 474 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 475 | unsigned SubReg = *SubRegs; |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 476 | if (!Live.count(SubReg)) |
| 477 | // Skip if this sub-register isn't defined. |
| 478 | continue; |
| 479 | HandlePhysRegKill(SubReg, MI); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 482 | if (MI) |
| 483 | Defs.push_back(Reg); // Remember this def. |
Evan Cheng | 296925d | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void LiveVariables::UpdatePhysRegDefs(MachineInstr *MI, |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 487 | SmallVectorImpl<unsigned> &Defs) { |
Evan Cheng | 296925d | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 488 | while (!Defs.empty()) { |
| 489 | unsigned Reg = Defs.back(); |
| 490 | Defs.pop_back(); |
Chad Rosier | 5cc3c98 | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 491 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 492 | SubRegs.isValid(); ++SubRegs) { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 493 | unsigned SubReg = *SubRegs; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 494 | PhysRegDef[SubReg] = MI; |
| 495 | PhysRegUse[SubReg] = NULL; |
Evan Cheng | 4efe741 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 496 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 500 | bool LiveVariables::runOnMachineFunction(MachineFunction &mf) { |
| 501 | MF = &mf; |
Evan Cheng | ea1d9cd | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 502 | MRI = &mf.getRegInfo(); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 503 | TRI = MF->getTarget().getRegisterInfo(); |
Chris Lattner | 96aef89 | 2004-02-09 01:35:21 +0000 | [diff] [blame] | 504 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 505 | unsigned NumRegs = TRI->getNumRegs(); |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 506 | PhysRegDef = new MachineInstr*[NumRegs]; |
| 507 | PhysRegUse = new MachineInstr*[NumRegs]; |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 508 | PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()]; |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 509 | std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0); |
| 510 | std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0); |
Jakob Stoklund Olesen | dcfe5f3 | 2010-02-23 22:43:58 +0000 | [diff] [blame] | 511 | PHIJoins.clear(); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 512 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 513 | // FIXME: LiveIntervals will be updated to remove its dependence on |
| 514 | // LiveVariables to improve compilation time and eliminate bizarre pass |
| 515 | // dependencies. Until then, we can't change much in -O0. |
| 516 | if (!MRI->isSSA()) |
| 517 | report_fatal_error("regalloc=... not currently supported with -O0"); |
| 518 | |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 519 | analyzePHINodes(mf); |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 520 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 521 | // Calculate live variable information in depth first order on the CFG of the |
| 522 | // function. This guarantees that we will see the definition of a virtual |
| 523 | // register before its uses due to dominance properties of SSA (except for PHI |
| 524 | // nodes, which are treated as a special case). |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 525 | MachineBasicBlock *Entry = MF->begin(); |
Evan Cheng | 0410407 | 2007-06-27 05:23:00 +0000 | [diff] [blame] | 526 | SmallPtrSet<MachineBasicBlock*,16> Visited; |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 527 | |
Evan Cheng | 0410407 | 2007-06-27 05:23:00 +0000 | [diff] [blame] | 528 | for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> > |
| 529 | DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); |
| 530 | DFI != E; ++DFI) { |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 531 | MachineBasicBlock *MBB = *DFI; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 532 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 533 | // Mark live-in registers as live-in. |
Evan Cheng | 296925d | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 534 | SmallVector<unsigned, 4> Defs; |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 535 | for (MachineBasicBlock::livein_iterator II = MBB->livein_begin(), |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 536 | EE = MBB->livein_end(); II != EE; ++II) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 537 | assert(TargetRegisterInfo::isPhysicalRegister(*II) && |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 538 | "Cannot have a live-in virtual register!"); |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 539 | HandlePhysRegDef(*II, 0, Defs); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 542 | // Loop over all of the instructions, processing them. |
Evan Cheng | ea1d9cd | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 543 | DistanceMap.clear(); |
| 544 | unsigned Dist = 0; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 545 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 546 | I != E; ++I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 547 | MachineInstr *MI = I; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 548 | if (MI->isDebugValue()) |
Dale Johannesen | d94998f | 2010-02-09 02:01:46 +0000 | [diff] [blame] | 549 | continue; |
Evan Cheng | ea1d9cd | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 550 | DistanceMap.insert(std::make_pair(MI, Dist++)); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 551 | |
| 552 | // Process all of the operands of the instruction... |
| 553 | unsigned NumOperandsToProcess = MI->getNumOperands(); |
| 554 | |
| 555 | // Unless it is a PHI node. In this case, ONLY process the DEF, not any |
| 556 | // of the uses. They will be handled in other basic blocks. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 557 | if (MI->isPHI()) |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 558 | NumOperandsToProcess = 1; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 559 | |
Evan Cheng | d05e805 | 2010-03-26 02:12:24 +0000 | [diff] [blame] | 560 | // Clear kill and dead markers. LV will recompute them. |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 561 | SmallVector<unsigned, 4> UseRegs; |
| 562 | SmallVector<unsigned, 4> DefRegs; |
Jakob Stoklund Olesen | 8c47ad8 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 563 | SmallVector<unsigned, 1> RegMasks; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 564 | for (unsigned i = 0; i != NumOperandsToProcess; ++i) { |
Evan Cheng | d05e805 | 2010-03-26 02:12:24 +0000 | [diff] [blame] | 565 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | 8c47ad8 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 566 | if (MO.isRegMask()) { |
| 567 | RegMasks.push_back(i); |
| 568 | continue; |
| 569 | } |
Evan Cheng | a894ae1 | 2009-01-20 21:25:12 +0000 | [diff] [blame] | 570 | if (!MO.isReg() || MO.getReg() == 0) |
| 571 | continue; |
| 572 | unsigned MOReg = MO.getReg(); |
Evan Cheng | d05e805 | 2010-03-26 02:12:24 +0000 | [diff] [blame] | 573 | if (MO.isUse()) { |
| 574 | MO.setIsKill(false); |
Jakob Stoklund Olesen | 7806c07 | 2012-06-23 02:23:00 +0000 | [diff] [blame] | 575 | if (MO.readsReg()) |
| 576 | UseRegs.push_back(MOReg); |
Evan Cheng | d05e805 | 2010-03-26 02:12:24 +0000 | [diff] [blame] | 577 | } else /*MO.isDef()*/ { |
| 578 | MO.setIsDead(false); |
Evan Cheng | a894ae1 | 2009-01-20 21:25:12 +0000 | [diff] [blame] | 579 | DefRegs.push_back(MOReg); |
Evan Cheng | d05e805 | 2010-03-26 02:12:24 +0000 | [diff] [blame] | 580 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 583 | // Process all uses. |
| 584 | for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) { |
| 585 | unsigned MOReg = UseRegs[i]; |
| 586 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) |
| 587 | HandleVirtRegUse(MOReg, MBB, MI); |
Jakob Stoklund Olesen | fb9ebbf | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 588 | else if (!MRI->isReserved(MOReg)) |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 589 | HandlePhysRegUse(MOReg, MI); |
| 590 | } |
| 591 | |
Jakob Stoklund Olesen | 8c47ad8 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 592 | // Process all masked registers. (Call clobbers). |
| 593 | for (unsigned i = 0, e = RegMasks.size(); i != e; ++i) |
| 594 | HandleRegMask(MI->getOperand(RegMasks[i])); |
| 595 | |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 596 | // Process all defs. |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 597 | for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) { |
| 598 | unsigned MOReg = DefRegs[i]; |
Dan Gohman | 3bdf5fe | 2008-09-21 21:11:41 +0000 | [diff] [blame] | 599 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) |
| 600 | HandleVirtRegDef(MOReg, MI); |
Jakob Stoklund Olesen | fb9ebbf | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 601 | else if (!MRI->isReserved(MOReg)) |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 602 | HandlePhysRegDef(MOReg, MI, Defs); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 603 | } |
Evan Cheng | 296925d | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 604 | UpdatePhysRegDefs(MI, Defs); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // Handle any virtual assignments from PHI nodes which might be at the |
| 608 | // bottom of this basic block. We check all of our successor blocks to see |
| 609 | // if they have PHI nodes, and if so, we simulate an assignment at the end |
| 610 | // of the current block. |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 611 | if (!PHIVarInfo[MBB->getNumber()].empty()) { |
Craig Topper | f22fd3f | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 612 | SmallVectorImpl<unsigned> &VarInfoVec = PHIVarInfo[MBB->getNumber()]; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 613 | |
Craig Topper | f22fd3f | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 614 | for (SmallVectorImpl<unsigned>::iterator I = VarInfoVec.begin(), |
Bill Wendling | 420cdeb | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 615 | E = VarInfoVec.end(); I != E; ++I) |
| 616 | // Mark it alive only in the block we are representing. |
Evan Cheng | ea1d9cd | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 617 | MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(), |
Owen Anderson | 40a627d | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 618 | MBB); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 619 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 620 | |
Evan Cheng | bfe8afa | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 621 | // MachineCSE may CSE instructions which write to non-allocatable physical |
| 622 | // registers across MBBs. Remember if any reserved register is liveout. |
| 623 | SmallSet<unsigned, 4> LiveOuts; |
| 624 | for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(), |
| 625 | SE = MBB->succ_end(); SI != SE; ++SI) { |
| 626 | MachineBasicBlock *SuccMBB = *SI; |
| 627 | if (SuccMBB->isLandingPad()) |
| 628 | continue; |
| 629 | for (MachineBasicBlock::livein_iterator LI = SuccMBB->livein_begin(), |
| 630 | LE = SuccMBB->livein_end(); LI != LE; ++LI) { |
| 631 | unsigned LReg = *LI; |
| 632 | if (!TRI->isInAllocatableClass(LReg)) |
| 633 | // Ignore other live-ins, e.g. those that are live into landing pads. |
| 634 | LiveOuts.insert(LReg); |
| 635 | } |
| 636 | } |
| 637 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 638 | // Loop over PhysRegDef / PhysRegUse, killing any registers that are |
| 639 | // available at the end of the basic block. |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 640 | for (unsigned i = 0; i != NumRegs; ++i) |
Evan Cheng | bfe8afa | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 641 | if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i)) |
Evan Cheng | ad934b8 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 642 | HandlePhysRegDef(i, 0, Defs); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 643 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 644 | std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0); |
| 645 | std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 648 | // Convert and transfer the dead / killed information we have gathered into |
| 649 | // VirtRegInfo onto MI's. |
Jakob Stoklund Olesen | b421c56 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 650 | for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) { |
| 651 | const unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 652 | for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j) |
| 653 | if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg)) |
| 654 | VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 655 | else |
Jakob Stoklund Olesen | b421c56 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 656 | VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI); |
| 657 | } |
Chris Lattner | a5287a6 | 2004-07-01 04:24:29 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 9fb6cf1 | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 659 | // Check to make sure there are no unreachable blocks in the MC CFG for the |
| 660 | // function. If so, it is due to a bug in the instruction selector or some |
| 661 | // other part of the code generator if this happens. |
| 662 | #ifndef NDEBUG |
Evan Cheng | c6a2410 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 663 | for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i) |
Chris Lattner | 9fb6cf1 | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 664 | assert(Visited.count(&*i) != 0 && "unreachable basic block found"); |
| 665 | #endif |
| 666 | |
Evan Cheng | 0d4bdde | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 667 | delete[] PhysRegDef; |
| 668 | delete[] PhysRegUse; |
Evan Cheng | e96f501 | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 669 | delete[] PHIVarInfo; |
| 670 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 673 | |
Evan Cheng | be04dc1 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 674 | /// replaceKillInstruction - Update register kill info by replacing a kill |
| 675 | /// instruction with a new one. |
| 676 | void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr *OldMI, |
| 677 | MachineInstr *NewMI) { |
| 678 | VarInfo &VI = getVarInfo(Reg); |
Evan Cheng | 5b9f60b | 2008-07-03 00:28:27 +0000 | [diff] [blame] | 679 | std::replace(VI.Kills.begin(), VI.Kills.end(), OldMI, NewMI); |
Evan Cheng | be04dc1 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 682 | /// removeVirtualRegistersKilled - Remove all killed info for the specified |
| 683 | /// instruction. |
| 684 | void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 685 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 686 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 687 | if (MO.isReg() && MO.isKill()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 688 | MO.setIsKill(false); |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 689 | unsigned Reg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 690 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 691 | bool removed = getVarInfo(Reg).removeKill(MI); |
| 692 | assert(removed && "kill not in register's VarInfo?"); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 693 | (void)removed; |
Evan Cheng | a6c4c1e | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 694 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
Chris Lattner | 7a3abdc | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 699 | /// analyzePHINodes - Gather information about the PHI nodes in here. In |
Bill Wendling | 6d79474 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 700 | /// particular, we want to map the variable information of a virtual register |
| 701 | /// 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] | 702 | /// |
| 703 | void LiveVariables::analyzePHINodes(const MachineFunction& Fn) { |
| 704 | for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end(); |
| 705 | I != E; ++I) |
| 706 | for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 707 | BBI != BBE && BBI->isPHI(); ++BBI) |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 708 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) |
Jakob Stoklund Olesen | 7806c07 | 2012-06-23 02:23:00 +0000 | [diff] [blame] | 709 | if (BBI->getOperand(i).readsReg()) |
| 710 | PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()] |
| 711 | .push_back(BBI->getOperand(i).getReg()); |
Bill Wendling | f7da4e9 | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 712 | } |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 713 | |
Jakob Stoklund Olesen | 323d8c3 | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 714 | bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB, |
| 715 | unsigned Reg, |
| 716 | MachineRegisterInfo &MRI) { |
| 717 | unsigned Num = MBB.getNumber(); |
| 718 | |
| 719 | // Reg is live-through. |
| 720 | if (AliveBlocks.test(Num)) |
| 721 | return true; |
| 722 | |
| 723 | // Registers defined in MBB cannot be live in. |
| 724 | const MachineInstr *Def = MRI.getVRegDef(Reg); |
| 725 | if (Def && Def->getParent() == &MBB) |
| 726 | return false; |
| 727 | |
| 728 | // Reg was not defined in MBB, was it killed here? |
| 729 | return findKill(&MBB); |
| 730 | } |
| 731 | |
Jakob Stoklund Olesen | 8f72235 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 732 | bool LiveVariables::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB) { |
| 733 | LiveVariables::VarInfo &VI = getVarInfo(Reg); |
| 734 | |
| 735 | // Loop over all of the successors of the basic block, checking to see if |
| 736 | // the value is either live in the block, or if it is killed in the block. |
Benjamin Kramer | f337fb2 | 2011-03-08 17:28:36 +0000 | [diff] [blame] | 737 | SmallVector<MachineBasicBlock*, 8> OpSuccBlocks; |
Jakob Stoklund Olesen | 8f72235 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 738 | for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), |
| 739 | E = MBB.succ_end(); SI != E; ++SI) { |
| 740 | MachineBasicBlock *SuccMBB = *SI; |
| 741 | |
| 742 | // Is it alive in this successor? |
| 743 | unsigned SuccIdx = SuccMBB->getNumber(); |
| 744 | if (VI.AliveBlocks.test(SuccIdx)) |
| 745 | return true; |
| 746 | OpSuccBlocks.push_back(SuccMBB); |
| 747 | } |
| 748 | |
| 749 | // Check to see if this value is live because there is a use in a successor |
| 750 | // that kills it. |
| 751 | switch (OpSuccBlocks.size()) { |
| 752 | case 1: { |
| 753 | MachineBasicBlock *SuccMBB = OpSuccBlocks[0]; |
| 754 | for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i) |
| 755 | if (VI.Kills[i]->getParent() == SuccMBB) |
| 756 | return true; |
| 757 | break; |
| 758 | } |
| 759 | case 2: { |
| 760 | MachineBasicBlock *SuccMBB1 = OpSuccBlocks[0], *SuccMBB2 = OpSuccBlocks[1]; |
| 761 | for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i) |
| 762 | if (VI.Kills[i]->getParent() == SuccMBB1 || |
| 763 | VI.Kills[i]->getParent() == SuccMBB2) |
| 764 | return true; |
| 765 | break; |
| 766 | } |
| 767 | default: |
| 768 | std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end()); |
| 769 | for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i) |
| 770 | if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(), |
| 771 | VI.Kills[i]->getParent())) |
| 772 | return true; |
| 773 | } |
| 774 | return false; |
| 775 | } |
| 776 | |
Jakob Stoklund Olesen | 3e20475 | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 777 | /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All |
| 778 | /// variables that are live out of DomBB will be marked as passing live through |
| 779 | /// BB. |
| 780 | void LiveVariables::addNewBlock(MachineBasicBlock *BB, |
Jakob Stoklund Olesen | 323d8c3 | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 781 | MachineBasicBlock *DomBB, |
| 782 | MachineBasicBlock *SuccBB) { |
Jakob Stoklund Olesen | 3e20475 | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 783 | const unsigned NumNew = BB->getNumber(); |
Jakob Stoklund Olesen | 323d8c3 | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 784 | |
Benjamin Kramer | 11b4505 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 785 | SmallSet<unsigned, 16> Defs, Kills; |
| 786 | |
| 787 | MachineBasicBlock::iterator BBI = SuccBB->begin(), BBE = SuccBB->end(); |
| 788 | for (; BBI != BBE && BBI->isPHI(); ++BBI) { |
| 789 | // Record the def of the PHI node. |
| 790 | Defs.insert(BBI->getOperand(0).getReg()); |
| 791 | |
| 792 | // All registers used by PHI nodes in SuccBB must be live through BB. |
Jakob Stoklund Olesen | 323d8c3 | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 793 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) |
| 794 | if (BBI->getOperand(i+1).getMBB() == BB) |
| 795 | getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew); |
Benjamin Kramer | 11b4505 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | // Record all vreg defs and kills of all instructions in SuccBB. |
| 799 | for (; BBI != BBE; ++BBI) { |
| 800 | for (MachineInstr::mop_iterator I = BBI->operands_begin(), |
| 801 | E = BBI->operands_end(); I != E; ++I) { |
| 802 | if (I->isReg() && TargetRegisterInfo::isVirtualRegister(I->getReg())) { |
| 803 | if (I->isDef()) |
| 804 | Defs.insert(I->getReg()); |
| 805 | else if (I->isKill()) |
| 806 | Kills.insert(I->getReg()); |
| 807 | } |
| 808 | } |
| 809 | } |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 810 | |
| 811 | // Update info for all live variables |
Jakob Stoklund Olesen | b421c56 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 812 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 813 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Benjamin Kramer | 11b4505 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 814 | |
| 815 | // If the Defs is defined in the successor it can't be live in BB. |
| 816 | if (Defs.count(Reg)) |
| 817 | continue; |
| 818 | |
| 819 | // If the register is either killed in or live through SuccBB it's also live |
| 820 | // through BB. |
Jakob Stoklund Olesen | 3e20475 | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 821 | VarInfo &VI = getVarInfo(Reg); |
Benjamin Kramer | 11b4505 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 822 | if (Kills.count(Reg) || VI.AliveBlocks.test(SuccBB->getNumber())) |
Jakob Stoklund Olesen | 3e20475 | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 823 | VI.AliveBlocks.set(NumNew); |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 824 | } |
| 825 | } |