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 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 254 | LoopPtrSet *LPS = 0; |
| 255 | switch(analyzeLoopPeripheralUse(Blocks)) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 256 | case OutsideLoop: |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 257 | LPS = &Loops; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 258 | break; |
| 259 | case MultiPeripheral: |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 260 | LPS = &SecondLoops; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 261 | break; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 262 | case ContainedInLoop: |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 263 | DEBUG(dbgs() << " contained in " << *Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 264 | continue; |
| 265 | case SinglePeripheral: |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 266 | DEBUG(dbgs() << " single peripheral use in " << *Loop); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 267 | continue; |
| 268 | } |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 269 | // Will it be possible to split around this loop? |
| 270 | getCriticalExits(Blocks, CriticalExits); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 271 | DEBUG(dbgs() << " " << CriticalExits.size() << " critical exits from " |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 272 | << *Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 273 | if (!canSplitCriticalExits(Blocks, CriticalExits)) |
| 274 | continue; |
| 275 | // This is a possible split. |
| 276 | assert(LPS); |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 277 | LPS->insert(Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 280 | DEBUG(dbgs() << " getBestSplitLoop found " << Loops.size() << " + " |
| 281 | << SecondLoops.size() << " candidate loops.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 282 | |
| 283 | // If there are no first class loops available, look at second class loops. |
| 284 | if (Loops.empty()) |
| 285 | Loops = SecondLoops; |
| 286 | |
| 287 | if (Loops.empty()) |
| 288 | return 0; |
| 289 | |
| 290 | // Pick the earliest loop. |
| 291 | // FIXME: Are there other heuristics to consider? |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 292 | const MachineLoop *Best = 0; |
| 293 | SlotIndex BestIdx; |
| 294 | for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E; |
| 295 | ++I) { |
| 296 | SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader()); |
| 297 | if (!Best || Idx < BestIdx) |
| 298 | Best = *I, BestIdx = Idx; |
| 299 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 300 | DEBUG(dbgs() << " getBestSplitLoop found " << *Best); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 301 | return Best; |
| 302 | } |
| 303 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 304 | /// getMultiUseBlocks - if curli has more than one use in a basic block, it |
| 305 | /// may be an advantage to split curli for the duration of the block. |
| 306 | bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { |
| 307 | // If curli is local to one block, there is no point to splitting it. |
| 308 | if (usingBlocks_.size() <= 1) |
| 309 | return false; |
| 310 | // Add blocks with multiple uses. |
| 311 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 312 | I != E; ++I) |
| 313 | switch (I->second) { |
| 314 | case 0: |
| 315 | case 1: |
| 316 | continue; |
| 317 | case 2: { |
| 318 | // It doesn't pay to split a 2-instr block if it redefines curli. |
| 319 | VNInfo *VN1 = curli_->getVNInfoAt(lis_.getMBBStartIdx(I->first)); |
| 320 | VNInfo *VN2 = |
| 321 | curli_->getVNInfoAt(lis_.getMBBEndIdx(I->first).getPrevIndex()); |
| 322 | // live-in and live-out with a different value. |
| 323 | if (VN1 && VN2 && VN1 != VN2) |
| 324 | continue; |
| 325 | } // Fall through. |
| 326 | default: |
| 327 | Blocks.insert(I->first); |
| 328 | } |
| 329 | return !Blocks.empty(); |
| 330 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 331 | |
| 332 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 333 | // LiveIntervalMap |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 336 | // Work around the fact that the std::pair constructors are broken for pointer |
| 337 | // pairs in some implementations. makeVV(x, 0) works. |
| 338 | static inline std::pair<const VNInfo*, VNInfo*> |
| 339 | makeVV(const VNInfo *a, VNInfo *b) { |
| 340 | return std::make_pair(a, b); |
| 341 | } |
| 342 | |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 343 | void LiveIntervalMap::reset(LiveInterval *li) { |
| 344 | li_ = li; |
| 345 | valueMap_.clear(); |
| 346 | } |
| 347 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 348 | bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const { |
| 349 | ValueMap::const_iterator i = valueMap_.find(ParentVNI); |
| 350 | return i != valueMap_.end() && i->second == 0; |
| 351 | } |
| 352 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 353 | // defValue - Introduce a li_ def for ParentVNI that could be later than |
| 354 | // ParentVNI->def. |
| 355 | VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 356 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 357 | assert(ParentVNI && "Mapping NULL value"); |
| 358 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 359 | assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
| 360 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 361 | // Create a new value. |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 362 | VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 363 | |
| 364 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 365 | std::pair<ValueMap::iterator,bool> InsP = |
| 366 | valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 367 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 368 | // 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] | 369 | if (!InsP.second) |
| 370 | InsP.first->second = 0; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 371 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 372 | return VNI; |
| 373 | } |
| 374 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 375 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 376 | // mapValue - Find the mapped value for ParentVNI at Idx. |
| 377 | // Potentially create phi-def values. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 378 | VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx, |
| 379 | bool *simple) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 380 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 381 | assert(ParentVNI && "Mapping NULL value"); |
| 382 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 383 | assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
| 384 | |
| 385 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 386 | std::pair<ValueMap::iterator,bool> InsP = |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 387 | valueMap_.insert(makeVV(ParentVNI, 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 388 | |
| 389 | // This was an unknown value. Create a simple mapping. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 390 | if (InsP.second) { |
| 391 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 392 | return InsP.first->second = li_->createValueCopy(ParentVNI, |
| 393 | lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 396 | // This was a simple mapped value. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 397 | if (InsP.first->second) { |
| 398 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 399 | return InsP.first->second; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 400 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 401 | |
| 402 | // This is a complex mapped value. There may be multiple defs, and we may need |
| 403 | // to create phi-defs. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 404 | if (simple) *simple = false; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 405 | MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx); |
| 406 | assert(IdxMBB && "No MBB at Idx"); |
| 407 | |
| 408 | // Is there a def in the same MBB we can extend? |
| 409 | if (VNInfo *VNI = extendTo(IdxMBB, Idx)) |
| 410 | return VNI; |
| 411 | |
| 412 | // Now for the fun part. We know that ParentVNI potentially has multiple defs, |
| 413 | // and we may need to create even more phi-defs to preserve VNInfo SSA form. |
| 414 | // Perform a depth-first search for predecessor blocks where we know the |
| 415 | // dominating VNInfo. Insert phi-def VNInfos along the path back to IdxMBB. |
| 416 | |
| 417 | // Track MBBs where we have created or learned the dominating value. |
| 418 | // This may change during the DFS as we create new phi-defs. |
| 419 | typedef DenseMap<MachineBasicBlock*, VNInfo*> MBBValueMap; |
| 420 | MBBValueMap DomValue; |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 421 | typedef SplitAnalysis::BlockPtrSet BlockPtrSet; |
| 422 | BlockPtrSet Visited; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 423 | |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 424 | // Iterate over IdxMBB predecessors in a depth-first order. |
| 425 | // Skip begin() since that is always IdxMBB. |
| 426 | for (idf_ext_iterator<MachineBasicBlock*, BlockPtrSet> |
| 427 | IDFI = llvm::next(idf_ext_begin(IdxMBB, Visited)), |
| 428 | IDFE = idf_ext_end(IdxMBB, Visited); IDFI != IDFE;) { |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 429 | MachineBasicBlock *MBB = *IDFI; |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 430 | SlotIndex End = lis_.getMBBEndIdx(MBB).getPrevSlot(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 431 | |
| 432 | // We are operating on the restricted CFG where ParentVNI is live. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 433 | if (parentli_.getVNInfoAt(End) != ParentVNI) { |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 434 | IDFI.skipChildren(); |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | // Do we have a dominating value in this block? |
| 439 | VNInfo *VNI = extendTo(MBB, End); |
| 440 | if (!VNI) { |
| 441 | ++IDFI; |
| 442 | continue; |
| 443 | } |
| 444 | |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 445 | // Yes, VNI dominates MBB. Make sure we visit MBB again from other paths. |
| 446 | Visited.erase(MBB); |
| 447 | |
| 448 | // Track the path back to IdxMBB, creating phi-defs |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 449 | // as needed along the way. |
| 450 | for (unsigned PI = IDFI.getPathLength()-1; PI != 0; --PI) { |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 451 | // Start from MBB's immediate successor. End at IdxMBB. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 452 | MachineBasicBlock *Succ = IDFI.getPath(PI-1); |
| 453 | std::pair<MBBValueMap::iterator, bool> InsP = |
| 454 | DomValue.insert(MBBValueMap::value_type(Succ, VNI)); |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 455 | |
| 456 | // This is the first time we backtrack to Succ. |
| 457 | if (InsP.second) |
| 458 | continue; |
| 459 | |
| 460 | // We reached Succ again with the same VNI. Nothing is going to change. |
| 461 | VNInfo *OVNI = InsP.first->second; |
| 462 | if (OVNI == VNI) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 463 | break; |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 464 | |
| 465 | // Succ already has a phi-def. No need to continue. |
| 466 | SlotIndex Start = lis_.getMBBStartIdx(Succ); |
| 467 | if (OVNI->def == Start) |
| 468 | break; |
| 469 | |
| 470 | // We have a collision between the old and new VNI at Succ. That means |
| 471 | // neither dominates and we need a new phi-def. |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 472 | VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 473 | VNI->setIsPHIDef(true); |
| 474 | InsP.first->second = VNI; |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 475 | |
| 476 | // Replace OVNI with VNI in the remaining path. |
| 477 | for (; PI > 1 ; --PI) { |
| 478 | MBBValueMap::iterator I = DomValue.find(IDFI.getPath(PI-2)); |
| 479 | if (I == DomValue.end() || I->second != OVNI) |
| 480 | break; |
| 481 | I->second = VNI; |
| 482 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // No need to search the children, we found a dominating value. |
Jakob Stoklund Olesen | cf16bea | 2010-08-18 20:06:05 +0000 | [diff] [blame] | 486 | IDFI.skipChildren(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // The search should at least find a dominating value for IdxMBB. |
| 490 | assert(!DomValue.empty() && "Couldn't find a reaching definition"); |
| 491 | |
| 492 | // Since we went through the trouble of a full DFS visiting all reaching defs, |
| 493 | // the values in DomValue are now accurate. No more phi-defs are needed for |
| 494 | // these blocks, so we can color the live ranges. |
| 495 | // This makes the next mapValue call much faster. |
| 496 | VNInfo *IdxVNI = 0; |
| 497 | for (MBBValueMap::iterator I = DomValue.begin(), E = DomValue.end(); I != E; |
| 498 | ++I) { |
| 499 | MachineBasicBlock *MBB = I->first; |
| 500 | VNInfo *VNI = I->second; |
| 501 | SlotIndex Start = lis_.getMBBStartIdx(MBB); |
| 502 | if (MBB == IdxMBB) { |
| 503 | // Don't add full liveness to IdxMBB, stop at Idx. |
| 504 | if (Start != Idx) |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 505 | li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI)); |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 506 | // 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] | 507 | IdxVNI = VNI; |
| 508 | } else |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 509 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | assert(IdxVNI && "Didn't find value for Idx"); |
| 513 | return IdxVNI; |
| 514 | } |
| 515 | |
| 516 | // extendTo - Find the last li_ value defined in MBB at or before Idx. The |
| 517 | // parentli_ is assumed to be live at Idx. Extend the live range to Idx. |
| 518 | // Return the found VNInfo, or NULL. |
| 519 | VNInfo *LiveIntervalMap::extendTo(MachineBasicBlock *MBB, SlotIndex Idx) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 520 | assert(li_ && "call reset first"); |
| 521 | LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx); |
| 522 | if (I == li_->begin()) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | --I; |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 525 | if (I->end <= lis_.getMBBStartIdx(MBB)) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 526 | return 0; |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 527 | if (I->end <= Idx) |
| 528 | I->end = Idx.getNextSlot(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 529 | return I->valno; |
| 530 | } |
| 531 | |
| 532 | // addSimpleRange - Add a simple range from parentli_ to li_. |
| 533 | // ParentVNI must be live in the [Start;End) interval. |
| 534 | void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End, |
| 535 | const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 536 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 537 | bool simple; |
| 538 | VNInfo *VNI = mapValue(ParentVNI, Start, &simple); |
| 539 | // A simple mapping is easy. |
| 540 | if (simple) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 541 | li_->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 542 | return; |
| 543 | } |
| 544 | |
| 545 | // ParentVNI is a complex value. We must map per MBB. |
| 546 | MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start); |
Jakob Stoklund Olesen | dbc3609 | 2010-10-05 22:19:29 +0000 | [diff] [blame] | 547 | MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot()); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 548 | |
| 549 | if (MBB == MBBE) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 550 | li_->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | |
| 554 | // First block. |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 555 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 556 | |
| 557 | // Run sequence of full blocks. |
| 558 | for (++MBB; MBB != MBBE; ++MBB) { |
| 559 | Start = lis_.getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 560 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), |
| 561 | mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // Final block. |
| 565 | Start = lis_.getMBBStartIdx(MBB); |
| 566 | if (Start != End) |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 567 | li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_. |
| 571 | /// All needed values whose def is not inside [Start;End) must be defined |
| 572 | /// beforehand so mapValue will work. |
| 573 | void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 574 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 575 | LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end(); |
| 576 | LiveInterval::const_iterator I = std::lower_bound(B, E, Start); |
| 577 | |
| 578 | // Check if --I begins before Start and overlaps. |
| 579 | if (I != B) { |
| 580 | --I; |
| 581 | if (I->end > Start) |
| 582 | addSimpleRange(Start, std::min(End, I->end), I->valno); |
| 583 | ++I; |
| 584 | } |
| 585 | |
| 586 | // The remaining ranges begin after Start. |
| 587 | for (;I != E && I->start < End; ++I) |
| 588 | addSimpleRange(I->start, std::min(End, I->end), I->valno); |
| 589 | } |
| 590 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 591 | VNInfo *LiveIntervalMap::defByCopyFrom(unsigned Reg, |
| 592 | const VNInfo *ParentVNI, |
| 593 | MachineBasicBlock &MBB, |
| 594 | MachineBasicBlock::iterator I) { |
| 595 | const TargetInstrDesc &TID = MBB.getParent()->getTarget().getInstrInfo()-> |
| 596 | get(TargetOpcode::COPY); |
| 597 | MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), TID, li_->reg).addReg(Reg); |
| 598 | SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
| 599 | VNInfo *VNI = defValue(ParentVNI, DefIdx); |
| 600 | VNI->setCopy(MI); |
| 601 | li_->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), VNI)); |
| 602 | return VNI; |
| 603 | } |
| 604 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 605 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 606 | // Split Editor |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | |
| 609 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 610 | SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm, |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 611 | SmallVectorImpl<LiveInterval*> &intervals) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 612 | : sa_(sa), lis_(lis), vrm_(vrm), |
| 613 | mri_(vrm.getMachineFunction().getRegInfo()), |
| 614 | tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 615 | curli_(sa_.getCurLI()), |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 616 | dupli_(lis_, *curli_), |
| 617 | openli_(lis_, *curli_), |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 618 | intervals_(intervals), |
| 619 | firstInterval(intervals_.size()) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 620 | { |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 621 | assert(curli_ && "SplitEditor created from empty SplitAnalysis"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 622 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 623 | // Make sure curli_ is assigned a stack slot, so all our intervals get the |
| 624 | // same slot as curli_. |
| 625 | if (vrm_.getStackSlot(curli_->reg) == VirtRegMap::NO_STACK_SLOT) |
| 626 | vrm_.assignVirt2StackSlot(curli_->reg); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 627 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | LiveInterval *SplitEditor::createInterval() { |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 631 | unsigned Reg = mri_.createVirtualRegister(mri_.getRegClass(curli_->reg)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 632 | LiveInterval &Intv = lis_.getOrCreateInterval(Reg); |
| 633 | vrm_.grow(); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 634 | vrm_.assignVirt2StackSlot(Reg, vrm_.getStackSlot(curli_->reg)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 635 | return &Intv; |
| 636 | } |
| 637 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 638 | bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const { |
| 639 | for (int i = firstInterval, e = intervals_.size(); i != e; ++i) |
| 640 | if (intervals_[i]->liveAt(Idx)) |
| 641 | return true; |
| 642 | return false; |
| 643 | } |
| 644 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 645 | /// Create a new virtual register and live interval. |
| 646 | void SplitEditor::openIntv() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 647 | assert(!openli_.getLI() && "Previous LI not closed before openIntv"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 648 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 649 | if (!dupli_.getLI()) |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 650 | dupli_.reset(createInterval()); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 651 | |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 652 | openli_.reset(createInterval()); |
| 653 | intervals_.push_back(openli_.getLI()); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 656 | /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is |
| 657 | /// not live before Idx, a COPY is not inserted. |
| 658 | void SplitEditor::enterIntvBefore(SlotIndex Idx) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 659 | assert(openli_.getLI() && "openIntv not called before enterIntvBefore"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 660 | VNInfo *ParentVNI = curli_->getVNInfoAt(Idx.getUseIndex()); |
| 661 | if (!ParentVNI) { |
| 662 | DEBUG(dbgs() << " enterIntvBefore " << Idx << ": not live\n"); |
| 663 | return; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 664 | } |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 665 | truncatedValues.insert(ParentVNI); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 666 | MachineInstr *MI = lis_.getInstructionFromIndex(Idx); |
| 667 | assert(MI && "enterIntvBefore called with invalid index"); |
| 668 | openli_.defByCopyFrom(curli_->reg, ParentVNI, *MI->getParent(), MI); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 669 | DEBUG(dbgs() << " enterIntvBefore " << Idx << ": " << *openli_.getLI() |
| 670 | << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 673 | /// enterIntvAtEnd - Enter openli at the end of MBB. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 674 | void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 675 | assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 676 | SlotIndex End = lis_.getMBBEndIdx(&MBB); |
| 677 | VNInfo *ParentVNI = curli_->getVNInfoAt(End.getPrevSlot()); |
| 678 | if (!ParentVNI) { |
| 679 | DEBUG(dbgs() << " enterIntvAtEnd " << End << ": not live\n"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 680 | return; |
| 681 | } |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 682 | truncatedValues.insert(ParentVNI); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 683 | VNInfo *VNI = openli_.defByCopyFrom(curli_->reg, ParentVNI, |
| 684 | MBB, MBB.getFirstTerminator()); |
| 685 | // Make sure openli is live out of MBB. |
| 686 | openli_.getLI()->addRange(LiveRange(VNI->def, End, VNI)); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 687 | DEBUG(dbgs() << " enterIntvAtEnd: " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 690 | /// useIntv - indicate that all instructions in MBB should use openli. |
| 691 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
| 692 | useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 695 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 696 | assert(openli_.getLI() && "openIntv not called before useIntv"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 697 | openli_.addRange(Start, End); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 698 | DEBUG(dbgs() << " use [" << Start << ';' << End << "): " |
| 699 | << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 702 | /// leaveIntvAfter - Leave openli after the instruction at Idx. |
| 703 | void SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 704 | assert(openli_.getLI() && "openIntv not called before leaveIntvAfter"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 705 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 706 | // The interval must be live beyond the instruction at Idx. |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 707 | VNInfo *ParentVNI = curli_->getVNInfoAt(Idx.getBoundaryIndex()); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 708 | if (!ParentVNI) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 709 | DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": not live\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 710 | return; |
| 711 | } |
| 712 | |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 713 | MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx); |
| 714 | MachineBasicBlock *MBB = MII->getParent(); |
| 715 | VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI, *MBB, |
| 716 | llvm::next(MII)); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +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 Idx to the |
| 719 | // new copy. |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 720 | openli_.addSimpleRange(Idx.getBoundaryIndex(), VNI->def, ParentVNI); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 721 | DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": " << *openli_.getLI() |
| 722 | << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 725 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
| 726 | /// Currently, only one value can leave the interval. |
| 727 | void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 728 | assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 729 | |
| 730 | SlotIndex Start = lis_.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 731 | VNInfo *ParentVNI = curli_->getVNInfoAt(Start); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 732 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 733 | // Is curli even live-in to MBB? |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 734 | if (!ParentVNI) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 735 | DEBUG(dbgs() << " leaveIntvAtTop at " << Start << ": not live\n"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 739 | // 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] | 740 | VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI, |
| 741 | MBB, MBB.begin()); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 742 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 743 | // Finally we must make sure that openli is properly extended from Start to |
| 744 | // the new copy. |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 745 | openli_.addSimpleRange(Start, VNI->def, ParentVNI); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 746 | DEBUG(dbgs() << " leaveIntvAtTop at " << Start << ": " << *openli_.getLI() |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 747 | << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 750 | /// closeIntv - Indicate that we are done editing the currently open |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 751 | /// LiveInterval, and ranges can be trimmed. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 752 | void SplitEditor::closeIntv() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 753 | assert(openli_.getLI() && "openIntv not called before closeIntv"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 754 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 755 | DEBUG(dbgs() << " closeIntv cleaning up\n"); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 756 | DEBUG(dbgs() << " open " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 757 | openli_.reset(0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 760 | void |
| 761 | SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) { |
| 762 | SlotIndex sidx = Start; |
| 763 | |
| 764 | // Break [Start;End) into segments that don't overlap any intervals. |
| 765 | for (;;) { |
| 766 | SlotIndex next = sidx, eidx = End; |
| 767 | // Find overlapping intervals. |
| 768 | for (int i = firstInterval, e = intervals_.size(); i != e && sidx < eidx; |
| 769 | ++i) { |
| 770 | LiveInterval::const_iterator I = intervals_[i]->find(sidx); |
| 771 | LiveInterval::const_iterator E = intervals_[i]->end(); |
| 772 | if (I == E) |
| 773 | continue; |
| 774 | // Interval I is overlapping [sidx;eidx). Trim sidx. |
| 775 | if (I->start <= sidx) { |
| 776 | sidx = I->end; |
| 777 | if (++I == E) |
| 778 | continue; |
| 779 | } |
| 780 | // Trim eidx too if needed. |
| 781 | if (I->start >= eidx) |
| 782 | continue; |
| 783 | eidx = I->start; |
| 784 | if (I->end > next) |
| 785 | next = I->end; |
| 786 | } |
| 787 | // Now, [sidx;eidx) doesn't overlap anything in intervals_. |
| 788 | if (sidx < eidx) |
| 789 | dupli_.addSimpleRange(sidx, eidx, VNI); |
| 790 | // If the interval end was truncated, we can try again from next. |
| 791 | if (next <= sidx) |
| 792 | break; |
| 793 | sidx = next; |
| 794 | } |
| 795 | } |
| 796 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 797 | /// rewrite - after all the new live ranges have been created, rewrite |
| 798 | /// instructions using curli to use the new intervals. |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 799 | void SplitEditor::rewrite() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 800 | assert(!openli_.getLI() && "Previous LI not closed before rewrite"); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 801 | |
| 802 | // First we need to fill in the live ranges in dupli. |
| 803 | // If values were redefined, we need a full recoloring with SSA update. |
| 804 | // If values were truncated, we only need to truncate the ranges. |
| 805 | // If values were partially rematted, we should shrink to uses. |
| 806 | // If values were fully rematted, they should be omitted. |
| 807 | // FIXME: If a single value is redefined, just move the def and truncate. |
| 808 | |
| 809 | // Values that are fully contained in the split intervals. |
| 810 | SmallPtrSet<const VNInfo*, 8> deadValues; |
| 811 | |
| 812 | // Map all curli values that should have live defs in dupli. |
| 813 | for (LiveInterval::const_vni_iterator I = curli_->vni_begin(), |
| 814 | E = curli_->vni_end(); I != E; ++I) { |
| 815 | const VNInfo *VNI = *I; |
| 816 | // Original def is contained in the split intervals. |
| 817 | if (intervalsLiveAt(VNI->def)) { |
| 818 | // Did this value escape? |
| 819 | if (dupli_.isMapped(VNI)) |
| 820 | truncatedValues.insert(VNI); |
| 821 | else |
| 822 | deadValues.insert(VNI); |
| 823 | continue; |
| 824 | } |
| 825 | // Add minimal live range at the definition. |
| 826 | VNInfo *DVNI = dupli_.defValue(VNI, VNI->def); |
| 827 | dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI)); |
| 828 | } |
| 829 | |
| 830 | // Add all ranges to dupli. |
| 831 | for (LiveInterval::const_iterator I = curli_->begin(), E = curli_->end(); |
| 832 | I != E; ++I) { |
| 833 | const LiveRange &LR = *I; |
| 834 | if (truncatedValues.count(LR.valno)) { |
| 835 | // recolor after removing intervals_. |
| 836 | addTruncSimpleRange(LR.start, LR.end, LR.valno); |
| 837 | } else if (!deadValues.count(LR.valno)) { |
| 838 | // recolor without truncation. |
| 839 | dupli_.addSimpleRange(LR.start, LR.end, LR.valno); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 844 | const LiveInterval *curli = sa_.getCurLI(); |
| 845 | for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(curli->reg), |
| 846 | RE = mri_.reg_end(); RI != RE;) { |
| 847 | MachineOperand &MO = RI.getOperand(); |
| 848 | MachineInstr *MI = MO.getParent(); |
| 849 | ++RI; |
| 850 | if (MI->isDebugValue()) { |
| 851 | DEBUG(dbgs() << "Zapping " << *MI); |
| 852 | // FIXME: We can do much better with debug values. |
| 853 | MO.setReg(0); |
| 854 | continue; |
| 855 | } |
| 856 | SlotIndex Idx = lis_.getInstructionIndex(MI); |
| 857 | Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 858 | LiveInterval *LI = dupli_.getLI(); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 859 | for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) { |
| 860 | LiveInterval *testli = intervals_[i]; |
| 861 | if (testli->liveAt(Idx)) { |
| 862 | LI = testli; |
| 863 | break; |
| 864 | } |
| 865 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 866 | if (LI) { |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 867 | MO.setReg(LI->reg); |
Jakob Stoklund Olesen | 00667a5 | 2010-08-13 01:05:23 +0000 | [diff] [blame] | 868 | sa_.removeUse(MI); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 869 | DEBUG(dbgs() << " rewrite " << Idx << '\t' << *MI); |
| 870 | } |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | // dupli_ goes in last, after rewriting. |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 874 | if (dupli_.getLI()) { |
| 875 | if (dupli_.getLI()->empty()) { |
Jakob Stoklund Olesen | 09c45d2 | 2010-08-12 23:02:57 +0000 | [diff] [blame] | 876 | DEBUG(dbgs() << " dupli became empty?\n"); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 877 | lis_.removeInterval(dupli_.getLI()->reg); |
| 878 | dupli_.reset(0); |
Jakob Stoklund Olesen | 09c45d2 | 2010-08-12 23:02:57 +0000 | [diff] [blame] | 879 | } else { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 880 | dupli_.getLI()->RenumberValues(lis_); |
| 881 | intervals_.push_back(dupli_.getLI()); |
Jakob Stoklund Olesen | 09c45d2 | 2010-08-12 23:02:57 +0000 | [diff] [blame] | 882 | } |
Jakob Stoklund Olesen | 2343659 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 883 | } |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 884 | |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 885 | // Calculate spill weight and allocation hints for new intervals. |
| 886 | VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_); |
| 887 | for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) { |
| 888 | LiveInterval &li = *intervals_[i]; |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 889 | vrai.CalculateRegClass(li.reg); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 890 | vrai.CalculateWeightAndHint(li); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 891 | DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName() |
| 892 | << ":" << li << '\n'); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 893 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 897 | //===----------------------------------------------------------------------===// |
| 898 | // Loop Splitting |
| 899 | //===----------------------------------------------------------------------===// |
| 900 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 901 | void SplitEditor::splitAroundLoop(const MachineLoop *Loop) { |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 902 | SplitAnalysis::LoopBlocks Blocks; |
| 903 | sa_.getLoopBlocks(Loop, Blocks); |
| 904 | |
| 905 | // Break critical edges as needed. |
| 906 | SplitAnalysis::BlockPtrSet CriticalExits; |
| 907 | sa_.getCriticalExits(Blocks, CriticalExits); |
| 908 | assert(CriticalExits.empty() && "Cannot break critical exits yet"); |
| 909 | |
| 910 | // Create new live interval for the loop. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 911 | openIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 912 | |
| 913 | // Insert copies in the predecessors. |
| 914 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(), |
| 915 | E = Blocks.Preds.end(); I != E; ++I) { |
| 916 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 917 | enterIntvAtEnd(MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | // Switch all loop blocks. |
| 921 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(), |
| 922 | E = Blocks.Loop.end(); I != E; ++I) |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 923 | useIntv(**I); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 924 | |
| 925 | // Insert back copies in the exit blocks. |
| 926 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(), |
| 927 | E = Blocks.Exits.end(); I != E; ++I) { |
| 928 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 929 | leaveIntvAtTop(MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // Done. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 933 | closeIntv(); |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 934 | rewrite(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 937 | |
| 938 | //===----------------------------------------------------------------------===// |
| 939 | // Single Block Splitting |
| 940 | //===----------------------------------------------------------------------===// |
| 941 | |
| 942 | /// splitSingleBlocks - Split curli into a separate live interval inside each |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 943 | /// basic block in Blocks. |
| 944 | void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 945 | DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 946 | // Determine the first and last instruction using curli in each block. |
| 947 | typedef std::pair<SlotIndex,SlotIndex> IndexPair; |
| 948 | typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap; |
| 949 | IndexPairMap MBBRange; |
| 950 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 951 | E = sa_.usingInstrs_.end(); I != E; ++I) { |
| 952 | const MachineBasicBlock *MBB = (*I)->getParent(); |
| 953 | if (!Blocks.count(MBB)) |
| 954 | continue; |
| 955 | SlotIndex Idx = lis_.getInstructionIndex(*I); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 956 | DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 957 | IndexPair &IP = MBBRange[MBB]; |
| 958 | if (!IP.first.isValid() || Idx < IP.first) |
| 959 | IP.first = Idx; |
| 960 | if (!IP.second.isValid() || Idx > IP.second) |
| 961 | IP.second = Idx; |
| 962 | } |
| 963 | |
| 964 | // Create a new interval for each block. |
| 965 | for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(), |
| 966 | E = Blocks.end(); I != E; ++I) { |
| 967 | IndexPair &IP = MBBRange[*I]; |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 968 | DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": [" |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 969 | << IP.first << ';' << IP.second << ")\n"); |
| 970 | assert(IP.first.isValid() && IP.second.isValid()); |
| 971 | |
| 972 | openIntv(); |
| 973 | enterIntvBefore(IP.first); |
| 974 | useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex()); |
| 975 | leaveIntvAfter(IP.second); |
| 976 | closeIntv(); |
| 977 | } |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 978 | rewrite(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 981 | |
| 982 | //===----------------------------------------------------------------------===// |
| 983 | // Sub Block Splitting |
| 984 | //===----------------------------------------------------------------------===// |
| 985 | |
| 986 | /// getBlockForInsideSplit - If curli is contained inside a single basic block, |
| 987 | /// and it wou pay to subdivide the interval inside that block, return it. |
| 988 | /// Otherwise return NULL. The returned block can be passed to |
| 989 | /// SplitEditor::splitInsideBlock. |
| 990 | const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() { |
| 991 | // The interval must be exclusive to one block. |
| 992 | if (usingBlocks_.size() != 1) |
| 993 | return 0; |
| 994 | // Don't to this for less than 4 instructions. We want to be sure that |
| 995 | // splitting actually reduces the instruction count per interval. |
| 996 | if (usingInstrs_.size() < 4) |
| 997 | return 0; |
| 998 | return usingBlocks_.begin()->first; |
| 999 | } |
| 1000 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1001 | /// splitInsideBlock - Split curli into multiple intervals inside MBB. |
| 1002 | void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1003 | SmallVector<SlotIndex, 32> Uses; |
| 1004 | Uses.reserve(sa_.usingInstrs_.size()); |
| 1005 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 1006 | E = sa_.usingInstrs_.end(); I != E; ++I) |
| 1007 | if ((*I)->getParent() == MBB) |
| 1008 | Uses.push_back(lis_.getInstructionIndex(*I)); |
| 1009 | DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for " |
| 1010 | << Uses.size() << " instructions.\n"); |
| 1011 | assert(Uses.size() >= 3 && "Need at least 3 instructions"); |
| 1012 | array_pod_sort(Uses.begin(), Uses.end()); |
| 1013 | |
| 1014 | // Simple algorithm: Find the largest gap between uses as determined by slot |
| 1015 | // indices. Create new intervals for instructions before the gap and after the |
| 1016 | // gap. |
| 1017 | unsigned bestPos = 0; |
| 1018 | int bestGap = 0; |
| 1019 | DEBUG(dbgs() << " dist (" << Uses[0]); |
| 1020 | for (unsigned i = 1, e = Uses.size(); i != e; ++i) { |
| 1021 | int g = Uses[i-1].distance(Uses[i]); |
| 1022 | DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]); |
| 1023 | if (g > bestGap) |
| 1024 | bestPos = i, bestGap = g; |
| 1025 | } |
| 1026 | DEBUG(dbgs() << "), best: -" << bestGap << "-\n"); |
| 1027 | |
| 1028 | // bestPos points to the first use after the best gap. |
| 1029 | assert(bestPos > 0 && "Invalid gap"); |
| 1030 | |
| 1031 | // FIXME: Don't create intervals for low densities. |
| 1032 | |
| 1033 | // First interval before the gap. Don't create single-instr intervals. |
| 1034 | if (bestPos > 1) { |
| 1035 | openIntv(); |
| 1036 | enterIntvBefore(Uses.front()); |
| 1037 | useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex()); |
| 1038 | leaveIntvAfter(Uses[bestPos-1]); |
| 1039 | closeIntv(); |
| 1040 | } |
| 1041 | |
| 1042 | // Second interval after the gap. |
| 1043 | if (bestPos < Uses.size()-1) { |
| 1044 | openIntv(); |
| 1045 | enterIntvBefore(Uses[bestPos]); |
| 1046 | useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex()); |
| 1047 | leaveIntvAfter(Uses.back()); |
| 1048 | closeIntv(); |
| 1049 | } |
| 1050 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1051 | rewrite(); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1052 | } |