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