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