Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 1 | //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the SplitAnalysis class as well as mutator functions for |
| 11 | // live range splitting. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "splitter" |
| 16 | #include "SplitKit.h" |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 17 | #include "LiveRangeEdit.h" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 18 | #include "VirtRegMap.h" |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame^] | 21 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetInstrInfo.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 33 | static cl::opt<bool> |
| 34 | AllowSplit("spiller-splits-edges", |
| 35 | cl::desc("Allow critical edge splitting during spilling")); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // Split Analysis |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 41 | SplitAnalysis::SplitAnalysis(const MachineFunction &mf, |
| 42 | const LiveIntervals &lis, |
| 43 | const MachineLoopInfo &mli) |
| 44 | : mf_(mf), |
| 45 | lis_(lis), |
| 46 | loops_(mli), |
| 47 | tii_(*mf.getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 48 | curli_(0) {} |
| 49 | |
| 50 | void SplitAnalysis::clear() { |
| 51 | usingInstrs_.clear(); |
| 52 | usingBlocks_.clear(); |
| 53 | usingLoops_.clear(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 54 | curli_ = 0; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 57 | bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) { |
| 58 | MachineBasicBlock *T, *F; |
| 59 | SmallVector<MachineOperand, 4> Cond; |
| 60 | return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond); |
| 61 | } |
| 62 | |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 63 | /// analyzeUses - Count instructions, basic blocks, and loops using curli. |
| 64 | void SplitAnalysis::analyzeUses() { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 65 | const MachineRegisterInfo &MRI = mf_.getRegInfo(); |
| 66 | for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg); |
| 67 | MachineInstr *MI = I.skipInstruction();) { |
| 68 | if (MI->isDebugValue() || !usingInstrs_.insert(MI)) |
| 69 | continue; |
| 70 | MachineBasicBlock *MBB = MI->getParent(); |
| 71 | if (usingBlocks_[MBB]++) |
| 72 | continue; |
Jakob Stoklund Olesen | 9b90d7e | 2010-10-05 23:10:12 +0000 | [diff] [blame] | 73 | for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop; |
| 74 | Loop = Loop->getParentLoop()) |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 75 | usingLoops_[Loop]++; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 76 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 77 | DEBUG(dbgs() << " counted " |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 78 | << usingInstrs_.size() << " instrs, " |
| 79 | << usingBlocks_.size() << " blocks, " |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 80 | << usingLoops_.size() << " loops.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 83 | void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const { |
| 84 | for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 85 | unsigned count = usingBlocks_.lookup(*I); |
| 86 | OS << " BB#" << (*I)->getNumber(); |
| 87 | if (count) |
| 88 | OS << '(' << count << ')'; |
| 89 | } |
| 90 | } |
| 91 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 92 | // Get three sets of basic blocks surrounding a loop: Blocks inside the loop, |
| 93 | // predecessor blocks, and exit blocks. |
| 94 | void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) { |
| 95 | Blocks.clear(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 96 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 97 | // Blocks in the loop. |
| 98 | Blocks.Loop.insert(Loop->block_begin(), Loop->block_end()); |
| 99 | |
| 100 | // Predecessor blocks. |
| 101 | const MachineBasicBlock *Header = Loop->getHeader(); |
| 102 | for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(), |
| 103 | E = Header->pred_end(); I != E; ++I) |
| 104 | if (!Blocks.Loop.count(*I)) |
| 105 | Blocks.Preds.insert(*I); |
| 106 | |
| 107 | // Exit blocks. |
| 108 | for (MachineLoop::block_iterator I = Loop->block_begin(), |
| 109 | E = Loop->block_end(); I != E; ++I) { |
| 110 | const MachineBasicBlock *MBB = *I; |
| 111 | for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(), |
| 112 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 113 | if (!Blocks.Loop.count(*SI)) |
| 114 | Blocks.Exits.insert(*SI); |
| 115 | } |
| 116 | } |
| 117 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 118 | void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const { |
| 119 | OS << "Loop:"; |
| 120 | print(B.Loop, OS); |
| 121 | OS << ", preds:"; |
| 122 | print(B.Preds, OS); |
| 123 | OS << ", exits:"; |
| 124 | print(B.Exits, OS); |
| 125 | } |
| 126 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 127 | /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in |
| 128 | /// and around the Loop. |
| 129 | SplitAnalysis::LoopPeripheralUse SplitAnalysis:: |
| 130 | analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 131 | LoopPeripheralUse use = ContainedInLoop; |
| 132 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 133 | I != E; ++I) { |
| 134 | const MachineBasicBlock *MBB = I->first; |
| 135 | // Is this a peripheral block? |
| 136 | if (use < MultiPeripheral && |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 137 | (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 138 | if (I->second > 1) use = MultiPeripheral; |
| 139 | else use = SinglePeripheral; |
| 140 | continue; |
| 141 | } |
| 142 | // Is it a loop block? |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 143 | if (Blocks.Loop.count(MBB)) |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 144 | continue; |
| 145 | // It must be an unrelated block. |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 146 | DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber()); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 147 | return OutsideLoop; |
| 148 | } |
| 149 | return use; |
| 150 | } |
| 151 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 152 | /// getCriticalExits - It may be necessary to partially break critical edges |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 153 | /// leaving the loop if an exit block has predecessors from outside the loop |
| 154 | /// periphery. |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 155 | void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks, |
| 156 | BlockPtrSet &CriticalExits) { |
| 157 | CriticalExits.clear(); |
| 158 | |
Jakob Stoklund Olesen | 0960a65 | 2010-10-27 00:39:05 +0000 | [diff] [blame] | 159 | // A critical exit block has curli live-in, and has a predecessor that is not |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 160 | // in the loop nor a loop predecessor. For such an exit block, the edges |
| 161 | // carrying the new variable must be moved to a new pre-exit block. |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 162 | for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end(); |
| 163 | I != E; ++I) { |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 164 | const MachineBasicBlock *Exit = *I; |
| 165 | // A single-predecessor exit block is definitely not a critical edge. |
| 166 | if (Exit->pred_size() == 1) |
| 167 | continue; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 168 | // This exit may not have curli live in at all. No need to split. |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 169 | if (!lis_.isLiveInToMBB(*curli_, Exit)) |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 170 | continue; |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 171 | // Does this exit block have a predecessor that is not a loop block or loop |
| 172 | // predecessor? |
| 173 | for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(), |
| 174 | PE = Exit->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 175 | const MachineBasicBlock *Pred = *PI; |
| 176 | if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred)) |
| 177 | continue; |
| 178 | // This is a critical exit block, and we need to split the exit edge. |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 179 | CriticalExits.insert(Exit); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Jakob Stoklund Olesen | 0960a65 | 2010-10-27 00:39:05 +0000 | [diff] [blame] | 185 | void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks, |
| 186 | BlockPtrSet &CriticalPreds) { |
| 187 | CriticalPreds.clear(); |
| 188 | |
| 189 | // A critical predecessor block has curli live-out, and has a successor that |
| 190 | // has curli live-in and is not in the loop nor a loop exit block. For such a |
| 191 | // predecessor block, we must carry the value in both the 'inside' and |
| 192 | // 'outside' registers. |
| 193 | for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end(); |
| 194 | I != E; ++I) { |
| 195 | const MachineBasicBlock *Pred = *I; |
| 196 | // Definitely not a critical edge. |
| 197 | if (Pred->succ_size() == 1) |
| 198 | continue; |
| 199 | // This block may not have curli live out at all if there is a PHI. |
| 200 | if (!lis_.isLiveOutOfMBB(*curli_, Pred)) |
| 201 | continue; |
| 202 | // Does this block have a successor outside the loop? |
| 203 | for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(), |
| 204 | SE = Pred->succ_end(); SI != SE; ++SI) { |
| 205 | const MachineBasicBlock *Succ = *SI; |
| 206 | if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ)) |
| 207 | continue; |
| 208 | if (!lis_.isLiveInToMBB(*curli_, Succ)) |
| 209 | continue; |
| 210 | // This is a critical predecessor block. |
| 211 | CriticalPreds.insert(Pred); |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 217 | /// canSplitCriticalExits - Return true if it is possible to insert new exit |
| 218 | /// blocks before the blocks in CriticalExits. |
| 219 | bool |
| 220 | SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks, |
| 221 | BlockPtrSet &CriticalExits) { |
| 222 | // If we don't allow critical edge splitting, require no critical exits. |
| 223 | if (!AllowSplit) |
| 224 | return CriticalExits.empty(); |
| 225 | |
| 226 | for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end(); |
| 227 | I != E; ++I) { |
| 228 | const MachineBasicBlock *Succ = *I; |
| 229 | // We want to insert a new pre-exit MBB before Succ, and change all the |
| 230 | // in-loop blocks to branch to the pre-exit instead of Succ. |
| 231 | // Check that all the in-loop predecessors can be changed. |
| 232 | for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(), |
| 233 | PE = Succ->pred_end(); PI != PE; ++PI) { |
| 234 | const MachineBasicBlock *Pred = *PI; |
| 235 | // The external predecessors won't be altered. |
| 236 | if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred)) |
| 237 | continue; |
| 238 | if (!canAnalyzeBranch(Pred)) |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // If Succ's layout predecessor falls through, that too must be analyzable. |
| 243 | // We need to insert the pre-exit block in the gap. |
| 244 | MachineFunction::const_iterator MFI = Succ; |
| 245 | if (MFI == mf_.begin()) |
| 246 | continue; |
| 247 | if (!canAnalyzeBranch(--MFI)) |
| 248 | return false; |
| 249 | } |
| 250 | // No problems found. |
| 251 | return true; |
| 252 | } |
| 253 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 254 | void SplitAnalysis::analyze(const LiveInterval *li) { |
| 255 | clear(); |
| 256 | curli_ = li; |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 257 | analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | const MachineLoop *SplitAnalysis::getBestSplitLoop() { |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 261 | assert(curli_ && "Call analyze() before getBestSplitLoop"); |
| 262 | if (usingLoops_.empty()) |
| 263 | return 0; |
| 264 | |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 265 | LoopPtrSet Loops; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 266 | LoopBlocks Blocks; |
| 267 | BlockPtrSet CriticalExits; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 268 | |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 269 | // We split around loops where curli is used outside the periphery. |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 270 | for (LoopCountMap::const_iterator I = usingLoops_.begin(), |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 271 | E = usingLoops_.end(); I != E; ++I) { |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 272 | const MachineLoop *Loop = I->first; |
| 273 | getLoopBlocks(Loop, Blocks); |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 274 | DEBUG({ dbgs() << " "; print(Blocks, dbgs()); }); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 275 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 276 | switch(analyzeLoopPeripheralUse(Blocks)) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 277 | case OutsideLoop: |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 278 | break; |
| 279 | case MultiPeripheral: |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 280 | // FIXME: We could split a live range with multiple uses in a peripheral |
| 281 | // block and still make progress. However, it is possible that splitting |
| 282 | // another live range will insert copies into a peripheral block, and |
| 283 | // there is a small chance we can enter an infinity loop, inserting copies |
| 284 | // forever. |
| 285 | // For safety, stick to splitting live ranges with uses outside the |
| 286 | // periphery. |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 287 | DEBUG(dbgs() << ": multiple peripheral uses\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 288 | break; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 289 | case ContainedInLoop: |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 290 | DEBUG(dbgs() << ": fully contained\n"); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 291 | continue; |
| 292 | case SinglePeripheral: |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 293 | DEBUG(dbgs() << ": single peripheral use\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 294 | continue; |
| 295 | } |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 296 | // Will it be possible to split around this loop? |
| 297 | getCriticalExits(Blocks, CriticalExits); |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 298 | DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n"); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 299 | if (!canSplitCriticalExits(Blocks, CriticalExits)) |
| 300 | continue; |
| 301 | // This is a possible split. |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 302 | Loops.insert(Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 305 | DEBUG(dbgs() << " getBestSplitLoop found " << Loops.size() |
| 306 | << " candidate loops.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 307 | |
| 308 | if (Loops.empty()) |
| 309 | return 0; |
| 310 | |
| 311 | // Pick the earliest loop. |
| 312 | // FIXME: Are there other heuristics to consider? |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 313 | const MachineLoop *Best = 0; |
| 314 | SlotIndex BestIdx; |
| 315 | for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E; |
| 316 | ++I) { |
| 317 | SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader()); |
| 318 | if (!Best || Idx < BestIdx) |
| 319 | Best = *I, BestIdx = Idx; |
| 320 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 321 | DEBUG(dbgs() << " getBestSplitLoop found " << *Best); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 322 | return Best; |
| 323 | } |
| 324 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 325 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 326 | // LiveIntervalMap |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 329 | // Work around the fact that the std::pair constructors are broken for pointer |
| 330 | // pairs in some implementations. makeVV(x, 0) works. |
| 331 | static inline std::pair<const VNInfo*, VNInfo*> |
| 332 | makeVV(const VNInfo *a, VNInfo *b) { |
| 333 | return std::make_pair(a, b); |
| 334 | } |
| 335 | |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 336 | void LiveIntervalMap::reset(LiveInterval *li) { |
| 337 | li_ = li; |
| 338 | valueMap_.clear(); |
| 339 | } |
| 340 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 341 | bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const { |
| 342 | ValueMap::const_iterator i = valueMap_.find(ParentVNI); |
| 343 | return i != valueMap_.end() && i->second == 0; |
| 344 | } |
| 345 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 346 | // defValue - Introduce a li_ def for ParentVNI that could be later than |
| 347 | // ParentVNI->def. |
| 348 | VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 349 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 350 | assert(ParentVNI && "Mapping NULL value"); |
| 351 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 352 | assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
| 353 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 354 | // Create a new value. |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 355 | VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 356 | |
Jakob Stoklund Olesen | 79c0262 | 2010-10-26 22:36:02 +0000 | [diff] [blame] | 357 | // Preserve the PHIDef bit. |
| 358 | if (ParentVNI->isPHIDef() && Idx == ParentVNI->def) |
| 359 | VNI->setIsPHIDef(true); |
| 360 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 361 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 362 | std::pair<ValueMap::iterator,bool> InsP = |
| 363 | valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 364 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 365 | // This is now a complex def. Mark with a NULL in valueMap. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 366 | if (!InsP.second) |
| 367 | InsP.first->second = 0; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 368 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 369 | return VNI; |
| 370 | } |
| 371 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 372 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 373 | // mapValue - Find the mapped value for ParentVNI at Idx. |
| 374 | // Potentially create phi-def values. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 375 | VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx, |
| 376 | bool *simple) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 377 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 378 | assert(ParentVNI && "Mapping NULL value"); |
| 379 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 380 | assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
| 381 | |
| 382 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 383 | std::pair<ValueMap::iterator,bool> InsP = |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 384 | valueMap_.insert(makeVV(ParentVNI, 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 385 | |
| 386 | // This was an unknown value. Create a simple mapping. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 387 | if (InsP.second) { |
| 388 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 389 | return InsP.first->second = li_->createValueCopy(ParentVNI, |
| 390 | lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 393 | // This was a simple mapped value. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 394 | if (InsP.first->second) { |
| 395 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 396 | return InsP.first->second; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 397 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 398 | |
| 399 | // This is a complex mapped value. There may be multiple defs, and we may need |
| 400 | // to create phi-defs. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 401 | if (simple) *simple = false; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 402 | MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx); |
| 403 | assert(IdxMBB && "No MBB at Idx"); |
| 404 | |
| 405 | // Is there a def in the same MBB we can extend? |
| 406 | if (VNInfo *VNI = extendTo(IdxMBB, Idx)) |
| 407 | return VNI; |
| 408 | |
| 409 | // Now for the fun part. We know that ParentVNI potentially has multiple defs, |
| 410 | // and we may need to create even more phi-defs to preserve VNInfo SSA form. |
| 411 | // Perform a depth-first search for predecessor blocks where we know the |
| 412 | // dominating VNInfo. Insert phi-def VNInfos along the path back to IdxMBB. |
| 413 | |
| 414 | // Track MBBs where we have created or learned the dominating value. |
| 415 | // This may change during the DFS as we create new phi-defs. |
| 416 | typedef DenseMap<MachineBasicBlock*, VNInfo*> MBBValueMap; |
| 417 | MBBValueMap DomValue; |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 418 | typedef SplitAnalysis::BlockPtrSet BlockPtrSet; |
| 419 | BlockPtrSet Visited; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 420 | |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 421 | // Iterate over IdxMBB predecessors in a depth-first order. |
| 422 | // Skip begin() since that is always IdxMBB. |
| 423 | for (idf_ext_iterator<MachineBasicBlock*, BlockPtrSet> |
| 424 | IDFI = llvm::next(idf_ext_begin(IdxMBB, Visited)), |
| 425 | IDFE = idf_ext_end(IdxMBB, Visited); IDFI != IDFE;) { |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 426 | MachineBasicBlock *MBB = *IDFI; |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 427 | SlotIndex End = lis_.getMBBEndIdx(MBB).getPrevSlot(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 428 | |
| 429 | // We are operating on the restricted CFG where ParentVNI is live. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 430 | if (parentli_.getVNInfoAt(End) != ParentVNI) { |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 431 | IDFI.skipChildren(); |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | // Do we have a dominating value in this block? |
| 436 | VNInfo *VNI = extendTo(MBB, End); |
| 437 | if (!VNI) { |
| 438 | ++IDFI; |
| 439 | continue; |
| 440 | } |
| 441 | |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 442 | // Yes, VNI dominates MBB. Make sure we visit MBB again from other paths. |
| 443 | Visited.erase(MBB); |
| 444 | |
| 445 | // Track the path back to IdxMBB, creating phi-defs |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 446 | // as needed along the way. |
| 447 | for (unsigned PI = IDFI.getPathLength()-1; PI != 0; --PI) { |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 448 | // Start from MBB's immediate successor. End at IdxMBB. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 449 | MachineBasicBlock *Succ = IDFI.getPath(PI-1); |
| 450 | std::pair<MBBValueMap::iterator, bool> InsP = |
| 451 | DomValue.insert(MBBValueMap::value_type(Succ, VNI)); |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 452 | |
| 453 | // This is the first time we backtrack to Succ. |
| 454 | if (InsP.second) |
| 455 | continue; |
| 456 | |
| 457 | // We reached Succ again with the same VNI. Nothing is going to change. |
| 458 | VNInfo *OVNI = InsP.first->second; |
| 459 | if (OVNI == VNI) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 460 | break; |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 461 | |
| 462 | // Succ already has a phi-def. No need to continue. |
| 463 | SlotIndex Start = lis_.getMBBStartIdx(Succ); |
| 464 | if (OVNI->def == Start) |
| 465 | break; |
| 466 | |
| 467 | // We have a collision between the old and new VNI at Succ. That means |
| 468 | // neither dominates and we need a new phi-def. |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 469 | VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 470 | VNI->setIsPHIDef(true); |
| 471 | InsP.first->second = VNI; |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 472 | |
| 473 | // Replace OVNI with VNI in the remaining path. |
| 474 | for (; PI > 1 ; --PI) { |
| 475 | MBBValueMap::iterator I = DomValue.find(IDFI.getPath(PI-2)); |
| 476 | if (I == DomValue.end() || I->second != OVNI) |
| 477 | break; |
| 478 | I->second = VNI; |
| 479 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // No need to search the children, we found a dominating value. |
Jakob Stoklund Olesen | cf16bea | 2010-08-18 20:06:05 +0000 | [diff] [blame] | 483 | IDFI.skipChildren(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // The search should at least find a dominating value for IdxMBB. |
| 487 | assert(!DomValue.empty() && "Couldn't find a reaching definition"); |
| 488 | |
| 489 | // Since we went through the trouble of a full DFS visiting all reaching defs, |
| 490 | // the values in DomValue are now accurate. No more phi-defs are needed for |
| 491 | // these blocks, so we can color the live ranges. |
| 492 | // This makes the next mapValue call much faster. |
| 493 | VNInfo *IdxVNI = 0; |
| 494 | for (MBBValueMap::iterator I = DomValue.begin(), E = DomValue.end(); I != E; |
| 495 | ++I) { |
| 496 | MachineBasicBlock *MBB = I->first; |
| 497 | VNInfo *VNI = I->second; |
| 498 | SlotIndex Start = lis_.getMBBStartIdx(MBB); |
| 499 | if (MBB == IdxMBB) { |
| 500 | // Don't add full liveness to IdxMBB, stop at Idx. |
| 501 | if (Start != Idx) |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 502 | li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI)); |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 503 | // The caller had better add some liveness to IdxVNI, or it leaks. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 504 | IdxVNI = VNI; |
| 505 | } else |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 506 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | assert(IdxVNI && "Didn't find value for Idx"); |
| 510 | return IdxVNI; |
| 511 | } |
| 512 | |
| 513 | // extendTo - Find the last li_ value defined in MBB at or before Idx. The |
| 514 | // parentli_ is assumed to be live at Idx. Extend the live range to Idx. |
| 515 | // Return the found VNInfo, or NULL. |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 516 | VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 517 | assert(li_ && "call reset first"); |
| 518 | LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx); |
| 519 | if (I == li_->begin()) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 520 | return 0; |
| 521 | --I; |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 522 | if (I->end <= lis_.getMBBStartIdx(MBB)) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 523 | return 0; |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 524 | if (I->end <= Idx) |
| 525 | I->end = Idx.getNextSlot(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 526 | return I->valno; |
| 527 | } |
| 528 | |
| 529 | // addSimpleRange - Add a simple range from parentli_ to li_. |
| 530 | // ParentVNI must be live in the [Start;End) interval. |
| 531 | void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End, |
| 532 | const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 533 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 534 | bool simple; |
| 535 | VNInfo *VNI = mapValue(ParentVNI, Start, &simple); |
| 536 | // A simple mapping is easy. |
| 537 | if (simple) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 538 | li_->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 539 | return; |
| 540 | } |
| 541 | |
| 542 | // ParentVNI is a complex value. We must map per MBB. |
| 543 | MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start); |
Jakob Stoklund Olesen | dbc3609 | 2010-10-05 22:19:29 +0000 | [diff] [blame] | 544 | MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot()); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 545 | |
| 546 | if (MBB == MBBE) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 547 | li_->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
| 551 | // First block. |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 552 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 553 | |
| 554 | // Run sequence of full blocks. |
| 555 | for (++MBB; MBB != MBBE; ++MBB) { |
| 556 | Start = lis_.getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 557 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), |
| 558 | mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Final block. |
| 562 | Start = lis_.getMBBStartIdx(MBB); |
| 563 | if (Start != End) |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 564 | li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_. |
| 568 | /// All needed values whose def is not inside [Start;End) must be defined |
| 569 | /// beforehand so mapValue will work. |
| 570 | void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 571 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 572 | LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end(); |
| 573 | LiveInterval::const_iterator I = std::lower_bound(B, E, Start); |
| 574 | |
| 575 | // Check if --I begins before Start and overlaps. |
| 576 | if (I != B) { |
| 577 | --I; |
| 578 | if (I->end > Start) |
| 579 | addSimpleRange(Start, std::min(End, I->end), I->valno); |
| 580 | ++I; |
| 581 | } |
| 582 | |
| 583 | // The remaining ranges begin after Start. |
| 584 | for (;I != E && I->start < End; ++I) |
| 585 | addSimpleRange(I->start, std::min(End, I->end), I->valno); |
| 586 | } |
| 587 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 588 | VNInfo *LiveIntervalMap::defByCopyFrom(unsigned Reg, |
| 589 | const VNInfo *ParentVNI, |
| 590 | MachineBasicBlock &MBB, |
| 591 | MachineBasicBlock::iterator I) { |
| 592 | const TargetInstrDesc &TID = MBB.getParent()->getTarget().getInstrInfo()-> |
| 593 | get(TargetOpcode::COPY); |
| 594 | MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), TID, li_->reg).addReg(Reg); |
| 595 | SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
| 596 | VNInfo *VNI = defValue(ParentVNI, DefIdx); |
| 597 | VNI->setCopy(MI); |
| 598 | li_->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), VNI)); |
| 599 | return VNI; |
| 600 | } |
| 601 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 602 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 603 | // Split Editor |
| 604 | //===----------------------------------------------------------------------===// |
| 605 | |
| 606 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame^] | 607 | SplitEditor::SplitEditor(SplitAnalysis &sa, |
| 608 | LiveIntervals &lis, |
| 609 | VirtRegMap &vrm, |
| 610 | MachineDominatorTree &mdt, |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 611 | LiveRangeEdit &edit) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 612 | : sa_(sa), lis_(lis), vrm_(vrm), |
| 613 | mri_(vrm.getMachineFunction().getRegInfo()), |
| 614 | tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 615 | edit_(edit), |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame^] | 616 | dupli_(lis_, mdt, edit.getParent()), |
| 617 | openli_(lis_, mdt, edit.getParent()) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 618 | { |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 621 | bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 622 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I) |
| 623 | if (*I != dupli_.getLI() && (*I)->liveAt(Idx)) |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 624 | return true; |
| 625 | return false; |
| 626 | } |
| 627 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 628 | /// Create a new virtual register and live interval. |
| 629 | void SplitEditor::openIntv() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 630 | assert(!openli_.getLI() && "Previous LI not closed before openIntv"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 631 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 632 | if (!dupli_.getLI()) |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 633 | dupli_.reset(&edit_.create(mri_, lis_, vrm_)); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 634 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 635 | openli_.reset(&edit_.create(mri_, lis_, vrm_)); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 638 | /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is |
| 639 | /// not live before Idx, a COPY is not inserted. |
| 640 | void SplitEditor::enterIntvBefore(SlotIndex Idx) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 641 | assert(openli_.getLI() && "openIntv not called before enterIntvBefore"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 642 | DEBUG(dbgs() << " enterIntvBefore " << Idx); |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 643 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx.getUseIndex()); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 644 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 645 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 646 | return; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 647 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 648 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 649 | truncatedValues.insert(ParentVNI); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 650 | MachineInstr *MI = lis_.getInstructionFromIndex(Idx); |
| 651 | assert(MI && "enterIntvBefore called with invalid index"); |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 652 | VNInfo *VNI = openli_.defByCopyFrom(edit_.getReg(), ParentVNI, |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 653 | *MI->getParent(), MI); |
| 654 | openli_.getLI()->addRange(LiveRange(VNI->def, Idx.getDefIndex(), VNI)); |
| 655 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 658 | /// enterIntvAtEnd - Enter openli at the end of MBB. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 659 | void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 660 | assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 661 | SlotIndex End = lis_.getMBBEndIdx(&MBB); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 662 | DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End); |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 663 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(End.getPrevSlot()); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 664 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 665 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 666 | return; |
| 667 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 668 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 669 | truncatedValues.insert(ParentVNI); |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 670 | VNInfo *VNI = openli_.defByCopyFrom(edit_.getReg(), ParentVNI, |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 671 | MBB, MBB.getFirstTerminator()); |
| 672 | // Make sure openli is live out of MBB. |
| 673 | openli_.getLI()->addRange(LiveRange(VNI->def, End, VNI)); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 674 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 677 | /// useIntv - indicate that all instructions in MBB should use openli. |
| 678 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
| 679 | useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 682 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 683 | assert(openli_.getLI() && "openIntv not called before useIntv"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 684 | openli_.addRange(Start, End); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 685 | DEBUG(dbgs() << " use [" << Start << ';' << End << "): " |
| 686 | << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 689 | /// leaveIntvAfter - Leave openli after the instruction at Idx. |
| 690 | void SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 691 | assert(openli_.getLI() && "openIntv not called before leaveIntvAfter"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 692 | DEBUG(dbgs() << " leaveIntvAfter " << Idx); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 693 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 694 | // The interval must be live beyond the instruction at Idx. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 695 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx.getBoundaryIndex()); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 696 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 697 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 698 | return; |
| 699 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 700 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 701 | |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 702 | MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx); |
| 703 | MachineBasicBlock *MBB = MII->getParent(); |
| 704 | VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI, *MBB, |
| 705 | llvm::next(MII)); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 706 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 707 | // Finally we must make sure that openli is properly extended from Idx to the |
| 708 | // new copy. |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 709 | openli_.addSimpleRange(Idx.getBoundaryIndex(), VNI->def, ParentVNI); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 710 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 713 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
| 714 | /// Currently, only one value can leave the interval. |
| 715 | void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 716 | assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 717 | SlotIndex Start = lis_.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 718 | DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 719 | |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 720 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 721 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 722 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 726 | // We are going to insert a back copy, so we must have a dupli_. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 727 | VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI, |
| 728 | MBB, MBB.begin()); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 729 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 730 | // Finally we must make sure that openli is properly extended from Start to |
| 731 | // the new copy. |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 732 | openli_.addSimpleRange(Start, VNI->def, ParentVNI); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 733 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 736 | /// closeIntv - Indicate that we are done editing the currently open |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 737 | /// LiveInterval, and ranges can be trimmed. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 738 | void SplitEditor::closeIntv() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 739 | assert(openli_.getLI() && "openIntv not called before closeIntv"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 740 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 741 | DEBUG(dbgs() << " closeIntv cleaning up\n"); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 742 | DEBUG(dbgs() << " open " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 743 | openli_.reset(0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 746 | /// rewrite - Rewrite all uses of reg to use the new registers. |
| 747 | void SplitEditor::rewrite(unsigned reg) { |
| 748 | for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(reg), |
| 749 | RE = mri_.reg_end(); RI != RE;) { |
| 750 | MachineOperand &MO = RI.getOperand(); |
| 751 | MachineInstr *MI = MO.getParent(); |
| 752 | ++RI; |
| 753 | if (MI->isDebugValue()) { |
| 754 | DEBUG(dbgs() << "Zapping " << *MI); |
| 755 | // FIXME: We can do much better with debug values. |
| 756 | MO.setReg(0); |
| 757 | continue; |
| 758 | } |
| 759 | SlotIndex Idx = lis_.getInstructionIndex(MI); |
| 760 | Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); |
| 761 | LiveInterval *LI = 0; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 762 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; |
| 763 | ++I) { |
| 764 | LiveInterval *testli = *I; |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 765 | if (testli->liveAt(Idx)) { |
| 766 | LI = testli; |
| 767 | break; |
| 768 | } |
| 769 | } |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 770 | DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 771 | assert(LI && "No register was live at use"); |
| 772 | MO.setReg(LI->reg); |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 773 | DEBUG(dbgs() << '\t' << *MI); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 777 | void |
| 778 | SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) { |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 779 | // Build vector of iterator pairs from the intervals. |
| 780 | typedef std::pair<LiveInterval::const_iterator, |
| 781 | LiveInterval::const_iterator> IIPair; |
| 782 | SmallVector<IIPair, 8> Iters; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 783 | for (LiveRangeEdit::iterator LI = edit_.begin(), LE = edit_.end(); LI != LE; |
| 784 | ++LI) { |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 785 | if (*LI == dupli_.getLI()) |
| 786 | continue; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 787 | LiveInterval::const_iterator I = (*LI)->find(Start); |
| 788 | LiveInterval::const_iterator E = (*LI)->end(); |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 789 | if (I != E) |
| 790 | Iters.push_back(std::make_pair(I, E)); |
| 791 | } |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 792 | |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 793 | SlotIndex sidx = Start; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 794 | // Break [Start;End) into segments that don't overlap any intervals. |
| 795 | for (;;) { |
| 796 | SlotIndex next = sidx, eidx = End; |
| 797 | // Find overlapping intervals. |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 798 | for (unsigned i = 0; i != Iters.size() && sidx < eidx; ++i) { |
| 799 | LiveInterval::const_iterator I = Iters[i].first; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 800 | // Interval I is overlapping [sidx;eidx). Trim sidx. |
| 801 | if (I->start <= sidx) { |
| 802 | sidx = I->end; |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 803 | // Move to the next run, remove iters when all are consumed. |
| 804 | I = ++Iters[i].first; |
| 805 | if (I == Iters[i].second) { |
| 806 | Iters.erase(Iters.begin() + i); |
| 807 | --i; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 808 | continue; |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 809 | } |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 810 | } |
| 811 | // Trim eidx too if needed. |
| 812 | if (I->start >= eidx) |
| 813 | continue; |
| 814 | eidx = I->start; |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 815 | next = I->end; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 816 | } |
| 817 | // Now, [sidx;eidx) doesn't overlap anything in intervals_. |
| 818 | if (sidx < eidx) |
| 819 | dupli_.addSimpleRange(sidx, eidx, VNI); |
| 820 | // If the interval end was truncated, we can try again from next. |
| 821 | if (next <= sidx) |
| 822 | break; |
| 823 | sidx = next; |
| 824 | } |
| 825 | } |
| 826 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 827 | void SplitEditor::computeRemainder() { |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 828 | // First we need to fill in the live ranges in dupli. |
| 829 | // If values were redefined, we need a full recoloring with SSA update. |
| 830 | // If values were truncated, we only need to truncate the ranges. |
| 831 | // If values were partially rematted, we should shrink to uses. |
| 832 | // If values were fully rematted, they should be omitted. |
| 833 | // FIXME: If a single value is redefined, just move the def and truncate. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 834 | LiveInterval &parent = edit_.getParent(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 835 | |
| 836 | // Values that are fully contained in the split intervals. |
| 837 | SmallPtrSet<const VNInfo*, 8> deadValues; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 838 | // Map all curli values that should have live defs in dupli. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 839 | for (LiveInterval::const_vni_iterator I = parent.vni_begin(), |
| 840 | E = parent.vni_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 841 | const VNInfo *VNI = *I; |
| 842 | // Original def is contained in the split intervals. |
| 843 | if (intervalsLiveAt(VNI->def)) { |
| 844 | // Did this value escape? |
| 845 | if (dupli_.isMapped(VNI)) |
| 846 | truncatedValues.insert(VNI); |
| 847 | else |
| 848 | deadValues.insert(VNI); |
| 849 | continue; |
| 850 | } |
| 851 | // Add minimal live range at the definition. |
| 852 | VNInfo *DVNI = dupli_.defValue(VNI, VNI->def); |
| 853 | dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI)); |
| 854 | } |
| 855 | |
| 856 | // Add all ranges to dupli. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 857 | for (LiveInterval::const_iterator I = parent.begin(), E = parent.end(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 858 | I != E; ++I) { |
| 859 | const LiveRange &LR = *I; |
| 860 | if (truncatedValues.count(LR.valno)) { |
| 861 | // recolor after removing intervals_. |
| 862 | addTruncSimpleRange(LR.start, LR.end, LR.valno); |
| 863 | } else if (!deadValues.count(LR.valno)) { |
| 864 | // recolor without truncation. |
| 865 | dupli_.addSimpleRange(LR.start, LR.end, LR.valno); |
| 866 | } |
| 867 | } |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 868 | |
| 869 | // Extend dupli_ to be live out of any critical loop predecessors. |
| 870 | // This means we have multiple registers live out of those blocks. |
| 871 | // The alternative would be to split the critical edges. |
| 872 | if (criticalPreds_.empty()) |
| 873 | return; |
| 874 | for (SplitAnalysis::BlockPtrSet::iterator I = criticalPreds_.begin(), |
| 875 | E = criticalPreds_.end(); I != E; ++I) |
| 876 | dupli_.extendTo(*I, lis_.getMBBEndIdx(*I).getPrevSlot()); |
| 877 | criticalPreds_.clear(); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | void SplitEditor::finish() { |
| 881 | assert(!openli_.getLI() && "Previous LI not closed before rewrite"); |
| 882 | assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?"); |
| 883 | |
| 884 | // Complete dupli liveness. |
| 885 | computeRemainder(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 886 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 887 | // Get rid of unused values and set phi-kill flags. |
Jakob Stoklund Olesen | f1354ae | 2010-10-26 22:36:05 +0000 | [diff] [blame] | 888 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I) |
| 889 | (*I)->RenumberValues(lis_); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 890 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 891 | // Rewrite instructions. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 892 | rewrite(edit_.getReg()); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 893 | |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 894 | // Now check if any registers were separated into multiple components. |
| 895 | ConnectedVNInfoEqClasses ConEQ(lis_); |
| 896 | for (unsigned i = 0, e = edit_.size(); i != e; ++i) { |
| 897 | // Don't use iterators, they are invalidated by create() below. |
| 898 | LiveInterval *li = edit_.get(i); |
| 899 | unsigned NumComp = ConEQ.Classify(li); |
| 900 | if (NumComp <= 1) |
| 901 | continue; |
| 902 | DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); |
| 903 | SmallVector<LiveInterval*, 8> dups; |
| 904 | dups.push_back(li); |
| 905 | for (unsigned i = 1; i != NumComp; ++i) |
| 906 | dups.push_back(&edit_.create(mri_, lis_, vrm_)); |
| 907 | ConEQ.Distribute(&dups[0]); |
| 908 | // Rewrite uses to the new regs. |
| 909 | rewrite(li->reg); |
| 910 | } |
| 911 | |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 912 | // Calculate spill weight and allocation hints for new intervals. |
| 913 | VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 914 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I){ |
| 915 | LiveInterval &li = **I; |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 916 | vrai.CalculateRegClass(li.reg); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 917 | vrai.CalculateWeightAndHint(li); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 918 | DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName() |
| 919 | << ":" << li << '\n'); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 920 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 924 | //===----------------------------------------------------------------------===// |
| 925 | // Loop Splitting |
| 926 | //===----------------------------------------------------------------------===// |
| 927 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 928 | void SplitEditor::splitAroundLoop(const MachineLoop *Loop) { |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 929 | SplitAnalysis::LoopBlocks Blocks; |
| 930 | sa_.getLoopBlocks(Loop, Blocks); |
| 931 | |
Jakob Stoklund Olesen | 452a9fd | 2010-10-07 18:47:07 +0000 | [diff] [blame] | 932 | DEBUG({ |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 933 | dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n'; |
Jakob Stoklund Olesen | 452a9fd | 2010-10-07 18:47:07 +0000 | [diff] [blame] | 934 | }); |
| 935 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 936 | // Break critical edges as needed. |
| 937 | SplitAnalysis::BlockPtrSet CriticalExits; |
| 938 | sa_.getCriticalExits(Blocks, CriticalExits); |
| 939 | assert(CriticalExits.empty() && "Cannot break critical exits yet"); |
| 940 | |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 941 | // Get critical predecessors so computeRemainder can deal with them. |
| 942 | sa_.getCriticalPreds(Blocks, criticalPreds_); |
| 943 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 944 | // Create new live interval for the loop. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 945 | openIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 946 | |
| 947 | // Insert copies in the predecessors. |
| 948 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(), |
| 949 | E = Blocks.Preds.end(); I != E; ++I) { |
| 950 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 951 | enterIntvAtEnd(MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | // Switch all loop blocks. |
| 955 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(), |
| 956 | E = Blocks.Loop.end(); I != E; ++I) |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 957 | useIntv(**I); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 958 | |
| 959 | // Insert back copies in the exit blocks. |
| 960 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(), |
| 961 | E = Blocks.Exits.end(); I != E; ++I) { |
| 962 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 963 | leaveIntvAtTop(MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | // Done. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 967 | closeIntv(); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 968 | finish(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 971 | |
| 972 | //===----------------------------------------------------------------------===// |
| 973 | // Single Block Splitting |
| 974 | //===----------------------------------------------------------------------===// |
| 975 | |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 976 | /// getMultiUseBlocks - if curli has more than one use in a basic block, it |
| 977 | /// may be an advantage to split curli for the duration of the block. |
| 978 | bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { |
| 979 | // If curli is local to one block, there is no point to splitting it. |
| 980 | if (usingBlocks_.size() <= 1) |
| 981 | return false; |
| 982 | // Add blocks with multiple uses. |
| 983 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 984 | I != E; ++I) |
| 985 | switch (I->second) { |
| 986 | case 0: |
| 987 | case 1: |
| 988 | continue; |
| 989 | case 2: { |
| 990 | // When there are only two uses and curli is both live in and live out, |
| 991 | // we don't really win anything by isolating the block since we would be |
| 992 | // inserting two copies. |
| 993 | // The remaing register would still have two uses in the block. (Unless it |
| 994 | // separates into disconnected components). |
| 995 | if (lis_.isLiveInToMBB(*curli_, I->first) && |
| 996 | lis_.isLiveOutOfMBB(*curli_, I->first)) |
| 997 | continue; |
| 998 | } // Fall through. |
| 999 | default: |
| 1000 | Blocks.insert(I->first); |
| 1001 | } |
| 1002 | return !Blocks.empty(); |
| 1003 | } |
| 1004 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1005 | /// splitSingleBlocks - Split curli into a separate live interval inside each |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1006 | /// basic block in Blocks. |
| 1007 | void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1008 | DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1009 | // Determine the first and last instruction using curli in each block. |
| 1010 | typedef std::pair<SlotIndex,SlotIndex> IndexPair; |
| 1011 | typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap; |
| 1012 | IndexPairMap MBBRange; |
| 1013 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 1014 | E = sa_.usingInstrs_.end(); I != E; ++I) { |
| 1015 | const MachineBasicBlock *MBB = (*I)->getParent(); |
| 1016 | if (!Blocks.count(MBB)) |
| 1017 | continue; |
| 1018 | SlotIndex Idx = lis_.getInstructionIndex(*I); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1019 | DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1020 | IndexPair &IP = MBBRange[MBB]; |
| 1021 | if (!IP.first.isValid() || Idx < IP.first) |
| 1022 | IP.first = Idx; |
| 1023 | if (!IP.second.isValid() || Idx > IP.second) |
| 1024 | IP.second = Idx; |
| 1025 | } |
| 1026 | |
| 1027 | // Create a new interval for each block. |
| 1028 | for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(), |
| 1029 | E = Blocks.end(); I != E; ++I) { |
| 1030 | IndexPair &IP = MBBRange[*I]; |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1031 | DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": [" |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1032 | << IP.first << ';' << IP.second << ")\n"); |
| 1033 | assert(IP.first.isValid() && IP.second.isValid()); |
| 1034 | |
| 1035 | openIntv(); |
| 1036 | enterIntvBefore(IP.first); |
| 1037 | useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex()); |
| 1038 | leaveIntvAfter(IP.second); |
| 1039 | closeIntv(); |
| 1040 | } |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1041 | finish(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1044 | |
| 1045 | //===----------------------------------------------------------------------===// |
| 1046 | // Sub Block Splitting |
| 1047 | //===----------------------------------------------------------------------===// |
| 1048 | |
| 1049 | /// getBlockForInsideSplit - If curli is contained inside a single basic block, |
| 1050 | /// and it wou pay to subdivide the interval inside that block, return it. |
| 1051 | /// Otherwise return NULL. The returned block can be passed to |
| 1052 | /// SplitEditor::splitInsideBlock. |
| 1053 | const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() { |
| 1054 | // The interval must be exclusive to one block. |
| 1055 | if (usingBlocks_.size() != 1) |
| 1056 | return 0; |
| 1057 | // Don't to this for less than 4 instructions. We want to be sure that |
| 1058 | // splitting actually reduces the instruction count per interval. |
| 1059 | if (usingInstrs_.size() < 4) |
| 1060 | return 0; |
| 1061 | return usingBlocks_.begin()->first; |
| 1062 | } |
| 1063 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1064 | /// splitInsideBlock - Split curli into multiple intervals inside MBB. |
| 1065 | void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1066 | SmallVector<SlotIndex, 32> Uses; |
| 1067 | Uses.reserve(sa_.usingInstrs_.size()); |
| 1068 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 1069 | E = sa_.usingInstrs_.end(); I != E; ++I) |
| 1070 | if ((*I)->getParent() == MBB) |
| 1071 | Uses.push_back(lis_.getInstructionIndex(*I)); |
| 1072 | DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for " |
| 1073 | << Uses.size() << " instructions.\n"); |
| 1074 | assert(Uses.size() >= 3 && "Need at least 3 instructions"); |
| 1075 | array_pod_sort(Uses.begin(), Uses.end()); |
| 1076 | |
| 1077 | // Simple algorithm: Find the largest gap between uses as determined by slot |
| 1078 | // indices. Create new intervals for instructions before the gap and after the |
| 1079 | // gap. |
| 1080 | unsigned bestPos = 0; |
| 1081 | int bestGap = 0; |
| 1082 | DEBUG(dbgs() << " dist (" << Uses[0]); |
| 1083 | for (unsigned i = 1, e = Uses.size(); i != e; ++i) { |
| 1084 | int g = Uses[i-1].distance(Uses[i]); |
| 1085 | DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]); |
| 1086 | if (g > bestGap) |
| 1087 | bestPos = i, bestGap = g; |
| 1088 | } |
| 1089 | DEBUG(dbgs() << "), best: -" << bestGap << "-\n"); |
| 1090 | |
| 1091 | // bestPos points to the first use after the best gap. |
| 1092 | assert(bestPos > 0 && "Invalid gap"); |
| 1093 | |
| 1094 | // FIXME: Don't create intervals for low densities. |
| 1095 | |
| 1096 | // First interval before the gap. Don't create single-instr intervals. |
| 1097 | if (bestPos > 1) { |
| 1098 | openIntv(); |
| 1099 | enterIntvBefore(Uses.front()); |
| 1100 | useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex()); |
| 1101 | leaveIntvAfter(Uses[bestPos-1]); |
| 1102 | closeIntv(); |
| 1103 | } |
| 1104 | |
| 1105 | // Second interval after the gap. |
| 1106 | if (bestPos < Uses.size()-1) { |
| 1107 | openIntv(); |
| 1108 | enterIntvBefore(Uses[bestPos]); |
| 1109 | useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex()); |
| 1110 | leaveIntvAfter(Uses.back()); |
| 1111 | closeIntv(); |
| 1112 | } |
| 1113 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1114 | finish(); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1115 | } |