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