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