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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha 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 | 61b08f1 | 2004-02-10 21:18:55 +0000 | [diff] [blame] | 31 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DepthFirstIterator.h" |
| 35 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 6fcd8d8 | 2004-10-25 18:44:14 +0000 | [diff] [blame] | 36 | #include "llvm/Config/alloca.h" |
Chris Lattner | 49a5aaa | 2004-01-30 22:08:53 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 39 | static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis"); |
| 40 | |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 41 | LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) { |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 42 | assert(MRegisterInfo::isVirtualRegister(RegIdx) && |
Chris Lattner | fb2cb69 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 43 | "getVarInfo: not a virtual register!"); |
| 44 | RegIdx -= MRegisterInfo::FirstVirtualRegister; |
| 45 | if (RegIdx >= VirtRegInfo.size()) { |
| 46 | if (RegIdx >= 2*VirtRegInfo.size()) |
| 47 | VirtRegInfo.resize(RegIdx*2); |
| 48 | else |
| 49 | VirtRegInfo.resize(2*VirtRegInfo.size()); |
| 50 | } |
| 51 | return VirtRegInfo[RegIdx]; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 56 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo, |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 57 | MachineBasicBlock *MBB) { |
Chris Lattner | 8ba9771 | 2004-07-01 04:29:47 +0000 | [diff] [blame] | 58 | unsigned BBNum = MBB->getNumber(); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 59 | |
| 60 | // Check to see if this basic block is one of the killing blocks. If so, |
| 61 | // remove it... |
| 62 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 63 | if (VRInfo.Kills[i]->getParent() == MBB) { |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 64 | VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry |
| 65 | break; |
| 66 | } |
| 67 | |
Chris Lattner | 73d4adf | 2004-07-19 06:26:50 +0000 | [diff] [blame] | 68 | if (MBB == VRInfo.DefInst->getParent()) return; // Terminate recursion |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 69 | |
| 70 | if (VRInfo.AliveBlocks.size() <= BBNum) |
| 71 | VRInfo.AliveBlocks.resize(BBNum+1); // Make space... |
| 72 | |
| 73 | if (VRInfo.AliveBlocks[BBNum]) |
| 74 | return; // We already know the block is live |
| 75 | |
| 76 | // Mark the variable known alive in this bb |
| 77 | VRInfo.AliveBlocks[BBNum] = true; |
| 78 | |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 79 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 80 | E = MBB->pred_end(); PI != E; ++PI) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 81 | MarkVirtRegAliveInBlock(VRInfo, *PI); |
| 82 | } |
| 83 | |
| 84 | void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB, |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 85 | MachineInstr *MI) { |
Alkis Evlogimenos | 2e58a41 | 2004-09-01 22:34:52 +0000 | [diff] [blame] | 86 | assert(VRInfo.DefInst && "Register use before def!"); |
| 87 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 88 | // Check to see if this basic block is already a kill block... |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 89 | if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) { |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 90 | // Yes, this register is killed in this basic block already. Increase the |
| 91 | // live range by updating the kill instruction. |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 92 | VRInfo.Kills.back() = MI; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | #ifndef NDEBUG |
| 97 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 98 | assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 101 | assert(MBB != VRInfo.DefInst->getParent() && |
Chris Lattner | 73d4adf | 2004-07-19 06:26:50 +0000 | [diff] [blame] | 102 | "Should have kill for defblock!"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 103 | |
| 104 | // Add a new kill entry for this basic block. |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 105 | VRInfo.Kills.push_back(MI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 106 | |
| 107 | // Update all dominating blocks to mark them known live. |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 108 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 109 | E = MBB->pred_end(); PI != E; ++PI) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 110 | MarkVirtRegAliveInBlock(VRInfo, *PI); |
| 111 | } |
| 112 | |
| 113 | void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) { |
Alkis Evlogimenos | c55640f | 2004-01-13 21:16:25 +0000 | [diff] [blame] | 114 | PhysRegInfo[Reg] = MI; |
| 115 | PhysRegUsed[Reg] = true; |
Chris Lattner | 6d3848d | 2004-05-10 05:12:43 +0000 | [diff] [blame] | 116 | |
| 117 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 118 | unsigned Alias = *AliasSet; ++AliasSet) { |
| 119 | PhysRegInfo[Alias] = MI; |
| 120 | PhysRegUsed[Alias] = true; |
| 121 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) { |
| 125 | // Does this kill a previous version of this register? |
| 126 | if (MachineInstr *LastUse = PhysRegInfo[Reg]) { |
| 127 | if (PhysRegUsed[Reg]) |
| 128 | RegistersKilled.insert(std::make_pair(LastUse, Reg)); |
| 129 | else |
| 130 | RegistersDead.insert(std::make_pair(LastUse, Reg)); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 131 | } |
| 132 | PhysRegInfo[Reg] = MI; |
| 133 | PhysRegUsed[Reg] = false; |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 134 | |
| 135 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
Chris Lattner | 6d3848d | 2004-05-10 05:12:43 +0000 | [diff] [blame] | 136 | unsigned Alias = *AliasSet; ++AliasSet) { |
Chris Lattner | 4994877 | 2004-02-09 01:43:23 +0000 | [diff] [blame] | 137 | if (MachineInstr *LastUse = PhysRegInfo[Alias]) { |
| 138 | if (PhysRegUsed[Alias]) |
Chris Lattner | 6d3848d | 2004-05-10 05:12:43 +0000 | [diff] [blame] | 139 | RegistersKilled.insert(std::make_pair(LastUse, Alias)); |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 140 | else |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 141 | RegistersDead.insert(std::make_pair(LastUse, Alias)); |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 4994877 | 2004-02-09 01:43:23 +0000 | [diff] [blame] | 143 | PhysRegInfo[Alias] = MI; |
| 144 | PhysRegUsed[Alias] = false; |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool LiveVariables::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 9bcdcd1 | 2004-06-02 05:57:12 +0000 | [diff] [blame] | 149 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
Chris Lattner | 96aef89 | 2004-02-09 01:35:21 +0000 | [diff] [blame] | 150 | RegInfo = MF.getTarget().getRegisterInfo(); |
| 151 | assert(RegInfo && "Target doesn't have register information?"); |
| 152 | |
Alkis Evlogimenos | 22a2f6d | 2004-08-26 22:23:32 +0000 | [diff] [blame] | 153 | AllocatablePhysicalRegisters = RegInfo->getAllocatableSet(MF); |
Chris Lattner | 5cdfbad | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 154 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 155 | // PhysRegInfo - Keep track of which instruction was the last use of a |
| 156 | // physical register. This is a purely local property, because all physical |
| 157 | // register references as presumed dead across basic blocks. |
| 158 | // |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 159 | PhysRegInfo = (MachineInstr**)alloca(sizeof(MachineInstr*) * |
Chris Lattner | 6fcd8d8 | 2004-10-25 18:44:14 +0000 | [diff] [blame] | 160 | RegInfo->getNumRegs()); |
| 161 | PhysRegUsed = (bool*)alloca(sizeof(bool)*RegInfo->getNumRegs()); |
| 162 | std::fill(PhysRegInfo, PhysRegInfo+RegInfo->getNumRegs(), (MachineInstr*)0); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 163 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 164 | /// Get some space for a respectable number of registers... |
| 165 | VirtRegInfo.resize(64); |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 166 | |
| 167 | // Mark live-in registers as live-in. |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame^] | 168 | for (MachineFunction::livein_iterator I = MF.livein_begin(), |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 169 | E = MF.livein_end(); I != E; ++I) { |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame^] | 170 | assert(MRegisterInfo::isPhysicalRegister(I->first) && |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 171 | "Cannot have a live-in virtual register!"); |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame^] | 172 | HandlePhysRegDef(I->first, 0); |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 173 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 174 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 175 | // Calculate live variable information in depth first order on the CFG of the |
| 176 | // function. This guarantees that we will see the definition of a virtual |
| 177 | // register before its uses due to dominance properties of SSA (except for PHI |
| 178 | // nodes, which are treated as a special case). |
| 179 | // |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 180 | MachineBasicBlock *Entry = MF.begin(); |
Chris Lattner | a5287a6 | 2004-07-01 04:24:29 +0000 | [diff] [blame] | 181 | std::set<MachineBasicBlock*> Visited; |
| 182 | for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited), |
| 183 | E = df_ext_end(Entry, Visited); DFI != E; ++DFI) { |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 184 | MachineBasicBlock *MBB = *DFI; |
Chris Lattner | 8ba9771 | 2004-07-01 04:29:47 +0000 | [diff] [blame] | 185 | unsigned BBNum = MBB->getNumber(); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 186 | |
| 187 | // Loop over all of the instructions, processing them. |
| 188 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 189 | I != E; ++I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 190 | MachineInstr *MI = I; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 191 | const TargetInstrDescriptor &MID = TII.get(MI->getOpcode()); |
| 192 | |
| 193 | // Process all of the operands of the instruction... |
| 194 | unsigned NumOperandsToProcess = MI->getNumOperands(); |
| 195 | |
| 196 | // Unless it is a PHI node. In this case, ONLY process the DEF, not any |
| 197 | // of the uses. They will be handled in other basic blocks. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 198 | if (MI->getOpcode() == TargetInstrInfo::PHI) |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 199 | NumOperandsToProcess = 1; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 200 | |
| 201 | // Loop over implicit uses, using them. |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 202 | for (const unsigned *ImplicitUses = MID.ImplicitUses; |
| 203 | *ImplicitUses; ++ImplicitUses) |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 204 | HandlePhysRegUse(*ImplicitUses, MI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 205 | |
| 206 | // Process all explicit uses... |
| 207 | for (unsigned i = 0; i != NumOperandsToProcess; ++i) { |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 208 | MachineOperand &MO = MI->getOperand(i); |
| 209 | if (MO.isUse() && MO.isRegister() && MO.getReg()) { |
| 210 | if (MRegisterInfo::isVirtualRegister(MO.getReg())){ |
| 211 | HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI); |
| 212 | } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) && |
Chris Lattner | 5cdfbad | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 213 | AllocatablePhysicalRegisters[MO.getReg()]) { |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 214 | HandlePhysRegUse(MO.getReg(), MI); |
| 215 | } |
| 216 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Loop over implicit defs, defining them. |
Alkis Evlogimenos | efe995a | 2003-12-13 01:20:58 +0000 | [diff] [blame] | 220 | for (const unsigned *ImplicitDefs = MID.ImplicitDefs; |
| 221 | *ImplicitDefs; ++ImplicitDefs) |
| 222 | HandlePhysRegDef(*ImplicitDefs, MI); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 223 | |
| 224 | // Process all explicit defs... |
| 225 | for (unsigned i = 0; i != NumOperandsToProcess; ++i) { |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 226 | MachineOperand &MO = MI->getOperand(i); |
| 227 | if (MO.isDef() && MO.isRegister() && MO.getReg()) { |
| 228 | if (MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 229 | VarInfo &VRInfo = getVarInfo(MO.getReg()); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 73d4adf | 2004-07-19 06:26:50 +0000 | [diff] [blame] | 231 | assert(VRInfo.DefInst == 0 && "Variable multiply defined!"); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 232 | VRInfo.DefInst = MI; |
Chris Lattner | 472405e | 2004-07-19 06:55:21 +0000 | [diff] [blame] | 233 | // Defaults to dead |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 234 | VRInfo.Kills.push_back(MI); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 235 | } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) && |
Chris Lattner | 5cdfbad | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 236 | AllocatablePhysicalRegisters[MO.getReg()]) { |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 237 | HandlePhysRegDef(MO.getReg(), MI); |
| 238 | } |
| 239 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // Handle any virtual assignments from PHI nodes which might be at the |
| 244 | // bottom of this basic block. We check all of our successor blocks to see |
| 245 | // if they have PHI nodes, and if so, we simulate an assignment at the end |
| 246 | // of the current block. |
Chris Lattner | f25fb4b | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 247 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 248 | E = MBB->succ_end(); SI != E; ++SI) { |
| 249 | MachineBasicBlock *Succ = *SI; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 250 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 251 | // PHI nodes are guaranteed to be at the top of the block... |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 252 | for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end(); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 253 | MI != ME && MI->getOpcode() == TargetInstrInfo::PHI; ++MI) { |
| 254 | for (unsigned i = 1; ; i += 2) { |
Chris Lattner | 92bc3bc | 2004-02-29 22:01:51 +0000 | [diff] [blame] | 255 | assert(MI->getNumOperands() > i+1 && |
| 256 | "Didn't find an entry for our predecessor??"); |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 257 | if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) { |
| 258 | MachineOperand &MO = MI->getOperand(i); |
| 259 | if (!MO.getVRegValueOrNull()) { |
| 260 | VarInfo &VRInfo = getVarInfo(MO.getReg()); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 261 | |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 262 | // Only mark it alive only in the block we are representing... |
| 263 | MarkVirtRegAliveInBlock(VRInfo, MBB); |
| 264 | break; // Found the PHI entry for this block... |
| 265 | } |
| 266 | } |
Chris Lattner | 92bc3bc | 2004-02-29 22:01:51 +0000 | [diff] [blame] | 267 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 270 | |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 271 | // Finally, if the last block in the function is a return, make sure to mark |
| 272 | // it as using all of the live-out values in the function. |
| 273 | if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) { |
| 274 | MachineInstr *Ret = &MBB->back(); |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame^] | 275 | for (MachineFunction::liveout_iterator I = MF.liveout_begin(), |
Chris Lattner | d493b34 | 2005-04-09 15:23:25 +0000 | [diff] [blame] | 276 | E = MF.liveout_end(); I != E; ++I) { |
| 277 | assert(MRegisterInfo::isPhysicalRegister(*I) && |
| 278 | "Cannot have a live-in virtual register!"); |
| 279 | HandlePhysRegUse(*I, Ret); |
| 280 | } |
| 281 | } |
| 282 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 283 | // Loop over PhysRegInfo, killing any registers that are available at the |
| 284 | // end of the basic block. This also resets the PhysRegInfo map. |
Chris Lattner | 96aef89 | 2004-02-09 01:35:21 +0000 | [diff] [blame] | 285 | for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 286 | if (PhysRegInfo[i]) |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 287 | HandlePhysRegDef(i, 0); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 290 | // Convert the information we have gathered into VirtRegInfo and transform it |
| 291 | // into a form usable by RegistersKilled. |
| 292 | // |
| 293 | for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i) |
| 294 | for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) { |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 295 | if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst) |
| 296 | RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j], |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 297 | i + MRegisterInfo::FirstVirtualRegister)); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 298 | |
| 299 | else |
Chris Lattner | 74de8b1 | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 300 | RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j], |
Misha Brukman | 09ba906 | 2004-06-24 21:31:16 +0000 | [diff] [blame] | 301 | i + MRegisterInfo::FirstVirtualRegister)); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 302 | } |
Chris Lattner | a5287a6 | 2004-07-01 04:24:29 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 9fb6cf1 | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 304 | // Check to make sure there are no unreachable blocks in the MC CFG for the |
| 305 | // function. If so, it is due to a bug in the instruction selector or some |
| 306 | // other part of the code generator if this happens. |
| 307 | #ifndef NDEBUG |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 308 | for(MachineFunction::iterator i = MF.begin(), e = MF.end(); i != e; ++i) |
Chris Lattner | 9fb6cf1 | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 309 | assert(Visited.count(&*i) != 0 && "unreachable basic block found"); |
| 310 | #endif |
| 311 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 312 | return false; |
| 313 | } |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 314 | |
| 315 | /// instructionChanged - When the address of an instruction changes, this |
| 316 | /// method should be called so that live variables can update its internal |
| 317 | /// data structures. This removes the records for OldMI, transfering them to |
| 318 | /// the records for NewMI. |
| 319 | void LiveVariables::instructionChanged(MachineInstr *OldMI, |
| 320 | MachineInstr *NewMI) { |
| 321 | // If the instruction defines any virtual registers, update the VarInfo for |
| 322 | // the instruction. |
Alkis Evlogimenos | a8db01a | 2004-03-30 22:44:39 +0000 | [diff] [blame] | 323 | for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) { |
| 324 | MachineOperand &MO = OldMI->getOperand(i); |
Chris Lattner | d45be36 | 2005-01-19 17:09:15 +0000 | [diff] [blame] | 325 | if (MO.isRegister() && MO.getReg() && |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 326 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 327 | unsigned Reg = MO.getReg(); |
| 328 | VarInfo &VI = getVarInfo(Reg); |
Chris Lattner | d45be36 | 2005-01-19 17:09:15 +0000 | [diff] [blame] | 329 | if (MO.isDef()) { |
| 330 | // Update the defining instruction. |
| 331 | if (VI.DefInst == OldMI) |
| 332 | VI.DefInst = NewMI; |
Chris Lattner | 2a6e163 | 2005-01-19 17:11:51 +0000 | [diff] [blame] | 333 | } |
| 334 | if (MO.isUse()) { |
Chris Lattner | d45be36 | 2005-01-19 17:09:15 +0000 | [diff] [blame] | 335 | // If this is a kill of the value, update the VI kills list. |
| 336 | if (VI.removeKill(OldMI)) |
| 337 | VI.Kills.push_back(NewMI); // Yes, there was a kill of it |
| 338 | } |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | // Move the killed information over... |
| 343 | killed_iterator I, E; |
| 344 | tie(I, E) = killed_range(OldMI); |
Chris Lattner | a96478d | 2004-02-19 18:32:29 +0000 | [diff] [blame] | 345 | std::vector<unsigned> Regs; |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 346 | for (killed_iterator A = I; A != E; ++A) |
Chris Lattner | a96478d | 2004-02-19 18:32:29 +0000 | [diff] [blame] | 347 | Regs.push_back(A->second); |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 348 | RegistersKilled.erase(I, E); |
| 349 | |
Chris Lattner | a96478d | 2004-02-19 18:32:29 +0000 | [diff] [blame] | 350 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) |
| 351 | RegistersKilled.insert(std::make_pair(NewMI, Regs[i])); |
| 352 | Regs.clear(); |
| 353 | |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 354 | // Move the dead information over... |
| 355 | tie(I, E) = dead_range(OldMI); |
| 356 | for (killed_iterator A = I; A != E; ++A) |
Chris Lattner | a96478d | 2004-02-19 18:32:29 +0000 | [diff] [blame] | 357 | Regs.push_back(A->second); |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 358 | RegistersDead.erase(I, E); |
Chris Lattner | a96478d | 2004-02-19 18:32:29 +0000 | [diff] [blame] | 359 | |
| 360 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) |
| 361 | RegistersDead.insert(std::make_pair(NewMI, Regs[i])); |
Chris Lattner | 5ed001b | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 362 | } |