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