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