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/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 32 | static cl::opt<bool> |
| 33 | AllowSplit("spiller-splits-edges", |
| 34 | cl::desc("Allow critical edge splitting during spilling")); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Split Analysis |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 40 | SplitAnalysis::SplitAnalysis(const MachineFunction &mf, |
| 41 | const LiveIntervals &lis, |
| 42 | const MachineLoopInfo &mli) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 43 | : MF(mf), |
| 44 | LIS(lis), |
| 45 | Loops(mli), |
| 46 | TII(*mf.getTarget().getInstrInfo()), |
| 47 | CurLI(0) {} |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 48 | |
| 49 | void SplitAnalysis::clear() { |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 50 | UseSlots.clear(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 51 | UsingInstrs.clear(); |
| 52 | UsingBlocks.clear(); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 53 | LiveBlocks.clear(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 54 | CurLI = 0; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 57 | bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) { |
| 58 | MachineBasicBlock *T, *F; |
| 59 | SmallVector<MachineOperand, 4> Cond; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 60 | return !TII.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond); |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 63 | /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 64 | void SplitAnalysis::analyzeUses() { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 65 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Jakob Stoklund Olesen | a372d16 | 2011-02-09 21:52:09 +0000 | [diff] [blame] | 66 | for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg), |
| 67 | E = MRI.reg_end(); I != E; ++I) { |
| 68 | MachineOperand &MO = I.getOperand(); |
| 69 | if (MO.isUse() && MO.isUndef()) |
| 70 | continue; |
| 71 | MachineInstr *MI = MO.getParent(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 72 | if (MI->isDebugValue() || !UsingInstrs.insert(MI)) |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 73 | continue; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 74 | UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex()); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 75 | MachineBasicBlock *MBB = MI->getParent(); |
Jakob Stoklund Olesen | 4f5c9d2 | 2011-02-09 23:56:18 +0000 | [diff] [blame] | 76 | UsingBlocks[MBB]++; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 77 | } |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 78 | array_pod_sort(UseSlots.begin(), UseSlots.end()); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 79 | calcLiveBlockInfo(); |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 80 | DEBUG(dbgs() << " counted " |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 81 | << UsingInstrs.size() << " instrs, " |
Jakob Stoklund Olesen | 4f5c9d2 | 2011-02-09 23:56:18 +0000 | [diff] [blame] | 82 | << UsingBlocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 85 | /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks |
| 86 | /// where CurLI is live. |
| 87 | void SplitAnalysis::calcLiveBlockInfo() { |
| 88 | if (CurLI->empty()) |
| 89 | return; |
| 90 | |
| 91 | LiveInterval::const_iterator LVI = CurLI->begin(); |
| 92 | LiveInterval::const_iterator LVE = CurLI->end(); |
| 93 | |
| 94 | SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; |
| 95 | UseI = UseSlots.begin(); |
| 96 | UseE = UseSlots.end(); |
| 97 | |
| 98 | // Loop over basic blocks where CurLI is live. |
| 99 | MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start); |
| 100 | for (;;) { |
| 101 | BlockInfo BI; |
| 102 | BI.MBB = MFI; |
| 103 | SlotIndex Start, Stop; |
| 104 | tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); |
| 105 | |
| 106 | // The last split point is the latest possible insertion point that dominates |
| 107 | // all successor blocks. If interference reaches LastSplitPoint, it is not |
| 108 | // possible to insert a split or reload that makes CurLI live in the |
| 109 | // outgoing bundle. |
| 110 | MachineBasicBlock::iterator LSP = LIS.getLastSplitPoint(*CurLI, BI.MBB); |
| 111 | if (LSP == BI.MBB->end()) |
| 112 | BI.LastSplitPoint = Stop; |
| 113 | else |
| 114 | BI.LastSplitPoint = LIS.getInstructionIndex(LSP); |
| 115 | |
| 116 | // LVI is the first live segment overlapping MBB. |
| 117 | BI.LiveIn = LVI->start <= Start; |
| 118 | if (!BI.LiveIn) |
| 119 | BI.Def = LVI->start; |
| 120 | |
| 121 | // Find the first and last uses in the block. |
| 122 | BI.Uses = hasUses(MFI); |
| 123 | if (BI.Uses && UseI != UseE) { |
| 124 | BI.FirstUse = *UseI; |
| 125 | assert(BI.FirstUse >= Start); |
| 126 | do ++UseI; |
| 127 | while (UseI != UseE && *UseI < Stop); |
| 128 | BI.LastUse = UseI[-1]; |
| 129 | assert(BI.LastUse < Stop); |
| 130 | } |
| 131 | |
| 132 | // Look for gaps in the live range. |
| 133 | bool hasGap = false; |
| 134 | BI.LiveOut = true; |
| 135 | while (LVI->end < Stop) { |
| 136 | SlotIndex LastStop = LVI->end; |
| 137 | if (++LVI == LVE || LVI->start >= Stop) { |
| 138 | BI.Kill = LastStop; |
| 139 | BI.LiveOut = false; |
| 140 | break; |
| 141 | } |
| 142 | if (LastStop < LVI->start) { |
| 143 | hasGap = true; |
| 144 | BI.Kill = LastStop; |
| 145 | BI.Def = LVI->start; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Don't set LiveThrough when the block has a gap. |
| 150 | BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut; |
| 151 | LiveBlocks.push_back(BI); |
| 152 | |
| 153 | // LVI is now at LVE or LVI->end >= Stop. |
| 154 | if (LVI == LVE) |
| 155 | break; |
| 156 | |
| 157 | // Live segment ends exactly at Stop. Move to the next segment. |
| 158 | if (LVI->end == Stop && ++LVI == LVE) |
| 159 | break; |
| 160 | |
| 161 | // Pick the next basic block. |
| 162 | if (LVI->start < Stop) |
| 163 | ++MFI; |
| 164 | else |
| 165 | MFI = LIS.getMBBFromIndex(LVI->start); |
| 166 | } |
| 167 | } |
| 168 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 169 | void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const { |
| 170 | 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] | 171 | unsigned count = UsingBlocks.lookup(*I); |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 172 | OS << " BB#" << (*I)->getNumber(); |
| 173 | if (count) |
| 174 | OS << '(' << count << ')'; |
| 175 | } |
| 176 | } |
| 177 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 178 | void SplitAnalysis::analyze(const LiveInterval *li) { |
| 179 | clear(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 180 | CurLI = li; |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 181 | analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Jakob Stoklund Olesen | 697483a | 2010-12-15 17:49:52 +0000 | [diff] [blame] | 184 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 185 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 186 | // LiveIntervalMap |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | |
Jakob Stoklund Olesen | b3e9681 | 2010-09-13 21:29:45 +0000 | [diff] [blame] | 189 | // Work around the fact that the std::pair constructors are broken for pointer |
| 190 | // pairs in some implementations. makeVV(x, 0) works. |
| 191 | static inline std::pair<const VNInfo*, VNInfo*> |
| 192 | makeVV(const VNInfo *a, VNInfo *b) { |
| 193 | return std::make_pair(a, b); |
| 194 | } |
| 195 | |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 196 | void LiveIntervalMap::reset(LiveInterval *li) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 197 | LI = li; |
| 198 | Values.clear(); |
| 199 | LiveOutCache.clear(); |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 202 | bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 203 | ValueMap::const_iterator i = Values.find(ParentVNI); |
| 204 | return i != Values.end() && i->second == 0; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 207 | // 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] | 208 | // ParentVNI->def. |
| 209 | VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 210 | assert(LI && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 211 | assert(ParentVNI && "Mapping NULL value"); |
| 212 | assert(Idx.isValid() && "Invalid SlotIndex"); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 213 | assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 214 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 215 | // Create a new value. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 216 | VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 217 | |
Jakob Stoklund Olesen | 79c0262 | 2010-10-26 22:36:02 +0000 | [diff] [blame] | 218 | // Preserve the PHIDef bit. |
| 219 | if (ParentVNI->isPHIDef() && Idx == ParentVNI->def) |
| 220 | VNI->setIsPHIDef(true); |
| 221 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 222 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 223 | std::pair<ValueMap::iterator,bool> InsP = |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 224 | Values.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 225 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 226 | // 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] | 227 | if (!InsP.second) |
| 228 | InsP.first->second = 0; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 229 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 230 | return VNI; |
| 231 | } |
| 232 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 233 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 234 | // mapValue - Find the mapped value for ParentVNI at Idx. |
| 235 | // Potentially create phi-def values. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 236 | VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx, |
| 237 | bool *simple) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 238 | assert(LI && "call reset first"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 239 | assert(ParentVNI && "Mapping NULL value"); |
| 240 | assert(Idx.isValid() && "Invalid SlotIndex"); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 241 | assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI"); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 242 | |
| 243 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 244 | std::pair<ValueMap::iterator,bool> InsP = |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 245 | Values.insert(makeVV(ParentVNI, 0)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 246 | |
| 247 | // This was an unknown value. Create a simple mapping. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 248 | if (InsP.second) { |
| 249 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 250 | return InsP.first->second = LI->createValueCopy(ParentVNI, |
| 251 | LIS.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 254 | // This was a simple mapped value. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 255 | if (InsP.first->second) { |
| 256 | if (simple) *simple = true; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 257 | return InsP.first->second; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 258 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 259 | |
| 260 | // This is a complex mapped value. There may be multiple defs, and we may need |
| 261 | // to create phi-defs. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 262 | if (simple) *simple = false; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 263 | MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 264 | assert(IdxMBB && "No MBB at Idx"); |
| 265 | |
| 266 | // Is there a def in the same MBB we can extend? |
| 267 | if (VNInfo *VNI = extendTo(IdxMBB, Idx)) |
| 268 | return VNI; |
| 269 | |
| 270 | // Now for the fun part. We know that ParentVNI potentially has multiple defs, |
| 271 | // 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] | 272 | // Perform a search for all predecessor blocks where we know the dominating |
| 273 | // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB. |
| 274 | DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber() |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 275 | << " at " << Idx << " in " << *LI << '\n'); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 276 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 277 | // Blocks where LI should be live-in. |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 278 | SmallVector<MachineDomTreeNode*, 16> LiveIn; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 279 | LiveIn.push_back(MDT[IdxMBB]); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 280 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 281 | // 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] | 282 | for (unsigned i = 0; i != LiveIn.size(); ++i) { |
| 283 | MachineBasicBlock *MBB = LiveIn[i]->getBlock(); |
| 284 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 285 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 286 | MachineBasicBlock *Pred = *PI; |
| 287 | // Is this a known live-out block? |
| 288 | std::pair<LiveOutMap::iterator,bool> LOIP = |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 289 | LiveOutCache.insert(std::make_pair(Pred, LiveOutPair())); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 290 | // Yes, we have been here before. |
| 291 | if (!LOIP.second) { |
Benjamin Kramer | 8a8e26f | 2010-10-29 17:40:05 +0000 | [diff] [blame] | 292 | DEBUG(if (VNInfo *VNI = LOIP.first->second.first) |
| 293 | dbgs() << " known valno #" << VNI->id |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 294 | << " at BB#" << Pred->getNumber() << '\n'); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 295 | continue; |
| 296 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 297 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 298 | // Does Pred provide a live-out value? |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 299 | SlotIndex Last = LIS.getMBBEndIdx(Pred).getPrevSlot(); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 300 | if (VNInfo *VNI = extendTo(Pred, Last)) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 301 | MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 302 | DEBUG(dbgs() << " found valno #" << VNI->id |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 303 | << " from BB#" << DefMBB->getNumber() |
| 304 | << " at BB#" << Pred->getNumber() << '\n'); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 305 | LiveOutPair &LOP = LOIP.first->second; |
| 306 | LOP.first = VNI; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 307 | LOP.second = MDT[DefMBB]; |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 308 | continue; |
| 309 | } |
| 310 | // No, we need a live-in value for Pred as well |
| 311 | if (Pred != IdxMBB) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 312 | LiveIn.push_back(MDT[Pred]); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 313 | } |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 314 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 315 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 316 | // We may need to add phi-def values to preserve the SSA form. |
| 317 | // This is essentially the same iterative algorithm that SSAUpdater uses, |
| 318 | // except we already have a dominator tree, so we don't have to recompute it. |
| 319 | VNInfo *IdxVNI = 0; |
| 320 | unsigned Changes; |
| 321 | do { |
| 322 | Changes = 0; |
| 323 | DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n"); |
| 324 | // Propagate live-out values down the dominator tree, inserting phi-defs when |
| 325 | // necessary. Since LiveIn was created by a BFS, going backwards makes it more |
| 326 | // likely for us to visit immediate dominators before their children. |
| 327 | for (unsigned i = LiveIn.size(); i; --i) { |
| 328 | MachineDomTreeNode *Node = LiveIn[i-1]; |
| 329 | MachineBasicBlock *MBB = Node->getBlock(); |
| 330 | MachineDomTreeNode *IDom = Node->getIDom(); |
| 331 | LiveOutPair IDomValue; |
| 332 | // We need a live-in value to a block with no immediate dominator? |
| 333 | // This is probably an unreachable block that has survived somehow. |
| 334 | bool needPHI = !IDom; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 335 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 336 | // Get the IDom live-out value. |
| 337 | if (!needPHI) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 338 | LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock()); |
| 339 | if (I != LiveOutCache.end()) |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 340 | IDomValue = I->second; |
| 341 | else |
| 342 | // If IDom is outside our set of live-out blocks, there must be new |
| 343 | // defs, and we need a phi-def here. |
| 344 | needPHI = true; |
| 345 | } |
Jakob Stoklund Olesen | 984a7fc | 2010-10-05 20:36:28 +0000 | [diff] [blame] | 346 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 347 | // IDom dominates all of our predecessors, but it may not be the immediate |
| 348 | // dominator. Check if any of them have live-out values that are properly |
| 349 | // dominated by IDom. If so, we need a phi-def here. |
| 350 | if (!needPHI) { |
| 351 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 352 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 353 | LiveOutPair Value = LiveOutCache[*PI]; |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 354 | if (!Value.first || Value.first == IDomValue.first) |
| 355 | continue; |
| 356 | // This predecessor is carrying something other than IDomValue. |
| 357 | // It could be because IDomValue hasn't propagated yet, or it could be |
| 358 | // because MBB is in the dominance frontier of that value. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 359 | if (MDT.dominates(IDom, Value.second)) { |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 360 | needPHI = true; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | } |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 365 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 366 | // Create a phi-def if required. |
| 367 | if (needPHI) { |
| 368 | ++Changes; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 369 | SlotIndex Start = LIS.getMBBStartIdx(MBB); |
| 370 | VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator()); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 371 | VNI->setIsPHIDef(true); |
| 372 | DEBUG(dbgs() << " - BB#" << MBB->getNumber() |
| 373 | << " phi-def #" << VNI->id << " at " << Start << '\n'); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 374 | // We no longer need LI to be live-in. |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 375 | LiveIn.erase(LiveIn.begin()+(i-1)); |
| 376 | // Blocks in LiveIn are either IdxMBB, or have a value live-through. |
| 377 | if (MBB == IdxMBB) |
| 378 | IdxVNI = VNI; |
| 379 | // Check if we need to update live-out info. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 380 | LiveOutMap::iterator I = LiveOutCache.find(MBB); |
| 381 | if (I == LiveOutCache.end() || I->second.second == Node) { |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 382 | // We already have a live-out defined in MBB, so this must be IdxMBB. |
| 383 | assert(MBB == IdxMBB && "Adding phi-def to known live-out"); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 384 | LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI)); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 385 | } else { |
| 386 | // 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] | 387 | LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 388 | I->second = LiveOutPair(VNI, Node); |
| 389 | } |
| 390 | } else if (IDomValue.first) { |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 391 | // No phi-def here. Remember incoming value for IdxMBB. |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 392 | if (MBB == IdxMBB) |
| 393 | IdxVNI = IDomValue.first; |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 394 | // Propagate IDomValue if needed: |
| 395 | // MBB is live-out and doesn't define its own value. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 396 | LiveOutMap::iterator I = LiveOutCache.find(MBB); |
| 397 | if (I != LiveOutCache.end() && I->second.second != Node && |
Jakob Stoklund Olesen | c94fcb1 | 2010-10-29 17:37:25 +0000 | [diff] [blame] | 398 | I->second.first != IDomValue.first) { |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 399 | ++Changes; |
| 400 | I->second = IDomValue; |
| 401 | DEBUG(dbgs() << " - BB#" << MBB->getNumber() |
| 402 | << " idom valno #" << IDomValue.first->id |
| 403 | << " from BB#" << IDom->getBlock()->getNumber() << '\n'); |
| 404 | } |
Jakob Stoklund Olesen | ff3ae86 | 2010-08-18 20:29:53 +0000 | [diff] [blame] | 405 | } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 406 | } |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 407 | DEBUG(dbgs() << " - made " << Changes << " changes.\n"); |
| 408 | } while (Changes); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 409 | |
| 410 | assert(IdxVNI && "Didn't find value for Idx"); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 411 | |
| 412 | #ifndef NDEBUG |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 413 | // Check the LiveOutCache invariants. |
| 414 | for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end(); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 415 | I != E; ++I) { |
| 416 | assert(I->first && "Null MBB entry in cache"); |
| 417 | assert(I->second.first && "Null VNInfo in cache"); |
| 418 | assert(I->second.second && "Null DomTreeNode in cache"); |
| 419 | if (I->second.second->getBlock() == I->first) |
| 420 | continue; |
| 421 | for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(), |
| 422 | PE = I->first->pred_end(); PI != PE; ++PI) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 423 | assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant"); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 424 | } |
| 425 | #endif |
| 426 | |
| 427 | // Since we went through the trouble of a full BFS visiting all reaching defs, |
| 428 | // the values in LiveIn are now accurate. No more phi-defs are needed |
| 429 | // for these blocks, so we can color the live ranges. |
| 430 | // This makes the next mapValue call much faster. |
| 431 | for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) { |
| 432 | MachineBasicBlock *MBB = LiveIn[i]->getBlock(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 433 | SlotIndex Start = LIS.getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 434 | VNInfo *VNI = LiveOutCache.lookup(MBB).first; |
Jakob Stoklund Olesen | 08eb8dd | 2011-02-03 20:29:36 +0000 | [diff] [blame] | 435 | |
| 436 | // Anything in LiveIn other than IdxMBB is live-through. |
| 437 | // In IdxMBB, we should stop at Idx unless the same value is live-out. |
| 438 | if (MBB == IdxMBB && IdxVNI != VNI) |
| 439 | LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI)); |
| 440 | else |
| 441 | LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 444 | return IdxVNI; |
| 445 | } |
| 446 | |
Jakob Stoklund Olesen | d7ca577 | 2011-01-20 17:45:20 +0000 | [diff] [blame] | 447 | #ifndef NDEBUG |
| 448 | void LiveIntervalMap::dumpCache() { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 449 | for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end(); |
Jakob Stoklund Olesen | d7ca577 | 2011-01-20 17:45:20 +0000 | [diff] [blame] | 450 | I != E; ++I) { |
| 451 | assert(I->first && "Null MBB entry in cache"); |
| 452 | assert(I->second.first && "Null VNInfo in cache"); |
| 453 | assert(I->second.second && "Null DomTreeNode in cache"); |
| 454 | dbgs() << " cache: BB#" << I->first->getNumber() |
| 455 | << " has valno #" << I->second.first->id << " from BB#" |
| 456 | << I->second.second->getBlock()->getNumber() << ", preds"; |
| 457 | for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(), |
| 458 | PE = I->first->pred_end(); PI != PE; ++PI) |
| 459 | dbgs() << " BB#" << (*PI)->getNumber(); |
| 460 | dbgs() << '\n'; |
| 461 | } |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 462 | dbgs() << " cache: " << LiveOutCache.size() << " entries.\n"; |
Jakob Stoklund Olesen | d7ca577 | 2011-01-20 17:45:20 +0000 | [diff] [blame] | 463 | } |
| 464 | #endif |
| 465 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 466 | // extendTo - Find the last LI value defined in MBB at or before Idx. The |
| 467 | // 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] | 468 | // Return the found VNInfo, or NULL. |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 469 | VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 470 | assert(LI && "call reset first"); |
| 471 | LiveInterval::iterator I = std::upper_bound(LI->begin(), LI->end(), Idx); |
| 472 | if (I == LI->begin()) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | --I; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 475 | if (I->end <= LIS.getMBBStartIdx(MBB)) |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 476 | return 0; |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 477 | if (I->end <= Idx) |
| 478 | I->end = Idx.getNextSlot(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 479 | return I->valno; |
| 480 | } |
| 481 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 482 | // addSimpleRange - Add a simple range from ParentLI to LI. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 483 | // ParentVNI must be live in the [Start;End) interval. |
| 484 | void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End, |
| 485 | const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 486 | assert(LI && "call reset first"); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 487 | bool simple; |
| 488 | VNInfo *VNI = mapValue(ParentVNI, Start, &simple); |
| 489 | // A simple mapping is easy. |
| 490 | if (simple) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 491 | LI->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
| 495 | // ParentVNI is a complex value. We must map per MBB. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 496 | MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); |
| 497 | MachineFunction::iterator MBBE = LIS.getMBBFromIndex(End.getPrevSlot()); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 498 | |
| 499 | if (MBB == MBBE) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 500 | LI->addRange(LiveRange(Start, End, VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
| 504 | // First block. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 505 | LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 506 | |
| 507 | // Run sequence of full blocks. |
| 508 | for (++MBB; MBB != MBBE; ++MBB) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 509 | Start = LIS.getMBBStartIdx(MBB); |
| 510 | LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 511 | mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Final block. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 515 | Start = LIS.getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 516 | if (Start != End) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 517 | LI->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start))); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 520 | /// addRange - Add live ranges to LI where [Start;End) intersects ParentLI. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 521 | /// All needed values whose def is not inside [Start;End) must be defined |
| 522 | /// beforehand so mapValue will work. |
| 523 | void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 524 | assert(LI && "call reset first"); |
| 525 | LiveInterval::const_iterator B = ParentLI.begin(), E = ParentLI.end(); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 526 | LiveInterval::const_iterator I = std::lower_bound(B, E, Start); |
| 527 | |
| 528 | // Check if --I begins before Start and overlaps. |
| 529 | if (I != B) { |
| 530 | --I; |
| 531 | if (I->end > Start) |
| 532 | addSimpleRange(Start, std::min(End, I->end), I->valno); |
| 533 | ++I; |
| 534 | } |
| 535 | |
| 536 | // The remaining ranges begin after Start. |
| 537 | for (;I != E && I->start < End; ++I) |
| 538 | addSimpleRange(I->start, std::min(End, I->end), I->valno); |
| 539 | } |
| 540 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 541 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 542 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 543 | // Split Editor |
| 544 | //===----------------------------------------------------------------------===// |
| 545 | |
| 546 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 547 | SplitEditor::SplitEditor(SplitAnalysis &sa, |
| 548 | LiveIntervals &lis, |
| 549 | VirtRegMap &vrm, |
| 550 | MachineDominatorTree &mdt, |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 551 | LiveRangeEdit &edit) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 552 | : sa_(sa), LIS(lis), VRM(vrm), |
| 553 | MRI(vrm.getMachineFunction().getRegInfo()), |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 554 | MDT(mdt), |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 555 | TII(*vrm.getMachineFunction().getTarget().getInstrInfo()), |
| 556 | TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()), |
| 557 | Edit(edit), |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 558 | OpenIdx(0), |
| 559 | RegAssign(Allocator) |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 560 | { |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 561 | // We don't need an AliasAnalysis since we will only be performing |
| 562 | // cheap-as-a-copy remats anyway. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 563 | Edit.anyRematerializable(LIS, TII, 0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 566 | void SplitEditor::dump() const { |
| 567 | if (RegAssign.empty()) { |
| 568 | dbgs() << " empty\n"; |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) |
| 573 | dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); |
| 574 | dbgs() << '\n'; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 577 | VNInfo *SplitEditor::defFromParent(unsigned RegIdx, |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 578 | VNInfo *ParentVNI, |
| 579 | SlotIndex UseIdx, |
| 580 | MachineBasicBlock &MBB, |
| 581 | MachineBasicBlock::iterator I) { |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 582 | MachineInstr *CopyMI = 0; |
| 583 | SlotIndex Def; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 584 | LiveInterval *LI = Edit.get(RegIdx); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 585 | |
| 586 | // Attempt cheap-as-a-copy rematerialization. |
| 587 | LiveRangeEdit::Remat RM(ParentVNI); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 588 | if (Edit.canRematerializeAt(RM, UseIdx, true, LIS)) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 589 | Def = Edit.rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 590 | } else { |
| 591 | // Can't remat, just insert a copy from parent. |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 592 | CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) |
| 593 | .addReg(Edit.getReg()); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 594 | Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex(); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Define the value in Reg. |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 598 | VNInfo *VNI = LIMappers[RegIdx].defValue(ParentVNI, Def); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 599 | VNI->setCopy(CopyMI); |
| 600 | |
| 601 | // Add minimal liveness for the new value. |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 602 | Edit.get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI)); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 603 | return VNI; |
| 604 | } |
| 605 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 606 | /// Create a new virtual register and live interval. |
| 607 | void SplitEditor::openIntv() { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 608 | assert(!OpenIdx && "Previous LI not closed before openIntv"); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 609 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 610 | // Create the complement as index 0. |
| 611 | if (Edit.empty()) { |
| 612 | Edit.create(MRI, LIS, VRM); |
| 613 | LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent())); |
| 614 | LIMappers.back().reset(Edit.get(0)); |
| 615 | } |
| 616 | |
| 617 | // Create the open interval. |
| 618 | OpenIdx = Edit.size(); |
| 619 | Edit.create(MRI, LIS, VRM); |
| 620 | LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent())); |
| 621 | LIMappers[OpenIdx].reset(Edit.get(OpenIdx)); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 624 | SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 625 | assert(OpenIdx && "openIntv not called before enterIntvBefore"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 626 | DEBUG(dbgs() << " enterIntvBefore " << Idx); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 627 | Idx = Idx.getBaseIndex(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 628 | VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 629 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 630 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 631 | return Idx; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 632 | } |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 633 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 634 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 635 | assert(MI && "enterIntvBefore called with invalid index"); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 636 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 637 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); |
| 638 | return VNI->def; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 641 | SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 642 | assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 643 | SlotIndex End = LIS.getMBBEndIdx(&MBB); |
| 644 | SlotIndex Last = End.getPrevSlot(); |
| 645 | DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); |
| 646 | VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Last); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 647 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 648 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 649 | return End; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 650 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 651 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 652 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, |
Jakob Stoklund Olesen | cb64047 | 2011-02-04 19:33:11 +0000 | [diff] [blame] | 653 | LIS.getLastSplitPoint(Edit.getParent(), &MBB)); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 654 | RegAssign.insert(VNI->def, End, OpenIdx); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 655 | DEBUG(dump()); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 656 | return VNI->def; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 659 | /// useIntv - indicate that all instructions in MBB should use OpenLI. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 660 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 661 | useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 664 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 665 | assert(OpenIdx && "openIntv not called before useIntv"); |
| 666 | DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); |
| 667 | RegAssign.insert(Start, End, OpenIdx); |
| 668 | DEBUG(dump()); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 671 | SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 672 | assert(OpenIdx && "openIntv not called before leaveIntvAfter"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 673 | DEBUG(dbgs() << " leaveIntvAfter " << Idx); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 674 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 675 | // The interval must be live beyond the instruction at Idx. |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 676 | Idx = Idx.getBoundaryIndex(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 677 | VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 678 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 679 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 680 | return Idx.getNextSlot(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 681 | } |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 682 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 683 | |
Jakob Stoklund Olesen | 01cb34b | 2011-02-08 18:50:18 +0000 | [diff] [blame] | 684 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
| 685 | assert(MI && "No instruction at index"); |
| 686 | VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), |
| 687 | llvm::next(MachineBasicBlock::iterator(MI))); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 688 | return VNI->def; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 691 | SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { |
| 692 | assert(OpenIdx && "openIntv not called before leaveIntvBefore"); |
| 693 | DEBUG(dbgs() << " leaveIntvBefore " << Idx); |
| 694 | |
| 695 | // The interval must be live into the instruction at Idx. |
| 696 | Idx = Idx.getBoundaryIndex(); |
| 697 | VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); |
| 698 | if (!ParentVNI) { |
| 699 | DEBUG(dbgs() << ": not live\n"); |
| 700 | return Idx.getNextSlot(); |
| 701 | } |
| 702 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
| 703 | |
| 704 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
| 705 | assert(MI && "No instruction at index"); |
| 706 | VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); |
| 707 | return VNI->def; |
| 708 | } |
| 709 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 710 | SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 711 | assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 712 | SlotIndex Start = LIS.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 713 | DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 714 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 715 | VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Start); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 716 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 717 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 718 | return Start; |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 721 | VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 722 | MBB.SkipPHIsAndLabels(MBB.begin())); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 723 | RegAssign.insert(Start, VNI->def, OpenIdx); |
| 724 | DEBUG(dump()); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 725 | return VNI->def; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Jakob Stoklund Olesen | 5c716bd | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 728 | void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { |
| 729 | assert(OpenIdx && "openIntv not called before overlapIntv"); |
| 730 | assert(Edit.getParent().getVNInfoAt(Start) == |
| 731 | Edit.getParent().getVNInfoAt(End.getPrevSlot()) && |
| 732 | "Parent changes value in extended range"); |
| 733 | assert(Edit.get(0)->getVNInfoAt(Start) && "Start must come from leaveIntv*"); |
| 734 | assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && |
| 735 | "Range cannot span basic blocks"); |
| 736 | |
| 737 | // Treat this as useIntv() for now. The complement interval will be extended |
| 738 | // as needed by mapValue(). |
| 739 | DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); |
| 740 | RegAssign.insert(Start, End, OpenIdx); |
| 741 | DEBUG(dump()); |
| 742 | } |
| 743 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 744 | /// closeIntv - Indicate that we are done editing the currently open |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 745 | /// LiveInterval, and ranges can be trimmed. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 746 | void SplitEditor::closeIntv() { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 747 | assert(OpenIdx && "openIntv not called before closeIntv"); |
| 748 | OpenIdx = 0; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 751 | /// rewriteAssigned - Rewrite all uses of Edit.getReg(). |
| 752 | void SplitEditor::rewriteAssigned() { |
| 753 | for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit.getReg()), |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 754 | RE = MRI.reg_end(); RI != RE;) { |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 755 | MachineOperand &MO = RI.getOperand(); |
| 756 | MachineInstr *MI = MO.getParent(); |
| 757 | ++RI; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 758 | // LiveDebugVariables should have handled all DBG_VALUE instructions. |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 759 | if (MI->isDebugValue()) { |
| 760 | DEBUG(dbgs() << "Zapping " << *MI); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 761 | MO.setReg(0); |
| 762 | continue; |
| 763 | } |
Jakob Stoklund Olesen | a372d16 | 2011-02-09 21:52:09 +0000 | [diff] [blame] | 764 | |
| 765 | // <undef> operands don't really read the register, so just assign them to |
| 766 | // the complement. |
| 767 | if (MO.isUse() && MO.isUndef()) { |
| 768 | MO.setReg(Edit.get(0)->reg); |
| 769 | continue; |
| 770 | } |
| 771 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 772 | SlotIndex Idx = LIS.getInstructionIndex(MI); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 773 | Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 774 | |
| 775 | // Rewrite to the mapped register at Idx. |
| 776 | unsigned RegIdx = RegAssign.lookup(Idx); |
| 777 | MO.setReg(Edit.get(RegIdx)->reg); |
| 778 | DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' |
| 779 | << Idx << ':' << RegIdx << '\t' << *MI); |
| 780 | |
| 781 | // Extend liveness to Idx. |
| 782 | const VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx); |
| 783 | LIMappers[RegIdx].mapValue(ParentVNI, Idx); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 787 | /// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping. |
| 788 | void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs, |
| 789 | const ConnectedVNInfoEqClasses &ConEq) { |
| 790 | for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg), |
| 791 | RE = MRI.reg_end(); RI != RE;) { |
| 792 | MachineOperand &MO = RI.getOperand(); |
| 793 | MachineInstr *MI = MO.getParent(); |
| 794 | ++RI; |
| 795 | if (MO.isUse() && MO.isUndef()) |
Jakob Stoklund Olesen | 9d99977 | 2010-10-21 18:47:08 +0000 | [diff] [blame] | 796 | continue; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 797 | // DBG_VALUE instructions should have been eliminated earlier. |
| 798 | SlotIndex Idx = LIS.getInstructionIndex(MI); |
| 799 | Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex(); |
| 800 | DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' |
| 801 | << Idx << ':'); |
| 802 | const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx); |
| 803 | assert(VNI && "Interval not live at use."); |
| 804 | MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg); |
| 805 | DEBUG(dbgs() << VNI->id << '\t' << *MI); |
Eric Christopher | 463a297 | 2011-02-03 05:40:54 +0000 | [diff] [blame] | 806 | } |
Eric Christopher | 463a297 | 2011-02-03 05:40:54 +0000 | [diff] [blame] | 807 | } |
Jakob Stoklund Olesen | 2cd2111 | 2011-02-03 00:54:23 +0000 | [diff] [blame] | 808 | |
Eric Christopher | 463a297 | 2011-02-03 05:40:54 +0000 | [diff] [blame] | 809 | void SplitEditor::finish() { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 810 | assert(OpenIdx == 0 && "Previous LI not closed before rewrite"); |
Eric Christopher | 463a297 | 2011-02-03 05:40:54 +0000 | [diff] [blame] | 811 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 812 | // At this point, the live intervals in Edit contain VNInfos corresponding to |
| 813 | // the inserted copies. |
| 814 | |
| 815 | // Add the original defs from the parent interval. |
| 816 | for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(), |
| 817 | E = Edit.getParent().vni_end(); I != E; ++I) { |
| 818 | const VNInfo *ParentVNI = *I; |
Jakob Stoklund Olesen | 9ecd1e7 | 2011-02-04 00:59:23 +0000 | [diff] [blame] | 819 | if (ParentVNI->isUnused()) |
| 820 | continue; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 821 | LiveIntervalMap &LIM = LIMappers[RegAssign.lookup(ParentVNI->def)]; |
| 822 | VNInfo *VNI = LIM.defValue(ParentVNI, ParentVNI->def); |
| 823 | LIM.getLI()->addRange(LiveRange(ParentVNI->def, |
| 824 | ParentVNI->def.getNextSlot(), VNI)); |
| 825 | // Mark all values as complex to force liveness computation. |
| 826 | // This should really only be necessary for remat victims, but we are lazy. |
| 827 | LIM.markComplexMapped(ParentVNI); |
| 828 | } |
| 829 | |
| 830 | #ifndef NDEBUG |
| 831 | // Every new interval must have a def by now, otherwise the split is bogus. |
| 832 | for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I) |
| 833 | assert((*I)->hasAtLeastOneValue() && "Split interval has no value"); |
| 834 | #endif |
| 835 | |
| 836 | // FIXME: Don't recompute the liveness of all values, infer it from the |
| 837 | // overlaps between the parent live interval and RegAssign. |
| 838 | // The mapValue algorithm is only necessary when: |
| 839 | // - The parent value maps to multiple defs, and new phis are needed, or |
| 840 | // - The value has been rematerialized before some uses, and we want to |
| 841 | // minimize the live range so it only reaches the remaining uses. |
| 842 | // All other values have simple liveness that can be computed from RegAssign |
| 843 | // and the parent live interval. |
| 844 | |
| 845 | // Extend live ranges to be live-out for successor PHI values. |
| 846 | for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(), |
| 847 | E = Edit.getParent().vni_end(); I != E; ++I) { |
| 848 | const VNInfo *PHIVNI = *I; |
Jakob Stoklund Olesen | 9ecd1e7 | 2011-02-04 00:59:23 +0000 | [diff] [blame] | 849 | if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 850 | continue; |
Jakob Stoklund Olesen | c50f077 | 2011-02-03 20:29:39 +0000 | [diff] [blame] | 851 | unsigned RegIdx = RegAssign.lookup(PHIVNI->def); |
| 852 | LiveIntervalMap &LIM = LIMappers[RegIdx]; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 853 | MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); |
Jakob Stoklund Olesen | c50f077 | 2011-02-03 20:29:39 +0000 | [diff] [blame] | 854 | DEBUG(dbgs() << " map phi in BB#" << MBB->getNumber() << '@' << PHIVNI->def |
| 855 | << " -> " << RegIdx << '\n'); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 856 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 857 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 858 | SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot(); |
Jakob Stoklund Olesen | c50f077 | 2011-02-03 20:29:39 +0000 | [diff] [blame] | 859 | DEBUG(dbgs() << " pred BB#" << (*PI)->getNumber() << '@' << End); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 860 | // The predecessor may not have a live-out value. That is OK, like an |
| 861 | // undef PHI operand. |
Jakob Stoklund Olesen | c50f077 | 2011-02-03 20:29:39 +0000 | [diff] [blame] | 862 | if (VNInfo *VNI = Edit.getParent().getVNInfoAt(End)) { |
| 863 | DEBUG(dbgs() << " has parent valno #" << VNI->id << " live out\n"); |
| 864 | assert(RegAssign.lookup(End) == RegIdx && |
| 865 | "Different register assignment in phi predecessor"); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 866 | LIM.mapValue(VNI, End); |
Jakob Stoklund Olesen | c50f077 | 2011-02-03 20:29:39 +0000 | [diff] [blame] | 867 | } |
| 868 | else |
| 869 | DEBUG(dbgs() << " is not live-out\n"); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 870 | } |
Jakob Stoklund Olesen | c50f077 | 2011-02-03 20:29:39 +0000 | [diff] [blame] | 871 | DEBUG(dbgs() << " " << *LIM.getLI() << '\n'); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | // Rewrite instructions. |
| 875 | rewriteAssigned(); |
| 876 | |
| 877 | // FIXME: Delete defs that were rematted everywhere. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 878 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 879 | // Get rid of unused values and set phi-kill flags. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 880 | for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I) |
| 881 | (*I)->RenumberValues(LIS); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 882 | |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 883 | // Now check if any registers were separated into multiple components. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 884 | ConnectedVNInfoEqClasses ConEQ(LIS); |
| 885 | for (unsigned i = 0, e = Edit.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 886 | // Don't use iterators, they are invalidated by create() below. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 887 | LiveInterval *li = Edit.get(i); |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 888 | unsigned NumComp = ConEQ.Classify(li); |
| 889 | if (NumComp <= 1) |
| 890 | continue; |
| 891 | DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); |
| 892 | SmallVector<LiveInterval*, 8> dups; |
| 893 | dups.push_back(li); |
| 894 | for (unsigned i = 1; i != NumComp; ++i) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 895 | dups.push_back(&Edit.create(MRI, LIS, VRM)); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 896 | rewriteComponents(dups, ConEQ); |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 897 | ConEQ.Distribute(&dups[0]); |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 900 | // Calculate spill weight and allocation hints for new intervals. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 901 | VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, sa_.Loops); |
| 902 | 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] | 903 | LiveInterval &li = **I; |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 904 | vrai.CalculateRegClass(li.reg); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 905 | vrai.CalculateWeightAndHint(li); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 906 | DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName() |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 907 | << ":" << li << '\n'); |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 908 | } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 912 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 913 | // Single Block Splitting |
| 914 | //===----------------------------------------------------------------------===// |
| 915 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 916 | /// getMultiUseBlocks - if CurLI has more than one use in a basic block, it |
| 917 | /// 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] | 918 | bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 919 | // 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] | 920 | if (LiveBlocks.size() <= 1) |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 921 | return false; |
| 922 | // Add blocks with multiple uses. |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 923 | for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) { |
| 924 | const BlockInfo &BI = LiveBlocks[i]; |
| 925 | if (!BI.Uses) |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 926 | continue; |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 927 | unsigned Instrs = UsingBlocks.lookup(BI.MBB); |
| 928 | if (Instrs <= 1) |
| 929 | continue; |
| 930 | if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough) |
| 931 | continue; |
| 932 | Blocks.insert(BI.MBB); |
| 933 | } |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 934 | return !Blocks.empty(); |
| 935 | } |
| 936 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 937 | /// splitSingleBlocks - Split CurLI into a separate live interval inside each |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 938 | /// basic block in Blocks. |
| 939 | void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 940 | DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 941 | |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 942 | for (unsigned i = 0, e = sa_.LiveBlocks.size(); i != e; ++i) { |
| 943 | const SplitAnalysis::BlockInfo &BI = sa_.LiveBlocks[i]; |
| 944 | if (!BI.Uses || !Blocks.count(BI.MBB)) |
| 945 | continue; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 946 | |
| 947 | openIntv(); |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 948 | SlotIndex SegStart = enterIntvBefore(BI.FirstUse); |
| 949 | if (BI.LastUse < BI.LastSplitPoint) { |
| 950 | useIntv(SegStart, leaveIntvAfter(BI.LastUse)); |
| 951 | } else { |
| 952 | // THe last use os after tha last valid split point. |
| 953 | SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint); |
| 954 | useIntv(SegStart, SegStop); |
| 955 | overlapIntv(SegStop, BI.LastUse); |
| 956 | } |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 957 | closeIntv(); |
| 958 | } |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 959 | finish(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 962 | |
| 963 | //===----------------------------------------------------------------------===// |
| 964 | // Sub Block Splitting |
| 965 | //===----------------------------------------------------------------------===// |
| 966 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 967 | /// getBlockForInsideSplit - If CurLI is contained inside a single basic block, |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 968 | /// and it wou pay to subdivide the interval inside that block, return it. |
| 969 | /// Otherwise return NULL. The returned block can be passed to |
| 970 | /// SplitEditor::splitInsideBlock. |
| 971 | const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() { |
| 972 | // The interval must be exclusive to one block. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 973 | if (UsingBlocks.size() != 1) |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 974 | return 0; |
| 975 | // Don't to this for less than 4 instructions. We want to be sure that |
| 976 | // splitting actually reduces the instruction count per interval. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 977 | if (UsingInstrs.size() < 4) |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 978 | return 0; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 979 | return UsingBlocks.begin()->first; |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 982 | /// splitInsideBlock - Split CurLI into multiple intervals inside MBB. |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 983 | void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 984 | SmallVector<SlotIndex, 32> Uses; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 985 | Uses.reserve(sa_.UsingInstrs.size()); |
| 986 | for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.UsingInstrs.begin(), |
| 987 | E = sa_.UsingInstrs.end(); I != E; ++I) |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 988 | if ((*I)->getParent() == MBB) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 989 | Uses.push_back(LIS.getInstructionIndex(*I)); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 990 | DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for " |
| 991 | << Uses.size() << " instructions.\n"); |
| 992 | assert(Uses.size() >= 3 && "Need at least 3 instructions"); |
| 993 | array_pod_sort(Uses.begin(), Uses.end()); |
| 994 | |
| 995 | // Simple algorithm: Find the largest gap between uses as determined by slot |
| 996 | // indices. Create new intervals for instructions before the gap and after the |
| 997 | // gap. |
| 998 | unsigned bestPos = 0; |
| 999 | int bestGap = 0; |
| 1000 | DEBUG(dbgs() << " dist (" << Uses[0]); |
| 1001 | for (unsigned i = 1, e = Uses.size(); i != e; ++i) { |
| 1002 | int g = Uses[i-1].distance(Uses[i]); |
| 1003 | DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]); |
| 1004 | if (g > bestGap) |
| 1005 | bestPos = i, bestGap = g; |
| 1006 | } |
| 1007 | DEBUG(dbgs() << "), best: -" << bestGap << "-\n"); |
| 1008 | |
| 1009 | // bestPos points to the first use after the best gap. |
| 1010 | assert(bestPos > 0 && "Invalid gap"); |
| 1011 | |
| 1012 | // FIXME: Don't create intervals for low densities. |
| 1013 | |
| 1014 | // First interval before the gap. Don't create single-instr intervals. |
| 1015 | if (bestPos > 1) { |
| 1016 | openIntv(); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 1017 | useIntv(enterIntvBefore(Uses.front()), leaveIntvAfter(Uses[bestPos-1])); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1018 | closeIntv(); |
| 1019 | } |
| 1020 | |
| 1021 | // Second interval after the gap. |
| 1022 | if (bestPos < Uses.size()-1) { |
| 1023 | openIntv(); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 1024 | useIntv(enterIntvBefore(Uses[bestPos]), leaveIntvAfter(Uses.back())); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1025 | closeIntv(); |
| 1026 | } |
| 1027 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1028 | finish(); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 1029 | } |