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