Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 1 | //===- LiveIntervals.cpp - Live Interval Analysis -------------------------===// |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Matthias Braun | 95a3600 | 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 | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveIntervals.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "LiveRangeCalc.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
| 20 | #include "llvm/ADT/DepthFirstIterator.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/iterator_range.h" |
Dan Gohman | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveInterval.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LiveVariables.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Michael Gottesman | f392e88 | 2013-12-14 00:53:32 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunction.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineInstrBundle.h" |
| 33 | #include "llvm/CodeGen/MachineOperand.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/Passes.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 38 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/VirtRegMap.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 40 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 6463296 | 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 | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 44 | #include "llvm/Support/BlockFrequency.h" |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Eugene Zelenko | 6463296 | 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 | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 57 | using namespace llvm; |
| 58 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 59 | #define DEBUG_TYPE "regalloc" |
| 60 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 61 | char LiveIntervals::ID = 0; |
Jakob Stoklund Olesen | dcc4436 | 2012-08-03 22:12:54 +0000 | [diff] [blame] | 62 | char &llvm::LiveIntervalsID = LiveIntervals::ID; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", |
| 64 | "Live Interval Analysis", false, false) |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 65 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 66 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 67 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 68 | INITIALIZE_PASS_END(LiveIntervals, "liveintervals", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 69 | "Live Interval Analysis", false, false) |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 70 | |
Andrew Trick | c6bae79 | 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 | 4c2a2ac | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 79 | namespace llvm { |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 80 | |
Quentin Colombet | 4c2a2ac | 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 | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 85 | |
| 86 | } // end namespace llvm |
Quentin Colombet | 4c2a2ac | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 87 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 88 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 89 | AU.setPreservesCFG(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 90 | AU.addRequired<AAResultsWrapperPass>(); |
| 91 | AU.addPreserved<AAResultsWrapperPass>(); |
Evan Cheng | 148341c | 2010-08-17 21:00:37 +0000 | [diff] [blame] | 92 | AU.addPreserved<LiveVariables>(); |
Andrew Trick | d35576b | 2012-02-13 20:44:42 +0000 | [diff] [blame] | 93 | AU.addPreservedID(MachineLoopInfoID); |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 94 | AU.addRequiredTransitiveID(MachineDominatorsID); |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 95 | AU.addPreservedID(MachineDominatorsID); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 96 | AU.addPreserved<SlotIndexes>(); |
| 97 | AU.addRequiredTransitive<SlotIndexes>(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 98 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 101 | LiveIntervals::LiveIntervals() : MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | 34c6f98 | 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 | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 109 | void LiveIntervals::releaseMemory() { |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 110 | // Free the live intervals themselves. |
Jakob Stoklund Olesen | 7fa6784 | 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 | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 114 | RegMaskSlots.clear(); |
| 115 | RegMaskBits.clear(); |
Jakob Stoklund Olesen | 34e85d0 | 2012-02-10 01:26:29 +0000 | [diff] [blame] | 116 | RegMaskBlocks.clear(); |
Lang Hames | ffd1326 | 2009-07-09 03:57:02 +0000 | [diff] [blame] | 117 | |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 118 | for (LiveRange *LR : RegUnitRanges) |
| 119 | delete LR; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 120 | RegUnitRanges.clear(); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 121 | |
Benjamin Kramer | ce9a20b | 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 | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Owen Anderson | 80b3ce6 | 2008-05-28 20:54:50 +0000 | [diff] [blame] | 126 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 127 | MF = &fn; |
| 128 | MRI = &MF->getRegInfo(); |
Eric Christopher | 3788687 | 2014-10-14 06:26:53 +0000 | [diff] [blame] | 129 | TRI = MF->getSubtarget().getRegisterInfo(); |
| 130 | TII = MF->getSubtarget().getInstrInfo(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 131 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 132 | Indexes = &getAnalysis<SlotIndexes>(); |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 133 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Matthias Braun | 7fbeb8d | 2014-12-10 01:12:30 +0000 | [diff] [blame] | 134 | |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 135 | if (!LRCalc) |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 136 | LRCalc = new LiveRangeCalc(); |
Owen Anderson | 80b3ce6 | 2008-05-28 20:54:50 +0000 | [diff] [blame] | 137 | |
Jakob Stoklund Olesen | 3dfa38a | 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 | ec7b25d | 2013-02-09 00:04:07 +0000 | [diff] [blame] | 141 | computeVirtRegs(); |
| 142 | computeRegMasks(); |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 143 | computeLiveInRegUnits(); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 144 | |
Andrew Trick | c6bae79 | 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 | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 151 | LLVM_DEBUG(dump()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 152 | return true; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 155 | void LiveIntervals::print(raw_ostream &OS, const Module* ) const { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 156 | OS << "********** INTERVALS **********\n"; |
Jakob Stoklund Olesen | f658af5 | 2012-02-14 23:46:21 +0000 | [diff] [blame] | 157 | |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 158 | // Dump the regunits. |
Matthias Braun | 95a3600 | 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 | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 161 | OS << printRegUnit(Unit, TRI) << ' ' << *LR << '\n'; |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 162 | |
Jakob Stoklund Olesen | f658af5 | 2012-02-14 23:46:21 +0000 | [diff] [blame] | 163 | // Dump the virtregs. |
Jakob Stoklund Olesen | 7fa6784 | 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 | 03d9609 | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 167 | OS << getInterval(Reg) << '\n'; |
Jakob Stoklund Olesen | 7fa6784 | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 169 | |
Jakob Stoklund Olesen | 722c9a7 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 170 | OS << "RegMasks:"; |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 171 | for (SlotIndex Idx : RegMaskSlots) |
| 172 | OS << ' ' << Idx; |
Jakob Stoklund Olesen | 722c9a7 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 173 | OS << '\n'; |
| 174 | |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 175 | printInstrs(OS); |
| 176 | } |
| 177 | |
| 178 | void LiveIntervals::printInstrs(raw_ostream &OS) const { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 179 | OS << "********** MACHINEINSTRS **********\n"; |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 180 | MF->print(OS, Indexes); |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 183 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 184 | LLVM_DUMP_METHOD void LiveIntervals::dumpInstrs() const { |
David Greene | 8a34229 | 2010-01-04 22:49:02 +0000 | [diff] [blame] | 185 | printInstrs(dbgs()); |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 186 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 187 | #endif |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 188 | |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 189 | LiveInterval* LiveIntervals::createInterval(unsigned reg) { |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 190 | float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? huge_valf : 0.0F; |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 191 | return new LiveInterval(reg, Weight); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 192 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 193 | |
Matthias Braun | 95a3600 | 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 | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 195 | void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) { |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 196 | assert(LRCalc && "LRCalc not initialized."); |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 197 | assert(LI.empty() && "Should only compute empty intervals."); |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 198 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
Matthias Braun | 277501a | 2016-04-28 20:35:26 +0000 | [diff] [blame] | 199 | LRCalc->calculate(LI, MRI->shouldTrackSubRegLiveness(LI.reg)); |
| 200 | computeDeadValues(LI, nullptr); |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Jakob Stoklund Olesen | c16bf79 | 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 | e742d68 | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 208 | createAndComputeVirtRegInterval(Reg); |
Jakob Stoklund Olesen | c16bf79 | 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 | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 216 | for (const MachineBasicBlock &MBB : *MF) { |
Reid Kleckner | baea4d9 | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 217 | std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB.getNumber()]; |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 218 | RMB.first = RegMaskSlots.size(); |
Reid Kleckner | f0a04c0 | 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 | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 226 | for (const MachineInstr &MI : MBB) { |
Reid Kleckner | baea4d9 | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 227 | for (const MachineOperand &MO : MI.operands()) { |
Matthias Braun | e67bd6c | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 228 | if (!MO.isRegMask()) |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 229 | continue; |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 230 | RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot()); |
Reid Kleckner | baea4d9 | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 231 | RegMaskBits.push_back(MO.getRegMask()); |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 232 | } |
Reid Kleckner | baea4d9 | 2015-11-06 02:01:02 +0000 | [diff] [blame] | 233 | } |
Reid Kleckner | f0a04c0 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 234 | |
Reid Kleckner | 0e6b04c | 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 | f0a04c0 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 238 | if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) { |
Reid Kleckner | 0e6b04c | 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 | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 241 | Indexes->getInstructionIndex(MBB.back()).getRegSlot()); |
Reid Kleckner | f0a04c0 | 2015-11-06 17:06:38 +0000 | [diff] [blame] | 242 | RegMaskBits.push_back(Mask); |
| 243 | } |
| 244 | |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 245 | // Compute the number of register mask instructions in this block. |
Dmitri Gribenko | 2de0572 | 2012-09-10 21:26:47 +0000 | [diff] [blame] | 246 | RMB.second = RegMaskSlots.size() - RMB.first; |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 249 | |
Jakob Stoklund Olesen | 34c6f98 | 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 | 95a3600 | 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 | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 264 | void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) { |
Jakob Stoklund Olesen | 34c6f98 | 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 | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 273 | bool IsReserved = false; |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 274 | for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { |
Matthias Braun | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 275 | bool IsRootReserved = true; |
Matthias Braun | 95a3600 | 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 | 5862c98 | 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 | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 284 | IsRootReserved = false; |
Matthias Braun | 4151e89 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 285 | } |
Matthias Braun | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 286 | IsReserved |= IsRootReserved; |
Matthias Braun | 4151e89 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 287 | } |
Matthias Braun | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 288 | assert(IsReserved == MRI->isReservedRegUnit(Unit) && |
| 289 | "reserved computation mismatch"); |
Matthias Braun | 4151e89 | 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 | 5862c98 | 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 | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
Quentin Colombet | 4c2a2ac | 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 | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Matthias Braun | 95a3600 | 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 | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 312 | void LiveIntervals::computeLiveInRegUnits() { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 313 | RegUnitRanges.resize(TRI->getNumRegUnits()); |
Nicola Zaghen | 0818e78 | 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 | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 315 | |
Matthias Braun | 4f3b5e8 | 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 | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 318 | |
| 319 | // Check all basic blocks for live-ins. |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 320 | for (const MachineBasicBlock &MBB : *MF) { |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 321 | // We only care about ABI blocks: Entry + landing pads. |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 322 | if ((&MBB != &MF->front() && !MBB.isEHPad()) || MBB.livein_empty()) |
Jakob Stoklund Olesen | 34c6f98 | 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 | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 326 | SlotIndex Begin = Indexes->getMBBStartIdx(&MBB); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 327 | LLVM_DEBUG(dbgs() << Begin << "\t" << printMBBReference(MBB)); |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 328 | for (const auto &LI : MBB.liveins()) { |
Matthias Braun | af5ff60 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 329 | for (MCRegUnitIterator Units(LI.PhysReg, TRI); Units.isValid(); ++Units) { |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 330 | unsigned Unit = *Units; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 331 | LiveRange *LR = RegUnitRanges[Unit]; |
| 332 | if (!LR) { |
Quentin Colombet | 4c2a2ac | 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 | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 335 | NewRanges.push_back(Unit); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 336 | } |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 337 | VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator()); |
Matt Beaumont-Gay | 05b46f0 | 2012-06-05 23:00:03 +0000 | [diff] [blame] | 338 | (void)VNI; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 339 | LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI) << '#' << VNI->id); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 342 | LLVM_DEBUG(dbgs() << '\n'); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 343 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 344 | LLVM_DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n"); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 345 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 346 | // Compute the 'normal' part of the ranges. |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 347 | for (unsigned Unit : NewRanges) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 348 | computeRegUnitRange(*RegUnitRanges[Unit], Unit); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 351 | static void createSegmentsForValues(LiveRange &LR, |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 352 | iterator_range<LiveInterval::vni_iterator> VNIs) { |
| 353 | for (VNInfo *VNI : VNIs) { |
Matthias Braun | e59399c | 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 | 2c45bcb | 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 | e59399c | 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 | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 367 | SmallPtrSet<const MachineBasicBlock*, 16> LiveOut; |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 368 | |
Krzysztof Parzyszek | 2c45bcb | 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 | e59399c | 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 | 2c45bcb | 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 | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 392 | |
| 393 | // Extend the live range for VNI to be live at Idx. |
Krzysztof Parzyszek | 2c45bcb | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 394 | if (VNInfo *ExtVNI = Segments.extendInBlock(BlockStart, Idx)) { |
Matthias Braun | e59399c | 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 | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 402 | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 403 | if (!LiveOut.insert(Pred).second) |
| 404 | continue; |
Krzysztof Parzyszek | 2c45bcb | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 405 | SlotIndex Stop = Indexes->getMBBEndIdx(Pred); |
Matthias Braun | e59399c | 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 | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 414 | LLVM_DEBUG(dbgs() << " live-in at " << BlockStart << '\n'); |
Krzysztof Parzyszek | 2c45bcb | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 415 | Segments.addSegment(LiveRange::Segment(BlockStart, Idx, VNI)); |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 416 | |
| 417 | // Make sure VNI is live-out from the predecessors. |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 418 | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 419 | if (!LiveOut.insert(Pred).second) |
| 420 | continue; |
Krzysztof Parzyszek | 2c45bcb | 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"); |
Krzysztof Parzyszek | e873939 | 2018-06-26 14:55:04 +0000 | [diff] [blame] | 424 | (void)OldVNI; |
Krzysztof Parzyszek | 2c45bcb | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 425 | WorkList.push_back(std::make_pair(Stop, VNI)); |
| 426 | } else { |
| 427 | #ifndef NDEBUG |
| 428 | // There was no old VNI. Verify that Stop is jointly dominated |
| 429 | // by <undef>s for this live range. |
| 430 | assert(LaneMask.any() && |
| 431 | "Missing value out of predecessor for main range"); |
| 432 | SmallVector<SlotIndex,8> Undefs; |
| 433 | LI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes); |
| 434 | assert(LiveRangeCalc::isJointlyDominated(Pred, Undefs, *Indexes) && |
| 435 | "Missing value out of predecessor for subrange"); |
| 436 | #endif |
| 437 | } |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 442 | bool LiveIntervals::shrinkToUses(LiveInterval *li, |
Jakob Stoklund Olesen | 0d8ccaa | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 443 | SmallVectorImpl<MachineInstr*> *dead) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 444 | LLVM_DEBUG(dbgs() << "Shrink: " << *li << '\n'); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 445 | assert(TargetRegisterInfo::isVirtualRegister(li->reg) |
Lang Hames | 567cdba | 2012-01-03 20:05:57 +0000 | [diff] [blame] | 446 | && "Can only shrink virtual registers"); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 447 | |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 448 | // Shrink subregister live ranges. |
Matthias Braun | 0219a27 | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 449 | bool NeedsCleanup = false; |
Matthias Braun | 1bfcc2d | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 450 | for (LiveInterval::SubRange &S : li->subranges()) { |
| 451 | shrinkToUses(S, li->reg); |
Matthias Braun | 0219a27 | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 452 | if (S.empty()) |
| 453 | NeedsCleanup = true; |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 454 | } |
Matthias Braun | 0219a27 | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 455 | if (NeedsCleanup) |
| 456 | li->removeEmptySubRanges(); |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 457 | |
| 458 | // Find all the values used, including PHI kills. |
| 459 | ShrinkToUsesWorkList WorkList; |
Jakob Stoklund Olesen | 031432f | 2011-09-15 15:24:16 +0000 | [diff] [blame] | 460 | |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 461 | // Visit all instructions reading li->reg. |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 462 | unsigned Reg = li->reg; |
| 463 | for (MachineInstr &UseMI : MRI->reg_instructions(Reg)) { |
| 464 | if (UseMI.isDebugValue() || !UseMI.readsVirtualRegister(Reg)) |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 465 | continue; |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 466 | SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); |
Matthias Braun | 5649e25 | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 467 | LiveQueryResult LRQ = li->Query(Idx); |
Jakob Stoklund Olesen | 97769fc | 2012-05-20 02:54:52 +0000 | [diff] [blame] | 468 | VNInfo *VNI = LRQ.valueIn(); |
Jakob Stoklund Olesen | 9ef931e | 2011-03-18 03:06:04 +0000 | [diff] [blame] | 469 | if (!VNI) { |
| 470 | // This shouldn't happen: readsVirtualRegister returns true, but there is |
| 471 | // no live value. It is likely caused by a target getting <undef> flags |
| 472 | // wrong. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 473 | LLVM_DEBUG( |
| 474 | dbgs() << Idx << '\t' << UseMI |
| 475 | << "Warning: Instr claims to read non-existent value in " |
| 476 | << *li << '\n'); |
Jakob Stoklund Olesen | 9ef931e | 2011-03-18 03:06:04 +0000 | [diff] [blame] | 477 | continue; |
| 478 | } |
Jakob Stoklund Olesen | f054e19 | 2011-11-14 18:45:38 +0000 | [diff] [blame] | 479 | // Special case: An early-clobber tied operand reads and writes the |
Jakob Stoklund Olesen | 97769fc | 2012-05-20 02:54:52 +0000 | [diff] [blame] | 480 | // register one slot early. |
| 481 | if (VNInfo *DefVNI = LRQ.valueDefined()) |
| 482 | Idx = DefVNI->def; |
| 483 | |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 484 | WorkList.push_back(std::make_pair(Idx, VNI)); |
| 485 | } |
| 486 | |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 487 | // Create new live ranges with only minimal live segments per def. |
| 488 | LiveRange NewLR; |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 489 | createSegmentsForValues(NewLR, make_range(li->vni_begin(), li->vni_end())); |
Krzysztof Parzyszek | 2c45bcb | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 490 | extendSegmentsToUses(NewLR, WorkList, Reg, LaneBitmask::getNone()); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 491 | |
Pete Cooper | dc6eaa4 | 2014-06-03 22:42:10 +0000 | [diff] [blame] | 492 | // Move the trimmed segments back. |
| 493 | li->segments.swap(NewLR.segments); |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 494 | |
| 495 | // Handle dead values. |
| 496 | bool CanSeparate = computeDeadValues(*li, dead); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 497 | LLVM_DEBUG(dbgs() << "Shrunk: " << *li << '\n'); |
Pete Cooper | dc6eaa4 | 2014-06-03 22:42:10 +0000 | [diff] [blame] | 498 | return CanSeparate; |
| 499 | } |
| 500 | |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 501 | bool LiveIntervals::computeDeadValues(LiveInterval &LI, |
Pete Cooper | dc6eaa4 | 2014-06-03 22:42:10 +0000 | [diff] [blame] | 502 | SmallVectorImpl<MachineInstr*> *dead) { |
Matthias Braun | 6404850 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 503 | bool MayHaveSplitComponents = false; |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 504 | for (VNInfo *VNI : LI.valnos) { |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 505 | if (VNI->isUnused()) |
| 506 | continue; |
Matthias Braun | 9a43e3d | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 507 | SlotIndex Def = VNI->def; |
| 508 | LiveRange::iterator I = LI.FindSegmentContaining(Def); |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 509 | assert(I != LI.end() && "Missing segment for VNI"); |
Matthias Braun | 9a43e3d | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 510 | |
| 511 | // Is the register live before? Otherwise we may have to add a read-undef |
| 512 | // flag for subregister defs. |
Matthias Braun | 6404850 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 513 | unsigned VReg = LI.reg; |
| 514 | if (MRI->shouldTrackSubRegLiveness(VReg)) { |
Matthias Braun | 9a43e3d | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 515 | if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) { |
| 516 | MachineInstr *MI = getInstructionFromIndex(Def); |
Matthias Braun | f98fd35 | 2015-11-11 00:41:58 +0000 | [diff] [blame] | 517 | MI->setRegisterDefReadUndef(VReg); |
Matthias Braun | 9a43e3d | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | if (I->end != Def.getDeadSlot()) |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 522 | continue; |
Jakob Stoklund Olesen | a4d3473 | 2011-03-02 00:33:01 +0000 | [diff] [blame] | 523 | if (VNI->isPHIDef()) { |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 524 | // This is a dead PHI. Remove it. |
Jakob Stoklund Olesen | b2beac2 | 2012-08-03 20:59:32 +0000 | [diff] [blame] | 525 | VNI->markUnused(); |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 526 | LI.removeSegment(I); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 527 | LLVM_DEBUG(dbgs() << "Dead PHI at " << Def << " may separate interval\n"); |
Matthias Braun | 6404850 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 528 | MayHaveSplitComponents = true; |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 529 | } else { |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 530 | // This is a dead def. Make sure the instruction knows. |
Matthias Braun | 9a43e3d | 2015-01-21 22:55:13 +0000 | [diff] [blame] | 531 | MachineInstr *MI = getInstructionFromIndex(Def); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 532 | assert(MI && "No instruction defining live value"); |
Matthias Braun | 277501a | 2016-04-28 20:35:26 +0000 | [diff] [blame] | 533 | MI->addRegisterDead(LI.reg, TRI); |
Jakob Stoklund Olesen | 0d8ccaa | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 534 | if (dead && MI->allDefsAreDead()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 535 | LLVM_DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI); |
Jakob Stoklund Olesen | 0d8ccaa | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 536 | dead->push_back(MI); |
| 537 | } |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
Matthias Braun | 6404850 | 2015-09-22 22:37:44 +0000 | [diff] [blame] | 540 | return MayHaveSplitComponents; |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 543 | void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, unsigned Reg) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 544 | LLVM_DEBUG(dbgs() << "Shrink: " << SR << '\n'); |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 545 | assert(TargetRegisterInfo::isVirtualRegister(Reg) |
| 546 | && "Can only shrink virtual registers"); |
| 547 | // Find all the values used, including PHI kills. |
| 548 | ShrinkToUsesWorkList WorkList; |
| 549 | |
| 550 | // Visit all instructions reading Reg. |
| 551 | SlotIndex LastIdx; |
Krzysztof Parzyszek | 57c58d3 | 2016-09-02 19:48:55 +0000 | [diff] [blame] | 552 | for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { |
| 553 | // Skip "undef" uses. |
| 554 | if (!MO.readsReg()) |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 555 | continue; |
| 556 | // Maybe the operand is for a subregister we don't care about. |
| 557 | unsigned SubReg = MO.getSubReg(); |
| 558 | if (SubReg != 0) { |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 559 | LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg); |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 560 | if ((LaneMask & SR.LaneMask).none()) |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 561 | continue; |
| 562 | } |
| 563 | // We only need to visit each instruction once. |
Krzysztof Parzyszek | 57c58d3 | 2016-09-02 19:48:55 +0000 | [diff] [blame] | 564 | MachineInstr *UseMI = MO.getParent(); |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 565 | SlotIndex Idx = getInstructionIndex(*UseMI).getRegSlot(); |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 566 | if (Idx == LastIdx) |
| 567 | continue; |
| 568 | LastIdx = Idx; |
| 569 | |
| 570 | LiveQueryResult LRQ = SR.Query(Idx); |
| 571 | VNInfo *VNI = LRQ.valueIn(); |
| 572 | // For Subranges it is possible that only undef values are left in that |
| 573 | // part of the subregister, so there is no real liverange at the use |
| 574 | if (!VNI) |
| 575 | continue; |
| 576 | |
| 577 | // Special case: An early-clobber tied operand reads and writes the |
| 578 | // register one slot early. |
| 579 | if (VNInfo *DefVNI = LRQ.valueDefined()) |
| 580 | Idx = DefVNI->def; |
| 581 | |
| 582 | WorkList.push_back(std::make_pair(Idx, VNI)); |
| 583 | } |
| 584 | |
| 585 | // Create a new live ranges with only minimal live segments per def. |
| 586 | LiveRange NewLR; |
| 587 | createSegmentsForValues(NewLR, make_range(SR.vni_begin(), SR.vni_end())); |
Krzysztof Parzyszek | 2c45bcb | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 588 | extendSegmentsToUses(NewLR, WorkList, Reg, SR.LaneMask); |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 589 | |
Matthias Braun | e59399c | 2014-12-10 01:12:18 +0000 | [diff] [blame] | 590 | // Move the trimmed ranges back. |
| 591 | SR.segments.swap(NewLR.segments); |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 592 | |
| 593 | // Remove dead PHI value numbers |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 594 | for (VNInfo *VNI : SR.valnos) { |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 595 | if (VNI->isUnused()) |
| 596 | continue; |
| 597 | const LiveRange::Segment *Segment = SR.getSegmentContaining(VNI->def); |
| 598 | assert(Segment != nullptr && "Missing segment for VNI"); |
| 599 | if (Segment->end != VNI->def.getDeadSlot()) |
| 600 | continue; |
| 601 | if (VNI->isPHIDef()) { |
| 602 | // This is a dead PHI. Remove it. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 603 | LLVM_DEBUG(dbgs() << "Dead PHI at " << VNI->def |
| 604 | << " may separate interval\n"); |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 605 | VNI->markUnused(); |
| 606 | SR.removeSegment(*Segment); |
Matthias Braun | b82636b | 2014-12-18 19:58:52 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 610 | LLVM_DEBUG(dbgs() << "Shrunk: " << SR << '\n'); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 613 | void LiveIntervals::extendToIndices(LiveRange &LR, |
Krzysztof Parzyszek | e0daa1e | 2016-09-01 12:10:36 +0000 | [diff] [blame] | 614 | ArrayRef<SlotIndex> Indices, |
| 615 | ArrayRef<SlotIndex> Undefs) { |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 616 | assert(LRCalc && "LRCalc not initialized."); |
| 617 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 618 | for (SlotIndex Idx : Indices) |
| 619 | LRCalc->extend(LR, Idx, /*PhysReg=*/0, Undefs); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 622 | void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill, |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 623 | SmallVectorImpl<SlotIndex> *EndPoints) { |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 624 | LiveQueryResult LRQ = LR.Query(Kill); |
| 625 | VNInfo *VNI = LRQ.valueOutOrDead(); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 626 | if (!VNI) |
| 627 | return; |
| 628 | |
| 629 | MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill); |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 630 | SlotIndex MBBEnd = Indexes->getMBBEndIdx(KillMBB); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 631 | |
| 632 | // If VNI isn't live out from KillMBB, the value is trivially pruned. |
| 633 | if (LRQ.endPoint() < MBBEnd) { |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 634 | LR.removeSegment(Kill, LRQ.endPoint()); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 635 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | // VNI is live out of KillMBB. |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 640 | LR.removeSegment(Kill, MBBEnd); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 641 | if (EndPoints) EndPoints->push_back(MBBEnd); |
| 642 | |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 643 | // Find all blocks that are reachable from KillMBB without leaving VNI's live |
| 644 | // range. It is possible that KillMBB itself is reachable, so start a DFS |
| 645 | // from each successor. |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 646 | using VisitedTy = df_iterator_default_set<MachineBasicBlock*,9>; |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 647 | VisitedTy Visited; |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 648 | for (MachineBasicBlock *Succ : KillMBB->successors()) { |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 649 | for (df_ext_iterator<MachineBasicBlock*, VisitedTy> |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 650 | I = df_ext_begin(Succ, Visited), E = df_ext_end(Succ, Visited); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 651 | I != E;) { |
| 652 | MachineBasicBlock *MBB = *I; |
| 653 | |
| 654 | // Check if VNI is live in to MBB. |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 655 | SlotIndex MBBStart, MBBEnd; |
Benjamin Kramer | a4f0aad | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 656 | std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB); |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 657 | LiveQueryResult LRQ = LR.Query(MBBStart); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 658 | if (LRQ.valueIn() != VNI) { |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 659 | // This block isn't part of the VNI segment. Prune the search. |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 660 | I.skipChildren(); |
| 661 | continue; |
| 662 | } |
| 663 | |
| 664 | // Prune the search if VNI is killed in MBB. |
| 665 | if (LRQ.endPoint() < MBBEnd) { |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 666 | LR.removeSegment(MBBStart, LRQ.endPoint()); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 667 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
| 668 | I.skipChildren(); |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | // VNI is live through MBB. |
Matthias Braun | 6e616d2 | 2014-12-10 01:12:36 +0000 | [diff] [blame] | 673 | LR.removeSegment(MBBStart, MBBEnd); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 674 | if (EndPoints) EndPoints->push_back(MBBEnd); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 675 | ++I; |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 676 | } |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 679 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 680 | //===----------------------------------------------------------------------===// |
| 681 | // Register allocator hooks. |
| 682 | // |
| 683 | |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 684 | void LiveIntervals::addKillFlags(const VirtRegMap *VRM) { |
| 685 | // Keep track of regunit ranges. |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 686 | SmallVector<std::pair<const LiveRange*, LiveRange::const_iterator>, 8> RU; |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 687 | // Keep track of subregister ranges. |
| 688 | SmallVector<std::pair<const LiveInterval::SubRange*, |
| 689 | LiveRange::const_iterator>, 4> SRs; |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 690 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 691 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 692 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 693 | if (MRI->reg_nodbg_empty(Reg)) |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 694 | continue; |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 695 | const LiveInterval &LI = getInterval(Reg); |
| 696 | if (LI.empty()) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 697 | continue; |
| 698 | |
| 699 | // Find the regunit intervals for the assigned register. They may overlap |
| 700 | // the virtual register live range, cancelling any kills. |
| 701 | RU.clear(); |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 702 | for (MCRegUnitIterator Unit(VRM->getPhys(Reg), TRI); Unit.isValid(); |
| 703 | ++Unit) { |
| 704 | const LiveRange &RURange = getRegUnit(*Unit); |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 705 | if (RURange.empty()) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 706 | continue; |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 707 | RU.push_back(std::make_pair(&RURange, RURange.find(LI.begin()->end))); |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 708 | } |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 709 | |
Matthias Braun | 5101c89 | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 710 | if (MRI->subRegLivenessEnabled()) { |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 711 | SRs.clear(); |
| 712 | for (const LiveInterval::SubRange &SR : LI.subranges()) { |
| 713 | SRs.push_back(std::make_pair(&SR, SR.find(LI.begin()->end))); |
| 714 | } |
| 715 | } |
| 716 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 717 | // Every instruction that kills Reg corresponds to a segment range end |
| 718 | // point. |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 719 | for (LiveInterval::const_iterator RI = LI.begin(), RE = LI.end(); RI != RE; |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 720 | ++RI) { |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 721 | // A block index indicates an MBB edge. |
| 722 | if (RI->end.isBlock()) |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 723 | continue; |
| 724 | MachineInstr *MI = getInstructionFromIndex(RI->end); |
| 725 | if (!MI) |
| 726 | continue; |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 727 | |
Matthias Braun | b1aa5e4 | 2013-10-04 16:52:58 +0000 | [diff] [blame] | 728 | // Check if any of the regunits are live beyond the end of RI. That could |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 729 | // happen when a physreg is defined as a copy of a virtreg: |
| 730 | // |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 731 | // %eax = COPY %5 |
| 732 | // FOO %5 <--- MI, cancel kill because %eax is live. |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 733 | // BAR killed %eax |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 734 | // |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 735 | // There should be no kill flag on FOO when %5 is rewritten as %eax. |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 736 | for (auto &RUP : RU) { |
| 737 | const LiveRange &RURange = *RUP.first; |
Matthias Braun | 94daece | 2014-12-24 02:11:43 +0000 | [diff] [blame] | 738 | LiveRange::const_iterator &I = RUP.second; |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 739 | if (I == RURange.end()) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 740 | continue; |
Matthias Braun | 1f6bcf1 | 2014-12-20 01:54:48 +0000 | [diff] [blame] | 741 | I = RURange.advanceTo(I, RI->end); |
| 742 | if (I == RURange.end() || I->start >= RI->end) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 743 | continue; |
| 744 | // I is overlapping RI. |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 745 | goto CancelKill; |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 746 | } |
Matthias Braun | 6681324 | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 747 | |
Matthias Braun | 5101c89 | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 748 | if (MRI->subRegLivenessEnabled()) { |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 749 | // When reading a partial undefined value we must not add a kill flag. |
| 750 | // The regalloc might have used the undef lane for something else. |
| 751 | // Example: |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 752 | // %1 = ... ; R32: %1 |
| 753 | // %2:high16 = ... ; R64: %2 |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 754 | // = read killed %2 ; R64: %2 |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 755 | // = read %1 ; R32: %1 |
| 756 | // The <kill> flag is correct for %2, but the register allocator may |
| 757 | // assign R0L to %1, and R0 to %2 because the low 32bits of R0 |
| 758 | // are actually never written by %2. After assignment the <kill> |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 759 | // flag at the read instruction is invalid. |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 760 | LaneBitmask DefinedLanesMask; |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 761 | if (!SRs.empty()) { |
| 762 | // Compute a mask of lanes that are defined. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 763 | DefinedLanesMask = LaneBitmask::getNone(); |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 764 | for (auto &SRP : SRs) { |
| 765 | const LiveInterval::SubRange &SR = *SRP.first; |
Matthias Braun | 94daece | 2014-12-24 02:11:43 +0000 | [diff] [blame] | 766 | LiveRange::const_iterator &I = SRP.second; |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 767 | if (I == SR.end()) |
| 768 | continue; |
| 769 | I = SR.advanceTo(I, RI->end); |
| 770 | if (I == SR.end() || I->start >= RI->end) |
| 771 | continue; |
| 772 | // I is overlapping RI |
| 773 | DefinedLanesMask |= SR.LaneMask; |
Matthias Braun | 6681324 | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 774 | } |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 775 | } else |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 776 | DefinedLanesMask = LaneBitmask::getAll(); |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 777 | |
| 778 | bool IsFullWrite = false; |
| 779 | for (const MachineOperand &MO : MI->operands()) { |
| 780 | if (!MO.isReg() || MO.getReg() != Reg) |
| 781 | continue; |
| 782 | if (MO.isUse()) { |
| 783 | // Reading any undefined lanes? |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 784 | LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg()); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 785 | if ((UseMask & ~DefinedLanesMask).any()) |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 786 | goto CancelKill; |
| 787 | } else if (MO.getSubReg() == 0) { |
| 788 | // Writing to the full register? |
| 789 | assert(MO.isDef()); |
| 790 | IsFullWrite = true; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // If an instruction writes to a subregister, a new segment starts in |
| 795 | // the LiveInterval. But as this is only overriding part of the register |
| 796 | // adding kill-flags is not correct here after registers have been |
| 797 | // assigned. |
| 798 | if (!IsFullWrite) { |
| 799 | // Next segment has to be adjacent in the subregister write case. |
| 800 | LiveRange::const_iterator N = std::next(RI); |
| 801 | if (N != LI.end() && N->start == RI->end) |
| 802 | goto CancelKill; |
Matthias Braun | 6681324 | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Matthias Braun | 4acc514 | 2014-12-20 01:54:50 +0000 | [diff] [blame] | 806 | MI->addRegisterKilled(Reg, nullptr); |
| 807 | continue; |
| 808 | CancelKill: |
| 809 | MI->clearRegisterKills(Reg, nullptr); |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 814 | MachineBasicBlock* |
| 815 | LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const { |
| 816 | // A local live range must be fully contained inside the block, meaning it is |
| 817 | // defined and killed at instructions, not at block boundaries. It is not |
Hiroshi Inoue | ef1bc2d | 2018-04-12 05:53:20 +0000 | [diff] [blame] | 818 | // live in or out of any block. |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 819 | // |
| 820 | // It is technically possible to have a PHI-defined live range identical to a |
| 821 | // single block, but we are going to return false in that case. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 822 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 823 | SlotIndex Start = LI.beginIndex(); |
| 824 | if (Start.isBlock()) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 825 | return nullptr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 826 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 827 | SlotIndex Stop = LI.endIndex(); |
| 828 | if (Stop.isBlock()) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 829 | return nullptr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 830 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 831 | // getMBBFromIndex doesn't need to search the MBB table when both indexes |
| 832 | // belong to proper instructions. |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 833 | MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start); |
| 834 | MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 835 | return MBB1 == MBB2 ? MBB1 : nullptr; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Jakob Stoklund Olesen | 0ab7103 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 838 | bool |
| 839 | LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const { |
Matthias Braun | 218d20a | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 840 | for (const VNInfo *PHI : LI.valnos) { |
Jakob Stoklund Olesen | 0ab7103 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 841 | if (PHI->isUnused() || !PHI->isPHIDef()) |
| 842 | continue; |
| 843 | const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def); |
| 844 | // Conservatively return true instead of scanning huge predecessor lists. |
| 845 | if (PHIMBB->pred_size() > 100) |
| 846 | return true; |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 847 | for (const MachineBasicBlock *Pred : PHIMBB->predecessors()) |
| 848 | if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(Pred))) |
Jakob Stoklund Olesen | 0ab7103 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | return false; |
| 852 | } |
| 853 | |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 854 | float LiveIntervals::getSpillWeight(bool isDef, bool isUse, |
| 855 | const MachineBlockFrequencyInfo *MBFI, |
| 856 | const MachineInstr &MI) { |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 857 | return getSpillWeight(isDef, isUse, MBFI, MI.getParent()); |
| 858 | } |
| 859 | |
| 860 | float LiveIntervals::getSpillWeight(bool isDef, bool isUse, |
| 861 | const MachineBlockFrequencyInfo *MBFI, |
| 862 | const MachineBasicBlock *MBB) { |
| 863 | BlockFrequency Freq = MBFI->getBlockFreq(MBB); |
Michael Gottesman | 523823b | 2013-12-14 02:37:38 +0000 | [diff] [blame] | 864 | const float Scale = 1.0f / MBFI->getEntryFreq(); |
Michael Gottesman | f392e88 | 2013-12-14 00:53:32 +0000 | [diff] [blame] | 865 | return (isDef + isUse) * (Freq.getFrequency() * Scale); |
Jakob Stoklund Olesen | e5d9041 | 2010-03-01 20:59:38 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 868 | LiveRange::Segment |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 869 | LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr &startInst) { |
Mark Lacey | e742d68 | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 870 | LiveInterval& Interval = createEmptyInterval(reg); |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 871 | VNInfo *VN = Interval.getNextValue( |
| 872 | SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
| 873 | getVNInfoAllocator()); |
| 874 | LiveRange::Segment S(SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
| 875 | getMBBEndIdx(startInst.getParent()), VN); |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 876 | Interval.addSegment(S); |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 877 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 878 | return S; |
Owen Anderson | c4dc132 | 2008-06-05 17:15:43 +0000 | [diff] [blame] | 879 | } |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 880 | |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 881 | //===----------------------------------------------------------------------===// |
| 882 | // Register mask functions |
| 883 | //===----------------------------------------------------------------------===// |
| 884 | |
| 885 | bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI, |
| 886 | BitVector &UsableRegs) { |
| 887 | if (LI.empty()) |
| 888 | return false; |
Jakob Stoklund Olesen | 9f10ac6 | 2012-02-10 01:31:31 +0000 | [diff] [blame] | 889 | LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end(); |
| 890 | |
| 891 | // Use a smaller arrays for local live ranges. |
| 892 | ArrayRef<SlotIndex> Slots; |
| 893 | ArrayRef<const uint32_t*> Bits; |
| 894 | if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) { |
| 895 | Slots = getRegMaskSlotsInBlock(MBB->getNumber()); |
| 896 | Bits = getRegMaskBitsInBlock(MBB->getNumber()); |
| 897 | } else { |
| 898 | Slots = getRegMaskSlots(); |
| 899 | Bits = getRegMaskBits(); |
| 900 | } |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 901 | |
| 902 | // We are going to enumerate all the register mask slots contained in LI. |
| 903 | // Start with a binary search of RegMaskSlots to find a starting point. |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 904 | ArrayRef<SlotIndex>::iterator SlotI = |
| 905 | std::lower_bound(Slots.begin(), Slots.end(), LiveI->start); |
| 906 | ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); |
| 907 | |
| 908 | // No slots in range, LI begins after the last call. |
| 909 | if (SlotI == SlotE) |
| 910 | return false; |
| 911 | |
| 912 | bool Found = false; |
Eugene Zelenko | 6463296 | 2017-05-24 23:10:29 +0000 | [diff] [blame] | 913 | while (true) { |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 914 | assert(*SlotI >= LiveI->start); |
| 915 | // Loop over all slots overlapping this segment. |
| 916 | while (*SlotI < LiveI->end) { |
| 917 | // *SlotI overlaps LI. Collect mask bits. |
| 918 | if (!Found) { |
| 919 | // This is the first overlap. Initialize UsableRegs to all ones. |
| 920 | UsableRegs.clear(); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 921 | UsableRegs.resize(TRI->getNumRegs(), true); |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 922 | Found = true; |
| 923 | } |
| 924 | // Remove usable registers clobbered by this mask. |
Jakob Stoklund Olesen | 9f10ac6 | 2012-02-10 01:31:31 +0000 | [diff] [blame] | 925 | UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]); |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 926 | if (++SlotI == SlotE) |
| 927 | return Found; |
| 928 | } |
| 929 | // *SlotI is beyond the current LI segment. |
| 930 | LiveI = LI.advanceTo(LiveI, *SlotI); |
| 931 | if (LiveI == LiveE) |
| 932 | return Found; |
| 933 | // Advance SlotI until it overlaps. |
| 934 | while (*SlotI < LiveI->start) |
| 935 | if (++SlotI == SlotE) |
| 936 | return Found; |
| 937 | } |
| 938 | } |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 939 | |
| 940 | //===----------------------------------------------------------------------===// |
| 941 | // IntervalUpdate class. |
| 942 | //===----------------------------------------------------------------------===// |
| 943 | |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 944 | /// Toolkit used by handleMove to trim or extend live intervals. |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 945 | class LiveIntervals::HMEditor { |
| 946 | private: |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 947 | LiveIntervals& LIS; |
| 948 | const MachineRegisterInfo& MRI; |
| 949 | const TargetRegisterInfo& TRI; |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 950 | SlotIndex OldIdx; |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 951 | SlotIndex NewIdx; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 952 | SmallPtrSet<LiveRange*, 8> Updated; |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 953 | bool UpdateFlags; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 954 | |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 955 | public: |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 956 | HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI, |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 957 | const TargetRegisterInfo& TRI, |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 958 | SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags) |
| 959 | : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx), |
| 960 | UpdateFlags(UpdateFlags) {} |
| 961 | |
| 962 | // FIXME: UpdateFlags is a workaround that creates live intervals for all |
| 963 | // physregs, even those that aren't needed for regalloc, in order to update |
| 964 | // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill |
| 965 | // flags, and postRA passes will use a live register utility instead. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 966 | LiveRange *getRegUnitLI(unsigned Unit) { |
Matthias Braun | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 967 | if (UpdateFlags && !MRI.isReservedRegUnit(Unit)) |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 968 | return &LIS.getRegUnit(Unit); |
| 969 | return LIS.getCachedRegUnit(Unit); |
| 970 | } |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 971 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 972 | /// Update all live ranges touched by MI, assuming a move from OldIdx to |
| 973 | /// NewIdx. |
| 974 | void updateAllRanges(MachineInstr *MI) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 975 | LLVM_DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " |
| 976 | << *MI); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 977 | bool hasRegMask = false; |
Matthias Braun | e67bd6c | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 978 | for (MachineOperand &MO : MI->operands()) { |
| 979 | if (MO.isRegMask()) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 980 | hasRegMask = true; |
Matthias Braun | e67bd6c | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 981 | if (!MO.isReg()) |
Lang Hames | 4586d25 | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 982 | continue; |
Matthias Braun | 3783c29 | 2016-05-06 21:47:41 +0000 | [diff] [blame] | 983 | if (MO.isUse()) { |
| 984 | if (!MO.readsReg()) |
| 985 | continue; |
| 986 | // Aggressively clear all kill flags. |
| 987 | // They are reinserted by VirtRegRewriter. |
Matthias Braun | e67bd6c | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 988 | MO.setIsKill(false); |
Matthias Braun | 3783c29 | 2016-05-06 21:47:41 +0000 | [diff] [blame] | 989 | } |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 990 | |
Matthias Braun | e67bd6c | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 991 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 992 | if (!Reg) |
| 993 | continue; |
| 994 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 995 | LiveInterval &LI = LIS.getInterval(Reg); |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 996 | if (LI.hasSubRanges()) { |
Matthias Braun | e67bd6c | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 997 | unsigned SubReg = MO.getSubReg(); |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 998 | LaneBitmask LaneMask = SubReg ? TRI.getSubRegIndexLaneMask(SubReg) |
| 999 | : MRI.getMaxLaneMaskForVReg(Reg); |
Matthias Braun | 1bfcc2d | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 1000 | for (LiveInterval::SubRange &S : LI.subranges()) { |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1001 | if ((S.LaneMask & LaneMask).none()) |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1002 | continue; |
Matthias Braun | 1bfcc2d | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 1003 | updateRange(S, Reg, S.LaneMask); |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1006 | updateRange(LI, Reg, LaneBitmask::getNone()); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1007 | continue; |
| 1008 | } |
| 1009 | |
| 1010 | // For physregs, only update the regunits that actually have a |
| 1011 | // precomputed live range. |
| 1012 | for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1013 | if (LiveRange *LR = getRegUnitLI(*Units)) |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1014 | updateRange(*LR, *Units, LaneBitmask::getNone()); |
Lang Hames | 4586d25 | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 1015 | } |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1016 | if (hasRegMask) |
| 1017 | updateRegMaskSlots(); |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1020 | private: |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1021 | /// Update a single live range, assuming an instruction has been moved from |
| 1022 | /// OldIdx to NewIdx. |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1023 | void updateRange(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) { |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1024 | if (!Updated.insert(&LR).second) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1025 | return; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1026 | LLVM_DEBUG({ |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1027 | dbgs() << " "; |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1028 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1029 | dbgs() << printReg(Reg); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1030 | if (LaneMask.any()) |
Matthias Braun | 86ac1df | 2015-09-25 21:51:24 +0000 | [diff] [blame] | 1031 | dbgs() << " L" << PrintLaneMask(LaneMask); |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1032 | } else { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1033 | dbgs() << printRegUnit(Reg, &TRI); |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1034 | } |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1035 | dbgs() << ":\t" << LR << '\n'; |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1036 | }); |
| 1037 | if (SlotIndex::isEarlierInstr(OldIdx, NewIdx)) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1038 | handleMoveDown(LR); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1039 | else |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1040 | handleMoveUp(LR, Reg, LaneMask); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1041 | LLVM_DEBUG(dbgs() << " -->\t" << LR << '\n'); |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1042 | LR.verify(); |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1045 | /// Update LR to reflect an instruction has been moved downwards from OldIdx |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1046 | /// to NewIdx (OldIdx < NewIdx). |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1047 | void handleMoveDown(LiveRange &LR) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1048 | LiveRange::iterator E = LR.end(); |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1049 | // Segment going into OldIdx. |
| 1050 | LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); |
| 1051 | |
| 1052 | // No value live before or after OldIdx? Nothing to do. |
| 1053 | if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1054 | return; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1055 | |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1056 | LiveRange::iterator OldIdxOut; |
| 1057 | // Do we have a value live-in to OldIdx? |
| 1058 | if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1059 | // If the live-in value already extends to NewIdx, there is nothing to do. |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1060 | if (SlotIndex::isEarlierEqualInstr(NewIdx, OldIdxIn->end)) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | // Aggressively remove all kill flags from the old kill point. |
| 1063 | // Kill flags shouldn't be used while live intervals exist, they will be |
| 1064 | // reinserted by VirtRegRewriter. |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1065 | if (MachineInstr *KillMI = LIS.getInstructionFromIndex(OldIdxIn->end)) |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1066 | for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1067 | if (MO->isReg() && MO->isUse()) |
| 1068 | MO->setIsKill(false); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1069 | |
| 1070 | // Is there a def before NewIdx which is not OldIdx? |
| 1071 | LiveRange::iterator Next = std::next(OldIdxIn); |
| 1072 | if (Next != E && !SlotIndex::isSameInstr(OldIdx, Next->start) && |
| 1073 | SlotIndex::isEarlierInstr(Next->start, NewIdx)) { |
| 1074 | // If we are here then OldIdx was just a use but not a def. We only have |
| 1075 | // to ensure liveness extends to NewIdx. |
| 1076 | LiveRange::iterator NewIdxIn = |
| 1077 | LR.advanceTo(Next, NewIdx.getBaseIndex()); |
| 1078 | // Extend the segment before NewIdx if necessary. |
| 1079 | if (NewIdxIn == E || |
| 1080 | !SlotIndex::isEarlierInstr(NewIdxIn->start, NewIdx)) { |
| 1081 | LiveRange::iterator Prev = std::prev(NewIdxIn); |
| 1082 | Prev->end = NewIdx.getRegSlot(); |
| 1083 | } |
Matthias Braun | bb936d2 | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 1084 | // Extend OldIdxIn. |
| 1085 | OldIdxIn->end = Next->start; |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | } |
| 1088 | |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1089 | // Adjust OldIdxIn->end to reach NewIdx. This may temporarily make LR |
Matthias Braun | 632580f | 2016-01-26 01:40:48 +0000 | [diff] [blame] | 1090 | // invalid by overlapping ranges. |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1091 | bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); |
| 1092 | OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber()); |
| 1093 | // If this was not a kill, then there was no def and we're done. |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1094 | if (!isKill) |
| 1095 | return; |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Did we have a Def at OldIdx? |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1098 | OldIdxOut = Next; |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1099 | if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) |
| 1100 | return; |
| 1101 | } else { |
| 1102 | OldIdxOut = OldIdxIn; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1105 | // If we are here then there is a Definition at OldIdx. OldIdxOut points |
| 1106 | // to the segment starting there. |
| 1107 | assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && |
| 1108 | "No def?"); |
| 1109 | VNInfo *OldIdxVNI = OldIdxOut->valno; |
| 1110 | assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); |
| 1111 | |
| 1112 | // If the defined value extends beyond NewIdx, just move the beginning |
| 1113 | // of the segment to NewIdx. |
| 1114 | SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); |
| 1115 | if (SlotIndex::isEarlierInstr(NewIdxDef, OldIdxOut->end)) { |
| 1116 | OldIdxVNI->def = NewIdxDef; |
| 1117 | OldIdxOut->start = OldIdxVNI->def; |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1118 | return; |
| 1119 | } |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1120 | |
| 1121 | // If we are here then we have a Definition at OldIdx which ends before |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1122 | // NewIdx. |
| 1123 | |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1124 | // Is there an existing Def at NewIdx? |
| 1125 | LiveRange::iterator AfterNewIdx |
| 1126 | = LR.advanceTo(OldIdxOut, NewIdx.getRegSlot()); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1127 | bool OldIdxDefIsDead = OldIdxOut->end.isDead(); |
| 1128 | if (!OldIdxDefIsDead && |
| 1129 | SlotIndex::isEarlierInstr(OldIdxOut->end, NewIdxDef)) { |
| 1130 | // OldIdx is not a dead def, and NewIdxDef is inside a new interval. |
| 1131 | VNInfo *DefVNI; |
| 1132 | if (OldIdxOut != LR.begin() && |
| 1133 | !SlotIndex::isEarlierInstr(std::prev(OldIdxOut)->end, |
| 1134 | OldIdxOut->start)) { |
| 1135 | // There is no gap between OldIdxOut and its predecessor anymore, |
| 1136 | // merge them. |
| 1137 | LiveRange::iterator IPrev = std::prev(OldIdxOut); |
| 1138 | DefVNI = OldIdxVNI; |
| 1139 | IPrev->end = OldIdxOut->end; |
| 1140 | } else { |
| 1141 | // The value is live in to OldIdx |
| 1142 | LiveRange::iterator INext = std::next(OldIdxOut); |
| 1143 | assert(INext != E && "Must have following segment"); |
| 1144 | // We merge OldIdxOut and its successor. As we're dealing with subreg |
| 1145 | // reordering, there is always a successor to OldIdxOut in the same BB |
| 1146 | // We don't need INext->valno anymore and will reuse for the new segment |
| 1147 | // we create later. |
Matthias Braun | 289718f | 2016-04-28 02:11:49 +0000 | [diff] [blame] | 1148 | DefVNI = OldIdxVNI; |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1149 | INext->start = OldIdxOut->end; |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1150 | INext->valno->def = INext->start; |
| 1151 | } |
| 1152 | // If NewIdx is behind the last segment, extend that and append a new one. |
| 1153 | if (AfterNewIdx == E) { |
| 1154 | // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up |
| 1155 | // one position. |
| 1156 | // |- ?/OldIdxOut -| |- X0 -| ... |- Xn -| end |
| 1157 | // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS -| end |
| 1158 | std::copy(std::next(OldIdxOut), E, OldIdxOut); |
| 1159 | // The last segment is undefined now, reuse it for a dead def. |
| 1160 | LiveRange::iterator NewSegment = std::prev(E); |
| 1161 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
| 1162 | DefVNI); |
| 1163 | DefVNI->def = NewIdxDef; |
| 1164 | |
| 1165 | LiveRange::iterator Prev = std::prev(NewSegment); |
| 1166 | Prev->end = NewIdxDef; |
| 1167 | } else { |
| 1168 | // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up |
| 1169 | // one position. |
| 1170 | // |- ?/OldIdxOut -| |- X0 -| ... |- Xn/AfterNewIdx -| |- Next -| |
| 1171 | // => |- X0/OldIdxOut -| ... |- Xn -| |- Xn/AfterNewIdx -| |- Next -| |
| 1172 | std::copy(std::next(OldIdxOut), std::next(AfterNewIdx), OldIdxOut); |
| 1173 | LiveRange::iterator Prev = std::prev(AfterNewIdx); |
| 1174 | // We have two cases: |
| 1175 | if (SlotIndex::isEarlierInstr(Prev->start, NewIdxDef)) { |
| 1176 | // Case 1: NewIdx is inside a liverange. Split this liverange at |
| 1177 | // NewIdxDef into the segment "Prev" followed by "NewSegment". |
| 1178 | LiveRange::iterator NewSegment = AfterNewIdx; |
| 1179 | *NewSegment = LiveRange::Segment(NewIdxDef, Prev->end, Prev->valno); |
| 1180 | Prev->valno->def = NewIdxDef; |
| 1181 | |
| 1182 | *Prev = LiveRange::Segment(Prev->start, NewIdxDef, DefVNI); |
| 1183 | DefVNI->def = Prev->start; |
| 1184 | } else { |
| 1185 | // Case 2: NewIdx is in a lifetime hole. Keep AfterNewIdx as is and |
| 1186 | // turn Prev into a segment from NewIdx to AfterNewIdx->start. |
| 1187 | *Prev = LiveRange::Segment(NewIdxDef, AfterNewIdx->start, DefVNI); |
| 1188 | DefVNI->def = NewIdxDef; |
| 1189 | assert(DefVNI != AfterNewIdx->valno); |
| 1190 | } |
| 1191 | } |
| 1192 | return; |
| 1193 | } |
| 1194 | |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1195 | if (AfterNewIdx != E && |
| 1196 | SlotIndex::isSameInstr(AfterNewIdx->start, NewIdxDef)) { |
| 1197 | // There is an existing def at NewIdx. The def at OldIdx is coalesced into |
| 1198 | // that value. |
| 1199 | assert(AfterNewIdx->valno != OldIdxVNI && "Multiple defs of value?"); |
| 1200 | LR.removeValNo(OldIdxVNI); |
| 1201 | } else { |
| 1202 | // There was no existing def at NewIdx. We need to create a dead def |
| 1203 | // at NewIdx. Shift segments over the old OldIdxOut segment, this frees |
| 1204 | // a new segment at the place where we want to construct the dead def. |
| 1205 | // |- OldIdxOut -| |- X0 -| ... |- Xn -| |- AfterNewIdx -| |
| 1206 | // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS. -| |- AfterNewIdx -| |
| 1207 | assert(AfterNewIdx != OldIdxOut && "Inconsistent iterators"); |
| 1208 | std::copy(std::next(OldIdxOut), AfterNewIdx, OldIdxOut); |
| 1209 | // We can reuse OldIdxVNI now. |
| 1210 | LiveRange::iterator NewSegment = std::prev(AfterNewIdx); |
| 1211 | VNInfo *NewSegmentVNI = OldIdxVNI; |
| 1212 | NewSegmentVNI->def = NewIdxDef; |
| 1213 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
| 1214 | NewSegmentVNI); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1215 | } |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1218 | /// Update LR to reflect an instruction has been moved upwards from OldIdx |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1219 | /// to NewIdx (NewIdx < OldIdx). |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1220 | void handleMoveUp(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1221 | LiveRange::iterator E = LR.end(); |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1222 | // Segment going into OldIdx. |
| 1223 | LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); |
| 1224 | |
| 1225 | // No value live before or after OldIdx? Nothing to do. |
| 1226 | if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1227 | return; |
| 1228 | |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1229 | LiveRange::iterator OldIdxOut; |
| 1230 | // Do we have a value live-in to OldIdx? |
| 1231 | if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { |
| 1232 | // If the live-in value isn't killed here, then we have no Def at |
| 1233 | // OldIdx, moreover the value must be live at NewIdx so there is nothing |
| 1234 | // to do. |
| 1235 | bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); |
| 1236 | if (!isKill) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1237 | return; |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1238 | |
| 1239 | // At this point we have to move OldIdxIn->end back to the nearest |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1240 | // previous use or (dead-)def but no further than NewIdx. |
| 1241 | SlotIndex DefBeforeOldIdx |
| 1242 | = std::max(OldIdxIn->start.getDeadSlot(), |
| 1243 | NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber())); |
| 1244 | OldIdxIn->end = findLastUseBefore(DefBeforeOldIdx, Reg, LaneMask); |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1245 | |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1246 | // Did we have a Def at OldIdx? If not we are done now. |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1247 | OldIdxOut = std::next(OldIdxIn); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1248 | if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1249 | return; |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1250 | } else { |
| 1251 | OldIdxOut = OldIdxIn; |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1252 | OldIdxIn = OldIdxOut != LR.begin() ? std::prev(OldIdxOut) : E; |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | // If we are here then there is a Definition at OldIdx. OldIdxOut points |
| 1256 | // to the segment starting there. |
| 1257 | assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && |
| 1258 | "No def?"); |
| 1259 | VNInfo *OldIdxVNI = OldIdxOut->valno; |
| 1260 | assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); |
| 1261 | bool OldIdxDefIsDead = OldIdxOut->end.isDead(); |
| 1262 | |
| 1263 | // Is there an existing def at NewIdx? |
| 1264 | SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); |
| 1265 | LiveRange::iterator NewIdxOut = LR.find(NewIdx.getRegSlot()); |
| 1266 | if (SlotIndex::isSameInstr(NewIdxOut->start, NewIdx)) { |
| 1267 | assert(NewIdxOut->valno != OldIdxVNI && |
| 1268 | "Same value defined more than once?"); |
| 1269 | // If OldIdx was a dead def remove it. |
| 1270 | if (!OldIdxDefIsDead) { |
Matthias Braun | 632580f | 2016-01-26 01:40:48 +0000 | [diff] [blame] | 1271 | // Remove segment starting at NewIdx and move begin of OldIdxOut to |
| 1272 | // NewIdx so it can take its place. |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1273 | OldIdxVNI->def = NewIdxDef; |
| 1274 | OldIdxOut->start = NewIdxDef; |
| 1275 | LR.removeValNo(NewIdxOut->valno); |
| 1276 | } else { |
Matthias Braun | 632580f | 2016-01-26 01:40:48 +0000 | [diff] [blame] | 1277 | // Simply remove the dead def at OldIdx. |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1278 | LR.removeValNo(OldIdxVNI); |
| 1279 | } |
| 1280 | } else { |
| 1281 | // Previously nothing was live after NewIdx, so all we have to do now is |
| 1282 | // move the begin of OldIdxOut to NewIdx. |
| 1283 | if (!OldIdxDefIsDead) { |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1284 | // Do we have any intermediate Defs between OldIdx and NewIdx? |
| 1285 | if (OldIdxIn != E && |
| 1286 | SlotIndex::isEarlierInstr(NewIdxDef, OldIdxIn->start)) { |
| 1287 | // OldIdx is not a dead def and NewIdx is before predecessor start. |
| 1288 | LiveRange::iterator NewIdxIn = NewIdxOut; |
| 1289 | assert(NewIdxIn == LR.find(NewIdx.getBaseIndex())); |
| 1290 | const SlotIndex SplitPos = NewIdxDef; |
Stanislav Mekhanoshin | f99709a | 2017-03-11 00:14:52 +0000 | [diff] [blame] | 1291 | OldIdxVNI = OldIdxIn->valno; |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Merge the OldIdxIn and OldIdxOut segments into OldIdxOut. |
Stanislav Mekhanoshin | f99709a | 2017-03-11 00:14:52 +0000 | [diff] [blame] | 1294 | OldIdxOut->valno->def = OldIdxIn->start; |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1295 | *OldIdxOut = LiveRange::Segment(OldIdxIn->start, OldIdxOut->end, |
Stanislav Mekhanoshin | f99709a | 2017-03-11 00:14:52 +0000 | [diff] [blame] | 1296 | OldIdxOut->valno); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1297 | // OldIdxIn and OldIdxVNI are now undef and can be overridden. |
| 1298 | // We Slide [NewIdxIn, OldIdxIn) down one position. |
| 1299 | // |- X0/NewIdxIn -| ... |- Xn-1 -||- Xn/OldIdxIn -||- OldIdxOut -| |
| 1300 | // => |- undef/NexIdxIn -| |- X0 -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |
| 1301 | std::copy_backward(NewIdxIn, OldIdxIn, OldIdxOut); |
| 1302 | // NewIdxIn is now considered undef so we can reuse it for the moved |
| 1303 | // value. |
| 1304 | LiveRange::iterator NewSegment = NewIdxIn; |
| 1305 | LiveRange::iterator Next = std::next(NewSegment); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1306 | if (SlotIndex::isEarlierInstr(Next->start, NewIdx)) { |
| 1307 | // There is no gap between NewSegment and its predecessor. |
| 1308 | *NewSegment = LiveRange::Segment(Next->start, SplitPos, |
Matthias Braun | 6063d9d | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 1309 | Next->valno); |
| 1310 | *Next = LiveRange::Segment(SplitPos, Next->end, OldIdxVNI); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1311 | Next->valno->def = SplitPos; |
| 1312 | } else { |
| 1313 | // There is a gap between NewSegment and its predecessor |
| 1314 | // Value becomes live in. |
Matthias Braun | 6063d9d | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 1315 | *NewSegment = LiveRange::Segment(SplitPos, Next->start, OldIdxVNI); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1316 | NewSegment->valno->def = SplitPos; |
| 1317 | } |
| 1318 | } else { |
| 1319 | // Leave the end point of a live def. |
| 1320 | OldIdxOut->start = NewIdxDef; |
| 1321 | OldIdxVNI->def = NewIdxDef; |
| 1322 | if (OldIdxIn != E && SlotIndex::isEarlierInstr(NewIdx, OldIdxIn->end)) |
| 1323 | OldIdxIn->end = NewIdx.getRegSlot(); |
| 1324 | } |
Tim Renouf | 27467f8 | 2018-02-26 14:42:13 +0000 | [diff] [blame] | 1325 | } else if (OldIdxIn != E |
| 1326 | && SlotIndex::isEarlierInstr(NewIdxOut->start, NewIdx) |
| 1327 | && SlotIndex::isEarlierInstr(NewIdx, NewIdxOut->end)) { |
| 1328 | // OldIdxVNI is a dead def that has been moved into the middle of |
| 1329 | // another value in LR. That can happen when LR is a whole register, |
| 1330 | // but the dead def is a write to a subreg that is dead at NewIdx. |
| 1331 | // The dead def may have been moved across other values |
| 1332 | // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) |
| 1333 | // down one position. |
| 1334 | // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | |
| 1335 | // => |- X0/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| |
| 1336 | std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); |
| 1337 | // Modify the segment at NewIdxOut and the following segment to meet at |
| 1338 | // the point of the dead def, with the following segment getting |
| 1339 | // OldIdxVNI as its value number. |
| 1340 | *NewIdxOut = LiveRange::Segment( |
| 1341 | NewIdxOut->start, NewIdxDef.getRegSlot(), NewIdxOut->valno); |
| 1342 | *(NewIdxOut + 1) = LiveRange::Segment( |
| 1343 | NewIdxDef.getRegSlot(), (NewIdxOut + 1)->end, OldIdxVNI); |
| 1344 | OldIdxVNI->def = NewIdxDef; |
| 1345 | // Modify subsequent segments to be defined by the moved def OldIdxVNI. |
| 1346 | for (auto Idx = NewIdxOut + 2; Idx <= OldIdxOut; ++Idx) |
| 1347 | Idx->valno = OldIdxVNI; |
| 1348 | // Aggressively remove all dead flags from the former dead definition. |
| 1349 | // Kill/dead flags shouldn't be used while live intervals exist; they |
| 1350 | // will be reinserted by VirtRegRewriter. |
| 1351 | if (MachineInstr *KillMI = LIS.getInstructionFromIndex(NewIdx)) |
| 1352 | for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO) |
| 1353 | if (MO->isReg() && !MO->isUse()) |
| 1354 | MO->setIsDead(false); |
Matthias Braun | 1b9e9d8 | 2016-01-26 00:43:50 +0000 | [diff] [blame] | 1355 | } else { |
| 1356 | // OldIdxVNI is a dead def. It may have been moved across other values |
| 1357 | // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) |
| 1358 | // down one position. |
| 1359 | // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | |
| 1360 | // => |- undef/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| |
| 1361 | std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); |
| 1362 | // OldIdxVNI can be reused now to build a new dead def segment. |
| 1363 | LiveRange::iterator NewSegment = NewIdxOut; |
| 1364 | VNInfo *NewSegmentVNI = OldIdxVNI; |
| 1365 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
| 1366 | NewSegmentVNI); |
| 1367 | NewSegmentVNI->def = NewIdxDef; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1368 | } |
| 1369 | } |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1372 | void updateRegMaskSlots() { |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 1373 | SmallVectorImpl<SlotIndex>::iterator RI = |
| 1374 | std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(), |
| 1375 | OldIdx); |
Jakob Stoklund Olesen | 722c9a7 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 1376 | assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() && |
| 1377 | "No RegMask at OldIdx."); |
| 1378 | *RI = NewIdx.getRegSlot(); |
| 1379 | assert((RI == LIS.RegMaskSlots.begin() || |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1380 | SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) && |
| 1381 | "Cannot move regmask instruction above another call"); |
| 1382 | assert((std::next(RI) == LIS.RegMaskSlots.end() || |
| 1383 | SlotIndex::isEarlierInstr(*RI, *std::next(RI))) && |
| 1384 | "Cannot move regmask instruction below another call"); |
Lang Hames | fbc8dd3 | 2012-02-17 21:29:41 +0000 | [diff] [blame] | 1385 | } |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Return the last use of reg between NewIdx and OldIdx. |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1388 | SlotIndex findLastUseBefore(SlotIndex Before, unsigned Reg, |
| 1389 | LaneBitmask LaneMask) { |
Lang Hames | 6d742cc | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 1390 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1391 | SlotIndex LastUse = Before; |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1392 | for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) { |
Matthias Braun | b788f1e | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 1393 | if (MO.isUndef()) |
| 1394 | continue; |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1395 | unsigned SubReg = MO.getSubReg(); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1396 | if (SubReg != 0 && LaneMask.any() |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1397 | && (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask).none()) |
Matthias Braun | dc08729 | 2014-12-10 01:12:20 +0000 | [diff] [blame] | 1398 | continue; |
| 1399 | |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1400 | const MachineInstr &MI = *MO.getParent(); |
Lang Hames | 6d742cc | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 1401 | SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); |
| 1402 | if (InstSlot > LastUse && InstSlot < OldIdx) |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1403 | LastUse = InstSlot.getRegSlot(); |
Lang Hames | 6d742cc | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 1404 | } |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1405 | return LastUse; |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1406 | } |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1407 | |
| 1408 | // This is a regunit interval, so scanning the use list could be very |
| 1409 | // expensive. Scan upwards from OldIdx instead. |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1410 | assert(Before < OldIdx && "Expected upwards move"); |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1411 | SlotIndexes *Indexes = LIS.getSlotIndexes(); |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1412 | MachineBasicBlock *MBB = Indexes->getMBBFromIndex(Before); |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1413 | |
| 1414 | // OldIdx may not correspond to an instruction any longer, so set MII to |
| 1415 | // point to the next instruction after OldIdx, or MBB->end(). |
| 1416 | MachineBasicBlock::iterator MII = MBB->end(); |
| 1417 | if (MachineInstr *MI = Indexes->getInstructionFromIndex( |
| 1418 | Indexes->getNextNonNullIndex(OldIdx))) |
| 1419 | if (MI->getParent() == MBB) |
| 1420 | MII = MI; |
| 1421 | |
| 1422 | MachineBasicBlock::iterator Begin = MBB->begin(); |
| 1423 | while (MII != Begin) { |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 1424 | if ((--MII)->isDebugInstr()) |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1425 | continue; |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1426 | SlotIndex Idx = Indexes->getInstructionIndex(*MII); |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1427 | |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1428 | // Stop searching when Before is reached. |
| 1429 | if (!SlotIndex::isEarlierInstr(Before, Idx)) |
| 1430 | return Before; |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Check if MII uses Reg. |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1433 | for (MIBundleOperands MO(*MII); MO.isValid(); ++MO) |
Matthias Braun | b788f1e | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 1434 | if (MO->isReg() && !MO->isUndef() && |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1435 | TargetRegisterInfo::isPhysicalRegister(MO->getReg()) && |
| 1436 | TRI.hasRegUnit(MO->getReg(), Reg)) |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1437 | return Idx.getRegSlot(); |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 1438 | } |
Matthias Braun | 372250b | 2016-02-15 19:25:36 +0000 | [diff] [blame] | 1439 | // Didn't reach Before. It must be the first instruction in the block. |
| 1440 | return Before; |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1441 | } |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1442 | }; |
| 1443 | |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1444 | void LiveIntervals::handleMove(MachineInstr &MI, bool UpdateFlags) { |
| 1445 | assert(!MI.isBundled() && "Can't handle bundled instructions yet."); |
| 1446 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
| 1447 | Indexes->removeMachineInstrFromMaps(MI); |
| 1448 | SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); |
| 1449 | assert(getMBBStartIdx(MI.getParent()) <= OldIndex && |
| 1450 | OldIndex < getMBBEndIdx(MI.getParent()) && |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1451 | "Cannot handle moves across basic block boundaries."); |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1452 | |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1453 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1454 | HME.updateAllRanges(&MI); |
Lang Hames | 4586d25 | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1457 | void LiveIntervals::handleMoveIntoBundle(MachineInstr &MI, |
| 1458 | MachineInstr &BundleStart, |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1459 | bool UpdateFlags) { |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1460 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
| 1461 | SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart); |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1462 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1463 | HME.updateAllRanges(&MI); |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1464 | } |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1465 | |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1466 | void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin, |
| 1467 | const MachineBasicBlock::iterator End, |
| 1468 | const SlotIndex endIdx, |
| 1469 | LiveRange &LR, const unsigned Reg, |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1470 | LaneBitmask LaneMask) { |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1471 | LiveInterval::iterator LII = LR.find(endIdx); |
| 1472 | SlotIndex lastUseIdx; |
Nicolai Haehnle | be7124c | 2016-08-10 18:51:14 +0000 | [diff] [blame] | 1473 | if (LII == LR.begin()) { |
| 1474 | // This happens when the function is called for a subregister that only |
| 1475 | // occurs _after_ the range that is to be repaired. |
| 1476 | return; |
| 1477 | } |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1478 | if (LII != LR.end() && LII->start < endIdx) |
| 1479 | lastUseIdx = LII->end; |
| 1480 | else |
| 1481 | --LII; |
| 1482 | |
| 1483 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
| 1484 | --I; |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1485 | MachineInstr &MI = *I; |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 1486 | if (MI.isDebugInstr()) |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1487 | continue; |
| 1488 | |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1489 | SlotIndex instrIdx = getInstructionIndex(MI); |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1490 | bool isStartValid = getInstructionFromIndex(LII->start); |
| 1491 | bool isEndValid = getInstructionFromIndex(LII->end); |
| 1492 | |
| 1493 | // FIXME: This doesn't currently handle early-clobber or multiple removed |
| 1494 | // defs inside of the region to repair. |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1495 | for (MachineInstr::mop_iterator OI = MI.operands_begin(), |
| 1496 | OE = MI.operands_end(); |
| 1497 | OI != OE; ++OI) { |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1498 | const MachineOperand &MO = *OI; |
| 1499 | if (!MO.isReg() || MO.getReg() != Reg) |
| 1500 | continue; |
| 1501 | |
| 1502 | unsigned SubReg = MO.getSubReg(); |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1503 | LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg); |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1504 | if ((Mask & LaneMask).none()) |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1505 | continue; |
| 1506 | |
| 1507 | if (MO.isDef()) { |
| 1508 | if (!isStartValid) { |
| 1509 | if (LII->end.isDead()) { |
| 1510 | SlotIndex prevStart; |
| 1511 | if (LII != LR.begin()) |
| 1512 | prevStart = std::prev(LII)->start; |
| 1513 | |
| 1514 | // FIXME: This could be more efficient if there was a |
| 1515 | // removeSegment method that returned an iterator. |
| 1516 | LR.removeSegment(*LII, true); |
| 1517 | if (prevStart.isValid()) |
| 1518 | LII = LR.find(prevStart); |
| 1519 | else |
| 1520 | LII = LR.begin(); |
| 1521 | } else { |
| 1522 | LII->start = instrIdx.getRegSlot(); |
| 1523 | LII->valno->def = instrIdx.getRegSlot(); |
| 1524 | if (MO.getSubReg() && !MO.isUndef()) |
| 1525 | lastUseIdx = instrIdx.getRegSlot(); |
| 1526 | else |
| 1527 | lastUseIdx = SlotIndex(); |
| 1528 | continue; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | if (!lastUseIdx.isValid()) { |
| 1533 | VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); |
| 1534 | LiveRange::Segment S(instrIdx.getRegSlot(), |
| 1535 | instrIdx.getDeadSlot(), VNI); |
| 1536 | LII = LR.addSegment(S); |
| 1537 | } else if (LII->start != instrIdx.getRegSlot()) { |
| 1538 | VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); |
| 1539 | LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI); |
| 1540 | LII = LR.addSegment(S); |
| 1541 | } |
| 1542 | |
| 1543 | if (MO.getSubReg() && !MO.isUndef()) |
| 1544 | lastUseIdx = instrIdx.getRegSlot(); |
| 1545 | else |
| 1546 | lastUseIdx = SlotIndex(); |
| 1547 | } else if (MO.isUse()) { |
| 1548 | // FIXME: This should probably be handled outside of this branch, |
| 1549 | // either as part of the def case (for defs inside of the region) or |
| 1550 | // after the loop over the region. |
| 1551 | if (!isEndValid && !LII->end.isBlock()) |
| 1552 | LII->end = instrIdx.getRegSlot(); |
| 1553 | if (!lastUseIdx.isValid()) |
| 1554 | lastUseIdx = instrIdx.getRegSlot(); |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1560 | void |
| 1561 | LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB, |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1562 | MachineBasicBlock::iterator Begin, |
| 1563 | MachineBasicBlock::iterator End, |
Cameron Zwarich | 7324d4e | 2013-02-17 03:48:23 +0000 | [diff] [blame] | 1564 | ArrayRef<unsigned> OrigRegs) { |
Cameron Zwarich | c5b6135 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1565 | // Find anchor points, which are at the beginning/end of blocks or at |
| 1566 | // instructions that already have indexes. |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1567 | while (Begin != MBB->begin() && !Indexes->hasIndex(*Begin)) |
Cameron Zwarich | c5b6135 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1568 | --Begin; |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1569 | while (End != MBB->end() && !Indexes->hasIndex(*End)) |
Cameron Zwarich | c5b6135 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1570 | ++End; |
| 1571 | |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1572 | SlotIndex endIdx; |
| 1573 | if (End == MBB->end()) |
| 1574 | endIdx = getMBBEndIdx(MBB).getPrevSlot(); |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1575 | else |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1576 | endIdx = getInstructionIndex(*End); |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1577 | |
Hal Finkel | 8fdeacc | 2016-05-21 16:03:50 +0000 | [diff] [blame] | 1578 | Indexes->repairIndexesInRange(MBB, Begin, End); |
Cameron Zwarich | 349cf34 | 2013-02-20 06:46:41 +0000 | [diff] [blame] | 1579 | |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1580 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
| 1581 | --I; |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1582 | MachineInstr &MI = *I; |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 1583 | if (MI.isDebugInstr()) |
Cameron Zwarich | 79f5ab1 | 2013-02-23 10:25:25 +0000 | [diff] [blame] | 1584 | continue; |
Duncan P. N. Exon Smith | 5144d35 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 1585 | for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(), |
| 1586 | MOE = MI.operands_end(); |
| 1587 | MOI != MOE; ++MOI) { |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1588 | if (MOI->isReg() && |
| 1589 | TargetRegisterInfo::isVirtualRegister(MOI->getReg()) && |
| 1590 | !hasInterval(MOI->getReg())) { |
Mark Lacey | e742d68 | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1591 | createAndComputeVirtRegInterval(MOI->getReg()); |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1596 | for (unsigned Reg : OrigRegs) { |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1597 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 1598 | continue; |
| 1599 | |
| 1600 | LiveInterval &LI = getInterval(Reg); |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1601 | // FIXME: Should we support undefs that gain defs? |
| 1602 | if (!LI.hasAtLeastOneValue()) |
| 1603 | continue; |
| 1604 | |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1605 | for (LiveInterval::SubRange &S : LI.subranges()) |
Matthias Braun | 1bfcc2d | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 1606 | repairOldRegInRange(Begin, End, endIdx, S, Reg, S.LaneMask); |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1607 | |
Matthias Braun | 4402447 | 2014-12-10 01:12:26 +0000 | [diff] [blame] | 1608 | repairOldRegInRange(Begin, End, endIdx, LI, Reg); |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1609 | } |
| 1610 | } |
Matthias Braun | 1458e05 | 2015-01-21 18:50:21 +0000 | [diff] [blame] | 1611 | |
| 1612 | void LiveIntervals::removePhysRegDefAt(unsigned Reg, SlotIndex Pos) { |
Matthias Braun | 95a3600 | 2017-01-19 00:32:13 +0000 | [diff] [blame] | 1613 | for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) { |
| 1614 | if (LiveRange *LR = getCachedRegUnit(*Unit)) |
Matthias Braun | 1458e05 | 2015-01-21 18:50:21 +0000 | [diff] [blame] | 1615 | if (VNInfo *VNI = LR->getVNInfoAt(Pos)) |
| 1616 | LR->removeValNo(VNI); |
| 1617 | } |
| 1618 | } |
Matthias Braun | 7d3ec5a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1619 | |
| 1620 | void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) { |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1621 | // LI may not have the main range computed yet, but its subranges may |
| 1622 | // be present. |
Matthias Braun | 7d3ec5a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1623 | VNInfo *VNI = LI.getVNInfoAt(Pos); |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1624 | if (VNI != nullptr) { |
| 1625 | assert(VNI->def.getBaseIndex() == Pos.getBaseIndex()); |
| 1626 | LI.removeValNo(VNI); |
| 1627 | } |
Matthias Braun | 7d3ec5a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1628 | |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1629 | // Also remove the value defined in subranges. |
Matthias Braun | 7d3ec5a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1630 | for (LiveInterval::SubRange &S : LI.subranges()) { |
| 1631 | if (VNInfo *SVNI = S.getVNInfoAt(Pos)) |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 1632 | if (SVNI->def.getBaseIndex() == Pos.getBaseIndex()) |
| 1633 | S.removeValNo(SVNI); |
Matthias Braun | 7d3ec5a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 1634 | } |
| 1635 | LI.removeEmptySubRanges(); |
| 1636 | } |
Matthias Braun | 95e05dd | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1637 | |
| 1638 | void LiveIntervals::splitSeparateComponents(LiveInterval &LI, |
| 1639 | SmallVectorImpl<LiveInterval*> &SplitLIs) { |
| 1640 | ConnectedVNInfoEqClasses ConEQ(*this); |
Matthias Braun | dcaeedf | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1641 | unsigned NumComp = ConEQ.Classify(LI); |
Matthias Braun | 95e05dd | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1642 | if (NumComp <= 1) |
| 1643 | return; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1644 | LLVM_DEBUG(dbgs() << " Split " << NumComp << " components: " << LI << '\n'); |
Matthias Braun | 95e05dd | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1645 | unsigned Reg = LI.reg; |
| 1646 | const TargetRegisterClass *RegClass = MRI->getRegClass(Reg); |
| 1647 | for (unsigned I = 1; I < NumComp; ++I) { |
| 1648 | unsigned NewVReg = MRI->createVirtualRegister(RegClass); |
| 1649 | LiveInterval &NewLI = createEmptyInterval(NewVReg); |
| 1650 | SplitLIs.push_back(&NewLI); |
| 1651 | } |
| 1652 | ConEQ.Distribute(LI, SplitLIs.data(), *MRI); |
| 1653 | } |
Matthias Braun | 1c6737e | 2016-01-20 00:23:21 +0000 | [diff] [blame] | 1654 | |
Matthias Braun | 6054e84 | 2016-05-20 23:14:56 +0000 | [diff] [blame] | 1655 | void LiveIntervals::constructMainRangeFromSubranges(LiveInterval &LI) { |
| 1656 | assert(LRCalc && "LRCalc not initialized."); |
| 1657 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
| 1658 | LRCalc->constructMainRangeFromSubranges(LI); |
| 1659 | } |