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