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