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