Jakob Stoklund Olesen | 36d12c6 | 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 | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 15 | #include "SplitKit.h" |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Pete Cooper | 3ca96f9 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | e172a8b | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | a98af39 | 2011-09-14 16:45:39 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/VirtRegMap.h" |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | ed4075c | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "regalloc" |
| 32 | |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 33 | STATISTIC(NumFinished, "Number of splits finished"); |
| 34 | STATISTIC(NumSimple, "Number of splits that were simple"); |
Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 35 | STATISTIC(NumCopies, "Number of copies inserted for splitting"); |
| 36 | STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); |
Jakob Stoklund Olesen | 50215af | 2011-05-10 17:37:41 +0000 | [diff] [blame] | 37 | STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 38 | |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // Split Analysis |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 43 | SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, |
Jakob Stoklund Olesen | 0fef9dd | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 44 | const MachineLoopInfo &mli) |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 45 | : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli), |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 46 | TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr), |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 47 | LastSplitPoint(MF.getNumBlockIDs()) {} |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 48 | |
| 49 | void SplitAnalysis::clear() { |
Jakob Stoklund Olesen | 267f6c1 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 50 | UseSlots.clear(); |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 51 | UseBlocks.clear(); |
| 52 | ThroughBlocks.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 53 | CurLI = nullptr; |
Jakob Stoklund Olesen | eaa6ed1 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 54 | DidRepairRange = false; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jakob Stoklund Olesen | 50b2db8 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 57 | SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) { |
| 58 | const MachineBasicBlock *MBB = MF.getBlockNumbered(Num); |
| 59 | const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor(); |
| 60 | std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num]; |
Jakob Stoklund Olesen | 8b1d023 | 2012-01-11 02:07:05 +0000 | [diff] [blame] | 61 | SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB); |
Jakob Stoklund Olesen | 50b2db8 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 62 | |
| 63 | // Compute split points on the first call. The pair is independent of the |
| 64 | // current live interval. |
| 65 | if (!LSP.first.isValid()) { |
| 66 | MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator(); |
| 67 | if (FirstTerm == MBB->end()) |
Jakob Stoklund Olesen | 8b1d023 | 2012-01-11 02:07:05 +0000 | [diff] [blame] | 68 | LSP.first = MBBEnd; |
Jakob Stoklund Olesen | 50b2db8 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 69 | else |
| 70 | LSP.first = LIS.getInstructionIndex(FirstTerm); |
| 71 | |
| 72 | // If there is a landing pad successor, also find the call instruction. |
| 73 | if (!LPad) |
| 74 | return LSP.first; |
| 75 | // There may not be a call instruction (?) in which case we ignore LPad. |
| 76 | LSP.second = LSP.first; |
Jakob Stoklund Olesen | 040d659 | 2011-06-28 01:18:58 +0000 | [diff] [blame] | 77 | for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin(); |
| 78 | I != E;) { |
| 79 | --I; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 80 | if (I->isCall()) { |
Jakob Stoklund Olesen | 50b2db8 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 81 | LSP.second = LIS.getInstructionIndex(I); |
| 82 | break; |
| 83 | } |
Jakob Stoklund Olesen | 040d659 | 2011-06-28 01:18:58 +0000 | [diff] [blame] | 84 | } |
Jakob Stoklund Olesen | 50b2db8 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 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 | 8b1d023 | 2012-01-11 02:07:05 +0000 | [diff] [blame] | 89 | if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad)) |
Jakob Stoklund Olesen | 50b2db8 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 90 | return LSP.first; |
Jakob Stoklund Olesen | 8b1d023 | 2012-01-11 02:07:05 +0000 | [diff] [blame] | 91 | |
| 92 | // Find the value leaving MBB. |
| 93 | const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd); |
| 94 | if (!VNI) |
| 95 | return LSP.first; |
| 96 | |
| 97 | // If the value leaving MBB was defined after the call in MBB, it can't |
| 98 | // really be live-in to the landing pad. This can happen if the landing pad |
| 99 | // has a PHI, and this register is undef on the exceptional edge. |
| 100 | // <rdar://problem/10664933> |
| 101 | if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd) |
| 102 | return LSP.first; |
| 103 | |
| 104 | // Value is properly live-in to the landing pad. |
| 105 | // Only allow splits before the call. |
| 106 | return LSP.second; |
Jakob Stoklund Olesen | ed4075c | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Jakob Stoklund Olesen | 67aec12 | 2012-01-11 02:07:00 +0000 | [diff] [blame] | 109 | MachineBasicBlock::iterator |
| 110 | SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) { |
| 111 | SlotIndex LSP = getLastSplitPoint(MBB->getNumber()); |
| 112 | if (LSP == LIS.getMBBEndIdx(MBB)) |
| 113 | return MBB->end(); |
| 114 | return LIS.getInstructionFromIndex(LSP); |
| 115 | } |
| 116 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 117 | /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. |
Jakob Stoklund Olesen | ff09550 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 118 | void SplitAnalysis::analyzeUses() { |
Jakob Stoklund Olesen | fe6e07f | 2011-04-05 15:18:18 +0000 | [diff] [blame] | 119 | assert(UseSlots.empty() && "Call clear first"); |
| 120 | |
| 121 | // First get all the defs from the interval values. This provides the correct |
| 122 | // slots for early clobbers. |
| 123 | for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(), |
| 124 | E = CurLI->vni_end(); I != E; ++I) |
| 125 | if (!(*I)->isPHIDef() && !(*I)->isUnused()) |
| 126 | UseSlots.push_back((*I)->def); |
| 127 | |
| 128 | // Get use slots form the use-def chain. |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 129 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 130 | for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg)) |
| 131 | if (!MO.isUndef()) |
| 132 | UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot()); |
Jakob Stoklund Olesen | fe6e07f | 2011-04-05 15:18:18 +0000 | [diff] [blame] | 133 | |
Jakob Stoklund Olesen | 267f6c1 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 134 | array_pod_sort(UseSlots.begin(), UseSlots.end()); |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 135 | |
Jakob Stoklund Olesen | fe6e07f | 2011-04-05 15:18:18 +0000 | [diff] [blame] | 136 | // Remove duplicates, keeping the smaller slot for each instruction. |
| 137 | // That is what we want for early clobbers. |
| 138 | UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), |
| 139 | SlotIndex::isSameInstr), |
| 140 | UseSlots.end()); |
| 141 | |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 142 | // Compute per-live block info. |
| 143 | if (!calcLiveBlockInfo()) { |
| 144 | // FIXME: calcLiveBlockInfo found inconsistencies in the live range. |
Rafael Espindola | 676c405 | 2011-06-26 22:34:10 +0000 | [diff] [blame] | 145 | // I am looking at you, RegisterCoalescer! |
Jakob Stoklund Olesen | eaa6ed1 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 146 | DidRepairRange = true; |
Jakob Stoklund Olesen | 50215af | 2011-05-10 17:37:41 +0000 | [diff] [blame] | 147 | ++NumRepairs; |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 148 | DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); |
| 149 | const_cast<LiveIntervals&>(LIS) |
| 150 | .shrinkToUses(const_cast<LiveInterval*>(CurLI)); |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 151 | UseBlocks.clear(); |
| 152 | ThroughBlocks.clear(); |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 153 | bool fixed = calcLiveBlockInfo(); |
| 154 | (void)fixed; |
| 155 | assert(fixed && "Couldn't fix broken live interval"); |
| 156 | } |
| 157 | |
Jakob Stoklund Olesen | bd6b86e | 2011-03-27 22:49:23 +0000 | [diff] [blame] | 158 | DEBUG(dbgs() << "Analyze counted " |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 159 | << UseSlots.size() << " instrs in " |
| 160 | << UseBlocks.size() << " blocks, through " |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 161 | << NumThroughBlocks << " blocks.\n"); |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 164 | /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks |
| 165 | /// where CurLI is live. |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 166 | bool SplitAnalysis::calcLiveBlockInfo() { |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 167 | ThroughBlocks.resize(MF.getNumBlockIDs()); |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 168 | NumThroughBlocks = NumGapBlocks = 0; |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 169 | if (CurLI->empty()) |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 170 | return true; |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 171 | |
| 172 | LiveInterval::const_iterator LVI = CurLI->begin(); |
| 173 | LiveInterval::const_iterator LVE = CurLI->end(); |
| 174 | |
| 175 | SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; |
| 176 | UseI = UseSlots.begin(); |
| 177 | UseE = UseSlots.end(); |
| 178 | |
| 179 | // Loop over basic blocks where CurLI is live. |
| 180 | MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start); |
| 181 | for (;;) { |
| 182 | BlockInfo BI; |
| 183 | BI.MBB = MFI; |
Jakob Stoklund Olesen | 8933907 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 184 | SlotIndex Start, Stop; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 185 | std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 186 | |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 187 | // If the block contains no uses, the range must be live through. At one |
Rafael Espindola | 676c405 | 2011-06-26 22:34:10 +0000 | [diff] [blame] | 188 | // point, RegisterCoalescer could create dangling ranges that ended |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 189 | // mid-block. |
| 190 | if (UseI == UseE || *UseI >= Stop) { |
| 191 | ++NumThroughBlocks; |
| 192 | ThroughBlocks.set(BI.MBB->getNumber()); |
| 193 | // The range shouldn't end mid-block if there are no uses. This shouldn't |
| 194 | // happen. |
| 195 | if (LVI->end < Stop) |
| 196 | return false; |
| 197 | } else { |
| 198 | // This block has uses. Find the first and last uses in the block. |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 199 | BI.FirstInstr = *UseI; |
| 200 | assert(BI.FirstInstr >= Start); |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 201 | do ++UseI; |
Jakob Stoklund Olesen | 8933907 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 202 | while (UseI != UseE && *UseI < Stop); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 203 | BI.LastInstr = UseI[-1]; |
| 204 | assert(BI.LastInstr < Stop); |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 205 | |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 206 | // LVI is the first live segment overlapping MBB. |
| 207 | BI.LiveIn = LVI->start <= Start; |
Jakob Stoklund Olesen | ca6a4d8 | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 208 | |
Jakob Stoklund Olesen | ae8027c | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 209 | // When not live in, the first use should be a def. |
| 210 | if (!BI.LiveIn) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 211 | assert(LVI->start == LVI->valno->def && "Dangling Segment start"); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 212 | assert(LVI->start == BI.FirstInstr && "First instr should be a def"); |
| 213 | BI.FirstDef = BI.FirstInstr; |
Jakob Stoklund Olesen | ae8027c | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 216 | // Look for gaps in the live range. |
| 217 | BI.LiveOut = true; |
| 218 | while (LVI->end < Stop) { |
| 219 | SlotIndex LastStop = LVI->end; |
| 220 | if (++LVI == LVE || LVI->start >= Stop) { |
| 221 | BI.LiveOut = false; |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 222 | BI.LastInstr = LastStop; |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
Jakob Stoklund Olesen | ae8027c | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 225 | |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 226 | if (LastStop < LVI->start) { |
| 227 | // There is a gap in the live range. Create duplicate entries for the |
| 228 | // live-in snippet and the live-out snippet. |
| 229 | ++NumGapBlocks; |
| 230 | |
| 231 | // Push the Live-in part. |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 232 | BI.LiveOut = false; |
| 233 | UseBlocks.push_back(BI); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 234 | UseBlocks.back().LastInstr = LastStop; |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 235 | |
| 236 | // Set up BI for the live-out part. |
| 237 | BI.LiveIn = false; |
| 238 | BI.LiveOut = true; |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 239 | BI.FirstInstr = BI.FirstDef = LVI->start; |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 240 | } |
Jakob Stoklund Olesen | ae8027c | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 241 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 242 | // A Segment that starts in the middle of the block must be a def. |
| 243 | assert(LVI->start == LVI->valno->def && "Dangling Segment start"); |
Jakob Stoklund Olesen | ae8027c | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 244 | if (!BI.FirstDef) |
| 245 | BI.FirstDef = LVI->start; |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Jakob Stoklund Olesen | ca6a4d8 | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 248 | UseBlocks.push_back(BI); |
Jakob Stoklund Olesen | ca6a4d8 | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 249 | |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 250 | // LVI is now at LVE or LVI->end >= Stop. |
| 251 | if (LVI == LVE) |
| 252 | break; |
| 253 | } |
Jakob Stoklund Olesen | ca6a4d8 | 2011-05-29 21:24:39 +0000 | [diff] [blame] | 254 | |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 255 | // Live segment ends exactly at Stop. Move to the next segment. |
Jakob Stoklund Olesen | 8933907 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 256 | if (LVI->end == Stop && ++LVI == LVE) |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | // Pick the next basic block. |
Jakob Stoklund Olesen | 8933907 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 260 | if (LVI->start < Stop) |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 261 | ++MFI; |
| 262 | else |
| 263 | MFI = LIS.getMBBFromIndex(LVI->start); |
| 264 | } |
Jakob Stoklund Olesen | 5cc91b2 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 265 | |
| 266 | assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 267 | return true; |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Jakob Stoklund Olesen | eef2327 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 270 | unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { |
| 271 | if (cli->empty()) |
| 272 | return 0; |
| 273 | LiveInterval *li = const_cast<LiveInterval*>(cli); |
| 274 | LiveInterval::iterator LVI = li->begin(); |
| 275 | LiveInterval::iterator LVE = li->end(); |
| 276 | unsigned Count = 0; |
| 277 | |
| 278 | // Loop over basic blocks where li is live. |
| 279 | MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start); |
| 280 | SlotIndex Stop = LIS.getMBBEndIdx(MFI); |
| 281 | for (;;) { |
| 282 | ++Count; |
| 283 | LVI = li->advanceTo(LVI, Stop); |
| 284 | if (LVI == LVE) |
| 285 | return Count; |
| 286 | do { |
| 287 | ++MFI; |
| 288 | Stop = LIS.getMBBEndIdx(MFI); |
| 289 | } while (Stop <= LVI->start); |
| 290 | } |
| 291 | } |
| 292 | |
Jakob Stoklund Olesen | 60a26a6 | 2011-02-21 23:09:46 +0000 | [diff] [blame] | 293 | bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { |
| 294 | unsigned OrigReg = VRM.getOriginal(CurLI->reg); |
| 295 | const LiveInterval &Orig = LIS.getInterval(OrigReg); |
| 296 | assert(!Orig.empty() && "Splitting empty interval?"); |
| 297 | LiveInterval::const_iterator I = Orig.find(Idx); |
| 298 | |
| 299 | // Range containing Idx should begin at Idx. |
| 300 | if (I != Orig.end() && I->start <= Idx) |
| 301 | return I->start == Idx; |
| 302 | |
| 303 | // Range does not contain Idx, previous must end at Idx. |
| 304 | return I != Orig.begin() && (--I)->end == Idx; |
| 305 | } |
| 306 | |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 307 | void SplitAnalysis::analyze(const LiveInterval *li) { |
| 308 | clear(); |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 309 | CurLI = li; |
Jakob Stoklund Olesen | ff09550 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 310 | analyzeUses(); |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Jakob Stoklund Olesen | 28e769c | 2010-12-15 17:49:52 +0000 | [diff] [blame] | 313 | |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
| 315 | // Split Editor |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | |
| 318 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 319 | SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm, |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 320 | MachineDominatorTree &mdt, |
| 321 | MachineBlockFrequencyInfo &mbfi) |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 322 | : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()), |
| 323 | MDT(mdt), TII(*vrm.getMachineFunction() |
| 324 | .getTarget() |
| 325 | .getSubtargetImpl() |
| 326 | ->getInstrInfo()), |
| 327 | TRI(*vrm.getMachineFunction() |
| 328 | .getTarget() |
| 329 | .getSubtargetImpl() |
| 330 | ->getRegisterInfo()), |
| 331 | MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition), |
| 332 | RegAssign(Allocator) {} |
Jakob Stoklund Olesen | c960198 | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 333 | |
Jakob Stoklund Olesen | eecb2fb | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 334 | void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) { |
| 335 | Edit = &LRE; |
| 336 | SpillMode = SM; |
Jakob Stoklund Olesen | c960198 | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 337 | OpenIdx = 0; |
| 338 | RegAssign.clear(); |
| 339 | Values.clear(); |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 340 | |
| 341 | // Reset the LiveRangeCalc instances needed for this spill mode. |
Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 342 | LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, |
| 343 | &LIS.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 344 | if (SpillMode) |
Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 345 | LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, |
| 346 | &LIS.getVNInfoAllocator()); |
Jakob Stoklund Olesen | c960198 | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 347 | |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 348 | // We don't need an AliasAnalysis since we will only be performing |
| 349 | // cheap-as-a-copy remats anyway. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 350 | Edit->anyRematerializable(nullptr); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 353 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 354 | void SplitEditor::dump() const { |
| 355 | if (RegAssign.empty()) { |
| 356 | dbgs() << " empty\n"; |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) |
| 361 | dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); |
| 362 | dbgs() << '\n'; |
Jakob Stoklund Olesen | 6f8bd42 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 363 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 364 | #endif |
Jakob Stoklund Olesen | 6f8bd42 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 365 | |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 366 | VNInfo *SplitEditor::defValue(unsigned RegIdx, |
| 367 | const VNInfo *ParentVNI, |
| 368 | SlotIndex Idx) { |
| 369 | assert(ParentVNI && "Mapping NULL value"); |
| 370 | assert(Idx.isValid() && "Invalid SlotIndex"); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 371 | assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 372 | LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 373 | |
| 374 | // Create a new value. |
Jakob Stoklund Olesen | ad6b22e | 2012-02-04 05:20:49 +0000 | [diff] [blame] | 375 | VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 376 | |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 377 | // Use insert for lookup, so we can add missing values with a second lookup. |
| 378 | std::pair<ValueMap::iterator, bool> InsP = |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 379 | Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), |
| 380 | ValueForcePair(VNI, false))); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 381 | |
| 382 | // This was the first time (RegIdx, ParentVNI) was mapped. |
| 383 | // Keep it as a simple def without any liveness. |
| 384 | if (InsP.second) |
| 385 | return VNI; |
| 386 | |
| 387 | // If the previous value was a simple mapping, add liveness for it now. |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 388 | if (VNInfo *OldVNI = InsP.first->second.getPointer()) { |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 389 | SlotIndex Def = OldVNI->def; |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 390 | LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI)); |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 391 | // No longer a simple mapping. Switch to a complex, non-forced mapping. |
| 392 | InsP.first->second = ValueForcePair(); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // This is a complex mapping, add liveness for VNI |
| 396 | SlotIndex Def = VNI->def; |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 397 | LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI)); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 398 | |
| 399 | return VNI; |
| 400 | } |
| 401 | |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 402 | void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 403 | assert(ParentVNI && "Mapping NULL value"); |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 404 | ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)]; |
| 405 | VNInfo *VNI = VFP.getPointer(); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 406 | |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 407 | // ParentVNI was either unmapped or already complex mapped. Either way, just |
| 408 | // set the force bit. |
| 409 | if (!VNI) { |
| 410 | VFP.setInt(true); |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 411 | return; |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 412 | } |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 413 | |
| 414 | // This was previously a single mapping. Make sure the old def is represented |
| 415 | // by a trivial live range. |
| 416 | SlotIndex Def = VNI->def; |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 417 | LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 418 | LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI)); |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 419 | // Mark as complex mapped, forced. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 420 | VFP = ValueForcePair(nullptr, true); |
Jakob Stoklund Olesen | 4484f99 | 2011-09-13 18:05:29 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 423 | VNInfo *SplitEditor::defFromParent(unsigned RegIdx, |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 424 | VNInfo *ParentVNI, |
| 425 | SlotIndex UseIdx, |
| 426 | MachineBasicBlock &MBB, |
| 427 | MachineBasicBlock::iterator I) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 428 | MachineInstr *CopyMI = nullptr; |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 429 | SlotIndex Def; |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 430 | LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 431 | |
Jakob Stoklund Olesen | 7d40679 | 2011-05-02 05:29:58 +0000 | [diff] [blame] | 432 | // We may be trying to avoid interference that ends at a deleted instruction, |
| 433 | // so always begin RegIdx 0 early and all others late. |
| 434 | bool Late = RegIdx != 0; |
| 435 | |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 436 | // Attempt cheap-as-a-copy rematerialization. |
| 437 | LiveRangeEdit::Remat RM(ParentVNI); |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 438 | if (Edit->canRematerializeAt(RM, UseIdx, true)) { |
| 439 | Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late); |
Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 440 | ++NumRemats; |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 441 | } else { |
| 442 | // Can't remat, just insert a copy from parent. |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 443 | CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 444 | .addReg(Edit->getReg()); |
Jakob Stoklund Olesen | 7d40679 | 2011-05-02 05:29:58 +0000 | [diff] [blame] | 445 | Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late) |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 446 | .getRegSlot(); |
Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 447 | ++NumCopies; |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 450 | // Define the value in Reg. |
Jakob Stoklund Olesen | ad6b22e | 2012-02-04 05:20:49 +0000 | [diff] [blame] | 451 | return defValue(RegIdx, ParentVNI, Def); |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 454 | /// Create a new virtual register and live interval. |
Jakob Stoklund Olesen | 0840f50 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 455 | unsigned SplitEditor::openIntv() { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 456 | // Create the complement as index 0. |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 457 | if (Edit->empty()) |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 458 | Edit->createEmptyInterval(); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 459 | |
| 460 | // Create the open interval. |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 461 | OpenIdx = Edit->size(); |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 462 | Edit->createEmptyInterval(); |
Jakob Stoklund Olesen | 0840f50 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 463 | return OpenIdx; |
| 464 | } |
| 465 | |
| 466 | void SplitEditor::selectIntv(unsigned Idx) { |
| 467 | assert(Idx != 0 && "Cannot select the complement interval"); |
| 468 | assert(Idx < Edit->size() && "Can only select previously opened interval"); |
Jakob Stoklund Olesen | adc6a4c | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 469 | DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n'); |
Jakob Stoklund Olesen | 0840f50 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 470 | OpenIdx = Idx; |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 473 | SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 474 | assert(OpenIdx && "openIntv not called before enterIntvBefore"); |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 475 | DEBUG(dbgs() << " enterIntvBefore " << Idx); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 476 | Idx = Idx.getBaseIndex(); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 477 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | 9855109 | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 478 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 479 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 480 | return Idx; |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 481 | } |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 482 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 483 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
Jakob Stoklund Olesen | 9855109 | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 484 | assert(MI && "enterIntvBefore called with invalid index"); |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 485 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 486 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); |
| 487 | return VNI->def; |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Jakob Stoklund Olesen | adc6a4c | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 490 | SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) { |
| 491 | assert(OpenIdx && "openIntv not called before enterIntvAfter"); |
| 492 | DEBUG(dbgs() << " enterIntvAfter " << Idx); |
| 493 | Idx = Idx.getBoundaryIndex(); |
| 494 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); |
| 495 | if (!ParentVNI) { |
| 496 | DEBUG(dbgs() << ": not live\n"); |
| 497 | return Idx; |
| 498 | } |
| 499 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
| 500 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
| 501 | assert(MI && "enterIntvAfter called with invalid index"); |
| 502 | |
| 503 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 504 | std::next(MachineBasicBlock::iterator(MI))); |
Jakob Stoklund Olesen | adc6a4c | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 505 | return VNI->def; |
| 506 | } |
| 507 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 508 | SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 509 | assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 510 | SlotIndex End = LIS.getMBBEndIdx(&MBB); |
| 511 | SlotIndex Last = End.getPrevSlot(); |
| 512 | DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 513 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); |
Jakob Stoklund Olesen | 9855109 | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 514 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 515 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 516 | return End; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 517 | } |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 518 | DEBUG(dbgs() << ": valno " << ParentVNI->id); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 519 | VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, |
Jakob Stoklund Olesen | 67aec12 | 2012-01-11 02:07:00 +0000 | [diff] [blame] | 520 | SA.getLastSplitPointIter(&MBB)); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 521 | RegAssign.insert(VNI->def, End, OpenIdx); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 522 | DEBUG(dump()); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 523 | return VNI->def; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 526 | /// useIntv - indicate that all instructions in MBB should use OpenLI. |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 527 | void SplitEditor::useIntv(const MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 528 | useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 531 | void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 532 | assert(OpenIdx && "openIntv not called before useIntv"); |
| 533 | DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); |
| 534 | RegAssign.insert(Start, End, OpenIdx); |
| 535 | DEBUG(dump()); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 538 | SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 539 | assert(OpenIdx && "openIntv not called before leaveIntvAfter"); |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 540 | DEBUG(dbgs() << " leaveIntvAfter " << Idx); |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 541 | |
Jakob Stoklund Olesen | 9855109 | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 542 | // The interval must be live beyond the instruction at Idx. |
Jakob Stoklund Olesen | e2c92a3 | 2011-09-16 00:03:35 +0000 | [diff] [blame] | 543 | SlotIndex Boundary = Idx.getBoundaryIndex(); |
| 544 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary); |
Jakob Stoklund Olesen | 9855109 | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 545 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 546 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | e2c92a3 | 2011-09-16 00:03:35 +0000 | [diff] [blame] | 547 | return Boundary.getNextSlot(); |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 548 | } |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 549 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
Jakob Stoklund Olesen | e2c92a3 | 2011-09-16 00:03:35 +0000 | [diff] [blame] | 550 | MachineInstr *MI = LIS.getInstructionFromIndex(Boundary); |
Jakob Stoklund Olesen | 3d11c8e | 2011-02-08 18:50:18 +0000 | [diff] [blame] | 551 | assert(MI && "No instruction at index"); |
Jakob Stoklund Olesen | e2c92a3 | 2011-09-16 00:03:35 +0000 | [diff] [blame] | 552 | |
| 553 | // In spill mode, make live ranges as short as possible by inserting the copy |
| 554 | // before MI. This is only possible if that instruction doesn't redefine the |
| 555 | // value. The inserted COPY is not a kill, and we don't need to recompute |
| 556 | // the source live range. The spiller also won't try to hoist this copy. |
| 557 | if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) && |
| 558 | MI->readsVirtualRegister(Edit->getReg())) { |
| 559 | forceRecompute(0, ParentVNI); |
| 560 | defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); |
| 561 | return Idx; |
| 562 | } |
| 563 | |
| 564 | VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(), |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 565 | std::next(MachineBasicBlock::iterator(MI))); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 566 | return VNI->def; |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Jakob Stoklund Olesen | 7cb57b3 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 569 | SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { |
| 570 | assert(OpenIdx && "openIntv not called before leaveIntvBefore"); |
| 571 | DEBUG(dbgs() << " leaveIntvBefore " << Idx); |
| 572 | |
| 573 | // The interval must be live into the instruction at Idx. |
Jakob Stoklund Olesen | c45d38e | 2011-07-18 18:47:13 +0000 | [diff] [blame] | 574 | Idx = Idx.getBaseIndex(); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 575 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | 7cb57b3 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 576 | if (!ParentVNI) { |
| 577 | DEBUG(dbgs() << ": not live\n"); |
| 578 | return Idx.getNextSlot(); |
| 579 | } |
| 580 | DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); |
| 581 | |
| 582 | MachineInstr *MI = LIS.getInstructionFromIndex(Idx); |
| 583 | assert(MI && "No instruction at index"); |
| 584 | VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); |
| 585 | return VNI->def; |
| 586 | } |
| 587 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 588 | SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 589 | assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 590 | SlotIndex Start = LIS.getMBBStartIdx(&MBB); |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 591 | DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 592 | |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 593 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); |
Jakob Stoklund Olesen | 9855109 | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 594 | if (!ParentVNI) { |
Jakob Stoklund Olesen | 9575af4 | 2010-10-07 17:56:35 +0000 | [diff] [blame] | 595 | DEBUG(dbgs() << ": not live\n"); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 596 | return Start; |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 599 | VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 600 | MBB.SkipPHIsAndLabels(MBB.begin())); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 601 | RegAssign.insert(Start, VNI->def, OpenIdx); |
| 602 | DEBUG(dump()); |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 603 | return VNI->def; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Jakob Stoklund Olesen | 1749935 | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 606 | void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { |
| 607 | assert(OpenIdx && "openIntv not called before overlapIntv"); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 608 | const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); |
Jakob Stoklund Olesen | d7bcf43 | 2011-11-14 01:39:36 +0000 | [diff] [blame] | 609 | assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) && |
Jakob Stoklund Olesen | 1749935 | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 610 | "Parent changes value in extended range"); |
Jakob Stoklund Olesen | 1749935 | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 611 | assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && |
| 612 | "Range cannot span basic blocks"); |
| 613 | |
Jakob Stoklund Olesen | 820c8fd0 | 2011-09-13 17:38:57 +0000 | [diff] [blame] | 614 | // The complement interval will be extended as needed by LRCalc.extend(). |
Jakob Stoklund Olesen | 5c482cd | 2011-04-05 23:43:14 +0000 | [diff] [blame] | 615 | if (ParentVNI) |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 616 | forceRecompute(0, ParentVNI); |
Jakob Stoklund Olesen | 1749935 | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 617 | DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); |
| 618 | RegAssign.insert(Start, End, OpenIdx); |
| 619 | DEBUG(dump()); |
| 620 | } |
| 621 | |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 622 | //===----------------------------------------------------------------------===// |
| 623 | // Spill modes |
| 624 | //===----------------------------------------------------------------------===// |
| 625 | |
| 626 | void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) { |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 627 | LiveInterval *LI = &LIS.getInterval(Edit->get(0)); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 628 | DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n"); |
| 629 | RegAssignMap::iterator AssignI; |
| 630 | AssignI.setMap(RegAssign); |
| 631 | |
| 632 | for (unsigned i = 0, e = Copies.size(); i != e; ++i) { |
| 633 | VNInfo *VNI = Copies[i]; |
| 634 | SlotIndex Def = VNI->def; |
| 635 | MachineInstr *MI = LIS.getInstructionFromIndex(Def); |
| 636 | assert(MI && "No instruction for back-copy"); |
| 637 | |
| 638 | MachineBasicBlock *MBB = MI->getParent(); |
| 639 | MachineBasicBlock::iterator MBBI(MI); |
| 640 | bool AtBegin; |
| 641 | do AtBegin = MBBI == MBB->begin(); |
| 642 | while (!AtBegin && (--MBBI)->isDebugValue()); |
| 643 | |
| 644 | DEBUG(dbgs() << "Removing " << Def << '\t' << *MI); |
| 645 | LI->removeValNo(VNI); |
| 646 | LIS.RemoveMachineInstrFromMaps(MI); |
| 647 | MI->eraseFromParent(); |
| 648 | |
| 649 | // Adjust RegAssign if a register assignment is killed at VNI->def. We |
| 650 | // want to avoid calculating the live range of the source register if |
| 651 | // possible. |
Jakob Stoklund Olesen | 2180938 | 2012-08-03 20:59:29 +0000 | [diff] [blame] | 652 | AssignI.find(Def.getPrevSlot()); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 653 | if (!AssignI.valid() || AssignI.start() >= Def) |
| 654 | continue; |
| 655 | // If MI doesn't kill the assigned register, just leave it. |
| 656 | if (AssignI.stop() != Def) |
| 657 | continue; |
| 658 | unsigned RegIdx = AssignI.value(); |
| 659 | if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) { |
| 660 | DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n'); |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 661 | forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def)); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 662 | } else { |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 663 | SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot(); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 664 | DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI); |
| 665 | AssignI.setStop(Kill); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
Jakob Stoklund Olesen | a98af39 | 2011-09-14 16:45:39 +0000 | [diff] [blame] | 670 | MachineBasicBlock* |
| 671 | SplitEditor::findShallowDominator(MachineBasicBlock *MBB, |
| 672 | MachineBasicBlock *DefMBB) { |
| 673 | if (MBB == DefMBB) |
| 674 | return MBB; |
| 675 | assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def."); |
| 676 | |
| 677 | const MachineLoopInfo &Loops = SA.Loops; |
| 678 | const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB); |
| 679 | MachineDomTreeNode *DefDomNode = MDT[DefMBB]; |
| 680 | |
| 681 | // Best candidate so far. |
| 682 | MachineBasicBlock *BestMBB = MBB; |
| 683 | unsigned BestDepth = UINT_MAX; |
| 684 | |
| 685 | for (;;) { |
| 686 | const MachineLoop *Loop = Loops.getLoopFor(MBB); |
| 687 | |
| 688 | // MBB isn't in a loop, it doesn't get any better. All dominators have a |
| 689 | // higher frequency by definition. |
| 690 | if (!Loop) { |
| 691 | DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" |
| 692 | << MBB->getNumber() << " at depth 0\n"); |
| 693 | return MBB; |
| 694 | } |
| 695 | |
| 696 | // We'll never be able to exit the DefLoop. |
| 697 | if (Loop == DefLoop) { |
| 698 | DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" |
| 699 | << MBB->getNumber() << " in the same loop\n"); |
| 700 | return MBB; |
| 701 | } |
| 702 | |
| 703 | // Least busy dominator seen so far. |
| 704 | unsigned Depth = Loop->getLoopDepth(); |
| 705 | if (Depth < BestDepth) { |
| 706 | BestMBB = MBB; |
| 707 | BestDepth = Depth; |
| 708 | DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" |
| 709 | << MBB->getNumber() << " at depth " << Depth << '\n'); |
| 710 | } |
| 711 | |
| 712 | // Leave loop by going to the immediate dominator of the loop header. |
| 713 | // This is a bigger stride than simply walking up the dominator tree. |
| 714 | MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom(); |
| 715 | |
| 716 | // Too far up the dominator tree? |
| 717 | if (!IDom || !MDT.dominates(DefDomNode, IDom)) |
| 718 | return BestMBB; |
| 719 | |
| 720 | MBB = IDom->getBlock(); |
| 721 | } |
| 722 | } |
| 723 | |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 724 | void SplitEditor::hoistCopiesForSize() { |
| 725 | // Get the complement interval, always RegIdx 0. |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 726 | LiveInterval *LI = &LIS.getInterval(Edit->get(0)); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 727 | LiveInterval *Parent = &Edit->getParent(); |
| 728 | |
| 729 | // Track the nearest common dominator for all back-copies for each ParentVNI, |
| 730 | // indexed by ParentVNI->id. |
| 731 | typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair; |
| 732 | SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums()); |
| 733 | |
| 734 | // Find the nearest common dominator for parent values with multiple |
| 735 | // back-copies. If a single back-copy dominates, put it in DomPair.second. |
| 736 | for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end(); |
| 737 | VI != VE; ++VI) { |
| 738 | VNInfo *VNI = *VI; |
Jakob Stoklund Olesen | 2180938 | 2012-08-03 20:59:29 +0000 | [diff] [blame] | 739 | if (VNI->isUnused()) |
| 740 | continue; |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 741 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); |
| 742 | assert(ParentVNI && "Parent not live at complement def"); |
| 743 | |
| 744 | // Don't hoist remats. The complement is probably going to disappear |
| 745 | // completely anyway. |
| 746 | if (Edit->didRematerialize(ParentVNI)) |
| 747 | continue; |
| 748 | |
| 749 | MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def); |
| 750 | DomPair &Dom = NearestDom[ParentVNI->id]; |
| 751 | |
| 752 | // Keep directly defined parent values. This is either a PHI or an |
| 753 | // instruction in the complement range. All other copies of ParentVNI |
| 754 | // should be eliminated. |
| 755 | if (VNI->def == ParentVNI->def) { |
| 756 | DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n'); |
| 757 | Dom = DomPair(ValMBB, VNI->def); |
| 758 | continue; |
| 759 | } |
| 760 | // Skip the singly mapped values. There is nothing to gain from hoisting a |
| 761 | // single back-copy. |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 762 | if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) { |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 763 | DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n'); |
| 764 | continue; |
| 765 | } |
| 766 | |
| 767 | if (!Dom.first) { |
| 768 | // First time we see ParentVNI. VNI dominates itself. |
| 769 | Dom = DomPair(ValMBB, VNI->def); |
| 770 | } else if (Dom.first == ValMBB) { |
| 771 | // Two defs in the same block. Pick the earlier def. |
| 772 | if (!Dom.second.isValid() || VNI->def < Dom.second) |
| 773 | Dom.second = VNI->def; |
| 774 | } else { |
| 775 | // Different basic blocks. Check if one dominates. |
| 776 | MachineBasicBlock *Near = |
| 777 | MDT.findNearestCommonDominator(Dom.first, ValMBB); |
| 778 | if (Near == ValMBB) |
| 779 | // Def ValMBB dominates. |
| 780 | Dom = DomPair(ValMBB, VNI->def); |
| 781 | else if (Near != Dom.first) |
| 782 | // None dominate. Hoist to common dominator, need new def. |
| 783 | Dom = DomPair(Near, SlotIndex()); |
| 784 | } |
| 785 | |
| 786 | DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def |
| 787 | << " for parent " << ParentVNI->id << '@' << ParentVNI->def |
| 788 | << " hoist to BB#" << Dom.first->getNumber() << ' ' |
| 789 | << Dom.second << '\n'); |
| 790 | } |
| 791 | |
| 792 | // Insert the hoisted copies. |
| 793 | for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { |
| 794 | DomPair &Dom = NearestDom[i]; |
| 795 | if (!Dom.first || Dom.second.isValid()) |
| 796 | continue; |
Jakob Stoklund Olesen | a98af39 | 2011-09-14 16:45:39 +0000 | [diff] [blame] | 797 | // This value needs a hoisted copy inserted at the end of Dom.first. |
| 798 | VNInfo *ParentVNI = Parent->getValNumInfo(i); |
| 799 | MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def); |
| 800 | // Get a less loopy dominator than Dom.first. |
| 801 | Dom.first = findShallowDominator(Dom.first, DefMBB); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 802 | SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot(); |
| 803 | Dom.second = |
Jakob Stoklund Olesen | a98af39 | 2011-09-14 16:45:39 +0000 | [diff] [blame] | 804 | defFromParent(0, ParentVNI, Last, *Dom.first, |
Jakob Stoklund Olesen | 67aec12 | 2012-01-11 02:07:00 +0000 | [diff] [blame] | 805 | SA.getLastSplitPointIter(Dom.first))->def; |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Remove redundant back-copies that are now known to be dominated by another |
| 809 | // def with the same value. |
| 810 | SmallVector<VNInfo*, 8> BackCopies; |
| 811 | for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end(); |
| 812 | VI != VE; ++VI) { |
| 813 | VNInfo *VNI = *VI; |
Jakob Stoklund Olesen | 2180938 | 2012-08-03 20:59:29 +0000 | [diff] [blame] | 814 | if (VNI->isUnused()) |
| 815 | continue; |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 816 | VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); |
| 817 | const DomPair &Dom = NearestDom[ParentVNI->id]; |
| 818 | if (!Dom.first || Dom.second == VNI->def) |
| 819 | continue; |
| 820 | BackCopies.push_back(VNI); |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 821 | forceRecompute(0, ParentVNI); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 822 | } |
| 823 | removeBackCopies(BackCopies); |
| 824 | } |
| 825 | |
| 826 | |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 827 | /// transferValues - Transfer all possible values to the new live ranges. |
Jakob Stoklund Olesen | 820c8fd0 | 2011-09-13 17:38:57 +0000 | [diff] [blame] | 828 | /// Values that were rematerialized are left alone, they need LRCalc.extend(). |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 829 | bool SplitEditor::transferValues() { |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 830 | bool Skipped = false; |
| 831 | RegAssignMap::const_iterator AssignI = RegAssign.begin(); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 832 | for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(), |
| 833 | ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) { |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 834 | DEBUG(dbgs() << " blit " << *ParentI << ':'); |
| 835 | VNInfo *ParentVNI = ParentI->valno; |
| 836 | // RegAssign has holes where RegIdx 0 should be used. |
| 837 | SlotIndex Start = ParentI->start; |
| 838 | AssignI.advanceTo(Start); |
| 839 | do { |
| 840 | unsigned RegIdx; |
| 841 | SlotIndex End = ParentI->end; |
| 842 | if (!AssignI.valid()) { |
| 843 | RegIdx = 0; |
| 844 | } else if (AssignI.start() <= Start) { |
| 845 | RegIdx = AssignI.value(); |
| 846 | if (AssignI.stop() < End) { |
| 847 | End = AssignI.stop(); |
| 848 | ++AssignI; |
| 849 | } |
| 850 | } else { |
| 851 | RegIdx = 0; |
| 852 | End = std::min(End, AssignI.start()); |
| 853 | } |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 854 | |
| 855 | // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 856 | DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 857 | LiveRange &LR = LIS.getInterval(Edit->get(RegIdx)); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 858 | |
| 859 | // Check for a simply defined value that can be blitted directly. |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 860 | ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id)); |
| 861 | if (VNInfo *VNI = VFP.getPointer()) { |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 862 | DEBUG(dbgs() << ':' << VNI->id); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 863 | LR.addSegment(LiveInterval::Segment(Start, End, VNI)); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 864 | Start = End; |
| 865 | continue; |
| 866 | } |
| 867 | |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 868 | // Skip values with forced recomputation. |
| 869 | if (VFP.getInt()) { |
| 870 | DEBUG(dbgs() << "(recalc)"); |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 871 | Skipped = true; |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 872 | Start = End; |
| 873 | continue; |
| 874 | } |
| 875 | |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 876 | LiveRangeCalc &LRC = getLRCalc(RegIdx); |
| 877 | |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 878 | // This value has multiple defs in RegIdx, but it wasn't rematerialized, |
| 879 | // so the live range is accurate. Add live-in blocks in [Start;End) to the |
| 880 | // LiveInBlocks. |
| 881 | MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); |
| 882 | SlotIndex BlockStart, BlockEnd; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 883 | std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 884 | |
| 885 | // The first block may be live-in, or it may have its own def. |
| 886 | if (Start != BlockStart) { |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 887 | VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End)); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 888 | assert(VNI && "Missing def for complex mapped value"); |
| 889 | DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber()); |
| 890 | // MBB has its own def. Is it also live-out? |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 891 | if (BlockEnd <= End) |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 892 | LRC.setLiveOutValue(MBB, VNI); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 893 | |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 894 | // Skip to the next block for live-in. |
| 895 | ++MBB; |
| 896 | BlockStart = BlockEnd; |
| 897 | } |
| 898 | |
| 899 | // Handle the live-in blocks covered by [Start;End). |
| 900 | assert(Start <= BlockStart && "Expected live-in block"); |
| 901 | while (BlockStart < End) { |
| 902 | DEBUG(dbgs() << ">BB#" << MBB->getNumber()); |
| 903 | BlockEnd = LIS.getMBBEndIdx(MBB); |
| 904 | if (BlockStart == ParentVNI->def) { |
| 905 | // This block has the def of a parent PHI, so it isn't live-in. |
| 906 | assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 907 | VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End)); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 908 | assert(VNI && "Missing def for complex mapped parent PHI"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 909 | if (End >= BlockEnd) |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 910 | LRC.setLiveOutValue(MBB, VNI); // Live-out as well. |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 911 | } else { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 912 | // This block needs a live-in value. The last block covered may not |
| 913 | // be live-out. |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 914 | if (End < BlockEnd) |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 915 | LRC.addLiveInBlock(LR, MDT[MBB], End); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 916 | else { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 917 | // Live-through, and we don't know the value. |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 918 | LRC.addLiveInBlock(LR, MDT[MBB]); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 919 | LRC.setLiveOutValue(MBB, nullptr); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | BlockStart = BlockEnd; |
| 923 | ++MBB; |
| 924 | } |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 925 | Start = End; |
| 926 | } while (Start != ParentI->end); |
| 927 | DEBUG(dbgs() << '\n'); |
| 928 | } |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 929 | |
Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 930 | LRCalc[0].calculateValues(); |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 931 | if (SpillMode) |
Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 932 | LRCalc[1].calculateValues(); |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 933 | |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 934 | return Skipped; |
| 935 | } |
| 936 | |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 937 | void SplitEditor::extendPHIKillRanges() { |
| 938 | // Extend live ranges to be live-out for successor PHI values. |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 939 | for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(), |
| 940 | E = Edit->getParent().vni_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 941 | const VNInfo *PHIVNI = *I; |
| 942 | if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) |
| 943 | continue; |
| 944 | unsigned RegIdx = RegAssign.lookup(PHIVNI->def); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 945 | LiveRange &LR = LIS.getInterval(Edit->get(RegIdx)); |
Jakob Stoklund Olesen | 820c8fd0 | 2011-09-13 17:38:57 +0000 | [diff] [blame] | 946 | LiveRangeCalc &LRC = getLRCalc(RegIdx); |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 947 | MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); |
| 948 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 949 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 820c8fd0 | 2011-09-13 17:38:57 +0000 | [diff] [blame] | 950 | SlotIndex End = LIS.getMBBEndIdx(*PI); |
| 951 | SlotIndex LastUse = End.getPrevSlot(); |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 952 | // The predecessor may not have a live-out value. That is OK, like an |
| 953 | // undef PHI operand. |
Jakob Stoklund Olesen | 820c8fd0 | 2011-09-13 17:38:57 +0000 | [diff] [blame] | 954 | if (Edit->getParent().liveAt(LastUse)) { |
| 955 | assert(RegAssign.lookup(LastUse) == RegIdx && |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 956 | "Different register assignment in phi predecessor"); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 957 | LRC.extend(LR, End); |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 963 | /// rewriteAssigned - Rewrite all uses of Edit->getReg(). |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 964 | void SplitEditor::rewriteAssigned(bool ExtendRanges) { |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 965 | for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 966 | RE = MRI.reg_end(); RI != RE;) { |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 967 | MachineOperand &MO = *RI; |
Jakob Stoklund Olesen | 959fcc6 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 968 | MachineInstr *MI = MO.getParent(); |
| 969 | ++RI; |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 970 | // LiveDebugVariables should have handled all DBG_VALUE instructions. |
Jakob Stoklund Olesen | 959fcc6 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 971 | if (MI->isDebugValue()) { |
| 972 | DEBUG(dbgs() << "Zapping " << *MI); |
Jakob Stoklund Olesen | 959fcc6 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 973 | MO.setReg(0); |
| 974 | continue; |
| 975 | } |
Jakob Stoklund Olesen | f6e0394 | 2011-02-09 21:52:09 +0000 | [diff] [blame] | 976 | |
Jakob Stoklund Olesen | 56a56eb | 2011-07-24 20:23:50 +0000 | [diff] [blame] | 977 | // <undef> operands don't really read the register, so it doesn't matter |
| 978 | // which register we choose. When the use operand is tied to a def, we must |
| 979 | // use the same register as the def, so just do that always. |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 980 | SlotIndex Idx = LIS.getInstructionIndex(MI); |
Jakob Stoklund Olesen | 56a56eb | 2011-07-24 20:23:50 +0000 | [diff] [blame] | 981 | if (MO.isDef() || MO.isUndef()) |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 982 | Idx = Idx.getRegSlot(MO.isEarlyClobber()); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 983 | |
| 984 | // Rewrite to the mapped register at Idx. |
| 985 | unsigned RegIdx = RegAssign.lookup(Idx); |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 986 | LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); |
Jakob Stoklund Olesen | 820c8fd0 | 2011-09-13 17:38:57 +0000 | [diff] [blame] | 987 | MO.setReg(LI->reg); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 988 | DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' |
| 989 | << Idx << ':' << RegIdx << '\t' << *MI); |
| 990 | |
Jakob Stoklund Olesen | c099dde | 2011-03-18 03:06:02 +0000 | [diff] [blame] | 991 | // Extend liveness to Idx if the instruction reads reg. |
Jakob Stoklund Olesen | 73a9eb9 | 2011-07-24 20:33:23 +0000 | [diff] [blame] | 992 | if (!ExtendRanges || MO.isUndef()) |
Jakob Stoklund Olesen | c099dde | 2011-03-18 03:06:02 +0000 | [diff] [blame] | 993 | continue; |
| 994 | |
| 995 | // Skip instructions that don't read Reg. |
| 996 | if (MO.isDef()) { |
| 997 | if (!MO.getSubReg() && !MO.isEarlyClobber()) |
| 998 | continue; |
| 999 | // We may wan't to extend a live range for a partial redef, or for a use |
| 1000 | // tied to an early clobber. |
| 1001 | Idx = Idx.getPrevSlot(); |
| 1002 | if (!Edit->getParent().liveAt(Idx)) |
| 1003 | continue; |
| 1004 | } else |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 1005 | Idx = Idx.getRegSlot(true); |
Jakob Stoklund Olesen | c099dde | 2011-03-18 03:06:02 +0000 | [diff] [blame] | 1006 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 1007 | getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot()); |
Jakob Stoklund Olesen | 959fcc6 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1011 | void SplitEditor::deleteRematVictims() { |
| 1012 | SmallVector<MachineInstr*, 8> Dead; |
Jakob Stoklund Olesen | 3550242 | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 1013 | for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1014 | LiveInterval *LI = &LIS.getInterval(*I); |
Jakob Stoklund Olesen | 3550242 | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 1015 | for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end(); |
| 1016 | LII != LIE; ++LII) { |
Jakob Stoklund Olesen | d8f2405 | 2011-11-13 22:42:13 +0000 | [diff] [blame] | 1017 | // Dead defs end at the dead slot. |
| 1018 | if (LII->end != LII->valno->def.getDeadSlot()) |
Jakob Stoklund Olesen | 3550242 | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 1019 | continue; |
| 1020 | MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def); |
| 1021 | assert(MI && "Missing instruction for dead def"); |
| 1022 | MI->addRegisterDead(LI->reg, &TRI); |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1023 | |
Jakob Stoklund Olesen | 3550242 | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 1024 | if (!MI->allDefsAreDead()) |
| 1025 | continue; |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1026 | |
Jakob Stoklund Olesen | 3550242 | 2011-03-20 19:46:23 +0000 | [diff] [blame] | 1027 | DEBUG(dbgs() << "All defs dead: " << *MI); |
| 1028 | Dead.push_back(MI); |
| 1029 | } |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | if (Dead.empty()) |
| 1033 | return; |
| 1034 | |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 1035 | Edit->eliminateDeadDefs(Dead); |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Jakob Stoklund Olesen | 6a663b8 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1038 | void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1039 | ++NumFinished; |
Eric Christopher | 2193353 | 2011-02-03 05:40:54 +0000 | [diff] [blame] | 1040 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1041 | // At this point, the live intervals in Edit contain VNInfos corresponding to |
| 1042 | // the inserted copies. |
| 1043 | |
| 1044 | // Add the original defs from the parent interval. |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1045 | for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(), |
| 1046 | E = Edit->getParent().vni_end(); I != E; ++I) { |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1047 | const VNInfo *ParentVNI = *I; |
Jakob Stoklund Olesen | 3295a99 | 2011-02-04 00:59:23 +0000 | [diff] [blame] | 1048 | if (ParentVNI->isUnused()) |
| 1049 | continue; |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 1050 | unsigned RegIdx = RegAssign.lookup(ParentVNI->def); |
Jakob Stoklund Olesen | 97e14e0 | 2012-07-27 21:11:14 +0000 | [diff] [blame] | 1051 | defValue(RegIdx, ParentVNI, ParentVNI->def); |
Jakob Stoklund Olesen | 32210de | 2011-03-15 21:13:22 +0000 | [diff] [blame] | 1052 | |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 1053 | // Force rematted values to be recomputed everywhere. |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1054 | // The new live ranges may be truncated. |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1055 | if (Edit->didRematerialize(ParentVNI)) |
| 1056 | for (unsigned i = 0, e = Edit->size(); i != e; ++i) |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 1057 | forceRecompute(i, ParentVNI); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 1060 | // Hoist back-copies to the complement interval when in spill mode. |
| 1061 | switch (SpillMode) { |
| 1062 | case SM_Partition: |
| 1063 | // Leave all back-copies as is. |
| 1064 | break; |
| 1065 | case SM_Size: |
| 1066 | hoistCopiesForSize(); |
| 1067 | break; |
| 1068 | case SM_Speed: |
| 1069 | llvm_unreachable("Spill mode 'speed' not implemented yet"); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 1072 | // Transfer the simply mapped values, check if any are skipped. |
| 1073 | bool Skipped = transferValues(); |
| 1074 | if (Skipped) |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1075 | extendPHIKillRanges(); |
| 1076 | else |
| 1077 | ++NumSimple; |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1078 | |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 1079 | // Rewrite virtual registers, possibly extending ranges. |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 1080 | rewriteAssigned(Skipped); |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 1081 | |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1082 | // Delete defs that were rematted everywhere. |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 1083 | if (Skipped) |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 1084 | deleteRematVictims(); |
Jakob Stoklund Olesen | 6f8bd42 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1085 | |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1086 | // Get rid of unused values and set phi-kill flags. |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1087 | for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) { |
| 1088 | LiveInterval &LI = LIS.getInterval(*I); |
| 1089 | LI.RenumberValues(); |
| 1090 | } |
Jakob Stoklund Olesen | 6f8bd42 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 1091 | |
Jakob Stoklund Olesen | 6a663b8 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1092 | // Provide a reverse mapping from original indices to Edit ranges. |
| 1093 | if (LRMap) { |
| 1094 | LRMap->clear(); |
| 1095 | for (unsigned i = 0, e = Edit->size(); i != e; ++i) |
| 1096 | LRMap->push_back(i); |
| 1097 | } |
| 1098 | |
Jakob Stoklund Olesen | e4f3317 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1099 | // Now check if any registers were separated into multiple components. |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 1100 | ConnectedVNInfoEqClasses ConEQ(LIS); |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 1101 | for (unsigned i = 0, e = Edit->size(); i != e; ++i) { |
Jakob Stoklund Olesen | e4f3317 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1102 | // Don't use iterators, they are invalidated by create() below. |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1103 | LiveInterval *li = &LIS.getInterval(Edit->get(i)); |
Jakob Stoklund Olesen | e4f3317 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1104 | unsigned NumComp = ConEQ.Classify(li); |
| 1105 | if (NumComp <= 1) |
| 1106 | continue; |
| 1107 | DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); |
| 1108 | SmallVector<LiveInterval*, 8> dups; |
| 1109 | dups.push_back(li); |
Matt Beaumont-Gay | 70597d4 | 2011-04-21 19:46:23 +0000 | [diff] [blame] | 1110 | for (unsigned j = 1; j != NumComp; ++j) |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1111 | dups.push_back(&Edit->createEmptyInterval()); |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1112 | ConEQ.Distribute(&dups[0], MRI); |
Jakob Stoklund Olesen | 6a663b8 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1113 | // The new intervals all map back to i. |
| 1114 | if (LRMap) |
| 1115 | LRMap->resize(Edit->size(), i); |
Jakob Stoklund Olesen | e4f3317 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Jakob Stoklund Olesen | 284c2db | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 1118 | // Calculate spill weight and allocation hints for new intervals. |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 1119 | Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI); |
Jakob Stoklund Olesen | 6a663b8 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1120 | |
| 1121 | assert(!LRMap || LRMap->size() == Edit->size()); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 1125 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 1126 | // Single Block Splitting |
| 1127 | //===----------------------------------------------------------------------===// |
| 1128 | |
Jakob Stoklund Olesen | 8627ea9 | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 1129 | bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI, |
| 1130 | bool SingleInstrs) const { |
| 1131 | // Always split for multiple instructions. |
| 1132 | if (!BI.isOneInstr()) |
| 1133 | return true; |
| 1134 | // Don't split for single instructions unless explicitly requested. |
| 1135 | if (!SingleInstrs) |
| 1136 | return false; |
| 1137 | // Splitting a live-through range always makes progress. |
| 1138 | if (BI.LiveIn && BI.LiveOut) |
| 1139 | return true; |
| 1140 | // No point in isolating a copy. It has no register class constraints. |
| 1141 | if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike()) |
| 1142 | return false; |
| 1143 | // Finally, don't isolate an end point that was created by earlier splits. |
| 1144 | return isOriginalEndpoint(BI.FirstInstr); |
| 1145 | } |
| 1146 | |
Jakob Stoklund Olesen | c70b697 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1147 | void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { |
| 1148 | openIntv(); |
| 1149 | SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1150 | SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr, |
Jakob Stoklund Olesen | c70b697 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1151 | LastSplitPoint)); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1152 | if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) { |
| 1153 | useIntv(SegStart, leaveIntvAfter(BI.LastInstr)); |
Jakob Stoklund Olesen | c70b697 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1154 | } else { |
| 1155 | // The last use is after the last valid split point. |
| 1156 | SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); |
| 1157 | useIntv(SegStart, SegStop); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1158 | overlapIntv(SegStop, BI.LastInstr); |
Jakob Stoklund Olesen | c70b697 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1159 | } |
Jakob Stoklund Olesen | c70b697 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1162 | |
| 1163 | //===----------------------------------------------------------------------===// |
| 1164 | // Global Live Range Splitting Support |
| 1165 | //===----------------------------------------------------------------------===// |
| 1166 | |
| 1167 | // These methods support a method of global live range splitting that uses a |
| 1168 | // global algorithm to decide intervals for CFG edges. They will insert split |
| 1169 | // points and color intervals in basic blocks while avoiding interference. |
| 1170 | // |
| 1171 | // Note that splitSingleBlock is also useful for blocks where both CFG edges |
| 1172 | // are on the stack. |
| 1173 | |
| 1174 | void SplitEditor::splitLiveThroughBlock(unsigned MBBNum, |
| 1175 | unsigned IntvIn, SlotIndex LeaveBefore, |
| 1176 | unsigned IntvOut, SlotIndex EnterAfter){ |
| 1177 | SlotIndex Start, Stop; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 1178 | std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1179 | |
| 1180 | DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop |
| 1181 | << ") intf " << LeaveBefore << '-' << EnterAfter |
| 1182 | << ", live-through " << IntvIn << " -> " << IntvOut); |
| 1183 | |
| 1184 | assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks"); |
| 1185 | |
Jakob Stoklund Olesen | f500cce | 2011-07-23 03:32:26 +0000 | [diff] [blame] | 1186 | assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block"); |
| 1187 | assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf"); |
| 1188 | assert((!EnterAfter || EnterAfter >= Start) && "Interference before block"); |
| 1189 | |
| 1190 | MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum); |
| 1191 | |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1192 | if (!IntvOut) { |
| 1193 | DEBUG(dbgs() << ", spill on entry.\n"); |
| 1194 | // |
| 1195 | // <<<<<<<<< Possible LeaveBefore interference. |
| 1196 | // |-----------| Live through. |
| 1197 | // -____________ Spill on entry. |
| 1198 | // |
| 1199 | selectIntv(IntvIn); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1200 | SlotIndex Idx = leaveIntvAtTop(*MBB); |
| 1201 | assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); |
| 1202 | (void)Idx; |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | if (!IntvIn) { |
| 1207 | DEBUG(dbgs() << ", reload on exit.\n"); |
| 1208 | // |
| 1209 | // >>>>>>> Possible EnterAfter interference. |
| 1210 | // |-----------| Live through. |
| 1211 | // ___________-- Reload on exit. |
| 1212 | // |
| 1213 | selectIntv(IntvOut); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1214 | SlotIndex Idx = enterIntvAtEnd(*MBB); |
| 1215 | assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); |
| 1216 | (void)Idx; |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) { |
| 1221 | DEBUG(dbgs() << ", straight through.\n"); |
| 1222 | // |
| 1223 | // |-----------| Live through. |
| 1224 | // ------------- Straight through, same intv, no interference. |
| 1225 | // |
| 1226 | selectIntv(IntvOut); |
| 1227 | useIntv(Start, Stop); |
| 1228 | return; |
| 1229 | } |
| 1230 | |
| 1231 | // We cannot legally insert splits after LSP. |
| 1232 | SlotIndex LSP = SA.getLastSplitPoint(MBBNum); |
Jakob Stoklund Olesen | f500cce | 2011-07-23 03:32:26 +0000 | [diff] [blame] | 1233 | assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf"); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1234 | |
| 1235 | if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter || |
| 1236 | LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) { |
| 1237 | DEBUG(dbgs() << ", switch avoiding interference.\n"); |
| 1238 | // |
| 1239 | // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference. |
| 1240 | // |-----------| Live through. |
| 1241 | // ------======= Switch intervals between interference. |
| 1242 | // |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1243 | selectIntv(IntvOut); |
Jakob Stoklund Olesen | f500cce | 2011-07-23 03:32:26 +0000 | [diff] [blame] | 1244 | SlotIndex Idx; |
| 1245 | if (LeaveBefore && LeaveBefore < LSP) { |
| 1246 | Idx = enterIntvBefore(LeaveBefore); |
| 1247 | useIntv(Idx, Stop); |
| 1248 | } else { |
| 1249 | Idx = enterIntvAtEnd(*MBB); |
| 1250 | } |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1251 | selectIntv(IntvIn); |
| 1252 | useIntv(Start, Idx); |
| 1253 | assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); |
| 1254 | assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | DEBUG(dbgs() << ", create local intv for interference.\n"); |
| 1259 | // |
| 1260 | // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference. |
| 1261 | // |-----------| Live through. |
| 1262 | // ==---------== Switch intervals before/after interference. |
| 1263 | // |
| 1264 | assert(LeaveBefore <= EnterAfter && "Missed case"); |
| 1265 | |
| 1266 | selectIntv(IntvOut); |
| 1267 | SlotIndex Idx = enterIntvAfter(EnterAfter); |
| 1268 | useIntv(Idx, Stop); |
| 1269 | assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); |
| 1270 | |
| 1271 | selectIntv(IntvIn); |
| 1272 | Idx = leaveIntvBefore(LeaveBefore); |
| 1273 | useIntv(Start, Idx); |
| 1274 | assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI, |
| 1279 | unsigned IntvIn, SlotIndex LeaveBefore) { |
| 1280 | SlotIndex Start, Stop; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 1281 | std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1282 | |
| 1283 | DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1284 | << "), uses " << BI.FirstInstr << '-' << BI.LastInstr |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1285 | << ", reg-in " << IntvIn << ", leave before " << LeaveBefore |
| 1286 | << (BI.LiveOut ? ", stack-out" : ", killed in block")); |
| 1287 | |
| 1288 | assert(IntvIn && "Must have register in"); |
| 1289 | assert(BI.LiveIn && "Must be live-in"); |
| 1290 | assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference"); |
| 1291 | |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1292 | if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) { |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1293 | DEBUG(dbgs() << " before interference.\n"); |
| 1294 | // |
| 1295 | // <<< Interference after kill. |
| 1296 | // |---o---x | Killed in block. |
| 1297 | // ========= Use IntvIn everywhere. |
| 1298 | // |
| 1299 | selectIntv(IntvIn); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1300 | useIntv(Start, BI.LastInstr); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); |
| 1305 | |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1306 | if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) { |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1307 | // |
| 1308 | // <<< Possible interference after last use. |
| 1309 | // |---o---o---| Live-out on stack. |
| 1310 | // =========____ Leave IntvIn after last use. |
| 1311 | // |
| 1312 | // < Interference after last use. |
| 1313 | // |---o---o--o| Live-out on stack, late last use. |
| 1314 | // ============ Copy to stack after LSP, overlap IntvIn. |
| 1315 | // \_____ Stack interval is live-out. |
| 1316 | // |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1317 | if (BI.LastInstr < LSP) { |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1318 | DEBUG(dbgs() << ", spill after last use before interference.\n"); |
| 1319 | selectIntv(IntvIn); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1320 | SlotIndex Idx = leaveIntvAfter(BI.LastInstr); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1321 | useIntv(Start, Idx); |
| 1322 | assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); |
| 1323 | } else { |
| 1324 | DEBUG(dbgs() << ", spill before last split point.\n"); |
| 1325 | selectIntv(IntvIn); |
Jakob Stoklund Olesen | 37e3a13 | 2011-07-16 00:13:30 +0000 | [diff] [blame] | 1326 | SlotIndex Idx = leaveIntvBefore(LSP); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1327 | overlapIntv(Idx, BI.LastInstr); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1328 | useIntv(Start, Idx); |
| 1329 | assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); |
| 1330 | } |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | // The interference is overlapping somewhere we wanted to use IntvIn. That |
| 1335 | // means we need to create a local interval that can be allocated a |
| 1336 | // different register. |
| 1337 | unsigned LocalIntv = openIntv(); |
Matt Beaumont-Gay | 26909d8 | 2011-07-16 04:18:47 +0000 | [diff] [blame] | 1338 | (void)LocalIntv; |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1339 | DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n"); |
| 1340 | |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1341 | if (!BI.LiveOut || BI.LastInstr < LSP) { |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1342 | // |
| 1343 | // <<<<<<< Interference overlapping uses. |
| 1344 | // |---o---o---| Live-out on stack. |
| 1345 | // =====----____ Leave IntvIn before interference, then spill. |
| 1346 | // |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1347 | SlotIndex To = leaveIntvAfter(BI.LastInstr); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1348 | SlotIndex From = enterIntvBefore(LeaveBefore); |
| 1349 | useIntv(From, To); |
| 1350 | selectIntv(IntvIn); |
| 1351 | useIntv(Start, From); |
| 1352 | assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | // <<<<<<< Interference overlapping uses. |
| 1357 | // |---o---o--o| Live-out on stack, late last use. |
| 1358 | // =====------- Copy to stack before LSP, overlap LocalIntv. |
| 1359 | // \_____ Stack interval is live-out. |
| 1360 | // |
| 1361 | SlotIndex To = leaveIntvBefore(LSP); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1362 | overlapIntv(To, BI.LastInstr); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1363 | SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore)); |
| 1364 | useIntv(From, To); |
| 1365 | selectIntv(IntvIn); |
| 1366 | useIntv(Start, From); |
| 1367 | assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); |
| 1368 | } |
| 1369 | |
| 1370 | void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, |
| 1371 | unsigned IntvOut, SlotIndex EnterAfter) { |
| 1372 | SlotIndex Start, Stop; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 1373 | std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1374 | |
| 1375 | DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1376 | << "), uses " << BI.FirstInstr << '-' << BI.LastInstr |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1377 | << ", reg-out " << IntvOut << ", enter after " << EnterAfter |
| 1378 | << (BI.LiveIn ? ", stack-in" : ", defined in block")); |
| 1379 | |
| 1380 | SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); |
| 1381 | |
| 1382 | assert(IntvOut && "Must have register out"); |
| 1383 | assert(BI.LiveOut && "Must be live-out"); |
| 1384 | assert((!EnterAfter || EnterAfter < LSP) && "Bad interference"); |
| 1385 | |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1386 | if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) { |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1387 | DEBUG(dbgs() << " after interference.\n"); |
| 1388 | // |
| 1389 | // >>>> Interference before def. |
| 1390 | // | o---o---| Defined in block. |
| 1391 | // ========= Use IntvOut everywhere. |
| 1392 | // |
| 1393 | selectIntv(IntvOut); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1394 | useIntv(BI.FirstInstr, Stop); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | } |
| 1397 | |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1398 | if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) { |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1399 | DEBUG(dbgs() << ", reload after interference.\n"); |
| 1400 | // |
| 1401 | // >>>> Interference before def. |
| 1402 | // |---o---o---| Live-through, stack-in. |
| 1403 | // ____========= Enter IntvOut before first use. |
| 1404 | // |
| 1405 | selectIntv(IntvOut); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1406 | SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr)); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1407 | useIntv(Idx, Stop); |
| 1408 | assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | // The interference is overlapping somewhere we wanted to use IntvOut. That |
| 1413 | // means we need to create a local interval that can be allocated a |
| 1414 | // different register. |
| 1415 | DEBUG(dbgs() << ", interference overlaps uses.\n"); |
| 1416 | // |
| 1417 | // >>>>>>> Interference overlapping uses. |
| 1418 | // |---o---o---| Live-through, stack-in. |
| 1419 | // ____---====== Create local interval for interference range. |
| 1420 | // |
| 1421 | selectIntv(IntvOut); |
| 1422 | SlotIndex Idx = enterIntvAfter(EnterAfter); |
| 1423 | useIntv(Idx, Stop); |
| 1424 | assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); |
| 1425 | |
| 1426 | openIntv(); |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1427 | SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr)); |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1428 | useIntv(From, Idx); |
| 1429 | } |