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