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