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 | |
Jakob Stoklund Olesen | 376dcbd | 2010-11-03 20:39:23 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "regalloc" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 16 | #include "SplitKit.h" |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 17 | #include "LiveRangeEdit.h" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 18 | #include "VirtRegMap.h" |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Jakob Stoklund Olesen | 8d0963f | 2010-12-21 01:50:21 +0000 | [diff] [blame^] | 27 | #include "llvm/Support/GraphWriter.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetInstrInfo.h" |
| 30 | #include "llvm/Target/TargetMachine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 34 | static cl::opt<bool> |
| 35 | AllowSplit("spiller-splits-edges", |
| 36 | cl::desc("Allow critical edge splitting during spilling")); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 37 | |
| 38 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 8d0963f | 2010-12-21 01:50:21 +0000 | [diff] [blame^] | 39 | // Edge Bundles |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | /// compute - Compute the edge bundles for MF. Bundles depend only on the CFG. |
| 43 | void EdgeBundles::compute(const MachineFunction *mf) { |
| 44 | MF = mf; |
| 45 | EC.clear(); |
| 46 | EC.grow(2 * MF->size()); |
| 47 | |
| 48 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E; |
| 49 | ++I) { |
| 50 | const MachineBasicBlock &MBB = *I; |
| 51 | unsigned OutE = 2 * MBB.getNumber() + 1; |
| 52 | // Join the outgoing bundle with the ingoing bundles of all successors. |
| 53 | for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), |
| 54 | SE = MBB.succ_end(); SI != SE; ++SI) |
| 55 | EC.join(OutE, 2 * (*SI)->getNumber()); |
| 56 | } |
| 57 | EC.compress(); |
| 58 | } |
| 59 | |
| 60 | /// view - Visualize the annotated bipartite CFG with Graphviz. |
| 61 | void EdgeBundles::view() const { |
| 62 | ViewGraph(*this, "EdgeBundles"); |
| 63 | } |
| 64 | |
| 65 | /// Specialize WriteGraph, the standard implementation won't work. |
| 66 | raw_ostream &llvm::WriteGraph(raw_ostream &O, const EdgeBundles &G, |
| 67 | bool ShortNames, |
| 68 | const std::string &Title) { |
| 69 | const MachineFunction *MF = G.getMachineFunction(); |
| 70 | |
| 71 | O << "digraph {\n"; |
| 72 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 73 | I != E; ++I) { |
| 74 | unsigned BB = I->getNumber(); |
| 75 | O << "\t\"BB#" << BB << "\" [ shape=box ]\n" |
| 76 | << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n" |
| 77 | << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n'; |
| 78 | } |
| 79 | O << "}\n"; |
| 80 | return O; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 85 | // Split Analysis |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 88 | SplitAnalysis::SplitAnalysis(const MachineFunction &mf, |
| 89 | const LiveIntervals &lis, |
| 90 | const MachineLoopInfo &mli) |
| 91 | : mf_(mf), |
| 92 | lis_(lis), |
| 93 | loops_(mli), |
| 94 | tii_(*mf.getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 95 | curli_(0) {} |
| 96 | |
| 97 | void SplitAnalysis::clear() { |
| 98 | usingInstrs_.clear(); |
| 99 | usingBlocks_.clear(); |
| 100 | usingLoops_.clear(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 101 | curli_ = 0; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 104 | bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) { |
| 105 | MachineBasicBlock *T, *F; |
| 106 | SmallVector<MachineOperand, 4> Cond; |
| 107 | return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond); |
| 108 | } |
| 109 | |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 110 | /// analyzeUses - Count instructions, basic blocks, and loops using curli. |
| 111 | void SplitAnalysis::analyzeUses() { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 112 | const MachineRegisterInfo &MRI = mf_.getRegInfo(); |
| 113 | for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg); |
| 114 | MachineInstr *MI = I.skipInstruction();) { |
| 115 | if (MI->isDebugValue() || !usingInstrs_.insert(MI)) |
| 116 | continue; |
| 117 | MachineBasicBlock *MBB = MI->getParent(); |
| 118 | if (usingBlocks_[MBB]++) |
| 119 | continue; |
Jakob Stoklund Olesen | 9b90d7e | 2010-10-05 23:10:12 +0000 | [diff] [blame] | 120 | for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop; |
| 121 | Loop = Loop->getParentLoop()) |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 122 | usingLoops_[Loop]++; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 123 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 124 | DEBUG(dbgs() << " counted " |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 125 | << usingInstrs_.size() << " instrs, " |
| 126 | << usingBlocks_.size() << " blocks, " |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 127 | << usingLoops_.size() << " loops.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 130 | void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const { |
| 131 | for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 132 | unsigned count = usingBlocks_.lookup(*I); |
| 133 | OS << " BB#" << (*I)->getNumber(); |
| 134 | if (count) |
| 135 | OS << '(' << count << ')'; |
| 136 | } |
| 137 | } |
| 138 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 139 | // Get three sets of basic blocks surrounding a loop: Blocks inside the loop, |
| 140 | // predecessor blocks, and exit blocks. |
| 141 | void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) { |
| 142 | Blocks.clear(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 143 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 144 | // Blocks in the loop. |
| 145 | Blocks.Loop.insert(Loop->block_begin(), Loop->block_end()); |
| 146 | |
| 147 | // Predecessor blocks. |
| 148 | const MachineBasicBlock *Header = Loop->getHeader(); |
| 149 | for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(), |
| 150 | E = Header->pred_end(); I != E; ++I) |
| 151 | if (!Blocks.Loop.count(*I)) |
| 152 | Blocks.Preds.insert(*I); |
| 153 | |
| 154 | // Exit blocks. |
| 155 | for (MachineLoop::block_iterator I = Loop->block_begin(), |
| 156 | E = Loop->block_end(); I != E; ++I) { |
| 157 | const MachineBasicBlock *MBB = *I; |
| 158 | for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(), |
| 159 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 160 | if (!Blocks.Loop.count(*SI)) |
| 161 | Blocks.Exits.insert(*SI); |
| 162 | } |
| 163 | } |
| 164 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 165 | void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const { |
| 166 | OS << "Loop:"; |
| 167 | print(B.Loop, OS); |
| 168 | OS << ", preds:"; |
| 169 | print(B.Preds, OS); |
| 170 | OS << ", exits:"; |
| 171 | print(B.Exits, OS); |
| 172 | } |
| 173 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 174 | /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in |
| 175 | /// and around the Loop. |
| 176 | SplitAnalysis::LoopPeripheralUse SplitAnalysis:: |
| 177 | analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 178 | LoopPeripheralUse use = ContainedInLoop; |
| 179 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 180 | I != E; ++I) { |
| 181 | const MachineBasicBlock *MBB = I->first; |
| 182 | // Is this a peripheral block? |
| 183 | if (use < MultiPeripheral && |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 184 | (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 185 | if (I->second > 1) use = MultiPeripheral; |
| 186 | else use = SinglePeripheral; |
| 187 | continue; |
| 188 | } |
| 189 | // Is it a loop block? |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 190 | if (Blocks.Loop.count(MBB)) |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 191 | continue; |
| 192 | // It must be an unrelated block. |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 193 | DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber()); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 194 | return OutsideLoop; |
| 195 | } |
| 196 | return use; |
| 197 | } |
| 198 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 199 | /// getCriticalExits - It may be necessary to partially break critical edges |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 200 | /// leaving the loop if an exit block has predecessors from outside the loop |
| 201 | /// periphery. |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 202 | void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks, |
| 203 | BlockPtrSet &CriticalExits) { |
| 204 | CriticalExits.clear(); |
| 205 | |
Jakob Stoklund Olesen | 0960a65 | 2010-10-27 00:39:05 +0000 | [diff] [blame] | 206 | // A critical exit block has curli live-in, and has a predecessor that is not |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 207 | // in the loop nor a loop predecessor. For such an exit block, the edges |
| 208 | // carrying the new variable must be moved to a new pre-exit block. |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 209 | for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end(); |
| 210 | I != E; ++I) { |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 211 | const MachineBasicBlock *Exit = *I; |
| 212 | // A single-predecessor exit block is definitely not a critical edge. |
| 213 | if (Exit->pred_size() == 1) |
| 214 | continue; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 215 | // This exit may not have curli live in at all. No need to split. |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 216 | if (!lis_.isLiveInToMBB(*curli_, Exit)) |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 217 | continue; |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 218 | // Does this exit block have a predecessor that is not a loop block or loop |
| 219 | // predecessor? |
| 220 | for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(), |
| 221 | PE = Exit->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 222 | const MachineBasicBlock *Pred = *PI; |
| 223 | if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred)) |
| 224 | continue; |
| 225 | // This is a critical exit block, and we need to split the exit edge. |
Jakob Stoklund Olesen | 2c1442e | 2010-10-22 20:28:23 +0000 | [diff] [blame] | 226 | CriticalExits.insert(Exit); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
Jakob Stoklund Olesen | 0960a65 | 2010-10-27 00:39:05 +0000 | [diff] [blame] | 232 | void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks, |
| 233 | BlockPtrSet &CriticalPreds) { |
| 234 | CriticalPreds.clear(); |
| 235 | |
| 236 | // A critical predecessor block has curli live-out, and has a successor that |
| 237 | // has curli live-in and is not in the loop nor a loop exit block. For such a |
| 238 | // predecessor block, we must carry the value in both the 'inside' and |
| 239 | // 'outside' registers. |
| 240 | for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end(); |
| 241 | I != E; ++I) { |
| 242 | const MachineBasicBlock *Pred = *I; |
| 243 | // Definitely not a critical edge. |
| 244 | if (Pred->succ_size() == 1) |
| 245 | continue; |
| 246 | // This block may not have curli live out at all if there is a PHI. |
| 247 | if (!lis_.isLiveOutOfMBB(*curli_, Pred)) |
| 248 | continue; |
| 249 | // Does this block have a successor outside the loop? |
| 250 | for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(), |
| 251 | SE = Pred->succ_end(); SI != SE; ++SI) { |
| 252 | const MachineBasicBlock *Succ = *SI; |
| 253 | if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ)) |
| 254 | continue; |
| 255 | if (!lis_.isLiveInToMBB(*curli_, Succ)) |
| 256 | continue; |
| 257 | // This is a critical predecessor block. |
| 258 | CriticalPreds.insert(Pred); |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 264 | /// canSplitCriticalExits - Return true if it is possible to insert new exit |
| 265 | /// blocks before the blocks in CriticalExits. |
| 266 | bool |
| 267 | SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks, |
| 268 | BlockPtrSet &CriticalExits) { |
| 269 | // If we don't allow critical edge splitting, require no critical exits. |
| 270 | if (!AllowSplit) |
| 271 | return CriticalExits.empty(); |
| 272 | |
| 273 | for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end(); |
| 274 | I != E; ++I) { |
| 275 | const MachineBasicBlock *Succ = *I; |
| 276 | // We want to insert a new pre-exit MBB before Succ, and change all the |
| 277 | // in-loop blocks to branch to the pre-exit instead of Succ. |
| 278 | // Check that all the in-loop predecessors can be changed. |
| 279 | for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(), |
| 280 | PE = Succ->pred_end(); PI != PE; ++PI) { |
| 281 | const MachineBasicBlock *Pred = *PI; |
| 282 | // The external predecessors won't be altered. |
| 283 | if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred)) |
| 284 | continue; |
| 285 | if (!canAnalyzeBranch(Pred)) |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | // If Succ's layout predecessor falls through, that too must be analyzable. |
| 290 | // We need to insert the pre-exit block in the gap. |
| 291 | MachineFunction::const_iterator MFI = Succ; |
| 292 | if (MFI == mf_.begin()) |
| 293 | continue; |
| 294 | if (!canAnalyzeBranch(--MFI)) |
| 295 | return false; |
| 296 | } |
| 297 | // No problems found. |
| 298 | return true; |
| 299 | } |
| 300 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 301 | void SplitAnalysis::analyze(const LiveInterval *li) { |
| 302 | clear(); |
| 303 | curli_ = li; |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 304 | analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 307 | void SplitAnalysis::getSplitLoops(LoopPtrSet &Loops) { |
| 308 | assert(curli_ && "Call analyze() before getSplitLoops"); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 309 | if (usingLoops_.empty()) |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 310 | return; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 311 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 312 | LoopBlocks Blocks; |
| 313 | BlockPtrSet CriticalExits; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 314 | |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 315 | // We split around loops where curli is used outside the periphery. |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 316 | for (LoopCountMap::const_iterator I = usingLoops_.begin(), |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 317 | E = usingLoops_.end(); I != E; ++I) { |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 318 | const MachineLoop *Loop = I->first; |
| 319 | getLoopBlocks(Loop, Blocks); |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 320 | DEBUG({ dbgs() << " "; print(Blocks, dbgs()); }); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 321 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 322 | switch(analyzeLoopPeripheralUse(Blocks)) { |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 323 | case OutsideLoop: |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 324 | break; |
| 325 | case MultiPeripheral: |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 326 | // FIXME: We could split a live range with multiple uses in a peripheral |
| 327 | // block and still make progress. However, it is possible that splitting |
| 328 | // another live range will insert copies into a peripheral block, and |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 329 | // there is a small chance we can enter an infinite loop, inserting copies |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 330 | // forever. |
| 331 | // For safety, stick to splitting live ranges with uses outside the |
| 332 | // periphery. |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 333 | DEBUG(dbgs() << ": multiple peripheral uses"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 334 | break; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 335 | case ContainedInLoop: |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 336 | DEBUG(dbgs() << ": fully contained\n"); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 337 | continue; |
| 338 | case SinglePeripheral: |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 339 | DEBUG(dbgs() << ": single peripheral use\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 340 | continue; |
| 341 | } |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 342 | // Will it be possible to split around this loop? |
| 343 | getCriticalExits(Blocks, CriticalExits); |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 344 | DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n"); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 345 | if (!canSplitCriticalExits(Blocks, CriticalExits)) |
| 346 | continue; |
| 347 | // This is a possible split. |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 348 | Loops.insert(Loop); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 351 | DEBUG(dbgs() << " getSplitLoops found " << Loops.size() |
Jakob Stoklund Olesen | ab00e9f | 2010-10-14 18:26:45 +0000 | [diff] [blame] | 352 | << " candidate loops.\n"); |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 353 | } |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 354 | |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 355 | const MachineLoop *SplitAnalysis::getBestSplitLoop() { |
| 356 | LoopPtrSet Loops; |
| 357 | getSplitLoops(Loops); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 358 | if (Loops.empty()) |
| 359 | return 0; |
| 360 | |
| 361 | // Pick the earliest loop. |
| 362 | // FIXME: Are there other heuristics to consider? |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 363 | const MachineLoop *Best = 0; |
| 364 | SlotIndex BestIdx; |
| 365 | for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E; |
| 366 | ++I) { |
| 367 | SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader()); |
| 368 | if (!Best || Idx < BestIdx) |
| 369 | Best = *I, BestIdx = Idx; |
| 370 | } |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 371 | DEBUG(dbgs() << " getBestSplitLoop found " << *Best); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 372 | return Best; |
| 373 | } |
| 374 | |
Jakob Stoklund Olesen | 697483a | 2010-12-15 17:49:52 +0000 | [diff] [blame] | 375 | /// isBypassLoop - Return true if curli is live through Loop and has no uses |
| 376 | /// inside the loop. Bypass loops are candidates for splitting because it can |
| 377 | /// prevent interference inside the loop. |
| 378 | bool SplitAnalysis::isBypassLoop(const MachineLoop *Loop) { |
| 379 | // If curli is live into the loop header and there are no uses in the loop, it |
| 380 | // must be live in the entire loop and live on at least one exiting edge. |
| 381 | return !usingLoops_.count(Loop) && |
| 382 | lis_.isLiveInToMBB(*curli_, Loop->getHeader()); |
| 383 | } |
| 384 | |
| 385 | /// getBypassLoops - Get all the maximal bypass loops. These are the bypass |
| 386 | /// loops whose parent is not a bypass loop. |
| 387 | void SplitAnalysis::getBypassLoops(LoopPtrSet &BypassLoops) { |
| 388 | SmallVector<MachineLoop*, 8> Todo(loops_.begin(), loops_.end()); |
Jakob Stoklund Olesen | 6203295 | 2010-12-15 18:07:48 +0000 | [diff] [blame] | 389 | while (!Todo.empty()) { |
Jakob Stoklund Olesen | 697483a | 2010-12-15 17:49:52 +0000 | [diff] [blame] | 390 | MachineLoop *Loop = Todo.pop_back_val(); |
| 391 | if (!usingLoops_.count(Loop)) { |
| 392 | // This is either a bypass loop or completely irrelevant. |
| 393 | if (lis_.isLiveInToMBB(*curli_, Loop->getHeader())) |
| 394 | BypassLoops.insert(Loop); |
| 395 | // Either way, skip the child loops. |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | // The child loops may be bypass loops. |
| 400 | Todo.append(Loop->begin(), Loop->end()); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 405 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 406 | // LiveIntervalMap |
| 407 | //===----------------------------------------------------------------------===// |
| 408 | |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 409 | // Work around the fact that the std::pair constructors are broken for pointer |
| 410 | // pairs in some implementations. makeVV(x, 0) works. |
| 411 | static inline std::pair<const VNInfo*, VNInfo*> |
| 412 | makeVV(const VNInfo *a, VNInfo *b) { |
| 413 | return std::make_pair(a, b); |
| 414 | } |
| 415 | |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 416 | void LiveIntervalMap::reset(LiveInterval *li) { |
| 417 | li_ = li; |
| 418 | valueMap_.clear(); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 419 | liveOutCache_.clear(); |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 422 | bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const { |
| 423 | ValueMap::const_iterator i = valueMap_.find(ParentVNI); |
| 424 | return i != valueMap_.end() && i->second == 0; |
| 425 | } |
| 426 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 427 | // defValue - Introduce a li_ def for ParentVNI that could be later than |
| 428 | // ParentVNI->def. |
| 429 | VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 430 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 431 | assert(ParentVNI && "Mapping NULL value"); |
| 432 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 433 | assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
| 434 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 435 | // Create a new value. |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 436 | VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 437 | |
Jakob Stoklund Olesen | 79c0262 | 2010-10-26 22:36:02 +0000 | [diff] [blame] | 438 | // Preserve the PHIDef bit. |
| 439 | if (ParentVNI->isPHIDef() && Idx == ParentVNI->def) |
| 440 | VNI->setIsPHIDef(true); |
| 441 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 442 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 443 | std::pair<ValueMap::iterator,bool> InsP = |
| 444 | valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 445 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 446 | // 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] | 447 | if (!InsP.second) |
| 448 | InsP.first->second = 0; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 449 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 450 | return VNI; |
| 451 | } |
| 452 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 453 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 454 | // mapValue - Find the mapped value for ParentVNI at Idx. |
| 455 | // Potentially create phi-def values. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 456 | VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx, |
| 457 | bool *simple) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 458 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 459 | assert(ParentVNI && "Mapping NULL value"); |
| 460 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 461 | assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
| 462 | |
| 463 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 464 | std::pair<ValueMap::iterator,bool> InsP = |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 465 | valueMap_.insert(makeVV(ParentVNI, 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 466 | |
| 467 | // This was an unknown value. Create a simple mapping. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 468 | if (InsP.second) { |
| 469 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 470 | return InsP.first->second = li_->createValueCopy(ParentVNI, |
| 471 | lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 474 | // This was a simple mapped value. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 475 | if (InsP.first->second) { |
| 476 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 477 | return InsP.first->second; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 478 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 479 | |
| 480 | // This is a complex mapped value. There may be multiple defs, and we may need |
| 481 | // to create phi-defs. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 482 | if (simple) *simple = false; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 483 | MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx); |
| 484 | assert(IdxMBB && "No MBB at Idx"); |
| 485 | |
| 486 | // Is there a def in the same MBB we can extend? |
| 487 | if (VNInfo *VNI = extendTo(IdxMBB, Idx)) |
| 488 | return VNI; |
| 489 | |
| 490 | // Now for the fun part. We know that ParentVNI potentially has multiple defs, |
| 491 | // and we may need to create even more phi-defs to preserve VNInfo SSA form. |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 492 | // Perform a search for all predecessor blocks where we know the dominating |
| 493 | // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB. |
| 494 | DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber() |
| 495 | << " at " << Idx << " in " << *li_ << '\n'); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 496 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 497 | // Blocks where li_ should be live-in. |
| 498 | SmallVector<MachineDomTreeNode*, 16> LiveIn; |
| 499 | LiveIn.push_back(mdt_[IdxMBB]); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 500 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 501 | // Using liveOutCache_ as a visited set, perform a BFS for all reaching defs. |
| 502 | for (unsigned i = 0; i != LiveIn.size(); ++i) { |
| 503 | MachineBasicBlock *MBB = LiveIn[i]->getBlock(); |
| 504 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 505 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 506 | MachineBasicBlock *Pred = *PI; |
| 507 | // Is this a known live-out block? |
| 508 | std::pair<LiveOutMap::iterator,bool> LOIP = |
| 509 | liveOutCache_.insert(std::make_pair(Pred, LiveOutPair())); |
| 510 | // Yes, we have been here before. |
| 511 | if (!LOIP.second) { |
Benjamin Kramer | 8a8e26f | 2010-10-29 17:40:05 +0000 | [diff] [blame] | 512 | DEBUG(if (VNInfo *VNI = LOIP.first->second.first) |
| 513 | dbgs() << " known valno #" << VNI->id |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 514 | << " at BB#" << Pred->getNumber() << '\n'); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 515 | continue; |
| 516 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 517 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 518 | // Does Pred provide a live-out value? |
| 519 | SlotIndex Last = lis_.getMBBEndIdx(Pred).getPrevSlot(); |
| 520 | if (VNInfo *VNI = extendTo(Pred, Last)) { |
| 521 | MachineBasicBlock *DefMBB = lis_.getMBBFromIndex(VNI->def); |
| 522 | DEBUG(dbgs() << " found valno #" << VNI->id |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 523 | << " from BB#" << DefMBB->getNumber() |
| 524 | << " at BB#" << Pred->getNumber() << '\n'); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 525 | LiveOutPair &LOP = LOIP.first->second; |
| 526 | LOP.first = VNI; |
Benjamin Kramer | 8a8e26f | 2010-10-29 17:40:05 +0000 | [diff] [blame] | 527 | LOP.second = mdt_[DefMBB]; |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 528 | continue; |
| 529 | } |
| 530 | // No, we need a live-in value for Pred as well |
| 531 | if (Pred != IdxMBB) |
| 532 | LiveIn.push_back(mdt_[Pred]); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 533 | } |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 534 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 535 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 536 | // We may need to add phi-def values to preserve the SSA form. |
| 537 | // This is essentially the same iterative algorithm that SSAUpdater uses, |
| 538 | // except we already have a dominator tree, so we don't have to recompute it. |
| 539 | VNInfo *IdxVNI = 0; |
| 540 | unsigned Changes; |
| 541 | do { |
| 542 | Changes = 0; |
| 543 | DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n"); |
| 544 | // Propagate live-out values down the dominator tree, inserting phi-defs when |
| 545 | // necessary. Since LiveIn was created by a BFS, going backwards makes it more |
| 546 | // likely for us to visit immediate dominators before their children. |
| 547 | for (unsigned i = LiveIn.size(); i; --i) { |
| 548 | MachineDomTreeNode *Node = LiveIn[i-1]; |
| 549 | MachineBasicBlock *MBB = Node->getBlock(); |
| 550 | MachineDomTreeNode *IDom = Node->getIDom(); |
| 551 | LiveOutPair IDomValue; |
| 552 | // We need a live-in value to a block with no immediate dominator? |
| 553 | // This is probably an unreachable block that has survived somehow. |
| 554 | bool needPHI = !IDom; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 555 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 556 | // Get the IDom live-out value. |
| 557 | if (!needPHI) { |
| 558 | LiveOutMap::iterator I = liveOutCache_.find(IDom->getBlock()); |
| 559 | if (I != liveOutCache_.end()) |
| 560 | IDomValue = I->second; |
| 561 | else |
| 562 | // If IDom is outside our set of live-out blocks, there must be new |
| 563 | // defs, and we need a phi-def here. |
| 564 | needPHI = true; |
| 565 | } |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 566 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 567 | // IDom dominates all of our predecessors, but it may not be the immediate |
| 568 | // dominator. Check if any of them have live-out values that are properly |
| 569 | // dominated by IDom. If so, we need a phi-def here. |
| 570 | if (!needPHI) { |
| 571 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 572 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 573 | LiveOutPair Value = liveOutCache_[*PI]; |
| 574 | if (!Value.first || Value.first == IDomValue.first) |
| 575 | continue; |
| 576 | // This predecessor is carrying something other than IDomValue. |
| 577 | // It could be because IDomValue hasn't propagated yet, or it could be |
| 578 | // because MBB is in the dominance frontier of that value. |
| 579 | if (mdt_.dominates(IDom, Value.second)) { |
| 580 | needPHI = true; |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | } |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 585 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 586 | // Create a phi-def if required. |
| 587 | if (needPHI) { |
| 588 | ++Changes; |
| 589 | SlotIndex Start = lis_.getMBBStartIdx(MBB); |
| 590 | VNInfo *VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator()); |
| 591 | VNI->setIsPHIDef(true); |
| 592 | DEBUG(dbgs() << " - BB#" << MBB->getNumber() |
| 593 | << " phi-def #" << VNI->id << " at " << Start << '\n'); |
| 594 | // We no longer need li_ to be live-in. |
| 595 | LiveIn.erase(LiveIn.begin()+(i-1)); |
| 596 | // Blocks in LiveIn are either IdxMBB, or have a value live-through. |
| 597 | if (MBB == IdxMBB) |
| 598 | IdxVNI = VNI; |
| 599 | // Check if we need to update live-out info. |
| 600 | LiveOutMap::iterator I = liveOutCache_.find(MBB); |
| 601 | if (I == liveOutCache_.end() || I->second.second == Node) { |
| 602 | // We already have a live-out defined in MBB, so this must be IdxMBB. |
| 603 | assert(MBB == IdxMBB && "Adding phi-def to known live-out"); |
| 604 | li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI)); |
| 605 | } else { |
| 606 | // This phi-def is also live-out, so color the whole block. |
| 607 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
| 608 | I->second = LiveOutPair(VNI, Node); |
| 609 | } |
| 610 | } else if (IDomValue.first) { |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 611 | // No phi-def here. Remember incoming value for IdxMBB. |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 612 | if (MBB == IdxMBB) |
| 613 | IdxVNI = IDomValue.first; |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 614 | // Propagate IDomValue if needed: |
| 615 | // MBB is live-out and doesn't define its own value. |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 616 | LiveOutMap::iterator I = liveOutCache_.find(MBB); |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 617 | if (I != liveOutCache_.end() && I->second.second != Node && |
| 618 | I->second.first != IDomValue.first) { |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 619 | ++Changes; |
| 620 | I->second = IDomValue; |
| 621 | DEBUG(dbgs() << " - BB#" << MBB->getNumber() |
| 622 | << " idom valno #" << IDomValue.first->id |
| 623 | << " from BB#" << IDom->getBlock()->getNumber() << '\n'); |
| 624 | } |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 625 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 626 | } |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 627 | DEBUG(dbgs() << " - made " << Changes << " changes.\n"); |
| 628 | } while (Changes); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 629 | |
| 630 | assert(IdxVNI && "Didn't find value for Idx"); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 631 | |
| 632 | #ifndef NDEBUG |
| 633 | // Check the liveOutCache_ invariants. |
| 634 | for (LiveOutMap::iterator I = liveOutCache_.begin(), E = liveOutCache_.end(); |
| 635 | I != E; ++I) { |
| 636 | assert(I->first && "Null MBB entry in cache"); |
| 637 | assert(I->second.first && "Null VNInfo in cache"); |
| 638 | assert(I->second.second && "Null DomTreeNode in cache"); |
| 639 | if (I->second.second->getBlock() == I->first) |
| 640 | continue; |
| 641 | for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(), |
| 642 | PE = I->first->pred_end(); PI != PE; ++PI) |
| 643 | assert(liveOutCache_.lookup(*PI) == I->second && "Bad invariant"); |
| 644 | } |
| 645 | #endif |
| 646 | |
| 647 | // Since we went through the trouble of a full BFS visiting all reaching defs, |
| 648 | // the values in LiveIn are now accurate. No more phi-defs are needed |
| 649 | // for these blocks, so we can color the live ranges. |
| 650 | // This makes the next mapValue call much faster. |
| 651 | for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) { |
| 652 | MachineBasicBlock *MBB = LiveIn[i]->getBlock(); |
| 653 | SlotIndex Start = lis_.getMBBStartIdx(MBB); |
| 654 | if (MBB == IdxMBB) { |
| 655 | li_->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI)); |
| 656 | continue; |
| 657 | } |
| 658 | // Anything in LiveIn other than IdxMBB is live-through. |
| 659 | VNInfo *VNI = liveOutCache_.lookup(MBB).first; |
| 660 | assert(VNI && "Missing block value"); |
| 661 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
| 662 | } |
| 663 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 664 | return IdxVNI; |
| 665 | } |
| 666 | |
| 667 | // extendTo - Find the last li_ value defined in MBB at or before Idx. The |
| 668 | // parentli_ is assumed to be live at Idx. Extend the live range to Idx. |
| 669 | // Return the found VNInfo, or NULL. |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 670 | VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 671 | assert(li_ && "call reset first"); |
| 672 | LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx); |
| 673 | if (I == li_->begin()) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 674 | return 0; |
| 675 | --I; |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 676 | if (I->end <= lis_.getMBBStartIdx(MBB)) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 677 | return 0; |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 678 | if (I->end <= Idx) |
| 679 | I->end = Idx.getNextSlot(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 680 | return I->valno; |
| 681 | } |
| 682 | |
| 683 | // addSimpleRange - Add a simple range from parentli_ to li_. |
| 684 | // ParentVNI must be live in the [Start;End) interval. |
| 685 | void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End, |
| 686 | const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 687 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 688 | bool simple; |
| 689 | VNInfo *VNI = mapValue(ParentVNI, Start, &simple); |
| 690 | // A simple mapping is easy. |
| 691 | if (simple) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 692 | li_->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | |
| 696 | // ParentVNI is a complex value. We must map per MBB. |
| 697 | MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start); |
Jakob Stoklund Olesen | dbc3609 | 2010-10-05 22:19:29 +0000 | [diff] [blame] | 698 | MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot()); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 699 | |
| 700 | if (MBB == MBBE) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 701 | li_->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | |
| 705 | // First block. |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 706 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 707 | |
| 708 | // Run sequence of full blocks. |
| 709 | for (++MBB; MBB != MBBE; ++MBB) { |
| 710 | Start = lis_.getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 711 | li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), |
| 712 | mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Final block. |
| 716 | Start = lis_.getMBBStartIdx(MBB); |
| 717 | if (Start != End) |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 718 | li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_. |
| 722 | /// All needed values whose def is not inside [Start;End) must be defined |
| 723 | /// beforehand so mapValue will work. |
| 724 | void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 725 | assert(li_ && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 726 | LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end(); |
| 727 | LiveInterval::const_iterator I = std::lower_bound(B, E, Start); |
| 728 | |
| 729 | // Check if --I begins before Start and overlaps. |
| 730 | if (I != B) { |
| 731 | --I; |
| 732 | if (I->end > Start) |
| 733 | addSimpleRange(Start, std::min(End, I->end), I->valno); |
| 734 | ++I; |
| 735 | } |
| 736 | |
| 737 | // The remaining ranges begin after Start. |
| 738 | for (;I != E && I->start < End; ++I) |
| 739 | addSimpleRange(I->start, std::min(End, I->end), I->valno); |
| 740 | } |
| 741 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 742 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 743 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 744 | // Split Editor |
| 745 | //===----------------------------------------------------------------------===// |
| 746 | |
| 747 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 748 | SplitEditor::SplitEditor(SplitAnalysis &sa, |
| 749 | LiveIntervals &lis, |
| 750 | VirtRegMap &vrm, |
| 751 | MachineDominatorTree &mdt, |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 752 | LiveRangeEdit &edit) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 753 | : sa_(sa), lis_(lis), vrm_(vrm), |
| 754 | mri_(vrm.getMachineFunction().getRegInfo()), |
| 755 | tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 756 | tri_(*vrm.getMachineFunction().getTarget().getRegisterInfo()), |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 757 | edit_(edit), |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 758 | dupli_(lis_, mdt, edit.getParent()), |
| 759 | openli_(lis_, mdt, edit.getParent()) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 760 | { |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 761 | // We don't need an AliasAnalysis since we will only be performing |
| 762 | // cheap-as-a-copy remats anyway. |
| 763 | edit_.anyRematerializable(lis_, tii_, 0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 766 | bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 767 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I) |
| 768 | if (*I != dupli_.getLI() && (*I)->liveAt(Idx)) |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 769 | return true; |
| 770 | return false; |
| 771 | } |
| 772 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 773 | VNInfo *SplitEditor::defFromParent(LiveIntervalMap &Reg, |
| 774 | VNInfo *ParentVNI, |
| 775 | SlotIndex UseIdx, |
| 776 | MachineBasicBlock &MBB, |
| 777 | MachineBasicBlock::iterator I) { |
| 778 | VNInfo *VNI = 0; |
| 779 | MachineInstr *CopyMI = 0; |
| 780 | SlotIndex Def; |
| 781 | |
| 782 | // Attempt cheap-as-a-copy rematerialization. |
| 783 | LiveRangeEdit::Remat RM(ParentVNI); |
| 784 | if (edit_.canRematerializeAt(RM, UseIdx, true, lis_)) { |
| 785 | Def = edit_.rematerializeAt(MBB, I, Reg.getLI()->reg, RM, |
| 786 | lis_, tii_, tri_); |
| 787 | } else { |
| 788 | // Can't remat, just insert a copy from parent. |
| 789 | CopyMI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY), |
| 790 | Reg.getLI()->reg).addReg(edit_.getReg()); |
| 791 | Def = lis_.InsertMachineInstrInMaps(CopyMI).getDefIndex(); |
| 792 | } |
| 793 | |
| 794 | // Define the value in Reg. |
| 795 | VNI = Reg.defValue(ParentVNI, Def); |
| 796 | VNI->setCopy(CopyMI); |
| 797 | |
| 798 | // Add minimal liveness for the new value. |
| 799 | if (UseIdx < Def) |
| 800 | UseIdx = Def; |
| 801 | Reg.getLI()->addRange(LiveRange(Def, UseIdx.getNextSlot(), VNI)); |
| 802 | return VNI; |
| 803 | } |
| 804 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 805 | /// Create a new virtual register and live interval. |
| 806 | void SplitEditor::openIntv() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 807 | assert(!openli_.getLI() && "Previous LI not closed before openIntv"); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 808 | if (!dupli_.getLI()) |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 809 | dupli_.reset(&edit_.create(mri_, lis_, vrm_)); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 810 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 811 | openli_.reset(&edit_.create(mri_, lis_, vrm_)); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 814 | /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is |
| 815 | /// not live before Idx, a COPY is not inserted. |
| 816 | void SplitEditor::enterIntvBefore(SlotIndex Idx) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 817 | assert(openli_.getLI() && "openIntv not called before enterIntvBefore"); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 818 | Idx = Idx.getUseIndex(); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 819 | DEBUG(dbgs() << " enterIntvBefore " << Idx); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 820 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 821 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 822 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 823 | return; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 824 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 825 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 826 | truncatedValues.insert(ParentVNI); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 827 | MachineInstr *MI = lis_.getInstructionFromIndex(Idx); |
| 828 | assert(MI && "enterIntvBefore called with invalid index"); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 829 | |
| 830 | defFromParent(openli_, ParentVNI, Idx, *MI->getParent(), MI); |
| 831 | |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 832 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 835 | /// enterIntvAtEnd - Enter openli at the end of MBB. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 836 | void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 837 | assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 838 | SlotIndex End = lis_.getMBBEndIdx(&MBB).getPrevSlot(); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 839 | DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 840 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(End); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 841 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 842 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 843 | return; |
| 844 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 845 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 846 | truncatedValues.insert(ParentVNI); |
Jakob Stoklund Olesen | b5f327b | 2010-11-10 23:56:00 +0000 | [diff] [blame] | 847 | defFromParent(openli_, ParentVNI, End, MBB, MBB.getFirstTerminator()); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 848 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 851 | /// useIntv - indicate that all instructions in MBB should use openli. |
| 852 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
| 853 | useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 856 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 857 | assert(openli_.getLI() && "openIntv not called before useIntv"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 858 | openli_.addRange(Start, End); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 859 | DEBUG(dbgs() << " use [" << Start << ';' << End << "): " |
| 860 | << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 863 | /// leaveIntvAfter - Leave openli after the instruction at Idx. |
| 864 | void SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 865 | assert(openli_.getLI() && "openIntv not called before leaveIntvAfter"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 866 | DEBUG(dbgs() << " leaveIntvAfter " << Idx); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 867 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 868 | // The interval must be live beyond the instruction at Idx. |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 869 | Idx = Idx.getBoundaryIndex(); |
| 870 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 871 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 872 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 875 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 876 | |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 877 | MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 878 | VNInfo *VNI = defFromParent(dupli_, ParentVNI, Idx, |
| 879 | *MII->getParent(), llvm::next(MII)); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 880 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 881 | // Make sure that openli is properly extended from Idx to the new copy. |
| 882 | // FIXME: This shouldn't be necessary for remats. |
| 883 | openli_.addSimpleRange(Idx, VNI->def, ParentVNI); |
| 884 | |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 885 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 888 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
| 889 | /// Currently, only one value can leave the interval. |
| 890 | void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 891 | assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 892 | SlotIndex Start = lis_.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 893 | DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 894 | |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 895 | VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 896 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 897 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 898 | return; |
| 899 | } |
| 900 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 901 | VNInfo *VNI = defFromParent(dupli_, ParentVNI, Start, MBB, |
| 902 | MBB.SkipPHIsAndLabels(MBB.begin())); |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 903 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 904 | // Finally we must make sure that openli is properly extended from Start to |
| 905 | // the new copy. |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 906 | openli_.addSimpleRange(Start, VNI->def, ParentVNI); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 907 | DEBUG(dbgs() << ": " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 910 | /// closeIntv - Indicate that we are done editing the currently open |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 911 | /// LiveInterval, and ranges can be trimmed. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 912 | void SplitEditor::closeIntv() { |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 913 | assert(openli_.getLI() && "openIntv not called before closeIntv"); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 914 | |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 915 | DEBUG(dbgs() << " closeIntv cleaning up\n"); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 916 | DEBUG(dbgs() << " open " << *openli_.getLI() << '\n'); |
Jakob Stoklund Olesen | dd9f3fd | 2010-09-13 23:29:11 +0000 | [diff] [blame] | 917 | openli_.reset(0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 920 | /// rewrite - Rewrite all uses of reg to use the new registers. |
| 921 | void SplitEditor::rewrite(unsigned reg) { |
| 922 | for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(reg), |
| 923 | RE = mri_.reg_end(); RI != RE;) { |
| 924 | MachineOperand &MO = RI.getOperand(); |
Jakob Stoklund Olesen | 79cb53c | 2010-11-01 21:51:29 +0000 | [diff] [blame] | 925 | unsigned OpNum = RI.getOperandNo(); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 926 | MachineInstr *MI = MO.getParent(); |
| 927 | ++RI; |
| 928 | if (MI->isDebugValue()) { |
| 929 | DEBUG(dbgs() << "Zapping " << *MI); |
| 930 | // FIXME: We can do much better with debug values. |
| 931 | MO.setReg(0); |
| 932 | continue; |
| 933 | } |
| 934 | SlotIndex Idx = lis_.getInstructionIndex(MI); |
| 935 | Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); |
| 936 | LiveInterval *LI = 0; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 937 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; |
| 938 | ++I) { |
| 939 | LiveInterval *testli = *I; |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 940 | if (testli->liveAt(Idx)) { |
| 941 | LI = testli; |
| 942 | break; |
| 943 | } |
| 944 | } |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 945 | DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 946 | assert(LI && "No register was live at use"); |
| 947 | MO.setReg(LI->reg); |
Jakob Stoklund Olesen | 79cb53c | 2010-11-01 21:51:29 +0000 | [diff] [blame] | 948 | if (MO.isUse() && !MI->isRegTiedToDefOperand(OpNum)) |
| 949 | MO.setIsKill(LI->killedAt(Idx.getDefIndex())); |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 950 | DEBUG(dbgs() << '\t' << *MI); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 954 | void |
| 955 | SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) { |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 956 | // Build vector of iterator pairs from the intervals. |
| 957 | typedef std::pair<LiveInterval::const_iterator, |
| 958 | LiveInterval::const_iterator> IIPair; |
| 959 | SmallVector<IIPair, 8> Iters; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 960 | for (LiveRangeEdit::iterator LI = edit_.begin(), LE = edit_.end(); LI != LE; |
| 961 | ++LI) { |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 962 | if (*LI == dupli_.getLI()) |
| 963 | continue; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 964 | LiveInterval::const_iterator I = (*LI)->find(Start); |
| 965 | LiveInterval::const_iterator E = (*LI)->end(); |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 966 | if (I != E) |
| 967 | Iters.push_back(std::make_pair(I, E)); |
| 968 | } |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 969 | |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 970 | SlotIndex sidx = Start; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 971 | // Break [Start;End) into segments that don't overlap any intervals. |
| 972 | for (;;) { |
| 973 | SlotIndex next = sidx, eidx = End; |
| 974 | // Find overlapping intervals. |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 975 | for (unsigned i = 0; i != Iters.size() && sidx < eidx; ++i) { |
| 976 | LiveInterval::const_iterator I = Iters[i].first; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 977 | // Interval I is overlapping [sidx;eidx). Trim sidx. |
| 978 | if (I->start <= sidx) { |
| 979 | sidx = I->end; |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 980 | // Move to the next run, remove iters when all are consumed. |
| 981 | I = ++Iters[i].first; |
| 982 | if (I == Iters[i].second) { |
| 983 | Iters.erase(Iters.begin() + i); |
| 984 | --i; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 985 | continue; |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 986 | } |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 987 | } |
| 988 | // Trim eidx too if needed. |
| 989 | if (I->start >= eidx) |
| 990 | continue; |
| 991 | eidx = I->start; |
Jakob Stoklund Olesen | 4b3041c | 2010-10-07 17:56:39 +0000 | [diff] [blame] | 992 | next = I->end; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 993 | } |
| 994 | // Now, [sidx;eidx) doesn't overlap anything in intervals_. |
| 995 | if (sidx < eidx) |
| 996 | dupli_.addSimpleRange(sidx, eidx, VNI); |
| 997 | // If the interval end was truncated, we can try again from next. |
| 998 | if (next <= sidx) |
| 999 | break; |
| 1000 | sidx = next; |
| 1001 | } |
| 1002 | } |
| 1003 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1004 | void SplitEditor::computeRemainder() { |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1005 | // First we need to fill in the live ranges in dupli. |
| 1006 | // If values were redefined, we need a full recoloring with SSA update. |
| 1007 | // If values were truncated, we only need to truncate the ranges. |
| 1008 | // If values were partially rematted, we should shrink to uses. |
| 1009 | // If values were fully rematted, they should be omitted. |
| 1010 | // FIXME: If a single value is redefined, just move the def and truncate. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 1011 | LiveInterval &parent = edit_.getParent(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1012 | |
| 1013 | // Values that are fully contained in the split intervals. |
| 1014 | SmallPtrSet<const VNInfo*, 8> deadValues; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1015 | // Map all curli values that should have live defs in dupli. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 1016 | for (LiveInterval::const_vni_iterator I = parent.vni_begin(), |
| 1017 | E = parent.vni_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1018 | const VNInfo *VNI = *I; |
Jakob Stoklund Olesen | 3ccfce0 | 2010-10-29 17:47:49 +0000 | [diff] [blame] | 1019 | // Don't transfer unused values to the new intervals. |
| 1020 | if (VNI->isUnused()) |
| 1021 | continue; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1022 | // Original def is contained in the split intervals. |
| 1023 | if (intervalsLiveAt(VNI->def)) { |
| 1024 | // Did this value escape? |
| 1025 | if (dupli_.isMapped(VNI)) |
| 1026 | truncatedValues.insert(VNI); |
| 1027 | else |
| 1028 | deadValues.insert(VNI); |
| 1029 | continue; |
| 1030 | } |
| 1031 | // Add minimal live range at the definition. |
| 1032 | VNInfo *DVNI = dupli_.defValue(VNI, VNI->def); |
| 1033 | dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI)); |
| 1034 | } |
| 1035 | |
| 1036 | // Add all ranges to dupli. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 1037 | for (LiveInterval::const_iterator I = parent.begin(), E = parent.end(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1038 | I != E; ++I) { |
| 1039 | const LiveRange &LR = *I; |
| 1040 | if (truncatedValues.count(LR.valno)) { |
| 1041 | // recolor after removing intervals_. |
| 1042 | addTruncSimpleRange(LR.start, LR.end, LR.valno); |
| 1043 | } else if (!deadValues.count(LR.valno)) { |
| 1044 | // recolor without truncation. |
| 1045 | dupli_.addSimpleRange(LR.start, LR.end, LR.valno); |
| 1046 | } |
| 1047 | } |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 1048 | |
| 1049 | // Extend dupli_ to be live out of any critical loop predecessors. |
| 1050 | // This means we have multiple registers live out of those blocks. |
| 1051 | // The alternative would be to split the critical edges. |
| 1052 | if (criticalPreds_.empty()) |
| 1053 | return; |
| 1054 | for (SplitAnalysis::BlockPtrSet::iterator I = criticalPreds_.begin(), |
| 1055 | E = criticalPreds_.end(); I != E; ++I) |
| 1056 | dupli_.extendTo(*I, lis_.getMBBEndIdx(*I).getPrevSlot()); |
| 1057 | criticalPreds_.clear(); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void SplitEditor::finish() { |
| 1061 | assert(!openli_.getLI() && "Previous LI not closed before rewrite"); |
| 1062 | assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?"); |
| 1063 | |
| 1064 | // Complete dupli liveness. |
| 1065 | computeRemainder(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1066 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1067 | // Get rid of unused values and set phi-kill flags. |
Jakob Stoklund Olesen | f1354ae | 2010-10-26 22:36:05 +0000 | [diff] [blame] | 1068 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I) |
| 1069 | (*I)->RenumberValues(lis_); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1070 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1071 | // Rewrite instructions. |
Jakob Stoklund Olesen | 9d5d48b | 2010-10-15 00:34:01 +0000 | [diff] [blame] | 1072 | rewrite(edit_.getReg()); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 1073 | |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1074 | // Now check if any registers were separated into multiple components. |
| 1075 | ConnectedVNInfoEqClasses ConEQ(lis_); |
| 1076 | for (unsigned i = 0, e = edit_.size(); i != e; ++i) { |
| 1077 | // Don't use iterators, they are invalidated by create() below. |
| 1078 | LiveInterval *li = edit_.get(i); |
| 1079 | unsigned NumComp = ConEQ.Classify(li); |
| 1080 | if (NumComp <= 1) |
| 1081 | continue; |
| 1082 | DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); |
| 1083 | SmallVector<LiveInterval*, 8> dups; |
| 1084 | dups.push_back(li); |
| 1085 | for (unsigned i = 1; i != NumComp; ++i) |
| 1086 | dups.push_back(&edit_.create(mri_, lis_, vrm_)); |
| 1087 | ConEQ.Distribute(&dups[0]); |
| 1088 | // Rewrite uses to the new regs. |
| 1089 | rewrite(li->reg); |
| 1090 | } |
| 1091 | |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 1092 | // Calculate spill weight and allocation hints for new intervals. |
| 1093 | VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 1094 | for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I){ |
| 1095 | LiveInterval &li = **I; |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 1096 | vrai.CalculateRegClass(li.reg); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 1097 | vrai.CalculateWeightAndHint(li); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1098 | DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName() |
| 1099 | << ":" << li << '\n'); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 1100 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 1104 | //===----------------------------------------------------------------------===// |
| 1105 | // Loop Splitting |
| 1106 | //===----------------------------------------------------------------------===// |
| 1107 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1108 | void SplitEditor::splitAroundLoop(const MachineLoop *Loop) { |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1109 | SplitAnalysis::LoopBlocks Blocks; |
| 1110 | sa_.getLoopBlocks(Loop, Blocks); |
| 1111 | |
Jakob Stoklund Olesen | 452a9fd | 2010-10-07 18:47:07 +0000 | [diff] [blame] | 1112 | DEBUG({ |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 1113 | dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n'; |
Jakob Stoklund Olesen | 452a9fd | 2010-10-07 18:47:07 +0000 | [diff] [blame] | 1114 | }); |
| 1115 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1116 | // Break critical edges as needed. |
| 1117 | SplitAnalysis::BlockPtrSet CriticalExits; |
| 1118 | sa_.getCriticalExits(Blocks, CriticalExits); |
| 1119 | assert(CriticalExits.empty() && "Cannot break critical exits yet"); |
| 1120 | |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 1121 | // Get critical predecessors so computeRemainder can deal with them. |
| 1122 | sa_.getCriticalPreds(Blocks, criticalPreds_); |
| 1123 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1124 | // Create new live interval for the loop. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 1125 | openIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1126 | |
Jakob Stoklund Olesen | dfe3b6d | 2010-12-18 01:06:19 +0000 | [diff] [blame] | 1127 | // Insert copies in the predecessors if live-in to the header. |
| 1128 | if (lis_.isLiveInToMBB(edit_.getParent(), Loop->getHeader())) { |
| 1129 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(), |
| 1130 | E = Blocks.Preds.end(); I != E; ++I) { |
| 1131 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
| 1132 | enterIntvAtEnd(MBB); |
| 1133 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | // Switch all loop blocks. |
| 1137 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(), |
| 1138 | E = Blocks.Loop.end(); I != E; ++I) |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 1139 | useIntv(**I); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Insert back copies in the exit blocks. |
| 1142 | for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(), |
| 1143 | E = Blocks.Exits.end(); I != E; ++I) { |
| 1144 | MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 1145 | leaveIntvAtTop(MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | // Done. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 1149 | closeIntv(); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1150 | finish(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1153 | |
| 1154 | //===----------------------------------------------------------------------===// |
| 1155 | // Single Block Splitting |
| 1156 | //===----------------------------------------------------------------------===// |
| 1157 | |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 1158 | /// getMultiUseBlocks - if curli has more than one use in a basic block, it |
| 1159 | /// may be an advantage to split curli for the duration of the block. |
| 1160 | bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { |
| 1161 | // If curli is local to one block, there is no point to splitting it. |
| 1162 | if (usingBlocks_.size() <= 1) |
| 1163 | return false; |
| 1164 | // Add blocks with multiple uses. |
| 1165 | for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end(); |
| 1166 | I != E; ++I) |
| 1167 | switch (I->second) { |
| 1168 | case 0: |
| 1169 | case 1: |
| 1170 | continue; |
| 1171 | case 2: { |
| 1172 | // When there are only two uses and curli is both live in and live out, |
| 1173 | // we don't really win anything by isolating the block since we would be |
| 1174 | // inserting two copies. |
| 1175 | // The remaing register would still have two uses in the block. (Unless it |
| 1176 | // separates into disconnected components). |
| 1177 | if (lis_.isLiveInToMBB(*curli_, I->first) && |
| 1178 | lis_.isLiveOutOfMBB(*curli_, I->first)) |
| 1179 | continue; |
| 1180 | } // Fall through. |
| 1181 | default: |
| 1182 | Blocks.insert(I->first); |
| 1183 | } |
| 1184 | return !Blocks.empty(); |
| 1185 | } |
| 1186 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1187 | /// splitSingleBlocks - Split curli into a separate live interval inside each |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1188 | /// basic block in Blocks. |
| 1189 | void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1190 | DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1191 | // Determine the first and last instruction using curli in each block. |
| 1192 | typedef std::pair<SlotIndex,SlotIndex> IndexPair; |
| 1193 | typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap; |
| 1194 | IndexPairMap MBBRange; |
| 1195 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 1196 | E = sa_.usingInstrs_.end(); I != E; ++I) { |
| 1197 | const MachineBasicBlock *MBB = (*I)->getParent(); |
| 1198 | if (!Blocks.count(MBB)) |
| 1199 | continue; |
| 1200 | SlotIndex Idx = lis_.getInstructionIndex(*I); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1201 | DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1202 | IndexPair &IP = MBBRange[MBB]; |
| 1203 | if (!IP.first.isValid() || Idx < IP.first) |
| 1204 | IP.first = Idx; |
| 1205 | if (!IP.second.isValid() || Idx > IP.second) |
| 1206 | IP.second = Idx; |
| 1207 | } |
| 1208 | |
| 1209 | // Create a new interval for each block. |
| 1210 | for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(), |
| 1211 | E = Blocks.end(); I != E; ++I) { |
| 1212 | IndexPair &IP = MBBRange[*I]; |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1213 | DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": [" |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1214 | << IP.first << ';' << IP.second << ")\n"); |
| 1215 | assert(IP.first.isValid() && IP.second.isValid()); |
| 1216 | |
| 1217 | openIntv(); |
| 1218 | enterIntvBefore(IP.first); |
| 1219 | useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex()); |
| 1220 | leaveIntvAfter(IP.second); |
| 1221 | closeIntv(); |
| 1222 | } |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1223 | finish(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1226 | |
| 1227 | //===----------------------------------------------------------------------===// |
| 1228 | // Sub Block Splitting |
| 1229 | //===----------------------------------------------------------------------===// |
| 1230 | |
| 1231 | /// getBlockForInsideSplit - If curli is contained inside a single basic block, |
| 1232 | /// and it wou pay to subdivide the interval inside that block, return it. |
| 1233 | /// Otherwise return NULL. The returned block can be passed to |
| 1234 | /// SplitEditor::splitInsideBlock. |
| 1235 | const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() { |
| 1236 | // The interval must be exclusive to one block. |
| 1237 | if (usingBlocks_.size() != 1) |
| 1238 | return 0; |
| 1239 | // Don't to this for less than 4 instructions. We want to be sure that |
| 1240 | // splitting actually reduces the instruction count per interval. |
| 1241 | if (usingInstrs_.size() < 4) |
| 1242 | return 0; |
| 1243 | return usingBlocks_.begin()->first; |
| 1244 | } |
| 1245 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1246 | /// splitInsideBlock - Split curli into multiple intervals inside MBB. |
| 1247 | void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1248 | SmallVector<SlotIndex, 32> Uses; |
| 1249 | Uses.reserve(sa_.usingInstrs_.size()); |
| 1250 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(), |
| 1251 | E = sa_.usingInstrs_.end(); I != E; ++I) |
| 1252 | if ((*I)->getParent() == MBB) |
| 1253 | Uses.push_back(lis_.getInstructionIndex(*I)); |
| 1254 | DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for " |
| 1255 | << Uses.size() << " instructions.\n"); |
| 1256 | assert(Uses.size() >= 3 && "Need at least 3 instructions"); |
| 1257 | array_pod_sort(Uses.begin(), Uses.end()); |
| 1258 | |
| 1259 | // Simple algorithm: Find the largest gap between uses as determined by slot |
| 1260 | // indices. Create new intervals for instructions before the gap and after the |
| 1261 | // gap. |
| 1262 | unsigned bestPos = 0; |
| 1263 | int bestGap = 0; |
| 1264 | DEBUG(dbgs() << " dist (" << Uses[0]); |
| 1265 | for (unsigned i = 1, e = Uses.size(); i != e; ++i) { |
| 1266 | int g = Uses[i-1].distance(Uses[i]); |
| 1267 | DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]); |
| 1268 | if (g > bestGap) |
| 1269 | bestPos = i, bestGap = g; |
| 1270 | } |
| 1271 | DEBUG(dbgs() << "), best: -" << bestGap << "-\n"); |
| 1272 | |
| 1273 | // bestPos points to the first use after the best gap. |
| 1274 | assert(bestPos > 0 && "Invalid gap"); |
| 1275 | |
| 1276 | // FIXME: Don't create intervals for low densities. |
| 1277 | |
| 1278 | // First interval before the gap. Don't create single-instr intervals. |
| 1279 | if (bestPos > 1) { |
| 1280 | openIntv(); |
| 1281 | enterIntvBefore(Uses.front()); |
| 1282 | useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex()); |
| 1283 | leaveIntvAfter(Uses[bestPos-1]); |
| 1284 | closeIntv(); |
| 1285 | } |
| 1286 | |
| 1287 | // Second interval after the gap. |
| 1288 | if (bestPos < Uses.size()-1) { |
| 1289 | openIntv(); |
| 1290 | enterIntvBefore(Uses[bestPos]); |
| 1291 | useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex()); |
| 1292 | leaveIntvAfter(Uses.back()); |
| 1293 | closeIntv(); |
| 1294 | } |
| 1295 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1296 | finish(); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1297 | } |