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