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