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