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