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" |
| 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 32 | static cl::opt<bool> |
| 33 | AllowSplit("spiller-splits-edges", |
| 34 | cl::desc("Allow critical edge splitting during spilling")); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Split Analysis |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 40 | SplitAnalysis::SplitAnalysis(const MachineFunction &mf, |
| 41 | const LiveIntervals &lis, |
| 42 | const MachineLoopInfo &mli) |
| 43 | : mf_(mf), |
| 44 | lis_(lis), |
| 45 | loops_(mli), |
| 46 | tii_(*mf.getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 47 | curli_(0) {} |
| 48 | |
| 49 | void SplitAnalysis::clear() { |
| 50 | usingInstrs_.clear(); |
| 51 | usingBlocks_.clear(); |
| 52 | usingLoops_.clear(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 53 | curli_ = 0; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 56 | bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) { |
| 57 | MachineBasicBlock *T, *F; |
| 58 | SmallVector<MachineOperand, 4> Cond; |
| 59 | return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond); |
| 60 | } |
| 61 | |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 62 | /// analyzeUses - Count instructions, basic blocks, and loops using curli. |
| 63 | void SplitAnalysis::analyzeUses() { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 64 | const MachineRegisterInfo &MRI = mf_.getRegInfo(); |
| 65 | for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg); |
| 66 | MachineInstr *MI = I.skipInstruction();) { |
| 67 | if (MI->isDebugValue() || !usingInstrs_.insert(MI)) |
| 68 | continue; |
| 69 | MachineBasicBlock *MBB = MI->getParent(); |
| 70 | if (usingBlocks_[MBB]++) |
| 71 | continue; |
| 72 | if (MachineLoop *Loop = loops_.getLoopFor(MBB)) |
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 | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 81 | /// removeUse - Update statistics by noting that MI no longer uses curli. |
| 82 | void SplitAnalysis::removeUse(const MachineInstr *MI) { |
| 83 | if (!usingInstrs_.erase(MI)) |
| 84 | return; |
| 85 | |
| 86 | // Decrement MBB count. |
| 87 | const MachineBasicBlock *MBB = MI->getParent(); |
| 88 | BlockCountMap::iterator bi = usingBlocks_.find(MBB); |
| 89 | assert(bi != usingBlocks_.end() && "MBB missing"); |
| 90 | assert(bi->second && "0 count in map"); |
| 91 | if (--bi->second) |
| 92 | return; |
| 93 | // No more uses in MBB. |
| 94 | usingBlocks_.erase(bi); |
| 95 | |
| 96 | // Decrement loop count. |
| 97 | MachineLoop *Loop = loops_.getLoopFor(MBB); |
| 98 | if (!Loop) |
| 99 | return; |
| 100 | LoopCountMap::iterator li = usingLoops_.find(Loop); |
| 101 | assert(li != usingLoops_.end() && "Loop missing"); |
| 102 | assert(li->second && "0 count in map"); |
| 103 | if (--li->second) |
| 104 | return; |
| 105 | // No more blocks in Loop. |
| 106 | usingLoops_.erase(li); |
| 107 | } |
| 108 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 109 | // Get three sets of basic blocks surrounding a loop: Blocks inside the loop, |
| 110 | // predecessor blocks, and exit blocks. |
| 111 | void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) { |
| 112 | Blocks.clear(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 113 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 114 | // Blocks in the loop. |
| 115 | Blocks.Loop.insert(Loop->block_begin(), Loop->block_end()); |
| 116 | |
| 117 | // Predecessor blocks. |
| 118 | const MachineBasicBlock *Header = Loop->getHeader(); |
| 119 | for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(), |
| 120 | E = Header->pred_end(); I != E; ++I) |
| 121 | if (!Blocks.Loop.count(*I)) |
| 122 | Blocks.Preds.insert(*I); |
| 123 | |
| 124 | // Exit blocks. |
| 125 | for (MachineLoop::block_iterator I = Loop->block_begin(), |
| 126 | E = Loop->block_end(); I != E; ++I) { |
| 127 | const MachineBasicBlock *MBB = *I; |
| 128 | for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(), |
| 129 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 130 | if (!Blocks.Loop.count(*SI)) |
| 131 | Blocks.Exits.insert(*SI); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in |
| 136 | /// and around the Loop. |
| 137 | SplitAnalysis::LoopPeripheralUse SplitAnalysis:: |
| 138 | analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 139 | LoopPeripheralUse use = ContainedInLoop; |
| 140 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 141 | I != E; ++I) { |
| 142 | const MachineBasicBlock *MBB = I->first; |
| 143 | // Is this a peripheral block? |
| 144 | if (use < MultiPeripheral && |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 145 | (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 146 | if (I->second > 1) use = MultiPeripheral; |
| 147 | else use = SinglePeripheral; |
| 148 | continue; |
| 149 | } |
| 150 | // Is it a loop block? |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 151 | if (Blocks.Loop.count(MBB)) |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 152 | continue; |
| 153 | // It must be an unrelated block. |
| 154 | return OutsideLoop; |
| 155 | } |
| 156 | return use; |
| 157 | } |
| 158 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 159 | /// getCriticalExits - It may be necessary to partially break critical edges |
| 160 | /// leaving the loop if an exit block has phi uses of curli. Collect the exit |
| 161 | /// blocks that need special treatment into CriticalExits. |
| 162 | void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks, |
| 163 | BlockPtrSet &CriticalExits) { |
| 164 | CriticalExits.clear(); |
| 165 | |
| 166 | // A critical exit block contains a phi def of curli, and has a predecessor |
| 167 | // that is not in the loop nor a loop predecessor. |
| 168 | // For such an exit block, the edges carrying the new variable must be moved |
| 169 | // to a new pre-exit block. |
| 170 | for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end(); |
| 171 | I != E; ++I) { |
| 172 | const MachineBasicBlock *Succ = *I; |
| 173 | SlotIndex SuccIdx = lis_.getMBBStartIdx(Succ); |
| 174 | VNInfo *SuccVNI = curli_->getVNInfoAt(SuccIdx); |
| 175 | // This exit may not have curli live in at all. No need to split. |
| 176 | if (!SuccVNI) |
| 177 | continue; |
| 178 | // If this is not a PHI def, it is either using a value from before the |
| 179 | // loop, or a value defined inside the loop. Both are safe. |
| 180 | if (!SuccVNI->isPHIDef() || SuccVNI->def.getBaseIndex() != SuccIdx) |
| 181 | continue; |
| 182 | // This exit block does have a PHI. Does it also have a predecessor that is |
| 183 | // not a loop block or loop predecessor? |
| 184 | for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(), |
| 185 | PE = Succ->pred_end(); PI != PE; ++PI) { |
| 186 | const MachineBasicBlock *Pred = *PI; |
| 187 | if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred)) |
| 188 | continue; |
| 189 | // This is a critical exit block, and we need to split the exit edge. |
| 190 | CriticalExits.insert(Succ); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /// canSplitCriticalExits - Return true if it is possible to insert new exit |
| 197 | /// blocks before the blocks in CriticalExits. |
| 198 | bool |
| 199 | SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks, |
| 200 | BlockPtrSet &CriticalExits) { |
| 201 | // If we don't allow critical edge splitting, require no critical exits. |
| 202 | if (!AllowSplit) |
| 203 | return CriticalExits.empty(); |
| 204 | |
| 205 | for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end(); |
| 206 | I != E; ++I) { |
| 207 | const MachineBasicBlock *Succ = *I; |
| 208 | // We want to insert a new pre-exit MBB before Succ, and change all the |
| 209 | // in-loop blocks to branch to the pre-exit instead of Succ. |
| 210 | // Check that all the in-loop predecessors can be changed. |
| 211 | for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(), |
| 212 | PE = Succ->pred_end(); PI != PE; ++PI) { |
| 213 | const MachineBasicBlock *Pred = *PI; |
| 214 | // The external predecessors won't be altered. |
| 215 | if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred)) |
| 216 | continue; |
| 217 | if (!canAnalyzeBranch(Pred)) |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | // If Succ's layout predecessor falls through, that too must be analyzable. |
| 222 | // We need to insert the pre-exit block in the gap. |
| 223 | MachineFunction::const_iterator MFI = Succ; |
| 224 | if (MFI == mf_.begin()) |
| 225 | continue; |
| 226 | if (!canAnalyzeBranch(--MFI)) |
| 227 | return false; |
| 228 | } |
| 229 | // No problems found. |
| 230 | return true; |
| 231 | } |
| 232 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 233 | void SplitAnalysis::analyze(const LiveInterval *li) { |
| 234 | clear(); |
| 235 | curli_ = li; |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 236 | analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | const MachineLoop *SplitAnalysis::getBestSplitLoop() { |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 240 | assert(curli_ && "Call analyze() before getBestSplitLoop"); |
| 241 | if (usingLoops_.empty()) |
| 242 | return 0; |
| 243 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 244 | LoopPtrSet Loops, SecondLoops; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 245 | LoopBlocks Blocks; |
| 246 | BlockPtrSet CriticalExits; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 247 | |
| 248 | // Find first-class and second class candidate loops. |
| 249 | // 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^] | 250 | for (LoopCountMap::const_iterator I = usingLoops_.begin(), |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 251 | E = usingLoops_.end(); I != E; ++I) { |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 252 | const MachineLoop *Loop = I->first; |
| 253 | getLoopBlocks(Loop, Blocks); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 254 | |
| 255 | // FIXME: We need an SSA updater to properly handle multiple exit blocks. |
| 256 | if (Blocks.Exits.size() > 1) { |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 257 | DEBUG(dbgs() << " multiple exits from " << *Loop); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 261 | LoopPtrSet *LPS = 0; |
| 262 | switch(analyzeLoopPeripheralUse(Blocks)) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 263 | case OutsideLoop: |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 264 | LPS = &Loops; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 265 | break; |
| 266 | case MultiPeripheral: |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 267 | LPS = &SecondLoops; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 268 | break; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 269 | case ContainedInLoop: |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 270 | DEBUG(dbgs() << " contained in " << *Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 271 | continue; |
| 272 | case SinglePeripheral: |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 273 | DEBUG(dbgs() << " single peripheral use in " << *Loop); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 274 | continue; |
| 275 | } |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 276 | // Will it be possible to split around this loop? |
| 277 | getCriticalExits(Blocks, CriticalExits); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 278 | DEBUG(dbgs() << " " << CriticalExits.size() << " critical exits from " |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 279 | << *Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 280 | if (!canSplitCriticalExits(Blocks, CriticalExits)) |
| 281 | continue; |
| 282 | // This is a possible split. |
| 283 | assert(LPS); |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame^] | 284 | LPS->insert(Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 287 | DEBUG(dbgs() << " getBestSplitLoop found " << Loops.size() << " + " |
| 288 | << SecondLoops.size() << " candidate loops.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 289 | |
| 290 | // If there are no first class loops available, look at second class loops. |
| 291 | if (Loops.empty()) |
| 292 | Loops = SecondLoops; |
| 293 | |
| 294 | if (Loops.empty()) |
| 295 | return 0; |
| 296 | |
| 297 | // Pick the earliest loop. |
| 298 | // FIXME: Are there other heuristics to consider? |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 299 | const MachineLoop *Best = 0; |
| 300 | SlotIndex BestIdx; |
| 301 | for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E; |
| 302 | ++I) { |
| 303 | SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader()); |
| 304 | if (!Best || Idx < BestIdx) |
| 305 | Best = *I, BestIdx = Idx; |
| 306 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 307 | DEBUG(dbgs() << " getBestSplitLoop found " << *Best); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 308 | return Best; |
| 309 | } |
| 310 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 311 | /// getMultiUseBlocks - if curli has more than one use in a basic block, it |
| 312 | /// may be an advantage to split curli for the duration of the block. |
| 313 | bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { |
| 314 | // If curli is local to one block, there is no point to splitting it. |
| 315 | if (usingBlocks_.size() <= 1) |
| 316 | return false; |
| 317 | // Add blocks with multiple uses. |
| 318 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 319 | I != E; ++I) |
| 320 | switch (I->second) { |
| 321 | case 0: |
| 322 | case 1: |
| 323 | continue; |
| 324 | case 2: { |
| 325 | // It doesn't pay to split a 2-instr block if it redefines curli. |
| 326 | VNInfo *VN1 = curli_->getVNInfoAt(lis_.getMBBStartIdx(I->first)); |
| 327 | VNInfo *VN2 = |
| 328 | curli_->getVNInfoAt(lis_.getMBBEndIdx(I->first).getPrevIndex()); |
| 329 | // live-in and live-out with a different value. |
| 330 | if (VN1 && VN2 && VN1 != VN2) |
| 331 | continue; |
| 332 | } // Fall through. |
| 333 | default: |
| 334 | Blocks.insert(I->first); |
| 335 | } |
| 336 | return !Blocks.empty(); |
| 337 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 338 | |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | // Split Editor |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
| 343 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 344 | SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm, |
| 345 | std::vector<LiveInterval*> &intervals) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 346 | : sa_(sa), lis_(lis), vrm_(vrm), |
| 347 | mri_(vrm.getMachineFunction().getRegInfo()), |
| 348 | tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 349 | curli_(sa_.getCurLI()), |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 350 | dupli_(0), openli_(0), |
| 351 | intervals_(intervals), |
| 352 | firstInterval(intervals_.size()) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 353 | { |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 354 | assert(curli_ && "SplitEditor created from empty SplitAnalysis"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 355 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 356 | // Make sure curli_ is assigned a stack slot, so all our intervals get the |
| 357 | // same slot as curli_. |
| 358 | if (vrm_.getStackSlot(curli_->reg) == VirtRegMap::NO_STACK_SLOT) |
| 359 | vrm_.assignVirt2StackSlot(curli_->reg); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 360 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | LiveInterval *SplitEditor::createInterval() { |
| 364 | unsigned curli = sa_.getCurLI()->reg; |
| 365 | unsigned Reg = mri_.createVirtualRegister(mri_.getRegClass(curli)); |
| 366 | LiveInterval &Intv = lis_.getOrCreateInterval(Reg); |
| 367 | vrm_.grow(); |
| 368 | vrm_.assignVirt2StackSlot(Reg, vrm_.getStackSlot(curli)); |
| 369 | return &Intv; |
| 370 | } |
| 371 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 372 | LiveInterval *SplitEditor::getDupLI() { |
| 373 | if (!dupli_) { |
| 374 | // Create an interval for dupli that is a copy of curli. |
| 375 | dupli_ = createInterval(); |
| 376 | dupli_->Copy(*curli_, &mri_, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 377 | } |
| 378 | return dupli_; |
| 379 | } |
| 380 | |
| 381 | VNInfo *SplitEditor::mapValue(const VNInfo *curliVNI) { |
| 382 | VNInfo *&VNI = valueMap_[curliVNI]; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 383 | if (!VNI) |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 384 | VNI = openli_->createValueCopy(curliVNI, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 385 | return VNI; |
| 386 | } |
| 387 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 388 | /// Insert a COPY instruction curli -> li. Allocate a new value from li |
| 389 | /// defined by the COPY. Note that rewrite() will deal with the curli |
| 390 | /// register, so this function can be used to copy from any interval - openli, |
| 391 | /// curli, or dupli. |
| 392 | VNInfo *SplitEditor::insertCopy(LiveInterval &LI, |
| 393 | MachineBasicBlock &MBB, |
| 394 | MachineBasicBlock::iterator I) { |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 395 | MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY), |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 396 | LI.reg).addReg(curli_->reg); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 397 | SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
| 398 | return LI.getNextValue(DefIdx, MI, true, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 401 | /// Create a new virtual register and live interval. |
| 402 | void SplitEditor::openIntv() { |
| 403 | assert(!openli_ && "Previous LI not closed before openIntv"); |
| 404 | openli_ = createInterval(); |
| 405 | intervals_.push_back(openli_); |
| 406 | liveThrough_ = false; |
| 407 | } |
| 408 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 409 | /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is |
| 410 | /// not live before Idx, a COPY is not inserted. |
| 411 | void SplitEditor::enterIntvBefore(SlotIndex Idx) { |
| 412 | assert(openli_ && "openIntv not called before enterIntvBefore"); |
| 413 | |
| 414 | // Copy from curli_ if it is live. |
| 415 | if (VNInfo *CurVNI = curli_->getVNInfoAt(Idx.getUseIndex())) { |
| 416 | MachineInstr *MI = lis_.getInstructionFromIndex(Idx); |
| 417 | assert(MI && "enterIntvBefore called with invalid index"); |
| 418 | VNInfo *VNI = insertCopy(*openli_, *MI->getParent(), MI); |
| 419 | openli_->addRange(LiveRange(VNI->def, Idx.getDefIndex(), VNI)); |
| 420 | |
| 421 | // Make sure CurVNI is properly mapped. |
| 422 | VNInfo *&mapVNI = valueMap_[CurVNI]; |
| 423 | // We dont have SSA update yet, so only one entry per value is allowed. |
| 424 | assert(!mapVNI && "enterIntvBefore called more than once for the same value"); |
| 425 | mapVNI = VNI; |
| 426 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 427 | DEBUG(dbgs() << " enterIntvBefore " << Idx << ": " << *openli_ << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 430 | /// enterIntvAtEnd - Enter openli at the end of MBB. |
| 431 | /// PhiMBB is a successor inside openli where a PHI value is created. |
| 432 | /// Currently, all entries must share the same PhiMBB. |
| 433 | void SplitEditor::enterIntvAtEnd(MachineBasicBlock &A, MachineBasicBlock &B) { |
| 434 | assert(openli_ && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 435 | |
| 436 | SlotIndex EndA = lis_.getMBBEndIdx(&A); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 437 | VNInfo *CurVNIA = curli_->getVNInfoAt(EndA.getPrevIndex()); |
| 438 | if (!CurVNIA) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 439 | DEBUG(dbgs() << " enterIntvAtEnd, curli not live out of BB#" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 440 | << A.getNumber() << ".\n"); |
| 441 | return; |
| 442 | } |
| 443 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 444 | // Add a phi kill value and live range out of A. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 445 | VNInfo *VNIA = insertCopy(*openli_, A, A.getFirstTerminator()); |
| 446 | openli_->addRange(LiveRange(VNIA->def, EndA, VNIA)); |
| 447 | |
| 448 | // FIXME: If this is the only entry edge, we don't need the extra PHI value. |
| 449 | // FIXME: If there are multiple entry blocks (so not a loop), we need proper |
| 450 | // SSA update. |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 451 | |
| 452 | // Now look at the start of B. |
| 453 | SlotIndex StartB = lis_.getMBBStartIdx(&B); |
| 454 | SlotIndex EndB = lis_.getMBBEndIdx(&B); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 455 | const LiveRange *CurB = curli_->getLiveRangeContaining(StartB); |
| 456 | if (!CurB) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 457 | DEBUG(dbgs() << " enterIntvAtEnd: curli not live in to BB#" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 458 | << B.getNumber() << ".\n"); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | VNInfo *VNIB = openli_->getVNInfoAt(StartB); |
| 463 | if (!VNIB) { |
| 464 | // Create a phi value. |
| 465 | VNIB = openli_->getNextValue(SlotIndex(StartB, true), 0, false, |
| 466 | lis_.getVNInfoAllocator()); |
| 467 | VNIB->setIsPHIDef(true); |
| 468 | // Add a minimal range for the new value. |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 469 | openli_->addRange(LiveRange(VNIB->def, std::min(EndB, CurB->end), VNIB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 470 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 471 | VNInfo *&mapVNI = valueMap_[CurB->valno]; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 472 | if (mapVNI) { |
| 473 | // Multiple copies - must create PHI value. |
| 474 | abort(); |
| 475 | } else { |
| 476 | // This is the first copy of dupLR. Mark the mapping. |
| 477 | mapVNI = VNIB; |
| 478 | } |
| 479 | |
| 480 | } |
| 481 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 482 | DEBUG(dbgs() << " enterIntvAtEnd: " << *openli_ << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 485 | /// useIntv - indicate that all instructions in MBB should use openli. |
| 486 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
| 487 | useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 490 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
| 491 | assert(openli_ && "openIntv not called before useIntv"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 492 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 493 | // Map the curli values from the interval into openli_ |
| 494 | LiveInterval::const_iterator B = curli_->begin(), E = curli_->end(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 495 | LiveInterval::const_iterator I = std::lower_bound(B, E, Start); |
| 496 | |
| 497 | if (I != B) { |
| 498 | --I; |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 499 | // I begins before Start, but overlaps. openli may already have a value. |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 500 | if (I->end > Start && !openli_->liveAt(Start)) |
| 501 | openli_->addRange(LiveRange(Start, std::min(End, I->end), |
| 502 | mapValue(I->valno))); |
| 503 | ++I; |
| 504 | } |
| 505 | |
| 506 | // The remaining ranges begin after Start. |
| 507 | for (;I != E && I->start < End; ++I) |
| 508 | openli_->addRange(LiveRange(I->start, std::min(End, I->end), |
| 509 | mapValue(I->valno))); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 510 | DEBUG(dbgs() << " use [" << Start << ';' << End << "): " << *openli_ |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 511 | << '\n'); |
| 512 | } |
| 513 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 514 | /// leaveIntvAfter - Leave openli after the instruction at Idx. |
| 515 | void SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
| 516 | assert(openli_ && "openIntv not called before leaveIntvAfter"); |
| 517 | |
| 518 | const LiveRange *CurLR = curli_->getLiveRangeContaining(Idx.getDefIndex()); |
| 519 | if (!CurLR || CurLR->end <= Idx.getBoundaryIndex()) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 520 | DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": not live\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
| 524 | // Was this value of curli live through openli? |
| 525 | if (!openli_->liveAt(CurLR->valno->def)) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 526 | DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": using external value\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 527 | liveThrough_ = true; |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | // We are going to insert a back copy, so we must have a dupli_. |
| 532 | LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Idx.getDefIndex()); |
| 533 | assert(DupLR && "dupli not live into black, but curli is?"); |
| 534 | |
| 535 | // Insert the COPY instruction. |
| 536 | MachineBasicBlock::iterator I = lis_.getInstructionFromIndex(Idx); |
| 537 | MachineInstr *MI = BuildMI(*I->getParent(), llvm::next(I), I->getDebugLoc(), |
| 538 | tii_.get(TargetOpcode::COPY), dupli_->reg) |
| 539 | .addReg(openli_->reg); |
| 540 | SlotIndex CopyIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
| 541 | openli_->addRange(LiveRange(Idx.getDefIndex(), CopyIdx, |
| 542 | mapValue(CurLR->valno))); |
| 543 | DupLR->valno->def = CopyIdx; |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 544 | DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": " << *openli_ << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 547 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
| 548 | /// Currently, only one value can leave the interval. |
| 549 | void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
| 550 | assert(openli_ && "openIntv not called before leaveIntvAtTop"); |
| 551 | |
| 552 | SlotIndex Start = lis_.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 553 | const LiveRange *CurLR = curli_->getLiveRangeContaining(Start); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 554 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 555 | // Is curli even live-in to MBB? |
| 556 | if (!CurLR) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 557 | DEBUG(dbgs() << " leaveIntvAtTop at " << Start << ": not live\n"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 561 | // Is curli defined by PHI at the beginning of MBB? |
| 562 | bool isPHIDef = CurLR->valno->isPHIDef() && |
| 563 | CurLR->valno->def.getBaseIndex() == Start; |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 564 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 565 | // If MBB is using a value of curli that was defined outside the openli range, |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 566 | // we don't want to copy it back here. |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 567 | if (!isPHIDef && !openli_->liveAt(CurLR->valno->def)) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 568 | DEBUG(dbgs() << " leaveIntvAtTop at " << Start |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 569 | << ": using external value\n"); |
| 570 | liveThrough_ = true; |
| 571 | return; |
| 572 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 573 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 574 | // We are going to insert a back copy, so we must have a dupli_. |
| 575 | LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Start); |
| 576 | assert(DupLR && "dupli not live into black, but curli is?"); |
| 577 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 578 | // Insert the COPY instruction. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 579 | MachineInstr *MI = BuildMI(MBB, MBB.begin(), DebugLoc(), |
Jakob Stoklund Olesen | b85f538 | 2010-08-06 18:04:17 +0000 | [diff] [blame] | 580 | tii_.get(TargetOpcode::COPY), dupli_->reg) |
| 581 | .addReg(openli_->reg); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 582 | SlotIndex Idx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 583 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 584 | // Adjust dupli and openli values. |
| 585 | if (isPHIDef) { |
| 586 | // dupli was already a PHI on entry to MBB. Simply insert an openli PHI, |
| 587 | // and shift the dupli def down to the COPY. |
| 588 | VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false, |
| 589 | lis_.getVNInfoAllocator()); |
| 590 | VNI->setIsPHIDef(true); |
| 591 | openli_->addRange(LiveRange(VNI->def, Idx, VNI)); |
| 592 | |
| 593 | dupli_->removeRange(Start, Idx); |
| 594 | DupLR->valno->def = Idx; |
| 595 | DupLR->valno->setIsPHIDef(false); |
| 596 | } else { |
| 597 | // The dupli value was defined somewhere inside the openli range. |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 598 | DEBUG(dbgs() << " leaveIntvAtTop source value defined at " |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 599 | << DupLR->valno->def << "\n"); |
| 600 | // FIXME: We may not need a PHI here if all predecessors have the same |
| 601 | // value. |
| 602 | VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false, |
| 603 | lis_.getVNInfoAllocator()); |
| 604 | VNI->setIsPHIDef(true); |
| 605 | openli_->addRange(LiveRange(VNI->def, Idx, VNI)); |
| 606 | |
| 607 | // FIXME: What if DupLR->valno is used by multiple exits? SSA Update. |
| 608 | |
| 609 | // closeIntv is going to remove the superfluous live ranges. |
| 610 | DupLR->valno->def = Idx; |
| 611 | DupLR->valno->setIsPHIDef(false); |
| 612 | } |
| 613 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 614 | DEBUG(dbgs() << " leaveIntvAtTop at " << Idx << ": " << *openli_ << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 617 | /// closeIntv - Indicate that we are done editing the currently open |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 618 | /// LiveInterval, and ranges can be trimmed. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 619 | void SplitEditor::closeIntv() { |
| 620 | assert(openli_ && "openIntv not called before closeIntv"); |
| 621 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 622 | DEBUG(dbgs() << " closeIntv cleaning up\n"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 623 | DEBUG(dbgs() << " open " << *openli_ << '\n'); |
| 624 | |
| 625 | if (liveThrough_) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 626 | DEBUG(dbgs() << " value live through region, leaving dupli as is.\n"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 627 | } else { |
| 628 | // live out with copies inserted, or killed by region. Either way we need to |
| 629 | // remove the overlapping region from dupli. |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 630 | getDupLI(); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 631 | for (LiveInterval::iterator I = openli_->begin(), E = openli_->end(); |
| 632 | I != E; ++I) { |
| 633 | dupli_->removeRange(I->start, I->end); |
| 634 | } |
| 635 | // FIXME: A block branching to the entry block may also branch elsewhere |
| 636 | // curli is live. We need both openli and curli to be live in that case. |
| 637 | DEBUG(dbgs() << " dup2 " << *dupli_ << '\n'); |
| 638 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 639 | openli_ = 0; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 640 | valueMap_.clear(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /// rewrite - after all the new live ranges have been created, rewrite |
| 644 | /// instructions using curli to use the new intervals. |
| 645 | void SplitEditor::rewrite() { |
| 646 | assert(!openli_ && "Previous LI not closed before rewrite"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 647 | const LiveInterval *curli = sa_.getCurLI(); |
| 648 | for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(curli->reg), |
| 649 | RE = mri_.reg_end(); RI != RE;) { |
| 650 | MachineOperand &MO = RI.getOperand(); |
| 651 | MachineInstr *MI = MO.getParent(); |
| 652 | ++RI; |
| 653 | if (MI->isDebugValue()) { |
| 654 | DEBUG(dbgs() << "Zapping " << *MI); |
| 655 | // FIXME: We can do much better with debug values. |
| 656 | MO.setReg(0); |
| 657 | continue; |
| 658 | } |
| 659 | SlotIndex Idx = lis_.getInstructionIndex(MI); |
| 660 | Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); |
| 661 | LiveInterval *LI = dupli_; |
| 662 | for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) { |
| 663 | LiveInterval *testli = intervals_[i]; |
| 664 | if (testli->liveAt(Idx)) { |
| 665 | LI = testli; |
| 666 | break; |
| 667 | } |
| 668 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 669 | if (LI) { |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 670 | MO.setReg(LI->reg); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 671 | DEBUG(dbgs() << " rewrite " << Idx << '\t' << *MI); |
| 672 | } |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // dupli_ goes in last, after rewriting. |
Jakob Stoklund Olesen | 2343659 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 676 | if (dupli_) { |
Jakob Stoklund Olesen | fff2c47 | 2010-08-12 20:38:03 +0000 | [diff] [blame] | 677 | dupli_->RenumberValues(lis_); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 678 | intervals_.push_back(dupli_); |
Jakob Stoklund Olesen | 2343659 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 679 | } |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 680 | |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 681 | // Calculate spill weight and allocation hints for new intervals. |
| 682 | VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_); |
| 683 | for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) { |
| 684 | LiveInterval &li = *intervals_[i]; |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 685 | vrai.CalculateRegClass(li.reg); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 686 | vrai.CalculateWeightAndHint(li); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 687 | DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName() |
| 688 | << ":" << li << '\n'); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 689 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 693 | //===----------------------------------------------------------------------===// |
| 694 | // Loop Splitting |
| 695 | //===----------------------------------------------------------------------===// |
| 696 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 697 | bool SplitEditor::splitAroundLoop(const MachineLoop *Loop) { |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 698 | SplitAnalysis::LoopBlocks Blocks; |
| 699 | sa_.getLoopBlocks(Loop, Blocks); |
| 700 | |
| 701 | // Break critical edges as needed. |
| 702 | SplitAnalysis::BlockPtrSet CriticalExits; |
| 703 | sa_.getCriticalExits(Blocks, CriticalExits); |
| 704 | assert(CriticalExits.empty() && "Cannot break critical exits yet"); |
| 705 | |
| 706 | // Create new live interval for the loop. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 707 | openIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 708 | |
| 709 | // Insert copies in the predecessors. |
| 710 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(), |
| 711 | E = Blocks.Preds.end(); I != E; ++I) { |
| 712 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 713 | enterIntvAtEnd(MBB, *Loop->getHeader()); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // Switch all loop blocks. |
| 717 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(), |
| 718 | E = Blocks.Loop.end(); I != E; ++I) |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 719 | useIntv(**I); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 720 | |
| 721 | // Insert back copies in the exit blocks. |
| 722 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(), |
| 723 | E = Blocks.Exits.end(); I != E; ++I) { |
| 724 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 725 | leaveIntvAtTop(MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // Done. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 729 | closeIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 730 | rewrite(); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 731 | return dupli_; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 734 | |
| 735 | //===----------------------------------------------------------------------===// |
| 736 | // Single Block Splitting |
| 737 | //===----------------------------------------------------------------------===// |
| 738 | |
| 739 | /// splitSingleBlocks - Split curli into a separate live interval inside each |
| 740 | /// basic block in Blocks. Return true if curli has been completely replaced, |
| 741 | /// false if curli is still intact, and needs to be spilled or split further. |
| 742 | bool SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 743 | DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 744 | // Determine the first and last instruction using curli in each block. |
| 745 | typedef std::pair<SlotIndex,SlotIndex> IndexPair; |
| 746 | typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap; |
| 747 | IndexPairMap MBBRange; |
| 748 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 749 | E = sa_.usingInstrs_.end(); I != E; ++I) { |
| 750 | const MachineBasicBlock *MBB = (*I)->getParent(); |
| 751 | if (!Blocks.count(MBB)) |
| 752 | continue; |
| 753 | SlotIndex Idx = lis_.getInstructionIndex(*I); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 754 | DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 755 | IndexPair &IP = MBBRange[MBB]; |
| 756 | if (!IP.first.isValid() || Idx < IP.first) |
| 757 | IP.first = Idx; |
| 758 | if (!IP.second.isValid() || Idx > IP.second) |
| 759 | IP.second = Idx; |
| 760 | } |
| 761 | |
| 762 | // Create a new interval for each block. |
| 763 | for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(), |
| 764 | E = Blocks.end(); I != E; ++I) { |
| 765 | IndexPair &IP = MBBRange[*I]; |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 766 | DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": [" |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 767 | << IP.first << ';' << IP.second << ")\n"); |
| 768 | assert(IP.first.isValid() && IP.second.isValid()); |
| 769 | |
| 770 | openIntv(); |
| 771 | enterIntvBefore(IP.first); |
| 772 | useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex()); |
| 773 | leaveIntvAfter(IP.second); |
| 774 | closeIntv(); |
| 775 | } |
| 776 | rewrite(); |
| 777 | return dupli_; |
| 778 | } |
| 779 | |