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" |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +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 | |
Evan Cheng | bc165e4 | 2007-08-16 07:24:22 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | // Hidden options for help debugging. |
| 41 | cl::opt<bool> DisableReMat("disable-rematerialization", |
| 42 | cl::init(false), cl::Hidden); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 43 | |
| 44 | cl::opt<bool> SplitAtBB("split-intervals-at-bb", |
| 45 | cl::init(false), cl::Hidden); |
Evan Cheng | bc165e4 | 2007-08-16 07:24:22 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 48 | STATISTIC(numIntervals, "Number of original intervals"); |
| 49 | STATISTIC(numIntervalsAfter, "Number of intervals after coalescing"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 50 | STATISTIC(numFolded , "Number of loads/stores folded into instructions"); |
| 51 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 52 | char LiveIntervals::ID = 0; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 53 | namespace { |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 54 | RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis"); |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 55 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 56 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 57 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 58 | AU.addPreserved<LiveVariables>(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 59 | AU.addRequired<LiveVariables>(); |
| 60 | AU.addPreservedID(PHIEliminationID); |
| 61 | AU.addRequiredID(PHIEliminationID); |
| 62 | AU.addRequiredID(TwoAddressInstructionPassID); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 63 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 66 | void LiveIntervals::releaseMemory() { |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 67 | Idx2MBBMap.clear(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 68 | mi2iMap_.clear(); |
| 69 | i2miMap_.clear(); |
| 70 | r2iMap_.clear(); |
Evan Cheng | dd199d2 | 2007-09-06 01:07:24 +0000 | [diff] [blame] | 71 | // Release VNInfo memroy regions after all VNInfo objects are dtor'd. |
| 72 | VNInfoAllocator.Reset(); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 73 | for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i) |
| 74 | delete ClonedMIs[i]; |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 77 | namespace llvm { |
| 78 | inline bool operator<(unsigned V, const IdxMBBPair &IM) { |
| 79 | return V < IM.first; |
| 80 | } |
| 81 | |
| 82 | inline bool operator<(const IdxMBBPair &IM, unsigned V) { |
| 83 | return IM.first < V; |
| 84 | } |
| 85 | |
| 86 | struct Idx2MBBCompare { |
| 87 | bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const { |
| 88 | return LHS.first < RHS.first; |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 93 | /// runOnMachineFunction - Register allocate the whole function |
| 94 | /// |
| 95 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 96 | mf_ = &fn; |
| 97 | tm_ = &fn.getTarget(); |
| 98 | mri_ = tm_->getRegisterInfo(); |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 99 | tii_ = tm_->getInstrInfo(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 100 | lv_ = &getAnalysis<LiveVariables>(); |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 101 | allocatableRegs_ = mri_->getAllocatableSet(fn); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 103 | // Number MachineInstrs and MachineBasicBlocks. |
| 104 | // Initialize MBB indexes to a sentinal. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 105 | MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U)); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 106 | |
| 107 | unsigned MIIndex = 0; |
| 108 | for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end(); |
| 109 | MBB != E; ++MBB) { |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 110 | unsigned StartIdx = MIIndex; |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 112 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 113 | I != E; ++I) { |
| 114 | bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 115 | assert(inserted && "multiple MachineInstr -> index mappings"); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 116 | i2miMap_.push_back(I); |
| 117 | MIIndex += InstrSlots::NUM; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 118 | } |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 119 | |
| 120 | // Set the MBB2IdxMap entry for this MBB. |
| 121 | MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1); |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 122 | Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB)); |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 123 | } |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 124 | std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare()); |
Alkis Evlogimenos | d6e40a6 | 2004-01-14 10:44:29 +0000 | [diff] [blame] | 125 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 126 | computeIntervals(); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 127 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 128 | numIntervals += getNumIntervals(); |
| 129 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 130 | DOUT << "********** INTERVALS **********\n"; |
| 131 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 132 | I->second.print(DOUT, mri_); |
| 133 | DOUT << "\n"; |
| 134 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 135 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 136 | numIntervalsAfter += getNumIntervals(); |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 137 | DEBUG(dump()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 138 | return true; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 141 | /// print - Implement the dump method. |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 142 | void LiveIntervals::print(std::ostream &O, const Module* ) const { |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 143 | O << "********** INTERVALS **********\n"; |
Chris Lattner | 8e7a709 | 2005-07-27 23:03:38 +0000 | [diff] [blame] | 144 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 145 | I->second.print(DOUT, mri_); |
| 146 | DOUT << "\n"; |
Chris Lattner | 8e7a709 | 2005-07-27 23:03:38 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 148 | |
| 149 | O << "********** MACHINEINSTRS **********\n"; |
| 150 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 151 | mbbi != mbbe; ++mbbi) { |
| 152 | O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n"; |
| 153 | for (MachineBasicBlock::iterator mii = mbbi->begin(), |
| 154 | mie = mbbi->end(); mii != mie; ++mii) { |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 155 | O << getInstructionIndex(mii) << '\t' << *mii; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 160 | /// conflictsWithPhysRegDef - Returns true if the specified register |
| 161 | /// is defined during the duration of the specified interval. |
| 162 | bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li, |
| 163 | VirtRegMap &vrm, unsigned reg) { |
| 164 | for (LiveInterval::Ranges::const_iterator |
| 165 | I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) { |
| 166 | for (unsigned index = getBaseIndex(I->start), |
| 167 | end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end; |
| 168 | index += InstrSlots::NUM) { |
| 169 | // skip deleted instructions |
| 170 | while (index != end && !getInstructionFromIndex(index)) |
| 171 | index += InstrSlots::NUM; |
| 172 | if (index == end) break; |
| 173 | |
| 174 | MachineInstr *MI = getInstructionFromIndex(index); |
Evan Cheng | 5d44626 | 2007-11-15 08:13:29 +0000 | [diff] [blame] | 175 | unsigned SrcReg, DstReg; |
| 176 | if (tii_->isMoveInstr(*MI, SrcReg, DstReg)) |
| 177 | if (SrcReg == li.reg || DstReg == li.reg) |
| 178 | continue; |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 179 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 180 | MachineOperand& mop = MI->getOperand(i); |
Evan Cheng | 5d44626 | 2007-11-15 08:13:29 +0000 | [diff] [blame] | 181 | if (!mop.isRegister()) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 182 | continue; |
| 183 | unsigned PhysReg = mop.getReg(); |
Evan Cheng | 5d44626 | 2007-11-15 08:13:29 +0000 | [diff] [blame] | 184 | if (PhysReg == 0 || PhysReg == li.reg) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 185 | continue; |
Evan Cheng | 5d44626 | 2007-11-15 08:13:29 +0000 | [diff] [blame] | 186 | if (MRegisterInfo::isVirtualRegister(PhysReg)) { |
| 187 | if (!vrm.hasPhys(PhysReg)) |
| 188 | continue; |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 189 | PhysReg = vrm.getPhys(PhysReg); |
Evan Cheng | 5d44626 | 2007-11-15 08:13:29 +0000 | [diff] [blame] | 190 | } |
Evan Cheng | 5f5f3b6 | 2007-11-05 00:59:10 +0000 | [diff] [blame] | 191 | if (PhysReg && mri_->regsOverlap(PhysReg, reg)) |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 200 | void LiveIntervals::printRegName(unsigned reg) const { |
| 201 | if (MRegisterInfo::isPhysicalRegister(reg)) |
| 202 | cerr << mri_->getName(reg); |
| 203 | else |
| 204 | cerr << "%reg" << reg; |
| 205 | } |
| 206 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 207 | void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb, |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 208 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 209 | unsigned MIIdx, |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 210 | LiveInterval &interval) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 211 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 212 | LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 213 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 214 | // Virtual registers may be defined multiple times (due to phi |
| 215 | // elimination and 2-addr elimination). Much of what we do only has to be |
| 216 | // 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] | 217 | // time we see a vreg. |
| 218 | if (interval.empty()) { |
| 219 | // Get the Idx of the defining instructions. |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 220 | unsigned defIndex = getDefIndex(MIIdx); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 221 | VNInfo *ValNo; |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 222 | unsigned SrcReg, DstReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 223 | if (tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 224 | ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator); |
Evan Cheng | 48ff282 | 2007-10-12 17:16:50 +0000 | [diff] [blame] | 225 | else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 226 | ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(), |
| 227 | VNInfoAllocator); |
| 228 | else |
| 229 | ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 230 | |
| 231 | assert(ValNo->id == 0 && "First value in interval is not 0?"); |
Chris Lattner | 7ac2d31 | 2004-07-24 02:59:07 +0000 | [diff] [blame] | 232 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 233 | // Loop over all of the blocks that the vreg is defined in. There are |
| 234 | // two cases we have to handle here. The most common case is a vreg |
| 235 | // whose lifetime is contained within a basic block. In this case there |
| 236 | // will be a single kill, in MBB, which comes after the definition. |
| 237 | if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) { |
| 238 | // FIXME: what about dead vars? |
| 239 | unsigned killIdx; |
| 240 | if (vi.Kills[0] != mi) |
| 241 | killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1; |
| 242 | else |
| 243 | killIdx = defIndex+1; |
Chris Lattner | 6097d13 | 2004-07-19 02:15:56 +0000 | [diff] [blame] | 244 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 245 | // If the kill happens after the definition, we have an intra-block |
| 246 | // live range. |
| 247 | if (killIdx > defIndex) { |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 248 | assert(vi.AliveBlocks.none() && |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 249 | "Shouldn't be alive across any blocks!"); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 250 | LiveRange LR(defIndex, killIdx, ValNo); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 251 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 252 | DOUT << " +" << LR << "\n"; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 253 | interval.addKill(ValNo, killIdx); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 254 | return; |
| 255 | } |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 256 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 257 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 258 | // The other case we handle is when a virtual register lives to the end |
| 259 | // of the defining block, potentially live across some blocks, then is |
| 260 | // live into some number of blocks, but gets killed. Start by adding a |
| 261 | // 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] | 262 | LiveRange NewLR(defIndex, |
| 263 | getInstructionIndex(&mbb->back()) + InstrSlots::NUM, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 264 | ValNo); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 265 | DOUT << " +" << NewLR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 266 | interval.addRange(NewLR); |
| 267 | |
| 268 | // Iterate over all of the blocks that the variable is completely |
| 269 | // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the |
| 270 | // live interval. |
| 271 | for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) { |
| 272 | if (vi.AliveBlocks[i]) { |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 273 | MachineBasicBlock *MBB = mf_->getBlockNumbered(i); |
| 274 | if (!MBB->empty()) { |
| 275 | LiveRange LR(getMBBStartIdx(i), |
| 276 | getInstructionIndex(&MBB->back()) + InstrSlots::NUM, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 277 | ValNo); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 278 | interval.addRange(LR); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 279 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Finally, this virtual register is live from the start of any killing |
| 285 | // block to the 'use' slot of the killing instruction. |
| 286 | for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) { |
| 287 | MachineInstr *Kill = vi.Kills[i]; |
Evan Cheng | 8df7860 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 288 | unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 289 | LiveRange LR(getMBBStartIdx(Kill->getParent()), |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 290 | killIdx, ValNo); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 291 | interval.addRange(LR); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 292 | interval.addKill(ValNo, killIdx); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 293 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | } else { |
| 297 | // If this is the second time we see a virtual register definition, it |
| 298 | // 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] | 299 | // the result of two address elimination, then the vreg is one of the |
| 300 | // def-and-use register operand. |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 301 | if (mi->isRegReDefinedByTwoAddr(interval.reg)) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 302 | // If this is a two-address definition, then we have already processed |
| 303 | // the live range. The only problem is that we didn't realize there |
| 304 | // are actually two values in the live interval. Because of this we |
| 305 | // need to take the LiveRegion that defines this register and split it |
| 306 | // into two values. |
| 307 | unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst)); |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 308 | unsigned RedefIndex = getDefIndex(MIIdx); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 309 | |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 310 | const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 311 | VNInfo *OldValNo = OldLR->valno; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 312 | unsigned OldEnd = OldLR->end; |
| 313 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 314 | // Delete the initial value, which should be short and continuous, |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 315 | // 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] | 316 | interval.removeRange(DefIndex, RedefIndex); |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 317 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 318 | // Two-address vregs should always only be redefined once. This means |
| 319 | // that at this point, there should be exactly one value number in it. |
| 320 | assert(interval.containsOneValue() && "Unexpected 2-addr liveint!"); |
| 321 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 322 | // The new value number (#1) is defined by the instruction we claimed |
| 323 | // defined value #0. |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 324 | VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator); |
| 325 | interval.copyValNumInfo(ValNo, OldValNo); |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 327 | // Value#0 is now defined by the 2-addr instruction. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 328 | OldValNo->def = RedefIndex; |
| 329 | OldValNo->reg = 0; |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 330 | |
| 331 | // Add the new live interval which replaces the range for the input copy. |
| 332 | LiveRange LR(DefIndex, RedefIndex, ValNo); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 333 | DOUT << " replace range with " << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 334 | interval.addRange(LR); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 335 | interval.addKill(ValNo, RedefIndex); |
| 336 | interval.removeKills(ValNo, RedefIndex, OldEnd); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 337 | |
| 338 | // If this redefinition is dead, we need to add a dummy unit live |
| 339 | // range covering the def slot. |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 340 | if (lv_->RegisterDefIsDead(mi, interval.reg)) |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 341 | interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo)); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 342 | |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 343 | DOUT << " RESULT: "; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 344 | interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 345 | |
| 346 | } else { |
| 347 | // Otherwise, this must be because of phi elimination. If this is the |
| 348 | // first redefinition of the vreg that we have seen, go back and change |
| 349 | // the live range in the PHI block to be a different value number. |
| 350 | if (interval.containsOneValue()) { |
| 351 | assert(vi.Kills.size() == 1 && |
| 352 | "PHI elimination vreg should have one kill, the PHI itself!"); |
| 353 | |
| 354 | // Remove the old range that we now know has an incorrect number. |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 355 | VNInfo *VNI = interval.getValNumInfo(0); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 356 | MachineInstr *Killer = vi.Kills[0]; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 357 | unsigned Start = getMBBStartIdx(Killer->getParent()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 358 | unsigned End = getUseIndex(getInstructionIndex(Killer))+1; |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 359 | DOUT << " Removing [" << Start << "," << End << "] from: "; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 360 | interval.print(DOUT, mri_); DOUT << "\n"; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 361 | interval.removeRange(Start, End); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 362 | interval.addKill(VNI, Start+1); // odd # means phi node |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 363 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 364 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 365 | // Replace the interval with one of a NEW value number. Note that this |
| 366 | // value number isn't actually defined by an instruction, weird huh? :) |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 367 | LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator)); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 368 | DOUT << " replace range with " << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 369 | interval.addRange(LR); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 370 | interval.addKill(LR.valno, End); |
Evan Cheng | 56fdd7a | 2007-03-15 21:19:28 +0000 | [diff] [blame] | 371 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // In the case of PHI elimination, each variable definition is only |
| 375 | // live until the end of the block. We've already taken care of the |
| 376 | // rest of the live range. |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 377 | unsigned defIndex = getDefIndex(MIIdx); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 378 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 379 | VNInfo *ValNo; |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 380 | unsigned SrcReg, DstReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 381 | if (tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 382 | ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 383 | else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) |
| 384 | ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(), |
| 385 | VNInfoAllocator); |
| 386 | else |
| 387 | ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator); |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 388 | |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 389 | unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 390 | LiveRange LR(defIndex, killIndex, ValNo); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 391 | interval.addRange(LR); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 392 | interval.addKill(ValNo, killIndex-1); // odd # means phi node |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 393 | DOUT << " +" << LR; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 397 | DOUT << '\n'; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 400 | void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB, |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 401 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 402 | unsigned MIIdx, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 403 | LiveInterval &interval, |
| 404 | unsigned SrcReg) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 405 | // A physical register cannot be live across basic block, so its |
| 406 | // lifetime must end somewhere in its defining basic block. |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 407 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 409 | unsigned baseIndex = MIIdx; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 410 | unsigned start = getDefIndex(baseIndex); |
| 411 | unsigned end = start; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 412 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 413 | // If it is not used after definition, it is considered dead at |
| 414 | // the instruction defining it. Hence its interval is: |
| 415 | // [defSlot(def), defSlot(def)+1) |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 416 | if (lv_->RegisterDefIsDead(mi, interval.reg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 417 | DOUT << " dead"; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 418 | end = getDefIndex(start) + 1; |
| 419 | goto exit; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // If it is not dead on definition, it must be killed by a |
| 423 | // subsequent instruction. Hence its interval is: |
| 424 | // [defSlot(def), useSlot(kill)+1) |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 425 | while (++mi != MBB->end()) { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 426 | baseIndex += InstrSlots::NUM; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 427 | if (lv_->KillsRegister(mi, interval.reg)) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 428 | DOUT << " killed"; |
Chris Lattner | ab4b66d | 2005-08-23 22:51:41 +0000 | [diff] [blame] | 429 | end = getUseIndex(baseIndex) + 1; |
| 430 | goto exit; |
Evan Cheng | 9a1956a | 2006-11-15 20:54:11 +0000 | [diff] [blame] | 431 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 432 | // Another instruction redefines the register before it is ever read. |
| 433 | // Then the register is essentially dead at the instruction that defines |
| 434 | // it. Hence its interval is: |
| 435 | // [defSlot(def), defSlot(def)+1) |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 436 | DOUT << " dead"; |
Evan Cheng | 9a1956a | 2006-11-15 20:54:11 +0000 | [diff] [blame] | 437 | end = getDefIndex(start) + 1; |
| 438 | goto exit; |
Alkis Evlogimenos | af25473 | 2004-01-13 22:26:14 +0000 | [diff] [blame] | 439 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 441 | |
| 442 | // The only case we should have a dead physreg here without a killing or |
| 443 | // instruction where we know it's dead is if it is live-in to the function |
| 444 | // and never used. |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 445 | assert(!SrcReg && "physreg was not killed in defining block!"); |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 446 | end = getDefIndex(start) + 1; // It's dead. |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 447 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 448 | exit: |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 449 | assert(start < end && "did not find end of interval?"); |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 450 | |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 451 | // Already exists? Extend old live interval. |
| 452 | LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 453 | VNInfo *ValNo = (OldLR != interval.end()) |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 454 | ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 455 | LiveRange LR(start, end, ValNo); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 456 | interval.addRange(LR); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 457 | interval.addKill(LR.valno, end); |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 458 | DOUT << " +" << LR << '\n'; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 461 | void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB, |
| 462 | MachineBasicBlock::iterator MI, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 463 | unsigned MIIdx, |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 464 | unsigned reg) { |
| 465 | if (MRegisterInfo::isVirtualRegister(reg)) |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 466 | handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg)); |
Alkis Evlogimenos | 5327801 | 2004-08-26 22:22:38 +0000 | [diff] [blame] | 467 | else if (allocatableRegs_[reg]) { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 468 | unsigned SrcReg, DstReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 469 | if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) |
| 470 | SrcReg = MI->getOperand(1).getReg(); |
| 471 | else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg)) |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 472 | SrcReg = 0; |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 473 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg); |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 474 | // Def of a register also defines its sub-registers. |
| 475 | for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS) |
| 476 | // Avoid processing some defs more than once. |
| 477 | if (!MI->findRegisterDefOperand(*AS)) |
| 478 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0); |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 479 | } |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 482 | void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB, |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 483 | unsigned MIIdx, |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 484 | LiveInterval &interval, bool isAlias) { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 485 | DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg)); |
| 486 | |
| 487 | // Look for kills, if it reaches a def before it's killed, then it shouldn't |
| 488 | // be considered a livein. |
| 489 | MachineBasicBlock::iterator mi = MBB->begin(); |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 490 | unsigned baseIndex = MIIdx; |
| 491 | unsigned start = baseIndex; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 492 | unsigned end = start; |
| 493 | while (mi != MBB->end()) { |
| 494 | if (lv_->KillsRegister(mi, interval.reg)) { |
| 495 | DOUT << " killed"; |
| 496 | end = getUseIndex(baseIndex) + 1; |
| 497 | goto exit; |
| 498 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 499 | // Another instruction redefines the register before it is ever read. |
| 500 | // Then the register is essentially dead at the instruction that defines |
| 501 | // it. Hence its interval is: |
| 502 | // [defSlot(def), defSlot(def)+1) |
| 503 | DOUT << " dead"; |
| 504 | end = getDefIndex(start) + 1; |
| 505 | goto exit; |
| 506 | } |
| 507 | |
| 508 | baseIndex += InstrSlots::NUM; |
| 509 | ++mi; |
| 510 | } |
| 511 | |
| 512 | exit: |
Evan Cheng | 75611fb | 2007-06-27 01:16:36 +0000 | [diff] [blame] | 513 | // Live-in register might not be used at all. |
| 514 | if (end == MIIdx) { |
Evan Cheng | 292da94 | 2007-06-27 18:47:28 +0000 | [diff] [blame] | 515 | if (isAlias) { |
| 516 | DOUT << " dead"; |
Evan Cheng | 75611fb | 2007-06-27 01:16:36 +0000 | [diff] [blame] | 517 | end = getDefIndex(MIIdx) + 1; |
Evan Cheng | 292da94 | 2007-06-27 18:47:28 +0000 | [diff] [blame] | 518 | } else { |
| 519 | DOUT << " live through"; |
| 520 | end = baseIndex; |
| 521 | } |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 524 | LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator)); |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 525 | interval.addRange(LR); |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 526 | interval.addKill(LR.valno, end); |
Evan Cheng | 24c2e5c | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 527 | DOUT << " +" << LR << '\n'; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 530 | /// computeIntervals - computes the live intervals for virtual |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 531 | /// registers. for some ordering of the machine instructions [1,N] a |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 532 | /// 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] | 533 | /// which a variable is live |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 534 | void LiveIntervals::computeIntervals() { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 535 | DOUT << "********** COMPUTING LIVE INTERVALS **********\n" |
| 536 | << "********** Function: " |
| 537 | << ((Value*)mf_->getFunction())->getName() << '\n'; |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 538 | // Track the index of the current machine instr. |
| 539 | unsigned MIIndex = 0; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 540 | for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end(); |
| 541 | MBBI != E; ++MBBI) { |
| 542 | MachineBasicBlock *MBB = MBBI; |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 543 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 545 | MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end(); |
Evan Cheng | 0c9f92e | 2007-02-13 01:30:55 +0000 | [diff] [blame] | 546 | |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 547 | // Create intervals for live-ins to this BB first. |
| 548 | for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(), |
| 549 | LE = MBB->livein_end(); LI != LE; ++LI) { |
| 550 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI)); |
| 551 | // Multiple live-ins can alias the same register. |
| 552 | for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS) |
| 553 | if (!hasInterval(*AS)) |
| 554 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS), |
| 555 | true); |
Chris Lattner | dffb2e8 | 2006-09-04 18:27:40 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 558 | for (; MI != miEnd; ++MI) { |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 559 | DOUT << MIIndex << "\t" << *MI; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 560 | |
Evan Cheng | 438f7bc | 2006-11-10 08:43:01 +0000 | [diff] [blame] | 561 | // Handle defs. |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 562 | for (int i = MI->getNumOperands() - 1; i >= 0; --i) { |
| 563 | MachineOperand &MO = MI->getOperand(i); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 564 | // handle register defs - build intervals |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 565 | if (MO.isRegister() && MO.getReg() && MO.isDef()) |
| 566 | handleRegisterDef(MBB, MI, MIIndex, MO.getReg()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 567 | } |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 568 | |
| 569 | MIIndex += InstrSlots::NUM; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 570 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 571 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 572 | } |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 573 | |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 574 | bool LiveIntervals::findLiveInMBBs(const LiveRange &LR, |
Evan Cheng | a5bfc97 | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 575 | SmallVectorImpl<MachineBasicBlock*> &MBBs) const { |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 576 | std::vector<IdxMBBPair>::const_iterator I = |
| 577 | std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start); |
| 578 | |
| 579 | bool ResVal = false; |
| 580 | while (I != Idx2MBBMap.end()) { |
| 581 | if (LR.end <= I->first) |
| 582 | break; |
| 583 | MBBs.push_back(I->second); |
| 584 | ResVal = true; |
| 585 | ++I; |
| 586 | } |
| 587 | return ResVal; |
| 588 | } |
| 589 | |
| 590 | |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 591 | LiveInterval LiveIntervals::createInterval(unsigned reg) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 592 | float Weight = MRegisterInfo::isPhysicalRegister(reg) ? |
Jim Laskey | 7902c75 | 2006-11-07 12:25:45 +0000 | [diff] [blame] | 593 | HUGE_VALF : 0.0F; |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 594 | return LiveInterval(reg, Weight); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 595 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 596 | |
| 597 | |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | // Register allocator hooks. |
| 600 | // |
| 601 | |
| 602 | /// isReMaterializable - Returns true if the definition MI of the specified |
| 603 | /// val# of the specified interval is re-materializable. |
| 604 | bool LiveIntervals::isReMaterializable(const LiveInterval &li, |
| 605 | const VNInfo *ValNo, MachineInstr *MI) { |
| 606 | if (DisableReMat) |
| 607 | return false; |
| 608 | |
| 609 | if (tii_->isTriviallyReMaterializable(MI)) |
| 610 | return true; |
| 611 | |
| 612 | int FrameIdx = 0; |
| 613 | if (!tii_->isLoadFromStackSlot(MI, FrameIdx) || |
| 614 | !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx)) |
| 615 | return false; |
| 616 | |
| 617 | // This is a load from fixed stack slot. It can be rematerialized unless it's |
| 618 | // re-defined by a two-address instruction. |
| 619 | for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end(); |
| 620 | i != e; ++i) { |
| 621 | const VNInfo *VNI = *i; |
| 622 | if (VNI == ValNo) |
| 623 | continue; |
| 624 | unsigned DefIdx = VNI->def; |
| 625 | if (DefIdx == ~1U) |
| 626 | continue; // Dead val#. |
| 627 | MachineInstr *DefMI = (DefIdx == ~0u) |
| 628 | ? NULL : getInstructionFromIndex(DefIdx); |
| 629 | if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg)) |
| 630 | return false; |
| 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from |
| 636 | /// slot / to reg or any rematerialized load into ith operand of specified |
| 637 | /// MI. If it is successul, MI is updated with the newly created MI and |
| 638 | /// returns true. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 639 | bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, |
| 640 | VirtRegMap &vrm, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 641 | MachineInstr *DefMI, |
| 642 | unsigned index, unsigned i, |
| 643 | bool isSS, int slot, unsigned reg) { |
| 644 | MachineInstr *fmi = isSS |
| 645 | ? mri_->foldMemoryOperand(MI, i, slot) |
| 646 | : mri_->foldMemoryOperand(MI, i, DefMI); |
| 647 | if (fmi) { |
| 648 | // Attempt to fold the memory reference into the instruction. If |
| 649 | // we can do this, we don't need to insert spill code. |
| 650 | if (lv_) |
| 651 | lv_->instructionChanged(MI, fmi); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 652 | else |
| 653 | LiveVariables::transferKillDeadInfo(MI, fmi, mri_); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 654 | MachineBasicBlock &MBB = *MI->getParent(); |
| 655 | vrm.virtFolded(reg, MI, i, fmi); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 656 | vrm.transferSpillPts(MI, fmi); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 657 | mi2iMap_.erase(MI); |
| 658 | i2miMap_[index/InstrSlots::NUM] = fmi; |
| 659 | mi2iMap_[fmi] = index; |
| 660 | MI = MBB.insert(MBB.erase(MI), fmi); |
| 661 | ++numFolded; |
| 662 | return true; |
| 663 | } |
| 664 | return false; |
| 665 | } |
| 666 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 667 | bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const { |
| 668 | SmallPtrSet<MachineBasicBlock*, 4> MBBs; |
| 669 | for (LiveInterval::Ranges::const_iterator |
| 670 | I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) { |
| 671 | std::vector<IdxMBBPair>::const_iterator II = |
| 672 | std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start); |
| 673 | if (II == Idx2MBBMap.end()) |
| 674 | continue; |
| 675 | if (I->end > II->first) // crossing a MBB. |
| 676 | return false; |
| 677 | MBBs.insert(II->second); |
| 678 | if (MBBs.size() > 1) |
| 679 | return false; |
| 680 | } |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | static |
| 685 | bool hasALaterUse(MachineBasicBlock *MBB, MachineInstr *MI, unsigned Reg) { |
| 686 | MachineBasicBlock::iterator I = MI; |
| 687 | if (I == MBB->end()) |
| 688 | return false; |
| 689 | ++I; |
| 690 | while (I != MBB->end()) { |
| 691 | if (I->findRegisterUseOperandIdx(Reg) != -1) |
| 692 | return true; |
| 693 | ++I; |
| 694 | } |
| 695 | return false; |
| 696 | } |
| 697 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 698 | /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions |
| 699 | /// for addIntervalsForSpills to rewrite uses / defs for the given live range. |
| 700 | void LiveIntervals:: |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 701 | rewriteInstructionForSpills(const LiveInterval &li, bool TrySplit, |
| 702 | unsigned id, unsigned index, unsigned end, MachineInstr *MI, |
| 703 | MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 704 | unsigned Slot, int LdSlot, |
| 705 | bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete, |
| 706 | VirtRegMap &vrm, SSARegMap *RegMap, |
| 707 | const TargetRegisterClass* rc, |
| 708 | SmallVector<int, 4> &ReMatIds, |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 709 | unsigned &NewVReg, bool &HasDef, bool &HasUse, |
| 710 | const LoopInfo *loopInfo, std::vector<unsigned> &NewVRegs, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 711 | std::vector<LiveInterval*> &NewLIs) { |
| 712 | RestartInstruction: |
| 713 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 714 | MachineOperand& mop = MI->getOperand(i); |
| 715 | if (!mop.isRegister()) |
| 716 | continue; |
| 717 | unsigned Reg = mop.getReg(); |
| 718 | unsigned RegI = Reg; |
| 719 | if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg)) |
| 720 | continue; |
Evan Cheng | c498b02 | 2007-11-14 07:59:08 +0000 | [diff] [blame] | 721 | unsigned SubIdx = mop.getSubReg(); |
| 722 | bool isSubReg = SubIdx != 0; |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 723 | if (Reg != li.reg) |
| 724 | continue; |
| 725 | |
| 726 | bool TryFold = !DefIsReMat; |
| 727 | bool FoldSS = true; |
| 728 | int FoldSlot = Slot; |
| 729 | if (DefIsReMat) { |
| 730 | // If this is the rematerializable definition MI itself and |
| 731 | // all of its uses are rematerialized, simply delete it. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 732 | if (MI == ReMatOrigDefMI && CanDelete) { |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 733 | RemoveMachineInstrFromMaps(MI); |
| 734 | MI->eraseFromParent(); |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | // If def for this use can't be rematerialized, then try folding. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 739 | TryFold = !ReMatOrigDefMI || |
| 740 | (ReMatOrigDefMI && (MI == ReMatOrigDefMI || isLoad)); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 741 | if (isLoad) { |
| 742 | // Try fold loads (from stack slot, constant pool, etc.) into uses. |
| 743 | FoldSS = isLoadSS; |
| 744 | FoldSlot = LdSlot; |
| 745 | } |
| 746 | } |
| 747 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 748 | // If we are splitting live intervals, only fold if it's 1) the first |
| 749 | // use and it's a kill or 2) there isn't another use later in this MBB. |
| 750 | TryFold &= NewVReg == 0; |
| 751 | if (TryFold && TrySplit) |
| 752 | // Do not fold store into def here if we are splitting. We'll find an |
| 753 | // optimal point to insert a store later. |
| 754 | if (HasDef || mop.isDef() || |
| 755 | (!mop.isKill() && hasALaterUse(MI->getParent(), MI, li.reg))) |
| 756 | TryFold = false; |
| 757 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 758 | // FIXME: fold subreg use |
| 759 | if (!isSubReg && TryFold && |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 760 | tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index, i, FoldSS, FoldSlot, |
| 761 | Reg)) |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 762 | // Folding the load/store can completely change the instruction in |
| 763 | // unpredictable ways, rescan it from the beginning. |
| 764 | goto RestartInstruction; |
| 765 | |
| 766 | // Create a new virtual register for the spill interval. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 767 | bool CreatedNewVReg = false; |
| 768 | if (NewVReg == 0) { |
| 769 | NewVReg = RegMap->createVirtualRegister(rc); |
| 770 | vrm.grow(); |
| 771 | CreatedNewVReg = true; |
| 772 | } |
| 773 | mop.setReg(NewVReg); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 774 | |
| 775 | // Scan all of the operands of this instruction rewriting operands |
| 776 | // to use NewVReg instead of li.reg as appropriate. We do this for |
| 777 | // two reasons: |
| 778 | // |
| 779 | // 1. If the instr reads the same spilled vreg multiple times, we |
| 780 | // want to reuse the NewVReg. |
| 781 | // 2. If the instr is a two-addr instruction, we are required to |
| 782 | // keep the src/dst regs pinned. |
| 783 | // |
| 784 | // Keep track of whether we replace a use and/or def so that we can |
| 785 | // create the spill interval with the appropriate range. |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 786 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 787 | HasUse = mop.isUse(); |
| 788 | HasDef = mop.isDef(); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 789 | for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) { |
| 790 | if (!MI->getOperand(j).isRegister()) |
| 791 | continue; |
| 792 | unsigned RegJ = MI->getOperand(j).getReg(); |
| 793 | if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ)) |
| 794 | continue; |
| 795 | if (RegJ == RegI) { |
| 796 | MI->getOperand(j).setReg(NewVReg); |
| 797 | HasUse |= MI->getOperand(j).isUse(); |
| 798 | HasDef |= MI->getOperand(j).isDef(); |
| 799 | } |
| 800 | } |
| 801 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 802 | if (CreatedNewVReg) { |
| 803 | if (DefIsReMat) { |
| 804 | vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/); |
| 805 | if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) { |
| 806 | // Each valnum may have its own remat id. |
| 807 | ReMatIds[id] = vrm.assignVirtReMatId(NewVReg); |
| 808 | } else { |
| 809 | vrm.assignVirtReMatId(NewVReg, ReMatIds[id]); |
| 810 | } |
| 811 | if (!CanDelete || (HasUse && HasDef)) { |
| 812 | // If this is a two-addr instruction then its use operands are |
| 813 | // rematerializable but its def is not. It should be assigned a |
| 814 | // stack slot. |
| 815 | vrm.assignVirt2StackSlot(NewVReg, Slot); |
| 816 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 817 | } else { |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 818 | vrm.assignVirt2StackSlot(NewVReg, Slot); |
| 819 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | // create a new register interval for this spill / remat. |
| 823 | LiveInterval &nI = getOrCreateInterval(NewVReg); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 824 | if (CreatedNewVReg) { |
| 825 | NewLIs.push_back(&nI); |
| 826 | NewVRegs[MI->getParent()->getNumber()] = NewVReg; |
| 827 | if (TrySplit) |
| 828 | vrm.setIsSplitFromReg(NewVReg, li.reg); |
| 829 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 830 | |
| 831 | if (HasUse) { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 832 | if (CreatedNewVReg) { |
| 833 | LiveRange LR(getLoadIndex(index), getUseIndex(index)+1, |
| 834 | nI.getNextValue(~0U, 0, VNInfoAllocator)); |
| 835 | DOUT << " +" << LR; |
| 836 | nI.addRange(LR); |
| 837 | } else { |
| 838 | // Extend the split live interval to this def / use. |
| 839 | unsigned End = getUseIndex(index)+1; |
| 840 | LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End, |
| 841 | nI.getValNumInfo(nI.getNumValNums()-1)); |
| 842 | DOUT << " +" << LR; |
| 843 | nI.addRange(LR); |
| 844 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 845 | } |
| 846 | if (HasDef) { |
| 847 | LiveRange LR(getDefIndex(index), getStoreIndex(index), |
| 848 | nI.getNextValue(~0U, 0, VNInfoAllocator)); |
| 849 | DOUT << " +" << LR; |
| 850 | nI.addRange(LR); |
| 851 | } |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 852 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 853 | DOUT << "\t\t\t\tAdded new interval: "; |
| 854 | nI.print(DOUT, mri_); |
| 855 | DOUT << '\n'; |
| 856 | } |
| 857 | } |
| 858 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 859 | bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li, |
| 860 | MachineBasicBlock *MBB, unsigned Idx, |
| 861 | const VNInfo *VNI) const { |
| 862 | unsigned End = getMBBEndIdx(MBB); |
| 863 | if (VNI) { |
| 864 | for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) { |
| 865 | unsigned KillIdx = VNI->kills[j]; |
| 866 | if (KillIdx > Idx && KillIdx < End) |
| 867 | return true; |
| 868 | } |
| 869 | return false; |
| 870 | } |
| 871 | |
| 872 | // Look at all the VNInfo's. |
| 873 | for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end(); |
| 874 | i != e; ++i) { |
| 875 | const VNInfo *VNI = *i; |
| 876 | for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) { |
| 877 | unsigned KillIdx = VNI->kills[j]; |
| 878 | if (KillIdx > Idx && KillIdx < End) |
| 879 | return true; |
| 880 | } |
| 881 | } |
| 882 | return false; |
| 883 | } |
| 884 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 885 | void LiveIntervals:: |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 886 | rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 887 | LiveInterval::Ranges::const_iterator &I, |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 888 | MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 889 | unsigned Slot, int LdSlot, |
| 890 | bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete, |
| 891 | VirtRegMap &vrm, SSARegMap *RegMap, |
| 892 | const TargetRegisterClass* rc, |
| 893 | SmallVector<int, 4> &ReMatIds, |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 894 | const LoopInfo *loopInfo, |
| 895 | BitVector &SpillMBBs, |
| 896 | std::vector<std::pair<int, unsigned> > &SpillIdxes, |
| 897 | std::vector<unsigned> &NewVRegs, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 898 | std::vector<LiveInterval*> &NewLIs) { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 899 | unsigned NewVReg = 0; |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 900 | unsigned index = getBaseIndex(I->start); |
| 901 | unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 902 | bool TrySplitMI = TrySplit && vrm.getPreSplitReg(li.reg) == 0; |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 903 | for (; index != end; index += InstrSlots::NUM) { |
| 904 | // skip deleted instructions |
| 905 | while (index != end && !getInstructionFromIndex(index)) |
| 906 | index += InstrSlots::NUM; |
| 907 | if (index == end) break; |
| 908 | |
| 909 | MachineInstr *MI = getInstructionFromIndex(index); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 910 | MachineBasicBlock *MBB = MI->getParent(); |
| 911 | NewVReg = !TrySplitMI ? 0 : NewVRegs[MBB->getNumber()]; |
| 912 | bool IsNew = NewVReg == 0; |
| 913 | bool HasDef = false; |
| 914 | bool HasUse = false; |
| 915 | rewriteInstructionForSpills(li, TrySplitMI, I->valno->id, index, end, |
| 916 | MI, ReMatOrigDefMI, ReMatDefMI, Slot, LdSlot, |
| 917 | isLoad, isLoadSS, DefIsReMat, CanDelete, vrm, |
| 918 | RegMap, rc, ReMatIds, NewVReg, HasDef, HasUse, |
| 919 | loopInfo, NewVRegs, NewLIs); |
| 920 | if (!HasDef && !HasUse) |
| 921 | continue; |
| 922 | |
| 923 | // Update weight of spill interval. |
| 924 | LiveInterval &nI = getOrCreateInterval(NewVReg); |
| 925 | if (!TrySplitMI) |
| 926 | // The spill weight is now infinity as it cannot be spilled again. |
| 927 | nI.weight = HUGE_VALF; |
| 928 | else { |
| 929 | // Keep track of the last def in each MBB. |
| 930 | if (HasDef) { |
| 931 | if (MI != ReMatOrigDefMI || !CanDelete) { |
| 932 | // If this is a two-address code, then this index probably starts a |
| 933 | // VNInfo so we should examine all the VNInfo's. |
| 934 | bool HasKill = HasUse |
| 935 | ? anyKillInMBBAfterIdx(li, MBB, getDefIndex(index)) |
| 936 | : anyKillInMBBAfterIdx(li, MBB, getDefIndex(index), I->valno); |
| 937 | if (!HasKill) { |
| 938 | unsigned MBBId = MBB->getNumber(); |
| 939 | if ((int)index > SpillIdxes[MBBId].first) |
| 940 | // High bit specify whether this spill ought to be folded if |
| 941 | // possible. |
| 942 | SpillIdxes[MBBId] = std::make_pair(index, NewVReg | (1 << 31)); |
| 943 | SpillMBBs.set(MBBId); |
| 944 | } |
| 945 | } |
| 946 | if (!IsNew) { |
| 947 | // It this interval hasn't been assigned a stack slot |
| 948 | // (because earlier def is remat), do it now. |
| 949 | int SS = vrm.getStackSlot(NewVReg); |
| 950 | if (SS != (int)Slot) { |
| 951 | assert(SS == VirtRegMap::NO_STACK_SLOT); |
| 952 | vrm.assignVirt2StackSlot(NewVReg, Slot); |
| 953 | } |
| 954 | } |
| 955 | } else if (HasUse) { |
| 956 | // Use(s) following the last def, it's not safe to fold the spill. |
| 957 | unsigned MBBId = MBB->getNumber(); |
| 958 | if ((SpillIdxes[MBBId].second & ((1<<31)-1)) == NewVReg && |
| 959 | (int)getUseIndex(index) > SpillIdxes[MBBId].first) |
| 960 | SpillIdxes[MBBId].second &= (1<<31)-1; |
| 961 | } |
| 962 | |
| 963 | // Update spill weight. |
| 964 | unsigned loopDepth = loopInfo->getLoopDepth(MBB->getBasicBlock()); |
| 965 | nI.weight += getSpillWeight(HasDef, HasUse, loopDepth); |
| 966 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 970 | |
| 971 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 972 | std::vector<LiveInterval*> LiveIntervals:: |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 973 | addIntervalsForSpills(const LiveInterval &li, |
| 974 | const LoopInfo *loopInfo, VirtRegMap &vrm) { |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 975 | // Since this is called after the analysis is done we don't know if |
| 976 | // LiveVariables is available |
| 977 | lv_ = getAnalysisToUpdate<LiveVariables>(); |
| 978 | |
| 979 | assert(li.weight != HUGE_VALF && |
| 980 | "attempt to spill already spilled interval!"); |
| 981 | |
| 982 | DOUT << "\t\t\t\tadding intervals for spills for interval: "; |
| 983 | li.print(DOUT, mri_); |
| 984 | DOUT << '\n'; |
| 985 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 986 | // Each bit specify whether it a spill is required in the MBB. |
| 987 | BitVector SpillMBBs(mf_->getNumBlockIDs()); |
| 988 | std::vector<std::pair<int, unsigned> > SpillIdxes(mf_->getNumBlockIDs(), |
| 989 | std::make_pair(-1,0)); |
| 990 | std::vector<unsigned> NewVRegs(mf_->getNumBlockIDs(), 0); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 991 | std::vector<LiveInterval*> NewLIs; |
| 992 | SSARegMap *RegMap = mf_->getSSARegMap(); |
| 993 | const TargetRegisterClass* rc = RegMap->getRegClass(li.reg); |
| 994 | |
| 995 | unsigned NumValNums = li.getNumValNums(); |
| 996 | SmallVector<MachineInstr*, 4> ReMatDefs; |
| 997 | ReMatDefs.resize(NumValNums, NULL); |
| 998 | SmallVector<MachineInstr*, 4> ReMatOrigDefs; |
| 999 | ReMatOrigDefs.resize(NumValNums, NULL); |
| 1000 | SmallVector<int, 4> ReMatIds; |
| 1001 | ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT); |
| 1002 | BitVector ReMatDelete(NumValNums); |
| 1003 | unsigned Slot = VirtRegMap::MAX_STACK_SLOT; |
| 1004 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1005 | // Spilling a split live interval. It cannot be split any further. Also, |
| 1006 | // it's also guaranteed to be a single val# / range interval. |
| 1007 | if (vrm.getPreSplitReg(li.reg)) { |
| 1008 | vrm.setIsSplitFromReg(li.reg, 0); |
| 1009 | bool DefIsReMat = vrm.isReMaterialized(li.reg); |
| 1010 | Slot = vrm.getStackSlot(li.reg); |
| 1011 | assert(Slot != VirtRegMap::MAX_STACK_SLOT); |
| 1012 | MachineInstr *ReMatDefMI = DefIsReMat ? |
| 1013 | vrm.getReMaterializedMI(li.reg) : NULL; |
| 1014 | int LdSlot = 0; |
| 1015 | bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot); |
| 1016 | bool isLoad = isLoadSS || |
| 1017 | (DefIsReMat && (ReMatDefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG)); |
| 1018 | vrm.removeAllSpillPtsForReg(li.reg); |
| 1019 | bool IsFirstRange = true; |
| 1020 | for (LiveInterval::Ranges::const_iterator |
| 1021 | I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) { |
| 1022 | // If this is a split live interval with multiple ranges, it means there |
| 1023 | // are two-address instructions that re-defined the value. Only the |
| 1024 | // first def can be rematerialized! |
| 1025 | if (IsFirstRange) { |
| 1026 | rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI, |
| 1027 | Slot, LdSlot, isLoad, isLoadSS, DefIsReMat, |
| 1028 | false, vrm, RegMap, rc, ReMatIds, |
| 1029 | loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs); |
| 1030 | } else { |
| 1031 | rewriteInstructionsForSpills(li, false, I, NULL, 0, |
| 1032 | Slot, 0, false, false, false, |
| 1033 | false, vrm, RegMap, rc, ReMatIds, |
| 1034 | loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs); |
| 1035 | } |
| 1036 | IsFirstRange = false; |
| 1037 | } |
| 1038 | return NewLIs; |
| 1039 | } |
| 1040 | |
| 1041 | bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1042 | bool NeedStackSlot = false; |
| 1043 | for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end(); |
| 1044 | i != e; ++i) { |
| 1045 | const VNInfo *VNI = *i; |
| 1046 | unsigned VN = VNI->id; |
| 1047 | unsigned DefIdx = VNI->def; |
| 1048 | if (DefIdx == ~1U) |
| 1049 | continue; // Dead val#. |
| 1050 | // Is the def for the val# rematerializable? |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1051 | MachineInstr *ReMatDefMI = (DefIdx == ~0u) |
| 1052 | ? 0 : getInstructionFromIndex(DefIdx); |
| 1053 | if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI)) { |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1054 | // Remember how to remat the def of this val#. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1055 | ReMatOrigDefs[VN] = ReMatDefMI; |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1056 | // Original def may be modified so we have to make a copy here. vrm must |
| 1057 | // delete these! |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1058 | ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone(); |
| 1059 | vrm.setVirtIsReMaterialized(li.reg, ReMatDefMI); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1060 | |
| 1061 | bool CanDelete = true; |
| 1062 | for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) { |
| 1063 | unsigned KillIdx = VNI->kills[j]; |
| 1064 | MachineInstr *KillMI = (KillIdx & 1) |
| 1065 | ? NULL : getInstructionFromIndex(KillIdx); |
| 1066 | // Kill is a phi node, not all of its uses can be rematerialized. |
| 1067 | // It must not be deleted. |
| 1068 | if (!KillMI) { |
| 1069 | CanDelete = false; |
| 1070 | // Need a stack slot if there is any live range where uses cannot be |
| 1071 | // rematerialized. |
| 1072 | NeedStackSlot = true; |
| 1073 | break; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | if (CanDelete) |
| 1078 | ReMatDelete.set(VN); |
| 1079 | } else { |
| 1080 | // Need a stack slot if there is any live range where uses cannot be |
| 1081 | // rematerialized. |
| 1082 | NeedStackSlot = true; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | // One stack slot per live interval. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1087 | if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1088 | Slot = vrm.assignVirt2StackSlot(li.reg); |
| 1089 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1090 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1091 | // Create new intervals and rewrite defs and uses. |
| 1092 | for (LiveInterval::Ranges::const_iterator |
| 1093 | I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1094 | MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id]; |
| 1095 | MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id]; |
| 1096 | bool DefIsReMat = ReMatDefMI != NULL; |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1097 | bool CanDelete = ReMatDelete[I->valno->id]; |
| 1098 | int LdSlot = 0; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1099 | bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1100 | bool isLoad = isLoadSS || |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1101 | (DefIsReMat && (ReMatDefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG)); |
| 1102 | rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI, |
| 1103 | Slot, LdSlot, isLoad, isLoadSS, DefIsReMat, |
| 1104 | CanDelete, vrm, RegMap, rc, ReMatIds, |
| 1105 | loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame^] | 1108 | // Insert spills if we are splitting. |
| 1109 | if (TrySplit && NeedStackSlot) { |
| 1110 | int Id = SpillMBBs.find_first(); |
| 1111 | while (Id != -1) { |
| 1112 | unsigned index = SpillIdxes[Id].first; |
| 1113 | unsigned VReg = SpillIdxes[Id].second & ((1 << 31)-1); |
| 1114 | bool TryFold = SpillIdxes[Id].second & (1 << 31); |
| 1115 | MachineInstr *MI = getInstructionFromIndex(index); |
| 1116 | int OpIdx = -1; |
| 1117 | if (TryFold) { |
| 1118 | for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { |
| 1119 | MachineOperand &MO = MI->getOperand(j); |
| 1120 | if (!MO.isRegister() || MO.getReg() != VReg) |
| 1121 | continue; |
| 1122 | if (MO.isUse()) { |
| 1123 | // Can't fold if it's two-address code. |
| 1124 | OpIdx = -1; |
| 1125 | break; |
| 1126 | } |
| 1127 | OpIdx = (int)j; |
| 1128 | } |
| 1129 | } |
| 1130 | // Fold the store into the def if possible. |
| 1131 | if (OpIdx == -1 || |
| 1132 | !tryFoldMemoryOperand(MI, vrm, NULL, index, OpIdx, true, Slot, VReg)) |
| 1133 | // Else tell the spiller to issue a store for us. |
| 1134 | vrm.addSpillPoint(VReg, MI); |
| 1135 | Id = SpillMBBs.find_next(Id); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | // Finalize spill weights. |
| 1140 | if (TrySplit) |
| 1141 | for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) |
| 1142 | NewLIs[i]->weight /= NewLIs[i]->getSize(); |
| 1143 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 1144 | return NewLIs; |
| 1145 | } |