Chris Lattner | 8563833 | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 1 | //===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===// |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 10 | /// \file This file implements the LiveInterval analysis pass which is used |
| 11 | /// by the Linear Scan Register allocator. This pass linearizes the |
| 12 | /// basic blocks of the function in DFS order and computes live intervals for |
| 13 | /// each virtual and physical register. |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | b1f8982 | 2005-09-21 04:19:09 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "LiveRangeCalc.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 09b0448 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveVariables.h" |
Michael Gottesman | 9f49d74 | 2013-12-14 00:53:32 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineDominators.h" |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/VirtRegMap.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Value.h" |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 29 | #include "llvm/Support/BlockFrequency.h" |
Jakob Stoklund Olesen | 4021a7b | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetSubtargetInfo.h" |
Alkis Evlogimenos | a5c04ee | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 38 | #include <cmath> |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "regalloc" |
| 42 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 43 | char LiveIntervals::ID = 0; |
Jakob Stoklund Olesen | 1c46589 | 2012-08-03 22:12:54 +0000 | [diff] [blame] | 44 | char &llvm::LiveIntervalsID = LiveIntervals::ID; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 45 | INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", |
| 46 | "Live Interval Analysis", false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 47 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 48 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 49 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 50 | INITIALIZE_PASS_END(LiveIntervals, "liveintervals", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 51 | "Live Interval Analysis", false, false) |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 52 | |
Andrew Trick | 8d02e91 | 2013-06-21 18:33:23 +0000 | [diff] [blame] | 53 | #ifndef NDEBUG |
| 54 | static cl::opt<bool> EnablePrecomputePhysRegs( |
| 55 | "precompute-phys-liveness", cl::Hidden, |
| 56 | cl::desc("Eagerly compute live intervals for all physreg units.")); |
| 57 | #else |
| 58 | static bool EnablePrecomputePhysRegs = false; |
| 59 | #endif // NDEBUG |
| 60 | |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 61 | namespace llvm { |
| 62 | cl::opt<bool> UseSegmentSetForPhysRegs( |
| 63 | "use-segment-set-for-physregs", cl::Hidden, cl::init(true), |
| 64 | cl::desc( |
| 65 | "Use segment set for the computation of the live ranges of physregs.")); |
| 66 | } |
| 67 | |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 68 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 0402315 | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 69 | AU.setPreservesCFG(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 70 | AU.addRequired<AAResultsWrapperPass>(); |
| 71 | AU.addPreserved<AAResultsWrapperPass>(); |
Evan Cheng | 16bfe5b | 2010-08-17 21:00:37 +0000 | [diff] [blame] | 72 | AU.addPreserved<LiveVariables>(); |
Andrew Trick | 5188c00 | 2012-02-13 20:44:42 +0000 | [diff] [blame] | 73 | AU.addPreservedID(MachineLoopInfoID); |
Jakob Stoklund Olesen | 51c63e6 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 74 | AU.addRequiredTransitiveID(MachineDominatorsID); |
Bill Wendling | 0c20943 | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 75 | AU.addPreservedID(MachineDominatorsID); |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 76 | AU.addPreserved<SlotIndexes>(); |
| 77 | AU.addRequiredTransitive<SlotIndexes>(); |
Alkis Evlogimenos | a698308 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 78 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 81 | LiveIntervals::LiveIntervals() : MachineFunctionPass(ID), |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 82 | DomTree(nullptr), LRCalc(nullptr) { |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 83 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 84 | } |
| 85 | |
| 86 | LiveIntervals::~LiveIntervals() { |
| 87 | delete LRCalc; |
| 88 | } |
| 89 | |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 90 | void LiveIntervals::releaseMemory() { |
Owen Anderson | 51f689a | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 91 | // Free the live intervals themselves. |
Jakob Stoklund Olesen | c61edda | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 92 | for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i) |
| 93 | delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)]; |
| 94 | VirtRegIntervals.clear(); |
Jakob Stoklund Olesen | 3ff74d8 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 95 | RegMaskSlots.clear(); |
| 96 | RegMaskBits.clear(); |
Jakob Stoklund Olesen | 25c4195 | 2012-02-10 01:26:29 +0000 | [diff] [blame] | 97 | RegMaskBlocks.clear(); |
Lang Hames | dab7b06 | 2009-07-09 03:57:02 +0000 | [diff] [blame] | 98 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 99 | for (LiveRange *LR : RegUnitRanges) |
| 100 | delete LR; |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 101 | RegUnitRanges.clear(); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 102 | |
Benjamin Kramer | a000002 | 2010-06-26 11:30:59 +0000 | [diff] [blame] | 103 | // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd. |
| 104 | VNInfoAllocator.Reset(); |
Alkis Evlogimenos | 50d97e3 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Owen Anderson | 4f8e1ad | 2008-05-28 20:54:50 +0000 | [diff] [blame] | 107 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Jakob Stoklund Olesen | 11fb248 | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 108 | MF = &fn; |
| 109 | MRI = &MF->getRegInfo(); |
Eric Christopher | d3fa440 | 2014-10-14 06:26:53 +0000 | [diff] [blame] | 110 | TRI = MF->getSubtarget().getRegisterInfo(); |
| 111 | TII = MF->getSubtarget().getInstrInfo(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 112 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Jakob Stoklund Olesen | 11fb248 | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 113 | Indexes = &getAnalysis<SlotIndexes>(); |
Jakob Stoklund Olesen | 51c63e6 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 114 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Matthias Braun | e3d3b88 | 2014-12-10 01:12:30 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | 51c63e6 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 116 | if (!LRCalc) |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 117 | LRCalc = new LiveRangeCalc(); |
Owen Anderson | 4f8e1ad | 2008-05-28 20:54:50 +0000 | [diff] [blame] | 118 | |
Jakob Stoklund Olesen | 4021a7b | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 119 | // Allocate space for all virtual registers. |
| 120 | VirtRegIntervals.resize(MRI->getNumVirtRegs()); |
| 121 | |
Jakob Stoklund Olesen | fac770b | 2013-02-09 00:04:07 +0000 | [diff] [blame] | 122 | computeVirtRegs(); |
| 123 | computeRegMasks(); |
Jakob Stoklund Olesen | 51c63e6 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 124 | computeLiveInRegUnits(); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 125 | |
Andrew Trick | 8d02e91 | 2013-06-21 18:33:23 +0000 | [diff] [blame] | 126 | if (EnablePrecomputePhysRegs) { |
| 127 | // For stress testing, precompute live ranges of all physical register |
| 128 | // units, including reserved registers. |
| 129 | for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) |
| 130 | getRegUnit(i); |
| 131 | } |
Chris Lattner | b0b707f | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 132 | DEBUG(dump()); |
Alkis Evlogimenos | a698308 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 133 | return true; |
Alkis Evlogimenos | 0e9ded7 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 136 | void LiveIntervals::print(raw_ostream &OS, const Module* ) const { |
Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 137 | OS << "********** INTERVALS **********\n"; |
Jakob Stoklund Olesen | 20d25a7 | 2012-02-14 23:46:21 +0000 | [diff] [blame] | 138 | |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 139 | // Dump the regunits. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 140 | for (unsigned Unit = 0, UnitE = RegUnitRanges.size(); Unit != UnitE; ++Unit) |
| 141 | if (LiveRange *LR = RegUnitRanges[Unit]) |
| 142 | OS << PrintRegUnit(Unit, TRI) << ' ' << *LR << '\n'; |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 143 | |
Jakob Stoklund Olesen | 20d25a7 | 2012-02-14 23:46:21 +0000 | [diff] [blame] | 144 | // Dump the virtregs. |
Jakob Stoklund Olesen | c61edda | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 145 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 146 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 147 | if (hasInterval(Reg)) |
Matthias Braun | f6fe6bf | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 148 | OS << getInterval(Reg) << '\n'; |
Jakob Stoklund Olesen | c61edda | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | b0b707f | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 150 | |
Jakob Stoklund Olesen | 13d5562 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 151 | OS << "RegMasks:"; |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 152 | for (SlotIndex Idx : RegMaskSlots) |
| 153 | OS << ' ' << Idx; |
Jakob Stoklund Olesen | 13d5562 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 154 | OS << '\n'; |
| 155 | |
Evan Cheng | 7f78959 | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 156 | printInstrs(OS); |
| 157 | } |
| 158 | |
| 159 | void LiveIntervals::printInstrs(raw_ostream &OS) const { |
Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 160 | OS << "********** MACHINEINSTRS **********\n"; |
Jakob Stoklund Olesen | 11fb248 | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 161 | MF->print(OS, Indexes); |
Chris Lattner | b0b707f | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 164 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 165 | LLVM_DUMP_METHOD void LiveIntervals::dumpInstrs() const { |
David Greene | 1a51a21 | 2010-01-04 22:49:02 +0000 | [diff] [blame] | 166 | printInstrs(dbgs()); |
Evan Cheng | 7f78959 | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 167 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 168 | #endif |
Evan Cheng | 7f78959 | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 169 | |
Owen Anderson | 51f689a | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 170 | LiveInterval* LiveIntervals::createInterval(unsigned reg) { |
Aaron Ballman | 0499904 | 2013-11-13 00:15:44 +0000 | [diff] [blame] | 171 | float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? |
| 172 | llvm::huge_valf : 0.0F; |
Owen Anderson | 51f689a | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 173 | return new LiveInterval(reg, Weight); |
Alkis Evlogimenos | 237f203 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 174 | } |
Evan Cheng | be51f28 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 175 | |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 176 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 177 | /// Compute the live interval of a virtual register, based on defs and uses. |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 178 | void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) { |
Jakob Stoklund Olesen | 4021a7b | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 179 | assert(LRCalc && "LRCalc not initialized."); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 180 | assert(LI.empty() && "Should only compute empty intervals."); |
Jakob Stoklund Olesen | 4021a7b | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 181 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
Matthias Braun | e9631f1 | 2016-04-28 20:35:26 +0000 | [diff] [blame] | 182 | LRCalc->calculate(LI, MRI->shouldTrackSubRegLiveness(LI.reg)); |
| 183 | computeDeadValues(LI, nullptr); |
Jakob Stoklund Olesen | 4021a7b | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 186 | void LiveIntervals::computeVirtRegs() { |
| 187 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 188 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 189 | if (MRI->reg_nodbg_empty(Reg)) |
| 190 | continue; |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 191 | createAndComputeVirtRegInterval(Reg); |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void LiveIntervals::computeRegMasks() { |
| 196 | RegMaskBlocks.resize(MF->getNumBlockIDs()); |
| 197 | |
| 198 | // Find all instructions with regmask operands. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 199 | for (const MachineBasicBlock &MBB : *MF) { |
Reid Kleckner | e535c1f | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 200 | std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB.getNumber()]; |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 201 | RMB.first = RegMaskSlots.size(); |
Reid Kleckner | b8fd162 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 202 | |
| 203 | // Some block starts, such as EH funclets, create masks. |
| 204 | if (const uint32_t *Mask = MBB.getBeginClobberMask(TRI)) { |
| 205 | RegMaskSlots.push_back(Indexes->getMBBStartIdx(&MBB)); |
| 206 | RegMaskBits.push_back(Mask); |
| 207 | } |
| 208 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 209 | for (const MachineInstr &MI : MBB) { |
Reid Kleckner | e535c1f | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 210 | for (const MachineOperand &MO : MI.operands()) { |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 211 | if (!MO.isRegMask()) |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 212 | continue; |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 213 | RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot()); |
Reid Kleckner | e535c1f | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 214 | RegMaskBits.push_back(MO.getRegMask()); |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 215 | } |
Reid Kleckner | e535c1f | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 216 | } |
Reid Kleckner | b8fd162 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 217 | |
Reid Kleckner | 70c9bc7 | 2016-02-26 16:53:19 +0000 | [diff] [blame] | 218 | // Some block ends, such as funclet returns, create masks. Put the mask on |
| 219 | // the last instruction of the block, because MBB slot index intervals are |
| 220 | // half-open. |
Reid Kleckner | b8fd162 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 221 | if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) { |
Reid Kleckner | 70c9bc7 | 2016-02-26 16:53:19 +0000 | [diff] [blame] | 222 | assert(!MBB.empty() && "empty return block?"); |
| 223 | RegMaskSlots.push_back( |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 224 | Indexes->getInstructionIndex(MBB.back()).getRegSlot()); |
Reid Kleckner | b8fd162 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 225 | RegMaskBits.push_back(Mask); |
| 226 | } |
| 227 | |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 228 | // Compute the number of register mask instructions in this block. |
Dmitri Gribenko | ca1e27b | 2012-09-10 21:26:47 +0000 | [diff] [blame] | 229 | RMB.second = RegMaskSlots.size() - RMB.first; |
Jakob Stoklund Olesen | 7dfe7ab | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Jakob Stoklund Olesen | 4021a7b | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 232 | |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | // Register Unit Liveness |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | // |
| 237 | // Fixed interference typically comes from ABI boundaries: Function arguments |
| 238 | // and return values are passed in fixed registers, and so are exception |
| 239 | // pointers entering landing pads. Certain instructions require values to be |
| 240 | // present in specific registers. That is also represented through fixed |
| 241 | // interference. |
| 242 | // |
| 243 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 244 | /// Compute the live range of a register unit, based on the uses and defs of |
| 245 | /// aliasing registers. The range should be empty, or contain only dead |
| 246 | /// phi-defs from ABI blocks. |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 247 | void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) { |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 248 | assert(LRCalc && "LRCalc not initialized."); |
| 249 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
| 250 | |
| 251 | // The physregs aliasing Unit are the roots and their super-registers. |
| 252 | // Create all values as dead defs before extending to uses. Note that roots |
| 253 | // may share super-registers. That's OK because createDeadDefs() is |
| 254 | // idempotent. It is very rare for a register unit to have multiple roots, so |
| 255 | // uniquing super-registers is probably not worthwhile. |
Matthias Braun | b901d33 | 2017-01-24 01:12:58 +0000 | [diff] [blame] | 256 | bool IsReserved = true; |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 257 | for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { |
| 258 | for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true); |
| 259 | Super.isValid(); ++Super) { |
| 260 | unsigned Reg = *Super; |
| 261 | if (!MRI->reg_empty(Reg)) |
| 262 | LRCalc->createDeadDefs(LR, Reg); |
Matthias Braun | b901d33 | 2017-01-24 01:12:58 +0000 | [diff] [blame] | 263 | // A register unit is considered reserved if all its roots and all their |
| 264 | // super registers are reserved. |
| 265 | if (!MRI->isReserved(Reg)) |
| 266 | IsReserved = false; |
Matthias Braun | c3a72c2 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | // Now extend LR to reach all uses. |
| 271 | // Ignore uses of reserved registers. We only track defs of those. |
Matthias Braun | b901d33 | 2017-01-24 01:12:58 +0000 | [diff] [blame] | 272 | if (!IsReserved) { |
| 273 | for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { |
| 274 | for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true); |
| 275 | Super.isValid(); ++Super) { |
| 276 | unsigned Reg = *Super; |
| 277 | if (!MRI->reg_empty(Reg)) |
| 278 | LRCalc->extendToUses(LR, Reg); |
| 279 | } |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 282 | |
| 283 | // Flush the segment set to the segment vector. |
| 284 | if (UseSegmentSetForPhysRegs) |
| 285 | LR.flushSegmentSet(); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 288 | /// Precompute the live ranges of any register units that are live-in to an ABI |
| 289 | /// block somewhere. Register values can appear without a corresponding def when |
| 290 | /// entering the entry block or a landing pad. |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 291 | void LiveIntervals::computeLiveInRegUnits() { |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 292 | RegUnitRanges.resize(TRI->getNumRegUnits()); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 293 | DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n"); |
| 294 | |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 295 | // Keep track of the live range sets allocated. |
| 296 | SmallVector<unsigned, 8> NewRanges; |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 297 | |
| 298 | // Check all basic blocks for live-ins. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 299 | for (const MachineBasicBlock &MBB : *MF) { |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 300 | // We only care about ABI blocks: Entry + landing pads. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 301 | if ((&MBB != &MF->front() && !MBB.isEHPad()) || MBB.livein_empty()) |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 302 | continue; |
| 303 | |
| 304 | // Create phi-defs at Begin for all live-in registers. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 305 | SlotIndex Begin = Indexes->getMBBStartIdx(&MBB); |
| 306 | DEBUG(dbgs() << Begin << "\tBB#" << MBB.getNumber()); |
| 307 | for (const auto &LI : MBB.liveins()) { |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 308 | for (MCRegUnitIterator Units(LI.PhysReg, TRI); Units.isValid(); ++Units) { |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 309 | unsigned Unit = *Units; |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 310 | LiveRange *LR = RegUnitRanges[Unit]; |
| 311 | if (!LR) { |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 312 | // Use segment set to speed-up initial computation of the live range. |
| 313 | LR = RegUnitRanges[Unit] = new LiveRange(UseSegmentSetForPhysRegs); |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 314 | NewRanges.push_back(Unit); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 315 | } |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 316 | VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator()); |
Matt Beaumont-Gay | 7ba769b | 2012-06-05 23:00:03 +0000 | [diff] [blame] | 317 | (void)VNI; |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 318 | DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << '#' << VNI->id); |
| 319 | } |
| 320 | } |
| 321 | DEBUG(dbgs() << '\n'); |
| 322 | } |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 323 | DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n"); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 324 | |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 325 | // Compute the 'normal' part of the ranges. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 326 | for (unsigned Unit : NewRanges) |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 327 | computeRegUnitRange(*RegUnitRanges[Unit], Unit); |
Jakob Stoklund Olesen | 12e03da | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 330 | static void createSegmentsForValues(LiveRange &LR, |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 331 | iterator_range<LiveInterval::vni_iterator> VNIs) { |
| 332 | for (VNInfo *VNI : VNIs) { |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 333 | if (VNI->isUnused()) |
| 334 | continue; |
| 335 | SlotIndex Def = VNI->def; |
| 336 | LR.addSegment(LiveRange::Segment(Def, Def.getDeadSlot(), VNI)); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | typedef SmallVector<std::pair<SlotIndex, VNInfo*>, 16> ShrinkToUsesWorkList; |
| 341 | |
| 342 | static void extendSegmentsToUses(LiveRange &LR, const SlotIndexes &Indexes, |
| 343 | ShrinkToUsesWorkList &WorkList, |
| 344 | const LiveRange &OldRange) { |
| 345 | // Keep track of the PHIs that are in use. |
| 346 | SmallPtrSet<VNInfo*, 8> UsedPHIs; |
| 347 | // Blocks that have already been added to WorkList as live-out. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 348 | SmallPtrSet<const MachineBasicBlock*, 16> LiveOut; |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 349 | |
| 350 | // Extend intervals to reach all uses in WorkList. |
| 351 | while (!WorkList.empty()) { |
| 352 | SlotIndex Idx = WorkList.back().first; |
| 353 | VNInfo *VNI = WorkList.back().second; |
| 354 | WorkList.pop_back(); |
| 355 | const MachineBasicBlock *MBB = Indexes.getMBBFromIndex(Idx.getPrevSlot()); |
| 356 | SlotIndex BlockStart = Indexes.getMBBStartIdx(MBB); |
| 357 | |
| 358 | // Extend the live range for VNI to be live at Idx. |
| 359 | if (VNInfo *ExtVNI = LR.extendInBlock(BlockStart, Idx)) { |
| 360 | assert(ExtVNI == VNI && "Unexpected existing value number"); |
| 361 | (void)ExtVNI; |
| 362 | // Is this a PHIDef we haven't seen before? |
| 363 | if (!VNI->isPHIDef() || VNI->def != BlockStart || |
| 364 | !UsedPHIs.insert(VNI).second) |
| 365 | continue; |
| 366 | // The PHI is live, make sure the predecessors are live-out. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 367 | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 368 | if (!LiveOut.insert(Pred).second) |
| 369 | continue; |
| 370 | SlotIndex Stop = Indexes.getMBBEndIdx(Pred); |
| 371 | // A predecessor is not required to have a live-out value for a PHI. |
| 372 | if (VNInfo *PVNI = OldRange.getVNInfoBefore(Stop)) |
| 373 | WorkList.push_back(std::make_pair(Stop, PVNI)); |
| 374 | } |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | // VNI is live-in to MBB. |
| 379 | DEBUG(dbgs() << " live-in at " << BlockStart << '\n'); |
| 380 | LR.addSegment(LiveRange::Segment(BlockStart, Idx, VNI)); |
| 381 | |
| 382 | // Make sure VNI is live-out from the predecessors. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 383 | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 384 | if (!LiveOut.insert(Pred).second) |
| 385 | continue; |
| 386 | SlotIndex Stop = Indexes.getMBBEndIdx(Pred); |
| 387 | assert(OldRange.getVNInfoBefore(Stop) == VNI && |
| 388 | "Wrong value out of predecessor"); |
| 389 | WorkList.push_back(std::make_pair(Stop, VNI)); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Jakob Stoklund Olesen | 8630840 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 394 | bool LiveIntervals::shrinkToUses(LiveInterval *li, |
Jakob Stoklund Olesen | 71c380f | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 395 | SmallVectorImpl<MachineInstr*> *dead) { |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 396 | DEBUG(dbgs() << "Shrink: " << *li << '\n'); |
| 397 | assert(TargetRegisterInfo::isVirtualRegister(li->reg) |
Lang Hames | c405ac4 | 2012-01-03 20:05:57 +0000 | [diff] [blame] | 398 | && "Can only shrink virtual registers"); |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 399 | |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 400 | // Shrink subregister live ranges. |
Matthias Braun | 0d4cebd | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 401 | bool NeedsCleanup = false; |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 402 | for (LiveInterval::SubRange &S : li->subranges()) { |
| 403 | shrinkToUses(S, li->reg); |
Matthias Braun | 0d4cebd | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 404 | if (S.empty()) |
| 405 | NeedsCleanup = true; |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 406 | } |
Matthias Braun | 0d4cebd | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 407 | if (NeedsCleanup) |
| 408 | li->removeEmptySubRanges(); |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 409 | |
| 410 | // Find all the values used, including PHI kills. |
| 411 | ShrinkToUsesWorkList WorkList; |
Jakob Stoklund Olesen | b8b1d4c | 2011-09-15 15:24:16 +0000 | [diff] [blame] | 412 | |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 413 | // Visit all instructions reading li->reg. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 414 | unsigned Reg = li->reg; |
| 415 | for (MachineInstr &UseMI : MRI->reg_instructions(Reg)) { |
| 416 | if (UseMI.isDebugValue() || !UseMI.readsVirtualRegister(Reg)) |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 417 | continue; |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 418 | SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); |
Matthias Braun | 88dd0ab | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 419 | LiveQueryResult LRQ = li->Query(Idx); |
Jakob Stoklund Olesen | 02d83e3 | 2012-05-20 02:54:52 +0000 | [diff] [blame] | 420 | VNInfo *VNI = LRQ.valueIn(); |
Jakob Stoklund Olesen | fdc0994 | 2011-03-18 03:06:04 +0000 | [diff] [blame] | 421 | if (!VNI) { |
| 422 | // This shouldn't happen: readsVirtualRegister returns true, but there is |
| 423 | // no live value. It is likely caused by a target getting <undef> flags |
| 424 | // wrong. |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 425 | DEBUG(dbgs() << Idx << '\t' << UseMI |
Jakob Stoklund Olesen | fdc0994 | 2011-03-18 03:06:04 +0000 | [diff] [blame] | 426 | << "Warning: Instr claims to read non-existent value in " |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 427 | << *li << '\n'); |
Jakob Stoklund Olesen | fdc0994 | 2011-03-18 03:06:04 +0000 | [diff] [blame] | 428 | continue; |
| 429 | } |
Jakob Stoklund Olesen | 7e6004a | 2011-11-14 18:45:38 +0000 | [diff] [blame] | 430 | // Special case: An early-clobber tied operand reads and writes the |
Jakob Stoklund Olesen | 02d83e3 | 2012-05-20 02:54:52 +0000 | [diff] [blame] | 431 | // register one slot early. |
| 432 | if (VNInfo *DefVNI = LRQ.valueDefined()) |
| 433 | Idx = DefVNI->def; |
| 434 | |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 435 | WorkList.push_back(std::make_pair(Idx, VNI)); |
| 436 | } |
| 437 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 438 | // Create new live ranges with only minimal live segments per def. |
| 439 | LiveRange NewLR; |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 440 | createSegmentsForValues(NewLR, make_range(li->vni_begin(), li->vni_end())); |
| 441 | extendSegmentsToUses(NewLR, *Indexes, WorkList, *li); |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 442 | |
Pete Cooper | 7223557 | 2014-06-03 22:42:10 +0000 | [diff] [blame] | 443 | // Move the trimmed segments back. |
| 444 | li->segments.swap(NewLR.segments); |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 445 | |
| 446 | // Handle dead values. |
| 447 | bool CanSeparate = computeDeadValues(*li, dead); |
Pete Cooper | 7223557 | 2014-06-03 22:42:10 +0000 | [diff] [blame] | 448 | DEBUG(dbgs() << "Shrunk: " << *li << '\n'); |
| 449 | return CanSeparate; |
| 450 | } |
| 451 | |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 452 | bool LiveIntervals::computeDeadValues(LiveInterval &LI, |
Pete Cooper | 7223557 | 2014-06-03 22:42:10 +0000 | [diff] [blame] | 453 | SmallVectorImpl<MachineInstr*> *dead) { |
Matthias Braun | 73e4221 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 454 | bool MayHaveSplitComponents = false; |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 455 | for (VNInfo *VNI : LI.valnos) { |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 456 | if (VNI->isUnused()) |
| 457 | continue; |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 458 | SlotIndex Def = VNI->def; |
| 459 | LiveRange::iterator I = LI.FindSegmentContaining(Def); |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 460 | assert(I != LI.end() && "Missing segment for VNI"); |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 461 | |
| 462 | // Is the register live before? Otherwise we may have to add a read-undef |
| 463 | // flag for subregister defs. |
Matthias Braun | 73e4221 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 464 | unsigned VReg = LI.reg; |
| 465 | if (MRI->shouldTrackSubRegLiveness(VReg)) { |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 466 | if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) { |
| 467 | MachineInstr *MI = getInstructionFromIndex(Def); |
Matthias Braun | 2c98d0f | 2015-11-11 00:41:58 +0000 | [diff] [blame] | 468 | MI->setRegisterDefReadUndef(VReg); |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | if (I->end != Def.getDeadSlot()) |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 473 | continue; |
Jakob Stoklund Olesen | 81eb18d | 2011-03-02 00:33:01 +0000 | [diff] [blame] | 474 | if (VNI->isPHIDef()) { |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 475 | // This is a dead PHI. Remove it. |
Jakob Stoklund Olesen | daae19f | 2012-08-03 20:59:32 +0000 | [diff] [blame] | 476 | VNI->markUnused(); |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 477 | LI.removeSegment(I); |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 478 | DEBUG(dbgs() << "Dead PHI at " << Def << " may separate interval\n"); |
Matthias Braun | 73e4221 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 479 | MayHaveSplitComponents = true; |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 480 | } else { |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 481 | // This is a dead def. Make sure the instruction knows. |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 482 | MachineInstr *MI = getInstructionFromIndex(Def); |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 483 | assert(MI && "No instruction defining live value"); |
Matthias Braun | e9631f1 | 2016-04-28 20:35:26 +0000 | [diff] [blame] | 484 | MI->addRegisterDead(LI.reg, TRI); |
Jakob Stoklund Olesen | 71c380f | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 485 | if (dead && MI->allDefsAreDead()) { |
Matthias Braun | c1988f3 | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 486 | DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI); |
Jakob Stoklund Olesen | 71c380f | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 487 | dead->push_back(MI); |
| 488 | } |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
Matthias Braun | 73e4221 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 491 | return MayHaveSplitComponents; |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 494 | void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, unsigned Reg) { |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 495 | DEBUG(dbgs() << "Shrink: " << SR << '\n'); |
| 496 | assert(TargetRegisterInfo::isVirtualRegister(Reg) |
| 497 | && "Can only shrink virtual registers"); |
| 498 | // Find all the values used, including PHI kills. |
| 499 | ShrinkToUsesWorkList WorkList; |
| 500 | |
| 501 | // Visit all instructions reading Reg. |
| 502 | SlotIndex LastIdx; |
Krzysztof Parzyszek | 3bf4aec | 2016-09-02 19:48:55 +0000 | [diff] [blame] | 503 | for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { |
| 504 | // Skip "undef" uses. |
| 505 | if (!MO.readsReg()) |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 506 | continue; |
| 507 | // Maybe the operand is for a subregister we don't care about. |
| 508 | unsigned SubReg = MO.getSubReg(); |
| 509 | if (SubReg != 0) { |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 510 | LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg); |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 511 | if ((LaneMask & SR.LaneMask).none()) |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 512 | continue; |
| 513 | } |
| 514 | // We only need to visit each instruction once. |
Krzysztof Parzyszek | 3bf4aec | 2016-09-02 19:48:55 +0000 | [diff] [blame] | 515 | MachineInstr *UseMI = MO.getParent(); |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 516 | SlotIndex Idx = getInstructionIndex(*UseMI).getRegSlot(); |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 517 | if (Idx == LastIdx) |
| 518 | continue; |
| 519 | LastIdx = Idx; |
| 520 | |
| 521 | LiveQueryResult LRQ = SR.Query(Idx); |
| 522 | VNInfo *VNI = LRQ.valueIn(); |
| 523 | // For Subranges it is possible that only undef values are left in that |
| 524 | // part of the subregister, so there is no real liverange at the use |
| 525 | if (!VNI) |
| 526 | continue; |
| 527 | |
| 528 | // Special case: An early-clobber tied operand reads and writes the |
| 529 | // register one slot early. |
| 530 | if (VNInfo *DefVNI = LRQ.valueDefined()) |
| 531 | Idx = DefVNI->def; |
| 532 | |
| 533 | WorkList.push_back(std::make_pair(Idx, VNI)); |
| 534 | } |
| 535 | |
| 536 | // Create a new live ranges with only minimal live segments per def. |
| 537 | LiveRange NewLR; |
| 538 | createSegmentsForValues(NewLR, make_range(SR.vni_begin(), SR.vni_end())); |
| 539 | extendSegmentsToUses(NewLR, *Indexes, WorkList, SR); |
| 540 | |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 541 | // Move the trimmed ranges back. |
| 542 | SR.segments.swap(NewLR.segments); |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 543 | |
| 544 | // Remove dead PHI value numbers |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 545 | for (VNInfo *VNI : SR.valnos) { |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 546 | if (VNI->isUnused()) |
| 547 | continue; |
| 548 | const LiveRange::Segment *Segment = SR.getSegmentContaining(VNI->def); |
| 549 | assert(Segment != nullptr && "Missing segment for VNI"); |
| 550 | if (Segment->end != VNI->def.getDeadSlot()) |
| 551 | continue; |
| 552 | if (VNI->isPHIDef()) { |
| 553 | // This is a dead PHI. Remove it. |
Krzysztof Parzyszek | 98c0f48 | 2016-07-12 17:55:28 +0000 | [diff] [blame] | 554 | DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n"); |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 555 | VNI->markUnused(); |
| 556 | SR.removeSegment(*Segment); |
Matthias Braun | 15abf37 | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Matthias Braun | 20e1f38 | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 560 | DEBUG(dbgs() << "Shrunk: " << SR << '\n'); |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 563 | void LiveIntervals::extendToIndices(LiveRange &LR, |
Krzysztof Parzyszek | 4f863d7 | 2016-09-01 12:10:36 +0000 | [diff] [blame] | 564 | ArrayRef<SlotIndex> Indices, |
| 565 | ArrayRef<SlotIndex> Undefs) { |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 566 | assert(LRCalc && "LRCalc not initialized."); |
| 567 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 568 | for (SlotIndex Idx : Indices) |
| 569 | LRCalc->extend(LR, Idx, /*PhysReg=*/0, Undefs); |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 572 | void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill, |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 573 | SmallVectorImpl<SlotIndex> *EndPoints) { |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 574 | LiveQueryResult LRQ = LR.Query(Kill); |
| 575 | VNInfo *VNI = LRQ.valueOutOrDead(); |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 576 | if (!VNI) |
| 577 | return; |
| 578 | |
| 579 | MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill); |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 580 | SlotIndex MBBEnd = Indexes->getMBBEndIdx(KillMBB); |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 581 | |
| 582 | // If VNI isn't live out from KillMBB, the value is trivially pruned. |
| 583 | if (LRQ.endPoint() < MBBEnd) { |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 584 | LR.removeSegment(Kill, LRQ.endPoint()); |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 585 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | // VNI is live out of KillMBB. |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 590 | LR.removeSegment(Kill, MBBEnd); |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 591 | if (EndPoints) EndPoints->push_back(MBBEnd); |
| 592 | |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 593 | // Find all blocks that are reachable from KillMBB without leaving VNI's live |
| 594 | // range. It is possible that KillMBB itself is reachable, so start a DFS |
| 595 | // from each successor. |
David Callahan | c1051ab | 2016-10-05 21:36:16 +0000 | [diff] [blame] | 596 | typedef df_iterator_default_set<MachineBasicBlock*,9> VisitedTy; |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 597 | VisitedTy Visited; |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 598 | for (MachineBasicBlock *Succ : KillMBB->successors()) { |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 599 | for (df_ext_iterator<MachineBasicBlock*, VisitedTy> |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 600 | I = df_ext_begin(Succ, Visited), E = df_ext_end(Succ, Visited); |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 601 | I != E;) { |
| 602 | MachineBasicBlock *MBB = *I; |
| 603 | |
| 604 | // Check if VNI is live in to MBB. |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 605 | SlotIndex MBBStart, MBBEnd; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 606 | std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB); |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 607 | LiveQueryResult LRQ = LR.Query(MBBStart); |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 608 | if (LRQ.valueIn() != VNI) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 609 | // This block isn't part of the VNI segment. Prune the search. |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 610 | I.skipChildren(); |
| 611 | continue; |
| 612 | } |
| 613 | |
| 614 | // Prune the search if VNI is killed in MBB. |
| 615 | if (LRQ.endPoint() < MBBEnd) { |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 616 | LR.removeSegment(MBBStart, LRQ.endPoint()); |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 617 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
| 618 | I.skipChildren(); |
| 619 | continue; |
| 620 | } |
| 621 | |
| 622 | // VNI is live through MBB. |
Matthias Braun | 8970d84 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 623 | LR.removeSegment(MBBStart, MBBEnd); |
Jakob Stoklund Olesen | 2f6dfc7 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 624 | if (EndPoints) EndPoints->push_back(MBBEnd); |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 625 | ++I; |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 626 | } |
Jakob Stoklund Olesen | 0bb3dd7 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
Jakob Stoklund Olesen | 55fc1d0 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 629 | |
Evan Cheng | be51f28 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 630 | //===----------------------------------------------------------------------===// |
| 631 | // Register allocator hooks. |
| 632 | // |
| 633 | |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 634 | void LiveIntervals::addKillFlags(const VirtRegMap *VRM) { |
| 635 | // Keep track of regunit ranges. |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 636 | SmallVector<std::pair<const LiveRange*, LiveRange::const_iterator>, 8> RU; |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 637 | // Keep track of subregister ranges. |
| 638 | SmallVector<std::pair<const LiveInterval::SubRange*, |
| 639 | LiveRange::const_iterator>, 4> SRs; |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 640 | |
Jakob Stoklund Olesen | 781e0b9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 641 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 642 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 11fb248 | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 643 | if (MRI->reg_nodbg_empty(Reg)) |
Jakob Stoklund Olesen | f2b16dc | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 644 | continue; |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 645 | const LiveInterval &LI = getInterval(Reg); |
| 646 | if (LI.empty()) |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 647 | continue; |
| 648 | |
| 649 | // Find the regunit intervals for the assigned register. They may overlap |
| 650 | // the virtual register live range, cancelling any kills. |
| 651 | RU.clear(); |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 652 | for (MCRegUnitIterator Unit(VRM->getPhys(Reg), TRI); Unit.isValid(); |
| 653 | ++Unit) { |
| 654 | const LiveRange &RURange = getRegUnit(*Unit); |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 655 | if (RURange.empty()) |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 656 | continue; |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 657 | RU.push_back(std::make_pair(&RURange, RURange.find(LI.begin()->end))); |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 658 | } |
Jakob Stoklund Olesen | f2b16dc | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 659 | |
Matthias Braun | a25e13a | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 660 | if (MRI->subRegLivenessEnabled()) { |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 661 | SRs.clear(); |
| 662 | for (const LiveInterval::SubRange &SR : LI.subranges()) { |
| 663 | SRs.push_back(std::make_pair(&SR, SR.find(LI.begin()->end))); |
| 664 | } |
| 665 | } |
| 666 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 667 | // Every instruction that kills Reg corresponds to a segment range end |
| 668 | // point. |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 669 | for (LiveInterval::const_iterator RI = LI.begin(), RE = LI.end(); RI != RE; |
Jakob Stoklund Olesen | f2b16dc | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 670 | ++RI) { |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 671 | // A block index indicates an MBB edge. |
| 672 | if (RI->end.isBlock()) |
Jakob Stoklund Olesen | f2b16dc | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 673 | continue; |
| 674 | MachineInstr *MI = getInstructionFromIndex(RI->end); |
| 675 | if (!MI) |
| 676 | continue; |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 677 | |
Matthias Braun | c9d5c0f | 2013-10-04 16:52:58 +0000 | [diff] [blame] | 678 | // Check if any of the regunits are live beyond the end of RI. That could |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 679 | // happen when a physreg is defined as a copy of a virtreg: |
| 680 | // |
| 681 | // %EAX = COPY %vreg5 |
| 682 | // FOO %vreg5 <--- MI, cancel kill because %EAX is live. |
| 683 | // BAR %EAX<kill> |
| 684 | // |
| 685 | // There should be no kill flag on FOO when %vreg5 is rewritten as %EAX. |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 686 | for (auto &RUP : RU) { |
| 687 | const LiveRange &RURange = *RUP.first; |
Matthias Braun | f603c88 | 2014-12-24 02:11:43 +0000 | [diff] [blame] | 688 | LiveRange::const_iterator &I = RUP.second; |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 689 | if (I == RURange.end()) |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 690 | continue; |
Matthias Braun | 7f8dece | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 691 | I = RURange.advanceTo(I, RI->end); |
| 692 | if (I == RURange.end() || I->start >= RI->end) |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 693 | continue; |
| 694 | // I is overlapping RI. |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 695 | goto CancelKill; |
Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 696 | } |
Matthias Braun | d70caaf | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 697 | |
Matthias Braun | a25e13a | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 698 | if (MRI->subRegLivenessEnabled()) { |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 699 | // When reading a partial undefined value we must not add a kill flag. |
| 700 | // The regalloc might have used the undef lane for something else. |
| 701 | // Example: |
| 702 | // %vreg1 = ... ; R32: %vreg1 |
| 703 | // %vreg2:high16 = ... ; R64: %vreg2 |
| 704 | // = read %vreg2<kill> ; R64: %vreg2 |
| 705 | // = read %vreg1 ; R32: %vreg1 |
| 706 | // The <kill> flag is correct for %vreg2, but the register allocator may |
| 707 | // assign R0L to %vreg1, and R0 to %vreg2 because the low 32bits of R0 |
| 708 | // are actually never written by %vreg2. After assignment the <kill> |
| 709 | // flag at the read instruction is invalid. |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 710 | LaneBitmask DefinedLanesMask; |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 711 | if (!SRs.empty()) { |
| 712 | // Compute a mask of lanes that are defined. |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 713 | DefinedLanesMask = LaneBitmask::getNone(); |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 714 | for (auto &SRP : SRs) { |
| 715 | const LiveInterval::SubRange &SR = *SRP.first; |
Matthias Braun | f603c88 | 2014-12-24 02:11:43 +0000 | [diff] [blame] | 716 | LiveRange::const_iterator &I = SRP.second; |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 717 | if (I == SR.end()) |
| 718 | continue; |
| 719 | I = SR.advanceTo(I, RI->end); |
| 720 | if (I == SR.end() || I->start >= RI->end) |
| 721 | continue; |
| 722 | // I is overlapping RI |
| 723 | DefinedLanesMask |= SR.LaneMask; |
Matthias Braun | d70caaf | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 724 | } |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 725 | } else |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 726 | DefinedLanesMask = LaneBitmask::getAll(); |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 727 | |
| 728 | bool IsFullWrite = false; |
| 729 | for (const MachineOperand &MO : MI->operands()) { |
| 730 | if (!MO.isReg() || MO.getReg() != Reg) |
| 731 | continue; |
| 732 | if (MO.isUse()) { |
| 733 | // Reading any undefined lanes? |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 734 | LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg()); |
Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 735 | if ((UseMask & ~DefinedLanesMask).any()) |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 736 | goto CancelKill; |
| 737 | } else if (MO.getSubReg() == 0) { |
| 738 | // Writing to the full register? |
| 739 | assert(MO.isDef()); |
| 740 | IsFullWrite = true; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | // If an instruction writes to a subregister, a new segment starts in |
| 745 | // the LiveInterval. But as this is only overriding part of the register |
| 746 | // adding kill-flags is not correct here after registers have been |
| 747 | // assigned. |
| 748 | if (!IsFullWrite) { |
| 749 | // Next segment has to be adjacent in the subregister write case. |
| 750 | LiveRange::const_iterator N = std::next(RI); |
| 751 | if (N != LI.end() && N->start == RI->end) |
| 752 | goto CancelKill; |
Matthias Braun | d70caaf | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Matthias Braun | 714c494 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 756 | MI->addRegisterKilled(Reg, nullptr); |
| 757 | continue; |
| 758 | CancelKill: |
| 759 | MI->clearRegisterKills(Reg, nullptr); |
Jakob Stoklund Olesen | f2b16dc | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
Jakob Stoklund Olesen | aa06de2 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 764 | MachineBasicBlock* |
| 765 | LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const { |
| 766 | // A local live range must be fully contained inside the block, meaning it is |
| 767 | // defined and killed at instructions, not at block boundaries. It is not |
| 768 | // live in or or out of any block. |
| 769 | // |
| 770 | // It is technically possible to have a PHI-defined live range identical to a |
| 771 | // single block, but we are going to return false in that case. |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 772 | |
Jakob Stoklund Olesen | aa06de2 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 773 | SlotIndex Start = LI.beginIndex(); |
| 774 | if (Start.isBlock()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 775 | return nullptr; |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 776 | |
Jakob Stoklund Olesen | aa06de2 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 777 | SlotIndex Stop = LI.endIndex(); |
| 778 | if (Stop.isBlock()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 779 | return nullptr; |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 780 | |
Jakob Stoklund Olesen | aa06de2 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 781 | // getMBBFromIndex doesn't need to search the MBB table when both indexes |
| 782 | // belong to proper instructions. |
Jakob Stoklund Olesen | 11fb248 | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 783 | MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start); |
| 784 | MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 785 | return MBB1 == MBB2 ? MBB1 : nullptr; |
Evan Cheng | 8e22379 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Jakob Stoklund Olesen | 06d6a53 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 788 | bool |
| 789 | LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const { |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 790 | for (const VNInfo *PHI : LI.valnos) { |
Jakob Stoklund Olesen | 06d6a53 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 791 | if (PHI->isUnused() || !PHI->isPHIDef()) |
| 792 | continue; |
| 793 | const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def); |
| 794 | // Conservatively return true instead of scanning huge predecessor lists. |
| 795 | if (PHIMBB->pred_size() > 100) |
| 796 | return true; |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 797 | for (const MachineBasicBlock *Pred : PHIMBB->predecessors()) |
| 798 | if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(Pred))) |
Jakob Stoklund Olesen | 06d6a53 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 799 | return true; |
| 800 | } |
| 801 | return false; |
| 802 | } |
| 803 | |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 804 | float LiveIntervals::getSpillWeight(bool isDef, bool isUse, |
| 805 | const MachineBlockFrequencyInfo *MBFI, |
| 806 | const MachineInstr &MI) { |
| 807 | BlockFrequency Freq = MBFI->getBlockFreq(MI.getParent()); |
Michael Gottesman | 5e985ee | 2013-12-14 02:37:38 +0000 | [diff] [blame] | 808 | const float Scale = 1.0f / MBFI->getEntryFreq(); |
Michael Gottesman | 9f49d74 | 2013-12-14 00:53:32 +0000 | [diff] [blame] | 809 | return (isDef + isUse) * (Freq.getFrequency() * Scale); |
Jakob Stoklund Olesen | 115da88 | 2010-03-01 20:59:38 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 812 | LiveRange::Segment |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 813 | LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr &startInst) { |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 814 | LiveInterval& Interval = createEmptyInterval(reg); |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 815 | VNInfo *VN = Interval.getNextValue( |
| 816 | SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
| 817 | getVNInfoAllocator()); |
| 818 | LiveRange::Segment S(SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
| 819 | getMBBEndIdx(startInst.getParent()), VN); |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 820 | Interval.addSegment(S); |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 821 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 822 | return S; |
Owen Anderson | 35e2dfe | 2008-06-05 17:15:43 +0000 | [diff] [blame] | 823 | } |
Jakob Stoklund Olesen | 3ff74d8 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 824 | |
| 825 | |
| 826 | //===----------------------------------------------------------------------===// |
| 827 | // Register mask functions |
| 828 | //===----------------------------------------------------------------------===// |
| 829 | |
| 830 | bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI, |
| 831 | BitVector &UsableRegs) { |
| 832 | if (LI.empty()) |
| 833 | return false; |
Jakob Stoklund Olesen | 9ef50bd | 2012-02-10 01:31:31 +0000 | [diff] [blame] | 834 | LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end(); |
| 835 | |
| 836 | // Use a smaller arrays for local live ranges. |
| 837 | ArrayRef<SlotIndex> Slots; |
| 838 | ArrayRef<const uint32_t*> Bits; |
| 839 | if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) { |
| 840 | Slots = getRegMaskSlotsInBlock(MBB->getNumber()); |
| 841 | Bits = getRegMaskBitsInBlock(MBB->getNumber()); |
| 842 | } else { |
| 843 | Slots = getRegMaskSlots(); |
| 844 | Bits = getRegMaskBits(); |
| 845 | } |
Jakob Stoklund Olesen | 3ff74d8 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 846 | |
| 847 | // We are going to enumerate all the register mask slots contained in LI. |
| 848 | // Start with a binary search of RegMaskSlots to find a starting point. |
Jakob Stoklund Olesen | 3ff74d8 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 849 | ArrayRef<SlotIndex>::iterator SlotI = |
| 850 | std::lower_bound(Slots.begin(), Slots.end(), LiveI->start); |
| 851 | ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); |
| 852 | |
| 853 | // No slots in range, LI begins after the last call. |
| 854 | if (SlotI == SlotE) |
| 855 | return false; |
| 856 | |
| 857 | bool Found = false; |
| 858 | for (;;) { |
| 859 | assert(*SlotI >= LiveI->start); |
| 860 | // Loop over all slots overlapping this segment. |
| 861 | while (*SlotI < LiveI->end) { |
| 862 | // *SlotI overlaps LI. Collect mask bits. |
| 863 | if (!Found) { |
| 864 | // This is the first overlap. Initialize UsableRegs to all ones. |
| 865 | UsableRegs.clear(); |
Jakob Stoklund Olesen | 11fb248 | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 866 | UsableRegs.resize(TRI->getNumRegs(), true); |
Jakob Stoklund Olesen | 3ff74d8 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 867 | Found = true; |
| 868 | } |
| 869 | // Remove usable registers clobbered by this mask. |
Jakob Stoklund Olesen | 9ef50bd | 2012-02-10 01:31:31 +0000 | [diff] [blame] | 870 | UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]); |
Jakob Stoklund Olesen | 3ff74d8 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 871 | if (++SlotI == SlotE) |
| 872 | return Found; |
| 873 | } |
| 874 | // *SlotI is beyond the current LI segment. |
| 875 | LiveI = LI.advanceTo(LiveI, *SlotI); |
| 876 | if (LiveI == LiveE) |
| 877 | return Found; |
| 878 | // Advance SlotI until it overlaps. |
| 879 | while (*SlotI < LiveI->start) |
| 880 | if (++SlotI == SlotE) |
| 881 | return Found; |
| 882 | } |
| 883 | } |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 884 | |
| 885 | //===----------------------------------------------------------------------===// |
| 886 | // IntervalUpdate class. |
| 887 | //===----------------------------------------------------------------------===// |
| 888 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 889 | /// Toolkit used by handleMove to trim or extend live intervals. |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 890 | class LiveIntervals::HMEditor { |
| 891 | private: |
Lang Hames | 5976198 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 892 | LiveIntervals& LIS; |
| 893 | const MachineRegisterInfo& MRI; |
| 894 | const TargetRegisterInfo& TRI; |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 895 | SlotIndex OldIdx; |
Lang Hames | 5976198 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 896 | SlotIndex NewIdx; |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 897 | SmallPtrSet<LiveRange*, 8> Updated; |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 898 | bool UpdateFlags; |
Lang Hames | 13b1152 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 899 | |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 900 | public: |
Lang Hames | 5976198 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 901 | HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI, |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 902 | const TargetRegisterInfo& TRI, |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 903 | SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags) |
| 904 | : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx), |
| 905 | UpdateFlags(UpdateFlags) {} |
| 906 | |
| 907 | // FIXME: UpdateFlags is a workaround that creates live intervals for all |
| 908 | // physregs, even those that aren't needed for regalloc, in order to update |
| 909 | // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill |
| 910 | // flags, and postRA passes will use a live register utility instead. |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 911 | LiveRange *getRegUnitLI(unsigned Unit) { |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 912 | if (UpdateFlags) |
| 913 | return &LIS.getRegUnit(Unit); |
| 914 | return LIS.getCachedRegUnit(Unit); |
| 915 | } |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 916 | |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 917 | /// Update all live ranges touched by MI, assuming a move from OldIdx to |
| 918 | /// NewIdx. |
| 919 | void updateAllRanges(MachineInstr *MI) { |
| 920 | DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI); |
| 921 | bool hasRegMask = false; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 922 | for (MachineOperand &MO : MI->operands()) { |
| 923 | if (MO.isRegMask()) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 924 | hasRegMask = true; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 925 | if (!MO.isReg()) |
Lang Hames | d6e765c | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 926 | continue; |
Matthias Braun | 71474e8 | 2016-05-06 21:47:41 +0000 | [diff] [blame] | 927 | if (MO.isUse()) { |
| 928 | if (!MO.readsReg()) |
| 929 | continue; |
| 930 | // Aggressively clear all kill flags. |
| 931 | // They are reinserted by VirtRegRewriter. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 932 | MO.setIsKill(false); |
Matthias Braun | 71474e8 | 2016-05-06 21:47:41 +0000 | [diff] [blame] | 933 | } |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 934 | |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 935 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 936 | if (!Reg) |
| 937 | continue; |
| 938 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 939 | LiveInterval &LI = LIS.getInterval(Reg); |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 940 | if (LI.hasSubRanges()) { |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 941 | unsigned SubReg = MO.getSubReg(); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 942 | LaneBitmask LaneMask = SubReg ? TRI.getSubRegIndexLaneMask(SubReg) |
| 943 | : MRI.getMaxLaneMaskForVReg(Reg); |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 944 | for (LiveInterval::SubRange &S : LI.subranges()) { |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 945 | if ((S.LaneMask & LaneMask).none()) |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 946 | continue; |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 947 | updateRange(S, Reg, S.LaneMask); |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 950 | updateRange(LI, Reg, LaneBitmask::getNone()); |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 951 | continue; |
| 952 | } |
| 953 | |
| 954 | // For physregs, only update the regunits that actually have a |
| 955 | // precomputed live range. |
| 956 | for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 957 | if (LiveRange *LR = getRegUnitLI(*Units)) |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 958 | updateRange(*LR, *Units, LaneBitmask::getNone()); |
Lang Hames | d6e765c | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 959 | } |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 960 | if (hasRegMask) |
| 961 | updateRegMaskSlots(); |
Lang Hames | 13b1152 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Lang Hames | 4645a72 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 964 | private: |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 965 | /// Update a single live range, assuming an instruction has been moved from |
| 966 | /// OldIdx to NewIdx. |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 967 | void updateRange(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 968 | if (!Updated.insert(&LR).second) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 969 | return; |
| 970 | DEBUG({ |
| 971 | dbgs() << " "; |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 972 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 973 | dbgs() << PrintReg(Reg); |
Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 974 | if (LaneMask.any()) |
Matthias Braun | c804cdb | 2015-09-25 21:51:24 +0000 | [diff] [blame] | 975 | dbgs() << " L" << PrintLaneMask(LaneMask); |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 976 | } else { |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 977 | dbgs() << PrintRegUnit(Reg, &TRI); |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 978 | } |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 979 | dbgs() << ":\t" << LR << '\n'; |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 980 | }); |
| 981 | if (SlotIndex::isEarlierInstr(OldIdx, NewIdx)) |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 982 | handleMoveDown(LR); |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 983 | else |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 984 | handleMoveUp(LR, Reg, LaneMask); |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 985 | DEBUG(dbgs() << " -->\t" << LR << '\n'); |
| 986 | LR.verify(); |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 989 | /// Update LR to reflect an instruction has been moved downwards from OldIdx |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 990 | /// to NewIdx (OldIdx < NewIdx). |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 991 | void handleMoveDown(LiveRange &LR) { |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 992 | LiveRange::iterator E = LR.end(); |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 993 | // Segment going into OldIdx. |
| 994 | LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); |
| 995 | |
| 996 | // No value live before or after OldIdx? Nothing to do. |
| 997 | if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 998 | return; |
Lang Hames | 13b1152 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 999 | |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1000 | LiveRange::iterator OldIdxOut; |
| 1001 | // Do we have a value live-in to OldIdx? |
| 1002 | if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1003 | // If the live-in value already extends to NewIdx, there is nothing to do. |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1004 | if (SlotIndex::isEarlierEqualInstr(NewIdx, OldIdxIn->end)) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1005 | return; |
| 1006 | // Aggressively remove all kill flags from the old kill point. |
| 1007 | // Kill flags shouldn't be used while live intervals exist, they will be |
| 1008 | // reinserted by VirtRegRewriter. |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1009 | if (MachineInstr *KillMI = LIS.getInstructionFromIndex(OldIdxIn->end)) |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1010 | for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1011 | if (MO->isReg() && MO->isUse()) |
| 1012 | MO->setIsKill(false); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Is there a def before NewIdx which is not OldIdx? |
| 1015 | LiveRange::iterator Next = std::next(OldIdxIn); |
| 1016 | if (Next != E && !SlotIndex::isSameInstr(OldIdx, Next->start) && |
| 1017 | SlotIndex::isEarlierInstr(Next->start, NewIdx)) { |
| 1018 | // If we are here then OldIdx was just a use but not a def. We only have |
| 1019 | // to ensure liveness extends to NewIdx. |
| 1020 | LiveRange::iterator NewIdxIn = |
| 1021 | LR.advanceTo(Next, NewIdx.getBaseIndex()); |
| 1022 | // Extend the segment before NewIdx if necessary. |
| 1023 | if (NewIdxIn == E || |
| 1024 | !SlotIndex::isEarlierInstr(NewIdxIn->start, NewIdx)) { |
| 1025 | LiveRange::iterator Prev = std::prev(NewIdxIn); |
| 1026 | Prev->end = NewIdx.getRegSlot(); |
| 1027 | } |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 1028 | // Extend OldIdxIn. |
| 1029 | OldIdxIn->end = Next->start; |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1030 | return; |
| 1031 | } |
| 1032 | |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1033 | // Adjust OldIdxIn->end to reach NewIdx. This may temporarily make LR |
Matthias Braun | db32077 | 2016-01-26 01:40:48 +0000 | [diff] [blame] | 1034 | // invalid by overlapping ranges. |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1035 | bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); |
| 1036 | OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber()); |
| 1037 | // If this was not a kill, then there was no def and we're done. |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1038 | if (!isKill) |
| 1039 | return; |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1040 | |
| 1041 | // Did we have a Def at OldIdx? |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1042 | OldIdxOut = Next; |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1043 | if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) |
| 1044 | return; |
| 1045 | } else { |
| 1046 | OldIdxOut = OldIdxIn; |
Lang Hames | 13b1152 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1049 | // If we are here then there is a Definition at OldIdx. OldIdxOut points |
| 1050 | // to the segment starting there. |
| 1051 | assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && |
| 1052 | "No def?"); |
| 1053 | VNInfo *OldIdxVNI = OldIdxOut->valno; |
| 1054 | assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); |
| 1055 | |
| 1056 | // If the defined value extends beyond NewIdx, just move the beginning |
| 1057 | // of the segment to NewIdx. |
| 1058 | SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); |
| 1059 | if (SlotIndex::isEarlierInstr(NewIdxDef, OldIdxOut->end)) { |
| 1060 | OldIdxVNI->def = NewIdxDef; |
| 1061 | OldIdxOut->start = OldIdxVNI->def; |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | } |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1064 | |
| 1065 | // If we are here then we have a Definition at OldIdx which ends before |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1066 | // NewIdx. |
| 1067 | |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1068 | // Is there an existing Def at NewIdx? |
| 1069 | LiveRange::iterator AfterNewIdx |
| 1070 | = LR.advanceTo(OldIdxOut, NewIdx.getRegSlot()); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1071 | bool OldIdxDefIsDead = OldIdxOut->end.isDead(); |
| 1072 | if (!OldIdxDefIsDead && |
| 1073 | SlotIndex::isEarlierInstr(OldIdxOut->end, NewIdxDef)) { |
| 1074 | // OldIdx is not a dead def, and NewIdxDef is inside a new interval. |
| 1075 | VNInfo *DefVNI; |
| 1076 | if (OldIdxOut != LR.begin() && |
| 1077 | !SlotIndex::isEarlierInstr(std::prev(OldIdxOut)->end, |
| 1078 | OldIdxOut->start)) { |
| 1079 | // There is no gap between OldIdxOut and its predecessor anymore, |
| 1080 | // merge them. |
| 1081 | LiveRange::iterator IPrev = std::prev(OldIdxOut); |
| 1082 | DefVNI = OldIdxVNI; |
| 1083 | IPrev->end = OldIdxOut->end; |
| 1084 | } else { |
| 1085 | // The value is live in to OldIdx |
| 1086 | LiveRange::iterator INext = std::next(OldIdxOut); |
| 1087 | assert(INext != E && "Must have following segment"); |
| 1088 | // We merge OldIdxOut and its successor. As we're dealing with subreg |
| 1089 | // reordering, there is always a successor to OldIdxOut in the same BB |
| 1090 | // We don't need INext->valno anymore and will reuse for the new segment |
| 1091 | // we create later. |
Matthias Braun | c9e759a | 2016-04-28 02:11:49 +0000 | [diff] [blame] | 1092 | DefVNI = OldIdxVNI; |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1093 | INext->start = OldIdxOut->end; |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1094 | INext->valno->def = INext->start; |
| 1095 | } |
| 1096 | // If NewIdx is behind the last segment, extend that and append a new one. |
| 1097 | if (AfterNewIdx == E) { |
| 1098 | // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up |
| 1099 | // one position. |
| 1100 | // |- ?/OldIdxOut -| |- X0 -| ... |- Xn -| end |
| 1101 | // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS -| end |
| 1102 | std::copy(std::next(OldIdxOut), E, OldIdxOut); |
| 1103 | // The last segment is undefined now, reuse it for a dead def. |
| 1104 | LiveRange::iterator NewSegment = std::prev(E); |
| 1105 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
| 1106 | DefVNI); |
| 1107 | DefVNI->def = NewIdxDef; |
| 1108 | |
| 1109 | LiveRange::iterator Prev = std::prev(NewSegment); |
| 1110 | Prev->end = NewIdxDef; |
| 1111 | } else { |
| 1112 | // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up |
| 1113 | // one position. |
| 1114 | // |- ?/OldIdxOut -| |- X0 -| ... |- Xn/AfterNewIdx -| |- Next -| |
| 1115 | // => |- X0/OldIdxOut -| ... |- Xn -| |- Xn/AfterNewIdx -| |- Next -| |
| 1116 | std::copy(std::next(OldIdxOut), std::next(AfterNewIdx), OldIdxOut); |
| 1117 | LiveRange::iterator Prev = std::prev(AfterNewIdx); |
| 1118 | // We have two cases: |
| 1119 | if (SlotIndex::isEarlierInstr(Prev->start, NewIdxDef)) { |
| 1120 | // Case 1: NewIdx is inside a liverange. Split this liverange at |
| 1121 | // NewIdxDef into the segment "Prev" followed by "NewSegment". |
| 1122 | LiveRange::iterator NewSegment = AfterNewIdx; |
| 1123 | *NewSegment = LiveRange::Segment(NewIdxDef, Prev->end, Prev->valno); |
| 1124 | Prev->valno->def = NewIdxDef; |
| 1125 | |
| 1126 | *Prev = LiveRange::Segment(Prev->start, NewIdxDef, DefVNI); |
| 1127 | DefVNI->def = Prev->start; |
| 1128 | } else { |
| 1129 | // Case 2: NewIdx is in a lifetime hole. Keep AfterNewIdx as is and |
| 1130 | // turn Prev into a segment from NewIdx to AfterNewIdx->start. |
| 1131 | *Prev = LiveRange::Segment(NewIdxDef, AfterNewIdx->start, DefVNI); |
| 1132 | DefVNI->def = NewIdxDef; |
| 1133 | assert(DefVNI != AfterNewIdx->valno); |
| 1134 | } |
| 1135 | } |
| 1136 | return; |
| 1137 | } |
| 1138 | |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1139 | if (AfterNewIdx != E && |
| 1140 | SlotIndex::isSameInstr(AfterNewIdx->start, NewIdxDef)) { |
| 1141 | // There is an existing def at NewIdx. The def at OldIdx is coalesced into |
| 1142 | // that value. |
| 1143 | assert(AfterNewIdx->valno != OldIdxVNI && "Multiple defs of value?"); |
| 1144 | LR.removeValNo(OldIdxVNI); |
| 1145 | } else { |
| 1146 | // There was no existing def at NewIdx. We need to create a dead def |
| 1147 | // at NewIdx. Shift segments over the old OldIdxOut segment, this frees |
| 1148 | // a new segment at the place where we want to construct the dead def. |
| 1149 | // |- OldIdxOut -| |- X0 -| ... |- Xn -| |- AfterNewIdx -| |
| 1150 | // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS. -| |- AfterNewIdx -| |
| 1151 | assert(AfterNewIdx != OldIdxOut && "Inconsistent iterators"); |
| 1152 | std::copy(std::next(OldIdxOut), AfterNewIdx, OldIdxOut); |
| 1153 | // We can reuse OldIdxVNI now. |
| 1154 | LiveRange::iterator NewSegment = std::prev(AfterNewIdx); |
| 1155 | VNInfo *NewSegmentVNI = OldIdxVNI; |
| 1156 | NewSegmentVNI->def = NewIdxDef; |
| 1157 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
| 1158 | NewSegmentVNI); |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1159 | } |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1162 | /// Update LR to reflect an instruction has been moved upwards from OldIdx |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1163 | /// to NewIdx (NewIdx < OldIdx). |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1164 | void handleMoveUp(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) { |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1165 | LiveRange::iterator E = LR.end(); |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1166 | // Segment going into OldIdx. |
| 1167 | LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); |
| 1168 | |
| 1169 | // No value live before or after OldIdx? Nothing to do. |
| 1170 | if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1171 | return; |
| 1172 | |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1173 | LiveRange::iterator OldIdxOut; |
| 1174 | // Do we have a value live-in to OldIdx? |
| 1175 | if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { |
| 1176 | // If the live-in value isn't killed here, then we have no Def at |
| 1177 | // OldIdx, moreover the value must be live at NewIdx so there is nothing |
| 1178 | // to do. |
| 1179 | bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); |
| 1180 | if (!isKill) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1181 | return; |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1182 | |
| 1183 | // At this point we have to move OldIdxIn->end back to the nearest |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1184 | // previous use or (dead-)def but no further than NewIdx. |
| 1185 | SlotIndex DefBeforeOldIdx |
| 1186 | = std::max(OldIdxIn->start.getDeadSlot(), |
| 1187 | NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber())); |
| 1188 | OldIdxIn->end = findLastUseBefore(DefBeforeOldIdx, Reg, LaneMask); |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1189 | |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1190 | // Did we have a Def at OldIdx? If not we are done now. |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1191 | OldIdxOut = std::next(OldIdxIn); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1192 | if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1193 | return; |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1194 | } else { |
| 1195 | OldIdxOut = OldIdxIn; |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1196 | OldIdxIn = OldIdxOut != LR.begin() ? std::prev(OldIdxOut) : E; |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // If we are here then there is a Definition at OldIdx. OldIdxOut points |
| 1200 | // to the segment starting there. |
| 1201 | assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && |
| 1202 | "No def?"); |
| 1203 | VNInfo *OldIdxVNI = OldIdxOut->valno; |
| 1204 | assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); |
| 1205 | bool OldIdxDefIsDead = OldIdxOut->end.isDead(); |
| 1206 | |
| 1207 | // Is there an existing def at NewIdx? |
| 1208 | SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); |
| 1209 | LiveRange::iterator NewIdxOut = LR.find(NewIdx.getRegSlot()); |
| 1210 | if (SlotIndex::isSameInstr(NewIdxOut->start, NewIdx)) { |
| 1211 | assert(NewIdxOut->valno != OldIdxVNI && |
| 1212 | "Same value defined more than once?"); |
| 1213 | // If OldIdx was a dead def remove it. |
| 1214 | if (!OldIdxDefIsDead) { |
Matthias Braun | db32077 | 2016-01-26 01:40:48 +0000 | [diff] [blame] | 1215 | // Remove segment starting at NewIdx and move begin of OldIdxOut to |
| 1216 | // NewIdx so it can take its place. |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1217 | OldIdxVNI->def = NewIdxDef; |
| 1218 | OldIdxOut->start = NewIdxDef; |
| 1219 | LR.removeValNo(NewIdxOut->valno); |
| 1220 | } else { |
Matthias Braun | db32077 | 2016-01-26 01:40:48 +0000 | [diff] [blame] | 1221 | // Simply remove the dead def at OldIdx. |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1222 | LR.removeValNo(OldIdxVNI); |
| 1223 | } |
| 1224 | } else { |
| 1225 | // Previously nothing was live after NewIdx, so all we have to do now is |
| 1226 | // move the begin of OldIdxOut to NewIdx. |
| 1227 | if (!OldIdxDefIsDead) { |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1228 | // Do we have any intermediate Defs between OldIdx and NewIdx? |
| 1229 | if (OldIdxIn != E && |
| 1230 | SlotIndex::isEarlierInstr(NewIdxDef, OldIdxIn->start)) { |
| 1231 | // OldIdx is not a dead def and NewIdx is before predecessor start. |
| 1232 | LiveRange::iterator NewIdxIn = NewIdxOut; |
| 1233 | assert(NewIdxIn == LR.find(NewIdx.getBaseIndex())); |
| 1234 | const SlotIndex SplitPos = NewIdxDef; |
| 1235 | |
| 1236 | // Merge the OldIdxIn and OldIdxOut segments into OldIdxOut. |
| 1237 | *OldIdxOut = LiveRange::Segment(OldIdxIn->start, OldIdxOut->end, |
| 1238 | OldIdxIn->valno); |
| 1239 | // OldIdxIn and OldIdxVNI are now undef and can be overridden. |
| 1240 | // We Slide [NewIdxIn, OldIdxIn) down one position. |
| 1241 | // |- X0/NewIdxIn -| ... |- Xn-1 -||- Xn/OldIdxIn -||- OldIdxOut -| |
| 1242 | // => |- undef/NexIdxIn -| |- X0 -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |
| 1243 | std::copy_backward(NewIdxIn, OldIdxIn, OldIdxOut); |
| 1244 | // NewIdxIn is now considered undef so we can reuse it for the moved |
| 1245 | // value. |
| 1246 | LiveRange::iterator NewSegment = NewIdxIn; |
| 1247 | LiveRange::iterator Next = std::next(NewSegment); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1248 | if (SlotIndex::isEarlierInstr(Next->start, NewIdx)) { |
| 1249 | // There is no gap between NewSegment and its predecessor. |
| 1250 | *NewSegment = LiveRange::Segment(Next->start, SplitPos, |
Matthias Braun | fc4c8a1 | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 1251 | Next->valno); |
| 1252 | *Next = LiveRange::Segment(SplitPos, Next->end, OldIdxVNI); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1253 | Next->valno->def = SplitPos; |
| 1254 | } else { |
| 1255 | // There is a gap between NewSegment and its predecessor |
| 1256 | // Value becomes live in. |
Matthias Braun | fc4c8a1 | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 1257 | *NewSegment = LiveRange::Segment(SplitPos, Next->start, OldIdxVNI); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1258 | NewSegment->valno->def = SplitPos; |
| 1259 | } |
| 1260 | } else { |
| 1261 | // Leave the end point of a live def. |
| 1262 | OldIdxOut->start = NewIdxDef; |
| 1263 | OldIdxVNI->def = NewIdxDef; |
| 1264 | if (OldIdxIn != E && SlotIndex::isEarlierInstr(NewIdx, OldIdxIn->end)) |
| 1265 | OldIdxIn->end = NewIdx.getRegSlot(); |
| 1266 | } |
Matthias Braun | 242b8bb | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | // OldIdxVNI is a dead def. It may have been moved across other values |
| 1269 | // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) |
| 1270 | // down one position. |
| 1271 | // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | |
| 1272 | // => |- undef/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| |
| 1273 | std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); |
| 1274 | // OldIdxVNI can be reused now to build a new dead def segment. |
| 1275 | LiveRange::iterator NewSegment = NewIdxOut; |
| 1276 | VNInfo *NewSegmentVNI = OldIdxVNI; |
| 1277 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
| 1278 | NewSegmentVNI); |
| 1279 | NewSegmentVNI->def = NewIdxDef; |
Lang Hames | 13b1152 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1280 | } |
| 1281 | } |
Lang Hames | 13b1152 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Jakob Stoklund Olesen | 1a87a29 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1284 | void updateRegMaskSlots() { |
Lang Hames | 5976198 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 1285 | SmallVectorImpl<SlotIndex>::iterator RI = |
| 1286 | std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(), |
| 1287 | OldIdx); |
Jakob Stoklund Olesen | 13d5562 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 1288 | assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() && |
| 1289 | "No RegMask at OldIdx."); |
| 1290 | *RI = NewIdx.getRegSlot(); |
| 1291 | assert((RI == LIS.RegMaskSlots.begin() || |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1292 | SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) && |
| 1293 | "Cannot move regmask instruction above another call"); |
| 1294 | assert((std::next(RI) == LIS.RegMaskSlots.end() || |
| 1295 | SlotIndex::isEarlierInstr(*RI, *std::next(RI))) && |
| 1296 | "Cannot move regmask instruction below another call"); |
Lang Hames | a9afc6a | 2012-02-17 21:29:41 +0000 | [diff] [blame] | 1297 | } |
Lang Hames | 4645a72 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1298 | |
| 1299 | // Return the last use of reg between NewIdx and OldIdx. |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1300 | SlotIndex findLastUseBefore(SlotIndex Before, unsigned Reg, |
| 1301 | LaneBitmask LaneMask) { |
Lang Hames | c3d9a3d | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 1302 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1303 | SlotIndex LastUse = Before; |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1304 | for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) { |
Matthias Braun | 959a8c9 | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 1305 | if (MO.isUndef()) |
| 1306 | continue; |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1307 | unsigned SubReg = MO.getSubReg(); |
Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1308 | if (SubReg != 0 && LaneMask.any() |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1309 | && (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask).none()) |
Matthias Braun | 7044d69 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1310 | continue; |
| 1311 | |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1312 | const MachineInstr &MI = *MO.getParent(); |
Lang Hames | c3d9a3d | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 1313 | SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); |
| 1314 | if (InstSlot > LastUse && InstSlot < OldIdx) |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1315 | LastUse = InstSlot.getRegSlot(); |
Lang Hames | c3d9a3d | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 1316 | } |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1317 | return LastUse; |
Lang Hames | 4645a72 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1318 | } |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1319 | |
| 1320 | // This is a regunit interval, so scanning the use list could be very |
| 1321 | // expensive. Scan upwards from OldIdx instead. |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1322 | assert(Before < OldIdx && "Expected upwards move"); |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1323 | SlotIndexes *Indexes = LIS.getSlotIndexes(); |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1324 | MachineBasicBlock *MBB = Indexes->getMBBFromIndex(Before); |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1325 | |
| 1326 | // OldIdx may not correspond to an instruction any longer, so set MII to |
| 1327 | // point to the next instruction after OldIdx, or MBB->end(). |
| 1328 | MachineBasicBlock::iterator MII = MBB->end(); |
| 1329 | if (MachineInstr *MI = Indexes->getInstructionFromIndex( |
| 1330 | Indexes->getNextNonNullIndex(OldIdx))) |
| 1331 | if (MI->getParent() == MBB) |
| 1332 | MII = MI; |
| 1333 | |
| 1334 | MachineBasicBlock::iterator Begin = MBB->begin(); |
| 1335 | while (MII != Begin) { |
| 1336 | if ((--MII)->isDebugValue()) |
| 1337 | continue; |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1338 | SlotIndex Idx = Indexes->getInstructionIndex(*MII); |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1339 | |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1340 | // Stop searching when Before is reached. |
| 1341 | if (!SlotIndex::isEarlierInstr(Before, Idx)) |
| 1342 | return Before; |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Check if MII uses Reg. |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1345 | for (MIBundleOperands MO(*MII); MO.isValid(); ++MO) |
Matthias Braun | 959a8c9 | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 1346 | if (MO->isReg() && !MO->isUndef() && |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1347 | TargetRegisterInfo::isPhysicalRegister(MO->getReg()) && |
| 1348 | TRI.hasRegUnit(MO->getReg(), Reg)) |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1349 | return Idx.getRegSlot(); |
Jakob Stoklund Olesen | 8d1aaf2 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1350 | } |
Matthias Braun | 4a6c728 | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1351 | // Didn't reach Before. It must be the first instruction in the block. |
| 1352 | return Before; |
Lang Hames | 4645a72 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1353 | } |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1354 | }; |
| 1355 | |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1356 | void LiveIntervals::handleMove(MachineInstr &MI, bool UpdateFlags) { |
| 1357 | assert(!MI.isBundled() && "Can't handle bundled instructions yet."); |
| 1358 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
| 1359 | Indexes->removeMachineInstrFromMaps(MI); |
| 1360 | SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); |
| 1361 | assert(getMBBStartIdx(MI.getParent()) <= OldIndex && |
| 1362 | OldIndex < getMBBEndIdx(MI.getParent()) && |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1363 | "Cannot handle moves across basic block boundaries."); |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1364 | |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1365 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1366 | HME.updateAllRanges(&MI); |
Lang Hames | d6e765c | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1369 | void LiveIntervals::handleMoveIntoBundle(MachineInstr &MI, |
| 1370 | MachineInstr &BundleStart, |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1371 | bool UpdateFlags) { |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1372 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
| 1373 | SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart); |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1374 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1375 | HME.updateAllRanges(&MI); |
Lang Hames | b9057d5 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1376 | } |
Cameron Zwarich | bfebb41 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1377 | |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1378 | void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin, |
| 1379 | const MachineBasicBlock::iterator End, |
| 1380 | const SlotIndex endIdx, |
| 1381 | LiveRange &LR, const unsigned Reg, |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1382 | LaneBitmask LaneMask) { |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1383 | LiveInterval::iterator LII = LR.find(endIdx); |
| 1384 | SlotIndex lastUseIdx; |
Nicolai Haehnle | 02d7841 | 2016-08-10 18:51:14 +0000 | [diff] [blame] | 1385 | if (LII == LR.begin()) { |
| 1386 | // This happens when the function is called for a subregister that only |
| 1387 | // occurs _after_ the range that is to be repaired. |
| 1388 | return; |
| 1389 | } |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1390 | if (LII != LR.end() && LII->start < endIdx) |
| 1391 | lastUseIdx = LII->end; |
| 1392 | else |
| 1393 | --LII; |
| 1394 | |
| 1395 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
| 1396 | --I; |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1397 | MachineInstr &MI = *I; |
| 1398 | if (MI.isDebugValue()) |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1399 | continue; |
| 1400 | |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1401 | SlotIndex instrIdx = getInstructionIndex(MI); |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1402 | bool isStartValid = getInstructionFromIndex(LII->start); |
| 1403 | bool isEndValid = getInstructionFromIndex(LII->end); |
| 1404 | |
| 1405 | // FIXME: This doesn't currently handle early-clobber or multiple removed |
| 1406 | // defs inside of the region to repair. |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1407 | for (MachineInstr::mop_iterator OI = MI.operands_begin(), |
| 1408 | OE = MI.operands_end(); |
| 1409 | OI != OE; ++OI) { |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1410 | const MachineOperand &MO = *OI; |
| 1411 | if (!MO.isReg() || MO.getReg() != Reg) |
| 1412 | continue; |
| 1413 | |
| 1414 | unsigned SubReg = MO.getSubReg(); |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1415 | LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg); |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1416 | if ((Mask & LaneMask).none()) |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1417 | continue; |
| 1418 | |
| 1419 | if (MO.isDef()) { |
| 1420 | if (!isStartValid) { |
| 1421 | if (LII->end.isDead()) { |
| 1422 | SlotIndex prevStart; |
| 1423 | if (LII != LR.begin()) |
| 1424 | prevStart = std::prev(LII)->start; |
| 1425 | |
| 1426 | // FIXME: This could be more efficient if there was a |
| 1427 | // removeSegment method that returned an iterator. |
| 1428 | LR.removeSegment(*LII, true); |
| 1429 | if (prevStart.isValid()) |
| 1430 | LII = LR.find(prevStart); |
| 1431 | else |
| 1432 | LII = LR.begin(); |
| 1433 | } else { |
| 1434 | LII->start = instrIdx.getRegSlot(); |
| 1435 | LII->valno->def = instrIdx.getRegSlot(); |
| 1436 | if (MO.getSubReg() && !MO.isUndef()) |
| 1437 | lastUseIdx = instrIdx.getRegSlot(); |
| 1438 | else |
| 1439 | lastUseIdx = SlotIndex(); |
| 1440 | continue; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | if (!lastUseIdx.isValid()) { |
| 1445 | VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); |
| 1446 | LiveRange::Segment S(instrIdx.getRegSlot(), |
| 1447 | instrIdx.getDeadSlot(), VNI); |
| 1448 | LII = LR.addSegment(S); |
| 1449 | } else if (LII->start != instrIdx.getRegSlot()) { |
| 1450 | VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); |
| 1451 | LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI); |
| 1452 | LII = LR.addSegment(S); |
| 1453 | } |
| 1454 | |
| 1455 | if (MO.getSubReg() && !MO.isUndef()) |
| 1456 | lastUseIdx = instrIdx.getRegSlot(); |
| 1457 | else |
| 1458 | lastUseIdx = SlotIndex(); |
| 1459 | } else if (MO.isUse()) { |
| 1460 | // FIXME: This should probably be handled outside of this branch, |
| 1461 | // either as part of the def case (for defs inside of the region) or |
| 1462 | // after the loop over the region. |
| 1463 | if (!isEndValid && !LII->end.isBlock()) |
| 1464 | LII->end = instrIdx.getRegSlot(); |
| 1465 | if (!lastUseIdx.isValid()) |
| 1466 | lastUseIdx = instrIdx.getRegSlot(); |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
Cameron Zwarich | bfebb41 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1472 | void |
| 1473 | LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB, |
Cameron Zwarich | 2495596 | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1474 | MachineBasicBlock::iterator Begin, |
| 1475 | MachineBasicBlock::iterator End, |
Cameron Zwarich | 1286ef9 | 2013-02-17 03:48:23 +0000 | [diff] [blame] | 1476 | ArrayRef<unsigned> OrigRegs) { |
Cameron Zwarich | caad7e1 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1477 | // Find anchor points, which are at the beginning/end of blocks or at |
| 1478 | // instructions that already have indexes. |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1479 | while (Begin != MBB->begin() && !Indexes->hasIndex(*Begin)) |
Cameron Zwarich | caad7e1 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1480 | --Begin; |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1481 | while (End != MBB->end() && !Indexes->hasIndex(*End)) |
Cameron Zwarich | caad7e1 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1482 | ++End; |
| 1483 | |
Cameron Zwarich | 8e60d4d | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1484 | SlotIndex endIdx; |
| 1485 | if (End == MBB->end()) |
| 1486 | endIdx = getMBBEndIdx(MBB).getPrevSlot(); |
Cameron Zwarich | 2495596 | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1487 | else |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1488 | endIdx = getInstructionIndex(*End); |
Cameron Zwarich | 2495596 | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1489 | |
Hal Finkel | 7b1b3da | 2016-05-21 16:03:50 +0000 | [diff] [blame] | 1490 | Indexes->repairIndexesInRange(MBB, Begin, End); |
Cameron Zwarich | 2941482 | 2013-02-20 06:46:41 +0000 | [diff] [blame] | 1491 | |
Cameron Zwarich | 8e60d4d | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1492 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
| 1493 | --I; |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1494 | MachineInstr &MI = *I; |
| 1495 | if (MI.isDebugValue()) |
Cameron Zwarich | 63acc73 | 2013-02-23 10:25:25 +0000 | [diff] [blame] | 1496 | continue; |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1497 | for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(), |
| 1498 | MOE = MI.operands_end(); |
| 1499 | MOI != MOE; ++MOI) { |
Cameron Zwarich | 8e60d4d | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1500 | if (MOI->isReg() && |
| 1501 | TargetRegisterInfo::isVirtualRegister(MOI->getReg()) && |
| 1502 | !hasInterval(MOI->getReg())) { |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1503 | createAndComputeVirtRegInterval(MOI->getReg()); |
Cameron Zwarich | 8e60d4d | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1508 | for (unsigned Reg : OrigRegs) { |
Cameron Zwarich | bfebb41 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1509 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 1510 | continue; |
| 1511 | |
| 1512 | LiveInterval &LI = getInterval(Reg); |
Cameron Zwarich | 8e7dc06 | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1513 | // FIXME: Should we support undefs that gain defs? |
| 1514 | if (!LI.hasAtLeastOneValue()) |
| 1515 | continue; |
| 1516 | |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1517 | for (LiveInterval::SubRange &S : LI.subranges()) |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 1518 | repairOldRegInRange(Begin, End, endIdx, S, Reg, S.LaneMask); |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1519 | |
Matthias Braun | e5f861b | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1520 | repairOldRegInRange(Begin, End, endIdx, LI, Reg); |
Cameron Zwarich | bfebb41 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1521 | } |
| 1522 | } |
Matthias Braun | cfb8ad2 | 2015-01-21 18:50:21 +0000 | [diff] [blame] | 1523 | |
| 1524 | void LiveIntervals::removePhysRegDefAt(unsigned Reg, SlotIndex Pos) { |
Matthias Braun | 9f21a8d | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1525 | for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) { |
| 1526 | if (LiveRange *LR = getCachedRegUnit(*Unit)) |
Matthias Braun | cfb8ad2 | 2015-01-21 18:50:21 +0000 | [diff] [blame] | 1527 | if (VNInfo *VNI = LR->getVNInfoAt(Pos)) |
| 1528 | LR->removeValNo(VNI); |
| 1529 | } |
| 1530 | } |
Matthias Braun | 311730a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1531 | |
| 1532 | void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) { |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1533 | // LI may not have the main range computed yet, but its subranges may |
| 1534 | // be present. |
Matthias Braun | 311730a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1535 | VNInfo *VNI = LI.getVNInfoAt(Pos); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1536 | if (VNI != nullptr) { |
| 1537 | assert(VNI->def.getBaseIndex() == Pos.getBaseIndex()); |
| 1538 | LI.removeValNo(VNI); |
| 1539 | } |
Matthias Braun | 311730a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1540 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1541 | // Also remove the value defined in subranges. |
Matthias Braun | 311730a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1542 | for (LiveInterval::SubRange &S : LI.subranges()) { |
| 1543 | if (VNInfo *SVNI = S.getVNInfoAt(Pos)) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1544 | if (SVNI->def.getBaseIndex() == Pos.getBaseIndex()) |
| 1545 | S.removeValNo(SVNI); |
Matthias Braun | 311730a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1546 | } |
| 1547 | LI.removeEmptySubRanges(); |
| 1548 | } |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1549 | |
| 1550 | void LiveIntervals::splitSeparateComponents(LiveInterval &LI, |
| 1551 | SmallVectorImpl<LiveInterval*> &SplitLIs) { |
| 1552 | ConnectedVNInfoEqClasses ConEQ(*this); |
Matthias Braun | bf47f63 | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1553 | unsigned NumComp = ConEQ.Classify(LI); |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1554 | if (NumComp <= 1) |
| 1555 | return; |
| 1556 | DEBUG(dbgs() << " Split " << NumComp << " components: " << LI << '\n'); |
| 1557 | unsigned Reg = LI.reg; |
| 1558 | const TargetRegisterClass *RegClass = MRI->getRegClass(Reg); |
| 1559 | for (unsigned I = 1; I < NumComp; ++I) { |
| 1560 | unsigned NewVReg = MRI->createVirtualRegister(RegClass); |
| 1561 | LiveInterval &NewLI = createEmptyInterval(NewVReg); |
| 1562 | SplitLIs.push_back(&NewLI); |
| 1563 | } |
| 1564 | ConEQ.Distribute(LI, SplitLIs.data(), *MRI); |
| 1565 | } |
Matthias Braun | 3907fde | 2016-01-20 00:23:21 +0000 | [diff] [blame] | 1566 | |
Matthias Braun | 71f9564 | 2016-05-20 23:14:56 +0000 | [diff] [blame] | 1567 | void LiveIntervals::constructMainRangeFromSubranges(LiveInterval &LI) { |
| 1568 | assert(LRCalc && "LRCalc not initialized."); |
| 1569 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
| 1570 | LRCalc->constructMainRangeFromSubranges(LI); |
| 1571 | } |