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 | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.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" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 31 | STATISTIC(NumFinished, "Number of splits finished"); |
| 32 | STATISTIC(NumSimple, "Number of splits that were simple"); |
Jakob Stoklund Olesen | e9bd4ea | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 33 | STATISTIC(NumCopies, "Number of copies inserted for splitting"); |
| 34 | STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); |
Jakob Stoklund Olesen | bdda37d | 2011-05-10 17:37:41 +0000 | [diff] [blame] | 35 | STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 36 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // Split Analysis |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 41 | SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 42 | const LiveIntervals &lis, |
| 43 | const MachineLoopInfo &mli) |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 44 | : MF(vrm.getMachineFunction()), |
| 45 | VRM(vrm), |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 46 | LIS(lis), |
| 47 | Loops(mli), |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 48 | TII(*MF.getTarget().getInstrInfo()), |
Jakob Stoklund Olesen | 1a77445 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 49 | CurLI(0), |
| 50 | LastSplitPoint(MF.getNumBlockIDs()) {} |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 51 | |
| 52 | void SplitAnalysis::clear() { |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 53 | UseSlots.clear(); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 54 | UseBlocks.clear(); |
| 55 | ThroughBlocks.clear(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 56 | CurLI = 0; |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 57 | DidRepairRange = false; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jakob Stoklund Olesen | 1a77445 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 60 | SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) { |
| 61 | const MachineBasicBlock *MBB = MF.getBlockNumbered(Num); |
| 62 | const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor(); |
| 63 | std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num]; |
| 64 | |
| 65 | // Compute split points on the first call. The pair is independent of the |
| 66 | // current live interval. |
| 67 | if (!LSP.first.isValid()) { |
| 68 | MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator(); |
| 69 | if (FirstTerm == MBB->end()) |
| 70 | LSP.first = LIS.getMBBEndIdx(MBB); |
| 71 | else |
| 72 | LSP.first = LIS.getInstructionIndex(FirstTerm); |
| 73 | |
| 74 | // If there is a landing pad successor, also find the call instruction. |
| 75 | if (!LPad) |
| 76 | return LSP.first; |
| 77 | // There may not be a call instruction (?) in which case we ignore LPad. |
| 78 | LSP.second = LSP.first; |
| 79 | for (MachineBasicBlock::const_iterator I = FirstTerm, E = MBB->begin(); |
| 80 | I != E; --I) |
| 81 | if (I->getDesc().isCall()) { |
| 82 | LSP.second = LIS.getInstructionIndex(I); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // If CurLI is live into a landing pad successor, move the last split point |
| 88 | // back to the call that may throw. |
Jakob Stoklund Olesen | 71d9e65 | 2011-04-05 23:43:16 +0000 | [diff] [blame] | 89 | if (LPad && LSP.second.isValid() && LIS.isLiveInToMBB(*CurLI, LPad)) |
Jakob Stoklund Olesen | 1a77445 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 90 | return LSP.second; |
| 91 | else |
| 92 | return LSP.first; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 95 | /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 96 | void SplitAnalysis::analyzeUses() { |
Jakob Stoklund Olesen | a2948ef | 2011-04-05 15:18:18 +0000 | [diff] [blame] | 97 | assert(UseSlots.empty() && "Call clear first"); |
| 98 | |
| 99 | // First get all the defs from the interval values. This provides the correct |
| 100 | // slots for early clobbers. |
| 101 | for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(), |
| 102 | E = CurLI->vni_end(); I != E; ++I) |
| 103 | if (!(*I)->isPHIDef() && !(*I)->isUnused()) |
| 104 | UseSlots.push_back((*I)->def); |
| 105 | |
| 106 | // Get use slots form the use-def chain. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 107 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Jakob Stoklund Olesen | a2948ef | 2011-04-05 15:18:18 +0000 | [diff] [blame] | 108 | for (MachineRegisterInfo::use_nodbg_iterator |
| 109 | I = MRI.use_nodbg_begin(CurLI->reg), E = MRI.use_nodbg_end(); I != E; |
| 110 | ++I) |
| 111 | if (!I.getOperand().isUndef()) |
| 112 | UseSlots.push_back(LIS.getInstructionIndex(&*I).getDefIndex()); |
| 113 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 114 | array_pod_sort(UseSlots.begin(), UseSlots.end()); |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | a2948ef | 2011-04-05 15:18:18 +0000 | [diff] [blame] | 116 | // Remove duplicates, keeping the smaller slot for each instruction. |
| 117 | // That is what we want for early clobbers. |
| 118 | UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), |
| 119 | SlotIndex::isSameInstr), |
| 120 | UseSlots.end()); |
| 121 | |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 122 | // Compute per-live block info. |
| 123 | if (!calcLiveBlockInfo()) { |
| 124 | // FIXME: calcLiveBlockInfo found inconsistencies in the live range. |
| 125 | // I am looking at you, SimpleRegisterCoalescing! |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 126 | DidRepairRange = true; |
Jakob Stoklund Olesen | bdda37d | 2011-05-10 17:37:41 +0000 | [diff] [blame] | 127 | ++NumRepairs; |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 128 | DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); |
| 129 | const_cast<LiveIntervals&>(LIS) |
| 130 | .shrinkToUses(const_cast<LiveInterval*>(CurLI)); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 131 | UseBlocks.clear(); |
| 132 | ThroughBlocks.clear(); |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 133 | bool fixed = calcLiveBlockInfo(); |
| 134 | (void)fixed; |
| 135 | assert(fixed && "Couldn't fix broken live interval"); |
| 136 | } |
| 137 | |
Jakob Stoklund Olesen | ef1f5cc | 2011-03-27 22:49:23 +0000 | [diff] [blame] | 138 | DEBUG(dbgs() << "Analyze counted " |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 139 | << UseSlots.size() << " instrs in " |
| 140 | << UseBlocks.size() << " blocks, through " |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 141 | << NumThroughBlocks << " blocks.\n"); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 144 | /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks |
| 145 | /// where CurLI is live. |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 146 | bool SplitAnalysis::calcLiveBlockInfo() { |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 147 | ThroughBlocks.resize(MF.getNumBlockIDs()); |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 148 | NumThroughBlocks = NumGapBlocks = 0; |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 149 | if (CurLI->empty()) |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 150 | return true; |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 151 | |
| 152 | LiveInterval::const_iterator LVI = CurLI->begin(); |
| 153 | LiveInterval::const_iterator LVE = CurLI->end(); |
| 154 | |
| 155 | SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; |
| 156 | UseI = UseSlots.begin(); |
| 157 | UseE = UseSlots.end(); |
| 158 | |
| 159 | // Loop over basic blocks where CurLI is live. |
| 160 | MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start); |
| 161 | for (;;) { |
| 162 | BlockInfo BI; |
| 163 | BI.MBB = MFI; |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 164 | SlotIndex Start, Stop; |
| 165 | tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 166 | |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 167 | // If the block contains no uses, the range must be live through. At one |
| 168 | // point, SimpleRegisterCoalescing could create dangling ranges that ended |
| 169 | // mid-block. |
| 170 | if (UseI == UseE || *UseI >= Stop) { |
| 171 | ++NumThroughBlocks; |
| 172 | ThroughBlocks.set(BI.MBB->getNumber()); |
| 173 | // The range shouldn't end mid-block if there are no uses. This shouldn't |
| 174 | // happen. |
| 175 | if (LVI->end < Stop) |
| 176 | return false; |
| 177 | } else { |
| 178 | // This block has uses. Find the first and last uses in the block. |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 179 | BI.FirstUse = *UseI; |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 180 | assert(BI.FirstUse >= Start); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 181 | do ++UseI; |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 182 | while (UseI != UseE && *UseI < Stop); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 183 | BI.LastUse = UseI[-1]; |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 184 | assert(BI.LastUse < Stop); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 185 | |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 186 | // LVI is the first live segment overlapping MBB. |
| 187 | BI.LiveIn = LVI->start <= Start; |
Jakob Stoklund Olesen | 626d6fb | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 188 | |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 189 | // Look for gaps in the live range. |
| 190 | BI.LiveOut = true; |
| 191 | while (LVI->end < Stop) { |
| 192 | SlotIndex LastStop = LVI->end; |
| 193 | if (++LVI == LVE || LVI->start >= Stop) { |
| 194 | BI.LiveOut = false; |
| 195 | BI.LastUse = LastStop; |
| 196 | break; |
| 197 | } |
| 198 | if (LastStop < LVI->start) { |
| 199 | // There is a gap in the live range. Create duplicate entries for the |
| 200 | // live-in snippet and the live-out snippet. |
| 201 | ++NumGapBlocks; |
| 202 | |
| 203 | // Push the Live-in part. |
| 204 | BI.LiveThrough = false; |
| 205 | BI.LiveOut = false; |
| 206 | UseBlocks.push_back(BI); |
| 207 | UseBlocks.back().LastUse = LastStop; |
| 208 | |
| 209 | // Set up BI for the live-out part. |
| 210 | BI.LiveIn = false; |
| 211 | BI.LiveOut = true; |
| 212 | BI.FirstUse = LVI->start; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Don't set LiveThrough when the block has a gap. |
| 217 | BI.LiveThrough = BI.LiveIn && BI.LiveOut; |
Jakob Stoklund Olesen | 626d6fb | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 218 | UseBlocks.push_back(BI); |
Jakob Stoklund Olesen | 626d6fb | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 219 | |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 220 | // LVI is now at LVE or LVI->end >= Stop. |
| 221 | if (LVI == LVE) |
| 222 | break; |
| 223 | } |
Jakob Stoklund Olesen | 626d6fb | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 224 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 225 | // Live segment ends exactly at Stop. Move to the next segment. |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 226 | if (LVI->end == Stop && ++LVI == LVE) |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 227 | break; |
| 228 | |
| 229 | // Pick the next basic block. |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 230 | if (LVI->start < Stop) |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 231 | ++MFI; |
| 232 | else |
| 233 | MFI = LIS.getMBBFromIndex(LVI->start); |
| 234 | } |
Jakob Stoklund Olesen | b2abfa0 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 235 | |
| 236 | assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 237 | return true; |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jakob Stoklund Olesen | 9f4b893 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 240 | unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { |
| 241 | if (cli->empty()) |
| 242 | return 0; |
| 243 | LiveInterval *li = const_cast<LiveInterval*>(cli); |
| 244 | LiveInterval::iterator LVI = li->begin(); |
| 245 | LiveInterval::iterator LVE = li->end(); |
| 246 | unsigned Count = 0; |
| 247 | |
| 248 | // Loop over basic blocks where li is live. |
| 249 | MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start); |
| 250 | SlotIndex Stop = LIS.getMBBEndIdx(MFI); |
| 251 | for (;;) { |
| 252 | ++Count; |
| 253 | LVI = li->advanceTo(LVI, Stop); |
| 254 | if (LVI == LVE) |
| 255 | return Count; |
| 256 | do { |
| 257 | ++MFI; |
| 258 | Stop = LIS.getMBBEndIdx(MFI); |
| 259 | } while (Stop <= LVI->start); |
| 260 | } |
| 261 | } |
| 262 | |
Jakob Stoklund Olesen | 06c0f25 | 2011-02-21 23:09:46 +0000 | [diff] [blame] | 263 | bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { |
| 264 | unsigned OrigReg = VRM.getOriginal(CurLI->reg); |
| 265 | const LiveInterval &Orig = LIS.getInterval(OrigReg); |
| 266 | assert(!Orig.empty() && "Splitting empty interval?"); |
| 267 | LiveInterval::const_iterator I = Orig.find(Idx); |
| 268 | |
| 269 | // Range containing Idx should begin at Idx. |
| 270 | if (I != Orig.end() && I->start <= Idx) |
| 271 | return I->start == Idx; |
| 272 | |
| 273 | // Range does not contain Idx, previous must end at Idx. |
| 274 | return I != Orig.begin() && (--I)->end == Idx; |
| 275 | } |
| 276 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 277 | void SplitAnalysis::analyze(const LiveInterval *li) { |
| 278 | clear(); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 279 | CurLI = li; |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 280 | analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Jakob Stoklund Olesen | 697483a | 2010-12-15 17:49:52 +0000 | [diff] [blame] | 283 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 284 | //===----------------------------------------------------------------------===// |
| 285 | // Split Editor |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
| 288 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 289 | SplitEditor::SplitEditor(SplitAnalysis &sa, |
| 290 | LiveIntervals &lis, |
| 291 | VirtRegMap &vrm, |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 292 | MachineDominatorTree &mdt) |
Jakob Stoklund Olesen | 0eeca44 | 2011-02-19 00:42:33 +0000 | [diff] [blame] | 293 | : SA(sa), LIS(lis), VRM(vrm), |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 294 | MRI(vrm.getMachineFunction().getRegInfo()), |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 295 | MDT(mdt), |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 296 | TII(*vrm.getMachineFunction().getTarget().getInstrInfo()), |
| 297 | TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()), |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 298 | Edit(0), |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 299 | OpenIdx(0), |
| 300 | RegAssign(Allocator) |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 301 | {} |
| 302 | |
| 303 | void SplitEditor::reset(LiveRangeEdit &lre) { |
| 304 | Edit = &lre; |
| 305 | OpenIdx = 0; |
| 306 | RegAssign.clear(); |
| 307 | Values.clear(); |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 308 | |
| 309 | // We don't need to clear LiveOutCache, only LiveOutSeen entries are read. |
| 310 | LiveOutSeen.clear(); |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 311 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 312 | // We don't need an AliasAnalysis since we will only be performing |
| 313 | // cheap-as-a-copy remats anyway. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 314 | Edit->anyRematerializable(LIS, TII, 0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 317 | void SplitEditor::dump() const { |
| 318 | if (RegAssign.empty()) { |
| 319 | dbgs() << " empty\n"; |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) |
| 324 | dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); |
| 325 | dbgs() << '\n'; |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 328 | VNInfo *SplitEditor::defValue(unsigned RegIdx, |
| 329 | const VNInfo *ParentVNI, |
| 330 | SlotIndex Idx) { |
| 331 | assert(ParentVNI && "Mapping NULL value"); |
| 332 | assert(Idx.isValid() && "Invalid SlotIndex"); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 333 | assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); |
| 334 | LiveInterval *LI = Edit->get(RegIdx); |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 335 | |
| 336 | // Create a new value. |
| 337 | VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator()); |
| 338 | |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 339 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 340 | std::pair<ValueMap::iterator, bool> InsP = |
| 341 | Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI)); |
| 342 | |
| 343 | // This was the first time (RegIdx, ParentVNI) was mapped. |
| 344 | // Keep it as a simple def without any liveness. |
| 345 | if (InsP.second) |
| 346 | return VNI; |
| 347 | |
| 348 | // If the previous value was a simple mapping, add liveness for it now. |
| 349 | if (VNInfo *OldVNI = InsP.first->second) { |
| 350 | SlotIndex Def = OldVNI->def; |
| 351 | LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI)); |
| 352 | // No longer a simple mapping. |
| 353 | InsP.first->second = 0; |
| 354 | } |
| 355 | |
| 356 | // This is a complex mapping, add liveness for VNI |
| 357 | SlotIndex Def = VNI->def; |
| 358 | LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI)); |
| 359 | |
| 360 | return VNI; |
| 361 | } |
| 362 | |
| 363 | void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) { |
| 364 | assert(ParentVNI && "Mapping NULL value"); |
| 365 | VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)]; |
| 366 | |
| 367 | // ParentVNI was either unmapped or already complex mapped. Either way. |
| 368 | if (!VNI) |
| 369 | return; |
| 370 | |
| 371 | // This was previously a single mapping. Make sure the old def is represented |
| 372 | // by a trivial live range. |
| 373 | SlotIndex Def = VNI->def; |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 374 | Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI)); |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 375 | VNI = 0; |
| 376 | } |
| 377 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 378 | // extendRange - Extend the live range to reach Idx. |
| 379 | // Potentially create phi-def values. |
| 380 | void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) { |
| 381 | assert(Idx.isValid() && "Invalid SlotIndex"); |
| 382 | MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx); |
| 383 | assert(IdxMBB && "No MBB at Idx"); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 384 | LiveInterval *LI = Edit->get(RegIdx); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 385 | |
| 386 | // Is there a def in the same MBB we can extend? |
| 387 | if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx)) |
| 388 | return; |
| 389 | |
| 390 | // Now for the fun part. We know that ParentVNI potentially has multiple defs, |
| 391 | // and we may need to create even more phi-defs to preserve VNInfo SSA form. |
| 392 | // Perform a search for all predecessor blocks where we know the dominating |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 393 | // VNInfo. |
| 394 | VNInfo *VNI = findReachingDefs(LI, IdxMBB, Idx.getNextSlot()); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 395 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 396 | // When there were multiple different values, we may need new PHIs. |
| 397 | if (!VNI) |
| 398 | return updateSSA(); |
| 399 | |
| 400 | // Poor man's SSA update for the single-value case. |
| 401 | LiveOutPair LOP(VNI, MDT[LIS.getMBBFromIndex(VNI->def)]); |
| 402 | for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(), |
| 403 | E = LiveInBlocks.end(); I != E; ++I) { |
| 404 | MachineBasicBlock *MBB = I->DomNode->getBlock(); |
| 405 | SlotIndex Start = LIS.getMBBStartIdx(MBB); |
| 406 | if (I->Kill.isValid()) |
| 407 | LI->addRange(LiveRange(Start, I->Kill, VNI)); |
| 408 | else { |
| 409 | LiveOutCache[MBB] = LOP; |
| 410 | LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /// findReachingDefs - Search the CFG for known live-out values. |
| 416 | /// Add required live-in blocks to LiveInBlocks. |
| 417 | VNInfo *SplitEditor::findReachingDefs(LiveInterval *LI, |
| 418 | MachineBasicBlock *KillMBB, |
| 419 | SlotIndex Kill) { |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 420 | // Initialize the live-out cache the first time it is needed. |
| 421 | if (LiveOutSeen.empty()) { |
| 422 | unsigned N = VRM.getMachineFunction().getNumBlockIDs(); |
| 423 | LiveOutSeen.resize(N); |
| 424 | LiveOutCache.resize(N); |
| 425 | } |
| 426 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 427 | // Blocks where LI should be live-in. |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 428 | SmallVector<MachineBasicBlock*, 16> WorkList(1, KillMBB); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 429 | |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 430 | // Remember if we have seen more than one value. |
| 431 | bool UniqueVNI = true; |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 432 | VNInfo *TheVNI = 0; |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 433 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 434 | // Using LiveOutCache as a visited set, perform a BFS for all reaching defs. |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 435 | for (unsigned i = 0; i != WorkList.size(); ++i) { |
| 436 | MachineBasicBlock *MBB = WorkList[i]; |
Jakob Stoklund Olesen | 7cec179 | 2011-03-18 03:06:02 +0000 | [diff] [blame] | 437 | assert(!MBB->pred_empty() && "Value live-in to entry block?"); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 438 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 439 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 440 | MachineBasicBlock *Pred = *PI; |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 441 | LiveOutPair &LOP = LiveOutCache[Pred]; |
| 442 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 443 | // Is this a known live-out block? |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 444 | if (LiveOutSeen.test(Pred->getNumber())) { |
| 445 | if (VNInfo *VNI = LOP.first) { |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 446 | if (TheVNI && TheVNI != VNI) |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 447 | UniqueVNI = false; |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 448 | TheVNI = VNI; |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 449 | } |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 450 | continue; |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 451 | } |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 452 | |
| 453 | // First time. LOP is garbage and must be cleared below. |
| 454 | LiveOutSeen.set(Pred->getNumber()); |
| 455 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 456 | // Does Pred provide a live-out value? |
| 457 | SlotIndex Start, Last; |
| 458 | tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred); |
| 459 | Last = Last.getPrevSlot(); |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 460 | VNInfo *VNI = LI->extendInBlock(Start, Last); |
| 461 | LOP.first = VNI; |
| 462 | if (VNI) { |
| 463 | LOP.second = MDT[LIS.getMBBFromIndex(VNI->def)]; |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 464 | if (TheVNI && TheVNI != VNI) |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 465 | UniqueVNI = false; |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 466 | TheVNI = VNI; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 467 | continue; |
| 468 | } |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 469 | LOP.second = 0; |
| 470 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 471 | // No, we need a live-in value for Pred as well |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 472 | if (Pred != KillMBB) |
| 473 | WorkList.push_back(Pred); |
Jakob Stoklund Olesen | 8701768 | 2011-03-03 01:29:10 +0000 | [diff] [blame] | 474 | else |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 475 | // Loopback to KillMBB, so value is really live through. |
| 476 | Kill = SlotIndex(); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 480 | // Transfer WorkList to LiveInBlocks in reverse order. |
| 481 | // This ordering works best with updateSSA(). |
| 482 | LiveInBlocks.clear(); |
| 483 | LiveInBlocks.reserve(WorkList.size()); |
| 484 | while(!WorkList.empty()) |
| 485 | LiveInBlocks.push_back(MDT[WorkList.pop_back_val()]); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 486 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 487 | // The kill block may not be live-through. |
| 488 | assert(LiveInBlocks.back().DomNode->getBlock() == KillMBB); |
| 489 | LiveInBlocks.back().Kill = Kill; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 490 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 491 | return UniqueVNI ? TheVNI : 0; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 494 | void SplitEditor::updateSSA() { |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 495 | // This is essentially the same iterative algorithm that SSAUpdater uses, |
| 496 | // except we already have a dominator tree, so we don't have to recompute it. |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 497 | unsigned Changes; |
| 498 | do { |
| 499 | Changes = 0; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 500 | // Propagate live-out values down the dominator tree, inserting phi-defs |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 501 | // when necessary. |
| 502 | for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(), |
| 503 | E = LiveInBlocks.end(); I != E; ++I) { |
| 504 | MachineDomTreeNode *Node = I->DomNode; |
| 505 | // Skip block if the live-in value has already been determined. |
| 506 | if (!Node) |
| 507 | continue; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 508 | MachineBasicBlock *MBB = Node->getBlock(); |
| 509 | MachineDomTreeNode *IDom = Node->getIDom(); |
| 510 | LiveOutPair IDomValue; |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 511 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 512 | // We need a live-in value to a block with no immediate dominator? |
| 513 | // This is probably an unreachable block that has survived somehow. |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 514 | bool needPHI = !IDom || !LiveOutSeen.test(IDom->getBlock()->getNumber()); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 515 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 516 | // IDom dominates all of our predecessors, but it may not be their |
| 517 | // immediate dominator. Check if any of them have live-out values that are |
| 518 | // properly dominated by IDom. If so, we need a phi-def here. |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 519 | if (!needPHI) { |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 520 | IDomValue = LiveOutCache[IDom->getBlock()]; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 521 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 522 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 523 | LiveOutPair Value = LiveOutCache[*PI]; |
| 524 | if (!Value.first || Value.first == IDomValue.first) |
| 525 | continue; |
| 526 | // This predecessor is carrying something other than IDomValue. |
| 527 | // It could be because IDomValue hasn't propagated yet, or it could be |
| 528 | // because MBB is in the dominance frontier of that value. |
| 529 | if (MDT.dominates(IDom, Value.second)) { |
| 530 | needPHI = true; |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 536 | // The value may be live-through even if Kill is set, as can happen when |
| 537 | // we are called from extendRange. In that case LiveOutSeen is true, and |
| 538 | // LiveOutCache indicates a foreign or missing value. |
| 539 | LiveOutPair &LOP = LiveOutCache[MBB]; |
| 540 | |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 541 | // Create a phi-def if required. |
| 542 | if (needPHI) { |
| 543 | ++Changes; |
| 544 | SlotIndex Start = LIS.getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 545 | unsigned RegIdx = RegAssign.lookup(Start); |
| 546 | LiveInterval *LI = Edit->get(RegIdx); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 547 | VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator()); |
| 548 | VNI->setIsPHIDef(true); |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 549 | I->Value = VNI; |
| 550 | // This block is done, we know the final value. |
| 551 | I->DomNode = 0; |
| 552 | if (I->Kill.isValid()) |
| 553 | LI->addRange(LiveRange(Start, I->Kill, VNI)); |
| 554 | else { |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 555 | LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI)); |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 556 | LOP = LiveOutPair(VNI, Node); |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 557 | } |
| 558 | } else if (IDomValue.first) { |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 559 | // No phi-def here. Remember incoming value. |
| 560 | I->Value = IDomValue.first; |
| 561 | if (I->Kill.isValid()) |
| 562 | continue; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 563 | // Propagate IDomValue if needed: |
| 564 | // MBB is live-out and doesn't define its own value. |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 565 | if (LOP.second != Node && LOP.first != IDomValue.first) { |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 566 | ++Changes; |
Jakob Stoklund Olesen | 13ba2da | 2011-03-04 00:15:36 +0000 | [diff] [blame] | 567 | LOP = IDomValue; |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | } |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 571 | } while (Changes); |
| 572 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 573 | // The values in LiveInBlocks are now accurate. No more phi-defs are needed |
| 574 | // for these blocks, so we can color the live ranges. |
| 575 | for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(), |
| 576 | E = LiveInBlocks.end(); I != E; ++I) { |
| 577 | if (!I->DomNode) |
| 578 | continue; |
| 579 | assert(I->Value && "No live-in value found"); |
| 580 | MachineBasicBlock *MBB = I->DomNode->getBlock(); |
| 581 | SlotIndex Start = LIS.getMBBStartIdx(MBB); |
| 582 | unsigned RegIdx = RegAssign.lookup(Start); |
| 583 | LiveInterval *LI = Edit->get(RegIdx); |
| 584 | LI->addRange(LiveRange(Start, I->Kill.isValid() ? |
| 585 | I->Kill : LIS.getMBBEndIdx(MBB), I->Value)); |
| 586 | } |
Jakob Stoklund Olesen | 1c38ba6 | 2011-03-02 01:59:34 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 589 | VNInfo *SplitEditor::defFromParent(unsigned RegIdx, |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 590 | VNInfo *ParentVNI, |
| 591 | SlotIndex UseIdx, |
| 592 | MachineBasicBlock &MBB, |
| 593 | MachineBasicBlock::iterator I) { |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 594 | MachineInstr *CopyMI = 0; |
| 595 | SlotIndex Def; |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 596 | LiveInterval *LI = Edit->get(RegIdx); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 597 | |
Jakob Stoklund Olesen | bb30dd4 | 2011-05-02 05:29:58 +0000 | [diff] [blame] | 598 | // We may be trying to avoid interference that ends at a deleted instruction, |
| 599 | // so always begin RegIdx 0 early and all others late. |
| 600 | bool Late = RegIdx != 0; |
| 601 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 602 | // Attempt cheap-as-a-copy rematerialization. |
| 603 | LiveRangeEdit::Remat RM(ParentVNI); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 604 | if (Edit->canRematerializeAt(RM, UseIdx, true, LIS)) { |
Jakob Stoklund Olesen | bb30dd4 | 2011-05-02 05:29:58 +0000 | [diff] [blame] | 605 | Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI, Late); |
Jakob Stoklund Olesen | e9bd4ea | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 606 | ++NumRemats; |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 607 | } else { |
| 608 | // Can't remat, just insert a copy from parent. |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 609 | CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 610 | .addReg(Edit->getReg()); |
Jakob Stoklund Olesen | bb30dd4 | 2011-05-02 05:29:58 +0000 | [diff] [blame] | 611 | Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late) |
| 612 | .getDefIndex(); |
Jakob Stoklund Olesen | e9bd4ea | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 613 | ++NumCopies; |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 616 | // Define the value in Reg. |
| 617 | VNInfo *VNI = defValue(RegIdx, ParentVNI, Def); |
| 618 | VNI->setCopy(CopyMI); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 619 | return VNI; |
| 620 | } |
| 621 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 622 | /// Create a new virtual register and live interval. |
Jakob Stoklund Olesen | e1b43c3 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 623 | unsigned SplitEditor::openIntv() { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 624 | // Create the complement as index 0. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 625 | if (Edit->empty()) |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 626 | Edit->create(LIS, VRM); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 627 | |
| 628 | // Create the open interval. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 629 | OpenIdx = Edit->size(); |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 630 | Edit->create(LIS, VRM); |
Jakob Stoklund Olesen | e1b43c3 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 631 | return OpenIdx; |
| 632 | } |
| 633 | |
| 634 | void SplitEditor::selectIntv(unsigned Idx) { |
| 635 | assert(Idx != 0 && "Cannot select the complement interval"); |
| 636 | assert(Idx < Edit->size() && "Can only select previously opened interval"); |
| 637 | OpenIdx = Idx; |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 640 | SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 641 | assert(OpenIdx && "openIntv not called before enterIntvBefore"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 642 | DEBUG(dbgs() << " enterIntvBefore " << Idx); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 643 | Idx = Idx.getBaseIndex(); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 644 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 645 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 646 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 647 | return Idx; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 648 | } |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 649 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 650 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 651 | assert(MI && "enterIntvBefore called with invalid index"); |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 652 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 653 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); |
| 654 | return VNI->def; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 657 | SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 658 | assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 659 | SlotIndex End = LIS.getMBBEndIdx(&MBB); |
| 660 | SlotIndex Last = End.getPrevSlot(); |
| 661 | DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 662 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 663 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 664 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 665 | return End; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 666 | } |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 667 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 668 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 669 | LIS.getLastSplitPoint(Edit->getParent(), &MBB)); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 670 | RegAssign.insert(VNI->def, End, OpenIdx); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 671 | DEBUG(dump()); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 672 | return VNI->def; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 675 | /// useIntv - indicate that all instructions in MBB should use OpenLI. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 676 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 677 | useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 680 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 681 | assert(OpenIdx && "openIntv not called before useIntv"); |
| 682 | DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); |
| 683 | RegAssign.insert(Start, End, OpenIdx); |
| 684 | DEBUG(dump()); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 687 | SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 688 | assert(OpenIdx && "openIntv not called before leaveIntvAfter"); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 689 | DEBUG(dbgs() << " leaveIntvAfter " << Idx); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 690 | |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 691 | // The interval must be live beyond the instruction at Idx. |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 692 | Idx = Idx.getBoundaryIndex(); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 693 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 694 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 695 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 696 | return Idx.getNextSlot(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 697 | } |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 698 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 699 | |
Jakob Stoklund Olesen | 01cb34b | 2011-02-08 18:50:18 +0000 | [diff] [blame] | 700 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
| 701 | assert(MI && "No instruction at index"); |
| 702 | VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), |
| 703 | llvm::next(MachineBasicBlock::iterator(MI))); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 704 | return VNI->def; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 707 | SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { |
| 708 | assert(OpenIdx && "openIntv not called before leaveIntvBefore"); |
| 709 | DEBUG(dbgs() << " leaveIntvBefore " << Idx); |
| 710 | |
| 711 | // The interval must be live into the instruction at Idx. |
| 712 | Idx = Idx.getBoundaryIndex(); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 713 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 714 | if (!ParentVNI) { |
| 715 | DEBUG(dbgs() << ": not live\n"); |
| 716 | return Idx.getNextSlot(); |
| 717 | } |
| 718 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
| 719 | |
| 720 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
| 721 | assert(MI && "No instruction at index"); |
| 722 | VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); |
| 723 | return VNI->def; |
| 724 | } |
| 725 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 726 | SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 727 | assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 728 | SlotIndex Start = LIS.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 729 | DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 730 | |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 731 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 732 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9b24afe | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 733 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 734 | return Start; |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 737 | VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 738 | MBB.SkipPHIsAndLabels(MBB.begin())); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 739 | RegAssign.insert(Start, VNI->def, OpenIdx); |
| 740 | DEBUG(dump()); |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 741 | return VNI->def; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Jakob Stoklund Olesen | 5c716bd | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 744 | void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { |
| 745 | assert(OpenIdx && "openIntv not called before overlapIntv"); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 746 | const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); |
| 747 | assert(ParentVNI == Edit->getParent().getVNInfoAt(End.getPrevSlot()) && |
Jakob Stoklund Olesen | 5c716bd | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 748 | "Parent changes value in extended range"); |
Jakob Stoklund Olesen | 5c716bd | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 749 | assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && |
| 750 | "Range cannot span basic blocks"); |
| 751 | |
Jakob Stoklund Olesen | d3fdaeb | 2011-03-02 00:49:28 +0000 | [diff] [blame] | 752 | // The complement interval will be extended as needed by extendRange(). |
Jakob Stoklund Olesen | b3dd826 | 2011-04-05 23:43:14 +0000 | [diff] [blame] | 753 | if (ParentVNI) |
| 754 | markComplexMapped(0, ParentVNI); |
Jakob Stoklund Olesen | 5c716bd | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 755 | DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); |
| 756 | RegAssign.insert(Start, End, OpenIdx); |
| 757 | DEBUG(dump()); |
| 758 | } |
| 759 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 760 | /// transferValues - Transfer all possible values to the new live ranges. |
| 761 | /// Values that were rematerialized are left alone, they need extendRange(). |
| 762 | bool SplitEditor::transferValues() { |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 763 | bool Skipped = false; |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 764 | LiveInBlocks.clear(); |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 765 | RegAssignMap::const_iterator AssignI = RegAssign.begin(); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 766 | for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(), |
| 767 | ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) { |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 768 | DEBUG(dbgs() << " blit " << *ParentI << ':'); |
| 769 | VNInfo *ParentVNI = ParentI->valno; |
| 770 | // RegAssign has holes where RegIdx 0 should be used. |
| 771 | SlotIndex Start = ParentI->start; |
| 772 | AssignI.advanceTo(Start); |
| 773 | do { |
| 774 | unsigned RegIdx; |
| 775 | SlotIndex End = ParentI->end; |
| 776 | if (!AssignI.valid()) { |
| 777 | RegIdx = 0; |
| 778 | } else if (AssignI.start() <= Start) { |
| 779 | RegIdx = AssignI.value(); |
| 780 | if (AssignI.stop() < End) { |
| 781 | End = AssignI.stop(); |
| 782 | ++AssignI; |
| 783 | } |
| 784 | } else { |
| 785 | RegIdx = 0; |
| 786 | End = std::min(End, AssignI.start()); |
| 787 | } |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 788 | |
| 789 | // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 790 | DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx); |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 791 | LiveInterval *LI = Edit->get(RegIdx); |
| 792 | |
| 793 | // Check for a simply defined value that can be blitted directly. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 794 | if (VNInfo *VNI = Values.lookup(std::make_pair(RegIdx, ParentVNI->id))) { |
| 795 | DEBUG(dbgs() << ':' << VNI->id); |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 796 | LI->addRange(LiveRange(Start, End, VNI)); |
| 797 | Start = End; |
| 798 | continue; |
| 799 | } |
| 800 | |
| 801 | // Skip rematerialized values, we need to use extendRange() and |
| 802 | // extendPHIKillRanges() to completely recompute the live ranges. |
| 803 | if (Edit->didRematerialize(ParentVNI)) { |
| 804 | DEBUG(dbgs() << "(remat)"); |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 805 | Skipped = true; |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 806 | Start = End; |
| 807 | continue; |
| 808 | } |
| 809 | |
| 810 | // Initialize the live-out cache the first time it is needed. |
| 811 | if (LiveOutSeen.empty()) { |
| 812 | unsigned N = VRM.getMachineFunction().getNumBlockIDs(); |
| 813 | LiveOutSeen.resize(N); |
| 814 | LiveOutCache.resize(N); |
| 815 | } |
| 816 | |
| 817 | // This value has multiple defs in RegIdx, but it wasn't rematerialized, |
| 818 | // so the live range is accurate. Add live-in blocks in [Start;End) to the |
| 819 | // LiveInBlocks. |
| 820 | MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); |
| 821 | SlotIndex BlockStart, BlockEnd; |
| 822 | tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB); |
| 823 | |
| 824 | // The first block may be live-in, or it may have its own def. |
| 825 | if (Start != BlockStart) { |
| 826 | VNInfo *VNI = LI->extendInBlock(BlockStart, |
| 827 | std::min(BlockEnd, End).getPrevSlot()); |
| 828 | assert(VNI && "Missing def for complex mapped value"); |
| 829 | DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber()); |
| 830 | // MBB has its own def. Is it also live-out? |
| 831 | if (BlockEnd <= End) { |
| 832 | LiveOutSeen.set(MBB->getNumber()); |
| 833 | LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]); |
| 834 | } |
| 835 | // Skip to the next block for live-in. |
| 836 | ++MBB; |
| 837 | BlockStart = BlockEnd; |
| 838 | } |
| 839 | |
| 840 | // Handle the live-in blocks covered by [Start;End). |
| 841 | assert(Start <= BlockStart && "Expected live-in block"); |
| 842 | while (BlockStart < End) { |
| 843 | DEBUG(dbgs() << ">BB#" << MBB->getNumber()); |
| 844 | BlockEnd = LIS.getMBBEndIdx(MBB); |
| 845 | if (BlockStart == ParentVNI->def) { |
| 846 | // This block has the def of a parent PHI, so it isn't live-in. |
| 847 | assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); |
| 848 | VNInfo *VNI = LI->extendInBlock(BlockStart, |
| 849 | std::min(BlockEnd, End).getPrevSlot()); |
| 850 | assert(VNI && "Missing def for complex mapped parent PHI"); |
| 851 | if (End >= BlockEnd) { |
| 852 | // Live-out as well. |
| 853 | LiveOutSeen.set(MBB->getNumber()); |
| 854 | LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]); |
| 855 | } |
| 856 | } else { |
| 857 | // This block needs a live-in value. |
| 858 | LiveInBlocks.push_back(MDT[MBB]); |
| 859 | // The last block covered may not be live-out. |
| 860 | if (End < BlockEnd) |
| 861 | LiveInBlocks.back().Kill = End; |
| 862 | else { |
| 863 | // Live-out, but we need updateSSA to tell us the value. |
| 864 | LiveOutSeen.set(MBB->getNumber()); |
Francois Pichet | cbc5f40 | 2011-04-16 14:20:39 +0000 | [diff] [blame] | 865 | LiveOutCache[MBB] = LiveOutPair((VNInfo*)0, |
| 866 | (MachineDomTreeNode*)0); |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | BlockStart = BlockEnd; |
| 870 | ++MBB; |
| 871 | } |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 872 | Start = End; |
| 873 | } while (Start != ParentI->end); |
| 874 | DEBUG(dbgs() << '\n'); |
| 875 | } |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 876 | |
| 877 | if (!LiveInBlocks.empty()) |
| 878 | updateSSA(); |
| 879 | |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 880 | return Skipped; |
| 881 | } |
| 882 | |
Jakob Stoklund Olesen | e2dc0c9 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 883 | void SplitEditor::extendPHIKillRanges() { |
| 884 | // Extend live ranges to be live-out for successor PHI values. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 885 | for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(), |
| 886 | E = Edit->getParent().vni_end(); I != E; ++I) { |
Jakob Stoklund Olesen | e2dc0c9 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 887 | const VNInfo *PHIVNI = *I; |
| 888 | if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) |
| 889 | continue; |
| 890 | unsigned RegIdx = RegAssign.lookup(PHIVNI->def); |
| 891 | MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); |
| 892 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 893 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 894 | SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot(); |
| 895 | // The predecessor may not have a live-out value. That is OK, like an |
| 896 | // undef PHI operand. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 897 | if (Edit->getParent().liveAt(End)) { |
Jakob Stoklund Olesen | e2dc0c9 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 898 | assert(RegAssign.lookup(End) == RegIdx && |
| 899 | "Different register assignment in phi predecessor"); |
| 900 | extendRange(RegIdx, End); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 906 | /// rewriteAssigned - Rewrite all uses of Edit->getReg(). |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 907 | void SplitEditor::rewriteAssigned(bool ExtendRanges) { |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 908 | for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 909 | RE = MRI.reg_end(); RI != RE;) { |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 910 | MachineOperand &MO = RI.getOperand(); |
| 911 | MachineInstr *MI = MO.getParent(); |
| 912 | ++RI; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 913 | // LiveDebugVariables should have handled all DBG_VALUE instructions. |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 914 | if (MI->isDebugValue()) { |
| 915 | DEBUG(dbgs() << "Zapping " << *MI); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 916 | MO.setReg(0); |
| 917 | continue; |
| 918 | } |
Jakob Stoklund Olesen | a372d16 | 2011-02-09 21:52:09 +0000 | [diff] [blame] | 919 | |
| 920 | // <undef> operands don't really read the register, so just assign them to |
| 921 | // the complement. |
| 922 | if (MO.isUse() && MO.isUndef()) { |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 923 | MO.setReg(Edit->get(0)->reg); |
Jakob Stoklund Olesen | a372d16 | 2011-02-09 21:52:09 +0000 | [diff] [blame] | 924 | continue; |
| 925 | } |
| 926 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 927 | SlotIndex Idx = LIS.getInstructionIndex(MI); |
Jakob Stoklund Olesen | 7cec179 | 2011-03-18 03:06:02 +0000 | [diff] [blame] | 928 | if (MO.isDef()) |
| 929 | Idx = MO.isEarlyClobber() ? Idx.getUseIndex() : Idx.getDefIndex(); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 930 | |
| 931 | // Rewrite to the mapped register at Idx. |
| 932 | unsigned RegIdx = RegAssign.lookup(Idx); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 933 | MO.setReg(Edit->get(RegIdx)->reg); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 934 | DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' |
| 935 | << Idx << ':' << RegIdx << '\t' << *MI); |
| 936 | |
Jakob Stoklund Olesen | 7cec179 | 2011-03-18 03:06:02 +0000 | [diff] [blame] | 937 | // Extend liveness to Idx if the instruction reads reg. |
| 938 | if (!ExtendRanges) |
| 939 | continue; |
| 940 | |
| 941 | // Skip instructions that don't read Reg. |
| 942 | if (MO.isDef()) { |
| 943 | if (!MO.getSubReg() && !MO.isEarlyClobber()) |
| 944 | continue; |
| 945 | // We may wan't to extend a live range for a partial redef, or for a use |
| 946 | // tied to an early clobber. |
| 947 | Idx = Idx.getPrevSlot(); |
| 948 | if (!Edit->getParent().liveAt(Idx)) |
| 949 | continue; |
| 950 | } else |
| 951 | Idx = Idx.getUseIndex(); |
| 952 | |
| 953 | extendRange(RegIdx, Idx); |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 957 | void SplitEditor::deleteRematVictims() { |
| 958 | SmallVector<MachineInstr*, 8> Dead; |
Jakob Stoklund Olesen | 2dc455a | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 959 | for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ |
| 960 | LiveInterval *LI = *I; |
| 961 | for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end(); |
| 962 | LII != LIE; ++LII) { |
| 963 | // Dead defs end at the store slot. |
| 964 | if (LII->end != LII->valno->def.getNextSlot()) |
| 965 | continue; |
| 966 | MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def); |
| 967 | assert(MI && "Missing instruction for dead def"); |
| 968 | MI->addRegisterDead(LI->reg, &TRI); |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 969 | |
Jakob Stoklund Olesen | 2dc455a | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 970 | if (!MI->allDefsAreDead()) |
| 971 | continue; |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 972 | |
Jakob Stoklund Olesen | 2dc455a | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 973 | DEBUG(dbgs() << "All defs dead: " << *MI); |
| 974 | Dead.push_back(MI); |
| 975 | } |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | if (Dead.empty()) |
| 979 | return; |
| 980 | |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 981 | Edit->eliminateDeadDefs(Dead, LIS, VRM, TII); |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 984 | void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 985 | ++NumFinished; |
Eric Christopher | 463a297 | 2011-02-03 05:40:54 +0000 | [diff] [blame] | 986 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 987 | // At this point, the live intervals in Edit contain VNInfos corresponding to |
| 988 | // the inserted copies. |
| 989 | |
| 990 | // Add the original defs from the parent interval. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 991 | for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(), |
| 992 | E = Edit->getParent().vni_end(); I != E; ++I) { |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 993 | const VNInfo *ParentVNI = *I; |
Jakob Stoklund Olesen | 9ecd1e7 | 2011-02-04 00:59:23 +0000 | [diff] [blame] | 994 | if (ParentVNI->isUnused()) |
| 995 | continue; |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 996 | unsigned RegIdx = RegAssign.lookup(ParentVNI->def); |
Jakob Stoklund Olesen | 29ef875 | 2011-03-15 21:13:22 +0000 | [diff] [blame] | 997 | VNInfo *VNI = defValue(RegIdx, ParentVNI, ParentVNI->def); |
| 998 | VNI->setIsPHIDef(ParentVNI->isPHIDef()); |
| 999 | VNI->setCopy(ParentVNI->getCopy()); |
| 1000 | |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1001 | // Mark rematted values as complex everywhere to force liveness computation. |
| 1002 | // The new live ranges may be truncated. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1003 | if (Edit->didRematerialize(ParentVNI)) |
| 1004 | for (unsigned i = 0, e = Edit->size(); i != e; ++i) |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1005 | markComplexMapped(i, ParentVNI); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | #ifndef NDEBUG |
| 1009 | // Every new interval must have a def by now, otherwise the split is bogus. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1010 | for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1011 | assert((*I)->hasAtLeastOneValue() && "Split interval has no value"); |
| 1012 | #endif |
| 1013 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 1014 | // Transfer the simply mapped values, check if any are skipped. |
| 1015 | bool Skipped = transferValues(); |
| 1016 | if (Skipped) |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1017 | extendPHIKillRanges(); |
| 1018 | else |
| 1019 | ++NumSimple; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1020 | |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1021 | // Rewrite virtual registers, possibly extending ranges. |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 1022 | rewriteAssigned(Skipped); |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1023 | |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1024 | // Delete defs that were rematted everywhere. |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 1025 | if (Skipped) |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1026 | deleteRematVictims(); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1027 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1028 | // Get rid of unused values and set phi-kill flags. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1029 | for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 1030 | (*I)->RenumberValues(LIS); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1031 | |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1032 | // Provide a reverse mapping from original indices to Edit ranges. |
| 1033 | if (LRMap) { |
| 1034 | LRMap->clear(); |
| 1035 | for (unsigned i = 0, e = Edit->size(); i != e; ++i) |
| 1036 | LRMap->push_back(i); |
| 1037 | } |
| 1038 | |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1039 | // Now check if any registers were separated into multiple components. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 1040 | ConnectedVNInfoEqClasses ConEQ(LIS); |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1041 | for (unsigned i = 0, e = Edit->size(); i != e; ++i) { |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1042 | // Don't use iterators, they are invalidated by create() below. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1043 | LiveInterval *li = Edit->get(i); |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1044 | unsigned NumComp = ConEQ.Classify(li); |
| 1045 | if (NumComp <= 1) |
| 1046 | continue; |
| 1047 | DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); |
| 1048 | SmallVector<LiveInterval*, 8> dups; |
| 1049 | dups.push_back(li); |
Matt Beaumont-Gay | ae5fbee | 2011-04-21 19:46:23 +0000 | [diff] [blame] | 1050 | for (unsigned j = 1; j != NumComp; ++j) |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 1051 | dups.push_back(&Edit->create(LIS, VRM)); |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1052 | ConEQ.Distribute(&dups[0], MRI); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1053 | // The new intervals all map back to i. |
| 1054 | if (LRMap) |
| 1055 | LRMap->resize(Edit->size(), i); |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 1058 | // Calculate spill weight and allocation hints for new intervals. |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1059 | Edit->calculateRegClassAndHint(VRM.getMachineFunction(), LIS, SA.Loops); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1060 | |
| 1061 | assert(!LRMap || LRMap->size() == Edit->size()); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 1065 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1066 | // Single Block Splitting |
| 1067 | //===----------------------------------------------------------------------===// |
| 1068 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 1069 | /// getMultiUseBlocks - if CurLI has more than one use in a basic block, it |
| 1070 | /// 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] | 1071 | bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 1072 | // If CurLI is local to one block, there is no point to splitting it. |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1073 | if (UseBlocks.size() <= 1) |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 1074 | return false; |
| 1075 | // Add blocks with multiple uses. |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1076 | for (unsigned i = 0, e = UseBlocks.size(); i != e; ++i) { |
| 1077 | const BlockInfo &BI = UseBlocks[i]; |
| 1078 | if (BI.FirstUse == BI.LastUse) |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 1079 | continue; |
| 1080 | Blocks.insert(BI.MBB); |
| 1081 | } |
Jakob Stoklund Olesen | 2bfb324 | 2010-10-22 22:48:56 +0000 | [diff] [blame] | 1082 | return !Blocks.empty(); |
| 1083 | } |
| 1084 | |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1085 | void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { |
| 1086 | openIntv(); |
| 1087 | SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); |
| 1088 | SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstUse, |
| 1089 | LastSplitPoint)); |
| 1090 | if (!BI.LiveOut || BI.LastUse < LastSplitPoint) { |
| 1091 | useIntv(SegStart, leaveIntvAfter(BI.LastUse)); |
| 1092 | } else { |
| 1093 | // The last use is after the last valid split point. |
| 1094 | SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); |
| 1095 | useIntv(SegStart, SegStop); |
| 1096 | overlapIntv(SegStop, BI.LastUse); |
| 1097 | } |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 1100 | /// splitSingleBlocks - Split CurLI into a separate live interval inside each |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 1101 | /// basic block in Blocks. |
| 1102 | void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) { |
Jakob Stoklund Olesen | e1f543f | 2010-08-12 18:50:55 +0000 | [diff] [blame] | 1103 | DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n"); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1104 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA.getUseBlocks(); |
| 1105 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 1106 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1107 | if (Blocks.count(BI.MBB)) |
| 1108 | splitSingleBlock(BI); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1109 | } |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1110 | finish(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1111 | } |