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