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