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"); |
| 41 | STATISTIC(numJoins , "Number of interval joins performed"); |
| 42 | STATISTIC(numPeep , "Number of identity moves eliminated after coalescing"); |
| 43 | STATISTIC(numFolded , "Number of loads/stores folded into instructions"); |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 44 | STATISTIC(numAborts , "Number of times interval joining aborted"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 45 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 46 | namespace { |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 47 | RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis"); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 48 | |
Andrew Lenharth | ed41f1b | 2006-07-20 17:28:38 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 50 | EnableJoining("join-liveintervals", |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 51 | cl::desc("Coallesce copies (default=true)"), |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 52 | cl::init(true)); |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 53 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 54 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 55 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 56 | AU.addRequired<LiveVariables>(); |
| 57 | AU.addPreservedID(PHIEliminationID); |
| 58 | AU.addRequiredID(PHIEliminationID); |
| 59 | AU.addRequiredID(TwoAddressInstructionPassID); |
| 60 | AU.addRequired<LoopInfo>(); |
| 61 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 64 | void LiveIntervals::releaseMemory() { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 65 | mi2iMap_.clear(); |
| 66 | i2miMap_.clear(); |
| 67 | r2iMap_.clear(); |
| 68 | r2rMap_.clear(); |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 69 | JoinedLIs.clear(); |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
Evan Cheng | 9931414 | 2006-05-11 07:29:24 +0000 | [diff] [blame] | 73 | static bool isZeroLengthInterval(LiveInterval *li) { |
| 74 | for (LiveInterval::Ranges::const_iterator |
| 75 | i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i) |
| 76 | if (i->end - i->start > LiveIntervals::InstrSlots::NUM) |
| 77 | return false; |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 82 | /// runOnMachineFunction - Register allocate the whole function |
| 83 | /// |
| 84 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 85 | mf_ = &fn; |
| 86 | tm_ = &fn.getTarget(); |
| 87 | mri_ = tm_->getRegisterInfo(); |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 88 | tii_ = tm_->getInstrInfo(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 89 | lv_ = &getAnalysis<LiveVariables>(); |
Alkis Evlogimenos | 5327801 | 2004-08-26 22:22:38 +0000 | [diff] [blame] | 90 | allocatableRegs_ = mri_->getAllocatableSet(fn); |
Alkis Evlogimenos | 2c4f7b5 | 2004-09-09 19:24:38 +0000 | [diff] [blame] | 91 | r2rMap_.grow(mf_->getSSARegMap()->getLastVirtReg()); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 93 | // Number MachineInstrs and MachineBasicBlocks. |
| 94 | // Initialize MBB indexes to a sentinal. |
| 95 | MBB2IdxMap.resize(mf_->getNumBlockIDs(), ~0U); |
| 96 | |
| 97 | unsigned MIIndex = 0; |
| 98 | for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end(); |
| 99 | MBB != E; ++MBB) { |
| 100 | // Set the MBB2IdxMap entry for this MBB. |
| 101 | MBB2IdxMap[MBB->getNumber()] = MIIndex; |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 103 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 104 | I != E; ++I) { |
| 105 | bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 106 | assert(inserted && "multiple MachineInstr -> index mappings"); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 107 | i2miMap_.push_back(I); |
| 108 | MIIndex += InstrSlots::NUM; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 110 | } |
Alkis Evlogimenos | d6e40a6 | 2004-01-14 10:44:29 +0000 | [diff] [blame] | 111 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 112 | computeIntervals(); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 113 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 114 | numIntervals += getNumIntervals(); |
| 115 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 116 | DOUT << "********** INTERVALS **********\n"; |
| 117 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 118 | I->second.print(DOUT, mri_); |
| 119 | DOUT << "\n"; |
| 120 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 122 | // Join (coallesce) intervals if requested. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 123 | if (EnableJoining) joinIntervals(); |
| 124 | |
| 125 | numIntervalsAfter += getNumIntervals(); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 126 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 127 | |
| 128 | // perform a final pass over the instructions and compute spill |
Chris Lattner | fbecc5a | 2006-09-03 07:53:50 +0000 | [diff] [blame] | 129 | // weights, coalesce virtual registers and remove identity moves. |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 130 | const LoopInfo &loopInfo = getAnalysis<LoopInfo>(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 131 | |
| 132 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 133 | mbbi != mbbe; ++mbbi) { |
| 134 | MachineBasicBlock* mbb = mbbi; |
| 135 | unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock()); |
| 136 | |
| 137 | for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end(); |
| 138 | mii != mie; ) { |
| 139 | // if the move will be an identity move delete it |
| 140 | unsigned srcReg, dstReg, RegRep; |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 141 | if (tii_->isMoveInstr(*mii, srcReg, dstReg) && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 142 | (RegRep = rep(srcReg)) == rep(dstReg)) { |
| 143 | // remove from def list |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 144 | LiveInterval &RegInt = getOrCreateInterval(RegRep); |
| 145 | MachineOperand *MO = mii->findRegisterDefOperand(dstReg); |
| 146 | // If def of this move instruction is dead, remove its live range from |
| 147 | // the dstination register's live interval. |
| 148 | if (MO->isDead()) { |
| 149 | unsigned MoveIdx = getDefIndex(getInstructionIndex(mii)); |
| 150 | LiveInterval::iterator MLR = RegInt.FindLiveRangeContaining(MoveIdx); |
| 151 | RegInt.removeRange(MLR->start, MoveIdx+1); |
| 152 | if (RegInt.empty()) |
| 153 | removeInterval(RegRep); |
| 154 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 155 | RemoveMachineInstrFromMaps(mii); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 156 | mii = mbbi->erase(mii); |
| 157 | ++numPeep; |
| 158 | } |
| 159 | else { |
Chris Lattner | fbecc5a | 2006-09-03 07:53:50 +0000 | [diff] [blame] | 160 | for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) { |
| 161 | const MachineOperand &mop = mii->getOperand(i); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 162 | if (mop.isRegister() && mop.getReg() && |
| 163 | MRegisterInfo::isVirtualRegister(mop.getReg())) { |
| 164 | // replace register with representative register |
| 165 | unsigned reg = rep(mop.getReg()); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 166 | mii->getOperand(i).setReg(reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 167 | |
| 168 | LiveInterval &RegInt = getInterval(reg); |
| 169 | RegInt.weight += |
Chris Lattner | 7a36ae8 | 2004-10-25 18:40:47 +0000 | [diff] [blame] | 170 | (mop.isUse() + mop.isDef()) * pow(10.0F, (int)loopDepth); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | ++mii; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Evan Cheng | 9931414 | 2006-05-11 07:29:24 +0000 | [diff] [blame] | 178 | for (iterator I = begin(), E = end(); I != E; ++I) { |
Chris Lattner | b75a663 | 2006-11-07 07:18:40 +0000 | [diff] [blame] | 179 | LiveInterval &LI = I->second; |
| 180 | if (MRegisterInfo::isVirtualRegister(LI.reg)) { |
Chris Lattner | c9d94d1 | 2006-08-27 12:47:48 +0000 | [diff] [blame] | 181 | // If the live interval length is essentially zero, i.e. in every live |
Evan Cheng | 9931414 | 2006-05-11 07:29:24 +0000 | [diff] [blame] | 182 | // range the use follows def immediately, it doesn't make sense to spill |
| 183 | // it and hope it will be easier to allocate for this li. |
Chris Lattner | b75a663 | 2006-11-07 07:18:40 +0000 | [diff] [blame] | 184 | if (isZeroLengthInterval(&LI)) |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 185 | LI.weight = HUGE_VALF; |
Chris Lattner | b75a663 | 2006-11-07 07:18:40 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 393ebae | 2006-11-07 18:04:58 +0000 | [diff] [blame] | 187 | // Divide the weight of the interval by its size. This encourages |
| 188 | // spilling of intervals that are large and have few uses, and |
| 189 | // discourages spilling of small intervals with many uses. |
| 190 | unsigned Size = 0; |
| 191 | for (LiveInterval::iterator II = LI.begin(), E = LI.end(); II != E;++II) |
| 192 | Size += II->end - II->start; |
Chris Lattner | b75a663 | 2006-11-07 07:18:40 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 393ebae | 2006-11-07 18:04:58 +0000 | [diff] [blame] | 194 | LI.weight /= Size; |
Chris Lattner | c9d94d1 | 2006-08-27 12:47:48 +0000 | [diff] [blame] | 195 | } |
Evan Cheng | 9931414 | 2006-05-11 07:29:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 198 | DEBUG(dump()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 199 | return true; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 202 | /// print - Implement the dump method. |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 203 | void LiveIntervals::print(std::ostream &O, const Module* ) const { |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 204 | O << "********** INTERVALS **********\n"; |
Chris Lattner | 8e7a709 | 2005-07-27 23:03:38 +0000 | [diff] [blame] | 205 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 206 | I->second.print(DOUT, mri_); |
| 207 | DOUT << "\n"; |
Chris Lattner | 8e7a709 | 2005-07-27 23:03:38 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 209 | |
| 210 | O << "********** MACHINEINSTRS **********\n"; |
| 211 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 212 | mbbi != mbbe; ++mbbi) { |
| 213 | O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n"; |
| 214 | for (MachineBasicBlock::iterator mii = mbbi->begin(), |
| 215 | mie = mbbi->end(); mii != mie; ++mii) { |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 216 | O << getInstructionIndex(mii) << '\t' << *mii; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 221 | /// CreateNewLiveInterval - Create a new live interval with the given live |
| 222 | /// ranges. The new live interval will have an infinite spill weight. |
| 223 | LiveInterval& |
| 224 | LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI, |
| 225 | const std::vector<LiveRange> &LRs) { |
| 226 | const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg); |
| 227 | |
| 228 | // Create a new virtual register for the spill interval. |
| 229 | unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC); |
| 230 | |
| 231 | // Replace the old virtual registers in the machine operands with the shiny |
| 232 | // new one. |
| 233 | for (std::vector<LiveRange>::const_iterator |
| 234 | I = LRs.begin(), E = LRs.end(); I != E; ++I) { |
| 235 | unsigned Index = getBaseIndex(I->start); |
| 236 | unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM; |
| 237 | |
| 238 | for (; Index != End; Index += InstrSlots::NUM) { |
| 239 | // Skip deleted instructions |
| 240 | while (Index != End && !getInstructionFromIndex(Index)) |
| 241 | Index += InstrSlots::NUM; |
| 242 | |
| 243 | if (Index == End) break; |
| 244 | |
| 245 | MachineInstr *MI = getInstructionFromIndex(Index); |
| 246 | |
Bill Wendling | beeb77f | 2006-11-16 07:35:18 +0000 | [diff] [blame] | 247 | for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) { |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 248 | MachineOperand &MOp = MI->getOperand(J); |
| 249 | if (MOp.isRegister() && rep(MOp.getReg()) == LI->reg) |
| 250 | MOp.setReg(NewVReg); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | LiveInterval &NewLI = getOrCreateInterval(NewVReg); |
| 256 | |
| 257 | // The spill weight is now infinity as it cannot be spilled again |
| 258 | NewLI.weight = float(HUGE_VAL); |
| 259 | |
| 260 | for (std::vector<LiveRange>::const_iterator |
| 261 | I = LRs.begin(), E = LRs.end(); I != E; ++I) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 262 | DOUT << " Adding live range " << *I << " to new interval\n"; |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 263 | NewLI.addRange(*I); |
| 264 | } |
| 265 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 266 | DOUT << "Created new live interval " << NewLI << "\n"; |
Bill Wendling | 01352aa | 2006-11-16 02:41:50 +0000 | [diff] [blame] | 267 | return NewLI; |
| 268 | } |
| 269 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 270 | std::vector<LiveInterval*> LiveIntervals:: |
| 271 | addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, int slot) { |
Alkis Evlogimenos | d8d26b3 | 2004-08-27 18:59:22 +0000 | [diff] [blame] | 272 | // since this is called after the analysis is done we don't know if |
| 273 | // LiveVariables is available |
| 274 | lv_ = getAnalysisToUpdate<LiveVariables>(); |
| 275 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 276 | std::vector<LiveInterval*> added; |
Alkis Evlogimenos | 26f5a69 | 2004-05-30 07:24:39 +0000 | [diff] [blame] | 277 | |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 278 | assert(li.weight != HUGE_VALF && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 279 | "attempt to spill already spilled interval!"); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 280 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 281 | DOUT << "\t\t\t\tadding intervals for spills for interval: "; |
| 282 | li.print(DOUT, mri_); |
| 283 | DOUT << '\n'; |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 284 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 285 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg); |
Alkis Evlogimenos | 26f5a69 | 2004-05-30 07:24:39 +0000 | [diff] [blame] | 286 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 287 | for (LiveInterval::Ranges::const_iterator |
| 288 | i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) { |
| 289 | unsigned index = getBaseIndex(i->start); |
| 290 | unsigned end = getBaseIndex(i->end-1) + InstrSlots::NUM; |
| 291 | for (; index != end; index += InstrSlots::NUM) { |
| 292 | // skip deleted instructions |
| 293 | while (index != end && !getInstructionFromIndex(index)) |
| 294 | index += InstrSlots::NUM; |
| 295 | if (index == end) break; |
Chris Lattner | 8640f4e | 2004-07-19 15:16:53 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 3b9db83 | 2006-01-03 07:41:37 +0000 | [diff] [blame] | 297 | MachineInstr *MI = getInstructionFromIndex(index); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 299 | RestartInstruction: |
Chris Lattner | 3b9db83 | 2006-01-03 07:41:37 +0000 | [diff] [blame] | 300 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 301 | MachineOperand& mop = MI->getOperand(i); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 302 | if (mop.isRegister() && mop.getReg() == li.reg) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 303 | if (MachineInstr *fmi = mri_->foldMemoryOperand(MI, i, slot)) { |
Chris Lattner | b11443d | 2005-09-09 19:17:47 +0000 | [diff] [blame] | 304 | // Attempt to fold the memory reference into the instruction. If we |
| 305 | // can do this, we don't need to insert spill code. |
Alkis Evlogimenos | d8d26b3 | 2004-08-27 18:59:22 +0000 | [diff] [blame] | 306 | if (lv_) |
Chris Lattner | 3b9db83 | 2006-01-03 07:41:37 +0000 | [diff] [blame] | 307 | lv_->instructionChanged(MI, fmi); |
Evan Cheng | 200370f | 2006-04-30 08:41:47 +0000 | [diff] [blame] | 308 | MachineBasicBlock &MBB = *MI->getParent(); |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 309 | vrm.virtFolded(li.reg, MI, i, fmi); |
Chris Lattner | 3b9db83 | 2006-01-03 07:41:37 +0000 | [diff] [blame] | 310 | mi2iMap_.erase(MI); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 311 | i2miMap_[index/InstrSlots::NUM] = fmi; |
| 312 | mi2iMap_[fmi] = index; |
Chris Lattner | 3b9db83 | 2006-01-03 07:41:37 +0000 | [diff] [blame] | 313 | MI = MBB.insert(MBB.erase(MI), fmi); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 314 | ++numFolded; |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 315 | // Folding the load/store can completely change the instruction in |
| 316 | // unpredictable ways, rescan it from the beginning. |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 317 | goto RestartInstruction; |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 318 | } else { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 319 | // Create a new virtual register for the spill interval. |
| 320 | unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc); |
| 321 | |
| 322 | // Scan all of the operands of this instruction rewriting operands |
| 323 | // to use NewVReg instead of li.reg as appropriate. We do this for |
| 324 | // two reasons: |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 325 | // |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 326 | // 1. If the instr reads the same spilled vreg multiple times, we |
| 327 | // want to reuse the NewVReg. |
| 328 | // 2. If the instr is a two-addr instruction, we are required to |
| 329 | // keep the src/dst regs pinned. |
| 330 | // |
| 331 | // Keep track of whether we replace a use and/or def so that we can |
| 332 | // create the spill interval with the appropriate range. |
| 333 | mop.setReg(NewVReg); |
| 334 | |
| 335 | bool HasUse = mop.isUse(); |
| 336 | bool HasDef = mop.isDef(); |
| 337 | for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) { |
| 338 | if (MI->getOperand(j).isReg() && |
| 339 | MI->getOperand(j).getReg() == li.reg) { |
| 340 | MI->getOperand(j).setReg(NewVReg); |
| 341 | HasUse |= MI->getOperand(j).isUse(); |
| 342 | HasDef |= MI->getOperand(j).isDef(); |
| 343 | } |
| 344 | } |
Alkis Evlogimenos | 26f5a69 | 2004-05-30 07:24:39 +0000 | [diff] [blame] | 345 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 346 | // create a new register for this spill |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 347 | vrm.grow(); |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 348 | vrm.assignVirt2StackSlot(NewVReg, slot); |
| 349 | LiveInterval &nI = getOrCreateInterval(NewVReg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 350 | assert(nI.empty()); |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 351 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 352 | // the spill weight is now infinity as it |
| 353 | // cannot be spilled again |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 354 | nI.weight = HUGE_VALF; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 355 | |
| 356 | if (HasUse) { |
| 357 | LiveRange LR(getLoadIndex(index), getUseIndex(index), |
| 358 | nI.getNextValue(~0U, 0)); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 359 | DOUT << " +" << LR; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 360 | nI.addRange(LR); |
| 361 | } |
| 362 | if (HasDef) { |
| 363 | LiveRange LR(getDefIndex(index), getStoreIndex(index), |
| 364 | nI.getNextValue(~0U, 0)); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 365 | DOUT << " +" << LR; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 366 | nI.addRange(LR); |
| 367 | } |
| 368 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 369 | added.push_back(&nI); |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 370 | |
Alkis Evlogimenos | d8d26b3 | 2004-08-27 18:59:22 +0000 | [diff] [blame] | 371 | // update live variables if it is available |
| 372 | if (lv_) |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 373 | lv_->addVirtualRegisterKilled(NewVReg, MI); |
Chris Lattner | b11443d | 2005-09-09 19:17:47 +0000 | [diff] [blame] | 374 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 375 | DOUT << "\t\t\t\tadded new interval: "; |
| 376 | nI.print(DOUT, mri_); |
| 377 | DOUT << '\n'; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 378 | } |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 379 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 380 | } |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 381 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 382 | } |
Alkis Evlogimenos | 26f5a69 | 2004-05-30 07:24:39 +0000 | [diff] [blame] | 383 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 384 | return added; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 387 | void LiveIntervals::printRegName(unsigned reg) const { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 388 | if (MRegisterInfo::isPhysicalRegister(reg)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 389 | cerr << mri_->getName(reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 390 | else |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 391 | cerr << "%reg" << reg; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Evan Cheng | bf105c8 | 2006-11-03 03:04:46 +0000 | [diff] [blame] | 394 | /// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to |
| 395 | /// two addr elimination. |
| 396 | static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg, |
| 397 | const TargetInstrInfo *TII) { |
| 398 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 399 | MachineOperand &MO1 = MI->getOperand(i); |
| 400 | if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) { |
| 401 | for (unsigned j = i+1; j < e; ++j) { |
| 402 | MachineOperand &MO2 = MI->getOperand(j); |
| 403 | if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg && |
Evan Cheng | 51cdcd1 | 2006-12-07 01:21:59 +0000 | [diff] [blame] | 404 | MI->getInstrDescriptor()-> |
| 405 | getOperandConstraint(j, TOI::TIED_TO) == (int)i) |
Evan Cheng | bf105c8 | 2006-11-03 03:04:46 +0000 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | return false; |
| 411 | } |
| 412 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 413 | void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb, |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 414 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 415 | unsigned MIIdx, |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 416 | LiveInterval &interval) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 417 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 418 | LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 419 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 420 | // Virtual registers may be defined multiple times (due to phi |
| 421 | // elimination and 2-addr elimination). Much of what we do only has to be |
| 422 | // 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] | 423 | // time we see a vreg. |
| 424 | if (interval.empty()) { |
| 425 | // Get the Idx of the defining instructions. |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 426 | unsigned defIndex = getDefIndex(MIIdx); |
Chris Lattner | 6097d13 | 2004-07-19 02:15:56 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 428 | unsigned ValNum; |
| 429 | unsigned SrcReg, DstReg; |
| 430 | if (!tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
| 431 | ValNum = interval.getNextValue(~0U, 0); |
| 432 | else |
| 433 | ValNum = interval.getNextValue(defIndex, SrcReg); |
| 434 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 435 | assert(ValNum == 0 && "First value in interval is not 0?"); |
| 436 | ValNum = 0; // Clue in the optimizer. |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 437 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 438 | // Loop over all of the blocks that the vreg is defined in. There are |
| 439 | // two cases we have to handle here. The most common case is a vreg |
| 440 | // whose lifetime is contained within a basic block. In this case there |
| 441 | // will be a single kill, in MBB, which comes after the definition. |
| 442 | if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) { |
| 443 | // FIXME: what about dead vars? |
| 444 | unsigned killIdx; |
| 445 | if (vi.Kills[0] != mi) |
| 446 | killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1; |
| 447 | else |
| 448 | killIdx = defIndex+1; |
Chris Lattner | 6097d13 | 2004-07-19 02:15:56 +0000 | [diff] [blame] | 449 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 450 | // If the kill happens after the definition, we have an intra-block |
| 451 | // live range. |
| 452 | if (killIdx > defIndex) { |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 453 | assert(vi.AliveBlocks.none() && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 454 | "Shouldn't be alive across any blocks!"); |
| 455 | LiveRange LR(defIndex, killIdx, ValNum); |
| 456 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 457 | DOUT << " +" << LR << "\n"; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 458 | return; |
| 459 | } |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 460 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 461 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 462 | // The other case we handle is when a virtual register lives to the end |
| 463 | // of the defining block, potentially live across some blocks, then is |
| 464 | // live into some number of blocks, but gets killed. Start by adding a |
| 465 | // 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] | 466 | LiveRange NewLR(defIndex, |
| 467 | getInstructionIndex(&mbb->back()) + InstrSlots::NUM, |
| 468 | ValNum); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 469 | DOUT << " +" << NewLR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 470 | interval.addRange(NewLR); |
| 471 | |
| 472 | // Iterate over all of the blocks that the variable is completely |
| 473 | // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the |
| 474 | // live interval. |
| 475 | for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) { |
| 476 | if (vi.AliveBlocks[i]) { |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 477 | MachineBasicBlock *MBB = mf_->getBlockNumbered(i); |
| 478 | if (!MBB->empty()) { |
| 479 | LiveRange LR(getMBBStartIdx(i), |
| 480 | getInstructionIndex(&MBB->back()) + InstrSlots::NUM, |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 481 | ValNum); |
| 482 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 483 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Finally, this virtual register is live from the start of any killing |
| 489 | // block to the 'use' slot of the killing instruction. |
| 490 | for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) { |
| 491 | MachineInstr *Kill = vi.Kills[i]; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 492 | LiveRange LR(getMBBStartIdx(Kill->getParent()), |
Alkis Evlogimenos | d19e290 | 2004-08-31 17:39:15 +0000 | [diff] [blame] | 493 | getUseIndex(getInstructionIndex(Kill))+1, |
| 494 | ValNum); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 495 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 496 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | } else { |
| 500 | // If this is the second time we see a virtual register definition, it |
| 501 | // 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] | 502 | // the result of two address elimination, then the vreg is one of the |
| 503 | // def-and-use register operand. |
| 504 | if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 505 | // If this is a two-address definition, then we have already processed |
| 506 | // the live range. The only problem is that we didn't realize there |
| 507 | // are actually two values in the live interval. Because of this we |
| 508 | // need to take the LiveRegion that defines this register and split it |
| 509 | // into two values. |
| 510 | unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst)); |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 511 | unsigned RedefIndex = getDefIndex(MIIdx); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 512 | |
| 513 | // Delete the initial value, which should be short and continuous, |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 514 | // 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] | 515 | interval.removeRange(DefIndex, RedefIndex); |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 516 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 517 | // Two-address vregs should always only be redefined once. This means |
| 518 | // that at this point, there should be exactly one value number in it. |
| 519 | assert(interval.containsOneValue() && "Unexpected 2-addr liveint!"); |
| 520 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 521 | // The new value number (#1) is defined by the instruction we claimed |
| 522 | // defined value #0. |
| 523 | unsigned ValNo = interval.getNextValue(0, 0); |
| 524 | interval.setValueNumberInfo(1, interval.getValNumInfo(0)); |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 526 | // Value#0 is now defined by the 2-addr instruction. |
| 527 | interval.setValueNumberInfo(0, std::make_pair(~0U, 0U)); |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 528 | |
| 529 | // Add the new live interval which replaces the range for the input copy. |
| 530 | LiveRange LR(DefIndex, RedefIndex, ValNo); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 531 | DOUT << " replace range with " << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 532 | interval.addRange(LR); |
| 533 | |
| 534 | // If this redefinition is dead, we need to add a dummy unit live |
| 535 | // range covering the def slot. |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 536 | if (lv_->RegisterDefIsDead(mi, interval.reg)) |
| 537 | interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 538 | |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 539 | DOUT << " RESULT: "; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 540 | interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 541 | |
| 542 | } else { |
| 543 | // Otherwise, this must be because of phi elimination. If this is the |
| 544 | // first redefinition of the vreg that we have seen, go back and change |
| 545 | // the live range in the PHI block to be a different value number. |
| 546 | if (interval.containsOneValue()) { |
| 547 | assert(vi.Kills.size() == 1 && |
| 548 | "PHI elimination vreg should have one kill, the PHI itself!"); |
| 549 | |
| 550 | // Remove the old range that we now know has an incorrect number. |
| 551 | MachineInstr *Killer = vi.Kills[0]; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 552 | unsigned Start = getMBBStartIdx(Killer->getParent()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 553 | unsigned End = getUseIndex(getInstructionIndex(Killer))+1; |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 554 | DOUT << " Removing [" << Start << "," << End << "] from: "; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 555 | interval.print(DOUT, mri_); DOUT << "\n"; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 556 | interval.removeRange(Start, End); |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 557 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 558 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 559 | // Replace the interval with one of a NEW value number. Note that this |
| 560 | // value number isn't actually defined by an instruction, weird huh? :) |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 561 | LiveRange LR(Start, End, interval.getNextValue(~0U, 0)); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 562 | DOUT << " replace range with " << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 563 | interval.addRange(LR); |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 564 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | // In the case of PHI elimination, each variable definition is only |
| 568 | // live until the end of the block. We've already taken care of the |
| 569 | // rest of the live range. |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 570 | unsigned defIndex = getDefIndex(MIIdx); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 571 | |
| 572 | unsigned ValNum; |
| 573 | unsigned SrcReg, DstReg; |
| 574 | if (!tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
| 575 | ValNum = interval.getNextValue(~0U, 0); |
| 576 | else |
| 577 | ValNum = interval.getNextValue(defIndex, SrcReg); |
| 578 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 579 | LiveRange LR(defIndex, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 580 | getInstructionIndex(&mbb->back()) + InstrSlots::NUM, ValNum); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 581 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 582 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 586 | DOUT << '\n'; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 589 | void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB, |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 590 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 591 | unsigned MIIdx, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 592 | LiveInterval &interval, |
| 593 | unsigned SrcReg) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 594 | // A physical register cannot be live across basic block, so its |
| 595 | // lifetime must end somewhere in its defining basic block. |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 596 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 598 | unsigned baseIndex = MIIdx; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 599 | unsigned start = getDefIndex(baseIndex); |
| 600 | unsigned end = start; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 601 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 602 | // If it is not used after definition, it is considered dead at |
| 603 | // the instruction defining it. Hence its interval is: |
| 604 | // [defSlot(def), defSlot(def)+1) |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 605 | if (lv_->RegisterDefIsDead(mi, interval.reg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 606 | DOUT << " dead"; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 607 | end = getDefIndex(start) + 1; |
| 608 | goto exit; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // If it is not dead on definition, it must be killed by a |
| 612 | // subsequent instruction. Hence its interval is: |
| 613 | // [defSlot(def), useSlot(kill)+1) |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 614 | while (++mi != MBB->end()) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 615 | baseIndex += InstrSlots::NUM; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 616 | if (lv_->KillsRegister(mi, interval.reg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 617 | DOUT << " killed"; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 618 | end = getUseIndex(baseIndex) + 1; |
| 619 | goto exit; |
Evan Cheng | 9a1956a | 2006-11-15 20:54:11 +0000 | [diff] [blame] | 620 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 621 | // Another instruction redefines the register before it is ever read. |
| 622 | // Then the register is essentially dead at the instruction that defines |
| 623 | // it. Hence its interval is: |
| 624 | // [defSlot(def), defSlot(def)+1) |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 625 | DOUT << " dead"; |
Evan Cheng | 9a1956a | 2006-11-15 20:54:11 +0000 | [diff] [blame] | 626 | end = getDefIndex(start) + 1; |
| 627 | goto exit; |
Alkis Evlogimenos | af25473 | 2004-01-13 22:26:14 +0000 | [diff] [blame] | 628 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 629 | } |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 630 | |
| 631 | // The only case we should have a dead physreg here without a killing or |
| 632 | // instruction where we know it's dead is if it is live-in to the function |
| 633 | // and never used. |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 634 | assert(!SrcReg && "physreg was not killed in defining block!"); |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 635 | end = getDefIndex(start) + 1; // It's dead. |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 636 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 637 | exit: |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 638 | assert(start < end && "did not find end of interval?"); |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 639 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 640 | LiveRange LR(start, end, interval.getNextValue(SrcReg != 0 ? start : ~0U, |
| 641 | SrcReg)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 642 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 643 | DOUT << " +" << LR << '\n'; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 646 | void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB, |
| 647 | MachineBasicBlock::iterator MI, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 648 | unsigned MIIdx, |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 649 | unsigned reg) { |
| 650 | if (MRegisterInfo::isVirtualRegister(reg)) |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 651 | handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg)); |
Alkis Evlogimenos | 5327801 | 2004-08-26 22:22:38 +0000 | [diff] [blame] | 652 | else if (allocatableRegs_[reg]) { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 653 | unsigned SrcReg, DstReg; |
| 654 | if (!tii_->isMoveInstr(*MI, SrcReg, DstReg)) |
| 655 | SrcReg = 0; |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 656 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg); |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 657 | for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS) |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 658 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0); |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 659 | } |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 662 | void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB, |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 663 | unsigned MIIdx, |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 664 | LiveInterval &interval) { |
| 665 | DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg)); |
| 666 | |
| 667 | // Look for kills, if it reaches a def before it's killed, then it shouldn't |
| 668 | // be considered a livein. |
| 669 | MachineBasicBlock::iterator mi = MBB->begin(); |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 670 | unsigned baseIndex = MIIdx; |
| 671 | unsigned start = baseIndex; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 672 | unsigned end = start; |
| 673 | while (mi != MBB->end()) { |
| 674 | if (lv_->KillsRegister(mi, interval.reg)) { |
| 675 | DOUT << " killed"; |
| 676 | end = getUseIndex(baseIndex) + 1; |
| 677 | goto exit; |
| 678 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 679 | // Another instruction redefines the register before it is ever read. |
| 680 | // Then the register is essentially dead at the instruction that defines |
| 681 | // it. Hence its interval is: |
| 682 | // [defSlot(def), defSlot(def)+1) |
| 683 | DOUT << " dead"; |
| 684 | end = getDefIndex(start) + 1; |
| 685 | goto exit; |
| 686 | } |
| 687 | |
| 688 | baseIndex += InstrSlots::NUM; |
| 689 | ++mi; |
| 690 | } |
| 691 | |
| 692 | exit: |
| 693 | assert(start < end && "did not find end of interval?"); |
| 694 | |
| 695 | LiveRange LR(start, end, interval.getNextValue(~0U, 0)); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 696 | DOUT << " +" << LR << '\n'; |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 697 | interval.addRange(LR); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 700 | /// computeIntervals - computes the live intervals for virtual |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 701 | /// registers. for some ordering of the machine instructions [1,N] a |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 702 | /// 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] | 703 | /// which a variable is live |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 704 | void LiveIntervals::computeIntervals() { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 705 | DOUT << "********** COMPUTING LIVE INTERVALS **********\n" |
| 706 | << "********** Function: " |
| 707 | << ((Value*)mf_->getFunction())->getName() << '\n'; |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 708 | // Track the index of the current machine instr. |
| 709 | unsigned MIIndex = 0; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 710 | for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end(); |
| 711 | MBBI != E; ++MBBI) { |
| 712 | MachineBasicBlock *MBB = MBBI; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 713 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 715 | MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end(); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 716 | |
| 717 | if (MBB->livein_begin() != MBB->livein_end()) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 718 | // Create intervals for live-ins to this BB first. |
| 719 | for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(), |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 720 | LE = MBB->livein_end(); LI != LE; ++LI) { |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 721 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI)); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 722 | for (const unsigned* AS = mri_->getAliasSet(*LI); *AS; ++AS) |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 723 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS)); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 724 | } |
Chris Lattner | dffb2e8 | 2006-09-04 18:27:40 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 727 | for (; MI != miEnd; ++MI) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 728 | DOUT << MIIndex << "\t" << *MI; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 729 | |
Evan Cheng | 438f7bc | 2006-11-10 08:43:01 +0000 | [diff] [blame] | 730 | // Handle defs. |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 731 | for (int i = MI->getNumOperands() - 1; i >= 0; --i) { |
| 732 | MachineOperand &MO = MI->getOperand(i); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 733 | // handle register defs - build intervals |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 734 | if (MO.isRegister() && MO.getReg() && MO.isDef()) |
| 735 | handleRegisterDef(MBB, MI, MIIndex, MO.getReg()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 736 | } |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 737 | |
| 738 | MIIndex += InstrSlots::NUM; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 739 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 740 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 741 | } |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 742 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 743 | /// AdjustCopiesBackFrom - We found a non-trivially-coallescable copy with IntA |
| 744 | /// being the source and IntB being the dest, thus this defines a value number |
| 745 | /// in IntB. If the source value number (in IntA) is defined by a copy from B, |
| 746 | /// see if we can merge these two pieces of B into a single value number, |
| 747 | /// eliminating a copy. For example: |
| 748 | /// |
| 749 | /// A3 = B0 |
| 750 | /// ... |
| 751 | /// B1 = A3 <- this copy |
| 752 | /// |
| 753 | /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1 |
| 754 | /// value number to be replaced with B0 (which simplifies the B liveinterval). |
| 755 | /// |
| 756 | /// This returns true if an interval was modified. |
| 757 | /// |
| 758 | bool LiveIntervals::AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB, |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 759 | MachineInstr *CopyMI) { |
| 760 | unsigned CopyIdx = getDefIndex(getInstructionIndex(CopyMI)); |
| 761 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 762 | // BValNo is a value number in B that is defined by a copy from A. 'B3' in |
| 763 | // the example above. |
| 764 | LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx); |
| 765 | unsigned BValNo = BLR->ValId; |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 766 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 767 | // Get the location that B is defined at. Two options: either this value has |
| 768 | // an unknown definition point or it is defined at CopyIdx. If unknown, we |
| 769 | // can't process it. |
| 770 | unsigned BValNoDefIdx = IntB.getInstForValNum(BValNo); |
| 771 | if (BValNoDefIdx == ~0U) return false; |
| 772 | assert(BValNoDefIdx == CopyIdx && |
| 773 | "Copy doesn't define the value?"); |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 774 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 775 | // AValNo is the value number in A that defines the copy, A0 in the example. |
| 776 | LiveInterval::iterator AValLR = IntA.FindLiveRangeContaining(CopyIdx-1); |
| 777 | unsigned AValNo = AValLR->ValId; |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 778 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 779 | // If AValNo is defined as a copy from IntB, we can potentially process this. |
| 780 | |
| 781 | // Get the instruction that defines this value number. |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 782 | unsigned SrcReg = IntA.getSrcRegForValNum(AValNo); |
| 783 | if (!SrcReg) return false; // Not defined by a copy. |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 784 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 785 | // If the value number is not defined by a copy instruction, ignore it. |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 786 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 787 | // If the source register comes from an interval other than IntB, we can't |
| 788 | // handle this. |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 789 | if (rep(SrcReg) != IntB.reg) return false; |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 790 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 791 | // Get the LiveRange in IntB that this value number starts with. |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 792 | unsigned AValNoInstIdx = IntA.getInstForValNum(AValNo); |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 793 | LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNoInstIdx-1); |
| 794 | |
| 795 | // Make sure that the end of the live range is inside the same block as |
| 796 | // CopyMI. |
| 797 | MachineInstr *ValLREndInst = getInstructionFromIndex(ValLR->end-1); |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 798 | if (!ValLREndInst || |
| 799 | ValLREndInst->getParent() != CopyMI->getParent()) return false; |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 800 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 801 | // Okay, we now know that ValLR ends in the same block that the CopyMI |
| 802 | // live-range starts. If there are no intervening live ranges between them in |
| 803 | // IntB, we can merge them. |
| 804 | if (ValLR+1 != BLR) return false; |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 805 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 806 | DOUT << "\nExtending: "; IntB.print(DOUT, mri_); |
Chris Lattner | ba25603 | 2006-08-30 23:02:29 +0000 | [diff] [blame] | 807 | |
| 808 | // We are about to delete CopyMI, so need to remove it as the 'instruction |
| 809 | // that defines this value #'. |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 810 | IntB.setValueNumberInfo(BValNo, std::make_pair(~0U, 0)); |
Chris Lattner | ba25603 | 2006-08-30 23:02:29 +0000 | [diff] [blame] | 811 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 812 | // Okay, we can merge them. We need to insert a new liverange: |
| 813 | // [ValLR.end, BLR.begin) of either value number, then we merge the |
| 814 | // two value numbers. |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 815 | unsigned FillerStart = ValLR->end, FillerEnd = BLR->start; |
| 816 | IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo)); |
| 817 | |
| 818 | // If the IntB live range is assigned to a physical register, and if that |
| 819 | // physreg has aliases, |
| 820 | if (MRegisterInfo::isPhysicalRegister(IntB.reg)) { |
| 821 | for (const unsigned *AS = mri_->getAliasSet(IntB.reg); *AS; ++AS) { |
| 822 | LiveInterval &AliasLI = getInterval(*AS); |
| 823 | AliasLI.addRange(LiveRange(FillerStart, FillerEnd, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 824 | AliasLI.getNextValue(~0U, 0))); |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 827 | |
| 828 | // Okay, merge "B1" into the same value number as "B0". |
| 829 | if (BValNo != ValLR->ValId) |
| 830 | IntB.MergeValueNumberInto(BValNo, ValLR->ValId); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 831 | DOUT << " result = "; IntB.print(DOUT, mri_); |
| 832 | DOUT << "\n"; |
Evan Cheng | 16191f0 | 2007-02-25 09:41:59 +0000 | [diff] [blame] | 833 | |
| 834 | // If the source instruction was killing the source register before the |
| 835 | // merge, unset the isKill marker given the live range has been extended. |
| 836 | MachineOperand *MOK = ValLREndInst->findRegisterUseOperand(IntB.reg, true); |
| 837 | if (MOK) |
| 838 | MOK->unsetIsKill(); |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 839 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 840 | // Finally, delete the copy instruction. |
| 841 | RemoveMachineInstrFromMaps(CopyMI); |
| 842 | CopyMI->eraseFromParent(); |
| 843 | ++numPeep; |
Chris Lattner | aa51a48 | 2005-10-21 06:49:50 +0000 | [diff] [blame] | 844 | return true; |
| 845 | } |
| 846 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 847 | /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, |
| 848 | /// which are the src/dst of the copy instruction CopyMI. This returns true |
| 849 | /// if the copy was successfully coallesced away, or if it is never possible |
| 850 | /// to coallesce these this copy, due to register constraints. It returns |
| 851 | /// false if it is not currently possible to coallesce this interval, but |
| 852 | /// it may be possible if other things get coallesced. |
| 853 | bool LiveIntervals::JoinCopy(MachineInstr *CopyMI, |
| 854 | unsigned SrcReg, unsigned DstReg) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 855 | DOUT << getInstructionIndex(CopyMI) << '\t' << *CopyMI; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 856 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 857 | // Get representative registers. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 858 | unsigned repSrcReg = rep(SrcReg); |
| 859 | unsigned repDstReg = rep(DstReg); |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 860 | |
| 861 | // If they are already joined we continue. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 862 | if (repSrcReg == repDstReg) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 863 | DOUT << "\tCopy already coallesced.\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 864 | return true; // Not coallescable. |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 865 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 866 | |
| 867 | // If they are both physical registers, we cannot join them. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 868 | if (MRegisterInfo::isPhysicalRegister(repSrcReg) && |
| 869 | MRegisterInfo::isPhysicalRegister(repDstReg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 870 | DOUT << "\tCan not coallesce physregs.\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 871 | return true; // Not coallescable. |
| 872 | } |
| 873 | |
| 874 | // We only join virtual registers with allocatable physical registers. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 875 | if (MRegisterInfo::isPhysicalRegister(repSrcReg) && |
| 876 | !allocatableRegs_[repSrcReg]) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 877 | DOUT << "\tSrc reg is unallocatable physreg.\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 878 | return true; // Not coallescable. |
| 879 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 880 | if (MRegisterInfo::isPhysicalRegister(repDstReg) && |
| 881 | !allocatableRegs_[repDstReg]) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 882 | DOUT << "\tDst reg is unallocatable physreg.\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 883 | return true; // Not coallescable. |
| 884 | } |
| 885 | |
| 886 | // If they are not of the same register class, we cannot join them. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 887 | if (differingRegisterClasses(repSrcReg, repDstReg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 888 | DOUT << "\tSrc/Dest are different register classes.\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 889 | return true; // Not coallescable. |
| 890 | } |
| 891 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 892 | LiveInterval &SrcInt = getInterval(repSrcReg); |
| 893 | LiveInterval &DestInt = getInterval(repDstReg); |
| 894 | assert(SrcInt.reg == repSrcReg && DestInt.reg == repDstReg && |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 895 | "Register mapping is horribly broken!"); |
| 896 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 897 | DOUT << "\t\tInspecting "; SrcInt.print(DOUT, mri_); |
| 898 | DOUT << " and "; DestInt.print(DOUT, mri_); |
| 899 | DOUT << ": "; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 900 | |
| 901 | // Check if it is necessary to propagate "isDead" property before intervals |
| 902 | // are joined. |
| 903 | MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg); |
| 904 | bool isDead = mopd->isDead(); |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 905 | bool isShorten = false; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 906 | unsigned SrcStart = 0; |
| 907 | unsigned SrcEnd = 0; |
| 908 | if (isDead) { |
Evan Cheng | 48ef398 | 2007-02-25 09:46:31 +0000 | [diff] [blame] | 909 | unsigned CopyIdx = getInstructionIndex(CopyMI); |
| 910 | LiveInterval::iterator SrcLR = |
| 911 | SrcInt.FindLiveRangeContaining(getUseIndex(CopyIdx)); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 912 | SrcStart = SrcLR->start; |
| 913 | SrcEnd = SrcLR->end; |
Evan Cheng | 48ef398 | 2007-02-25 09:46:31 +0000 | [diff] [blame] | 914 | // The instruction which defines the src is only truly dead if there are |
| 915 | // no intermediate uses and there isn't a use beyond the copy. |
| 916 | // FIXME: find the last use, mark is kill and shorten the live range. |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 917 | if (SrcEnd > getDefIndex(CopyIdx)) |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 918 | isDead = false; |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 919 | else { |
| 920 | MachineOperand *MOU; |
| 921 | MachineInstr *LastUse = |
| 922 | lastRegisterUse(repSrcReg, SrcStart, CopyIdx, MOU); |
| 923 | if (LastUse) { |
| 924 | // Shorten the liveinterval to the end of last use. |
| 925 | MOU->setIsKill(); |
| 926 | isDead = false; |
| 927 | isShorten = true; |
| 928 | SrcEnd = getUseIndex(getInstructionIndex(LastUse)); |
| 929 | } |
| 930 | } |
| 931 | if (isDead) |
| 932 | isShorten = true; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 935 | // We need to be careful about coalescing a source physical register with a |
| 936 | // virtual register. Once the coalescing is done, it cannot be broken and |
| 937 | // these are not spillable! If the destination interval uses are far away, |
| 938 | // think twice about coalescing them! |
Evan Cheng | 757072d | 2007-03-19 18:08:26 +0000 | [diff] [blame^] | 939 | if (!mopd->isDead() && MRegisterInfo::isPhysicalRegister(repSrcReg)) { |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 940 | // Small function. No need to worry! |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 941 | unsigned Threshold = allocatableRegs_.count() * 2; |
| 942 | if (r2iMap_.size() <= Threshold) |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 943 | goto TryJoin; |
| 944 | |
| 945 | LiveVariables::VarInfo& dvi = lv_->getVarInfo(repDstReg); |
| 946 | // Is the value used in the current BB or any immediate successroe BB? |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 947 | MachineBasicBlock *CopyBB = CopyMI->getParent(); |
| 948 | if (dvi.UsedBlocks[CopyBB->getNumber()]) |
| 949 | goto TryJoin; |
| 950 | for (MachineBasicBlock::succ_iterator SI = CopyBB->succ_begin(), |
| 951 | SE = CopyBB->succ_end(); SI != SE; ++SI) { |
| 952 | MachineBasicBlock *SuccMBB = *SI; |
| 953 | if (dvi.UsedBlocks[SuccMBB->getNumber()]) |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 954 | goto TryJoin; |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | // Ok, no use in this BB and no use in immediate successor BB's. Be really |
| 958 | // careful now! |
| 959 | // It's only used in one BB, forget about it! |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 960 | if (dvi.UsedBlocks.count() < 2) { |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 961 | ++numAborts; |
| 962 | return false; |
| 963 | } |
| 964 | |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 965 | // Determine whether to allow coalescing based on how far the closest |
| 966 | // use is. |
| 967 | unsigned CopyIdx = getInstructionIndex(CopyMI); |
| 968 | unsigned MinDist = i2miMap_.size() * InstrSlots::NUM; |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 969 | int UseBBNum = dvi.UsedBlocks.find_first(); |
| 970 | while (UseBBNum != -1) { |
| 971 | MachineBasicBlock *UseBB = mf_->getBlockNumbered(UseBBNum); |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 972 | unsigned UseIdx = getMBBStartIdx(UseBB); |
| 973 | if (UseIdx > CopyIdx) { |
| 974 | MinDist = std::min(MinDist, UseIdx - CopyIdx); |
| 975 | if (MinDist <= Threshold) |
| 976 | break; |
| 977 | } |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 978 | UseBBNum = dvi.UsedBlocks.find_next(UseBBNum); |
| 979 | } |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 980 | if (MinDist > Threshold) { |
| 981 | // Don't do it! |
| 982 | ++numAborts; |
| 983 | return false; |
| 984 | } |
Evan Cheng | ba1a3df | 2007-03-17 09:27:35 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | TryJoin: |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 988 | // Okay, attempt to join these two intervals. On failure, this returns false. |
| 989 | // Otherwise, if one of the intervals being joined is a physreg, this method |
| 990 | // always canonicalizes DestInt to be it. The output "SrcInt" will not have |
| 991 | // been modified, so we can use this information below to update aliases. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 992 | if (JoinIntervals(DestInt, SrcInt)) { |
| 993 | if (isDead) { |
| 994 | // Result of the copy is dead. Propagate this property. |
Evan Cheng | a16d442 | 2007-03-03 02:18:00 +0000 | [diff] [blame] | 995 | if (SrcStart == 0) { |
| 996 | assert(MRegisterInfo::isPhysicalRegister(repSrcReg) && |
| 997 | "Live-in must be a physical register!"); |
| 998 | // Live-in to the function but dead. Remove it from entry live-in set. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 999 | // JoinIntervals may end up swapping the two intervals. |
Evan Cheng | a16d442 | 2007-03-03 02:18:00 +0000 | [diff] [blame] | 1000 | mf_->begin()->removeLiveIn(repSrcReg); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1001 | } else { |
| 1002 | MachineInstr *SrcMI = getInstructionFromIndex(SrcStart); |
| 1003 | if (SrcMI) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1004 | MachineOperand *mops = SrcMI->findRegisterDefOperand(SrcReg); |
| 1005 | if (mops) |
| 1006 | // FIXME: mops == NULL means SrcMI defines a subregister? |
| 1007 | mops->setIsDead(); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (isShorten) { |
| 1013 | // Shorten the live interval. |
| 1014 | LiveInterval &LiveInInt = (repSrcReg == DestInt.reg) ? DestInt : SrcInt; |
| 1015 | LiveInInt.removeRange(SrcStart, SrcEnd); |
| 1016 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1017 | } else { |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1018 | // Coallescing failed. |
| 1019 | |
| 1020 | // If we can eliminate the copy without merging the live ranges, do so now. |
| 1021 | if (AdjustCopiesBackFrom(SrcInt, DestInt, CopyMI)) |
| 1022 | return true; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1024 | // Otherwise, we are unable to join the intervals. |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 1025 | DOUT << "Interference!\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
| 1028 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1029 | bool Swapped = repSrcReg == DestInt.reg; |
Chris Lattner | e7f729b | 2006-08-26 01:28:16 +0000 | [diff] [blame] | 1030 | if (Swapped) |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1031 | std::swap(repSrcReg, repDstReg); |
| 1032 | assert(MRegisterInfo::isVirtualRegister(repSrcReg) && |
Chris Lattner | e7f729b | 2006-08-26 01:28:16 +0000 | [diff] [blame] | 1033 | "LiveInterval::join didn't work right!"); |
| 1034 | |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 1035 | // If we're about to merge live ranges into a physical register live range, |
| 1036 | // we have to update any aliased register's live ranges to indicate that they |
| 1037 | // have clobbered values for this range. |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1038 | if (MRegisterInfo::isPhysicalRegister(repDstReg)) { |
| 1039 | for (const unsigned *AS = mri_->getAliasSet(repDstReg); *AS; ++AS) |
Chris Lattner | e7f729b | 2006-08-26 01:28:16 +0000 | [diff] [blame] | 1040 | getInterval(*AS).MergeInClobberRanges(SrcInt); |
Evan Cheng | cf596c5 | 2007-03-18 09:05:55 +0000 | [diff] [blame] | 1041 | } else { |
| 1042 | // Merge UsedBlocks info if the destination is a virtual register. |
| 1043 | LiveVariables::VarInfo& dVI = lv_->getVarInfo(repDstReg); |
| 1044 | LiveVariables::VarInfo& sVI = lv_->getVarInfo(repSrcReg); |
| 1045 | dVI.UsedBlocks |= sVI.UsedBlocks; |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 1048 | DOUT << "\n\t\tJoined. Result = "; DestInt.print(DOUT, mri_); |
| 1049 | DOUT << "\n"; |
Evan Cheng | 30cac02 | 2007-02-22 23:03:39 +0000 | [diff] [blame] | 1050 | |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 1051 | // Remember these liveintervals have been joined. |
| 1052 | JoinedLIs.set(repSrcReg - MRegisterInfo::FirstVirtualRegister); |
| 1053 | if (MRegisterInfo::isVirtualRegister(repDstReg)) |
| 1054 | JoinedLIs.set(repDstReg - MRegisterInfo::FirstVirtualRegister); |
Evan Cheng | 30cac02 | 2007-02-22 23:03:39 +0000 | [diff] [blame] | 1055 | |
Evan Cheng | da2295e | 2007-02-23 20:40:13 +0000 | [diff] [blame] | 1056 | // If the intervals were swapped by Join, swap them back so that the register |
| 1057 | // mapping (in the r2i map) is correct. |
| 1058 | if (Swapped) SrcInt.swap(DestInt); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1059 | removeInterval(repSrcReg); |
| 1060 | r2rMap_[repSrcReg] = repDstReg; |
Chris Lattner | e7f729b | 2006-08-26 01:28:16 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | bfe180a | 2006-08-31 05:58:59 +0000 | [diff] [blame] | 1062 | // Finally, delete the copy instruction. |
| 1063 | RemoveMachineInstrFromMaps(CopyMI); |
| 1064 | CopyMI->eraseFromParent(); |
| 1065 | ++numPeep; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1066 | ++numJoins; |
| 1067 | return true; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1070 | /// ComputeUltimateVN - Assuming we are going to join two live intervals, |
| 1071 | /// compute what the resultant value numbers for each value in the input two |
| 1072 | /// ranges will be. This is complicated by copies between the two which can |
| 1073 | /// and will commonly cause multiple value numbers to be merged into one. |
| 1074 | /// |
| 1075 | /// VN is the value number that we're trying to resolve. InstDefiningValue |
| 1076 | /// keeps track of the new InstDefiningValue assignment for the result |
| 1077 | /// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of |
| 1078 | /// whether a value in this or other is a copy from the opposite set. |
| 1079 | /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have |
| 1080 | /// already been assigned. |
| 1081 | /// |
| 1082 | /// ThisFromOther[x] - If x is defined as a copy from the other interval, this |
| 1083 | /// contains the value number the copy is from. |
| 1084 | /// |
| 1085 | static unsigned ComputeUltimateVN(unsigned VN, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 1086 | SmallVector<std::pair<unsigned, |
| 1087 | unsigned>, 16> &ValueNumberInfo, |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1088 | SmallVector<int, 16> &ThisFromOther, |
| 1089 | SmallVector<int, 16> &OtherFromThis, |
| 1090 | SmallVector<int, 16> &ThisValNoAssignments, |
| 1091 | SmallVector<int, 16> &OtherValNoAssignments, |
| 1092 | LiveInterval &ThisLI, LiveInterval &OtherLI) { |
| 1093 | // If the VN has already been computed, just return it. |
| 1094 | if (ThisValNoAssignments[VN] >= 0) |
| 1095 | return ThisValNoAssignments[VN]; |
Chris Lattner | 8a67f6e | 2006-09-01 07:00:23 +0000 | [diff] [blame] | 1096 | // assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?"); |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1097 | |
| 1098 | // If this val is not a copy from the other val, then it must be a new value |
| 1099 | // number in the destination. |
| 1100 | int OtherValNo = ThisFromOther[VN]; |
| 1101 | if (OtherValNo == -1) { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 1102 | ValueNumberInfo.push_back(ThisLI.getValNumInfo(VN)); |
| 1103 | return ThisValNoAssignments[VN] = ValueNumberInfo.size()-1; |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Chris Lattner | 8a67f6e | 2006-09-01 07:00:23 +0000 | [diff] [blame] | 1106 | // Otherwise, this *is* a copy from the RHS. If the other side has already |
| 1107 | // been computed, return it. |
| 1108 | if (OtherValNoAssignments[OtherValNo] >= 0) |
| 1109 | return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo]; |
| 1110 | |
| 1111 | // Mark this value number as currently being computed, then ask what the |
| 1112 | // ultimate value # of the other value is. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1113 | ThisValNoAssignments[VN] = -2; |
| 1114 | unsigned UltimateVN = |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 1115 | ComputeUltimateVN(OtherValNo, ValueNumberInfo, |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1116 | OtherFromThis, ThisFromOther, |
| 1117 | OtherValNoAssignments, ThisValNoAssignments, |
| 1118 | OtherLI, ThisLI); |
| 1119 | return ThisValNoAssignments[VN] = UltimateVN; |
| 1120 | } |
| 1121 | |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 1122 | static bool InVector(unsigned Val, const SmallVector<unsigned, 8> &V) { |
| 1123 | return std::find(V.begin(), V.end(), Val) != V.end(); |
| 1124 | } |
| 1125 | |
| 1126 | /// SimpleJoin - Attempt to joint the specified interval into this one. The |
| 1127 | /// caller of this method must guarantee that the RHS only contains a single |
| 1128 | /// value number and that the RHS is not defined by a copy from this |
| 1129 | /// interval. This returns false if the intervals are not joinable, or it |
| 1130 | /// joins them and returns true. |
| 1131 | bool LiveIntervals::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS) { |
| 1132 | assert(RHS.containsOneValue()); |
| 1133 | |
| 1134 | // Some number (potentially more than one) value numbers in the current |
| 1135 | // interval may be defined as copies from the RHS. Scan the overlapping |
| 1136 | // portions of the LHS and RHS, keeping track of this and looking for |
| 1137 | // overlapping live ranges that are NOT defined as copies. If these exist, we |
| 1138 | // cannot coallesce. |
| 1139 | |
| 1140 | LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end(); |
| 1141 | LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end(); |
| 1142 | |
| 1143 | if (LHSIt->start < RHSIt->start) { |
| 1144 | LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start); |
| 1145 | if (LHSIt != LHS.begin()) --LHSIt; |
| 1146 | } else if (RHSIt->start < LHSIt->start) { |
| 1147 | RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start); |
| 1148 | if (RHSIt != RHS.begin()) --RHSIt; |
| 1149 | } |
| 1150 | |
| 1151 | SmallVector<unsigned, 8> EliminatedLHSVals; |
| 1152 | |
| 1153 | while (1) { |
| 1154 | // Determine if these live intervals overlap. |
| 1155 | bool Overlaps = false; |
| 1156 | if (LHSIt->start <= RHSIt->start) |
| 1157 | Overlaps = LHSIt->end > RHSIt->start; |
| 1158 | else |
| 1159 | Overlaps = RHSIt->end > LHSIt->start; |
| 1160 | |
| 1161 | // If the live intervals overlap, there are two interesting cases: if the |
| 1162 | // LHS interval is defined by a copy from the RHS, it's ok and we record |
| 1163 | // that the LHS value # is the same as the RHS. If it's not, then we cannot |
| 1164 | // coallesce these live ranges and we bail out. |
| 1165 | if (Overlaps) { |
| 1166 | // If we haven't already recorded that this value # is safe, check it. |
| 1167 | if (!InVector(LHSIt->ValId, EliminatedLHSVals)) { |
| 1168 | // Copy from the RHS? |
| 1169 | unsigned SrcReg = LHS.getSrcRegForValNum(LHSIt->ValId); |
| 1170 | if (rep(SrcReg) != RHS.reg) |
| 1171 | return false; // Nope, bail out. |
| 1172 | |
| 1173 | EliminatedLHSVals.push_back(LHSIt->ValId); |
| 1174 | } |
| 1175 | |
| 1176 | // We know this entire LHS live range is okay, so skip it now. |
| 1177 | if (++LHSIt == LHSEnd) break; |
| 1178 | continue; |
| 1179 | } |
| 1180 | |
| 1181 | if (LHSIt->end < RHSIt->end) { |
| 1182 | if (++LHSIt == LHSEnd) break; |
| 1183 | } else { |
| 1184 | // One interesting case to check here. It's possible that we have |
| 1185 | // something like "X3 = Y" which defines a new value number in the LHS, |
| 1186 | // and is the last use of this liverange of the RHS. In this case, we |
| 1187 | // want to notice this copy (so that it gets coallesced away) even though |
| 1188 | // the live ranges don't actually overlap. |
| 1189 | if (LHSIt->start == RHSIt->end) { |
| 1190 | if (InVector(LHSIt->ValId, EliminatedLHSVals)) { |
| 1191 | // We already know that this value number is going to be merged in |
| 1192 | // if coallescing succeeds. Just skip the liverange. |
| 1193 | if (++LHSIt == LHSEnd) break; |
| 1194 | } else { |
| 1195 | // Otherwise, if this is a copy from the RHS, mark it as being merged |
| 1196 | // in. |
| 1197 | if (rep(LHS.getSrcRegForValNum(LHSIt->ValId)) == RHS.reg) { |
| 1198 | EliminatedLHSVals.push_back(LHSIt->ValId); |
| 1199 | |
| 1200 | // We know this entire LHS live range is okay, so skip it now. |
| 1201 | if (++LHSIt == LHSEnd) break; |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (++RHSIt == RHSEnd) break; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | // If we got here, we know that the coallescing will be successful and that |
| 1211 | // the value numbers in EliminatedLHSVals will all be merged together. Since |
| 1212 | // the most common case is that EliminatedLHSVals has a single number, we |
| 1213 | // optimize for it: if there is more than one value, we merge them all into |
| 1214 | // the lowest numbered one, then handle the interval as if we were merging |
| 1215 | // with one value number. |
| 1216 | unsigned LHSValNo; |
| 1217 | if (EliminatedLHSVals.size() > 1) { |
| 1218 | // Loop through all the equal value numbers merging them into the smallest |
| 1219 | // one. |
| 1220 | unsigned Smallest = EliminatedLHSVals[0]; |
| 1221 | for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) { |
| 1222 | if (EliminatedLHSVals[i] < Smallest) { |
| 1223 | // Merge the current notion of the smallest into the smaller one. |
| 1224 | LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]); |
| 1225 | Smallest = EliminatedLHSVals[i]; |
| 1226 | } else { |
| 1227 | // Merge into the smallest. |
| 1228 | LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest); |
| 1229 | } |
| 1230 | } |
| 1231 | LHSValNo = Smallest; |
| 1232 | } else { |
| 1233 | assert(!EliminatedLHSVals.empty() && "No copies from the RHS?"); |
| 1234 | LHSValNo = EliminatedLHSVals[0]; |
| 1235 | } |
| 1236 | |
| 1237 | // Okay, now that there is a single LHS value number that we're merging the |
| 1238 | // RHS into, update the value number info for the LHS to indicate that the |
| 1239 | // value number is defined where the RHS value number was. |
| 1240 | LHS.setValueNumberInfo(LHSValNo, RHS.getValNumInfo(0)); |
| 1241 | |
| 1242 | // Okay, the final step is to loop over the RHS live intervals, adding them to |
| 1243 | // the LHS. |
| 1244 | LHS.MergeRangesInAsValue(RHS, LHSValNo); |
| 1245 | LHS.weight += RHS.weight; |
| 1246 | |
| 1247 | return true; |
| 1248 | } |
| 1249 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1250 | /// JoinIntervals - Attempt to join these two intervals. On failure, this |
| 1251 | /// returns false. Otherwise, if one of the intervals being joined is a |
| 1252 | /// physreg, this method always canonicalizes LHS to be it. The output |
| 1253 | /// "RHS" will not have been modified, so we can use this information |
| 1254 | /// below to update aliases. |
| 1255 | bool LiveIntervals::JoinIntervals(LiveInterval &LHS, LiveInterval &RHS) { |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1256 | // Compute the final value assignment, assuming that the live ranges can be |
| 1257 | // coallesced. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1258 | SmallVector<int, 16> LHSValNoAssignments; |
| 1259 | SmallVector<int, 16> RHSValNoAssignments; |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 1260 | SmallVector<std::pair<unsigned,unsigned>, 16> ValueNumberInfo; |
Chris Lattner | 238416c | 2006-09-01 06:10:18 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1262 | // Compute ultimate value numbers for the LHS and RHS values. |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1263 | if (RHS.containsOneValue()) { |
| 1264 | // Copies from a liveinterval with a single value are simple to handle and |
| 1265 | // very common, handle the special case here. This is important, because |
| 1266 | // often RHS is small and LHS is large (e.g. a physreg). |
| 1267 | |
| 1268 | // Find out if the RHS is defined as a copy from some value in the LHS. |
| 1269 | int RHSValID = -1; |
| 1270 | std::pair<unsigned,unsigned> RHSValNoInfo; |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 1271 | unsigned RHSSrcReg = RHS.getSrcRegForValNum(0); |
| 1272 | if ((RHSSrcReg == 0 || rep(RHSSrcReg) != LHS.reg)) { |
| 1273 | // If RHS is not defined as a copy from the LHS, we can use simpler and |
| 1274 | // faster checks to see if the live ranges are coallescable. This joiner |
| 1275 | // can't swap the LHS/RHS intervals though. |
| 1276 | if (!MRegisterInfo::isPhysicalRegister(RHS.reg)) { |
| 1277 | return SimpleJoin(LHS, RHS); |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1278 | } else { |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 1279 | RHSValNoInfo = RHS.getValNumInfo(0); |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1280 | } |
| 1281 | } else { |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 1282 | // It was defined as a copy from the LHS, find out what value # it is. |
| 1283 | unsigned ValInst = RHS.getInstForValNum(0); |
| 1284 | RHSValID = LHS.getLiveRangeContaining(ValInst-1)->ValId; |
| 1285 | RHSValNoInfo = LHS.getValNumInfo(RHSValID); |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 1288 | LHSValNoAssignments.resize(LHS.getNumValNums(), -1); |
| 1289 | RHSValNoAssignments.resize(RHS.getNumValNums(), -1); |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1290 | ValueNumberInfo.resize(LHS.getNumValNums()); |
| 1291 | |
| 1292 | // Okay, *all* of the values in LHS that are defined as a copy from RHS |
| 1293 | // should now get updated. |
| 1294 | for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) { |
| 1295 | if (unsigned LHSSrcReg = LHS.getSrcRegForValNum(VN)) { |
| 1296 | if (rep(LHSSrcReg) != RHS.reg) { |
| 1297 | // If this is not a copy from the RHS, its value number will be |
| 1298 | // unmodified by the coallescing. |
| 1299 | ValueNumberInfo[VN] = LHS.getValNumInfo(VN); |
| 1300 | LHSValNoAssignments[VN] = VN; |
| 1301 | } else if (RHSValID == -1) { |
| 1302 | // Otherwise, it is a copy from the RHS, and we don't already have a |
| 1303 | // value# for it. Keep the current value number, but remember it. |
| 1304 | LHSValNoAssignments[VN] = RHSValID = VN; |
| 1305 | ValueNumberInfo[VN] = RHSValNoInfo; |
| 1306 | } else { |
| 1307 | // Otherwise, use the specified value #. |
| 1308 | LHSValNoAssignments[VN] = RHSValID; |
| 1309 | if (VN != (unsigned)RHSValID) |
| 1310 | ValueNumberInfo[VN].first = ~1U; |
| 1311 | else |
| 1312 | ValueNumberInfo[VN] = RHSValNoInfo; |
| 1313 | } |
| 1314 | } else { |
| 1315 | ValueNumberInfo[VN] = LHS.getValNumInfo(VN); |
| 1316 | LHSValNoAssignments[VN] = VN; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | assert(RHSValID != -1 && "Didn't find value #?"); |
| 1321 | RHSValNoAssignments[0] = RHSValID; |
| 1322 | |
| 1323 | } else { |
Chris Lattner | 238416c | 2006-09-01 06:10:18 +0000 | [diff] [blame] | 1324 | // Loop over the value numbers of the LHS, seeing if any are defined from |
| 1325 | // the RHS. |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1326 | SmallVector<int, 16> LHSValsDefinedFromRHS; |
| 1327 | LHSValsDefinedFromRHS.resize(LHS.getNumValNums(), -1); |
| 1328 | for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) { |
| 1329 | unsigned ValSrcReg = LHS.getSrcRegForValNum(VN); |
| 1330 | if (ValSrcReg == 0) // Src not defined by a copy? |
| 1331 | continue; |
| 1332 | |
Chris Lattner | 238416c | 2006-09-01 06:10:18 +0000 | [diff] [blame] | 1333 | // DstReg is known to be a register in the LHS interval. If the src is |
| 1334 | // from the RHS interval, we can use its value #. |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1335 | if (rep(ValSrcReg) != RHS.reg) |
| 1336 | continue; |
| 1337 | |
| 1338 | // Figure out the value # from the RHS. |
| 1339 | unsigned ValInst = LHS.getInstForValNum(VN); |
| 1340 | LHSValsDefinedFromRHS[VN] = RHS.getLiveRangeContaining(ValInst-1)->ValId; |
| 1341 | } |
| 1342 | |
Chris Lattner | 238416c | 2006-09-01 06:10:18 +0000 | [diff] [blame] | 1343 | // Loop over the value numbers of the RHS, seeing if any are defined from |
| 1344 | // the LHS. |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1345 | SmallVector<int, 16> RHSValsDefinedFromLHS; |
| 1346 | RHSValsDefinedFromLHS.resize(RHS.getNumValNums(), -1); |
| 1347 | for (unsigned VN = 0, e = RHS.getNumValNums(); VN != e; ++VN) { |
| 1348 | unsigned ValSrcReg = RHS.getSrcRegForValNum(VN); |
| 1349 | if (ValSrcReg == 0) // Src not defined by a copy? |
| 1350 | continue; |
| 1351 | |
Chris Lattner | 238416c | 2006-09-01 06:10:18 +0000 | [diff] [blame] | 1352 | // DstReg is known to be a register in the RHS interval. If the src is |
| 1353 | // from the LHS interval, we can use its value #. |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1354 | if (rep(ValSrcReg) != LHS.reg) |
| 1355 | continue; |
| 1356 | |
| 1357 | // Figure out the value # from the LHS. |
| 1358 | unsigned ValInst = RHS.getInstForValNum(VN); |
| 1359 | RHSValsDefinedFromLHS[VN] = LHS.getLiveRangeContaining(ValInst-1)->ValId; |
| 1360 | } |
| 1361 | |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 1362 | LHSValNoAssignments.resize(LHS.getNumValNums(), -1); |
| 1363 | RHSValNoAssignments.resize(RHS.getNumValNums(), -1); |
| 1364 | ValueNumberInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums()); |
| 1365 | |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1366 | for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) { |
Chris Lattner | 8a67f6e | 2006-09-01 07:00:23 +0000 | [diff] [blame] | 1367 | if (LHSValNoAssignments[VN] >= 0 || LHS.getInstForValNum(VN) == ~2U) |
| 1368 | continue; |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1369 | ComputeUltimateVN(VN, ValueNumberInfo, |
| 1370 | LHSValsDefinedFromRHS, RHSValsDefinedFromLHS, |
| 1371 | LHSValNoAssignments, RHSValNoAssignments, LHS, RHS); |
| 1372 | } |
| 1373 | for (unsigned VN = 0, e = RHS.getNumValNums(); VN != e; ++VN) { |
Chris Lattner | 8a67f6e | 2006-09-01 07:00:23 +0000 | [diff] [blame] | 1374 | if (RHSValNoAssignments[VN] >= 0 || RHS.getInstForValNum(VN) == ~2U) |
| 1375 | continue; |
| 1376 | // If this value number isn't a copy from the LHS, it's a new number. |
| 1377 | if (RHSValsDefinedFromLHS[VN] == -1) { |
| 1378 | ValueNumberInfo.push_back(RHS.getValNumInfo(VN)); |
| 1379 | RHSValNoAssignments[VN] = ValueNumberInfo.size()-1; |
| 1380 | continue; |
| 1381 | } |
| 1382 | |
Chris Lattner | 2ebfa0c | 2006-08-31 06:48:26 +0000 | [diff] [blame] | 1383 | ComputeUltimateVN(VN, ValueNumberInfo, |
| 1384 | RHSValsDefinedFromLHS, LHSValsDefinedFromRHS, |
| 1385 | RHSValNoAssignments, LHSValNoAssignments, RHS, LHS); |
| 1386 | } |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | // Armed with the mappings of LHS/RHS values to ultimate values, walk the |
| 1390 | // interval lists to see if these intervals are coallescable. |
| 1391 | LiveInterval::const_iterator I = LHS.begin(); |
| 1392 | LiveInterval::const_iterator IE = LHS.end(); |
| 1393 | LiveInterval::const_iterator J = RHS.begin(); |
| 1394 | LiveInterval::const_iterator JE = RHS.end(); |
| 1395 | |
| 1396 | // Skip ahead until the first place of potential sharing. |
| 1397 | if (I->start < J->start) { |
| 1398 | I = std::upper_bound(I, IE, J->start); |
| 1399 | if (I != LHS.begin()) --I; |
| 1400 | } else if (J->start < I->start) { |
| 1401 | J = std::upper_bound(J, JE, I->start); |
| 1402 | if (J != RHS.begin()) --J; |
| 1403 | } |
| 1404 | |
| 1405 | while (1) { |
| 1406 | // Determine if these two live ranges overlap. |
| 1407 | bool Overlaps; |
| 1408 | if (I->start < J->start) { |
| 1409 | Overlaps = I->end > J->start; |
| 1410 | } else { |
| 1411 | Overlaps = J->end > I->start; |
| 1412 | } |
| 1413 | |
| 1414 | // If so, check value # info to determine if they are really different. |
| 1415 | if (Overlaps) { |
| 1416 | // If the live range overlap will map to the same value number in the |
| 1417 | // result liverange, we can still coallesce them. If not, we can't. |
| 1418 | if (LHSValNoAssignments[I->ValId] != RHSValNoAssignments[J->ValId]) |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
| 1422 | if (I->end < J->end) { |
| 1423 | ++I; |
| 1424 | if (I == IE) break; |
| 1425 | } else { |
| 1426 | ++J; |
| 1427 | if (J == JE) break; |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | // If we get here, we know that we can coallesce the live ranges. Ask the |
| 1432 | // intervals to coallesce themselves now. |
| 1433 | LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 1434 | ValueNumberInfo); |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 1435 | return true; |
| 1436 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1437 | |
| 1438 | |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1439 | namespace { |
| 1440 | // DepthMBBCompare - Comparison predicate that sort first based on the loop |
| 1441 | // depth of the basic block (the unsigned), and then on the MBB number. |
| 1442 | struct DepthMBBCompare { |
| 1443 | typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair; |
| 1444 | bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const { |
| 1445 | if (LHS.first > RHS.first) return true; // Deeper loops first |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 1446 | return LHS.first == RHS.first && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 1447 | LHS.second->getNumber() < RHS.second->getNumber(); |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1448 | } |
| 1449 | }; |
| 1450 | } |
Chris Lattner | 1c5c044 | 2004-07-19 14:08:10 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | 1acb17c | 2006-09-02 05:32:53 +0000 | [diff] [blame] | 1453 | void LiveIntervals::CopyCoallesceInMBB(MachineBasicBlock *MBB, |
| 1454 | std::vector<CopyRec> &TryAgain) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 1455 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1456 | |
| 1457 | for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); |
| 1458 | MII != E;) { |
| 1459 | MachineInstr *Inst = MII++; |
| 1460 | |
| 1461 | // If this isn't a copy, we can't join intervals. |
| 1462 | unsigned SrcReg, DstReg; |
| 1463 | if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg)) continue; |
| 1464 | |
Chris Lattner | 1acb17c | 2006-09-02 05:32:53 +0000 | [diff] [blame] | 1465 | if (!JoinCopy(Inst, SrcReg, DstReg)) |
| 1466 | TryAgain.push_back(getCopyRec(Inst, SrcReg, DstReg)); |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1471 | void LiveIntervals::joinIntervals() { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 1472 | DOUT << "********** JOINING INTERVALS ***********\n"; |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1473 | |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 1474 | JoinedLIs.resize(getNumIntervals()); |
| 1475 | JoinedLIs.reset(); |
| 1476 | |
Chris Lattner | 1acb17c | 2006-09-02 05:32:53 +0000 | [diff] [blame] | 1477 | std::vector<CopyRec> TryAgainList; |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1478 | const LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 1479 | if (LI.begin() == LI.end()) { |
| 1480 | // If there are no loops in the function, join intervals in function order. |
Chris Lattner | 1c5c044 | 2004-07-19 14:08:10 +0000 | [diff] [blame] | 1481 | for (MachineFunction::iterator I = mf_->begin(), E = mf_->end(); |
| 1482 | I != E; ++I) |
Chris Lattner | 1acb17c | 2006-09-02 05:32:53 +0000 | [diff] [blame] | 1483 | CopyCoallesceInMBB(I, TryAgainList); |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1484 | } else { |
| 1485 | // Otherwise, join intervals in inner loops before other intervals. |
| 1486 | // Unfortunately we can't just iterate over loop hierarchy here because |
| 1487 | // there may be more MBB's than BB's. Collect MBB's for sorting. |
| 1488 | std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs; |
| 1489 | for (MachineFunction::iterator I = mf_->begin(), E = mf_->end(); |
| 1490 | I != E; ++I) |
| 1491 | MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I)); |
| 1492 | |
| 1493 | // Sort by loop depth. |
| 1494 | std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare()); |
| 1495 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 1496 | // Finally, join intervals in loop nest order. |
Chris Lattner | cc0d156 | 2004-07-19 14:40:29 +0000 | [diff] [blame] | 1497 | for (unsigned i = 0, e = MBBs.size(); i != e; ++i) |
Chris Lattner | 1acb17c | 2006-09-02 05:32:53 +0000 | [diff] [blame] | 1498 | CopyCoallesceInMBB(MBBs[i].second, TryAgainList); |
| 1499 | } |
| 1500 | |
| 1501 | // Joining intervals can allow other intervals to be joined. Iteratively join |
| 1502 | // until we make no progress. |
| 1503 | bool ProgressMade = true; |
| 1504 | while (ProgressMade) { |
| 1505 | ProgressMade = false; |
| 1506 | |
| 1507 | for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) { |
| 1508 | CopyRec &TheCopy = TryAgainList[i]; |
| 1509 | if (TheCopy.MI && |
| 1510 | JoinCopy(TheCopy.MI, TheCopy.SrcReg, TheCopy.DstReg)) { |
| 1511 | TheCopy.MI = 0; // Mark this one as done. |
| 1512 | ProgressMade = true; |
| 1513 | } |
| 1514 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1515 | } |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Some live range has been lengthened due to colaescing, eliminate the |
| 1518 | // unnecessary kills. |
| 1519 | int RegNum = JoinedLIs.find_first(); |
| 1520 | while (RegNum != -1) { |
| 1521 | unsigned Reg = RegNum + MRegisterInfo::FirstVirtualRegister; |
| 1522 | unsigned repReg = rep(Reg); |
| 1523 | LiveInterval &LI = getInterval(repReg); |
| 1524 | LiveVariables::VarInfo& svi = lv_->getVarInfo(Reg); |
| 1525 | for (unsigned i = 0, e = svi.Kills.size(); i != e; ++i) { |
| 1526 | MachineInstr *Kill = svi.Kills[i]; |
| 1527 | // Suppose vr1 = op vr2, x |
| 1528 | // and vr1 and vr2 are coalesced. vr2 should still be marked kill |
| 1529 | // unless it is a two-address operand. |
| 1530 | if (isRemoved(Kill) || hasRegisterDef(Kill, repReg)) |
| 1531 | continue; |
| 1532 | if (LI.liveAt(getInstructionIndex(Kill) + InstrSlots::NUM)) |
| 1533 | unsetRegisterKill(Kill, repReg); |
| 1534 | } |
| 1535 | RegNum = JoinedLIs.find_next(RegNum); |
| 1536 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 1537 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 1538 | DOUT << "*** Register mapping ***\n"; |
| 1539 | for (int i = 0, e = r2rMap_.size(); i != e; ++i) |
| 1540 | if (r2rMap_[i]) { |
| 1541 | DOUT << " reg " << i << " -> "; |
| 1542 | DEBUG(printRegName(r2rMap_[i])); |
| 1543 | DOUT << "\n"; |
| 1544 | } |
Chris Lattner | 1c5c044 | 2004-07-19 14:08:10 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Evan Cheng | 647c15e | 2006-05-12 06:06:34 +0000 | [diff] [blame] | 1547 | /// Return true if the two specified registers belong to different register |
| 1548 | /// classes. The registers may be either phys or virt regs. |
| 1549 | bool LiveIntervals::differingRegisterClasses(unsigned RegA, |
| 1550 | unsigned RegB) const { |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 1552 | // Get the register classes for the first reg. |
Chris Lattner | ad3c74f | 2004-10-26 05:29:18 +0000 | [diff] [blame] | 1553 | if (MRegisterInfo::isPhysicalRegister(RegA)) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1554 | assert(MRegisterInfo::isVirtualRegister(RegB) && |
Chris Lattner | ad3c74f | 2004-10-26 05:29:18 +0000 | [diff] [blame] | 1555 | "Shouldn't consider two physregs!"); |
Evan Cheng | 647c15e | 2006-05-12 06:06:34 +0000 | [diff] [blame] | 1556 | return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA); |
Chris Lattner | ad3c74f | 2004-10-26 05:29:18 +0000 | [diff] [blame] | 1557 | } |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 1558 | |
| 1559 | // Compare against the regclass for the second reg. |
Evan Cheng | 647c15e | 2006-05-12 06:06:34 +0000 | [diff] [blame] | 1560 | const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA); |
| 1561 | if (MRegisterInfo::isVirtualRegister(RegB)) |
| 1562 | return RegClass != mf_->getSSARegMap()->getRegClass(RegB); |
| 1563 | else |
| 1564 | return !RegClass->contains(RegB); |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1567 | /// lastRegisterUse - Returns the last use of the specific register between |
| 1568 | /// cycles Start and End. It also returns the use operand by reference. It |
| 1569 | /// returns NULL if there are no uses. |
| 1570 | MachineInstr * |
| 1571 | LiveIntervals::lastRegisterUse(unsigned Reg, unsigned Start, unsigned End, |
| 1572 | MachineOperand *&MOU) { |
| 1573 | int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM; |
| 1574 | int s = Start; |
| 1575 | while (e >= s) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1576 | // Skip deleted instructions |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1577 | MachineInstr *MI = getInstructionFromIndex(e); |
| 1578 | while ((e - InstrSlots::NUM) >= s && !MI) { |
| 1579 | e -= InstrSlots::NUM; |
| 1580 | MI = getInstructionFromIndex(e); |
| 1581 | } |
| 1582 | if (e < s || MI == NULL) |
| 1583 | return NULL; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1584 | |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1585 | for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1586 | MachineOperand &MO = MI->getOperand(i); |
| 1587 | if (MO.isReg() && MO.isUse() && MO.getReg() && |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1588 | mri_->regsOverlap(rep(MO.getReg()), Reg)) { |
| 1589 | MOU = &MO; |
| 1590 | return MI; |
| 1591 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1592 | } |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1593 | |
| 1594 | e -= InstrSlots::NUM; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Evan Cheng | edeffb3 | 2007-02-26 21:37:37 +0000 | [diff] [blame] | 1597 | return NULL; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Evan Cheng | 30cac02 | 2007-02-22 23:03:39 +0000 | [diff] [blame] | 1600 | /// unsetRegisterKill - Unset IsKill property of all uses of specific register |
| 1601 | /// of the specific instruction. |
| 1602 | void LiveIntervals::unsetRegisterKill(MachineInstr *MI, unsigned Reg) { |
| 1603 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1604 | MachineOperand &MO = MI->getOperand(i); |
| 1605 | if (MO.isReg() && MO.isUse() && MO.isKill() && MO.getReg() && |
| 1606 | mri_->regsOverlap(rep(MO.getReg()), Reg)) |
| 1607 | MO.unsetIsKill(); |
| 1608 | } |
| 1609 | } |
| 1610 | |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 1611 | /// hasRegisterDef - True if the instruction defines the specific register. |
| 1612 | /// |
| 1613 | bool LiveIntervals::hasRegisterDef(MachineInstr *MI, unsigned Reg) { |
| 1614 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1615 | MachineOperand &MO = MI->getOperand(i); |
| 1616 | if (MO.isReg() && MO.isDef() && |
| 1617 | mri_->regsOverlap(rep(MO.getReg()), Reg)) |
| 1618 | return true; |
| 1619 | } |
| 1620 | return false; |
| 1621 | } |
| 1622 | |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 1623 | LiveInterval LiveIntervals::createInterval(unsigned reg) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1624 | float Weight = MRegisterInfo::isPhysicalRegister(reg) ? |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 1625 | HUGE_VALF : 0.0F; |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 1626 | return LiveInterval(reg, Weight); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 1627 | } |