Chris Lattner | a3b8b5c | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 1 | //===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===// |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 2 | // |
| 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 | // |
| 10 | // This file implements the LiveInterval analysis pass which is used |
| 11 | // by the Linear Scan Register allocator. This pass linearizes the |
| 12 | // basic blocks of the function in DFS order and uses the |
| 13 | // LiveVariables pass to conservatively compute live intervals for |
| 14 | // each virtual and physical register. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define DEBUG_TYPE "liveintervals" |
Chris Lattner | 3c3fe46 | 2005-09-21 04:19:09 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Misha Brukman | 08a6c76 | 2004-09-03 18:25:53 +0000 | [diff] [blame] | 20 | #include "VirtRegMap.h" |
Chris Lattner | 015959e | 2004-05-01 21:24:39 +0000 | [diff] [blame] | 21 | #include "llvm/Value.h" |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveVariables.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/Passes.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
| 28 | #include "llvm/Target/MRegisterInfo.h" |
| 29 | #include "llvm/Target/TargetInstrInfo.h" |
| 30 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 36 | #include <cmath> |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 39 | STATISTIC(numIntervals, "Number of original intervals"); |
| 40 | STATISTIC(numIntervalsAfter, "Number of intervals after coalescing"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 41 | STATISTIC(numFolded , "Number of loads/stores folded into instructions"); |
| 42 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 43 | char LiveIntervals::ID = 0; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 44 | namespace { |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 45 | RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis"); |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 46 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 48 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 49 | AU.addPreserved<LiveVariables>(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 50 | AU.addRequired<LiveVariables>(); |
| 51 | AU.addPreservedID(PHIEliminationID); |
| 52 | AU.addRequiredID(PHIEliminationID); |
| 53 | AU.addRequiredID(TwoAddressInstructionPassID); |
| 54 | AU.addRequired<LoopInfo>(); |
| 55 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 58 | void LiveIntervals::releaseMemory() { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 59 | mi2iMap_.clear(); |
| 60 | i2miMap_.clear(); |
| 61 | r2iMap_.clear(); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 62 | for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i) |
| 63 | delete ClonedMIs[i]; |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 66 | /// runOnMachineFunction - Register allocate the whole function |
| 67 | /// |
| 68 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 69 | mf_ = &fn; |
| 70 | tm_ = &fn.getTarget(); |
| 71 | mri_ = tm_->getRegisterInfo(); |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 72 | tii_ = tm_->getInstrInfo(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 73 | lv_ = &getAnalysis<LiveVariables>(); |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 74 | allocatableRegs_ = mri_->getAllocatableSet(fn); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 76 | // Number MachineInstrs and MachineBasicBlocks. |
| 77 | // Initialize MBB indexes to a sentinal. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 78 | MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U)); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 79 | |
| 80 | unsigned MIIndex = 0; |
| 81 | for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end(); |
| 82 | MBB != E; ++MBB) { |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 83 | unsigned StartIdx = MIIndex; |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 85 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 86 | I != E; ++I) { |
| 87 | bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 88 | assert(inserted && "multiple MachineInstr -> index mappings"); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 89 | i2miMap_.push_back(I); |
| 90 | MIIndex += InstrSlots::NUM; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 91 | } |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 92 | |
| 93 | // Set the MBB2IdxMap entry for this MBB. |
| 94 | MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 95 | } |
Alkis Evlogimenos | d6e40a6 | 2004-01-14 10:44:29 +0000 | [diff] [blame] | 96 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 97 | computeIntervals(); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 98 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 99 | numIntervals += getNumIntervals(); |
| 100 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 101 | DOUT << "********** INTERVALS **********\n"; |
| 102 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 103 | I->second.print(DOUT, mri_); |
| 104 | DOUT << "\n"; |
| 105 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 106 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 107 | numIntervalsAfter += getNumIntervals(); |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 108 | DEBUG(dump()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 109 | return true; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 112 | /// print - Implement the dump method. |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 113 | void LiveIntervals::print(std::ostream &O, const Module* ) const { |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 114 | O << "********** INTERVALS **********\n"; |
Chris Lattner | 8e7a709 | 2005-07-27 23:03:38 +0000 | [diff] [blame] | 115 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 116 | I->second.print(DOUT, mri_); |
| 117 | DOUT << "\n"; |
Chris Lattner | 8e7a709 | 2005-07-27 23:03:38 +0000 | [diff] [blame] | 118 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 119 | |
| 120 | O << "********** MACHINEINSTRS **********\n"; |
| 121 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 122 | mbbi != mbbe; ++mbbi) { |
| 123 | O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n"; |
| 124 | for (MachineBasicBlock::iterator mii = mbbi->begin(), |
| 125 | mie = mbbi->end(); mii != mie; ++mii) { |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 126 | O << getInstructionIndex(mii) << '\t' << *mii; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 131 | // Not called? |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 132 | /// CreateNewLiveInterval - Create a new live interval with the given live |
| 133 | /// ranges. The new live interval will have an infinite spill weight. |
| 134 | LiveInterval& |
| 135 | LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI, |
| 136 | const std::vector<LiveRange> &LRs) { |
| 137 | const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg); |
| 138 | |
| 139 | // Create a new virtual register for the spill interval. |
| 140 | unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC); |
| 141 | |
| 142 | // Replace the old virtual registers in the machine operands with the shiny |
| 143 | // new one. |
| 144 | for (std::vector<LiveRange>::const_iterator |
| 145 | I = LRs.begin(), E = LRs.end(); I != E; ++I) { |
| 146 | unsigned Index = getBaseIndex(I->start); |
| 147 | unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM; |
| 148 | |
| 149 | for (; Index != End; Index += InstrSlots::NUM) { |
| 150 | // Skip deleted instructions |
| 151 | while (Index != End && !getInstructionFromIndex(Index)) |
| 152 | Index += InstrSlots::NUM; |
| 153 | |
| 154 | if (Index == End) break; |
| 155 | |
| 156 | MachineInstr *MI = getInstructionFromIndex(Index); |
| 157 | |
Bill Wendling | beeb77f | 2006-11-16 07:35:18 +0000 | [diff] [blame] | 158 | for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) { |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 159 | MachineOperand &MOp = MI->getOperand(J); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 160 | if (MOp.isRegister() && MOp.getReg() == LI->reg) |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 161 | MOp.setReg(NewVReg); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | LiveInterval &NewLI = getOrCreateInterval(NewVReg); |
| 167 | |
| 168 | // The spill weight is now infinity as it cannot be spilled again |
| 169 | NewLI.weight = float(HUGE_VAL); |
| 170 | |
| 171 | for (std::vector<LiveRange>::const_iterator |
| 172 | I = LRs.begin(), E = LRs.end(); I != E; ++I) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 173 | DOUT << " Adding live range " << *I << " to new interval\n"; |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 174 | NewLI.addRange(*I); |
| 175 | } |
| 176 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 177 | DOUT << "Created new live interval " << NewLI << "\n"; |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 178 | return NewLI; |
| 179 | } |
| 180 | |
Evan Cheng | bf105c8 | 2006-11-03 03:04:46 +0000 | [diff] [blame] | 181 | /// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to |
| 182 | /// two addr elimination. |
| 183 | static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg, |
| 184 | const TargetInstrInfo *TII) { |
| 185 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 186 | MachineOperand &MO1 = MI->getOperand(i); |
| 187 | if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) { |
| 188 | for (unsigned j = i+1; j < e; ++j) { |
| 189 | MachineOperand &MO2 = MI->getOperand(j); |
| 190 | if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg && |
Evan Cheng | 51cdcd1 | 2006-12-07 01:21:59 +0000 | [diff] [blame] | 191 | MI->getInstrDescriptor()-> |
| 192 | getOperandConstraint(j, TOI::TIED_TO) == (int)i) |
Evan Cheng | bf105c8 | 2006-11-03 03:04:46 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 200 | /// isReMaterializable - Returns true if the definition MI of the specified |
| 201 | /// val# of the specified interval is re-materializable. |
| 202 | bool LiveIntervals::isReMaterializable(const LiveInterval &li, unsigned ValNum, |
| 203 | MachineInstr *MI) { |
| 204 | if (tii_->isTriviallyReMaterializable(MI)) |
| 205 | return true; |
| 206 | |
| 207 | int FrameIdx = 0; |
| 208 | if (!tii_->isLoadFromStackSlot(MI, FrameIdx) || |
| 209 | !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx)) |
| 210 | return false; |
| 211 | |
| 212 | // This is a load from fixed stack slot. It can be rematerialized unless it's |
| 213 | // re-defined by a two-address instruction. |
| 214 | for (unsigned i = 0, e = li.getNumValNums(); i != e; ++i) { |
| 215 | if (i == ValNum) |
| 216 | continue; |
| 217 | unsigned DefIdx = li.getDefForValNum(i); |
| 218 | if (DefIdx == ~1U) |
| 219 | continue; // Dead val#. |
| 220 | MachineInstr *DefMI = (DefIdx == ~0u) |
| 221 | ? NULL : getInstructionFromIndex(DefIdx); |
| 222 | if (DefMI && isReDefinedByTwoAddr(DefMI, li.reg, tii_)) |
| 223 | return false; |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm, |
| 229 | unsigned index, unsigned i, |
| 230 | int slot, unsigned reg) { |
| 231 | MachineInstr *fmi = mri_->foldMemoryOperand(MI, i, slot); |
| 232 | if (fmi) { |
| 233 | // Attempt to fold the memory reference into the instruction. If |
| 234 | // we can do this, we don't need to insert spill code. |
| 235 | if (lv_) |
| 236 | lv_->instructionChanged(MI, fmi); |
| 237 | MachineBasicBlock &MBB = *MI->getParent(); |
| 238 | vrm.virtFolded(reg, MI, i, fmi); |
| 239 | mi2iMap_.erase(MI); |
| 240 | i2miMap_[index/InstrSlots::NUM] = fmi; |
| 241 | mi2iMap_[fmi] = index; |
| 242 | MI = MBB.insert(MBB.erase(MI), fmi); |
| 243 | ++numFolded; |
| 244 | return true; |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | std::vector<LiveInterval*> LiveIntervals:: |
| 250 | addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) { |
| 251 | // since this is called after the analysis is done we don't know if |
| 252 | // LiveVariables is available |
| 253 | lv_ = getAnalysisToUpdate<LiveVariables>(); |
| 254 | |
| 255 | std::vector<LiveInterval*> added; |
| 256 | |
| 257 | assert(li.weight != HUGE_VALF && |
| 258 | "attempt to spill already spilled interval!"); |
| 259 | |
| 260 | DOUT << "\t\t\t\tadding intervals for spills for interval: "; |
| 261 | li.print(DOUT, mri_); |
| 262 | DOUT << '\n'; |
| 263 | |
| 264 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg); |
| 265 | |
| 266 | unsigned NumValNums = li.getNumValNums(); |
| 267 | SmallVector<MachineInstr*, 4> ReMatDefs; |
| 268 | ReMatDefs.resize(NumValNums, NULL); |
| 269 | SmallVector<MachineInstr*, 4> ReMatOrigDefs; |
| 270 | ReMatOrigDefs.resize(NumValNums, NULL); |
| 271 | SmallVector<int, 4> ReMatIds; |
| 272 | ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT); |
| 273 | BitVector ReMatDelete(NumValNums); |
| 274 | unsigned slot = VirtRegMap::MAX_STACK_SLOT; |
| 275 | |
| 276 | bool NeedStackSlot = false; |
| 277 | for (unsigned i = 0; i != NumValNums; ++i) { |
| 278 | unsigned DefIdx = li.getDefForValNum(i); |
| 279 | if (DefIdx == ~1U) |
| 280 | continue; // Dead val#. |
| 281 | // Is the def for the val# rematerializable? |
| 282 | MachineInstr *DefMI = (DefIdx == ~0u) |
| 283 | ? NULL : getInstructionFromIndex(DefIdx); |
| 284 | if (DefMI && isReMaterializable(li, i, DefMI)) { |
| 285 | // Remember how to remat the def of this val#. |
| 286 | ReMatOrigDefs[i] = DefMI; |
| 287 | // Original def may be modified so we have to make a copy here. vrm must |
| 288 | // delete these! |
| 289 | ReMatDefs[i] = DefMI = DefMI->clone(); |
| 290 | vrm.setVirtIsReMaterialized(reg, DefMI); |
| 291 | |
| 292 | bool CanDelete = true; |
| 293 | const SmallVector<unsigned, 4> &kills = li.getKillsForValNum(i); |
| 294 | for (unsigned j = 0, ee = kills.size(); j != ee; ++j) { |
| 295 | unsigned KillIdx = kills[j]; |
| 296 | MachineInstr *KillMI = (KillIdx & 1) |
| 297 | ? NULL : getInstructionFromIndex(KillIdx); |
| 298 | // Kill is a phi node, not all of its uses can be rematerialized. |
| 299 | // It must not be deleted. |
| 300 | if (!KillMI) { |
| 301 | CanDelete = false; |
| 302 | // Need a stack slot if there is any live range where uses cannot be |
| 303 | // rematerialized. |
| 304 | NeedStackSlot = true; |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (CanDelete) |
| 310 | ReMatDelete.set(i); |
| 311 | } else { |
| 312 | // Need a stack slot if there is any live range where uses cannot be |
| 313 | // rematerialized. |
| 314 | NeedStackSlot = true; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // One stack slot per live interval. |
| 319 | if (NeedStackSlot) |
| 320 | slot = vrm.assignVirt2StackSlot(reg); |
| 321 | |
| 322 | for (LiveInterval::Ranges::const_iterator |
| 323 | I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) { |
| 324 | MachineInstr *DefMI = ReMatDefs[I->ValId]; |
| 325 | MachineInstr *OrigDefMI = ReMatOrigDefs[I->ValId]; |
| 326 | bool DefIsReMat = DefMI != NULL; |
| 327 | bool CanDelete = ReMatDelete[I->ValId]; |
| 328 | int LdSlot = 0; |
| 329 | bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot); |
| 330 | unsigned index = getBaseIndex(I->start); |
| 331 | unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM; |
| 332 | for (; index != end; index += InstrSlots::NUM) { |
| 333 | // skip deleted instructions |
| 334 | while (index != end && !getInstructionFromIndex(index)) |
| 335 | index += InstrSlots::NUM; |
| 336 | if (index == end) break; |
| 337 | |
| 338 | MachineInstr *MI = getInstructionFromIndex(index); |
| 339 | |
| 340 | RestartInstruction: |
| 341 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 342 | MachineOperand& mop = MI->getOperand(i); |
| 343 | if (mop.isRegister() && mop.getReg() == li.reg) { |
| 344 | if (DefIsReMat) { |
| 345 | // If this is the rematerializable definition MI itself and |
| 346 | // all of its uses are rematerialized, simply delete it. |
| 347 | if (MI == OrigDefMI) { |
| 348 | if (CanDelete) { |
| 349 | RemoveMachineInstrFromMaps(MI); |
| 350 | MI->eraseFromParent(); |
| 351 | break; |
| 352 | } else if (tryFoldMemoryOperand(MI, vrm, index, i, slot, li.reg)) |
| 353 | // Folding the load/store can completely change the instruction |
| 354 | // in unpredictable ways, rescan it from the beginning. |
| 355 | goto RestartInstruction; |
| 356 | } else if (isLoadSS && |
| 357 | tryFoldMemoryOperand(MI, vrm, index, i, LdSlot, li.reg)){ |
| 358 | // FIXME: Other rematerializable loads can be folded as well. |
| 359 | // Folding the load/store can completely change the |
| 360 | // instruction in unpredictable ways, rescan it from |
| 361 | // the beginning. |
| 362 | goto RestartInstruction; |
| 363 | } |
| 364 | } else { |
| 365 | if (tryFoldMemoryOperand(MI, vrm, index, i, slot, li.reg)) |
| 366 | // Folding the load/store can completely change the instruction in |
| 367 | // unpredictable ways, rescan it from the beginning. |
| 368 | goto RestartInstruction; |
| 369 | } |
| 370 | |
| 371 | // Create a new virtual register for the spill interval. |
| 372 | unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc); |
| 373 | |
| 374 | // Scan all of the operands of this instruction rewriting operands |
| 375 | // to use NewVReg instead of li.reg as appropriate. We do this for |
| 376 | // two reasons: |
| 377 | // |
| 378 | // 1. If the instr reads the same spilled vreg multiple times, we |
| 379 | // want to reuse the NewVReg. |
| 380 | // 2. If the instr is a two-addr instruction, we are required to |
| 381 | // keep the src/dst regs pinned. |
| 382 | // |
| 383 | // Keep track of whether we replace a use and/or def so that we can |
| 384 | // create the spill interval with the appropriate range. |
| 385 | mop.setReg(NewVReg); |
| 386 | |
| 387 | bool HasUse = mop.isUse(); |
| 388 | bool HasDef = mop.isDef(); |
| 389 | for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) { |
| 390 | if (MI->getOperand(j).isReg() && |
| 391 | MI->getOperand(j).getReg() == li.reg) { |
| 392 | MI->getOperand(j).setReg(NewVReg); |
| 393 | HasUse |= MI->getOperand(j).isUse(); |
| 394 | HasDef |= MI->getOperand(j).isDef(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | vrm.grow(); |
| 399 | if (DefIsReMat) { |
| 400 | vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/); |
| 401 | if (ReMatIds[I->ValId] == VirtRegMap::MAX_STACK_SLOT) { |
| 402 | // Each valnum may have its own remat id. |
| 403 | ReMatIds[I->ValId] = vrm.assignVirtReMatId(NewVReg); |
| 404 | } else { |
| 405 | vrm.assignVirtReMatId(NewVReg, ReMatIds[I->ValId]); |
| 406 | } |
| 407 | if (!CanDelete || (HasUse && HasDef)) { |
| 408 | // If this is a two-addr instruction then its use operands are |
| 409 | // rematerializable but its def is not. It should be assigned a |
| 410 | // stack slot. |
| 411 | vrm.assignVirt2StackSlot(NewVReg, slot); |
| 412 | } |
| 413 | } else { |
| 414 | vrm.assignVirt2StackSlot(NewVReg, slot); |
| 415 | } |
| 416 | |
| 417 | // create a new register interval for this spill / remat. |
| 418 | LiveInterval &nI = getOrCreateInterval(NewVReg); |
| 419 | assert(nI.empty()); |
| 420 | |
| 421 | // the spill weight is now infinity as it |
| 422 | // cannot be spilled again |
| 423 | nI.weight = HUGE_VALF; |
| 424 | |
| 425 | if (HasUse) { |
| 426 | LiveRange LR(getLoadIndex(index), getUseIndex(index), |
| 427 | nI.getNextValue(~0U, 0)); |
| 428 | DOUT << " +" << LR; |
| 429 | nI.addRange(LR); |
| 430 | } |
| 431 | if (HasDef) { |
| 432 | LiveRange LR(getDefIndex(index), getStoreIndex(index), |
| 433 | nI.getNextValue(~0U, 0)); |
| 434 | DOUT << " +" << LR; |
| 435 | nI.addRange(LR); |
| 436 | } |
| 437 | |
| 438 | added.push_back(&nI); |
| 439 | |
| 440 | // update live variables if it is available |
| 441 | if (lv_) |
| 442 | lv_->addVirtualRegisterKilled(NewVReg, MI); |
| 443 | |
| 444 | DOUT << "\t\t\t\tadded new interval: "; |
| 445 | nI.print(DOUT, mri_); |
| 446 | DOUT << '\n'; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return added; |
| 453 | } |
| 454 | |
| 455 | void LiveIntervals::printRegName(unsigned reg) const { |
| 456 | if (MRegisterInfo::isPhysicalRegister(reg)) |
| 457 | cerr << mri_->getName(reg); |
| 458 | else |
| 459 | cerr << "%reg" << reg; |
| 460 | } |
| 461 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 462 | void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb, |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 463 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 464 | unsigned MIIdx, |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 465 | LiveInterval &interval) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 466 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 467 | LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 468 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 469 | // Virtual registers may be defined multiple times (due to phi |
| 470 | // elimination and 2-addr elimination). Much of what we do only has to be |
| 471 | // done once for the vreg. We use an empty interval to detect the first |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 472 | // time we see a vreg. |
| 473 | if (interval.empty()) { |
| 474 | // Get the Idx of the defining instructions. |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 475 | unsigned defIndex = getDefIndex(MIIdx); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 476 | unsigned ValNum; |
| 477 | unsigned SrcReg, DstReg; |
| 478 | if (!tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 479 | ValNum = interval.getNextValue(defIndex, 0); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 480 | else |
| 481 | ValNum = interval.getNextValue(defIndex, SrcReg); |
| 482 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 483 | assert(ValNum == 0 && "First value in interval is not 0?"); |
| 484 | ValNum = 0; // Clue in the optimizer. |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 485 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 486 | // Loop over all of the blocks that the vreg is defined in. There are |
| 487 | // two cases we have to handle here. The most common case is a vreg |
| 488 | // whose lifetime is contained within a basic block. In this case there |
| 489 | // will be a single kill, in MBB, which comes after the definition. |
| 490 | if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) { |
| 491 | // FIXME: what about dead vars? |
| 492 | unsigned killIdx; |
| 493 | if (vi.Kills[0] != mi) |
| 494 | killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1; |
| 495 | else |
| 496 | killIdx = defIndex+1; |
Chris Lattner | 6097d13 | 2004-07-19 02:15:56 +0000 | [diff] [blame] | 497 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 498 | // If the kill happens after the definition, we have an intra-block |
| 499 | // live range. |
| 500 | if (killIdx > defIndex) { |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 501 | assert(vi.AliveBlocks.none() && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 502 | "Shouldn't be alive across any blocks!"); |
| 503 | LiveRange LR(defIndex, killIdx, ValNum); |
| 504 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 505 | DOUT << " +" << LR << "\n"; |
Evan Cheng | 8df7860 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 506 | interval.addKillForValNum(ValNum, killIdx); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 509 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 510 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 511 | // The other case we handle is when a virtual register lives to the end |
| 512 | // of the defining block, potentially live across some blocks, then is |
| 513 | // live into some number of blocks, but gets killed. Start by adding a |
| 514 | // range that goes from this definition to the end of the defining block. |
Alkis Evlogimenos | d19e290 | 2004-08-31 17:39:15 +0000 | [diff] [blame] | 515 | LiveRange NewLR(defIndex, |
| 516 | getInstructionIndex(&mbb->back()) + InstrSlots::NUM, |
| 517 | ValNum); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 518 | DOUT << " +" << NewLR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 519 | interval.addRange(NewLR); |
| 520 | |
| 521 | // Iterate over all of the blocks that the variable is completely |
| 522 | // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the |
| 523 | // live interval. |
| 524 | for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) { |
| 525 | if (vi.AliveBlocks[i]) { |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 526 | MachineBasicBlock *MBB = mf_->getBlockNumbered(i); |
| 527 | if (!MBB->empty()) { |
| 528 | LiveRange LR(getMBBStartIdx(i), |
| 529 | getInstructionIndex(&MBB->back()) + InstrSlots::NUM, |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 530 | ValNum); |
| 531 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 532 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // Finally, this virtual register is live from the start of any killing |
| 538 | // block to the 'use' slot of the killing instruction. |
| 539 | for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) { |
| 540 | MachineInstr *Kill = vi.Kills[i]; |
Evan Cheng | 8df7860 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 541 | unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 542 | LiveRange LR(getMBBStartIdx(Kill->getParent()), |
Evan Cheng | 8df7860 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 543 | killIdx, ValNum); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 544 | interval.addRange(LR); |
Evan Cheng | 8df7860 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 545 | interval.addKillForValNum(ValNum, killIdx); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 546 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | } else { |
| 550 | // If this is the second time we see a virtual register definition, it |
| 551 | // must be due to phi elimination or two addr elimination. If this is |
Evan Cheng | bf105c8 | 2006-11-03 03:04:46 +0000 | [diff] [blame] | 552 | // the result of two address elimination, then the vreg is one of the |
| 553 | // def-and-use register operand. |
| 554 | if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 555 | // If this is a two-address definition, then we have already processed |
| 556 | // the live range. The only problem is that we didn't realize there |
| 557 | // are actually two values in the live interval. Because of this we |
| 558 | // need to take the LiveRegion that defines this register and split it |
| 559 | // into two values. |
| 560 | unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst)); |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 561 | unsigned RedefIndex = getDefIndex(MIIdx); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 562 | |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 563 | const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1); |
| 564 | unsigned OldEnd = OldLR->end; |
| 565 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 566 | // Delete the initial value, which should be short and continuous, |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 567 | // because the 2-addr copy must be in the same MBB as the redef. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 568 | interval.removeRange(DefIndex, RedefIndex); |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 569 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 570 | // Two-address vregs should always only be redefined once. This means |
| 571 | // that at this point, there should be exactly one value number in it. |
| 572 | assert(interval.containsOneValue() && "Unexpected 2-addr liveint!"); |
| 573 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 574 | // The new value number (#1) is defined by the instruction we claimed |
| 575 | // defined value #0. |
| 576 | unsigned ValNo = interval.getNextValue(0, 0); |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 577 | interval.copyValNumInfo(ValNo, 0); |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 579 | // Value#0 is now defined by the 2-addr instruction. |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 580 | interval.setDefForValNum(0, RedefIndex); |
| 581 | interval.setSrcRegForValNum(0, 0); |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 582 | |
| 583 | // Add the new live interval which replaces the range for the input copy. |
| 584 | LiveRange LR(DefIndex, RedefIndex, ValNo); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 585 | DOUT << " replace range with " << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 586 | interval.addRange(LR); |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 587 | interval.addKillForValNum(ValNo, RedefIndex); |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 588 | interval.removeKillForValNum(ValNo, RedefIndex, OldEnd); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 589 | |
| 590 | // If this redefinition is dead, we need to add a dummy unit live |
| 591 | // range covering the def slot. |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 592 | if (lv_->RegisterDefIsDead(mi, interval.reg)) |
| 593 | interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 594 | |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 595 | DOUT << " RESULT: "; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 596 | interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 597 | |
| 598 | } else { |
| 599 | // Otherwise, this must be because of phi elimination. If this is the |
| 600 | // first redefinition of the vreg that we have seen, go back and change |
| 601 | // the live range in the PHI block to be a different value number. |
| 602 | if (interval.containsOneValue()) { |
| 603 | assert(vi.Kills.size() == 1 && |
| 604 | "PHI elimination vreg should have one kill, the PHI itself!"); |
| 605 | |
| 606 | // Remove the old range that we now know has an incorrect number. |
| 607 | MachineInstr *Killer = vi.Kills[0]; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 608 | unsigned Start = getMBBStartIdx(Killer->getParent()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 609 | unsigned End = getUseIndex(getInstructionIndex(Killer))+1; |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 610 | DOUT << " Removing [" << Start << "," << End << "] from: "; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 611 | interval.print(DOUT, mri_); DOUT << "\n"; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 612 | interval.removeRange(Start, End); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 613 | interval.addKillForValNum(0, Start-1); // odd # means phi node |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 614 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 615 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 616 | // Replace the interval with one of a NEW value number. Note that this |
| 617 | // value number isn't actually defined by an instruction, weird huh? :) |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 618 | LiveRange LR(Start, End, interval.getNextValue(~0, 0)); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 619 | DOUT << " replace range with " << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 620 | interval.addRange(LR); |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 621 | interval.addKillForValNum(LR.ValId, End); |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 622 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // In the case of PHI elimination, each variable definition is only |
| 626 | // live until the end of the block. We've already taken care of the |
| 627 | // rest of the live range. |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 628 | unsigned defIndex = getDefIndex(MIIdx); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 629 | |
| 630 | unsigned ValNum; |
| 631 | unsigned SrcReg, DstReg; |
| 632 | if (!tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 633 | ValNum = interval.getNextValue(defIndex, 0); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 634 | else |
| 635 | ValNum = interval.getNextValue(defIndex, SrcReg); |
| 636 | |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 637 | unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM; |
| 638 | LiveRange LR(defIndex, killIndex, ValNum); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 639 | interval.addRange(LR); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame^] | 640 | interval.addKillForValNum(ValNum, killIndex-1); // odd # means phi node |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 641 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 645 | DOUT << '\n'; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 648 | void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB, |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 649 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 650 | unsigned MIIdx, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 651 | LiveInterval &interval, |
| 652 | unsigned SrcReg) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 653 | // A physical register cannot be live across basic block, so its |
| 654 | // lifetime must end somewhere in its defining basic block. |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 655 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 657 | unsigned baseIndex = MIIdx; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 658 | unsigned start = getDefIndex(baseIndex); |
| 659 | unsigned end = start; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 660 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 661 | // If it is not used after definition, it is considered dead at |
| 662 | // the instruction defining it. Hence its interval is: |
| 663 | // [defSlot(def), defSlot(def)+1) |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 664 | if (lv_->RegisterDefIsDead(mi, interval.reg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 665 | DOUT << " dead"; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 666 | end = getDefIndex(start) + 1; |
| 667 | goto exit; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // If it is not dead on definition, it must be killed by a |
| 671 | // subsequent instruction. Hence its interval is: |
| 672 | // [defSlot(def), useSlot(kill)+1) |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 673 | while (++mi != MBB->end()) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 674 | baseIndex += InstrSlots::NUM; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 675 | if (lv_->KillsRegister(mi, interval.reg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 676 | DOUT << " killed"; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 677 | end = getUseIndex(baseIndex) + 1; |
| 678 | goto exit; |
Evan Cheng | 9a1956a | 2006-11-15 20:54:11 +0000 | [diff] [blame] | 679 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 680 | // Another instruction redefines the register before it is ever read. |
| 681 | // Then the register is essentially dead at the instruction that defines |
| 682 | // it. Hence its interval is: |
| 683 | // [defSlot(def), defSlot(def)+1) |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 684 | DOUT << " dead"; |
Evan Cheng | 9a1956a | 2006-11-15 20:54:11 +0000 | [diff] [blame] | 685 | end = getDefIndex(start) + 1; |
| 686 | goto exit; |
Alkis Evlogimenos | af25473 | 2004-01-13 22:26:14 +0000 | [diff] [blame] | 687 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 689 | |
| 690 | // The only case we should have a dead physreg here without a killing or |
| 691 | // instruction where we know it's dead is if it is live-in to the function |
| 692 | // and never used. |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 693 | assert(!SrcReg && "physreg was not killed in defining block!"); |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 694 | end = getDefIndex(start) + 1; // It's dead. |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 695 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 696 | exit: |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 697 | assert(start < end && "did not find end of interval?"); |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 698 | |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 699 | // Already exists? Extend old live interval. |
| 700 | LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start); |
| 701 | unsigned Id = (OldLR != interval.end()) |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 702 | ? OldLR->ValId : interval.getNextValue(start, SrcReg); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 703 | LiveRange LR(start, end, Id); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 704 | interval.addRange(LR); |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 705 | interval.addKillForValNum(LR.ValId, end); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 706 | DOUT << " +" << LR << '\n'; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 709 | void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB, |
| 710 | MachineBasicBlock::iterator MI, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 711 | unsigned MIIdx, |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 712 | unsigned reg) { |
| 713 | if (MRegisterInfo::isVirtualRegister(reg)) |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 714 | handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg)); |
Alkis Evlogimenos | 5327801 | 2004-08-26 22:22:38 +0000 | [diff] [blame] | 715 | else if (allocatableRegs_[reg]) { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 716 | unsigned SrcReg, DstReg; |
| 717 | if (!tii_->isMoveInstr(*MI, SrcReg, DstReg)) |
| 718 | SrcReg = 0; |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 719 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 720 | // Def of a register also defines its sub-registers. |
| 721 | for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS) |
| 722 | // Avoid processing some defs more than once. |
| 723 | if (!MI->findRegisterDefOperand(*AS)) |
| 724 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0); |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 725 | } |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 728 | void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB, |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 729 | unsigned MIIdx, |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 730 | LiveInterval &interval, bool isAlias) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 731 | DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg)); |
| 732 | |
| 733 | // Look for kills, if it reaches a def before it's killed, then it shouldn't |
| 734 | // be considered a livein. |
| 735 | MachineBasicBlock::iterator mi = MBB->begin(); |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 736 | unsigned baseIndex = MIIdx; |
| 737 | unsigned start = baseIndex; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 738 | unsigned end = start; |
| 739 | while (mi != MBB->end()) { |
| 740 | if (lv_->KillsRegister(mi, interval.reg)) { |
| 741 | DOUT << " killed"; |
| 742 | end = getUseIndex(baseIndex) + 1; |
| 743 | goto exit; |
| 744 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 745 | // Another instruction redefines the register before it is ever read. |
| 746 | // Then the register is essentially dead at the instruction that defines |
| 747 | // it. Hence its interval is: |
| 748 | // [defSlot(def), defSlot(def)+1) |
| 749 | DOUT << " dead"; |
| 750 | end = getDefIndex(start) + 1; |
| 751 | goto exit; |
| 752 | } |
| 753 | |
| 754 | baseIndex += InstrSlots::NUM; |
| 755 | ++mi; |
| 756 | } |
| 757 | |
| 758 | exit: |
Evan Cheng | 75611fb | 2007-06-27 01:16:36 +0000 | [diff] [blame] | 759 | // Live-in register might not be used at all. |
| 760 | if (end == MIIdx) { |
Evan Cheng | 292da94 | 2007-06-27 18:47:28 +0000 | [diff] [blame] | 761 | if (isAlias) { |
| 762 | DOUT << " dead"; |
Evan Cheng | 75611fb | 2007-06-27 01:16:36 +0000 | [diff] [blame] | 763 | end = getDefIndex(MIIdx) + 1; |
Evan Cheng | 292da94 | 2007-06-27 18:47:28 +0000 | [diff] [blame] | 764 | } else { |
| 765 | DOUT << " live through"; |
| 766 | end = baseIndex; |
| 767 | } |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 770 | LiveRange LR(start, end, interval.getNextValue(start, 0)); |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 771 | interval.addRange(LR); |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 772 | interval.addKillForValNum(LR.ValId, end); |
| 773 | DOUT << " +" << LR << '\n'; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 776 | /// computeIntervals - computes the live intervals for virtual |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 777 | /// registers. for some ordering of the machine instructions [1,N] a |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 778 | /// live interval is an interval [i, j) where 1 <= i <= j < N for |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 779 | /// which a variable is live |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 780 | void LiveIntervals::computeIntervals() { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 781 | DOUT << "********** COMPUTING LIVE INTERVALS **********\n" |
| 782 | << "********** Function: " |
| 783 | << ((Value*)mf_->getFunction())->getName() << '\n'; |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 784 | // Track the index of the current machine instr. |
| 785 | unsigned MIIndex = 0; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 786 | for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end(); |
| 787 | MBBI != E; ++MBBI) { |
| 788 | MachineBasicBlock *MBB = MBBI; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 789 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 791 | MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end(); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 792 | |
| 793 | if (MBB->livein_begin() != MBB->livein_end()) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 794 | // Create intervals for live-ins to this BB first. |
| 795 | for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(), |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 796 | LE = MBB->livein_end(); LI != LE; ++LI) { |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 797 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI)); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 798 | // Multiple live-ins can alias the same register. |
| 799 | for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS) |
| 800 | if (!hasInterval(*AS)) |
Evan Cheng | 292da94 | 2007-06-27 18:47:28 +0000 | [diff] [blame] | 801 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS), |
| 802 | true); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | dffb2e8 | 2006-09-04 18:27:40 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 806 | for (; MI != miEnd; ++MI) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 807 | DOUT << MIIndex << "\t" << *MI; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 808 | |
Evan Cheng | 438f7bc | 2006-11-10 08:43:01 +0000 | [diff] [blame] | 809 | // Handle defs. |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 810 | for (int i = MI->getNumOperands() - 1; i >= 0; --i) { |
| 811 | MachineOperand &MO = MI->getOperand(i); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 812 | // handle register defs - build intervals |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 813 | if (MO.isRegister() && MO.getReg() && MO.isDef()) |
| 814 | handleRegisterDef(MBB, MI, MIIndex, MO.getReg()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 815 | } |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 816 | |
| 817 | MIIndex += InstrSlots::NUM; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 818 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 819 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 820 | } |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 821 | |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 822 | LiveInterval LiveIntervals::createInterval(unsigned reg) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 823 | float Weight = MRegisterInfo::isPhysicalRegister(reg) ? |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 824 | HUGE_VALF : 0.0F; |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 825 | return LiveInterval(reg, Weight); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 826 | } |